[SCSI] lpfc 8.3.17: Code Cleanup and Locking fixes
[deliverable/linux.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. *
d8e93df1 4 * Copyright (C) 2004-2009 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 *);
4f774513
JS
68static IOCB_t *
69lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
70{
71 return &iocbq->iocb;
72}
73
74/**
75 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
76 * @q: The Work Queue to operate on.
77 * @wqe: The work Queue Entry to put on the Work queue.
78 *
79 * This routine will copy the contents of @wqe to the next available entry on
80 * the @q. This function will then ring the Work Queue Doorbell to signal the
81 * HBA to start processing the Work Queue Entry. This function returns 0 if
82 * successful. If no entries are available on @q then this function will return
83 * -ENOMEM.
84 * The caller is expected to hold the hbalock when calling this routine.
85 **/
86static uint32_t
87lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
88{
89 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
90 struct lpfc_register doorbell;
91 uint32_t host_index;
92
93 /* If the host has not yet processed the next entry then we are done */
94 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
95 return -ENOMEM;
96 /* set consumption flag every once in a while */
97 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
cb5172ea 215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
cb5172ea 288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
e59058c4 408/**
3621a710 409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
ed957684
JS
418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
e59058c4 425/**
3621a710 426 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
ed957684
JS
435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
e59058c4 442/**
3621a710 443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
2e0fef85
JS
451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
458
459 if (iocbq)
460 phba->iocb_cnt++;
461 if (phba->iocb_cnt > phba->iocb_max)
462 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
463 return iocbq;
464}
465
da0436e9
JS
466/**
467 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
468 * @phba: Pointer to HBA context object.
469 * @xritag: XRI value.
470 *
471 * This function clears the sglq pointer from the array of acive
472 * sglq's. The xritag that is passed in is used to index into the
473 * array. Before the xritag can be used it needs to be adjusted
474 * by subtracting the xribase.
475 *
476 * Returns sglq ponter = success, NULL = Failure.
477 **/
478static struct lpfc_sglq *
479__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
480{
481 uint16_t adj_xri;
482 struct lpfc_sglq *sglq;
483 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
484 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
485 return NULL;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
487 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
0f65ff68 503struct lpfc_sglq *
da0436e9
JS
504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
506 uint16_t adj_xri;
507 struct lpfc_sglq *sglq;
508 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
509 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
510 return NULL;
511 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
512 return sglq;
513}
514
515/**
516 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
517 * @phba: Pointer to HBA context object.
518 *
519 * This function is called with hbalock held. This function
520 * Gets a new driver sglq object from the sglq list. If the
521 * list is not empty then it is successful, it returns pointer to the newly
522 * allocated sglq object else it returns NULL.
523 **/
524static struct lpfc_sglq *
525__lpfc_sli_get_sglq(struct lpfc_hba *phba)
526{
527 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
528 struct lpfc_sglq *sglq = NULL;
529 uint16_t adj_xri;
530 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
6a9c52cf
JS
531 if (!sglq)
532 return NULL;
da0436e9
JS
533 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
534 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
0f65ff68 535 sglq->state = SGL_ALLOCATED;
da0436e9
JS
536 return sglq;
537}
538
e59058c4 539/**
3621a710 540 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
541 * @phba: Pointer to HBA context object.
542 *
543 * This function is called with no lock held. This function
544 * allocates a new driver iocb object from the iocb pool. If the
545 * allocation is successful, it returns pointer to the newly
546 * allocated iocb object else it returns NULL.
547 **/
2e0fef85
JS
548struct lpfc_iocbq *
549lpfc_sli_get_iocbq(struct lpfc_hba *phba)
550{
551 struct lpfc_iocbq * iocbq = NULL;
552 unsigned long iflags;
553
554 spin_lock_irqsave(&phba->hbalock, iflags);
555 iocbq = __lpfc_sli_get_iocbq(phba);
556 spin_unlock_irqrestore(&phba->hbalock, iflags);
557 return iocbq;
558}
559
4f774513
JS
560/**
561 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
562 * @phba: Pointer to HBA context object.
563 * @iocbq: Pointer to driver iocb object.
564 *
565 * This function is called with hbalock held to release driver
566 * iocb object to the iocb pool. The iotag in the iocb object
567 * does not change for each use of the iocb object. This function
568 * clears all other fields of the iocb object when it is freed.
569 * The sqlq structure that holds the xritag and phys and virtual
570 * mappings for the scatter gather list is retrieved from the
571 * active array of sglq. The get of the sglq pointer also clears
572 * the entry in the array. If the status of the IO indiactes that
573 * this IO was aborted then the sglq entry it put on the
574 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
575 * IO has good status or fails for any other reason then the sglq
576 * entry is added to the free list (lpfc_sgl_list).
577 **/
578static void
579__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
580{
581 struct lpfc_sglq *sglq;
582 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
583 unsigned long iflag = 0;
584 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513
JS
585
586 if (iocbq->sli4_xritag == NO_XRI)
587 sglq = NULL;
588 else
589 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590 if (sglq) {
0f65ff68
JS
591 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
592 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
593 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
594 iflag);
595 list_add(&sglq->list,
596 &phba->sli4_hba.lpfc_abts_els_sgl_list);
597 spin_unlock_irqrestore(
598 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
599 } else {
600 sglq->state = SGL_FREED;
4f774513 601 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
2a9bf3d0
JS
602
603 /* Check if TXQ queue needs to be serviced */
589a52d6 604 if (pring->txq_cnt)
2a9bf3d0 605 lpfc_worker_wake_up(phba);
0f65ff68 606 }
4f774513
JS
607 }
608
609
610 /*
611 * Clean all volatile data fields, preserve iotag and node struct.
612 */
613 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
614 iocbq->sli4_xritag = NO_XRI;
615 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
616}
617
2a9bf3d0 618
e59058c4 619/**
3772a991 620 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
621 * @phba: Pointer to HBA context object.
622 * @iocbq: Pointer to driver iocb object.
623 *
624 * This function is called with hbalock held to release driver
625 * iocb object to the iocb pool. The iotag in the iocb object
626 * does not change for each use of the iocb object. This function
627 * clears all other fields of the iocb object when it is freed.
628 **/
a6ababd2 629static void
3772a991 630__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 631{
2e0fef85 632 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
633
634 /*
635 * Clean all volatile data fields, preserve iotag and node struct.
636 */
637 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 638 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
639 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
640}
641
3772a991
JS
642/**
643 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
644 * @phba: Pointer to HBA context object.
645 * @iocbq: Pointer to driver iocb object.
646 *
647 * This function is called with hbalock held to release driver
648 * iocb object to the iocb pool. The iotag in the iocb object
649 * does not change for each use of the iocb object. This function
650 * clears all other fields of the iocb object when it is freed.
651 **/
652static void
653__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
654{
655 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 656 phba->iocb_cnt--;
3772a991
JS
657}
658
e59058c4 659/**
3621a710 660 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
661 * @phba: Pointer to HBA context object.
662 * @iocbq: Pointer to driver iocb object.
663 *
664 * This function is called with no lock held to release the iocb to
665 * iocb pool.
666 **/
2e0fef85
JS
667void
668lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
669{
670 unsigned long iflags;
671
672 /*
673 * Clean all volatile data fields, preserve iotag and node struct.
674 */
675 spin_lock_irqsave(&phba->hbalock, iflags);
676 __lpfc_sli_release_iocbq(phba, iocbq);
677 spin_unlock_irqrestore(&phba->hbalock, iflags);
678}
679
a257bf90
JS
680/**
681 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
682 * @phba: Pointer to HBA context object.
683 * @iocblist: List of IOCBs.
684 * @ulpstatus: ULP status in IOCB command field.
685 * @ulpWord4: ULP word-4 in IOCB command field.
686 *
687 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
688 * on the list by invoking the complete callback function associated with the
689 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
690 * fields.
691 **/
692void
693lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
694 uint32_t ulpstatus, uint32_t ulpWord4)
695{
696 struct lpfc_iocbq *piocb;
697
698 while (!list_empty(iocblist)) {
699 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
700
701 if (!piocb->iocb_cmpl)
702 lpfc_sli_release_iocbq(phba, piocb);
703 else {
704 piocb->iocb.ulpStatus = ulpstatus;
705 piocb->iocb.un.ulpWord[4] = ulpWord4;
706 (piocb->iocb_cmpl) (phba, piocb, piocb);
707 }
708 }
709 return;
710}
711
e59058c4 712/**
3621a710
JS
713 * lpfc_sli_iocb_cmd_type - Get the iocb type
714 * @iocb_cmnd: iocb command code.
e59058c4
JS
715 *
716 * This function is called by ring event handler function to get the iocb type.
717 * This function translates the iocb command to an iocb command type used to
718 * decide the final disposition of each completed IOCB.
719 * The function returns
720 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
721 * LPFC_SOL_IOCB if it is a solicited iocb completion
722 * LPFC_ABORT_IOCB if it is an abort iocb
723 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
724 *
725 * The caller is not required to hold any lock.
726 **/
dea3101e 727static lpfc_iocb_type
728lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
729{
730 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
731
732 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
733 return 0;
734
735 switch (iocb_cmnd) {
736 case CMD_XMIT_SEQUENCE_CR:
737 case CMD_XMIT_SEQUENCE_CX:
738 case CMD_XMIT_BCAST_CN:
739 case CMD_XMIT_BCAST_CX:
740 case CMD_ELS_REQUEST_CR:
741 case CMD_ELS_REQUEST_CX:
742 case CMD_CREATE_XRI_CR:
743 case CMD_CREATE_XRI_CX:
744 case CMD_GET_RPI_CN:
745 case CMD_XMIT_ELS_RSP_CX:
746 case CMD_GET_RPI_CR:
747 case CMD_FCP_IWRITE_CR:
748 case CMD_FCP_IWRITE_CX:
749 case CMD_FCP_IREAD_CR:
750 case CMD_FCP_IREAD_CX:
751 case CMD_FCP_ICMND_CR:
752 case CMD_FCP_ICMND_CX:
f5603511
JS
753 case CMD_FCP_TSEND_CX:
754 case CMD_FCP_TRSP_CX:
755 case CMD_FCP_TRECEIVE_CX:
756 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 757 case CMD_ADAPTER_MSG:
758 case CMD_ADAPTER_DUMP:
759 case CMD_XMIT_SEQUENCE64_CR:
760 case CMD_XMIT_SEQUENCE64_CX:
761 case CMD_XMIT_BCAST64_CN:
762 case CMD_XMIT_BCAST64_CX:
763 case CMD_ELS_REQUEST64_CR:
764 case CMD_ELS_REQUEST64_CX:
765 case CMD_FCP_IWRITE64_CR:
766 case CMD_FCP_IWRITE64_CX:
767 case CMD_FCP_IREAD64_CR:
768 case CMD_FCP_IREAD64_CX:
769 case CMD_FCP_ICMND64_CR:
770 case CMD_FCP_ICMND64_CX:
f5603511
JS
771 case CMD_FCP_TSEND64_CX:
772 case CMD_FCP_TRSP64_CX:
773 case CMD_FCP_TRECEIVE64_CX:
dea3101e 774 case CMD_GEN_REQUEST64_CR:
775 case CMD_GEN_REQUEST64_CX:
776 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
777 case DSSCMD_IWRITE64_CR:
778 case DSSCMD_IWRITE64_CX:
779 case DSSCMD_IREAD64_CR:
780 case DSSCMD_IREAD64_CX:
dea3101e 781 type = LPFC_SOL_IOCB;
782 break;
783 case CMD_ABORT_XRI_CN:
784 case CMD_ABORT_XRI_CX:
785 case CMD_CLOSE_XRI_CN:
786 case CMD_CLOSE_XRI_CX:
787 case CMD_XRI_ABORTED_CX:
788 case CMD_ABORT_MXRI64_CN:
6669f9bb 789 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 790 type = LPFC_ABORT_IOCB;
791 break;
792 case CMD_RCV_SEQUENCE_CX:
793 case CMD_RCV_ELS_REQ_CX:
794 case CMD_RCV_SEQUENCE64_CX:
795 case CMD_RCV_ELS_REQ64_CX:
57127f15 796 case CMD_ASYNC_STATUS:
ed957684
JS
797 case CMD_IOCB_RCV_SEQ64_CX:
798 case CMD_IOCB_RCV_ELS64_CX:
799 case CMD_IOCB_RCV_CONT64_CX:
3163f725 800 case CMD_IOCB_RET_XRI64_CX:
dea3101e 801 type = LPFC_UNSOL_IOCB;
802 break;
3163f725
JS
803 case CMD_IOCB_XMIT_MSEQ64_CR:
804 case CMD_IOCB_XMIT_MSEQ64_CX:
805 case CMD_IOCB_RCV_SEQ_LIST64_CX:
806 case CMD_IOCB_RCV_ELS_LIST64_CX:
807 case CMD_IOCB_CLOSE_EXTENDED_CN:
808 case CMD_IOCB_ABORT_EXTENDED_CN:
809 case CMD_IOCB_RET_HBQE64_CN:
810 case CMD_IOCB_FCP_IBIDIR64_CR:
811 case CMD_IOCB_FCP_IBIDIR64_CX:
812 case CMD_IOCB_FCP_ITASKMGT64_CX:
813 case CMD_IOCB_LOGENTRY_CN:
814 case CMD_IOCB_LOGENTRY_ASYNC_CN:
815 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 816 __func__, iocb_cmnd);
3163f725
JS
817 type = LPFC_UNKNOWN_IOCB;
818 break;
dea3101e 819 default:
820 type = LPFC_UNKNOWN_IOCB;
821 break;
822 }
823
824 return type;
825}
826
e59058c4 827/**
3621a710 828 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
829 * @phba: Pointer to HBA context object.
830 *
831 * This function is called from SLI initialization code
832 * to configure every ring of the HBA's SLI interface. The
833 * caller is not required to hold any lock. This function issues
834 * a config_ring mailbox command for each ring.
835 * This function returns zero if successful else returns a negative
836 * error code.
837 **/
dea3101e 838static int
ed957684 839lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 840{
841 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
842 LPFC_MBOXQ_t *pmb;
843 MAILBOX_t *pmbox;
844 int i, rc, ret = 0;
dea3101e 845
ed957684
JS
846 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
847 if (!pmb)
848 return -ENOMEM;
04c68496 849 pmbox = &pmb->u.mb;
ed957684 850 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 851 for (i = 0; i < psli->num_rings; i++) {
dea3101e 852 lpfc_config_ring(phba, i, pmb);
853 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
854 if (rc != MBX_SUCCESS) {
92d7f7b0 855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 856 "0446 Adapter failed to init (%d), "
dea3101e 857 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
858 "ring %d\n",
e8b62011
JS
859 rc, pmbox->mbxCommand,
860 pmbox->mbxStatus, i);
2e0fef85 861 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
862 ret = -ENXIO;
863 break;
dea3101e 864 }
865 }
ed957684
JS
866 mempool_free(pmb, phba->mbox_mem_pool);
867 return ret;
dea3101e 868}
869
e59058c4 870/**
3621a710 871 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
872 * @phba: Pointer to HBA context object.
873 * @pring: Pointer to driver SLI ring object.
874 * @piocb: Pointer to the driver iocb object.
875 *
876 * This function is called with hbalock held. The function adds the
877 * new iocb to txcmplq of the given ring. This function always returns
878 * 0. If this function is called for ELS ring, this function checks if
879 * there is a vport associated with the ELS command. This function also
880 * starts els_tmofunc timer if this is an ELS command.
881 **/
dea3101e 882static int
2e0fef85
JS
883lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
884 struct lpfc_iocbq *piocb)
dea3101e 885{
dea3101e 886 list_add_tail(&piocb->list, &pring->txcmplq);
2a9bf3d0 887 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea3101e 888 pring->txcmplq_cnt++;
2a9bf3d0
JS
889 if (pring->txcmplq_cnt > pring->txcmplq_max)
890 pring->txcmplq_max = pring->txcmplq_cnt;
891
92d7f7b0
JS
892 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
893 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
894 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
895 if (!piocb->vport)
896 BUG();
897 else
898 mod_timer(&piocb->vport->els_tmofunc,
899 jiffies + HZ * (phba->fc_ratov << 1));
900 }
901
dea3101e 902
2e0fef85 903 return 0;
dea3101e 904}
905
e59058c4 906/**
3621a710 907 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
908 * @phba: Pointer to HBA context object.
909 * @pring: Pointer to driver SLI ring object.
910 *
911 * This function is called with hbalock held to get next
912 * iocb in txq of the given ring. If there is any iocb in
913 * the txq, the function returns first iocb in the list after
914 * removing the iocb from the list, else it returns NULL.
915 **/
2a9bf3d0 916struct lpfc_iocbq *
2e0fef85 917lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 918{
dea3101e 919 struct lpfc_iocbq *cmd_iocb;
920
858c9f6c
JS
921 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
922 if (cmd_iocb != NULL)
dea3101e 923 pring->txq_cnt--;
2e0fef85 924 return cmd_iocb;
dea3101e 925}
926
e59058c4 927/**
3621a710 928 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
929 * @phba: Pointer to HBA context object.
930 * @pring: Pointer to driver SLI ring object.
931 *
932 * This function is called with hbalock held and the caller must post the
933 * iocb without releasing the lock. If the caller releases the lock,
934 * iocb slot returned by the function is not guaranteed to be available.
935 * The function returns pointer to the next available iocb slot if there
936 * is available slot in the ring, else it returns NULL.
937 * If the get index of the ring is ahead of the put index, the function
938 * will post an error attention event to the worker thread to take the
939 * HBA to offline state.
940 **/
dea3101e 941static IOCB_t *
942lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
943{
34b02dcd 944 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 945 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 946 if ((pring->next_cmdidx == pring->cmdidx) &&
947 (++pring->next_cmdidx >= max_cmd_idx))
948 pring->next_cmdidx = 0;
949
950 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
951
952 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
953
954 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 956 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 957 "is bigger than cmd ring %d\n",
e8b62011 958 pring->ringno,
dea3101e 959 pring->local_getidx, max_cmd_idx);
960
2e0fef85 961 phba->link_state = LPFC_HBA_ERROR;
dea3101e 962 /*
963 * All error attention handlers are posted to
964 * worker thread
965 */
966 phba->work_ha |= HA_ERATT;
967 phba->work_hs = HS_FFER3;
92d7f7b0 968
5e9d9b82 969 lpfc_worker_wake_up(phba);
dea3101e 970
971 return NULL;
972 }
973
974 if (pring->local_getidx == pring->next_cmdidx)
975 return NULL;
976 }
977
ed957684 978 return lpfc_cmd_iocb(phba, pring);
dea3101e 979}
980
e59058c4 981/**
3621a710 982 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
983 * @phba: Pointer to HBA context object.
984 * @iocbq: Pointer to driver iocb object.
985 *
986 * This function gets an iotag for the iocb. If there is no unused iotag and
987 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
988 * array and assigns a new iotag.
989 * The function returns the allocated iotag if successful, else returns zero.
990 * Zero is not a valid iotag.
991 * The caller is not required to hold any lock.
992 **/
604a3e30 993uint16_t
2e0fef85 994lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 995{
2e0fef85
JS
996 struct lpfc_iocbq **new_arr;
997 struct lpfc_iocbq **old_arr;
604a3e30
JB
998 size_t new_len;
999 struct lpfc_sli *psli = &phba->sli;
1000 uint16_t iotag;
dea3101e 1001
2e0fef85 1002 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1003 iotag = psli->last_iotag;
1004 if(++iotag < psli->iocbq_lookup_len) {
1005 psli->last_iotag = iotag;
1006 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1007 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1008 iocbq->iotag = iotag;
1009 return iotag;
2e0fef85 1010 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1011 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1012 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1013 spin_unlock_irq(&phba->hbalock);
1014 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1015 GFP_KERNEL);
1016 if (new_arr) {
2e0fef85 1017 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1018 old_arr = psli->iocbq_lookup;
1019 if (new_len <= psli->iocbq_lookup_len) {
1020 /* highly unprobable case */
1021 kfree(new_arr);
1022 iotag = psli->last_iotag;
1023 if(++iotag < psli->iocbq_lookup_len) {
1024 psli->last_iotag = iotag;
1025 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1026 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1027 iocbq->iotag = iotag;
1028 return iotag;
1029 }
2e0fef85 1030 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1031 return 0;
1032 }
1033 if (psli->iocbq_lookup)
1034 memcpy(new_arr, old_arr,
1035 ((psli->last_iotag + 1) *
311464ec 1036 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1037 psli->iocbq_lookup = new_arr;
1038 psli->iocbq_lookup_len = new_len;
1039 psli->last_iotag = iotag;
1040 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1041 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1042 iocbq->iotag = iotag;
1043 kfree(old_arr);
1044 return iotag;
1045 }
8f6d98d2 1046 } else
2e0fef85 1047 spin_unlock_irq(&phba->hbalock);
dea3101e 1048
bc73905a 1049 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1050 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1051 psli->last_iotag);
dea3101e 1052
604a3e30 1053 return 0;
dea3101e 1054}
1055
e59058c4 1056/**
3621a710 1057 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1058 * @phba: Pointer to HBA context object.
1059 * @pring: Pointer to driver SLI ring object.
1060 * @iocb: Pointer to iocb slot in the ring.
1061 * @nextiocb: Pointer to driver iocb object which need to be
1062 * posted to firmware.
1063 *
1064 * This function is called with hbalock held to post a new iocb to
1065 * the firmware. This function copies the new iocb to ring iocb slot and
1066 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1067 * a completion call back for this iocb else the function will free the
1068 * iocb object.
1069 **/
dea3101e 1070static void
1071lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1072 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1073{
1074 /*
604a3e30 1075 * Set up an iotag
dea3101e 1076 */
604a3e30 1077 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1078
e2a0a9d6 1079
a58cbd52
JS
1080 if (pring->ringno == LPFC_ELS_RING) {
1081 lpfc_debugfs_slow_ring_trc(phba,
1082 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1083 *(((uint32_t *) &nextiocb->iocb) + 4),
1084 *(((uint32_t *) &nextiocb->iocb) + 6),
1085 *(((uint32_t *) &nextiocb->iocb) + 7));
1086 }
1087
dea3101e 1088 /*
1089 * Issue iocb command to adapter
1090 */
92d7f7b0 1091 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1092 wmb();
1093 pring->stats.iocb_cmd++;
1094
1095 /*
1096 * If there is no completion routine to call, we can release the
1097 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1098 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1099 */
1100 if (nextiocb->iocb_cmpl)
1101 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1102 else
2e0fef85 1103 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1104
1105 /*
1106 * Let the HBA know what IOCB slot will be the next one the
1107 * driver will put a command into.
1108 */
1109 pring->cmdidx = pring->next_cmdidx;
ed957684 1110 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1111}
1112
e59058c4 1113/**
3621a710 1114 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1115 * @phba: Pointer to HBA context object.
1116 * @pring: Pointer to driver SLI ring object.
1117 *
1118 * The caller is not required to hold any lock for calling this function.
1119 * This function updates the chip attention bits for the ring to inform firmware
1120 * that there are pending work to be done for this ring and requests an
1121 * interrupt when there is space available in the ring. This function is
1122 * called when the driver is unable to post more iocbs to the ring due
1123 * to unavailability of space in the ring.
1124 **/
dea3101e 1125static void
2e0fef85 1126lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1127{
1128 int ringno = pring->ringno;
1129
1130 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1131
1132 wmb();
1133
1134 /*
1135 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1136 * The HBA will tell us when an IOCB entry is available.
1137 */
1138 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1139 readl(phba->CAregaddr); /* flush */
1140
1141 pring->stats.iocb_cmd_full++;
1142}
1143
e59058c4 1144/**
3621a710 1145 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1146 * @phba: Pointer to HBA context object.
1147 * @pring: Pointer to driver SLI ring object.
1148 *
1149 * This function updates the chip attention register bit for the
1150 * given ring to inform HBA that there is more work to be done
1151 * in this ring. The caller is not required to hold any lock.
1152 **/
dea3101e 1153static void
2e0fef85 1154lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1155{
1156 int ringno = pring->ringno;
1157
1158 /*
1159 * Tell the HBA that there is work to do in this ring.
1160 */
34b02dcd
JS
1161 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1162 wmb();
1163 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1164 readl(phba->CAregaddr); /* flush */
1165 }
dea3101e 1166}
1167
e59058c4 1168/**
3621a710 1169 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1170 * @phba: Pointer to HBA context object.
1171 * @pring: Pointer to driver SLI ring object.
1172 *
1173 * This function is called with hbalock held to post pending iocbs
1174 * in the txq to the firmware. This function is called when driver
1175 * detects space available in the ring.
1176 **/
dea3101e 1177static void
2e0fef85 1178lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1179{
1180 IOCB_t *iocb;
1181 struct lpfc_iocbq *nextiocb;
1182
1183 /*
1184 * Check to see if:
1185 * (a) there is anything on the txq to send
1186 * (b) link is up
1187 * (c) link attention events can be processed (fcp ring only)
1188 * (d) IOCB processing is not blocked by the outstanding mbox command.
1189 */
1190 if (pring->txq_cnt &&
2e0fef85 1191 lpfc_is_link_up(phba) &&
dea3101e 1192 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1193 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1194
1195 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1196 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1197 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1198
1199 if (iocb)
1200 lpfc_sli_update_ring(phba, pring);
1201 else
1202 lpfc_sli_update_full_ring(phba, pring);
1203 }
1204
1205 return;
1206}
1207
e59058c4 1208/**
3621a710 1209 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1210 * @phba: Pointer to HBA context object.
1211 * @hbqno: HBQ number.
1212 *
1213 * This function is called with hbalock held to get the next
1214 * available slot for the given HBQ. If there is free slot
1215 * available for the HBQ it will return pointer to the next available
1216 * HBQ entry else it will return NULL.
1217 **/
a6ababd2 1218static struct lpfc_hbq_entry *
ed957684
JS
1219lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1220{
1221 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1222
1223 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1224 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1225 hbqp->next_hbqPutIdx = 0;
1226
1227 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1228 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1229 uint32_t getidx = le32_to_cpu(raw_index);
1230
1231 hbqp->local_hbqGetIdx = getidx;
1232
1233 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1234 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1235 LOG_SLI | LOG_VPORT,
e8b62011 1236 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1237 "%u is > than hbqp->entry_count %u\n",
e8b62011 1238 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1239 hbqp->entry_count);
1240
1241 phba->link_state = LPFC_HBA_ERROR;
1242 return NULL;
1243 }
1244
1245 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1246 return NULL;
1247 }
1248
51ef4c26
JS
1249 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1250 hbqp->hbqPutIdx;
ed957684
JS
1251}
1252
e59058c4 1253/**
3621a710 1254 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1255 * @phba: Pointer to HBA context object.
1256 *
1257 * This function is called with no lock held to free all the
1258 * hbq buffers while uninitializing the SLI interface. It also
1259 * frees the HBQ buffers returned by the firmware but not yet
1260 * processed by the upper layers.
1261 **/
ed957684
JS
1262void
1263lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1264{
92d7f7b0
JS
1265 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1266 struct hbq_dmabuf *hbq_buf;
3163f725 1267 unsigned long flags;
51ef4c26 1268 int i, hbq_count;
3163f725 1269 uint32_t hbqno;
ed957684 1270
51ef4c26 1271 hbq_count = lpfc_sli_hbq_count();
ed957684 1272 /* Return all memory used by all HBQs */
3163f725 1273 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1274 for (i = 0; i < hbq_count; ++i) {
1275 list_for_each_entry_safe(dmabuf, next_dmabuf,
1276 &phba->hbqs[i].hbq_buffer_list, list) {
1277 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1278 list_del(&hbq_buf->dbuf.list);
1279 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1280 }
a8adb832 1281 phba->hbqs[i].buffer_count = 0;
ed957684 1282 }
3163f725 1283 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1284 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1285 list) {
3163f725
JS
1286 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1287 list_del(&hbq_buf->dbuf.list);
1288 if (hbq_buf->tag == -1) {
1289 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1290 (phba, hbq_buf);
1291 } else {
1292 hbqno = hbq_buf->tag >> 16;
1293 if (hbqno >= LPFC_MAX_HBQS)
1294 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1295 (phba, hbq_buf);
1296 else
1297 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1298 hbq_buf);
1299 }
1300 }
1301
1302 /* Mark the HBQs not in use */
1303 phba->hbq_in_use = 0;
1304 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1305}
1306
e59058c4 1307/**
3621a710 1308 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1309 * @phba: Pointer to HBA context object.
1310 * @hbqno: HBQ number.
1311 * @hbq_buf: Pointer to HBQ buffer.
1312 *
1313 * This function is called with the hbalock held to post a
1314 * hbq buffer to the firmware. If the function finds an empty
1315 * slot in the HBQ, it will post the buffer. The function will return
1316 * pointer to the hbq entry if it successfully post the buffer
1317 * else it will return NULL.
1318 **/
3772a991 1319static int
ed957684 1320lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1321 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1322{
1323 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1324}
1325
1326/**
1327 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1328 * @phba: Pointer to HBA context object.
1329 * @hbqno: HBQ number.
1330 * @hbq_buf: Pointer to HBQ buffer.
1331 *
1332 * This function is called with the hbalock held to post a hbq buffer to the
1333 * firmware. If the function finds an empty slot in the HBQ, it will post the
1334 * buffer and place it on the hbq_buffer_list. The function will return zero if
1335 * it successfully post the buffer else it will return an error.
1336 **/
1337static int
1338lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1339 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1340{
1341 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1342 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1343
1344 /* Get next HBQ entry slot to use */
1345 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1346 if (hbqe) {
1347 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1348
92d7f7b0
JS
1349 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1350 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1351 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1352 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1353 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1354 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1355 /* Sync SLIM */
ed957684
JS
1356 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1357 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1358 /* flush */
ed957684 1359 readl(phba->hbq_put + hbqno);
51ef4c26 1360 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1361 return 0;
1362 } else
1363 return -ENOMEM;
ed957684
JS
1364}
1365
4f774513
JS
1366/**
1367 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1368 * @phba: Pointer to HBA context object.
1369 * @hbqno: HBQ number.
1370 * @hbq_buf: Pointer to HBQ buffer.
1371 *
1372 * This function is called with the hbalock held to post an RQE to the SLI4
1373 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1374 * the hbq_buffer_list and return zero, otherwise it will return an error.
1375 **/
1376static int
1377lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1378 struct hbq_dmabuf *hbq_buf)
1379{
1380 int rc;
1381 struct lpfc_rqe hrqe;
1382 struct lpfc_rqe drqe;
1383
1384 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1385 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1386 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1387 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1388 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1389 &hrqe, &drqe);
1390 if (rc < 0)
1391 return rc;
1392 hbq_buf->tag = rc;
1393 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1394 return 0;
1395}
1396
e59058c4 1397/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1398static struct lpfc_hbq_init lpfc_els_hbq = {
1399 .rn = 1,
def9c7a9 1400 .entry_count = 256,
92d7f7b0
JS
1401 .mask_count = 0,
1402 .profile = 0,
51ef4c26 1403 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1404 .buffer_count = 0,
a257bf90
JS
1405 .init_count = 40,
1406 .add_count = 40,
92d7f7b0 1407};
ed957684 1408
e59058c4 1409/* HBQ for the extra ring if needed */
51ef4c26
JS
1410static struct lpfc_hbq_init lpfc_extra_hbq = {
1411 .rn = 1,
1412 .entry_count = 200,
1413 .mask_count = 0,
1414 .profile = 0,
1415 .ring_mask = (1 << LPFC_EXTRA_RING),
1416 .buffer_count = 0,
1417 .init_count = 0,
1418 .add_count = 5,
1419};
1420
e59058c4 1421/* Array of HBQs */
78b2d852 1422struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1423 &lpfc_els_hbq,
51ef4c26 1424 &lpfc_extra_hbq,
92d7f7b0 1425};
ed957684 1426
e59058c4 1427/**
3621a710 1428 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1429 * @phba: Pointer to HBA context object.
1430 * @hbqno: HBQ number.
1431 * @count: Number of HBQ buffers to be posted.
1432 *
d7c255b2
JS
1433 * This function is called with no lock held to post more hbq buffers to the
1434 * given HBQ. The function returns the number of HBQ buffers successfully
1435 * posted.
e59058c4 1436 **/
311464ec 1437static int
92d7f7b0 1438lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1439{
d7c255b2 1440 uint32_t i, posted = 0;
3163f725 1441 unsigned long flags;
92d7f7b0 1442 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1443 LIST_HEAD(hbq_buf_list);
eafe1df9 1444 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1445 return 0;
51ef4c26 1446
d7c255b2
JS
1447 if ((phba->hbqs[hbqno].buffer_count + count) >
1448 lpfc_hbq_defs[hbqno]->entry_count)
1449 count = lpfc_hbq_defs[hbqno]->entry_count -
1450 phba->hbqs[hbqno].buffer_count;
1451 if (!count)
1452 return 0;
1453 /* Allocate HBQ entries */
1454 for (i = 0; i < count; i++) {
1455 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1456 if (!hbq_buffer)
1457 break;
1458 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1459 }
3163f725
JS
1460 /* Check whether HBQ is still in use */
1461 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1462 if (!phba->hbq_in_use)
d7c255b2
JS
1463 goto err;
1464 while (!list_empty(&hbq_buf_list)) {
1465 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1466 dbuf.list);
1467 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1468 (hbqno << 16));
3772a991 1469 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1470 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1471 posted++;
1472 } else
51ef4c26 1473 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1474 }
3163f725 1475 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1476 return posted;
1477err:
eafe1df9 1478 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1479 while (!list_empty(&hbq_buf_list)) {
1480 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1481 dbuf.list);
1482 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1483 }
1484 return 0;
ed957684
JS
1485}
1486
e59058c4 1487/**
3621a710 1488 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1489 * @phba: Pointer to HBA context object.
1490 * @qno: HBQ number.
1491 *
1492 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1493 * is called with no lock held. The function returns the number of HBQ entries
1494 * successfully allocated.
e59058c4 1495 **/
92d7f7b0
JS
1496int
1497lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1498{
def9c7a9
JS
1499 if (phba->sli_rev == LPFC_SLI_REV4)
1500 return 0;
1501 else
1502 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1504}
ed957684 1505
e59058c4 1506/**
3621a710 1507 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1508 * @phba: Pointer to HBA context object.
1509 * @qno: HBQ queue number.
1510 *
1511 * This function is called from SLI initialization code path with
1512 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1513 * function returns the number of HBQ entries successfully allocated.
e59058c4 1514 **/
a6ababd2 1515static int
92d7f7b0
JS
1516lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1517{
def9c7a9
JS
1518 if (phba->sli_rev == LPFC_SLI_REV4)
1519 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1520 lpfc_hbq_defs[qno]->entry_count);
1521 else
1522 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1523 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1524}
1525
3772a991
JS
1526/**
1527 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1528 * @phba: Pointer to HBA context object.
1529 * @hbqno: HBQ number.
1530 *
1531 * This function removes the first hbq buffer on an hbq list and returns a
1532 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1533 **/
1534static struct hbq_dmabuf *
1535lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1536{
1537 struct lpfc_dmabuf *d_buf;
1538
1539 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1540 if (!d_buf)
1541 return NULL;
1542 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1543}
1544
e59058c4 1545/**
3621a710 1546 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1547 * @phba: Pointer to HBA context object.
1548 * @tag: Tag of the hbq buffer.
1549 *
1550 * This function is called with hbalock held. This function searches
1551 * for the hbq buffer associated with the given tag in the hbq buffer
1552 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1553 * it returns NULL.
1554 **/
a6ababd2 1555static struct hbq_dmabuf *
92d7f7b0 1556lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1557{
92d7f7b0
JS
1558 struct lpfc_dmabuf *d_buf;
1559 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1560 uint32_t hbqno;
1561
1562 hbqno = tag >> 16;
a0a74e45 1563 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1564 return NULL;
ed957684 1565
3772a991 1566 spin_lock_irq(&phba->hbalock);
51ef4c26 1567 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1568 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1569 if (hbq_buf->tag == tag) {
3772a991 1570 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1571 return hbq_buf;
ed957684
JS
1572 }
1573 }
3772a991 1574 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1575 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1576 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1577 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1578 return NULL;
ed957684
JS
1579}
1580
e59058c4 1581/**
3621a710 1582 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1583 * @phba: Pointer to HBA context object.
1584 * @hbq_buffer: Pointer to HBQ buffer.
1585 *
1586 * This function is called with hbalock. This function gives back
1587 * the hbq buffer to firmware. If the HBQ does not have space to
1588 * post the buffer, it will free the buffer.
1589 **/
ed957684 1590void
51ef4c26 1591lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1592{
1593 uint32_t hbqno;
1594
51ef4c26
JS
1595 if (hbq_buffer) {
1596 hbqno = hbq_buffer->tag >> 16;
3772a991 1597 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1598 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1599 }
1600}
1601
e59058c4 1602/**
3621a710 1603 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1604 * @mbxCommand: mailbox command code.
1605 *
1606 * This function is called by the mailbox event handler function to verify
1607 * that the completed mailbox command is a legitimate mailbox command. If the
1608 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1609 * and the mailbox event handler will take the HBA offline.
1610 **/
dea3101e 1611static int
1612lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1613{
1614 uint8_t ret;
1615
1616 switch (mbxCommand) {
1617 case MBX_LOAD_SM:
1618 case MBX_READ_NV:
1619 case MBX_WRITE_NV:
a8adb832 1620 case MBX_WRITE_VPARMS:
dea3101e 1621 case MBX_RUN_BIU_DIAG:
1622 case MBX_INIT_LINK:
1623 case MBX_DOWN_LINK:
1624 case MBX_CONFIG_LINK:
1625 case MBX_CONFIG_RING:
1626 case MBX_RESET_RING:
1627 case MBX_READ_CONFIG:
1628 case MBX_READ_RCONFIG:
1629 case MBX_READ_SPARM:
1630 case MBX_READ_STATUS:
1631 case MBX_READ_RPI:
1632 case MBX_READ_XRI:
1633 case MBX_READ_REV:
1634 case MBX_READ_LNK_STAT:
1635 case MBX_REG_LOGIN:
1636 case MBX_UNREG_LOGIN:
1637 case MBX_READ_LA:
1638 case MBX_CLEAR_LA:
1639 case MBX_DUMP_MEMORY:
1640 case MBX_DUMP_CONTEXT:
1641 case MBX_RUN_DIAGS:
1642 case MBX_RESTART:
1643 case MBX_UPDATE_CFG:
1644 case MBX_DOWN_LOAD:
1645 case MBX_DEL_LD_ENTRY:
1646 case MBX_RUN_PROGRAM:
1647 case MBX_SET_MASK:
09372820 1648 case MBX_SET_VARIABLE:
dea3101e 1649 case MBX_UNREG_D_ID:
41415862 1650 case MBX_KILL_BOARD:
dea3101e 1651 case MBX_CONFIG_FARP:
41415862 1652 case MBX_BEACON:
dea3101e 1653 case MBX_LOAD_AREA:
1654 case MBX_RUN_BIU_DIAG64:
1655 case MBX_CONFIG_PORT:
1656 case MBX_READ_SPARM64:
1657 case MBX_READ_RPI64:
1658 case MBX_REG_LOGIN64:
1659 case MBX_READ_LA64:
09372820 1660 case MBX_WRITE_WWN:
dea3101e 1661 case MBX_SET_DEBUG:
1662 case MBX_LOAD_EXP_ROM:
57127f15 1663 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
1664 case MBX_REG_VPI:
1665 case MBX_UNREG_VPI:
858c9f6c 1666 case MBX_HEARTBEAT:
84774a4d
JS
1667 case MBX_PORT_CAPABILITIES:
1668 case MBX_PORT_IOV_CONTROL:
04c68496
JS
1669 case MBX_SLI4_CONFIG:
1670 case MBX_SLI4_REQ_FTRS:
1671 case MBX_REG_FCFI:
1672 case MBX_UNREG_FCFI:
1673 case MBX_REG_VFI:
1674 case MBX_UNREG_VFI:
1675 case MBX_INIT_VPI:
1676 case MBX_INIT_VFI:
1677 case MBX_RESUME_RPI:
c7495937
JS
1678 case MBX_READ_EVENT_LOG_STATUS:
1679 case MBX_READ_EVENT_LOG:
dea3101e 1680 ret = mbxCommand;
1681 break;
1682 default:
1683 ret = MBX_SHUTDOWN;
1684 break;
1685 }
2e0fef85 1686 return ret;
dea3101e 1687}
e59058c4
JS
1688
1689/**
3621a710 1690 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1691 * @phba: Pointer to HBA context object.
1692 * @pmboxq: Pointer to mailbox command.
1693 *
1694 * This is completion handler function for mailbox commands issued from
1695 * lpfc_sli_issue_mbox_wait function. This function is called by the
1696 * mailbox event handler function with no lock held. This function
1697 * will wake up thread waiting on the wait queue pointed by context1
1698 * of the mailbox.
1699 **/
04c68496 1700void
2e0fef85 1701lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 1702{
1703 wait_queue_head_t *pdone_q;
858c9f6c 1704 unsigned long drvr_flag;
dea3101e 1705
1706 /*
1707 * If pdone_q is empty, the driver thread gave up waiting and
1708 * continued running.
1709 */
7054a606 1710 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1711 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 1712 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1713 if (pdone_q)
1714 wake_up_interruptible(pdone_q);
858c9f6c 1715 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 1716 return;
1717}
1718
e59058c4
JS
1719
1720/**
3621a710 1721 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1722 * @phba: Pointer to HBA context object.
1723 * @pmb: Pointer to mailbox object.
1724 *
1725 * This function is the default mailbox completion handler. It
1726 * frees the memory resources associated with the completed mailbox
1727 * command. If the completed command is a REG_LOGIN mailbox command,
1728 * this function will issue a UREG_LOGIN to re-claim the RPI.
1729 **/
dea3101e 1730void
2e0fef85 1731lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1732{
d439d286 1733 struct lpfc_vport *vport = pmb->vport;
dea3101e 1734 struct lpfc_dmabuf *mp;
d439d286 1735 struct lpfc_nodelist *ndlp;
04c68496 1736 uint16_t rpi, vpi;
7054a606
JS
1737 int rc;
1738
dea3101e 1739 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1740
dea3101e 1741 if (mp) {
1742 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1743 kfree(mp);
1744 }
7054a606 1745
04c68496
JS
1746 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1747 (phba->sli_rev == LPFC_SLI_REV4))
1748 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1749
7054a606
JS
1750 /*
1751 * If a REG_LOGIN succeeded after node is destroyed or node
1752 * is in re-discovery driver need to cleanup the RPI.
1753 */
2e0fef85 1754 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1755 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1756 !pmb->u.mb.mbxStatus) {
1757 rpi = pmb->u.mb.un.varWords[0];
1758 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1759 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1760 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1761 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1762 if (rc != MBX_NOT_FINISHED)
1763 return;
1764 }
1765
695a814e
JS
1766 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1767 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1768 !(phba->pport->load_flag & FC_UNLOADING) &&
1769 !pmb->u.mb.mbxStatus) {
1770 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1771 pmb->vport = vport;
1772 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1773 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1774 if (rc != MBX_NOT_FINISHED)
1775 return;
1776 }
1777
d439d286
JS
1778 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
1779 ndlp = (struct lpfc_nodelist *)pmb->context2;
1780 lpfc_nlp_put(ndlp);
1781 pmb->context2 = NULL;
1782 }
1783
04c68496
JS
1784 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1785 lpfc_sli4_mbox_cmd_free(phba, pmb);
1786 else
1787 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1788}
1789
e59058c4 1790/**
3621a710 1791 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1792 * @phba: Pointer to HBA context object.
1793 *
1794 * This function is called with no lock held. This function processes all
1795 * the completed mailbox commands and gives it to upper layers. The interrupt
1796 * service routine processes mailbox completion interrupt and adds completed
1797 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1798 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1799 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1800 * function returns the mailbox commands to the upper layer by calling the
1801 * completion handler function of each mailbox.
1802 **/
dea3101e 1803int
2e0fef85 1804lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1805{
92d7f7b0 1806 MAILBOX_t *pmbox;
dea3101e 1807 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1808 int rc;
1809 LIST_HEAD(cmplq);
dea3101e 1810
1811 phba->sli.slistat.mbox_event++;
1812
92d7f7b0
JS
1813 /* Get all completed mailboxe buffers into the cmplq */
1814 spin_lock_irq(&phba->hbalock);
1815 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1816 spin_unlock_irq(&phba->hbalock);
dea3101e 1817
92d7f7b0
JS
1818 /* Get a Mailbox buffer to setup mailbox commands for callback */
1819 do {
1820 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1821 if (pmb == NULL)
1822 break;
2e0fef85 1823
04c68496 1824 pmbox = &pmb->u.mb;
dea3101e 1825
858c9f6c
JS
1826 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1827 if (pmb->vport) {
1828 lpfc_debugfs_disc_trc(pmb->vport,
1829 LPFC_DISC_TRC_MBOX_VPORT,
1830 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1831 (uint32_t)pmbox->mbxCommand,
1832 pmbox->un.varWords[0],
1833 pmbox->un.varWords[1]);
1834 }
1835 else {
1836 lpfc_debugfs_disc_trc(phba->pport,
1837 LPFC_DISC_TRC_MBOX,
1838 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1839 (uint32_t)pmbox->mbxCommand,
1840 pmbox->un.varWords[0],
1841 pmbox->un.varWords[1]);
1842 }
1843 }
1844
dea3101e 1845 /*
1846 * It is a fatal error if unknown mbox command completion.
1847 */
1848 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1849 MBX_SHUTDOWN) {
af901ca1 1850 /* Unknown mailbox command compl */
92d7f7b0 1851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1852 "(%d):0323 Unknown Mailbox command "
04c68496 1853 "x%x (x%x) Cmpl\n",
92d7f7b0 1854 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1855 pmbox->mbxCommand,
1856 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1857 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1858 phba->work_hs = HS_FFER3;
1859 lpfc_handle_eratt(phba);
92d7f7b0 1860 continue;
dea3101e 1861 }
1862
dea3101e 1863 if (pmbox->mbxStatus) {
1864 phba->sli.slistat.mbox_stat_err++;
1865 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1866 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1867 lpfc_printf_log(phba, KERN_INFO,
1868 LOG_MBOX | LOG_SLI,
e8b62011 1869 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1870 "error - RETRYing Data: x%x "
04c68496 1871 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1872 pmb->vport ? pmb->vport->vpi :0,
1873 pmbox->mbxCommand,
04c68496
JS
1874 lpfc_sli4_mbox_opcode_get(phba,
1875 pmb),
92d7f7b0
JS
1876 pmbox->mbxStatus,
1877 pmbox->un.varWords[0],
1878 pmb->vport->port_state);
dea3101e 1879 pmbox->mbxStatus = 0;
1880 pmbox->mbxOwner = OWN_HOST;
dea3101e 1881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1882 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1883 continue;
dea3101e 1884 }
1885 }
1886
1887 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1888 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1889 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1890 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1891 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1892 pmbox->mbxCommand,
04c68496 1893 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e 1894 pmb->mbox_cmpl,
1895 *((uint32_t *) pmbox),
1896 pmbox->un.varWords[0],
1897 pmbox->un.varWords[1],
1898 pmbox->un.varWords[2],
1899 pmbox->un.varWords[3],
1900 pmbox->un.varWords[4],
1901 pmbox->un.varWords[5],
1902 pmbox->un.varWords[6],
1903 pmbox->un.varWords[7]);
1904
92d7f7b0 1905 if (pmb->mbox_cmpl)
dea3101e 1906 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1907 } while (1);
1908 return 0;
1909}
dea3101e 1910
e59058c4 1911/**
3621a710 1912 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1913 * @phba: Pointer to HBA context object.
1914 * @pring: Pointer to driver SLI ring object.
1915 * @tag: buffer tag.
1916 *
1917 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1918 * is set in the tag the buffer is posted for a particular exchange,
1919 * the function will return the buffer without replacing the buffer.
1920 * If the buffer is for unsolicited ELS or CT traffic, this function
1921 * returns the buffer and also posts another buffer to the firmware.
1922 **/
76bb24ef
JS
1923static struct lpfc_dmabuf *
1924lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1925 struct lpfc_sli_ring *pring,
1926 uint32_t tag)
76bb24ef 1927{
9f1e1b50
JS
1928 struct hbq_dmabuf *hbq_entry;
1929
76bb24ef
JS
1930 if (tag & QUE_BUFTAG_BIT)
1931 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1932 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1933 if (!hbq_entry)
1934 return NULL;
1935 return &hbq_entry->dbuf;
76bb24ef 1936}
57127f15 1937
3772a991
JS
1938/**
1939 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1940 * @phba: Pointer to HBA context object.
1941 * @pring: Pointer to driver SLI ring object.
1942 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1943 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1944 * @fch_type: the type for the first frame of the sequence.
1945 *
1946 * This function is called with no lock held. This function uses the r_ctl and
1947 * type of the received sequence to find the correct callback function to call
1948 * to process the sequence.
1949 **/
1950static int
1951lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1952 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1953 uint32_t fch_type)
1954{
1955 int i;
1956
1957 /* unSolicited Responses */
1958 if (pring->prt[0].profile) {
1959 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1960 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1961 saveq);
1962 return 1;
1963 }
1964 /* We must search, based on rctl / type
1965 for the right routine */
1966 for (i = 0; i < pring->num_mask; i++) {
1967 if ((pring->prt[i].rctl == fch_r_ctl) &&
1968 (pring->prt[i].type == fch_type)) {
1969 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1970 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1971 (phba, pring, saveq);
1972 return 1;
1973 }
1974 }
1975 return 0;
1976}
e59058c4
JS
1977
1978/**
3621a710 1979 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1980 * @phba: Pointer to HBA context object.
1981 * @pring: Pointer to driver SLI ring object.
1982 * @saveq: Pointer to the unsolicited iocb.
1983 *
1984 * This function is called with no lock held by the ring event handler
1985 * when there is an unsolicited iocb posted to the response ring by the
1986 * firmware. This function gets the buffer associated with the iocbs
1987 * and calls the event handler for the ring. This function handles both
1988 * qring buffers and hbq buffers.
1989 * When the function returns 1 the caller can free the iocb object otherwise
1990 * upper layer functions will free the iocb objects.
1991 **/
dea3101e 1992static int
1993lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1994 struct lpfc_iocbq *saveq)
1995{
1996 IOCB_t * irsp;
1997 WORD5 * w5p;
1998 uint32_t Rctl, Type;
3772a991 1999 uint32_t match;
76bb24ef 2000 struct lpfc_iocbq *iocbq;
3163f725 2001 struct lpfc_dmabuf *dmzbuf;
dea3101e 2002
2003 match = 0;
2004 irsp = &(saveq->iocb);
57127f15
JS
2005
2006 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2007 if (pring->lpfc_sli_rcv_async_status)
2008 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2009 else
2010 lpfc_printf_log(phba,
2011 KERN_WARNING,
2012 LOG_SLI,
2013 "0316 Ring %d handler: unexpected "
2014 "ASYNC_STATUS iocb received evt_code "
2015 "0x%x\n",
2016 pring->ringno,
2017 irsp->un.asyncstat.evt_code);
2018 return 1;
2019 }
2020
3163f725
JS
2021 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2022 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2023 if (irsp->ulpBdeCount > 0) {
2024 dmzbuf = lpfc_sli_get_buff(phba, pring,
2025 irsp->un.ulpWord[3]);
2026 lpfc_in_buf_free(phba, dmzbuf);
2027 }
2028
2029 if (irsp->ulpBdeCount > 1) {
2030 dmzbuf = lpfc_sli_get_buff(phba, pring,
2031 irsp->unsli3.sli3Words[3]);
2032 lpfc_in_buf_free(phba, dmzbuf);
2033 }
2034
2035 if (irsp->ulpBdeCount > 2) {
2036 dmzbuf = lpfc_sli_get_buff(phba, pring,
2037 irsp->unsli3.sli3Words[7]);
2038 lpfc_in_buf_free(phba, dmzbuf);
2039 }
2040
2041 return 1;
2042 }
2043
92d7f7b0 2044 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2045 if (irsp->ulpBdeCount != 0) {
2046 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2047 irsp->un.ulpWord[3]);
2048 if (!saveq->context2)
2049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_SLI,
2052 "0341 Ring %d Cannot find buffer for "
2053 "an unsolicited iocb. tag 0x%x\n",
2054 pring->ringno,
2055 irsp->un.ulpWord[3]);
76bb24ef
JS
2056 }
2057 if (irsp->ulpBdeCount == 2) {
2058 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2059 irsp->unsli3.sli3Words[7]);
2060 if (!saveq->context3)
2061 lpfc_printf_log(phba,
2062 KERN_ERR,
2063 LOG_SLI,
2064 "0342 Ring %d Cannot find buffer for an"
2065 " unsolicited iocb. tag 0x%x\n",
2066 pring->ringno,
2067 irsp->unsli3.sli3Words[7]);
2068 }
2069 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2070 irsp = &(iocbq->iocb);
76bb24ef
JS
2071 if (irsp->ulpBdeCount != 0) {
2072 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2073 irsp->un.ulpWord[3]);
9c2face6 2074 if (!iocbq->context2)
76bb24ef
JS
2075 lpfc_printf_log(phba,
2076 KERN_ERR,
2077 LOG_SLI,
2078 "0343 Ring %d Cannot find "
2079 "buffer for an unsolicited iocb"
2080 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2081 irsp->un.ulpWord[3]);
76bb24ef
JS
2082 }
2083 if (irsp->ulpBdeCount == 2) {
2084 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2085 irsp->unsli3.sli3Words[7]);
9c2face6 2086 if (!iocbq->context3)
76bb24ef
JS
2087 lpfc_printf_log(phba,
2088 KERN_ERR,
2089 LOG_SLI,
2090 "0344 Ring %d Cannot find "
2091 "buffer for an unsolicited "
2092 "iocb. tag 0x%x\n",
2093 pring->ringno,
2094 irsp->unsli3.sli3Words[7]);
2095 }
2096 }
92d7f7b0 2097 }
9c2face6
JS
2098 if (irsp->ulpBdeCount != 0 &&
2099 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2100 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2101 int found = 0;
2102
2103 /* search continue save q for same XRI */
2104 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2105 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2106 list_add_tail(&saveq->list, &iocbq->list);
2107 found = 1;
2108 break;
2109 }
2110 }
2111 if (!found)
2112 list_add_tail(&saveq->clist,
2113 &pring->iocb_continue_saveq);
2114 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2115 list_del_init(&iocbq->clist);
2116 saveq = iocbq;
2117 irsp = &(saveq->iocb);
2118 } else
2119 return 0;
2120 }
2121 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2122 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2123 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2124 Rctl = FC_RCTL_ELS_REQ;
2125 Type = FC_TYPE_ELS;
9c2face6
JS
2126 } else {
2127 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2128 Rctl = w5p->hcsw.Rctl;
2129 Type = w5p->hcsw.Type;
2130
2131 /* Firmware Workaround */
2132 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2133 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2134 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2135 Rctl = FC_RCTL_ELS_REQ;
2136 Type = FC_TYPE_ELS;
9c2face6
JS
2137 w5p->hcsw.Rctl = Rctl;
2138 w5p->hcsw.Type = Type;
2139 }
2140 }
92d7f7b0 2141
3772a991 2142 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2143 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2144 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2145 "Type x%x received\n",
e8b62011 2146 pring->ringno, Rctl, Type);
3772a991 2147
92d7f7b0 2148 return 1;
dea3101e 2149}
2150
e59058c4 2151/**
3621a710 2152 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2153 * @phba: Pointer to HBA context object.
2154 * @pring: Pointer to driver SLI ring object.
2155 * @prspiocb: Pointer to response iocb object.
2156 *
2157 * This function looks up the iocb_lookup table to get the command iocb
2158 * corresponding to the given response iocb using the iotag of the
2159 * response iocb. This function is called with the hbalock held.
2160 * This function returns the command iocb object if it finds the command
2161 * iocb else returns NULL.
2162 **/
dea3101e 2163static struct lpfc_iocbq *
2e0fef85
JS
2164lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2165 struct lpfc_sli_ring *pring,
2166 struct lpfc_iocbq *prspiocb)
dea3101e 2167{
dea3101e 2168 struct lpfc_iocbq *cmd_iocb = NULL;
2169 uint16_t iotag;
2170
604a3e30
JB
2171 iotag = prspiocb->iocb.ulpIoTag;
2172
2173 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2174 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2175 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2176 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2177 pring->txcmplq_cnt--;
2178 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2179 }
604a3e30 2180 return cmd_iocb;
dea3101e 2181 }
2182
dea3101e 2183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2184 "0317 iotag x%x is out off "
604a3e30 2185 "range: max iotag x%x wd0 x%x\n",
e8b62011 2186 iotag, phba->sli.last_iotag,
604a3e30 2187 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2188 return NULL;
2189}
2190
3772a991
JS
2191/**
2192 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2193 * @phba: Pointer to HBA context object.
2194 * @pring: Pointer to driver SLI ring object.
2195 * @iotag: IOCB tag.
2196 *
2197 * This function looks up the iocb_lookup table to get the command iocb
2198 * corresponding to the given iotag. This function is called with the
2199 * hbalock held.
2200 * This function returns the command iocb object if it finds the command
2201 * iocb else returns NULL.
2202 **/
2203static struct lpfc_iocbq *
2204lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2205 struct lpfc_sli_ring *pring, uint16_t iotag)
2206{
2207 struct lpfc_iocbq *cmd_iocb;
2208
2209 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2210 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2211 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2212 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2213 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2214 pring->txcmplq_cnt--;
2215 }
3772a991
JS
2216 return cmd_iocb;
2217 }
2218
2219 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2220 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2221 iotag, phba->sli.last_iotag);
2222 return NULL;
2223}
2224
e59058c4 2225/**
3621a710 2226 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2227 * @phba: Pointer to HBA context object.
2228 * @pring: Pointer to driver SLI ring object.
2229 * @saveq: Pointer to the response iocb to be processed.
2230 *
2231 * This function is called by the ring event handler for non-fcp
2232 * rings when there is a new response iocb in the response ring.
2233 * The caller is not required to hold any locks. This function
2234 * gets the command iocb associated with the response iocb and
2235 * calls the completion handler for the command iocb. If there
2236 * is no completion handler, the function will free the resources
2237 * associated with command iocb. If the response iocb is for
2238 * an already aborted command iocb, the status of the completion
2239 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2240 * This function always returns 1.
2241 **/
dea3101e 2242static int
2e0fef85 2243lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2244 struct lpfc_iocbq *saveq)
2245{
2e0fef85 2246 struct lpfc_iocbq *cmdiocbp;
dea3101e 2247 int rc = 1;
2248 unsigned long iflag;
2249
2250 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2251 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2252 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2253 spin_unlock_irqrestore(&phba->hbalock, iflag);
2254
dea3101e 2255 if (cmdiocbp) {
2256 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2257 /*
2258 * If an ELS command failed send an event to mgmt
2259 * application.
2260 */
2261 if (saveq->iocb.ulpStatus &&
2262 (pring->ringno == LPFC_ELS_RING) &&
2263 (cmdiocbp->iocb.ulpCommand ==
2264 CMD_ELS_REQUEST64_CR))
2265 lpfc_send_els_failure_event(phba,
2266 cmdiocbp, saveq);
2267
dea3101e 2268 /*
2269 * Post all ELS completions to the worker thread.
2270 * All other are passed to the completion callback.
2271 */
2272 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2273 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2274 (cmdiocbp->iocb_flag &
2275 LPFC_DRIVER_ABORTED)) {
2276 spin_lock_irqsave(&phba->hbalock,
2277 iflag);
07951076
JS
2278 cmdiocbp->iocb_flag &=
2279 ~LPFC_DRIVER_ABORTED;
341af102
JS
2280 spin_unlock_irqrestore(&phba->hbalock,
2281 iflag);
07951076
JS
2282 saveq->iocb.ulpStatus =
2283 IOSTAT_LOCAL_REJECT;
2284 saveq->iocb.un.ulpWord[4] =
2285 IOERR_SLI_ABORTED;
0ff10d46
JS
2286
2287 /* Firmware could still be in progress
2288 * of DMAing payload, so don't free data
2289 * buffer till after a hbeat.
2290 */
341af102
JS
2291 spin_lock_irqsave(&phba->hbalock,
2292 iflag);
0ff10d46 2293 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2294 spin_unlock_irqrestore(&phba->hbalock,
2295 iflag);
2296 }
0f65ff68
JS
2297 if (phba->sli_rev == LPFC_SLI_REV4) {
2298 if (saveq->iocb_flag &
2299 LPFC_EXCHANGE_BUSY) {
2300 /* Set cmdiocb flag for the
2301 * exchange busy so sgl (xri)
2302 * will not be released until
2303 * the abort xri is received
2304 * from hba.
2305 */
2306 spin_lock_irqsave(
2307 &phba->hbalock, iflag);
2308 cmdiocbp->iocb_flag |=
2309 LPFC_EXCHANGE_BUSY;
2310 spin_unlock_irqrestore(
2311 &phba->hbalock, iflag);
2312 }
2313 if (cmdiocbp->iocb_flag &
2314 LPFC_DRIVER_ABORTED) {
2315 /*
2316 * Clear LPFC_DRIVER_ABORTED
2317 * bit in case it was driver
2318 * initiated abort.
2319 */
2320 spin_lock_irqsave(
2321 &phba->hbalock, iflag);
2322 cmdiocbp->iocb_flag &=
2323 ~LPFC_DRIVER_ABORTED;
2324 spin_unlock_irqrestore(
2325 &phba->hbalock, iflag);
2326 cmdiocbp->iocb.ulpStatus =
2327 IOSTAT_LOCAL_REJECT;
2328 cmdiocbp->iocb.un.ulpWord[4] =
2329 IOERR_ABORT_REQUESTED;
2330 /*
2331 * For SLI4, irsiocb contains
2332 * NO_XRI in sli_xritag, it
2333 * shall not affect releasing
2334 * sgl (xri) process.
2335 */
2336 saveq->iocb.ulpStatus =
2337 IOSTAT_LOCAL_REJECT;
2338 saveq->iocb.un.ulpWord[4] =
2339 IOERR_SLI_ABORTED;
2340 spin_lock_irqsave(
2341 &phba->hbalock, iflag);
2342 saveq->iocb_flag |=
2343 LPFC_DELAY_MEM_FREE;
2344 spin_unlock_irqrestore(
2345 &phba->hbalock, iflag);
2346 }
07951076 2347 }
dea3101e 2348 }
2e0fef85 2349 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2350 } else
2351 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2352 } else {
2353 /*
2354 * Unknown initiating command based on the response iotag.
2355 * This could be the case on the ELS ring because of
2356 * lpfc_els_abort().
2357 */
2358 if (pring->ringno != LPFC_ELS_RING) {
2359 /*
2360 * Ring <ringno> handler: unexpected completion IoTag
2361 * <IoTag>
2362 */
a257bf90 2363 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2364 "0322 Ring %d handler: "
2365 "unexpected completion IoTag x%x "
2366 "Data: x%x x%x x%x x%x\n",
2367 pring->ringno,
2368 saveq->iocb.ulpIoTag,
2369 saveq->iocb.ulpStatus,
2370 saveq->iocb.un.ulpWord[4],
2371 saveq->iocb.ulpCommand,
2372 saveq->iocb.ulpContext);
dea3101e 2373 }
2374 }
68876920 2375
dea3101e 2376 return rc;
2377}
2378
e59058c4 2379/**
3621a710 2380 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2381 * @phba: Pointer to HBA context object.
2382 * @pring: Pointer to driver SLI ring object.
2383 *
2384 * This function is called from the iocb ring event handlers when
2385 * put pointer is ahead of the get pointer for a ring. This function signal
2386 * an error attention condition to the worker thread and the worker
2387 * thread will transition the HBA to offline state.
2388 **/
2e0fef85
JS
2389static void
2390lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2391{
34b02dcd 2392 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2393 /*
025dfdaf 2394 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2395 * rsp ring <portRspMax>
2396 */
2397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2398 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2399 "is bigger than rsp ring %d\n",
e8b62011 2400 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2401 pring->numRiocb);
2402
2e0fef85 2403 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2404
2405 /*
2406 * All error attention handlers are posted to
2407 * worker thread
2408 */
2409 phba->work_ha |= HA_ERATT;
2410 phba->work_hs = HS_FFER3;
92d7f7b0 2411
5e9d9b82 2412 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2413
2414 return;
2415}
2416
9399627f 2417/**
3621a710 2418 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2419 * @ptr: Pointer to address of HBA context object.
2420 *
2421 * This function is invoked by the Error Attention polling timer when the
2422 * timer times out. It will check the SLI Error Attention register for
2423 * possible attention events. If so, it will post an Error Attention event
2424 * and wake up worker thread to process it. Otherwise, it will set up the
2425 * Error Attention polling timer for the next poll.
2426 **/
2427void lpfc_poll_eratt(unsigned long ptr)
2428{
2429 struct lpfc_hba *phba;
2430 uint32_t eratt = 0;
2431
2432 phba = (struct lpfc_hba *)ptr;
2433
2434 /* Check chip HA register for error event */
2435 eratt = lpfc_sli_check_eratt(phba);
2436
2437 if (eratt)
2438 /* Tell the worker thread there is work to do */
2439 lpfc_worker_wake_up(phba);
2440 else
2441 /* Restart the timer for next eratt poll */
2442 mod_timer(&phba->eratt_poll, jiffies +
2443 HZ * LPFC_ERATT_POLL_INTERVAL);
2444 return;
2445}
2446
875fbdfe 2447
e59058c4 2448/**
3621a710 2449 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2450 * @phba: Pointer to HBA context object.
2451 * @pring: Pointer to driver SLI ring object.
2452 * @mask: Host attention register mask for this ring.
2453 *
2454 * This function is called from the interrupt context when there is a ring
2455 * event for the fcp ring. The caller does not hold any lock.
2456 * The function processes each response iocb in the response ring until it
2457 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2458 * LE bit set. The function will call the completion handler of the command iocb
2459 * if the response iocb indicates a completion for a command iocb or it is
2460 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2461 * function if this is an unsolicited iocb.
dea3101e 2462 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2463 * to check it explicitly.
2464 */
2465int
2e0fef85
JS
2466lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2467 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2468{
34b02dcd 2469 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2470 IOCB_t *irsp = NULL;
87f6eaff 2471 IOCB_t *entry = NULL;
dea3101e 2472 struct lpfc_iocbq *cmdiocbq = NULL;
2473 struct lpfc_iocbq rspiocbq;
dea3101e 2474 uint32_t status;
2475 uint32_t portRspPut, portRspMax;
2476 int rc = 1;
2477 lpfc_iocb_type type;
2478 unsigned long iflag;
2479 uint32_t rsp_cmpl = 0;
dea3101e 2480
2e0fef85 2481 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2482 pring->stats.iocb_event++;
2483
dea3101e 2484 /*
2485 * The next available response entry should never exceed the maximum
2486 * entries. If it does, treat it as an adapter hardware error.
2487 */
2488 portRspMax = pring->numRiocb;
2489 portRspPut = le32_to_cpu(pgp->rspPutInx);
2490 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2491 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2492 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2493 return 1;
2494 }
45ed1190
JS
2495 if (phba->fcp_ring_in_use) {
2496 spin_unlock_irqrestore(&phba->hbalock, iflag);
2497 return 1;
2498 } else
2499 phba->fcp_ring_in_use = 1;
dea3101e 2500
2501 rmb();
2502 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2503 /*
2504 * Fetch an entry off the ring and copy it into a local data
2505 * structure. The copy involves a byte-swap since the
2506 * network byte order and pci byte orders are different.
2507 */
ed957684 2508 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2509 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2510
2511 if (++pring->rspidx >= portRspMax)
2512 pring->rspidx = 0;
2513
87f6eaff
JSEC
2514 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2515 (uint32_t *) &rspiocbq.iocb,
ed957684 2516 phba->iocb_rsp_size);
a4bc3379 2517 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2518 irsp = &rspiocbq.iocb;
2519
dea3101e 2520 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2521 pring->stats.iocb_rsp++;
2522 rsp_cmpl++;
2523
2524 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2525 /*
2526 * If resource errors reported from HBA, reduce
2527 * queuedepths of the SCSI device.
2528 */
2529 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2530 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2531 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2532 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2533 spin_lock_irqsave(&phba->hbalock, iflag);
2534 }
2535
dea3101e 2536 /* Rsp ring <ringno> error: IOCB */
2537 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2538 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2539 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2540 pring->ringno,
92d7f7b0
JS
2541 irsp->un.ulpWord[0],
2542 irsp->un.ulpWord[1],
2543 irsp->un.ulpWord[2],
2544 irsp->un.ulpWord[3],
2545 irsp->un.ulpWord[4],
2546 irsp->un.ulpWord[5],
d7c255b2
JS
2547 *(uint32_t *)&irsp->un1,
2548 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2549 }
2550
2551 switch (type) {
2552 case LPFC_ABORT_IOCB:
2553 case LPFC_SOL_IOCB:
2554 /*
2555 * Idle exchange closed via ABTS from port. No iocb
2556 * resources need to be recovered.
2557 */
2558 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2559 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2560 "0333 IOCB cmd 0x%x"
dca9479b 2561 " processed. Skipping"
92d7f7b0 2562 " completion\n",
dca9479b 2563 irsp->ulpCommand);
dea3101e 2564 break;
2565 }
2566
604a3e30
JB
2567 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2568 &rspiocbq);
0f65ff68
JS
2569 if (unlikely(!cmdiocbq))
2570 break;
2571 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2572 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2573 if (cmdiocbq->iocb_cmpl) {
2574 spin_unlock_irqrestore(&phba->hbalock, iflag);
2575 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2576 &rspiocbq);
2577 spin_lock_irqsave(&phba->hbalock, iflag);
2578 }
dea3101e 2579 break;
a4bc3379 2580 case LPFC_UNSOL_IOCB:
2e0fef85 2581 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2582 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2583 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2584 break;
dea3101e 2585 default:
2586 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2587 char adaptermsg[LPFC_MAX_ADPTMSG];
2588 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2589 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2590 MAX_MSG_DATA);
898eb71c
JP
2591 dev_warn(&((phba->pcidev)->dev),
2592 "lpfc%d: %s\n",
dea3101e 2593 phba->brd_no, adaptermsg);
2594 } else {
2595 /* Unknown IOCB command */
2596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2597 "0334 Unknown IOCB command "
92d7f7b0 2598 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2599 type, irsp->ulpCommand,
92d7f7b0
JS
2600 irsp->ulpStatus,
2601 irsp->ulpIoTag,
2602 irsp->ulpContext);
dea3101e 2603 }
2604 break;
2605 }
2606
2607 /*
2608 * The response IOCB has been processed. Update the ring
2609 * pointer in SLIM. If the port response put pointer has not
2610 * been updated, sync the pgp->rspPutInx and fetch the new port
2611 * response put pointer.
2612 */
ed957684 2613 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2614
2615 if (pring->rspidx == portRspPut)
2616 portRspPut = le32_to_cpu(pgp->rspPutInx);
2617 }
2618
2619 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2620 pring->stats.iocb_rsp_full++;
2621 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2622 writel(status, phba->CAregaddr);
2623 readl(phba->CAregaddr);
2624 }
2625 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2626 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2627 pring->stats.iocb_cmd_empty++;
2628
2629 /* Force update of the local copy of cmdGetInx */
2630 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2631 lpfc_sli_resume_iocb(phba, pring);
2632
2633 if ((pring->lpfc_sli_cmd_available))
2634 (pring->lpfc_sli_cmd_available) (phba, pring);
2635
2636 }
2637
45ed1190 2638 phba->fcp_ring_in_use = 0;
2e0fef85 2639 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2640 return rc;
2641}
2642
e59058c4 2643/**
3772a991
JS
2644 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2645 * @phba: Pointer to HBA context object.
2646 * @pring: Pointer to driver SLI ring object.
2647 * @rspiocbp: Pointer to driver response IOCB object.
2648 *
2649 * This function is called from the worker thread when there is a slow-path
2650 * response IOCB to process. This function chains all the response iocbs until
2651 * seeing the iocb with the LE bit set. The function will call
2652 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2653 * completion of a command iocb. The function will call the
2654 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2655 * The function frees the resources or calls the completion handler if this
2656 * iocb is an abort completion. The function returns NULL when the response
2657 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2658 * this function shall chain the iocb on to the iocb_continueq and return the
2659 * response iocb passed in.
2660 **/
2661static struct lpfc_iocbq *
2662lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2663 struct lpfc_iocbq *rspiocbp)
2664{
2665 struct lpfc_iocbq *saveq;
2666 struct lpfc_iocbq *cmdiocbp;
2667 struct lpfc_iocbq *next_iocb;
2668 IOCB_t *irsp = NULL;
2669 uint32_t free_saveq;
2670 uint8_t iocb_cmd_type;
2671 lpfc_iocb_type type;
2672 unsigned long iflag;
2673 int rc;
2674
2675 spin_lock_irqsave(&phba->hbalock, iflag);
2676 /* First add the response iocb to the countinueq list */
2677 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2678 pring->iocb_continueq_cnt++;
2679
2680 /* Now, determine whetehr the list is completed for processing */
2681 irsp = &rspiocbp->iocb;
2682 if (irsp->ulpLe) {
2683 /*
2684 * By default, the driver expects to free all resources
2685 * associated with this iocb completion.
2686 */
2687 free_saveq = 1;
2688 saveq = list_get_first(&pring->iocb_continueq,
2689 struct lpfc_iocbq, list);
2690 irsp = &(saveq->iocb);
2691 list_del_init(&pring->iocb_continueq);
2692 pring->iocb_continueq_cnt = 0;
2693
2694 pring->stats.iocb_rsp++;
2695
2696 /*
2697 * If resource errors reported from HBA, reduce
2698 * queuedepths of the SCSI device.
2699 */
2700 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2701 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2702 spin_unlock_irqrestore(&phba->hbalock, iflag);
2703 phba->lpfc_rampdown_queue_depth(phba);
2704 spin_lock_irqsave(&phba->hbalock, iflag);
2705 }
2706
2707 if (irsp->ulpStatus) {
2708 /* Rsp ring <ringno> error: IOCB */
2709 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2710 "0328 Rsp Ring %d error: "
2711 "IOCB Data: "
2712 "x%x x%x x%x x%x "
2713 "x%x x%x x%x x%x "
2714 "x%x x%x x%x x%x "
2715 "x%x x%x x%x x%x\n",
2716 pring->ringno,
2717 irsp->un.ulpWord[0],
2718 irsp->un.ulpWord[1],
2719 irsp->un.ulpWord[2],
2720 irsp->un.ulpWord[3],
2721 irsp->un.ulpWord[4],
2722 irsp->un.ulpWord[5],
2723 *(((uint32_t *) irsp) + 6),
2724 *(((uint32_t *) irsp) + 7),
2725 *(((uint32_t *) irsp) + 8),
2726 *(((uint32_t *) irsp) + 9),
2727 *(((uint32_t *) irsp) + 10),
2728 *(((uint32_t *) irsp) + 11),
2729 *(((uint32_t *) irsp) + 12),
2730 *(((uint32_t *) irsp) + 13),
2731 *(((uint32_t *) irsp) + 14),
2732 *(((uint32_t *) irsp) + 15));
2733 }
2734
2735 /*
2736 * Fetch the IOCB command type and call the correct completion
2737 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2738 * get freed back to the lpfc_iocb_list by the discovery
2739 * kernel thread.
2740 */
2741 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2742 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2743 switch (type) {
2744 case LPFC_SOL_IOCB:
2745 spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2747 spin_lock_irqsave(&phba->hbalock, iflag);
2748 break;
2749
2750 case LPFC_UNSOL_IOCB:
2751 spin_unlock_irqrestore(&phba->hbalock, iflag);
2752 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2753 spin_lock_irqsave(&phba->hbalock, iflag);
2754 if (!rc)
2755 free_saveq = 0;
2756 break;
2757
2758 case LPFC_ABORT_IOCB:
2759 cmdiocbp = NULL;
2760 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2761 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2762 saveq);
2763 if (cmdiocbp) {
2764 /* Call the specified completion routine */
2765 if (cmdiocbp->iocb_cmpl) {
2766 spin_unlock_irqrestore(&phba->hbalock,
2767 iflag);
2768 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2769 saveq);
2770 spin_lock_irqsave(&phba->hbalock,
2771 iflag);
2772 } else
2773 __lpfc_sli_release_iocbq(phba,
2774 cmdiocbp);
2775 }
2776 break;
2777
2778 case LPFC_UNKNOWN_IOCB:
2779 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2780 char adaptermsg[LPFC_MAX_ADPTMSG];
2781 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2782 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2783 MAX_MSG_DATA);
2784 dev_warn(&((phba->pcidev)->dev),
2785 "lpfc%d: %s\n",
2786 phba->brd_no, adaptermsg);
2787 } else {
2788 /* Unknown IOCB command */
2789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2790 "0335 Unknown IOCB "
2791 "command Data: x%x "
2792 "x%x x%x x%x\n",
2793 irsp->ulpCommand,
2794 irsp->ulpStatus,
2795 irsp->ulpIoTag,
2796 irsp->ulpContext);
2797 }
2798 break;
2799 }
2800
2801 if (free_saveq) {
2802 list_for_each_entry_safe(rspiocbp, next_iocb,
2803 &saveq->list, list) {
2804 list_del(&rspiocbp->list);
2805 __lpfc_sli_release_iocbq(phba, rspiocbp);
2806 }
2807 __lpfc_sli_release_iocbq(phba, saveq);
2808 }
2809 rspiocbp = NULL;
2810 }
2811 spin_unlock_irqrestore(&phba->hbalock, iflag);
2812 return rspiocbp;
2813}
2814
2815/**
2816 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2817 * @phba: Pointer to HBA context object.
2818 * @pring: Pointer to driver SLI ring object.
2819 * @mask: Host attention register mask for this ring.
2820 *
3772a991
JS
2821 * This routine wraps the actual slow_ring event process routine from the
2822 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2823 **/
3772a991 2824void
2e0fef85
JS
2825lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2826 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2827{
2828 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2829}
2830
2831/**
2832 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2833 * @phba: Pointer to HBA context object.
2834 * @pring: Pointer to driver SLI ring object.
2835 * @mask: Host attention register mask for this ring.
2836 *
2837 * This function is called from the worker thread when there is a ring event
2838 * for non-fcp rings. The caller does not hold any lock. The function will
2839 * remove each response iocb in the response ring and calls the handle
2840 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2841 **/
2842static void
2843lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2844 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2845{
34b02dcd 2846 struct lpfc_pgp *pgp;
dea3101e 2847 IOCB_t *entry;
2848 IOCB_t *irsp = NULL;
2849 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2850 uint32_t portRspPut, portRspMax;
dea3101e 2851 unsigned long iflag;
3772a991 2852 uint32_t status;
dea3101e 2853
34b02dcd 2854 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2855 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2856 pring->stats.iocb_event++;
2857
dea3101e 2858 /*
2859 * The next available response entry should never exceed the maximum
2860 * entries. If it does, treat it as an adapter hardware error.
2861 */
2862 portRspMax = pring->numRiocb;
2863 portRspPut = le32_to_cpu(pgp->rspPutInx);
2864 if (portRspPut >= portRspMax) {
2865 /*
025dfdaf 2866 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 2867 * rsp ring <portRspMax>
2868 */
ed957684 2869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2870 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2871 "is bigger than rsp ring %d\n",
e8b62011 2872 pring->ringno, portRspPut, portRspMax);
dea3101e 2873
2e0fef85
JS
2874 phba->link_state = LPFC_HBA_ERROR;
2875 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2876
2877 phba->work_hs = HS_FFER3;
2878 lpfc_handle_eratt(phba);
2879
3772a991 2880 return;
dea3101e 2881 }
2882
2883 rmb();
dea3101e 2884 while (pring->rspidx != portRspPut) {
2885 /*
2886 * Build a completion list and call the appropriate handler.
2887 * The process is to get the next available response iocb, get
2888 * a free iocb from the list, copy the response data into the
2889 * free iocb, insert to the continuation list, and update the
2890 * next response index to slim. This process makes response
2891 * iocb's in the ring available to DMA as fast as possible but
2892 * pays a penalty for a copy operation. Since the iocb is
2893 * only 32 bytes, this penalty is considered small relative to
2894 * the PCI reads for register values and a slim write. When
2895 * the ulpLe field is set, the entire Command has been
2896 * received.
2897 */
ed957684
JS
2898 entry = lpfc_resp_iocb(phba, pring);
2899
858c9f6c 2900 phba->last_completion_time = jiffies;
2e0fef85 2901 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 2902 if (rspiocbp == NULL) {
2903 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2904 "completion.\n", __func__);
dea3101e 2905 break;
2906 }
2907
ed957684
JS
2908 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2909 phba->iocb_rsp_size);
dea3101e 2910 irsp = &rspiocbp->iocb;
2911
2912 if (++pring->rspidx >= portRspMax)
2913 pring->rspidx = 0;
2914
a58cbd52
JS
2915 if (pring->ringno == LPFC_ELS_RING) {
2916 lpfc_debugfs_slow_ring_trc(phba,
2917 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2918 *(((uint32_t *) irsp) + 4),
2919 *(((uint32_t *) irsp) + 6),
2920 *(((uint32_t *) irsp) + 7));
2921 }
2922
ed957684 2923 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2924
3772a991
JS
2925 spin_unlock_irqrestore(&phba->hbalock, iflag);
2926 /* Handle the response IOCB */
2927 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2928 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2929
2930 /*
2931 * If the port response put pointer has not been updated, sync
2932 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2933 * response put pointer.
2934 */
2935 if (pring->rspidx == portRspPut) {
2936 portRspPut = le32_to_cpu(pgp->rspPutInx);
2937 }
2938 } /* while (pring->rspidx != portRspPut) */
2939
92d7f7b0 2940 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 2941 /* At least one response entry has been freed */
2942 pring->stats.iocb_rsp_full++;
2943 /* SET RxRE_RSP in Chip Att register */
2944 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2945 writel(status, phba->CAregaddr);
2946 readl(phba->CAregaddr); /* flush */
2947 }
2948 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2949 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2950 pring->stats.iocb_cmd_empty++;
2951
2952 /* Force update of the local copy of cmdGetInx */
2953 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2954 lpfc_sli_resume_iocb(phba, pring);
2955
2956 if ((pring->lpfc_sli_cmd_available))
2957 (pring->lpfc_sli_cmd_available) (phba, pring);
2958
2959 }
2960
2e0fef85 2961 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2962 return;
dea3101e 2963}
2964
4f774513
JS
2965/**
2966 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2967 * @phba: Pointer to HBA context object.
2968 * @pring: Pointer to driver SLI ring object.
2969 * @mask: Host attention register mask for this ring.
2970 *
2971 * This function is called from the worker thread when there is a pending
2972 * ELS response iocb on the driver internal slow-path response iocb worker
2973 * queue. The caller does not hold any lock. The function will remove each
2974 * response iocb from the response worker queue and calls the handle
2975 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2976 **/
2977static void
2978lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2979 struct lpfc_sli_ring *pring, uint32_t mask)
2980{
2981 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
2982 struct hbq_dmabuf *dmabuf;
2983 struct lpfc_cq_event *cq_event;
4f774513
JS
2984 unsigned long iflag;
2985
45ed1190
JS
2986 spin_lock_irqsave(&phba->hbalock, iflag);
2987 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2988 spin_unlock_irqrestore(&phba->hbalock, iflag);
2989 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
2990 /* Get the response iocb from the head of work queue */
2991 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 2992 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 2993 cq_event, struct lpfc_cq_event, list);
4f774513 2994 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
2995
2996 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2997 case CQE_CODE_COMPL_WQE:
2998 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2999 cq_event);
45ed1190
JS
3000 /* Translate ELS WCQE to response IOCBQ */
3001 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3002 irspiocbq);
3003 if (irspiocbq)
3004 lpfc_sli_sp_handle_rspiocb(phba, pring,
3005 irspiocbq);
4d9ab994
JS
3006 break;
3007 case CQE_CODE_RECEIVE:
3008 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3009 cq_event);
3010 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3011 break;
3012 default:
3013 break;
3014 }
4f774513
JS
3015 }
3016}
3017
e59058c4 3018/**
3621a710 3019 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3020 * @phba: Pointer to HBA context object.
3021 * @pring: Pointer to driver SLI ring object.
3022 *
3023 * This function aborts all iocbs in the given ring and frees all the iocb
3024 * objects in txq. This function issues an abort iocb for all the iocb commands
3025 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3026 * the return of this function. The caller is not required to hold any locks.
3027 **/
2e0fef85 3028void
dea3101e 3029lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3030{
2534ba75 3031 LIST_HEAD(completions);
dea3101e 3032 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3033
92d7f7b0
JS
3034 if (pring->ringno == LPFC_ELS_RING) {
3035 lpfc_fabric_abort_hba(phba);
3036 }
3037
dea3101e 3038 /* Error everything on txq and txcmplq
3039 * First do the txq.
3040 */
2e0fef85 3041 spin_lock_irq(&phba->hbalock);
2534ba75 3042 list_splice_init(&pring->txq, &completions);
dea3101e 3043 pring->txq_cnt = 0;
dea3101e 3044
3045 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3046 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3047 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3048
2e0fef85 3049 spin_unlock_irq(&phba->hbalock);
dea3101e 3050
a257bf90
JS
3051 /* Cancel all the IOCBs from the completions list */
3052 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3053 IOERR_SLI_ABORTED);
dea3101e 3054}
3055
a8e497d5 3056/**
3621a710 3057 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3058 * @phba: Pointer to HBA context object.
3059 *
3060 * This function flushes all iocbs in the fcp ring and frees all the iocb
3061 * objects in txq and txcmplq. This function will not issue abort iocbs
3062 * for all the iocb commands in txcmplq, they will just be returned with
3063 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3064 * slot has been permanently disabled.
3065 **/
3066void
3067lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3068{
3069 LIST_HEAD(txq);
3070 LIST_HEAD(txcmplq);
a8e497d5
JS
3071 struct lpfc_sli *psli = &phba->sli;
3072 struct lpfc_sli_ring *pring;
3073
3074 /* Currently, only one fcp ring */
3075 pring = &psli->ring[psli->fcp_ring];
3076
3077 spin_lock_irq(&phba->hbalock);
3078 /* Retrieve everything on txq */
3079 list_splice_init(&pring->txq, &txq);
3080 pring->txq_cnt = 0;
3081
3082 /* Retrieve everything on the txcmplq */
3083 list_splice_init(&pring->txcmplq, &txcmplq);
3084 pring->txcmplq_cnt = 0;
3085 spin_unlock_irq(&phba->hbalock);
3086
3087 /* Flush the txq */
a257bf90
JS
3088 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3089 IOERR_SLI_DOWN);
a8e497d5
JS
3090
3091 /* Flush the txcmpq */
a257bf90
JS
3092 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3093 IOERR_SLI_DOWN);
a8e497d5
JS
3094}
3095
e59058c4 3096/**
3772a991 3097 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3098 * @phba: Pointer to HBA context object.
3099 * @mask: Bit mask to be checked.
3100 *
3101 * This function reads the host status register and compares
3102 * with the provided bit mask to check if HBA completed
3103 * the restart. This function will wait in a loop for the
3104 * HBA to complete restart. If the HBA does not restart within
3105 * 15 iterations, the function will reset the HBA again. The
3106 * function returns 1 when HBA fail to restart otherwise returns
3107 * zero.
3108 **/
3772a991
JS
3109static int
3110lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3111{
41415862
JW
3112 uint32_t status;
3113 int i = 0;
3114 int retval = 0;
dea3101e 3115
41415862
JW
3116 /* Read the HBA Host Status Register */
3117 status = readl(phba->HSregaddr);
dea3101e 3118
41415862
JW
3119 /*
3120 * Check status register every 100ms for 5 retries, then every
3121 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3122 * every 2.5 sec for 4.
3123 * Break our of the loop if errors occurred during init.
3124 */
3125 while (((status & mask) != mask) &&
3126 !(status & HS_FFERM) &&
3127 i++ < 20) {
dea3101e 3128
41415862
JW
3129 if (i <= 5)
3130 msleep(10);
3131 else if (i <= 10)
3132 msleep(500);
3133 else
3134 msleep(2500);
dea3101e 3135
41415862 3136 if (i == 15) {
2e0fef85 3137 /* Do post */
92d7f7b0 3138 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3139 lpfc_sli_brdrestart(phba);
3140 }
3141 /* Read the HBA Host Status Register */
3142 status = readl(phba->HSregaddr);
3143 }
dea3101e 3144
41415862
JW
3145 /* Check to see if any errors occurred during init */
3146 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3148 "2751 Adapter failed to restart, "
3149 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3150 status,
3151 readl(phba->MBslimaddr + 0xa8),
3152 readl(phba->MBslimaddr + 0xac));
2e0fef85 3153 phba->link_state = LPFC_HBA_ERROR;
41415862 3154 retval = 1;
dea3101e 3155 }
dea3101e 3156
41415862
JW
3157 return retval;
3158}
dea3101e 3159
da0436e9
JS
3160/**
3161 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3162 * @phba: Pointer to HBA context object.
3163 * @mask: Bit mask to be checked.
3164 *
3165 * This function checks the host status register to check if HBA is
3166 * ready. This function will wait in a loop for the HBA to be ready
3167 * If the HBA is not ready , the function will will reset the HBA PCI
3168 * function again. The function returns 1 when HBA fail to be ready
3169 * otherwise returns zero.
3170 **/
3171static int
3172lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3173{
3174 uint32_t status;
3175 int retval = 0;
3176
3177 /* Read the HBA Host Status Register */
3178 status = lpfc_sli4_post_status_check(phba);
3179
3180 if (status) {
3181 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3182 lpfc_sli_brdrestart(phba);
3183 status = lpfc_sli4_post_status_check(phba);
3184 }
3185
3186 /* Check to see if any errors occurred during init */
3187 if (status) {
3188 phba->link_state = LPFC_HBA_ERROR;
3189 retval = 1;
3190 } else
3191 phba->sli4_hba.intr_enable = 0;
3192
3193 return retval;
3194}
3195
3196/**
3197 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3198 * @phba: Pointer to HBA context object.
3199 * @mask: Bit mask to be checked.
3200 *
3201 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3202 * from the API jump table function pointer from the lpfc_hba struct.
3203 **/
3204int
3205lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3206{
3207 return phba->lpfc_sli_brdready(phba, mask);
3208}
3209
9290831f
JS
3210#define BARRIER_TEST_PATTERN (0xdeadbeef)
3211
e59058c4 3212/**
3621a710 3213 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3214 * @phba: Pointer to HBA context object.
3215 *
3216 * This function is called before resetting an HBA. This
3217 * function requests HBA to quiesce DMAs before a reset.
3218 **/
2e0fef85 3219void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3220{
65a29c16
JS
3221 uint32_t __iomem *resp_buf;
3222 uint32_t __iomem *mbox_buf;
9290831f
JS
3223 volatile uint32_t mbox;
3224 uint32_t hc_copy;
3225 int i;
3226 uint8_t hdrtype;
3227
3228 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3229 if (hdrtype != 0x80 ||
3230 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3231 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3232 return;
3233
3234 /*
3235 * Tell the other part of the chip to suspend temporarily all
3236 * its DMA activity.
3237 */
65a29c16 3238 resp_buf = phba->MBslimaddr;
9290831f
JS
3239
3240 /* Disable the error attention */
3241 hc_copy = readl(phba->HCregaddr);
3242 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3243 readl(phba->HCregaddr); /* flush */
2e0fef85 3244 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3245
3246 if (readl(phba->HAregaddr) & HA_ERATT) {
3247 /* Clear Chip error bit */
3248 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3249 phba->pport->stopped = 1;
9290831f
JS
3250 }
3251
3252 mbox = 0;
3253 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3254 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3255
3256 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3257 mbox_buf = phba->MBslimaddr;
9290831f
JS
3258 writel(mbox, mbox_buf);
3259
3260 for (i = 0;
3261 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3262 mdelay(1);
3263
3264 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3265 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3266 phba->pport->stopped)
9290831f
JS
3267 goto restore_hc;
3268 else
3269 goto clear_errat;
3270 }
3271
3272 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3273 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3274 mdelay(1);
3275
3276clear_errat:
3277
3278 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3279 mdelay(1);
3280
3281 if (readl(phba->HAregaddr) & HA_ERATT) {
3282 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3283 phba->pport->stopped = 1;
9290831f
JS
3284 }
3285
3286restore_hc:
2e0fef85 3287 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3288 writel(hc_copy, phba->HCregaddr);
3289 readl(phba->HCregaddr); /* flush */
3290}
3291
e59058c4 3292/**
3621a710 3293 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3294 * @phba: Pointer to HBA context object.
3295 *
3296 * This function issues a kill_board mailbox command and waits for
3297 * the error attention interrupt. This function is called for stopping
3298 * the firmware processing. The caller is not required to hold any
3299 * locks. This function calls lpfc_hba_down_post function to free
3300 * any pending commands after the kill. The function will return 1 when it
3301 * fails to kill the board else will return 0.
3302 **/
41415862 3303int
2e0fef85 3304lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3305{
3306 struct lpfc_sli *psli;
3307 LPFC_MBOXQ_t *pmb;
3308 uint32_t status;
3309 uint32_t ha_copy;
3310 int retval;
3311 int i = 0;
dea3101e 3312
41415862 3313 psli = &phba->sli;
dea3101e 3314
41415862 3315 /* Kill HBA */
ed957684 3316 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3317 "0329 Kill HBA Data: x%x x%x\n",
3318 phba->pport->port_state, psli->sli_flag);
41415862 3319
98c9ea5c
JS
3320 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3321 if (!pmb)
41415862 3322 return 1;
41415862
JW
3323
3324 /* Disable the error attention */
2e0fef85 3325 spin_lock_irq(&phba->hbalock);
41415862
JW
3326 status = readl(phba->HCregaddr);
3327 status &= ~HC_ERINT_ENA;
3328 writel(status, phba->HCregaddr);
3329 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3330 phba->link_flag |= LS_IGNORE_ERATT;
3331 spin_unlock_irq(&phba->hbalock);
41415862
JW
3332
3333 lpfc_kill_board(phba, pmb);
3334 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3335 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3336
3337 if (retval != MBX_SUCCESS) {
3338 if (retval != MBX_BUSY)
3339 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 "2752 KILL_BOARD command failed retval %d\n",
3342 retval);
2e0fef85
JS
3343 spin_lock_irq(&phba->hbalock);
3344 phba->link_flag &= ~LS_IGNORE_ERATT;
3345 spin_unlock_irq(&phba->hbalock);
41415862
JW
3346 return 1;
3347 }
3348
f4b4c68f
JS
3349 spin_lock_irq(&phba->hbalock);
3350 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3351 spin_unlock_irq(&phba->hbalock);
9290831f 3352
41415862
JW
3353 mempool_free(pmb, phba->mbox_mem_pool);
3354
3355 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3356 * attention every 100ms for 3 seconds. If we don't get ERATT after
3357 * 3 seconds we still set HBA_ERROR state because the status of the
3358 * board is now undefined.
3359 */
3360 ha_copy = readl(phba->HAregaddr);
3361
3362 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3363 mdelay(100);
3364 ha_copy = readl(phba->HAregaddr);
3365 }
3366
3367 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3368 if (ha_copy & HA_ERATT) {
3369 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3370 phba->pport->stopped = 1;
9290831f 3371 }
2e0fef85 3372 spin_lock_irq(&phba->hbalock);
41415862 3373 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3374 psli->mbox_active = NULL;
2e0fef85
JS
3375 phba->link_flag &= ~LS_IGNORE_ERATT;
3376 spin_unlock_irq(&phba->hbalock);
41415862 3377
41415862 3378 lpfc_hba_down_post(phba);
2e0fef85 3379 phba->link_state = LPFC_HBA_ERROR;
41415862 3380
2e0fef85 3381 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3382}
3383
e59058c4 3384/**
3772a991 3385 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3386 * @phba: Pointer to HBA context object.
3387 *
3388 * This function resets the HBA by writing HC_INITFF to the control
3389 * register. After the HBA resets, this function resets all the iocb ring
3390 * indices. This function disables PCI layer parity checking during
3391 * the reset.
3392 * This function returns 0 always.
3393 * The caller is not required to hold any locks.
3394 **/
41415862 3395int
2e0fef85 3396lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3397{
41415862 3398 struct lpfc_sli *psli;
dea3101e 3399 struct lpfc_sli_ring *pring;
41415862 3400 uint16_t cfg_value;
dea3101e 3401 int i;
dea3101e 3402
41415862 3403 psli = &phba->sli;
dea3101e 3404
41415862
JW
3405 /* Reset HBA */
3406 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3407 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3408 phba->pport->port_state, psli->sli_flag);
dea3101e 3409
3410 /* perform board reset */
3411 phba->fc_eventTag = 0;
4d9ab994 3412 phba->link_events = 0;
2e0fef85
JS
3413 phba->pport->fc_myDID = 0;
3414 phba->pport->fc_prevDID = 0;
dea3101e 3415
41415862
JW
3416 /* Turn off parity checking and serr during the physical reset */
3417 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3418 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3419 (cfg_value &
3420 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3421
3772a991
JS
3422 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3423
41415862
JW
3424 /* Now toggle INITFF bit in the Host Control Register */
3425 writel(HC_INITFF, phba->HCregaddr);
3426 mdelay(1);
3427 readl(phba->HCregaddr); /* flush */
3428 writel(0, phba->HCregaddr);
3429 readl(phba->HCregaddr); /* flush */
3430
3431 /* Restore PCI cmd register */
3432 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3433
3434 /* Initialize relevant SLI info */
41415862
JW
3435 for (i = 0; i < psli->num_rings; i++) {
3436 pring = &psli->ring[i];
dea3101e 3437 pring->flag = 0;
3438 pring->rspidx = 0;
3439 pring->next_cmdidx = 0;
3440 pring->local_getidx = 0;
3441 pring->cmdidx = 0;
3442 pring->missbufcnt = 0;
3443 }
dea3101e 3444
2e0fef85 3445 phba->link_state = LPFC_WARM_START;
41415862
JW
3446 return 0;
3447}
3448
e59058c4 3449/**
da0436e9
JS
3450 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3451 * @phba: Pointer to HBA context object.
3452 *
3453 * This function resets a SLI4 HBA. This function disables PCI layer parity
3454 * checking during resets the device. The caller is not required to hold
3455 * any locks.
3456 *
3457 * This function returns 0 always.
3458 **/
3459int
3460lpfc_sli4_brdreset(struct lpfc_hba *phba)
3461{
3462 struct lpfc_sli *psli = &phba->sli;
3463 uint16_t cfg_value;
3464 uint8_t qindx;
3465
3466 /* Reset HBA */
3467 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3468 "0295 Reset HBA Data: x%x x%x\n",
3469 phba->pport->port_state, psli->sli_flag);
3470
3471 /* perform board reset */
3472 phba->fc_eventTag = 0;
4d9ab994 3473 phba->link_events = 0;
da0436e9
JS
3474 phba->pport->fc_myDID = 0;
3475 phba->pport->fc_prevDID = 0;
3476
3477 /* Turn off parity checking and serr during the physical reset */
3478 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3479 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3480 (cfg_value &
3481 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3482
3483 spin_lock_irq(&phba->hbalock);
3484 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3485 phba->fcf.fcf_flag = 0;
3486 /* Clean up the child queue list for the CQs */
3487 list_del_init(&phba->sli4_hba.mbx_wq->list);
3488 list_del_init(&phba->sli4_hba.els_wq->list);
3489 list_del_init(&phba->sli4_hba.hdr_rq->list);
3490 list_del_init(&phba->sli4_hba.dat_rq->list);
3491 list_del_init(&phba->sli4_hba.mbx_cq->list);
3492 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3493 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3494 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3495 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3496 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3497 spin_unlock_irq(&phba->hbalock);
3498
3499 /* Now physically reset the device */
3500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3501 "0389 Performing PCI function reset!\n");
3502 /* Perform FCoE PCI function reset */
3503 lpfc_pci_function_reset(phba);
3504
3505 return 0;
3506}
3507
3508/**
3509 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3510 * @phba: Pointer to HBA context object.
3511 *
3512 * This function is called in the SLI initialization code path to
3513 * restart the HBA. The caller is not required to hold any lock.
3514 * This function writes MBX_RESTART mailbox command to the SLIM and
3515 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3516 * function to free any pending commands. The function enables
3517 * POST only during the first initialization. The function returns zero.
3518 * The function does not guarantee completion of MBX_RESTART mailbox
3519 * command before the return of this function.
3520 **/
da0436e9
JS
3521static int
3522lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3523{
3524 MAILBOX_t *mb;
3525 struct lpfc_sli *psli;
41415862
JW
3526 volatile uint32_t word0;
3527 void __iomem *to_slim;
0d878419 3528 uint32_t hba_aer_enabled;
41415862 3529
2e0fef85 3530 spin_lock_irq(&phba->hbalock);
41415862 3531
0d878419
JS
3532 /* Take PCIe device Advanced Error Reporting (AER) state */
3533 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3534
41415862
JW
3535 psli = &phba->sli;
3536
3537 /* Restart HBA */
3538 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3539 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3540 phba->pport->port_state, psli->sli_flag);
41415862
JW
3541
3542 word0 = 0;
3543 mb = (MAILBOX_t *) &word0;
3544 mb->mbxCommand = MBX_RESTART;
3545 mb->mbxHc = 1;
3546
9290831f
JS
3547 lpfc_reset_barrier(phba);
3548
41415862
JW
3549 to_slim = phba->MBslimaddr;
3550 writel(*(uint32_t *) mb, to_slim);
3551 readl(to_slim); /* flush */
3552
3553 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3554 if (phba->pport->port_state)
41415862 3555 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3556 else
41415862 3557 word0 = 0; /* This is really setting up word1 */
65a29c16 3558 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3559 writel(*(uint32_t *) mb, to_slim);
3560 readl(to_slim); /* flush */
dea3101e 3561
41415862 3562 lpfc_sli_brdreset(phba);
2e0fef85
JS
3563 phba->pport->stopped = 0;
3564 phba->link_state = LPFC_INIT_START;
da0436e9 3565 phba->hba_flag = 0;
2e0fef85 3566 spin_unlock_irq(&phba->hbalock);
41415862 3567
64ba8818
JS
3568 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3569 psli->stats_start = get_seconds();
3570
eaf15d5b
JS
3571 /* Give the INITFF and Post time to settle. */
3572 mdelay(100);
41415862 3573
0d878419
JS
3574 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3575 if (hba_aer_enabled)
3576 pci_disable_pcie_error_reporting(phba->pcidev);
3577
41415862 3578 lpfc_hba_down_post(phba);
dea3101e 3579
3580 return 0;
3581}
3582
da0436e9
JS
3583/**
3584 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3585 * @phba: Pointer to HBA context object.
3586 *
3587 * This function is called in the SLI initialization code path to restart
3588 * a SLI4 HBA. The caller is not required to hold any lock.
3589 * At the end of the function, it calls lpfc_hba_down_post function to
3590 * free any pending commands.
3591 **/
3592static int
3593lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3594{
3595 struct lpfc_sli *psli = &phba->sli;
75baf696 3596 uint32_t hba_aer_enabled;
da0436e9
JS
3597
3598 /* Restart HBA */
3599 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3600 "0296 Restart HBA Data: x%x x%x\n",
3601 phba->pport->port_state, psli->sli_flag);
3602
75baf696
JS
3603 /* Take PCIe device Advanced Error Reporting (AER) state */
3604 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3605
da0436e9
JS
3606 lpfc_sli4_brdreset(phba);
3607
3608 spin_lock_irq(&phba->hbalock);
3609 phba->pport->stopped = 0;
3610 phba->link_state = LPFC_INIT_START;
3611 phba->hba_flag = 0;
3612 spin_unlock_irq(&phba->hbalock);
3613
3614 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3615 psli->stats_start = get_seconds();
3616
75baf696
JS
3617 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3618 if (hba_aer_enabled)
3619 pci_disable_pcie_error_reporting(phba->pcidev);
3620
da0436e9
JS
3621 lpfc_hba_down_post(phba);
3622
3623 return 0;
3624}
3625
3626/**
3627 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3628 * @phba: Pointer to HBA context object.
3629 *
3630 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3631 * API jump table function pointer from the lpfc_hba struct.
3632**/
3633int
3634lpfc_sli_brdrestart(struct lpfc_hba *phba)
3635{
3636 return phba->lpfc_sli_brdrestart(phba);
3637}
3638
e59058c4 3639/**
3621a710 3640 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3641 * @phba: Pointer to HBA context object.
3642 *
3643 * This function is called after a HBA restart to wait for successful
3644 * restart of the HBA. Successful restart of the HBA is indicated by
3645 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3646 * iteration, the function will restart the HBA again. The function returns
3647 * zero if HBA successfully restarted else returns negative error code.
3648 **/
dea3101e 3649static int
3650lpfc_sli_chipset_init(struct lpfc_hba *phba)
3651{
3652 uint32_t status, i = 0;
3653
3654 /* Read the HBA Host Status Register */
3655 status = readl(phba->HSregaddr);
3656
3657 /* Check status register to see what current state is */
3658 i = 0;
3659 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3660
3661 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3662 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3663 * 4.
3664 */
3665 if (i++ >= 20) {
3666 /* Adapter failed to init, timeout, status reg
3667 <status> */
ed957684 3668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3669 "0436 Adapter failed to init, "
09372820
JS
3670 "timeout, status reg x%x, "
3671 "FW Data: A8 x%x AC x%x\n", status,
3672 readl(phba->MBslimaddr + 0xa8),
3673 readl(phba->MBslimaddr + 0xac));
2e0fef85 3674 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3675 return -ETIMEDOUT;
3676 }
3677
3678 /* Check to see if any errors occurred during init */
3679 if (status & HS_FFERM) {
3680 /* ERROR: During chipset initialization */
3681 /* Adapter failed to init, chipset, status reg
3682 <status> */
ed957684 3683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3684 "0437 Adapter failed to init, "
09372820
JS
3685 "chipset, status reg x%x, "
3686 "FW Data: A8 x%x AC x%x\n", status,
3687 readl(phba->MBslimaddr + 0xa8),
3688 readl(phba->MBslimaddr + 0xac));
2e0fef85 3689 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3690 return -EIO;
3691 }
3692
3693 if (i <= 5) {
3694 msleep(10);
3695 } else if (i <= 10) {
3696 msleep(500);
3697 } else {
3698 msleep(2500);
3699 }
3700
3701 if (i == 15) {
2e0fef85 3702 /* Do post */
92d7f7b0 3703 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3704 lpfc_sli_brdrestart(phba);
dea3101e 3705 }
3706 /* Read the HBA Host Status Register */
3707 status = readl(phba->HSregaddr);
3708 }
3709
3710 /* Check to see if any errors occurred during init */
3711 if (status & HS_FFERM) {
3712 /* ERROR: During chipset initialization */
3713 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3715 "0438 Adapter failed to init, chipset, "
09372820
JS
3716 "status reg x%x, "
3717 "FW Data: A8 x%x AC x%x\n", status,
3718 readl(phba->MBslimaddr + 0xa8),
3719 readl(phba->MBslimaddr + 0xac));
2e0fef85 3720 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3721 return -EIO;
3722 }
3723
3724 /* Clear all interrupt enable conditions */
3725 writel(0, phba->HCregaddr);
3726 readl(phba->HCregaddr); /* flush */
3727
3728 /* setup host attn register */
3729 writel(0xffffffff, phba->HAregaddr);
3730 readl(phba->HAregaddr); /* flush */
3731 return 0;
3732}
3733
e59058c4 3734/**
3621a710 3735 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3736 *
3737 * This function calculates and returns the number of HBQs required to be
3738 * configured.
3739 **/
78b2d852 3740int
ed957684
JS
3741lpfc_sli_hbq_count(void)
3742{
92d7f7b0 3743 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3744}
3745
e59058c4 3746/**
3621a710 3747 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3748 *
3749 * This function adds the number of hbq entries in every HBQ to get
3750 * the total number of hbq entries required for the HBA and returns
3751 * the total count.
3752 **/
ed957684
JS
3753static int
3754lpfc_sli_hbq_entry_count(void)
3755{
3756 int hbq_count = lpfc_sli_hbq_count();
3757 int count = 0;
3758 int i;
3759
3760 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3761 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3762 return count;
3763}
3764
e59058c4 3765/**
3621a710 3766 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3767 *
3768 * This function calculates amount of memory required for all hbq entries
3769 * to be configured and returns the total memory required.
3770 **/
dea3101e 3771int
ed957684
JS
3772lpfc_sli_hbq_size(void)
3773{
3774 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3775}
3776
e59058c4 3777/**
3621a710 3778 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3779 * @phba: Pointer to HBA context object.
3780 *
3781 * This function is called during the SLI initialization to configure
3782 * all the HBQs and post buffers to the HBQ. The caller is not
3783 * required to hold any locks. This function will return zero if successful
3784 * else it will return negative error code.
3785 **/
ed957684
JS
3786static int
3787lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3788{
3789 int hbq_count = lpfc_sli_hbq_count();
3790 LPFC_MBOXQ_t *pmb;
3791 MAILBOX_t *pmbox;
3792 uint32_t hbqno;
3793 uint32_t hbq_entry_index;
ed957684 3794
92d7f7b0
JS
3795 /* Get a Mailbox buffer to setup mailbox
3796 * commands for HBA initialization
3797 */
ed957684
JS
3798 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3799
3800 if (!pmb)
3801 return -ENOMEM;
3802
04c68496 3803 pmbox = &pmb->u.mb;
ed957684
JS
3804
3805 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3806 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3807 phba->hbq_in_use = 1;
ed957684
JS
3808
3809 hbq_entry_index = 0;
3810 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3811 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3812 phba->hbqs[hbqno].hbqPutIdx = 0;
3813 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3814 phba->hbqs[hbqno].entry_count =
92d7f7b0 3815 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3816 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3817 hbq_entry_index, pmb);
ed957684
JS
3818 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3819
3820 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3821 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3822 mbxStatus <status>, ring <num> */
3823
3824 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3825 LOG_SLI | LOG_VPORT,
e8b62011 3826 "1805 Adapter failed to init. "
ed957684 3827 "Data: x%x x%x x%x\n",
e8b62011 3828 pmbox->mbxCommand,
ed957684
JS
3829 pmbox->mbxStatus, hbqno);
3830
3831 phba->link_state = LPFC_HBA_ERROR;
3832 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 3833 return -ENXIO;
ed957684
JS
3834 }
3835 }
3836 phba->hbq_count = hbq_count;
3837
ed957684
JS
3838 mempool_free(pmb, phba->mbox_mem_pool);
3839
92d7f7b0 3840 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3841 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3842 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3843 return 0;
3844}
3845
4f774513
JS
3846/**
3847 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3848 * @phba: Pointer to HBA context object.
3849 *
3850 * This function is called during the SLI initialization to configure
3851 * all the HBQs and post buffers to the HBQ. The caller is not
3852 * required to hold any locks. This function will return zero if successful
3853 * else it will return negative error code.
3854 **/
3855static int
3856lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3857{
3858 phba->hbq_in_use = 1;
3859 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3860 phba->hbq_count = 1;
3861 /* Initially populate or replenish the HBQs */
3862 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3863 return 0;
3864}
3865
e59058c4 3866/**
3621a710 3867 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3868 * @phba: Pointer to HBA context object.
3869 * @sli_mode: sli mode - 2/3
3870 *
3871 * This function is called by the sli intialization code path
3872 * to issue config_port mailbox command. This function restarts the
3873 * HBA firmware and issues a config_port mailbox command to configure
3874 * the SLI interface in the sli mode specified by sli_mode
3875 * variable. The caller is not required to hold any locks.
3876 * The function returns 0 if successful, else returns negative error
3877 * code.
3878 **/
9399627f
JS
3879int
3880lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 3881{
3882 LPFC_MBOXQ_t *pmb;
3883 uint32_t resetcount = 0, rc = 0, done = 0;
3884
3885 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3886 if (!pmb) {
2e0fef85 3887 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3888 return -ENOMEM;
3889 }
3890
ed957684 3891 phba->sli_rev = sli_mode;
dea3101e 3892 while (resetcount < 2 && !done) {
2e0fef85 3893 spin_lock_irq(&phba->hbalock);
1c067a42 3894 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3895 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3896 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3897 lpfc_sli_brdrestart(phba);
dea3101e 3898 rc = lpfc_sli_chipset_init(phba);
3899 if (rc)
3900 break;
3901
2e0fef85 3902 spin_lock_irq(&phba->hbalock);
1c067a42 3903 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3904 spin_unlock_irq(&phba->hbalock);
dea3101e 3905 resetcount++;
3906
ed957684
JS
3907 /* Call pre CONFIG_PORT mailbox command initialization. A
3908 * value of 0 means the call was successful. Any other
3909 * nonzero value is a failure, but if ERESTART is returned,
3910 * the driver may reset the HBA and try again.
3911 */
dea3101e 3912 rc = lpfc_config_port_prep(phba);
3913 if (rc == -ERESTART) {
ed957684 3914 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3915 continue;
34b02dcd 3916 } else if (rc)
dea3101e 3917 break;
2e0fef85 3918 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 3919 lpfc_config_port(phba, pmb);
3920 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3921 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3922 LPFC_SLI3_HBQ_ENABLED |
3923 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
3924 LPFC_SLI3_BG_ENABLED |
3925 LPFC_SLI3_DSS_ENABLED);
ed957684 3926 if (rc != MBX_SUCCESS) {
dea3101e 3927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3928 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3929 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3930 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3931 spin_lock_irq(&phba->hbalock);
04c68496 3932 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3933 spin_unlock_irq(&phba->hbalock);
3934 rc = -ENXIO;
04c68496
JS
3935 } else {
3936 /* Allow asynchronous mailbox command to go through */
3937 spin_lock_irq(&phba->hbalock);
3938 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3939 spin_unlock_irq(&phba->hbalock);
ed957684 3940 done = 1;
04c68496 3941 }
dea3101e 3942 }
ed957684
JS
3943 if (!done) {
3944 rc = -EINVAL;
3945 goto do_prep_failed;
3946 }
04c68496
JS
3947 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3948 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3949 rc = -ENXIO;
3950 goto do_prep_failed;
3951 }
04c68496 3952 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3953 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3954 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3955 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3956 phba->max_vpi : phba->max_vports;
3957
34b02dcd
JS
3958 } else
3959 phba->max_vpi = 0;
bc73905a
JS
3960 phba->fips_level = 0;
3961 phba->fips_spec_rev = 0;
3962 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 3963 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
3964 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
3965 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
3966 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3967 "2850 Security Crypto Active. FIPS x%d "
3968 "(Spec Rev: x%d)",
3969 phba->fips_level, phba->fips_spec_rev);
3970 }
3971 if (pmb->u.mb.un.varCfgPort.sec_err) {
3972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3973 "2856 Config Port Security Crypto "
3974 "Error: x%x ",
3975 pmb->u.mb.un.varCfgPort.sec_err);
3976 }
04c68496 3977 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3978 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3979 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3980 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
3981
3982 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3983 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
3984
3985 if (phba->cfg_enable_bg) {
04c68496 3986 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
3987 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3988 else
3989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3990 "0443 Adapter did not grant "
3991 "BlockGuard\n");
3992 }
34b02dcd 3993 } else {
8f34f4ce 3994 phba->hbq_get = NULL;
34b02dcd 3995 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 3996 phba->max_vpi = 0;
ed957684 3997 }
92d7f7b0 3998do_prep_failed:
ed957684
JS
3999 mempool_free(pmb, phba->mbox_mem_pool);
4000 return rc;
4001}
4002
e59058c4
JS
4003
4004/**
3621a710 4005 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4006 * @phba: Pointer to HBA context object.
4007 *
4008 * This function is the main SLI intialization function. This function
4009 * is called by the HBA intialization code, HBA reset code and HBA
4010 * error attention handler code. Caller is not required to hold any
4011 * locks. This function issues config_port mailbox command to configure
4012 * the SLI, setup iocb rings and HBQ rings. In the end the function
4013 * calls the config_port_post function to issue init_link mailbox
4014 * command and to start the discovery. The function will return zero
4015 * if successful, else it will return negative error code.
4016 **/
ed957684
JS
4017int
4018lpfc_sli_hba_setup(struct lpfc_hba *phba)
4019{
4020 uint32_t rc;
92d7f7b0 4021 int mode = 3;
ed957684
JS
4022
4023 switch (lpfc_sli_mode) {
4024 case 2:
78b2d852 4025 if (phba->cfg_enable_npiv) {
92d7f7b0 4026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4027 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4028 "parameter (%d) to auto (0).\n",
e8b62011 4029 lpfc_sli_mode);
92d7f7b0
JS
4030 break;
4031 }
ed957684
JS
4032 mode = 2;
4033 break;
4034 case 0:
4035 case 3:
4036 break;
4037 default:
92d7f7b0 4038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4039 "1819 Unrecognized lpfc_sli_mode "
4040 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4041
4042 break;
4043 }
4044
9399627f
JS
4045 rc = lpfc_sli_config_port(phba, mode);
4046
ed957684 4047 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4049 "1820 Unable to select SLI-3. "
4050 "Not supported by adapter.\n");
ed957684 4051 if (rc && mode != 2)
9399627f 4052 rc = lpfc_sli_config_port(phba, 2);
ed957684 4053 if (rc)
dea3101e 4054 goto lpfc_sli_hba_setup_error;
4055
0d878419
JS
4056 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4057 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4058 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4059 if (!rc) {
4060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4061 "2709 This device supports "
4062 "Advanced Error Reporting (AER)\n");
4063 spin_lock_irq(&phba->hbalock);
4064 phba->hba_flag |= HBA_AER_ENABLED;
4065 spin_unlock_irq(&phba->hbalock);
4066 } else {
4067 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4068 "2708 This device does not support "
4069 "Advanced Error Reporting (AER)\n");
4070 phba->cfg_aer_support = 0;
4071 }
4072 }
4073
ed957684
JS
4074 if (phba->sli_rev == 3) {
4075 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4076 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4077 } else {
4078 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4079 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4080 phba->sli3_options = 0;
ed957684
JS
4081 }
4082
4083 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4084 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4085 phba->sli_rev, phba->max_vpi);
ed957684 4086 rc = lpfc_sli_ring_map(phba);
dea3101e 4087
4088 if (rc)
4089 goto lpfc_sli_hba_setup_error;
4090
9399627f 4091 /* Init HBQs */
ed957684
JS
4092 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4093 rc = lpfc_sli_hbq_setup(phba);
4094 if (rc)
4095 goto lpfc_sli_hba_setup_error;
4096 }
04c68496 4097 spin_lock_irq(&phba->hbalock);
dea3101e 4098 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4099 spin_unlock_irq(&phba->hbalock);
dea3101e 4100
4101 rc = lpfc_config_port_post(phba);
4102 if (rc)
4103 goto lpfc_sli_hba_setup_error;
4104
ed957684
JS
4105 return rc;
4106
92d7f7b0 4107lpfc_sli_hba_setup_error:
2e0fef85 4108 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4110 "0445 Firmware initialization failed\n");
dea3101e 4111 return rc;
4112}
4113
e59058c4 4114/**
da0436e9
JS
4115 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4116 * @phba: Pointer to HBA context object.
4117 * @mboxq: mailbox pointer.
4118 * This function issue a dump mailbox command to read config region
4119 * 23 and parse the records in the region and populate driver
4120 * data structure.
e59058c4 4121 **/
da0436e9
JS
4122static int
4123lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4124 LPFC_MBOXQ_t *mboxq)
dea3101e 4125{
da0436e9
JS
4126 struct lpfc_dmabuf *mp;
4127 struct lpfc_mqe *mqe;
4128 uint32_t data_length;
4129 int rc;
dea3101e 4130
da0436e9
JS
4131 /* Program the default value of vlan_id and fc_map */
4132 phba->valid_vlan = 0;
4133 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4134 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4135 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4136
da0436e9
JS
4137 mqe = &mboxq->u.mqe;
4138 if (lpfc_dump_fcoe_param(phba, mboxq))
4139 return -ENOMEM;
4140
4141 mp = (struct lpfc_dmabuf *) mboxq->context1;
4142 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4143
4144 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4145 "(%d):2571 Mailbox cmd x%x Status x%x "
4146 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4147 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4148 "CQ: x%x x%x x%x x%x\n",
4149 mboxq->vport ? mboxq->vport->vpi : 0,
4150 bf_get(lpfc_mqe_command, mqe),
4151 bf_get(lpfc_mqe_status, mqe),
4152 mqe->un.mb_words[0], mqe->un.mb_words[1],
4153 mqe->un.mb_words[2], mqe->un.mb_words[3],
4154 mqe->un.mb_words[4], mqe->un.mb_words[5],
4155 mqe->un.mb_words[6], mqe->un.mb_words[7],
4156 mqe->un.mb_words[8], mqe->un.mb_words[9],
4157 mqe->un.mb_words[10], mqe->un.mb_words[11],
4158 mqe->un.mb_words[12], mqe->un.mb_words[13],
4159 mqe->un.mb_words[14], mqe->un.mb_words[15],
4160 mqe->un.mb_words[16], mqe->un.mb_words[50],
4161 mboxq->mcqe.word0,
4162 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4163 mboxq->mcqe.trailer);
4164
4165 if (rc) {
4166 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4167 kfree(mp);
4168 return -EIO;
4169 }
4170 data_length = mqe->un.mb_words[5];
a0c87cbd 4171 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4172 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4173 kfree(mp);
da0436e9 4174 return -EIO;
d11e31dd 4175 }
dea3101e 4176
da0436e9
JS
4177 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4178 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4179 kfree(mp);
4180 return 0;
4181}
e59058c4
JS
4182
4183/**
da0436e9
JS
4184 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4185 * @phba: pointer to lpfc hba data structure.
4186 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4187 * @vpd: pointer to the memory to hold resulting port vpd data.
4188 * @vpd_size: On input, the number of bytes allocated to @vpd.
4189 * On output, the number of data bytes in @vpd.
e59058c4 4190 *
da0436e9
JS
4191 * This routine executes a READ_REV SLI4 mailbox command. In
4192 * addition, this routine gets the port vpd data.
4193 *
4194 * Return codes
af901ca1 4195 * 0 - successful
d439d286 4196 * -ENOMEM - could not allocated memory.
e59058c4 4197 **/
da0436e9
JS
4198static int
4199lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4200 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4201{
da0436e9
JS
4202 int rc = 0;
4203 uint32_t dma_size;
4204 struct lpfc_dmabuf *dmabuf;
4205 struct lpfc_mqe *mqe;
dea3101e 4206
da0436e9
JS
4207 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4208 if (!dmabuf)
4209 return -ENOMEM;
4210
4211 /*
4212 * Get a DMA buffer for the vpd data resulting from the READ_REV
4213 * mailbox command.
a257bf90 4214 */
da0436e9
JS
4215 dma_size = *vpd_size;
4216 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4217 dma_size,
4218 &dmabuf->phys,
4219 GFP_KERNEL);
4220 if (!dmabuf->virt) {
4221 kfree(dmabuf);
4222 return -ENOMEM;
a257bf90 4223 }
da0436e9 4224 memset(dmabuf->virt, 0, dma_size);
a257bf90 4225
da0436e9
JS
4226 /*
4227 * The SLI4 implementation of READ_REV conflicts at word1,
4228 * bits 31:16 and SLI4 adds vpd functionality not present
4229 * in SLI3. This code corrects the conflicts.
1dcb58e5 4230 */
da0436e9
JS
4231 lpfc_read_rev(phba, mboxq);
4232 mqe = &mboxq->u.mqe;
4233 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4234 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4235 mqe->un.read_rev.word1 &= 0x0000FFFF;
4236 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4237 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4238
4239 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4240 if (rc) {
4241 dma_free_coherent(&phba->pcidev->dev, dma_size,
4242 dmabuf->virt, dmabuf->phys);
def9c7a9 4243 kfree(dmabuf);
da0436e9
JS
4244 return -EIO;
4245 }
1dcb58e5 4246
da0436e9
JS
4247 /*
4248 * The available vpd length cannot be bigger than the
4249 * DMA buffer passed to the port. Catch the less than
4250 * case and update the caller's size.
4251 */
4252 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4253 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4254
d7c47992
JS
4255 memcpy(vpd, dmabuf->virt, *vpd_size);
4256
da0436e9
JS
4257 dma_free_coherent(&phba->pcidev->dev, dma_size,
4258 dmabuf->virt, dmabuf->phys);
4259 kfree(dmabuf);
4260 return 0;
dea3101e 4261}
4262
e59058c4 4263/**
da0436e9
JS
4264 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4265 * @phba: pointer to lpfc hba data structure.
e59058c4 4266 *
da0436e9
JS
4267 * This routine is called to explicitly arm the SLI4 device's completion and
4268 * event queues
4269 **/
4270static void
4271lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4272{
4273 uint8_t fcp_eqidx;
4274
4275 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4276 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4277 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4278 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4279 LPFC_QUEUE_REARM);
4280 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4281 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4282 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4283 LPFC_QUEUE_REARM);
4284}
4285
4286/**
4287 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4288 * @phba: Pointer to HBA context object.
4289 *
4290 * This function is the main SLI4 device intialization PCI function. This
4291 * function is called by the HBA intialization code, HBA reset code and
4292 * HBA error attention handler code. Caller is not required to hold any
4293 * locks.
4294 **/
4295int
4296lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4297{
4298 int rc;
4299 LPFC_MBOXQ_t *mboxq;
4300 struct lpfc_mqe *mqe;
4301 uint8_t *vpd;
4302 uint32_t vpd_size;
4303 uint32_t ftr_rsp = 0;
4304 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4305 struct lpfc_vport *vport = phba->pport;
4306 struct lpfc_dmabuf *mp;
4307
4308 /* Perform a PCI function reset to start from clean */
4309 rc = lpfc_pci_function_reset(phba);
4310 if (unlikely(rc))
4311 return -ENODEV;
4312
4313 /* Check the HBA Host Status Register for readyness */
4314 rc = lpfc_sli4_post_status_check(phba);
4315 if (unlikely(rc))
4316 return -ENODEV;
4317 else {
4318 spin_lock_irq(&phba->hbalock);
4319 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4320 spin_unlock_irq(&phba->hbalock);
4321 }
4322
4323 /*
4324 * Allocate a single mailbox container for initializing the
4325 * port.
4326 */
4327 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4328 if (!mboxq)
4329 return -ENOMEM;
4330
4331 /*
4332 * Continue initialization with default values even if driver failed
4333 * to read FCoE param config regions
4334 */
4335 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4336 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4337 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4338
4339 /* Issue READ_REV to collect vpd and FW information. */
49198b37 4340 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
4341 vpd = kzalloc(vpd_size, GFP_KERNEL);
4342 if (!vpd) {
4343 rc = -ENOMEM;
4344 goto out_free_mbox;
4345 }
4346
4347 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4348 if (unlikely(rc))
4349 goto out_free_vpd;
4350
4351 mqe = &mboxq->u.mqe;
f1126688
JS
4352 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4353 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4354 phba->hba_flag |= HBA_FCOE_SUPPORT;
45ed1190
JS
4355
4356 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4357 LPFC_DCBX_CEE_MODE)
4358 phba->hba_flag |= HBA_FIP_SUPPORT;
4359 else
4360 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4361
f1126688
JS
4362 if (phba->sli_rev != LPFC_SLI_REV4 ||
4363 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4364 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4365 "0376 READ_REV Error. SLI Level %d "
4366 "FCoE enabled %d\n",
f1126688 4367 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4368 rc = -EIO;
4369 goto out_free_vpd;
4370 }
da0436e9
JS
4371 /*
4372 * Evaluate the read rev and vpd data. Populate the driver
4373 * state with the results. If this routine fails, the failure
4374 * is not fatal as the driver will use generic values.
4375 */
4376 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4377 if (unlikely(!rc)) {
4378 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4379 "0377 Error %d parsing vpd. "
4380 "Using defaults.\n", rc);
4381 rc = 0;
4382 }
4383
f1126688
JS
4384 /* Save information as VPD data */
4385 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4386 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4387 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4388 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4389 &mqe->un.read_rev);
4390 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4391 &mqe->un.read_rev);
4392 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4393 &mqe->un.read_rev);
4394 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4395 &mqe->un.read_rev);
4396 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4397 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4398 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4399 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4400 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4401 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4402 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4403 "(%d):0380 READ_REV Status x%x "
4404 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4405 mboxq->vport ? mboxq->vport->vpi : 0,
4406 bf_get(lpfc_mqe_status, mqe),
4407 phba->vpd.rev.opFwName,
4408 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4409 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4410
4411 /*
4412 * Discover the port's supported feature set and match it against the
4413 * hosts requests.
4414 */
4415 lpfc_request_features(phba, mboxq);
4416 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4417 if (unlikely(rc)) {
4418 rc = -EIO;
4419 goto out_free_vpd;
4420 }
4421
4422 /*
4423 * The port must support FCP initiator mode as this is the
4424 * only mode running in the host.
4425 */
4426 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4427 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4428 "0378 No support for fcpi mode.\n");
4429 ftr_rsp++;
4430 }
4431
4432 /*
4433 * If the port cannot support the host's requested features
4434 * then turn off the global config parameters to disable the
4435 * feature in the driver. This is not a fatal error.
4436 */
4437 if ((phba->cfg_enable_bg) &&
4438 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4439 ftr_rsp++;
4440
4441 if (phba->max_vpi && phba->cfg_enable_npiv &&
4442 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4443 ftr_rsp++;
4444
4445 if (ftr_rsp) {
4446 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4447 "0379 Feature Mismatch Data: x%08x %08x "
4448 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4449 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4450 phba->cfg_enable_npiv, phba->max_vpi);
4451 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4452 phba->cfg_enable_bg = 0;
4453 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4454 phba->cfg_enable_npiv = 0;
4455 }
4456
4457 /* These SLI3 features are assumed in SLI4 */
4458 spin_lock_irq(&phba->hbalock);
4459 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4460 spin_unlock_irq(&phba->hbalock);
4461
4462 /* Read the port's service parameters. */
9f1177a3
JS
4463 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4464 if (rc) {
4465 phba->link_state = LPFC_HBA_ERROR;
4466 rc = -ENOMEM;
4467 goto out_free_vpd;
4468 }
4469
da0436e9
JS
4470 mboxq->vport = vport;
4471 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4472 mp = (struct lpfc_dmabuf *) mboxq->context1;
4473 if (rc == MBX_SUCCESS) {
4474 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4475 rc = 0;
4476 }
4477
4478 /*
4479 * This memory was allocated by the lpfc_read_sparam routine. Release
4480 * it to the mbuf pool.
4481 */
4482 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4483 kfree(mp);
4484 mboxq->context1 = NULL;
4485 if (unlikely(rc)) {
4486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4487 "0382 READ_SPARAM command failed "
4488 "status %d, mbxStatus x%x\n",
4489 rc, bf_get(lpfc_mqe_status, mqe));
4490 phba->link_state = LPFC_HBA_ERROR;
4491 rc = -EIO;
4492 goto out_free_vpd;
4493 }
4494
4495 if (phba->cfg_soft_wwnn)
4496 u64_to_wwn(phba->cfg_soft_wwnn,
4497 vport->fc_sparam.nodeName.u.wwn);
4498 if (phba->cfg_soft_wwpn)
4499 u64_to_wwn(phba->cfg_soft_wwpn,
4500 vport->fc_sparam.portName.u.wwn);
4501 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4502 sizeof(struct lpfc_name));
4503 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4504 sizeof(struct lpfc_name));
4505
4506 /* Update the fc_host data structures with new wwn. */
4507 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4508 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4509
4510 /* Register SGL pool to the device using non-embedded mailbox command */
4511 rc = lpfc_sli4_post_sgl_list(phba);
4512 if (unlikely(rc)) {
4513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4514 "0582 Error %d during sgl post operation\n",
4515 rc);
da0436e9
JS
4516 rc = -ENODEV;
4517 goto out_free_vpd;
4518 }
4519
4520 /* Register SCSI SGL pool to the device */
4521 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4522 if (unlikely(rc)) {
4523 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4524 "0383 Error %d during scsi sgl post "
4525 "operation\n", rc);
da0436e9
JS
4526 /* Some Scsi buffers were moved to the abort scsi list */
4527 /* A pci function reset will repost them */
4528 rc = -ENODEV;
4529 goto out_free_vpd;
4530 }
4531
4532 /* Post the rpi header region to the device. */
4533 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4534 if (unlikely(rc)) {
4535 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4536 "0393 Error %d during rpi post operation\n",
4537 rc);
4538 rc = -ENODEV;
4539 goto out_free_vpd;
4540 }
da0436e9
JS
4541
4542 /* Set up all the queues to the device */
4543 rc = lpfc_sli4_queue_setup(phba);
4544 if (unlikely(rc)) {
4545 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4546 "0381 Error %d during queue setup.\n ", rc);
4547 goto out_stop_timers;
4548 }
4549
4550 /* Arm the CQs and then EQs on device */
4551 lpfc_sli4_arm_cqeq_intr(phba);
4552
4553 /* Indicate device interrupt mode */
4554 phba->sli4_hba.intr_enable = 1;
4555
4556 /* Allow asynchronous mailbox command to go through */
4557 spin_lock_irq(&phba->hbalock);
4558 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4559 spin_unlock_irq(&phba->hbalock);
4560
4561 /* Post receive buffers to the device */
4562 lpfc_sli4_rb_setup(phba);
4563
fc2b989b
JS
4564 /* Reset HBA FCF states after HBA reset */
4565 phba->fcf.fcf_flag = 0;
4566 phba->fcf.current_rec.flag = 0;
4567
da0436e9 4568 /* Start the ELS watchdog timer */
8fa38513
JS
4569 mod_timer(&vport->els_tmofunc,
4570 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4571
4572 /* Start heart beat timer */
4573 mod_timer(&phba->hb_tmofunc,
4574 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4575 phba->hb_outstanding = 0;
4576 phba->last_completion_time = jiffies;
4577
4578 /* Start error attention (ERATT) polling timer */
4579 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4580
75baf696
JS
4581 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4582 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4583 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4584 if (!rc) {
4585 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4586 "2829 This device supports "
4587 "Advanced Error Reporting (AER)\n");
4588 spin_lock_irq(&phba->hbalock);
4589 phba->hba_flag |= HBA_AER_ENABLED;
4590 spin_unlock_irq(&phba->hbalock);
4591 } else {
4592 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4593 "2830 This device does not support "
4594 "Advanced Error Reporting (AER)\n");
4595 phba->cfg_aer_support = 0;
4596 }
4597 }
4598
da0436e9
JS
4599 /*
4600 * The port is ready, set the host's link state to LINK_DOWN
4601 * in preparation for link interrupts.
4602 */
4603 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4604 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4605 lpfc_set_loopback_flag(phba);
4606 /* Change driver state to LPFC_LINK_DOWN right before init link */
4607 spin_lock_irq(&phba->hbalock);
4608 phba->link_state = LPFC_LINK_DOWN;
4609 spin_unlock_irq(&phba->hbalock);
4610 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4611 if (unlikely(rc != MBX_NOT_FINISHED)) {
4612 kfree(vpd);
4613 return 0;
4614 } else
4615 rc = -EIO;
4616
4617 /* Unset all the queues set up in this routine when error out */
4618 if (rc)
4619 lpfc_sli4_queue_unset(phba);
4620
4621out_stop_timers:
4622 if (rc)
4623 lpfc_stop_hba_timers(phba);
4624out_free_vpd:
4625 kfree(vpd);
4626out_free_mbox:
4627 mempool_free(mboxq, phba->mbox_mem_pool);
4628 return rc;
4629}
4630
4631/**
4632 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4633 * @ptr: context object - pointer to hba structure.
4634 *
4635 * This is the callback function for mailbox timer. The mailbox
4636 * timer is armed when a new mailbox command is issued and the timer
4637 * is deleted when the mailbox complete. The function is called by
4638 * the kernel timer code when a mailbox does not complete within
4639 * expected time. This function wakes up the worker thread to
4640 * process the mailbox timeout and returns. All the processing is
4641 * done by the worker thread function lpfc_mbox_timeout_handler.
4642 **/
4643void
4644lpfc_mbox_timeout(unsigned long ptr)
4645{
4646 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4647 unsigned long iflag;
4648 uint32_t tmo_posted;
4649
4650 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4651 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4652 if (!tmo_posted)
4653 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4654 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4655
4656 if (!tmo_posted)
4657 lpfc_worker_wake_up(phba);
4658 return;
4659}
4660
4661
4662/**
4663 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4664 * @phba: Pointer to HBA context object.
4665 *
4666 * This function is called from worker thread when a mailbox command times out.
4667 * The caller is not required to hold any locks. This function will reset the
4668 * HBA and recover all the pending commands.
4669 **/
4670void
4671lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4672{
4673 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4674 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4675 struct lpfc_sli *psli = &phba->sli;
4676 struct lpfc_sli_ring *pring;
4677
4678 /* Check the pmbox pointer first. There is a race condition
4679 * between the mbox timeout handler getting executed in the
4680 * worklist and the mailbox actually completing. When this
4681 * race condition occurs, the mbox_active will be NULL.
4682 */
4683 spin_lock_irq(&phba->hbalock);
4684 if (pmbox == NULL) {
4685 lpfc_printf_log(phba, KERN_WARNING,
4686 LOG_MBOX | LOG_SLI,
4687 "0353 Active Mailbox cleared - mailbox timeout "
4688 "exiting\n");
4689 spin_unlock_irq(&phba->hbalock);
4690 return;
4691 }
4692
4693 /* Mbox cmd <mbxCommand> timeout */
4694 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4695 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4696 mb->mbxCommand,
4697 phba->pport->port_state,
4698 phba->sli.sli_flag,
4699 phba->sli.mbox_active);
4700 spin_unlock_irq(&phba->hbalock);
4701
4702 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4703 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4704 * it to fail all oustanding SCSI IO.
4705 */
4706 spin_lock_irq(&phba->pport->work_port_lock);
4707 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4708 spin_unlock_irq(&phba->pport->work_port_lock);
4709 spin_lock_irq(&phba->hbalock);
4710 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4711 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4712 spin_unlock_irq(&phba->hbalock);
4713
4714 pring = &psli->ring[psli->fcp_ring];
4715 lpfc_sli_abort_iocb_ring(phba, pring);
4716
4717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4718 "0345 Resetting board due to mailbox timeout\n");
4719
4720 /* Reset the HBA device */
4721 lpfc_reset_hba(phba);
4722}
4723
4724/**
4725 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4726 * @phba: Pointer to HBA context object.
4727 * @pmbox: Pointer to mailbox object.
4728 * @flag: Flag indicating how the mailbox need to be processed.
4729 *
4730 * This function is called by discovery code and HBA management code
4731 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4732 * function gets the hbalock to protect the data structures.
4733 * The mailbox command can be submitted in polling mode, in which case
4734 * this function will wait in a polling loop for the completion of the
4735 * mailbox.
4736 * If the mailbox is submitted in no_wait mode (not polling) the
4737 * function will submit the command and returns immediately without waiting
4738 * for the mailbox completion. The no_wait is supported only when HBA
4739 * is in SLI2/SLI3 mode - interrupts are enabled.
4740 * The SLI interface allows only one mailbox pending at a time. If the
4741 * mailbox is issued in polling mode and there is already a mailbox
4742 * pending, then the function will return an error. If the mailbox is issued
4743 * in NO_WAIT mode and there is a mailbox pending already, the function
4744 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4745 * The sli layer owns the mailbox object until the completion of mailbox
4746 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4747 * return codes the caller owns the mailbox command after the return of
4748 * the function.
e59058c4 4749 **/
3772a991
JS
4750static int
4751lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4752 uint32_t flag)
dea3101e 4753{
dea3101e 4754 MAILBOX_t *mb;
2e0fef85 4755 struct lpfc_sli *psli = &phba->sli;
dea3101e 4756 uint32_t status, evtctr;
4757 uint32_t ha_copy;
4758 int i;
09372820 4759 unsigned long timeout;
dea3101e 4760 unsigned long drvr_flag = 0;
34b02dcd 4761 uint32_t word0, ldata;
dea3101e 4762 void __iomem *to_slim;
58da1ffb
JS
4763 int processing_queue = 0;
4764
4765 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4766 if (!pmbox) {
8568a4d2 4767 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4768 /* processing mbox queue from intr_handler */
3772a991
JS
4769 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4771 return MBX_SUCCESS;
4772 }
58da1ffb 4773 processing_queue = 1;
58da1ffb
JS
4774 pmbox = lpfc_mbox_get(phba);
4775 if (!pmbox) {
4776 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4777 return MBX_SUCCESS;
4778 }
4779 }
dea3101e 4780
ed957684 4781 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4782 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4783 if(!pmbox->vport) {
58da1ffb 4784 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4785 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4786 LOG_MBOX | LOG_VPORT,
e8b62011 4787 "1806 Mbox x%x failed. No vport\n",
3772a991 4788 pmbox->u.mb.mbxCommand);
ed957684 4789 dump_stack();
58da1ffb 4790 goto out_not_finished;
ed957684
JS
4791 }
4792 }
4793
8d63f375 4794 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4795 if (unlikely(pci_channel_offline(phba->pcidev))) {
4796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4797 goto out_not_finished;
4798 }
8d63f375 4799
a257bf90
JS
4800 /* If HBA has a deferred error attention, fail the iocb. */
4801 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4802 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4803 goto out_not_finished;
4804 }
4805
dea3101e 4806 psli = &phba->sli;
92d7f7b0 4807
3772a991 4808 mb = &pmbox->u.mb;
dea3101e 4809 status = MBX_SUCCESS;
4810
2e0fef85
JS
4811 if (phba->link_state == LPFC_HBA_ERROR) {
4812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4813
4814 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4815 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4816 "(%d):0311 Mailbox command x%x cannot "
4817 "issue Data: x%x x%x\n",
4818 pmbox->vport ? pmbox->vport->vpi : 0,
4819 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4820 goto out_not_finished;
41415862
JW
4821 }
4822
9290831f
JS
4823 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4824 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4825 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4827 "(%d):2528 Mailbox command x%x cannot "
4828 "issue Data: x%x x%x\n",
4829 pmbox->vport ? pmbox->vport->vpi : 0,
4830 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4831 goto out_not_finished;
9290831f
JS
4832 }
4833
dea3101e 4834 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4835 /* Polling for a mbox command when another one is already active
4836 * is not allowed in SLI. Also, the driver must have established
4837 * SLI2 mode to queue and process multiple mbox commands.
4838 */
4839
4840 if (flag & MBX_POLL) {
2e0fef85 4841 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4842
4843 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4844 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4845 "(%d):2529 Mailbox command x%x "
4846 "cannot issue Data: x%x x%x\n",
4847 pmbox->vport ? pmbox->vport->vpi : 0,
4848 pmbox->u.mb.mbxCommand,
4849 psli->sli_flag, flag);
58da1ffb 4850 goto out_not_finished;
dea3101e 4851 }
4852
3772a991 4853 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4854 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4855 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4856 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4857 "(%d):2530 Mailbox command x%x "
4858 "cannot issue Data: x%x x%x\n",
4859 pmbox->vport ? pmbox->vport->vpi : 0,
4860 pmbox->u.mb.mbxCommand,
4861 psli->sli_flag, flag);
58da1ffb 4862 goto out_not_finished;
dea3101e 4863 }
4864
dea3101e 4865 /* Another mailbox command is still being processed, queue this
4866 * command to be processed later.
4867 */
4868 lpfc_mbox_put(phba, pmbox);
4869
4870 /* Mbox cmd issue - BUSY */
ed957684 4871 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4872 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4873 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4874 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4875 mb->mbxCommand, phba->pport->port_state,
4876 psli->sli_flag, flag);
dea3101e 4877
4878 psli->slistat.mbox_busy++;
2e0fef85 4879 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4880
858c9f6c
JS
4881 if (pmbox->vport) {
4882 lpfc_debugfs_disc_trc(pmbox->vport,
4883 LPFC_DISC_TRC_MBOX_VPORT,
4884 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4885 (uint32_t)mb->mbxCommand,
4886 mb->un.varWords[0], mb->un.varWords[1]);
4887 }
4888 else {
4889 lpfc_debugfs_disc_trc(phba->pport,
4890 LPFC_DISC_TRC_MBOX,
4891 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4892 (uint32_t)mb->mbxCommand,
4893 mb->un.varWords[0], mb->un.varWords[1]);
4894 }
4895
2e0fef85 4896 return MBX_BUSY;
dea3101e 4897 }
4898
dea3101e 4899 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4900
4901 /* If we are not polling, we MUST be in SLI2 mode */
4902 if (flag != MBX_POLL) {
3772a991 4903 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4904 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4905 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4906 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4907 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4908 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4909 "(%d):2531 Mailbox command x%x "
4910 "cannot issue Data: x%x x%x\n",
4911 pmbox->vport ? pmbox->vport->vpi : 0,
4912 pmbox->u.mb.mbxCommand,
4913 psli->sli_flag, flag);
58da1ffb 4914 goto out_not_finished;
dea3101e 4915 }
4916 /* timeout active mbox command */
a309a6b6
JS
4917 mod_timer(&psli->mbox_tmo, (jiffies +
4918 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e 4919 }
4920
4921 /* Mailbox cmd <cmd> issue */
ed957684 4922 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4923 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4924 "x%x\n",
e8b62011 4925 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4926 mb->mbxCommand, phba->pport->port_state,
4927 psli->sli_flag, flag);
dea3101e 4928
858c9f6c
JS
4929 if (mb->mbxCommand != MBX_HEARTBEAT) {
4930 if (pmbox->vport) {
4931 lpfc_debugfs_disc_trc(pmbox->vport,
4932 LPFC_DISC_TRC_MBOX_VPORT,
4933 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4934 (uint32_t)mb->mbxCommand,
4935 mb->un.varWords[0], mb->un.varWords[1]);
4936 }
4937 else {
4938 lpfc_debugfs_disc_trc(phba->pport,
4939 LPFC_DISC_TRC_MBOX,
4940 "MBOX Send: cmd:x%x mb:x%x x%x",
4941 (uint32_t)mb->mbxCommand,
4942 mb->un.varWords[0], mb->un.varWords[1]);
4943 }
4944 }
4945
dea3101e 4946 psli->slistat.mbox_cmd++;
4947 evtctr = psli->slistat.mbox_event;
4948
4949 /* next set own bit for the adapter and copy over command word */
4950 mb->mbxOwner = OWN_CHIP;
4951
3772a991 4952 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
4953 /* Populate mbox extension offset word. */
4954 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4955 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4956 = (uint8_t *)phba->mbox_ext
4957 - (uint8_t *)phba->mbox;
4958 }
4959
4960 /* Copy the mailbox extension data */
4961 if (pmbox->in_ext_byte_len && pmbox->context2) {
4962 lpfc_sli_pcimem_bcopy(pmbox->context2,
4963 (uint8_t *)phba->mbox_ext,
4964 pmbox->in_ext_byte_len);
4965 }
4966 /* Copy command data to host SLIM area */
34b02dcd 4967 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4968 } else {
7a470277
JS
4969 /* Populate mbox extension offset word. */
4970 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4971 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4972 = MAILBOX_HBA_EXT_OFFSET;
4973
4974 /* Copy the mailbox extension data */
4975 if (pmbox->in_ext_byte_len && pmbox->context2) {
4976 lpfc_memcpy_to_slim(phba->MBslimaddr +
4977 MAILBOX_HBA_EXT_OFFSET,
4978 pmbox->context2, pmbox->in_ext_byte_len);
4979
4980 }
9290831f 4981 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4982 /* copy command data into host mbox for cmpl */
34b02dcd 4983 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4984 }
4985
4986 /* First copy mbox command data to HBA SLIM, skip past first
4987 word */
4988 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4989 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4990 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4991
4992 /* Next copy over first word, with mbxOwner set */
34b02dcd 4993 ldata = *((uint32_t *)mb);
dea3101e 4994 to_slim = phba->MBslimaddr;
4995 writel(ldata, to_slim);
4996 readl(to_slim); /* flush */
4997
4998 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4999 /* switch over to host mailbox */
3772a991 5000 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 5001 }
5002 }
5003
5004 wmb();
dea3101e 5005
5006 switch (flag) {
5007 case MBX_NOWAIT:
09372820 5008 /* Set up reference to mailbox command */
dea3101e 5009 psli->mbox_active = pmbox;
09372820
JS
5010 /* Interrupt board to do it */
5011 writel(CA_MBATT, phba->CAregaddr);
5012 readl(phba->CAregaddr); /* flush */
5013 /* Don't wait for it to finish, just return */
dea3101e 5014 break;
5015
5016 case MBX_POLL:
09372820 5017 /* Set up null reference to mailbox command */
dea3101e 5018 psli->mbox_active = NULL;
09372820
JS
5019 /* Interrupt board to do it */
5020 writel(CA_MBATT, phba->CAregaddr);
5021 readl(phba->CAregaddr); /* flush */
5022
3772a991 5023 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5024 /* First read mbox status word */
34b02dcd 5025 word0 = *((uint32_t *)phba->mbox);
dea3101e 5026 word0 = le32_to_cpu(word0);
5027 } else {
5028 /* First read mbox status word */
5029 word0 = readl(phba->MBslimaddr);
5030 }
5031
5032 /* Read the HBA Host Attention Register */
5033 ha_copy = readl(phba->HAregaddr);
09372820
JS
5034 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5035 mb->mbxCommand) *
5036 1000) + jiffies;
5037 i = 0;
dea3101e 5038 /* Wait for command to complete */
41415862
JW
5039 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5040 (!(ha_copy & HA_MBATT) &&
2e0fef85 5041 (phba->link_state > LPFC_WARM_START))) {
09372820 5042 if (time_after(jiffies, timeout)) {
dea3101e 5043 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 5044 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 5045 drvr_flag);
58da1ffb 5046 goto out_not_finished;
dea3101e 5047 }
5048
5049 /* Check if we took a mbox interrupt while we were
5050 polling */
5051 if (((word0 & OWN_CHIP) != OWN_CHIP)
5052 && (evtctr != psli->slistat.mbox_event))
5053 break;
5054
09372820
JS
5055 if (i++ > 10) {
5056 spin_unlock_irqrestore(&phba->hbalock,
5057 drvr_flag);
5058 msleep(1);
5059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5060 }
dea3101e 5061
3772a991 5062 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5063 /* First copy command data */
34b02dcd 5064 word0 = *((uint32_t *)phba->mbox);
dea3101e 5065 word0 = le32_to_cpu(word0);
5066 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5067 MAILBOX_t *slimmb;
34b02dcd 5068 uint32_t slimword0;
dea3101e 5069 /* Check real SLIM for any errors */
5070 slimword0 = readl(phba->MBslimaddr);
5071 slimmb = (MAILBOX_t *) & slimword0;
5072 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5073 && slimmb->mbxStatus) {
5074 psli->sli_flag &=
3772a991 5075 ~LPFC_SLI_ACTIVE;
dea3101e 5076 word0 = slimword0;
5077 }
5078 }
5079 } else {
5080 /* First copy command data */
5081 word0 = readl(phba->MBslimaddr);
5082 }
5083 /* Read the HBA Host Attention Register */
5084 ha_copy = readl(phba->HAregaddr);
5085 }
5086
3772a991 5087 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5088 /* copy results back to user */
34b02dcd 5089 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
7a470277
JS
5090 /* Copy the mailbox extension data */
5091 if (pmbox->out_ext_byte_len && pmbox->context2) {
5092 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5093 pmbox->context2,
5094 pmbox->out_ext_byte_len);
5095 }
dea3101e 5096 } else {
5097 /* First copy command data */
5098 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5099 MAILBOX_CMD_SIZE);
7a470277
JS
5100 /* Copy the mailbox extension data */
5101 if (pmbox->out_ext_byte_len && pmbox->context2) {
5102 lpfc_memcpy_from_slim(pmbox->context2,
5103 phba->MBslimaddr +
5104 MAILBOX_HBA_EXT_OFFSET,
5105 pmbox->out_ext_byte_len);
dea3101e 5106 }
5107 }
5108
5109 writel(HA_MBATT, phba->HAregaddr);
5110 readl(phba->HAregaddr); /* flush */
5111
5112 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5113 status = mb->mbxStatus;
5114 }
5115
2e0fef85
JS
5116 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5117 return status;
58da1ffb
JS
5118
5119out_not_finished:
5120 if (processing_queue) {
da0436e9 5121 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5122 lpfc_mbox_cmpl_put(phba, pmbox);
5123 }
5124 return MBX_NOT_FINISHED;
dea3101e 5125}
5126
f1126688
JS
5127/**
5128 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5129 * @phba: Pointer to HBA context object.
5130 *
5131 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5132 * the driver internal pending mailbox queue. It will then try to wait out the
5133 * possible outstanding mailbox command before return.
5134 *
5135 * Returns:
5136 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5137 * the outstanding mailbox command timed out.
5138 **/
5139static int
5140lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5141{
5142 struct lpfc_sli *psli = &phba->sli;
5143 uint8_t actcmd = MBX_HEARTBEAT;
5144 int rc = 0;
5145 unsigned long timeout;
5146
5147 /* Mark the asynchronous mailbox command posting as blocked */
5148 spin_lock_irq(&phba->hbalock);
5149 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5150 if (phba->sli.mbox_active)
5151 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5152 spin_unlock_irq(&phba->hbalock);
5153 /* Determine how long we might wait for the active mailbox
5154 * command to be gracefully completed by firmware.
5155 */
5156 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5157 jiffies;
5158 /* Wait for the outstnading mailbox command to complete */
5159 while (phba->sli.mbox_active) {
5160 /* Check active mailbox complete status every 2ms */
5161 msleep(2);
5162 if (time_after(jiffies, timeout)) {
5163 /* Timeout, marked the outstanding cmd not complete */
5164 rc = 1;
5165 break;
5166 }
5167 }
5168
5169 /* Can not cleanly block async mailbox command, fails it */
5170 if (rc) {
5171 spin_lock_irq(&phba->hbalock);
5172 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5173 spin_unlock_irq(&phba->hbalock);
5174 }
5175 return rc;
5176}
5177
5178/**
5179 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5180 * @phba: Pointer to HBA context object.
5181 *
5182 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5183 * commands from the driver internal pending mailbox queue. It makes sure
5184 * that there is no outstanding mailbox command before resuming posting
5185 * asynchronous mailbox commands. If, for any reason, there is outstanding
5186 * mailbox command, it will try to wait it out before resuming asynchronous
5187 * mailbox command posting.
5188 **/
5189static void
5190lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5191{
5192 struct lpfc_sli *psli = &phba->sli;
5193
5194 spin_lock_irq(&phba->hbalock);
5195 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5196 /* Asynchronous mailbox posting is not blocked, do nothing */
5197 spin_unlock_irq(&phba->hbalock);
5198 return;
5199 }
5200
5201 /* Outstanding synchronous mailbox command is guaranteed to be done,
5202 * successful or timeout, after timing-out the outstanding mailbox
5203 * command shall always be removed, so just unblock posting async
5204 * mailbox command and resume
5205 */
5206 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5207 spin_unlock_irq(&phba->hbalock);
5208
5209 /* wake up worker thread to post asynchronlous mailbox command */
5210 lpfc_worker_wake_up(phba);
5211}
5212
da0436e9
JS
5213/**
5214 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5215 * @phba: Pointer to HBA context object.
5216 * @mboxq: Pointer to mailbox object.
5217 *
5218 * The function posts a mailbox to the port. The mailbox is expected
5219 * to be comletely filled in and ready for the port to operate on it.
5220 * This routine executes a synchronous completion operation on the
5221 * mailbox by polling for its completion.
5222 *
5223 * The caller must not be holding any locks when calling this routine.
5224 *
5225 * Returns:
5226 * MBX_SUCCESS - mailbox posted successfully
5227 * Any of the MBX error values.
5228 **/
5229static int
5230lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5231{
5232 int rc = MBX_SUCCESS;
5233 unsigned long iflag;
5234 uint32_t db_ready;
5235 uint32_t mcqe_status;
5236 uint32_t mbx_cmnd;
5237 unsigned long timeout;
5238 struct lpfc_sli *psli = &phba->sli;
5239 struct lpfc_mqe *mb = &mboxq->u.mqe;
5240 struct lpfc_bmbx_create *mbox_rgn;
5241 struct dma_address *dma_address;
5242 struct lpfc_register bmbx_reg;
5243
5244 /*
5245 * Only one mailbox can be active to the bootstrap mailbox region
5246 * at a time and there is no queueing provided.
5247 */
5248 spin_lock_irqsave(&phba->hbalock, iflag);
5249 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5250 spin_unlock_irqrestore(&phba->hbalock, iflag);
5251 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5252 "(%d):2532 Mailbox command x%x (x%x) "
5253 "cannot issue Data: x%x x%x\n",
5254 mboxq->vport ? mboxq->vport->vpi : 0,
5255 mboxq->u.mb.mbxCommand,
5256 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5257 psli->sli_flag, MBX_POLL);
5258 return MBXERR_ERROR;
5259 }
5260 /* The server grabs the token and owns it until release */
5261 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5262 phba->sli.mbox_active = mboxq;
5263 spin_unlock_irqrestore(&phba->hbalock, iflag);
5264
5265 /*
5266 * Initialize the bootstrap memory region to avoid stale data areas
5267 * in the mailbox post. Then copy the caller's mailbox contents to
5268 * the bmbx mailbox region.
5269 */
5270 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5271 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5272 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5273 sizeof(struct lpfc_mqe));
5274
5275 /* Post the high mailbox dma address to the port and wait for ready. */
5276 dma_address = &phba->sli4_hba.bmbx.dma_address;
5277 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5278
5279 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5280 * 1000) + jiffies;
5281 do {
5282 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5283 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5284 if (!db_ready)
5285 msleep(2);
5286
5287 if (time_after(jiffies, timeout)) {
5288 rc = MBXERR_ERROR;
5289 goto exit;
5290 }
5291 } while (!db_ready);
5292
5293 /* Post the low mailbox dma address to the port. */
5294 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5295 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5296 * 1000) + jiffies;
5297 do {
5298 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5299 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5300 if (!db_ready)
5301 msleep(2);
5302
5303 if (time_after(jiffies, timeout)) {
5304 rc = MBXERR_ERROR;
5305 goto exit;
5306 }
5307 } while (!db_ready);
5308
5309 /*
5310 * Read the CQ to ensure the mailbox has completed.
5311 * If so, update the mailbox status so that the upper layers
5312 * can complete the request normally.
5313 */
5314 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5315 sizeof(struct lpfc_mqe));
5316 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5317 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5318 sizeof(struct lpfc_mcqe));
5319 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5320
5321 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5322 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5323 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5324 rc = MBXERR_ERROR;
d7c47992
JS
5325 } else
5326 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
5327
5328 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5329 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5330 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5331 " x%x x%x CQ: x%x x%x x%x x%x\n",
5332 mboxq->vport ? mboxq->vport->vpi : 0,
5333 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5334 bf_get(lpfc_mqe_status, mb),
5335 mb->un.mb_words[0], mb->un.mb_words[1],
5336 mb->un.mb_words[2], mb->un.mb_words[3],
5337 mb->un.mb_words[4], mb->un.mb_words[5],
5338 mb->un.mb_words[6], mb->un.mb_words[7],
5339 mb->un.mb_words[8], mb->un.mb_words[9],
5340 mb->un.mb_words[10], mb->un.mb_words[11],
5341 mb->un.mb_words[12], mboxq->mcqe.word0,
5342 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5343 mboxq->mcqe.trailer);
5344exit:
5345 /* We are holding the token, no needed for lock when release */
5346 spin_lock_irqsave(&phba->hbalock, iflag);
5347 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5348 phba->sli.mbox_active = NULL;
5349 spin_unlock_irqrestore(&phba->hbalock, iflag);
5350 return rc;
5351}
5352
5353/**
5354 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5355 * @phba: Pointer to HBA context object.
5356 * @pmbox: Pointer to mailbox object.
5357 * @flag: Flag indicating how the mailbox need to be processed.
5358 *
5359 * This function is called by discovery code and HBA management code to submit
5360 * a mailbox command to firmware with SLI-4 interface spec.
5361 *
5362 * Return codes the caller owns the mailbox command after the return of the
5363 * function.
5364 **/
5365static int
5366lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5367 uint32_t flag)
5368{
5369 struct lpfc_sli *psli = &phba->sli;
5370 unsigned long iflags;
5371 int rc;
5372
8fa38513
JS
5373 rc = lpfc_mbox_dev_check(phba);
5374 if (unlikely(rc)) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5376 "(%d):2544 Mailbox command x%x (x%x) "
5377 "cannot issue Data: x%x x%x\n",
5378 mboxq->vport ? mboxq->vport->vpi : 0,
5379 mboxq->u.mb.mbxCommand,
5380 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5381 psli->sli_flag, flag);
5382 goto out_not_finished;
5383 }
5384
da0436e9
JS
5385 /* Detect polling mode and jump to a handler */
5386 if (!phba->sli4_hba.intr_enable) {
5387 if (flag == MBX_POLL)
5388 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5389 else
5390 rc = -EIO;
5391 if (rc != MBX_SUCCESS)
5392 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5393 "(%d):2541 Mailbox command x%x "
5394 "(x%x) cannot issue Data: x%x x%x\n",
5395 mboxq->vport ? mboxq->vport->vpi : 0,
5396 mboxq->u.mb.mbxCommand,
5397 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5398 psli->sli_flag, flag);
5399 return rc;
5400 } else if (flag == MBX_POLL) {
f1126688
JS
5401 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5402 "(%d):2542 Try to issue mailbox command "
5403 "x%x (x%x) synchronously ahead of async"
5404 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5405 mboxq->vport ? mboxq->vport->vpi : 0,
5406 mboxq->u.mb.mbxCommand,
5407 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5408 psli->sli_flag, flag);
f1126688
JS
5409 /* Try to block the asynchronous mailbox posting */
5410 rc = lpfc_sli4_async_mbox_block(phba);
5411 if (!rc) {
5412 /* Successfully blocked, now issue sync mbox cmd */
5413 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5414 if (rc != MBX_SUCCESS)
5415 lpfc_printf_log(phba, KERN_ERR,
5416 LOG_MBOX | LOG_SLI,
5417 "(%d):2597 Mailbox command "
5418 "x%x (x%x) cannot issue "
5419 "Data: x%x x%x\n",
5420 mboxq->vport ?
5421 mboxq->vport->vpi : 0,
5422 mboxq->u.mb.mbxCommand,
5423 lpfc_sli4_mbox_opcode_get(phba,
5424 mboxq),
5425 psli->sli_flag, flag);
5426 /* Unblock the async mailbox posting afterward */
5427 lpfc_sli4_async_mbox_unblock(phba);
5428 }
5429 return rc;
da0436e9
JS
5430 }
5431
5432 /* Now, interrupt mode asynchrous mailbox command */
5433 rc = lpfc_mbox_cmd_check(phba, mboxq);
5434 if (rc) {
5435 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5436 "(%d):2543 Mailbox command x%x (x%x) "
5437 "cannot issue Data: x%x x%x\n",
5438 mboxq->vport ? mboxq->vport->vpi : 0,
5439 mboxq->u.mb.mbxCommand,
5440 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5441 psli->sli_flag, flag);
5442 goto out_not_finished;
5443 }
da0436e9
JS
5444
5445 /* Put the mailbox command to the driver internal FIFO */
5446 psli->slistat.mbox_busy++;
5447 spin_lock_irqsave(&phba->hbalock, iflags);
5448 lpfc_mbox_put(phba, mboxq);
5449 spin_unlock_irqrestore(&phba->hbalock, iflags);
5450 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5451 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5452 "x%x (x%x) x%x x%x x%x\n",
5453 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5454 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5455 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5456 phba->pport->port_state,
5457 psli->sli_flag, MBX_NOWAIT);
5458 /* Wake up worker thread to transport mailbox command from head */
5459 lpfc_worker_wake_up(phba);
5460
5461 return MBX_BUSY;
5462
5463out_not_finished:
5464 return MBX_NOT_FINISHED;
5465}
5466
5467/**
5468 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5469 * @phba: Pointer to HBA context object.
5470 *
5471 * This function is called by worker thread to send a mailbox command to
5472 * SLI4 HBA firmware.
5473 *
5474 **/
5475int
5476lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5477{
5478 struct lpfc_sli *psli = &phba->sli;
5479 LPFC_MBOXQ_t *mboxq;
5480 int rc = MBX_SUCCESS;
5481 unsigned long iflags;
5482 struct lpfc_mqe *mqe;
5483 uint32_t mbx_cmnd;
5484
5485 /* Check interrupt mode before post async mailbox command */
5486 if (unlikely(!phba->sli4_hba.intr_enable))
5487 return MBX_NOT_FINISHED;
5488
5489 /* Check for mailbox command service token */
5490 spin_lock_irqsave(&phba->hbalock, iflags);
5491 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5492 spin_unlock_irqrestore(&phba->hbalock, iflags);
5493 return MBX_NOT_FINISHED;
5494 }
5495 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5496 spin_unlock_irqrestore(&phba->hbalock, iflags);
5497 return MBX_NOT_FINISHED;
5498 }
5499 if (unlikely(phba->sli.mbox_active)) {
5500 spin_unlock_irqrestore(&phba->hbalock, iflags);
5501 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5502 "0384 There is pending active mailbox cmd\n");
5503 return MBX_NOT_FINISHED;
5504 }
5505 /* Take the mailbox command service token */
5506 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5507
5508 /* Get the next mailbox command from head of queue */
5509 mboxq = lpfc_mbox_get(phba);
5510
5511 /* If no more mailbox command waiting for post, we're done */
5512 if (!mboxq) {
5513 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5514 spin_unlock_irqrestore(&phba->hbalock, iflags);
5515 return MBX_SUCCESS;
5516 }
5517 phba->sli.mbox_active = mboxq;
5518 spin_unlock_irqrestore(&phba->hbalock, iflags);
5519
5520 /* Check device readiness for posting mailbox command */
5521 rc = lpfc_mbox_dev_check(phba);
5522 if (unlikely(rc))
5523 /* Driver clean routine will clean up pending mailbox */
5524 goto out_not_finished;
5525
5526 /* Prepare the mbox command to be posted */
5527 mqe = &mboxq->u.mqe;
5528 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5529
5530 /* Start timer for the mbox_tmo and log some mailbox post messages */
5531 mod_timer(&psli->mbox_tmo, (jiffies +
5532 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5533
5534 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5535 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5536 "x%x x%x\n",
5537 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5538 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5539 phba->pport->port_state, psli->sli_flag);
5540
5541 if (mbx_cmnd != MBX_HEARTBEAT) {
5542 if (mboxq->vport) {
5543 lpfc_debugfs_disc_trc(mboxq->vport,
5544 LPFC_DISC_TRC_MBOX_VPORT,
5545 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5546 mbx_cmnd, mqe->un.mb_words[0],
5547 mqe->un.mb_words[1]);
5548 } else {
5549 lpfc_debugfs_disc_trc(phba->pport,
5550 LPFC_DISC_TRC_MBOX,
5551 "MBOX Send: cmd:x%x mb:x%x x%x",
5552 mbx_cmnd, mqe->un.mb_words[0],
5553 mqe->un.mb_words[1]);
5554 }
5555 }
5556 psli->slistat.mbox_cmd++;
5557
5558 /* Post the mailbox command to the port */
5559 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5560 if (rc != MBX_SUCCESS) {
5561 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5562 "(%d):2533 Mailbox command x%x (x%x) "
5563 "cannot issue Data: x%x x%x\n",
5564 mboxq->vport ? mboxq->vport->vpi : 0,
5565 mboxq->u.mb.mbxCommand,
5566 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5567 psli->sli_flag, MBX_NOWAIT);
5568 goto out_not_finished;
5569 }
5570
5571 return rc;
5572
5573out_not_finished:
5574 spin_lock_irqsave(&phba->hbalock, iflags);
5575 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5576 __lpfc_mbox_cmpl_put(phba, mboxq);
5577 /* Release the token */
5578 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5579 phba->sli.mbox_active = NULL;
5580 spin_unlock_irqrestore(&phba->hbalock, iflags);
5581
5582 return MBX_NOT_FINISHED;
5583}
5584
5585/**
5586 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5587 * @phba: Pointer to HBA context object.
5588 * @pmbox: Pointer to mailbox object.
5589 * @flag: Flag indicating how the mailbox need to be processed.
5590 *
5591 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5592 * the API jump table function pointer from the lpfc_hba struct.
5593 *
5594 * Return codes the caller owns the mailbox command after the return of the
5595 * function.
5596 **/
5597int
5598lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5599{
5600 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5601}
5602
5603/**
5604 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5605 * @phba: The hba struct for which this call is being executed.
5606 * @dev_grp: The HBA PCI-Device group number.
5607 *
5608 * This routine sets up the mbox interface API function jump table in @phba
5609 * struct.
5610 * Returns: 0 - success, -ENODEV - failure.
5611 **/
5612int
5613lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5614{
5615
5616 switch (dev_grp) {
5617 case LPFC_PCI_DEV_LP:
5618 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5619 phba->lpfc_sli_handle_slow_ring_event =
5620 lpfc_sli_handle_slow_ring_event_s3;
5621 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5622 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5623 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5624 break;
5625 case LPFC_PCI_DEV_OC:
5626 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5627 phba->lpfc_sli_handle_slow_ring_event =
5628 lpfc_sli_handle_slow_ring_event_s4;
5629 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5630 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5631 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5632 break;
5633 default:
5634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5635 "1420 Invalid HBA PCI-device group: 0x%x\n",
5636 dev_grp);
5637 return -ENODEV;
5638 break;
5639 }
5640 return 0;
5641}
5642
e59058c4 5643/**
3621a710 5644 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5645 * @phba: Pointer to HBA context object.
5646 * @pring: Pointer to driver SLI ring object.
5647 * @piocb: Pointer to address of newly added command iocb.
5648 *
5649 * This function is called with hbalock held to add a command
5650 * iocb to the txq when SLI layer cannot submit the command iocb
5651 * to the ring.
5652 **/
2a9bf3d0 5653void
92d7f7b0 5654__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5655 struct lpfc_iocbq *piocb)
dea3101e 5656{
5657 /* Insert the caller's iocb in the txq tail for later processing. */
5658 list_add_tail(&piocb->list, &pring->txq);
5659 pring->txq_cnt++;
dea3101e 5660}
5661
e59058c4 5662/**
3621a710 5663 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5664 * @phba: Pointer to HBA context object.
5665 * @pring: Pointer to driver SLI ring object.
5666 * @piocb: Pointer to address of newly added command iocb.
5667 *
5668 * This function is called with hbalock held before a new
5669 * iocb is submitted to the firmware. This function checks
5670 * txq to flush the iocbs in txq to Firmware before
5671 * submitting new iocbs to the Firmware.
5672 * If there are iocbs in the txq which need to be submitted
5673 * to firmware, lpfc_sli_next_iocb returns the first element
5674 * of the txq after dequeuing it from txq.
5675 * If there is no iocb in the txq then the function will return
5676 * *piocb and *piocb is set to NULL. Caller needs to check
5677 * *piocb to find if there are more commands in the txq.
5678 **/
dea3101e 5679static struct lpfc_iocbq *
5680lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5681 struct lpfc_iocbq **piocb)
dea3101e 5682{
5683 struct lpfc_iocbq * nextiocb;
5684
5685 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5686 if (!nextiocb) {
5687 nextiocb = *piocb;
5688 *piocb = NULL;
5689 }
5690
5691 return nextiocb;
5692}
5693
e59058c4 5694/**
3772a991 5695 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5696 * @phba: Pointer to HBA context object.
3772a991 5697 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5698 * @piocb: Pointer to command iocb.
5699 * @flag: Flag indicating if this command can be put into txq.
5700 *
3772a991
JS
5701 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5702 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5703 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5704 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5705 * this function allows only iocbs for posting buffers. This function finds
5706 * next available slot in the command ring and posts the command to the
5707 * available slot and writes the port attention register to request HBA start
5708 * processing new iocb. If there is no slot available in the ring and
5709 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5710 * the function returns IOCB_BUSY.
e59058c4 5711 *
3772a991
JS
5712 * This function is called with hbalock held. The function will return success
5713 * after it successfully submit the iocb to firmware or after adding to the
5714 * txq.
e59058c4 5715 **/
98c9ea5c 5716static int
3772a991 5717__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 5718 struct lpfc_iocbq *piocb, uint32_t flag)
5719{
5720 struct lpfc_iocbq *nextiocb;
5721 IOCB_t *iocb;
3772a991 5722 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5723
92d7f7b0
JS
5724 if (piocb->iocb_cmpl && (!piocb->vport) &&
5725 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5726 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5727 lpfc_printf_log(phba, KERN_ERR,
5728 LOG_SLI | LOG_VPORT,
e8b62011 5729 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5730 piocb->iocb.ulpCommand);
5731 dump_stack();
5732 return IOCB_ERROR;
5733 }
5734
5735
8d63f375
LV
5736 /* If the PCI channel is in offline state, do not post iocbs. */
5737 if (unlikely(pci_channel_offline(phba->pcidev)))
5738 return IOCB_ERROR;
5739
a257bf90
JS
5740 /* If HBA has a deferred error attention, fail the iocb. */
5741 if (unlikely(phba->hba_flag & DEFER_ERATT))
5742 return IOCB_ERROR;
5743
dea3101e 5744 /*
5745 * We should never get an IOCB if we are in a < LINK_DOWN state
5746 */
2e0fef85 5747 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 5748 return IOCB_ERROR;
5749
5750 /*
5751 * Check to see if we are blocking IOCB processing because of a
0b727fea 5752 * outstanding event.
dea3101e 5753 */
0b727fea 5754 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 5755 goto iocb_busy;
5756
2e0fef85 5757 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5758 /*
2680eeaa 5759 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 5760 * can be issued if the link is not up.
5761 */
5762 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5763 case CMD_GEN_REQUEST64_CR:
5764 case CMD_GEN_REQUEST64_CX:
5765 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5766 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5767 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5768 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5769 MENLO_TRANSPORT_TYPE))
5770
5771 goto iocb_busy;
5772 break;
dea3101e 5773 case CMD_QUE_RING_BUF_CN:
5774 case CMD_QUE_RING_BUF64_CN:
dea3101e 5775 /*
5776 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5777 * completion, iocb_cmpl MUST be 0.
5778 */
5779 if (piocb->iocb_cmpl)
5780 piocb->iocb_cmpl = NULL;
5781 /*FALLTHROUGH*/
5782 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5783 case CMD_CLOSE_XRI_CN:
5784 case CMD_CLOSE_XRI_CX:
dea3101e 5785 break;
5786 default:
5787 goto iocb_busy;
5788 }
5789
5790 /*
5791 * For FCP commands, we must be in a state where we can process link
5792 * attention events.
5793 */
5794 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5795 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5796 goto iocb_busy;
92d7f7b0 5797 }
dea3101e 5798
dea3101e 5799 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5800 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5801 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5802
5803 if (iocb)
5804 lpfc_sli_update_ring(phba, pring);
5805 else
5806 lpfc_sli_update_full_ring(phba, pring);
5807
5808 if (!piocb)
5809 return IOCB_SUCCESS;
5810
5811 goto out_busy;
5812
5813 iocb_busy:
5814 pring->stats.iocb_cmd_delay++;
5815
5816 out_busy:
5817
5818 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5819 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 5820 return IOCB_SUCCESS;
5821 }
5822
5823 return IOCB_BUSY;
5824}
5825
3772a991 5826/**
4f774513
JS
5827 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5828 * @phba: Pointer to HBA context object.
5829 * @piocb: Pointer to command iocb.
5830 * @sglq: Pointer to the scatter gather queue object.
5831 *
5832 * This routine converts the bpl or bde that is in the IOCB
5833 * to a sgl list for the sli4 hardware. The physical address
5834 * of the bpl/bde is converted back to a virtual address.
5835 * If the IOCB contains a BPL then the list of BDE's is
5836 * converted to sli4_sge's. If the IOCB contains a single
5837 * BDE then it is converted to a single sli_sge.
5838 * The IOCB is still in cpu endianess so the contents of
5839 * the bpl can be used without byte swapping.
5840 *
5841 * Returns valid XRI = Success, NO_XRI = Failure.
5842**/
5843static uint16_t
5844lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5845 struct lpfc_sglq *sglq)
3772a991 5846{
4f774513
JS
5847 uint16_t xritag = NO_XRI;
5848 struct ulp_bde64 *bpl = NULL;
5849 struct ulp_bde64 bde;
5850 struct sli4_sge *sgl = NULL;
5851 IOCB_t *icmd;
5852 int numBdes = 0;
5853 int i = 0;
3772a991 5854
4f774513
JS
5855 if (!piocbq || !sglq)
5856 return xritag;
5857
5858 sgl = (struct sli4_sge *)sglq->sgl;
5859 icmd = &piocbq->iocb;
5860 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5861 numBdes = icmd->un.genreq64.bdl.bdeSize /
5862 sizeof(struct ulp_bde64);
5863 /* The addrHigh and addrLow fields within the IOCB
5864 * have not been byteswapped yet so there is no
5865 * need to swap them back.
5866 */
5867 bpl = (struct ulp_bde64 *)
5868 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5869
5870 if (!bpl)
5871 return xritag;
5872
5873 for (i = 0; i < numBdes; i++) {
5874 /* Should already be byte swapped. */
28baac74
JS
5875 sgl->addr_hi = bpl->addrHigh;
5876 sgl->addr_lo = bpl->addrLow;
5877
4f774513
JS
5878 if ((i+1) == numBdes)
5879 bf_set(lpfc_sli4_sge_last, sgl, 1);
5880 else
5881 bf_set(lpfc_sli4_sge_last, sgl, 0);
5882 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5883 /* swap the size field back to the cpu so we
5884 * can assign it to the sgl.
5885 */
5886 bde.tus.w = le32_to_cpu(bpl->tus.w);
5887 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
4f774513
JS
5888 bpl++;
5889 sgl++;
5890 }
5891 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5892 /* The addrHigh and addrLow fields of the BDE have not
5893 * been byteswapped yet so they need to be swapped
5894 * before putting them in the sgl.
5895 */
5896 sgl->addr_hi =
5897 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5898 sgl->addr_lo =
5899 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
4f774513
JS
5900 bf_set(lpfc_sli4_sge_last, sgl, 1);
5901 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5902 sgl->sge_len =
5903 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
5904 }
5905 return sglq->sli4_xritag;
3772a991 5906}
92d7f7b0 5907
e59058c4 5908/**
4f774513 5909 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5910 * @phba: Pointer to HBA context object.
e59058c4 5911 *
4f774513 5912 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5913 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5914 * held.
4f774513
JS
5915 *
5916 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5917 **/
4f774513 5918static uint32_t
8fa38513 5919lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5920{
8fa38513
JS
5921 ++phba->fcp_qidx;
5922 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5923 phba->fcp_qidx = 0;
92d7f7b0 5924
8fa38513 5925 return phba->fcp_qidx;
92d7f7b0
JS
5926}
5927
e59058c4 5928/**
4f774513 5929 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5930 * @phba: Pointer to HBA context object.
4f774513
JS
5931 * @piocb: Pointer to command iocb.
5932 * @wqe: Pointer to the work queue entry.
e59058c4 5933 *
4f774513
JS
5934 * This routine converts the iocb command to its Work Queue Entry
5935 * equivalent. The wqe pointer should not have any fields set when
5936 * this routine is called because it will memcpy over them.
5937 * This routine does not set the CQ_ID or the WQEC bits in the
5938 * wqe.
e59058c4 5939 *
4f774513 5940 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5941 **/
cf5bf97e 5942static int
4f774513
JS
5943lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5944 union lpfc_wqe *wqe)
cf5bf97e 5945{
5ffc266e 5946 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
5947 uint8_t ct = 0;
5948 uint32_t fip;
5949 uint32_t abort_tag;
5950 uint8_t command_type = ELS_COMMAND_NON_FIP;
5951 uint8_t cmnd;
5952 uint16_t xritag;
5953 struct ulp_bde64 *bpl = NULL;
c868595d 5954 uint32_t els_id = ELS_ID_DEFAULT;
5ffc266e
JS
5955 int numBdes, i;
5956 struct ulp_bde64 bde;
4f774513 5957
45ed1190 5958 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 5959 /* The fcp commands will set command type */
0c287589 5960 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5961 command_type = FCP_COMMAND;
c868595d 5962 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
5963 command_type = ELS_COMMAND_FIP;
5964 else
5965 command_type = ELS_COMMAND_NON_FIP;
5966
4f774513
JS
5967 /* Some of the fields are in the right position already */
5968 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5969 abort_tag = (uint32_t) iocbq->iotag;
5970 xritag = iocbq->sli4_xritag;
5971 wqe->words[7] = 0; /* The ct field has moved so reset */
5972 /* words0-2 bpl convert bde */
5973 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
5974 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5975 sizeof(struct ulp_bde64);
4f774513
JS
5976 bpl = (struct ulp_bde64 *)
5977 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5978 if (!bpl)
5979 return IOCB_ERROR;
cf5bf97e 5980
4f774513
JS
5981 /* Should already be byte swapped. */
5982 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5983 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5984 /* swap the size field back to the cpu so we
5985 * can assign it to the sgl.
5986 */
5987 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
5988 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5989 total_len = 0;
5990 for (i = 0; i < numBdes; i++) {
5991 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5992 total_len += bde.tus.f.bdeSize;
5993 }
4f774513 5994 } else
5ffc266e 5995 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 5996
4f774513
JS
5997 iocbq->iocb.ulpIoTag = iocbq->iotag;
5998 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 5999
4f774513
JS
6000 switch (iocbq->iocb.ulpCommand) {
6001 case CMD_ELS_REQUEST64_CR:
6002 if (!iocbq->iocb.ulpLe) {
6003 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6004 "2007 Only Limited Edition cmd Format"
6005 " supported 0x%x\n",
6006 iocbq->iocb.ulpCommand);
6007 return IOCB_ERROR;
6008 }
5ffc266e 6009 wqe->els_req.payload_len = xmit_len;
4f774513
JS
6010 /* Els_reguest64 has a TMO */
6011 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6012 iocbq->iocb.ulpTimeout);
6013 /* Need a VF for word 4 set the vf bit*/
6014 bf_set(els_req64_vf, &wqe->els_req, 0);
6015 /* And a VFID for word 12 */
6016 bf_set(els_req64_vfid, &wqe->els_req, 0);
6017 /*
6018 * Set ct field to 3, indicates that the context_tag field
6019 * contains the FCFI and remote N_Port_ID is
6020 * in word 5.
6021 */
6022
6023 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6024 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6025 iocbq->iocb.ulpContext);
6026
4f774513
JS
6027 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
6028 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
6029 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
6030
6031 if (command_type == ELS_COMMAND_FIP) {
6032 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6033 >> LPFC_FIP_ELS_ID_SHIFT);
6034 }
6035 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
6036
4f774513 6037 break;
5ffc266e
JS
6038 case CMD_XMIT_SEQUENCE64_CX:
6039 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6040 iocbq->iocb.un.ulpWord[3]);
6041 wqe->generic.word3 = 0;
6042 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5ffc266e
JS
6043 /* The entire sequence is transmitted for this IOCB */
6044 xmit_len = total_len;
6045 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513
JS
6046 case CMD_XMIT_SEQUENCE64_CR:
6047 /* word3 iocb=io_tag32 wqe=payload_offset */
6048 /* payload offset used for multilpe outstanding
6049 * sequences on the same exchange
6050 */
6051 wqe->words[3] = 0;
6052 /* word4 relative_offset memcpy */
6053 /* word5 r_ctl/df_ctl memcpy */
6054 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5ffc266e
JS
6055 wqe->xmit_sequence.xmit_len = xmit_len;
6056 command_type = OTHER_COMMAND;
4f774513
JS
6057 break;
6058 case CMD_XMIT_BCAST64_CN:
6059 /* word3 iocb=iotag32 wqe=payload_len */
6060 wqe->words[3] = 0; /* no definition for this in wqe */
6061 /* word4 iocb=rsvd wqe=rsvd */
6062 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6063 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6064 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6065 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6066 break;
6067 case CMD_FCP_IWRITE64_CR:
6068 command_type = FCP_COMMAND_DATA_OUT;
6069 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6070 * confusing.
6071 * word3 is payload_len: byte offset to the sgl entry for the
6072 * fcp_command.
6073 * word4 is total xfer len, same as the IOCB->ulpParameter.
6074 * word5 is initial xfer len 0 = wait for xfer-ready
6075 */
6076
6077 /* Always wait for xfer-ready before sending data */
6078 wqe->fcp_iwrite.initial_xfer_len = 0;
6079 /* word 4 (xfer length) should have been set on the memcpy */
6080
6081 /* allow write to fall through to read */
6082 case CMD_FCP_IREAD64_CR:
6083 /* FCP_CMD is always the 1st sgl entry */
6084 wqe->fcp_iread.payload_len =
5ffc266e 6085 xmit_len + sizeof(struct fcp_rsp);
4f774513
JS
6086
6087 /* word 4 (xfer length) should have been set on the memcpy */
6088
6089 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6090 iocbq->iocb.ulpFCP2Rcvy);
6091 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6092 /* The XC bit and the XS bit are similar. The driver never
6093 * tracked whether or not the exchange was previouslly open.
6094 * XC = Exchange create, 0 is create. 1 is already open.
6095 * XS = link cmd: 1 do not close the exchange after command.
6096 * XS = 0 close exchange when command completes.
6097 * The only time we would not set the XC bit is when the XS bit
6098 * is set and we are sending our 2nd or greater command on
6099 * this exchange.
6100 */
f1126688
JS
6101 /* Always open the exchange */
6102 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
4f774513 6103
f1126688
JS
6104 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6105 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6106 break;
4f774513
JS
6107 case CMD_FCP_ICMND64_CR:
6108 /* Always open the exchange */
6109 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6110
f1126688 6111 wqe->words[4] = 0;
4f774513 6112 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
f1126688 6113 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
4f774513
JS
6114 break;
6115 case CMD_GEN_REQUEST64_CR:
6116 /* word3 command length is described as byte offset to the
6117 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6118 * sgl[0] = cmnd
6119 * sgl[1] = rsp.
6120 *
6121 */
5ffc266e 6122 wqe->gen_req.command_len = xmit_len;
4f774513
JS
6123 /* Word4 parameter copied in the memcpy */
6124 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6125 /* word6 context tag copied in memcpy */
6126 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6127 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6128 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6129 "2015 Invalid CT %x command 0x%x\n",
6130 ct, iocbq->iocb.ulpCommand);
6131 return IOCB_ERROR;
6132 }
6133 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6134 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6135 iocbq->iocb.ulpTimeout);
6136
6137 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6138 command_type = OTHER_COMMAND;
6139 break;
6140 case CMD_XMIT_ELS_RSP64_CX:
6141 /* words0-2 BDE memcpy */
6142 /* word3 iocb=iotag32 wqe=rsvd */
6143 wqe->words[3] = 0;
6144 /* word4 iocb=did wge=rsvd. */
6145 wqe->words[4] = 0;
6146 /* word5 iocb=rsvd wge=did */
6147 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6148 iocbq->iocb.un.elsreq64.remoteID);
6149
6150 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6151 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6152
6153 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6154 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6155 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6156 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6157 iocbq->vport->vpi + phba->vpi_base);
6158 command_type = OTHER_COMMAND;
6159 break;
6160 case CMD_CLOSE_XRI_CN:
6161 case CMD_ABORT_XRI_CN:
6162 case CMD_ABORT_XRI_CX:
6163 /* words 0-2 memcpy should be 0 rserved */
6164 /* port will send abts */
6165 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6166 /*
6167 * The link is down so the fw does not need to send abts
6168 * on the wire.
6169 */
6170 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6171 else
6172 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6173 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
4f774513
JS
6174 wqe->words[5] = 0;
6175 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6176 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6177 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
6178 /*
6179 * The abort handler will send us CMD_ABORT_XRI_CN or
6180 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6181 */
6182 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6183 cmnd = CMD_ABORT_XRI_CX;
6184 command_type = OTHER_COMMAND;
6185 xritag = 0;
6186 break;
6669f9bb
JS
6187 case CMD_XMIT_BLS_RSP64_CX:
6188 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6189 * we re-construct this WQE here based on information in
6190 * iocbq from scratch.
6191 */
6192 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 6193 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 6194 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
5ffc266e
JS
6195 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6196 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6197 LPFC_ABTS_UNSOL_INT) {
6198 /* ABTS sent by initiator to CT exchange, the
6199 * RX_ID field will be filled with the newly
6200 * allocated responder XRI.
6201 */
6202 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6203 iocbq->sli4_xritag);
6204 } else {
6205 /* ABTS sent by responder to CT exchange, the
6206 * RX_ID field will be filled with the responder
6207 * RX_ID from ABTS.
6208 */
6209 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6210 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6211 }
6669f9bb
JS
6212 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6213 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6214 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6215 iocbq->iocb.ulpContext);
6216 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6217 command_type = OTHER_COMMAND;
6218 break;
4f774513
JS
6219 case CMD_XRI_ABORTED_CX:
6220 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6221 /* words0-2 are all 0's no bde */
6222 /* word3 and word4 are rsvrd */
6223 wqe->words[3] = 0;
6224 wqe->words[4] = 0;
6225 /* word5 iocb=rsvd wge=did */
6226 /* There is no remote port id in the IOCB? */
6227 /* Let this fall through and fail */
6228 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6229 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6230 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6231 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6232 default:
6233 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6234 "2014 Invalid command 0x%x\n",
6235 iocbq->iocb.ulpCommand);
6236 return IOCB_ERROR;
6237 break;
6238
6239 }
6240 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6241 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6242 wqe->generic.abort_tag = abort_tag;
6243 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6244 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6245 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6246 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6247
6248 return 0;
6249}
6250
6251/**
6252 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6253 * @phba: Pointer to HBA context object.
6254 * @ring_number: SLI ring number to issue iocb on.
6255 * @piocb: Pointer to command iocb.
6256 * @flag: Flag indicating if this command can be put into txq.
6257 *
6258 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6259 * an iocb command to an HBA with SLI-4 interface spec.
6260 *
6261 * This function is called with hbalock held. The function will return success
6262 * after it successfully submit the iocb to firmware or after adding to the
6263 * txq.
6264 **/
6265static int
6266__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6267 struct lpfc_iocbq *piocb, uint32_t flag)
6268{
6269 struct lpfc_sglq *sglq;
4f774513
JS
6270 union lpfc_wqe wqe;
6271 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
6272
6273 if (piocb->sli4_xritag == NO_XRI) {
6274 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6275 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6276 sglq = NULL;
6277 else {
2a9bf3d0
JS
6278 if (pring->txq_cnt) {
6279 if (!(flag & SLI_IOCB_RET_IOCB)) {
6280 __lpfc_sli_ringtx_put(phba,
6281 pring, piocb);
6282 return IOCB_SUCCESS;
6283 } else {
6284 return IOCB_BUSY;
6285 }
6286 } else {
4f774513 6287 sglq = __lpfc_sli_get_sglq(phba);
2a9bf3d0
JS
6288 if (!sglq) {
6289 if (!(flag & SLI_IOCB_RET_IOCB)) {
6290 __lpfc_sli_ringtx_put(phba,
6291 pring,
6292 piocb);
6293 return IOCB_SUCCESS;
6294 } else
6295 return IOCB_BUSY;
6296 }
6297 }
4f774513
JS
6298 }
6299 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6300 sglq = NULL; /* These IO's already have an XRI and
6301 * a mapped sgl.
6302 */
6303 } else {
6304 /* This is a continuation of a commandi,(CX) so this
6305 * sglq is on the active list
6306 */
6307 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6308 if (!sglq)
6309 return IOCB_ERROR;
6310 }
6311
6312 if (sglq) {
2a9bf3d0
JS
6313 piocb->sli4_xritag = sglq->sli4_xritag;
6314
6315 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
6316 return IOCB_ERROR;
6317 }
6318
6319 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6320 return IOCB_ERROR;
6321
341af102
JS
6322 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6323 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
6324 /*
6325 * For FCP command IOCB, get a new WQ index to distribute
6326 * WQE across the WQsr. On the other hand, for abort IOCB,
6327 * it carries the same WQ index to the original command
6328 * IOCB.
6329 */
341af102 6330 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
6331 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6332 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6333 &wqe))
4f774513
JS
6334 return IOCB_ERROR;
6335 } else {
6336 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6337 return IOCB_ERROR;
6338 }
6339 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6340
6341 return 0;
6342}
6343
6344/**
6345 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6346 *
6347 * This routine wraps the actual lockless version for issusing IOCB function
6348 * pointer from the lpfc_hba struct.
6349 *
6350 * Return codes:
6351 * IOCB_ERROR - Error
6352 * IOCB_SUCCESS - Success
6353 * IOCB_BUSY - Busy
6354 **/
2a9bf3d0 6355int
4f774513
JS
6356__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6357 struct lpfc_iocbq *piocb, uint32_t flag)
6358{
6359 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6360}
6361
6362/**
6363 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6364 * @phba: The hba struct for which this call is being executed.
6365 * @dev_grp: The HBA PCI-Device group number.
6366 *
6367 * This routine sets up the SLI interface API function jump table in @phba
6368 * struct.
6369 * Returns: 0 - success, -ENODEV - failure.
6370 **/
6371int
6372lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6373{
6374
6375 switch (dev_grp) {
6376 case LPFC_PCI_DEV_LP:
6377 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6378 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6379 break;
6380 case LPFC_PCI_DEV_OC:
6381 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6382 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6383 break;
6384 default:
6385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6386 "1419 Invalid HBA PCI-device group: 0x%x\n",
6387 dev_grp);
6388 return -ENODEV;
6389 break;
6390 }
6391 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6392 return 0;
6393}
6394
6395/**
6396 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6397 * @phba: Pointer to HBA context object.
6398 * @pring: Pointer to driver SLI ring object.
6399 * @piocb: Pointer to command iocb.
6400 * @flag: Flag indicating if this command can be put into txq.
6401 *
6402 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6403 * function. This function gets the hbalock and calls
6404 * __lpfc_sli_issue_iocb function and will return the error returned
6405 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6406 * functions which do not hold hbalock.
6407 **/
6408int
6409lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6410 struct lpfc_iocbq *piocb, uint32_t flag)
6411{
6412 unsigned long iflags;
6413 int rc;
6414
6415 spin_lock_irqsave(&phba->hbalock, iflags);
6416 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6417 spin_unlock_irqrestore(&phba->hbalock, iflags);
6418
6419 return rc;
6420}
6421
6422/**
6423 * lpfc_extra_ring_setup - Extra ring setup function
6424 * @phba: Pointer to HBA context object.
6425 *
6426 * This function is called while driver attaches with the
6427 * HBA to setup the extra ring. The extra ring is used
6428 * only when driver needs to support target mode functionality
6429 * or IP over FC functionalities.
6430 *
6431 * This function is called with no lock held.
6432 **/
6433static int
6434lpfc_extra_ring_setup( struct lpfc_hba *phba)
6435{
6436 struct lpfc_sli *psli;
6437 struct lpfc_sli_ring *pring;
6438
6439 psli = &phba->sli;
6440
6441 /* Adjust cmd/rsp ring iocb entries more evenly */
6442
6443 /* Take some away from the FCP ring */
6444 pring = &psli->ring[psli->fcp_ring];
6445 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6446 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6447 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6448 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6449
a4bc3379
JS
6450 /* and give them to the extra ring */
6451 pring = &psli->ring[psli->extra_ring];
6452
cf5bf97e
JW
6453 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6454 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6455 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6456 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6457
6458 /* Setup default profile for this ring */
6459 pring->iotag_max = 4096;
6460 pring->num_mask = 1;
6461 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6462 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6463 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6464 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6465 return 0;
6466}
6467
e59058c4 6468/**
3621a710 6469 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6470 * @phba: Pointer to HBA context object.
6471 * @pring: Pointer to driver SLI ring object.
6472 * @iocbq: Pointer to iocb object.
6473 *
6474 * This function is called by the slow ring event handler
6475 * function when there is an ASYNC event iocb in the ring.
6476 * This function is called with no lock held.
6477 * Currently this function handles only temperature related
6478 * ASYNC events. The function decodes the temperature sensor
6479 * event message and posts events for the management applications.
6480 **/
98c9ea5c 6481static void
57127f15
JS
6482lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6483 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6484{
6485 IOCB_t *icmd;
6486 uint16_t evt_code;
6487 uint16_t temp;
6488 struct temp_event temp_event_data;
6489 struct Scsi_Host *shost;
a257bf90 6490 uint32_t *iocb_w;
57127f15
JS
6491
6492 icmd = &iocbq->iocb;
6493 evt_code = icmd->un.asyncstat.evt_code;
6494 temp = icmd->ulpContext;
6495
6496 if ((evt_code != ASYNC_TEMP_WARN) &&
6497 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6498 iocb_w = (uint32_t *) icmd;
57127f15
JS
6499 lpfc_printf_log(phba,
6500 KERN_ERR,
6501 LOG_SLI,
76bb24ef 6502 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6503 " evt_code 0x%x\n"
a257bf90
JS
6504 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6505 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6506 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6507 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6508 pring->ringno,
a257bf90
JS
6509 icmd->un.asyncstat.evt_code,
6510 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6511 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6512 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6513 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6514
57127f15
JS
6515 return;
6516 }
6517 temp_event_data.data = (uint32_t)temp;
6518 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6519 if (evt_code == ASYNC_TEMP_WARN) {
6520 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6521 lpfc_printf_log(phba,
09372820 6522 KERN_ERR,
57127f15 6523 LOG_TEMP,
76bb24ef 6524 "0347 Adapter is very hot, please take "
57127f15
JS
6525 "corrective action. temperature : %d Celsius\n",
6526 temp);
6527 }
6528 if (evt_code == ASYNC_TEMP_SAFE) {
6529 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6530 lpfc_printf_log(phba,
09372820 6531 KERN_ERR,
57127f15
JS
6532 LOG_TEMP,
6533 "0340 Adapter temperature is OK now. "
6534 "temperature : %d Celsius\n",
6535 temp);
6536 }
6537
6538 /* Send temperature change event to applications */
6539 shost = lpfc_shost_from_vport(phba->pport);
6540 fc_host_post_vendor_event(shost, fc_get_event_number(),
6541 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6542 LPFC_NL_VENDOR_ID);
57127f15
JS
6543
6544}
6545
6546
e59058c4 6547/**
3621a710 6548 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6549 * @phba: Pointer to HBA context object.
6550 *
6551 * lpfc_sli_setup sets up rings of the SLI interface with
6552 * number of iocbs per ring and iotags. This function is
6553 * called while driver attach to the HBA and before the
6554 * interrupts are enabled. So there is no need for locking.
6555 *
6556 * This function always returns 0.
6557 **/
dea3101e 6558int
6559lpfc_sli_setup(struct lpfc_hba *phba)
6560{
ed957684 6561 int i, totiocbsize = 0;
dea3101e 6562 struct lpfc_sli *psli = &phba->sli;
6563 struct lpfc_sli_ring *pring;
6564
6565 psli->num_rings = MAX_CONFIGURED_RINGS;
6566 psli->sli_flag = 0;
6567 psli->fcp_ring = LPFC_FCP_RING;
6568 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6569 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6570
604a3e30
JB
6571 psli->iocbq_lookup = NULL;
6572 psli->iocbq_lookup_len = 0;
6573 psli->last_iotag = 0;
6574
dea3101e 6575 for (i = 0; i < psli->num_rings; i++) {
6576 pring = &psli->ring[i];
6577 switch (i) {
6578 case LPFC_FCP_RING: /* ring 0 - FCP */
6579 /* numCiocb and numRiocb are used in config_port */
6580 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6581 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6582 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6583 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6584 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6585 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6586 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6587 SLI3_IOCB_CMD_SIZE :
6588 SLI2_IOCB_CMD_SIZE;
ed957684 6589 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6590 SLI3_IOCB_RSP_SIZE :
6591 SLI2_IOCB_RSP_SIZE;
dea3101e 6592 pring->iotag_ctr = 0;
6593 pring->iotag_max =
92d7f7b0 6594 (phba->cfg_hba_queue_depth * 2);
dea3101e 6595 pring->fast_iotag = pring->iotag_max;
6596 pring->num_mask = 0;
6597 break;
a4bc3379 6598 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 6599 /* numCiocb and numRiocb are used in config_port */
6600 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6601 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6602 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6603 SLI3_IOCB_CMD_SIZE :
6604 SLI2_IOCB_CMD_SIZE;
ed957684 6605 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6606 SLI3_IOCB_RSP_SIZE :
6607 SLI2_IOCB_RSP_SIZE;
2e0fef85 6608 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 6609 pring->num_mask = 0;
6610 break;
6611 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6612 /* numCiocb and numRiocb are used in config_port */
6613 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6614 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6615 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6616 SLI3_IOCB_CMD_SIZE :
6617 SLI2_IOCB_CMD_SIZE;
ed957684 6618 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6619 SLI3_IOCB_RSP_SIZE :
6620 SLI2_IOCB_RSP_SIZE;
dea3101e 6621 pring->fast_iotag = 0;
6622 pring->iotag_ctr = 0;
6623 pring->iotag_max = 4096;
57127f15
JS
6624 pring->lpfc_sli_rcv_async_status =
6625 lpfc_sli_async_event_handler;
6669f9bb 6626 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6627 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6628 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6629 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6630 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6631 lpfc_els_unsol_event;
dea3101e 6632 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6633 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6634 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6635 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6636 lpfc_els_unsol_event;
dea3101e 6637 pring->prt[2].profile = 0; /* Mask 2 */
6638 /* NameServer Inquiry */
6a9c52cf 6639 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6640 /* NameServer */
6a9c52cf 6641 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6642 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6643 lpfc_ct_unsol_event;
dea3101e 6644 pring->prt[3].profile = 0; /* Mask 3 */
6645 /* NameServer response */
6a9c52cf 6646 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6647 /* NameServer */
6a9c52cf 6648 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6649 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6650 lpfc_ct_unsol_event;
6669f9bb
JS
6651 /* abort unsolicited sequence */
6652 pring->prt[4].profile = 0; /* Mask 4 */
6653 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6654 pring->prt[4].type = FC_TYPE_BLS;
6655 pring->prt[4].lpfc_sli_rcv_unsol_event =
6656 lpfc_sli4_ct_abort_unsol_event;
dea3101e 6657 break;
6658 }
ed957684 6659 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6660 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6661 }
ed957684 6662 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6663 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6664 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6665 "SLI2 SLIM Data: x%x x%lx\n",
6666 phba->brd_no, totiocbsize,
6667 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6668 }
cf5bf97e
JW
6669 if (phba->cfg_multi_ring_support == 2)
6670 lpfc_extra_ring_setup(phba);
dea3101e 6671
6672 return 0;
6673}
6674
e59058c4 6675/**
3621a710 6676 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6677 * @phba: Pointer to HBA context object.
6678 *
6679 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6680 * ring. This function also initializes ring indices of each ring.
6681 * This function is called during the initialization of the SLI
6682 * interface of an HBA.
6683 * This function is called with no lock held and always returns
6684 * 1.
6685 **/
dea3101e 6686int
2e0fef85 6687lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 6688{
6689 struct lpfc_sli *psli;
6690 struct lpfc_sli_ring *pring;
604a3e30 6691 int i;
dea3101e 6692
6693 psli = &phba->sli;
2e0fef85 6694 spin_lock_irq(&phba->hbalock);
dea3101e 6695 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6696 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 6697 /* Initialize list headers for txq and txcmplq as double linked lists */
6698 for (i = 0; i < psli->num_rings; i++) {
6699 pring = &psli->ring[i];
6700 pring->ringno = i;
6701 pring->next_cmdidx = 0;
6702 pring->local_getidx = 0;
6703 pring->cmdidx = 0;
6704 INIT_LIST_HEAD(&pring->txq);
6705 INIT_LIST_HEAD(&pring->txcmplq);
6706 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6707 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6708 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6709 }
2e0fef85
JS
6710 spin_unlock_irq(&phba->hbalock);
6711 return 1;
dea3101e 6712}
6713
04c68496
JS
6714/**
6715 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6716 * @phba: Pointer to HBA context object.
6717 *
6718 * This routine flushes the mailbox command subsystem. It will unconditionally
6719 * flush all the mailbox commands in the three possible stages in the mailbox
6720 * command sub-system: pending mailbox command queue; the outstanding mailbox
6721 * command; and completed mailbox command queue. It is caller's responsibility
6722 * to make sure that the driver is in the proper state to flush the mailbox
6723 * command sub-system. Namely, the posting of mailbox commands into the
6724 * pending mailbox command queue from the various clients must be stopped;
6725 * either the HBA is in a state that it will never works on the outstanding
6726 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6727 * mailbox command has been completed.
6728 **/
6729static void
6730lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6731{
6732 LIST_HEAD(completions);
6733 struct lpfc_sli *psli = &phba->sli;
6734 LPFC_MBOXQ_t *pmb;
6735 unsigned long iflag;
6736
6737 /* Flush all the mailbox commands in the mbox system */
6738 spin_lock_irqsave(&phba->hbalock, iflag);
6739 /* The pending mailbox command queue */
6740 list_splice_init(&phba->sli.mboxq, &completions);
6741 /* The outstanding active mailbox command */
6742 if (psli->mbox_active) {
6743 list_add_tail(&psli->mbox_active->list, &completions);
6744 psli->mbox_active = NULL;
6745 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6746 }
6747 /* The completed mailbox command queue */
6748 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6749 spin_unlock_irqrestore(&phba->hbalock, iflag);
6750
6751 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6752 while (!list_empty(&completions)) {
6753 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6754 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6755 if (pmb->mbox_cmpl)
6756 pmb->mbox_cmpl(phba, pmb);
6757 }
6758}
6759
e59058c4 6760/**
3621a710 6761 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6762 * @vport: Pointer to virtual port object.
6763 *
6764 * lpfc_sli_host_down is called to clean up the resources
6765 * associated with a vport before destroying virtual
6766 * port data structures.
6767 * This function does following operations:
6768 * - Free discovery resources associated with this virtual
6769 * port.
6770 * - Free iocbs associated with this virtual port in
6771 * the txq.
6772 * - Send abort for all iocb commands associated with this
6773 * vport in txcmplq.
6774 *
6775 * This function is called with no lock held and always returns 1.
6776 **/
92d7f7b0
JS
6777int
6778lpfc_sli_host_down(struct lpfc_vport *vport)
6779{
858c9f6c 6780 LIST_HEAD(completions);
92d7f7b0
JS
6781 struct lpfc_hba *phba = vport->phba;
6782 struct lpfc_sli *psli = &phba->sli;
6783 struct lpfc_sli_ring *pring;
6784 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6785 int i;
6786 unsigned long flags = 0;
6787 uint16_t prev_pring_flag;
6788
6789 lpfc_cleanup_discovery_resources(vport);
6790
6791 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6792 for (i = 0; i < psli->num_rings; i++) {
6793 pring = &psli->ring[i];
6794 prev_pring_flag = pring->flag;
5e9d9b82
JS
6795 /* Only slow rings */
6796 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6797 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6798 /* Set the lpfc data pending flag */
6799 set_bit(LPFC_DATA_READY, &phba->data_flags);
6800 }
92d7f7b0
JS
6801 /*
6802 * Error everything on the txq since these iocbs have not been
6803 * given to the FW yet.
6804 */
92d7f7b0
JS
6805 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6806 if (iocb->vport != vport)
6807 continue;
858c9f6c 6808 list_move_tail(&iocb->list, &completions);
92d7f7b0 6809 pring->txq_cnt--;
92d7f7b0
JS
6810 }
6811
6812 /* Next issue ABTS for everything on the txcmplq */
6813 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6814 list) {
6815 if (iocb->vport != vport)
6816 continue;
6817 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6818 }
6819
6820 pring->flag = prev_pring_flag;
6821 }
6822
6823 spin_unlock_irqrestore(&phba->hbalock, flags);
6824
a257bf90
JS
6825 /* Cancel all the IOCBs from the completions list */
6826 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6827 IOERR_SLI_DOWN);
92d7f7b0
JS
6828 return 1;
6829}
6830
e59058c4 6831/**
3621a710 6832 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6833 * @phba: Pointer to HBA context object.
6834 *
6835 * This function cleans up all iocb, buffers, mailbox commands
6836 * while shutting down the HBA. This function is called with no
6837 * lock held and always returns 1.
6838 * This function does the following to cleanup driver resources:
6839 * - Free discovery resources for each virtual port
6840 * - Cleanup any pending fabric iocbs
6841 * - Iterate through the iocb txq and free each entry
6842 * in the list.
6843 * - Free up any buffer posted to the HBA
6844 * - Free mailbox commands in the mailbox queue.
6845 **/
dea3101e 6846int
2e0fef85 6847lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6848{
2534ba75 6849 LIST_HEAD(completions);
2e0fef85 6850 struct lpfc_sli *psli = &phba->sli;
dea3101e 6851 struct lpfc_sli_ring *pring;
0ff10d46 6852 struct lpfc_dmabuf *buf_ptr;
dea3101e 6853 unsigned long flags = 0;
04c68496
JS
6854 int i;
6855
6856 /* Shutdown the mailbox command sub-system */
6857 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6858
dea3101e 6859 lpfc_hba_down_prep(phba);
6860
92d7f7b0
JS
6861 lpfc_fabric_abort_hba(phba);
6862
2e0fef85 6863 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 6864 for (i = 0; i < psli->num_rings; i++) {
6865 pring = &psli->ring[i];
5e9d9b82
JS
6866 /* Only slow rings */
6867 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6868 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6869 /* Set the lpfc data pending flag */
6870 set_bit(LPFC_DATA_READY, &phba->data_flags);
6871 }
dea3101e 6872
6873 /*
6874 * Error everything on the txq since these iocbs have not been
6875 * given to the FW yet.
6876 */
2534ba75 6877 list_splice_init(&pring->txq, &completions);
dea3101e 6878 pring->txq_cnt = 0;
6879
2534ba75 6880 }
2e0fef85 6881 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6882
a257bf90
JS
6883 /* Cancel all the IOCBs from the completions list */
6884 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6885 IOERR_SLI_DOWN);
dea3101e 6886
0ff10d46
JS
6887 spin_lock_irqsave(&phba->hbalock, flags);
6888 list_splice_init(&phba->elsbuf, &completions);
6889 phba->elsbuf_cnt = 0;
6890 phba->elsbuf_prev_cnt = 0;
6891 spin_unlock_irqrestore(&phba->hbalock, flags);
6892
6893 while (!list_empty(&completions)) {
6894 list_remove_head(&completions, buf_ptr,
6895 struct lpfc_dmabuf, list);
6896 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6897 kfree(buf_ptr);
6898 }
6899
dea3101e 6900 /* Return any active mbox cmds */
6901 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6902
da0436e9 6903 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6904 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6905 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6906
da0436e9
JS
6907 return 1;
6908}
6909
6910/**
6911 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6912 * @phba: Pointer to HBA context object.
6913 *
6914 * This function cleans up all queues, iocb, buffers, mailbox commands while
6915 * shutting down the SLI4 HBA FCoE function. This function is called with no
6916 * lock held and always returns 1.
6917 *
6918 * This function does the following to cleanup driver FCoE function resources:
6919 * - Free discovery resources for each virtual port
6920 * - Cleanup any pending fabric iocbs
6921 * - Iterate through the iocb txq and free each entry in the list.
6922 * - Free up any buffer posted to the HBA.
6923 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6924 * - Free mailbox commands in the mailbox queue.
6925 **/
6926int
6927lpfc_sli4_hba_down(struct lpfc_hba *phba)
6928{
6929 /* Stop the SLI4 device port */
6930 lpfc_stop_port(phba);
6931
6932 /* Tear down the queues in the HBA */
6933 lpfc_sli4_queue_unset(phba);
6934
6935 /* unregister default FCFI from the HBA */
6936 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
92d7f7b0 6937
dea3101e 6938 return 1;
6939}
6940
e59058c4 6941/**
3621a710 6942 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6943 * @srcp: Source memory pointer.
6944 * @destp: Destination memory pointer.
6945 * @cnt: Number of words required to be copied.
6946 *
6947 * This function is used for copying data between driver memory
6948 * and the SLI memory. This function also changes the endianness
6949 * of each word if native endianness is different from SLI
6950 * endianness. This function can be called with or without
6951 * lock.
6952 **/
dea3101e 6953void
6954lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6955{
6956 uint32_t *src = srcp;
6957 uint32_t *dest = destp;
6958 uint32_t ldata;
6959 int i;
6960
6961 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6962 ldata = *src;
6963 ldata = le32_to_cpu(ldata);
6964 *dest = ldata;
6965 src++;
6966 dest++;
6967 }
6968}
6969
e59058c4 6970
a0c87cbd
JS
6971/**
6972 * lpfc_sli_bemem_bcopy - SLI memory copy function
6973 * @srcp: Source memory pointer.
6974 * @destp: Destination memory pointer.
6975 * @cnt: Number of words required to be copied.
6976 *
6977 * This function is used for copying data between a data structure
6978 * with big endian representation to local endianness.
6979 * This function can be called with or without lock.
6980 **/
6981void
6982lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6983{
6984 uint32_t *src = srcp;
6985 uint32_t *dest = destp;
6986 uint32_t ldata;
6987 int i;
6988
6989 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6990 ldata = *src;
6991 ldata = be32_to_cpu(ldata);
6992 *dest = ldata;
6993 src++;
6994 dest++;
6995 }
6996}
6997
e59058c4 6998/**
3621a710 6999 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
7000 * @phba: Pointer to HBA context object.
7001 * @pring: Pointer to driver SLI ring object.
7002 * @mp: Pointer to driver buffer object.
7003 *
7004 * This function is called with no lock held.
7005 * It always return zero after adding the buffer to the postbufq
7006 * buffer list.
7007 **/
dea3101e 7008int
2e0fef85
JS
7009lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7010 struct lpfc_dmabuf *mp)
dea3101e 7011{
7012 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7013 later */
2e0fef85 7014 spin_lock_irq(&phba->hbalock);
dea3101e 7015 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 7016 pring->postbufq_cnt++;
2e0fef85 7017 spin_unlock_irq(&phba->hbalock);
dea3101e 7018 return 0;
7019}
7020
e59058c4 7021/**
3621a710 7022 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
7023 * @phba: Pointer to HBA context object.
7024 *
7025 * When HBQ is enabled, buffers are searched based on tags. This function
7026 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7027 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7028 * does not conflict with tags of buffer posted for unsolicited events.
7029 * The function returns the allocated tag. The function is called with
7030 * no locks held.
7031 **/
76bb24ef
JS
7032uint32_t
7033lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7034{
7035 spin_lock_irq(&phba->hbalock);
7036 phba->buffer_tag_count++;
7037 /*
7038 * Always set the QUE_BUFTAG_BIT to distiguish between
7039 * a tag assigned by HBQ.
7040 */
7041 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7042 spin_unlock_irq(&phba->hbalock);
7043 return phba->buffer_tag_count;
7044}
7045
e59058c4 7046/**
3621a710 7047 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
7048 * @phba: Pointer to HBA context object.
7049 * @pring: Pointer to driver SLI ring object.
7050 * @tag: Buffer tag.
7051 *
7052 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7053 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7054 * iocb is posted to the response ring with the tag of the buffer.
7055 * This function searches the pring->postbufq list using the tag
7056 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7057 * iocb. If the buffer is found then lpfc_dmabuf object of the
7058 * buffer is returned to the caller else NULL is returned.
7059 * This function is called with no lock held.
7060 **/
76bb24ef
JS
7061struct lpfc_dmabuf *
7062lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7063 uint32_t tag)
7064{
7065 struct lpfc_dmabuf *mp, *next_mp;
7066 struct list_head *slp = &pring->postbufq;
7067
7068 /* Search postbufq, from the begining, looking for a match on tag */
7069 spin_lock_irq(&phba->hbalock);
7070 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7071 if (mp->buffer_tag == tag) {
7072 list_del_init(&mp->list);
7073 pring->postbufq_cnt--;
7074 spin_unlock_irq(&phba->hbalock);
7075 return mp;
7076 }
7077 }
7078
7079 spin_unlock_irq(&phba->hbalock);
7080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 7081 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
7082 "ring %d Data x%lx x%p x%p x%x\n",
7083 pring->ringno, (unsigned long) tag,
7084 slp->next, slp->prev, pring->postbufq_cnt);
7085
7086 return NULL;
7087}
dea3101e 7088
e59058c4 7089/**
3621a710 7090 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
7091 * @phba: Pointer to HBA context object.
7092 * @pring: Pointer to driver SLI ring object.
7093 * @phys: DMA address of the buffer.
7094 *
7095 * This function searches the buffer list using the dma_address
7096 * of unsolicited event to find the driver's lpfc_dmabuf object
7097 * corresponding to the dma_address. The function returns the
7098 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7099 * This function is called by the ct and els unsolicited event
7100 * handlers to get the buffer associated with the unsolicited
7101 * event.
7102 *
7103 * This function is called with no lock held.
7104 **/
dea3101e 7105struct lpfc_dmabuf *
7106lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7107 dma_addr_t phys)
7108{
7109 struct lpfc_dmabuf *mp, *next_mp;
7110 struct list_head *slp = &pring->postbufq;
7111
7112 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 7113 spin_lock_irq(&phba->hbalock);
dea3101e 7114 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7115 if (mp->phys == phys) {
7116 list_del_init(&mp->list);
7117 pring->postbufq_cnt--;
2e0fef85 7118 spin_unlock_irq(&phba->hbalock);
dea3101e 7119 return mp;
7120 }
7121 }
7122
2e0fef85 7123 spin_unlock_irq(&phba->hbalock);
dea3101e 7124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 7125 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 7126 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 7127 pring->ringno, (unsigned long long)phys,
dea3101e 7128 slp->next, slp->prev, pring->postbufq_cnt);
7129 return NULL;
7130}
7131
e59058c4 7132/**
3621a710 7133 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
7134 * @phba: Pointer to HBA context object.
7135 * @cmdiocb: Pointer to driver command iocb object.
7136 * @rspiocb: Pointer to driver response iocb object.
7137 *
7138 * This function is the completion handler for the abort iocbs for
7139 * ELS commands. This function is called from the ELS ring event
7140 * handler with no lock held. This function frees memory resources
7141 * associated with the abort iocb.
7142 **/
dea3101e 7143static void
2e0fef85
JS
7144lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7145 struct lpfc_iocbq *rspiocb)
dea3101e 7146{
2e0fef85 7147 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7148 uint16_t abort_iotag, abort_context;
92d7f7b0 7149 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7150 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7151
7152 abort_iocb = NULL;
2680eeaa
JS
7153
7154 if (irsp->ulpStatus) {
7155 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7156 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7157
2e0fef85 7158 spin_lock_irq(&phba->hbalock);
45ed1190
JS
7159 if (phba->sli_rev < LPFC_SLI_REV4) {
7160 if (abort_iotag != 0 &&
7161 abort_iotag <= phba->sli.last_iotag)
7162 abort_iocb =
7163 phba->sli.iocbq_lookup[abort_iotag];
7164 } else
7165 /* For sli4 the abort_tag is the XRI,
7166 * so the abort routine puts the iotag of the iocb
7167 * being aborted in the context field of the abort
7168 * IOCB.
7169 */
7170 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 7171
58da1ffb
JS
7172 /*
7173 * If the iocb is not found in Firmware queue the iocb
7174 * might have completed already. Do not free it again.
7175 */
9b379605 7176 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
7177 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7178 spin_unlock_irq(&phba->hbalock);
7179 lpfc_sli_release_iocbq(phba, cmdiocb);
7180 return;
7181 }
7182 /* For SLI4 the ulpContext field for abort IOCB
7183 * holds the iotag of the IOCB being aborted so
7184 * the local abort_context needs to be reset to
7185 * match the aborted IOCBs ulpContext.
7186 */
7187 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7188 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 7189 }
2a9bf3d0
JS
7190
7191 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7192 "0327 Cannot abort els iocb %p "
7193 "with tag %x context %x, abort status %x, "
7194 "abort code %x\n",
7195 abort_iocb, abort_iotag, abort_context,
7196 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa
JS
7197 /*
7198 * make sure we have the right iocbq before taking it
7199 * off the txcmplq and try to call completion routine.
7200 */
2e0fef85
JS
7201 if (!abort_iocb ||
7202 abort_iocb->iocb.ulpContext != abort_context ||
7203 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7204 spin_unlock_irq(&phba->hbalock);
341af102
JS
7205 else if (phba->sli_rev < LPFC_SLI_REV4) {
7206 /*
7207 * leave the SLI4 aborted command on the txcmplq
7208 * list and the command complete WCQE's XB bit
7209 * will tell whether the SGL (XRI) can be released
7210 * immediately or to the aborted SGL list for the
7211 * following abort XRI from the HBA.
7212 */
92d7f7b0 7213 list_del_init(&abort_iocb->list);
2a9bf3d0
JS
7214 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7215 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7216 pring->txcmplq_cnt--;
7217 }
2680eeaa 7218
0ff10d46
JS
7219 /* Firmware could still be in progress of DMAing
7220 * payload, so don't free data buffer till after
7221 * a hbeat.
7222 */
7223 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 7224 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
7225 spin_unlock_irq(&phba->hbalock);
7226
92d7f7b0 7227 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 7228 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 7229 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
49198b37
JS
7230 } else
7231 spin_unlock_irq(&phba->hbalock);
2680eeaa
JS
7232 }
7233
604a3e30 7234 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 7235 return;
7236}
7237
e59058c4 7238/**
3621a710 7239 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7240 * @phba: Pointer to HBA context object.
7241 * @cmdiocb: Pointer to driver command iocb object.
7242 * @rspiocb: Pointer to driver response iocb object.
7243 *
7244 * The function is called from SLI ring event handler with no
7245 * lock held. This function is the completion handler for ELS commands
7246 * which are aborted. The function frees memory resources used for
7247 * the aborted ELS commands.
7248 **/
92d7f7b0
JS
7249static void
7250lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7251 struct lpfc_iocbq *rspiocb)
7252{
7253 IOCB_t *irsp = &rspiocb->iocb;
7254
7255 /* ELS cmd tag <ulpIoTag> completes */
7256 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7257 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7258 "x%x x%x x%x\n",
e8b62011 7259 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7260 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7261 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7262 lpfc_ct_free_iocb(phba, cmdiocb);
7263 else
7264 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7265 return;
7266}
7267
e59058c4 7268/**
3621a710 7269 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
e59058c4
JS
7270 * @phba: Pointer to HBA context object.
7271 * @pring: Pointer to driver SLI ring object.
7272 * @cmdiocb: Pointer to driver command iocb object.
7273 *
7274 * This function issues an abort iocb for the provided command
7275 * iocb. This function is called with hbalock held.
7276 * The function returns 0 when it fails due to memory allocation
7277 * failure or when the command iocb is an abort request.
7278 **/
dea3101e 7279int
2e0fef85
JS
7280lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7281 struct lpfc_iocbq *cmdiocb)
dea3101e 7282{
2e0fef85 7283 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7284 struct lpfc_iocbq *abtsiocbp;
dea3101e 7285 IOCB_t *icmd = NULL;
7286 IOCB_t *iabt = NULL;
07951076
JS
7287 int retval = IOCB_ERROR;
7288
92d7f7b0
JS
7289 /*
7290 * There are certain command types we don't want to abort. And we
7291 * don't want to abort commands that are already in the process of
7292 * being aborted.
07951076
JS
7293 */
7294 icmd = &cmdiocb->iocb;
2e0fef85 7295 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7296 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7297 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7298 return 0;
7299
858c9f6c
JS
7300 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7301 * callback so that nothing happens when it finishes.
07951076 7302 */
858c9f6c
JS
7303 if ((vport->load_flag & FC_UNLOADING) &&
7304 (pring->ringno == LPFC_ELS_RING)) {
92d7f7b0
JS
7305 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7306 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7307 else
7308 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
07951076 7309 goto abort_iotag_exit;
92d7f7b0 7310 }
dea3101e 7311
7312 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7313 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 7314 if (abtsiocbp == NULL)
7315 return 0;
dea3101e 7316
07951076 7317 /* This signals the response to set the correct status
341af102 7318 * before calling the completion handler
07951076
JS
7319 */
7320 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7321
dea3101e 7322 iabt = &abtsiocbp->iocb;
07951076
JS
7323 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7324 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 7325 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 7326 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
7327 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7328 }
da0436e9
JS
7329 else
7330 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7331 iabt->ulpLe = 1;
7332 iabt->ulpClass = icmd->ulpClass;
dea3101e 7333
5ffc266e
JS
7334 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7335 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
7336 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7337 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7338
2e0fef85 7339 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7340 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7341 else
7342 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7343
07951076 7344 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7345
e8b62011
JS
7346 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7347 "0339 Abort xri x%x, original iotag x%x, "
7348 "abort cmd iotag x%x\n",
2a9bf3d0 7349 iabt->un.acxri.abortIoTag,
e8b62011 7350 iabt->un.acxri.abortContextTag,
2a9bf3d0 7351 abtsiocbp->iotag);
da0436e9 7352 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7353
d7c255b2
JS
7354 if (retval)
7355 __lpfc_sli_release_iocbq(phba, abtsiocbp);
07951076 7356abort_iotag_exit:
2e0fef85
JS
7357 /*
7358 * Caller to this routine should check for IOCB_ERROR
7359 * and handle it properly. This routine no longer removes
7360 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7361 */
2e0fef85 7362 return retval;
dea3101e 7363}
7364
e59058c4 7365/**
3621a710 7366 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7367 * @iocbq: Pointer to driver iocb object.
7368 * @vport: Pointer to driver virtual port object.
7369 * @tgt_id: SCSI ID of the target.
7370 * @lun_id: LUN ID of the scsi device.
7371 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7372 *
3621a710 7373 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7374 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7375 * 0 if the filtering criteria is met for the given iocb and will return
7376 * 1 if the filtering criteria is not met.
7377 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7378 * given iocb is for the SCSI device specified by vport, tgt_id and
7379 * lun_id parameter.
7380 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7381 * given iocb is for the SCSI target specified by vport and tgt_id
7382 * parameters.
7383 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7384 * given iocb is for the SCSI host associated with the given vport.
7385 * This function is called with no locks held.
7386 **/
dea3101e 7387static int
51ef4c26
JS
7388lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7389 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7390 lpfc_ctx_cmd ctx_cmd)
dea3101e 7391{
0bd4ca25 7392 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7393 int rc = 1;
7394
0bd4ca25
JSEC
7395 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7396 return rc;
7397
51ef4c26
JS
7398 if (iocbq->vport != vport)
7399 return rc;
7400
0bd4ca25 7401 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7402
495a714c 7403 if (lpfc_cmd->pCmd == NULL)
dea3101e 7404 return rc;
7405
7406 switch (ctx_cmd) {
7407 case LPFC_CTX_LUN:
495a714c
JS
7408 if ((lpfc_cmd->rdata->pnode) &&
7409 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7410 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 7411 rc = 0;
7412 break;
7413 case LPFC_CTX_TGT:
495a714c
JS
7414 if ((lpfc_cmd->rdata->pnode) &&
7415 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 7416 rc = 0;
7417 break;
dea3101e 7418 case LPFC_CTX_HOST:
7419 rc = 0;
7420 break;
7421 default:
7422 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7423 __func__, ctx_cmd);
dea3101e 7424 break;
7425 }
7426
7427 return rc;
7428}
7429
e59058c4 7430/**
3621a710 7431 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7432 * @vport: Pointer to virtual port.
7433 * @tgt_id: SCSI ID of the target.
7434 * @lun_id: LUN ID of the scsi device.
7435 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7436 *
7437 * This function returns number of FCP commands pending for the vport.
7438 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7439 * commands pending on the vport associated with SCSI device specified
7440 * by tgt_id and lun_id parameters.
7441 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7442 * commands pending on the vport associated with SCSI target specified
7443 * by tgt_id parameter.
7444 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7445 * commands pending on the vport.
7446 * This function returns the number of iocbs which satisfy the filter.
7447 * This function is called without any lock held.
7448 **/
dea3101e 7449int
51ef4c26
JS
7450lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7451 lpfc_ctx_cmd ctx_cmd)
dea3101e 7452{
51ef4c26 7453 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7454 struct lpfc_iocbq *iocbq;
7455 int sum, i;
dea3101e 7456
0bd4ca25
JSEC
7457 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7458 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7459
51ef4c26
JS
7460 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7461 ctx_cmd) == 0)
0bd4ca25 7462 sum++;
dea3101e 7463 }
0bd4ca25 7464
dea3101e 7465 return sum;
7466}
7467
e59058c4 7468/**
3621a710 7469 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7470 * @phba: Pointer to HBA context object
7471 * @cmdiocb: Pointer to command iocb object.
7472 * @rspiocb: Pointer to response iocb object.
7473 *
7474 * This function is called when an aborted FCP iocb completes. This
7475 * function is called by the ring event handler with no lock held.
7476 * This function frees the iocb.
7477 **/
5eb95af0 7478void
2e0fef85
JS
7479lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7480 struct lpfc_iocbq *rspiocb)
5eb95af0 7481{
604a3e30 7482 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7483 return;
7484}
7485
e59058c4 7486/**
3621a710 7487 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7488 * @vport: Pointer to virtual port.
7489 * @pring: Pointer to driver SLI ring object.
7490 * @tgt_id: SCSI ID of the target.
7491 * @lun_id: LUN ID of the scsi device.
7492 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7493 *
7494 * This function sends an abort command for every SCSI command
7495 * associated with the given virtual port pending on the ring
7496 * filtered by lpfc_sli_validate_fcp_iocb function.
7497 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7498 * FCP iocbs associated with lun specified by tgt_id and lun_id
7499 * parameters
7500 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7501 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7502 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7503 * FCP iocbs associated with virtual port.
7504 * This function returns number of iocbs it failed to abort.
7505 * This function is called with no locks held.
7506 **/
dea3101e 7507int
51ef4c26
JS
7508lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7509 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7510{
51ef4c26 7511 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7512 struct lpfc_iocbq *iocbq;
7513 struct lpfc_iocbq *abtsiocb;
dea3101e 7514 IOCB_t *cmd = NULL;
dea3101e 7515 int errcnt = 0, ret_val = 0;
0bd4ca25 7516 int i;
dea3101e 7517
0bd4ca25
JSEC
7518 for (i = 1; i <= phba->sli.last_iotag; i++) {
7519 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7520
51ef4c26 7521 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7522 abort_cmd) != 0)
dea3101e 7523 continue;
7524
7525 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7526 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 7527 if (abtsiocb == NULL) {
7528 errcnt++;
7529 continue;
7530 }
dea3101e 7531
0bd4ca25 7532 cmd = &iocbq->iocb;
dea3101e 7533 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7534 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7535 if (phba->sli_rev == LPFC_SLI_REV4)
7536 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7537 else
7538 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 7539 abtsiocb->iocb.ulpLe = 1;
7540 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7541 abtsiocb->vport = phba->pport;
dea3101e 7542
5ffc266e
JS
7543 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7544 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
7545 if (iocbq->iocb_flag & LPFC_IO_FCP)
7546 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7547
2e0fef85 7548 if (lpfc_is_link_up(phba))
dea3101e 7549 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7550 else
7551 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7552
5eb95af0
JSEC
7553 /* Setup callback routine and issue the command. */
7554 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7555 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7556 abtsiocb, 0);
dea3101e 7557 if (ret_val == IOCB_ERROR) {
604a3e30 7558 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 7559 errcnt++;
7560 continue;
7561 }
7562 }
7563
7564 return errcnt;
7565}
7566
e59058c4 7567/**
3621a710 7568 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7569 * @phba: Pointer to HBA context object.
7570 * @cmdiocbq: Pointer to command iocb.
7571 * @rspiocbq: Pointer to response iocb.
7572 *
7573 * This function is the completion handler for iocbs issued using
7574 * lpfc_sli_issue_iocb_wait function. This function is called by the
7575 * ring event handler function without any lock held. This function
7576 * can be called from both worker thread context and interrupt
7577 * context. This function also can be called from other thread which
7578 * cleans up the SLI layer objects.
7579 * This function copy the contents of the response iocb to the
7580 * response iocb memory object provided by the caller of
7581 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7582 * sleeps for the iocb completion.
7583 **/
68876920
JSEC
7584static void
7585lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7586 struct lpfc_iocbq *cmdiocbq,
7587 struct lpfc_iocbq *rspiocbq)
dea3101e 7588{
68876920
JSEC
7589 wait_queue_head_t *pdone_q;
7590 unsigned long iflags;
0f65ff68 7591 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7592
2e0fef85 7593 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7594 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7595 if (cmdiocbq->context2 && rspiocbq)
7596 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7597 &rspiocbq->iocb, sizeof(IOCB_t));
7598
0f65ff68
JS
7599 /* Set the exchange busy flag for task management commands */
7600 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7601 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7602 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7603 cur_iocbq);
7604 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7605 }
7606
68876920 7607 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7608 if (pdone_q)
7609 wake_up(pdone_q);
858c9f6c 7610 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 7611 return;
7612}
7613
d11e31dd
JS
7614/**
7615 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7616 * @phba: Pointer to HBA context object..
7617 * @piocbq: Pointer to command iocb.
7618 * @flag: Flag to test.
7619 *
7620 * This routine grabs the hbalock and then test the iocb_flag to
7621 * see if the passed in flag is set.
7622 * Returns:
7623 * 1 if flag is set.
7624 * 0 if flag is not set.
7625 **/
7626static int
7627lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7628 struct lpfc_iocbq *piocbq, uint32_t flag)
7629{
7630 unsigned long iflags;
7631 int ret;
7632
7633 spin_lock_irqsave(&phba->hbalock, iflags);
7634 ret = piocbq->iocb_flag & flag;
7635 spin_unlock_irqrestore(&phba->hbalock, iflags);
7636 return ret;
7637
7638}
7639
e59058c4 7640/**
3621a710 7641 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7642 * @phba: Pointer to HBA context object..
7643 * @pring: Pointer to sli ring.
7644 * @piocb: Pointer to command iocb.
7645 * @prspiocbq: Pointer to response iocb.
7646 * @timeout: Timeout in number of seconds.
7647 *
7648 * This function issues the iocb to firmware and waits for the
7649 * iocb to complete. If the iocb command is not
7650 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7651 * Caller should not free the iocb resources if this function
7652 * returns IOCB_TIMEDOUT.
7653 * The function waits for the iocb completion using an
7654 * non-interruptible wait.
7655 * This function will sleep while waiting for iocb completion.
7656 * So, this function should not be called from any context which
7657 * does not allow sleeping. Due to the same reason, this function
7658 * cannot be called with interrupt disabled.
7659 * This function assumes that the iocb completions occur while
7660 * this function sleep. So, this function cannot be called from
7661 * the thread which process iocb completion for this ring.
7662 * This function clears the iocb_flag of the iocb object before
7663 * issuing the iocb and the iocb completion handler sets this
7664 * flag and wakes this thread when the iocb completes.
7665 * The contents of the response iocb will be copied to prspiocbq
7666 * by the completion handler when the command completes.
7667 * This function returns IOCB_SUCCESS when success.
7668 * This function is called with no lock held.
7669 **/
dea3101e 7670int
2e0fef85 7671lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7672 uint32_t ring_number,
2e0fef85
JS
7673 struct lpfc_iocbq *piocb,
7674 struct lpfc_iocbq *prspiocbq,
68876920 7675 uint32_t timeout)
dea3101e 7676{
7259f0d0 7677 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7678 long timeleft, timeout_req = 0;
7679 int retval = IOCB_SUCCESS;
875fbdfe 7680 uint32_t creg_val;
2a9bf3d0 7681 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 7682 /*
68876920
JSEC
7683 * If the caller has provided a response iocbq buffer, then context2
7684 * is NULL or its an error.
dea3101e 7685 */
68876920
JSEC
7686 if (prspiocbq) {
7687 if (piocb->context2)
7688 return IOCB_ERROR;
7689 piocb->context2 = prspiocbq;
dea3101e 7690 }
7691
68876920
JSEC
7692 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7693 piocb->context_un.wait_queue = &done_q;
7694 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7695
875fbdfe
JSEC
7696 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7697 creg_val = readl(phba->HCregaddr);
7698 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7699 writel(creg_val, phba->HCregaddr);
7700 readl(phba->HCregaddr); /* flush */
7701 }
7702
2a9bf3d0
JS
7703 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7704 SLI_IOCB_RET_IOCB);
68876920
JSEC
7705 if (retval == IOCB_SUCCESS) {
7706 timeout_req = timeout * HZ;
68876920 7707 timeleft = wait_event_timeout(done_q,
d11e31dd 7708 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7709 timeout_req);
dea3101e 7710
7054a606
JS
7711 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7712 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7713 "0331 IOCB wake signaled\n");
7054a606 7714 } else if (timeleft == 0) {
68876920 7715 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7716 "0338 IOCB wait timeout error - no "
7717 "wake response Data x%x\n", timeout);
68876920 7718 retval = IOCB_TIMEDOUT;
7054a606 7719 } else {
68876920 7720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7721 "0330 IOCB wake NOT set, "
7722 "Data x%x x%lx\n",
68876920
JSEC
7723 timeout, (timeleft / jiffies));
7724 retval = IOCB_TIMEDOUT;
dea3101e 7725 }
2a9bf3d0
JS
7726 } else if (retval == IOCB_BUSY) {
7727 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7728 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7729 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7730 return retval;
68876920
JSEC
7731 } else {
7732 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7733 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7734 retval);
68876920 7735 retval = IOCB_ERROR;
dea3101e 7736 }
7737
875fbdfe
JSEC
7738 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7739 creg_val = readl(phba->HCregaddr);
7740 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7741 writel(creg_val, phba->HCregaddr);
7742 readl(phba->HCregaddr); /* flush */
7743 }
7744
68876920
JSEC
7745 if (prspiocbq)
7746 piocb->context2 = NULL;
7747
7748 piocb->context_un.wait_queue = NULL;
7749 piocb->iocb_cmpl = NULL;
dea3101e 7750 return retval;
7751}
68876920 7752
e59058c4 7753/**
3621a710 7754 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7755 * @phba: Pointer to HBA context object.
7756 * @pmboxq: Pointer to driver mailbox object.
7757 * @timeout: Timeout in number of seconds.
7758 *
7759 * This function issues the mailbox to firmware and waits for the
7760 * mailbox command to complete. If the mailbox command is not
7761 * completed within timeout seconds, it returns MBX_TIMEOUT.
7762 * The function waits for the mailbox completion using an
7763 * interruptible wait. If the thread is woken up due to a
7764 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7765 * should not free the mailbox resources, if this function returns
7766 * MBX_TIMEOUT.
7767 * This function will sleep while waiting for mailbox completion.
7768 * So, this function should not be called from any context which
7769 * does not allow sleeping. Due to the same reason, this function
7770 * cannot be called with interrupt disabled.
7771 * This function assumes that the mailbox completion occurs while
7772 * this function sleep. So, this function cannot be called from
7773 * the worker thread which processes mailbox completion.
7774 * This function is called in the context of HBA management
7775 * applications.
7776 * This function returns MBX_SUCCESS when successful.
7777 * This function is called with no lock held.
7778 **/
dea3101e 7779int
2e0fef85 7780lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 7781 uint32_t timeout)
7782{
7259f0d0 7783 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7784 int retval;
858c9f6c 7785 unsigned long flag;
dea3101e 7786
7787 /* The caller must leave context1 empty. */
98c9ea5c 7788 if (pmboxq->context1)
2e0fef85 7789 return MBX_NOT_FINISHED;
dea3101e 7790
495a714c 7791 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 7792 /* setup wake call as IOCB callback */
7793 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7794 /* setup context field to pass wait_queue pointer to wake function */
7795 pmboxq->context1 = &done_q;
7796
dea3101e 7797 /* now issue the command */
7798 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7799
7800 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7801 wait_event_interruptible_timeout(done_q,
7802 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7803 timeout * HZ);
7804
858c9f6c 7805 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7806 pmboxq->context1 = NULL;
7054a606
JS
7807 /*
7808 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7809 * else do not free the resources.
7810 */
d7c47992 7811 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 7812 retval = MBX_SUCCESS;
d7c47992
JS
7813 lpfc_sli4_swap_str(phba, pmboxq);
7814 } else {
7054a606 7815 retval = MBX_TIMEOUT;
858c9f6c
JS
7816 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7817 }
7818 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 7819 }
7820
dea3101e 7821 return retval;
7822}
7823
e59058c4 7824/**
3772a991 7825 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7826 * @phba: Pointer to HBA context.
7827 *
3772a991
JS
7828 * This function is called to shutdown the driver's mailbox sub-system.
7829 * It first marks the mailbox sub-system is in a block state to prevent
7830 * the asynchronous mailbox command from issued off the pending mailbox
7831 * command queue. If the mailbox command sub-system shutdown is due to
7832 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7833 * the mailbox sub-system flush routine to forcefully bring down the
7834 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7835 * as with offline or HBA function reset), this routine will wait for the
7836 * outstanding mailbox command to complete before invoking the mailbox
7837 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7838 **/
3772a991
JS
7839void
7840lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7841{
3772a991
JS
7842 struct lpfc_sli *psli = &phba->sli;
7843 uint8_t actcmd = MBX_HEARTBEAT;
7844 unsigned long timeout;
b4c02652 7845
3772a991
JS
7846 spin_lock_irq(&phba->hbalock);
7847 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7848 spin_unlock_irq(&phba->hbalock);
b4c02652 7849
3772a991 7850 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7851 spin_lock_irq(&phba->hbalock);
3772a991
JS
7852 if (phba->sli.mbox_active)
7853 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7854 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7855 /* Determine how long we might wait for the active mailbox
7856 * command to be gracefully completed by firmware.
7857 */
7858 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7859 1000) + jiffies;
7860 while (phba->sli.mbox_active) {
7861 /* Check active mailbox complete status every 2ms */
7862 msleep(2);
7863 if (time_after(jiffies, timeout))
7864 /* Timeout, let the mailbox flush routine to
7865 * forcefully release active mailbox command
7866 */
7867 break;
7868 }
7869 }
7870 lpfc_sli_mbox_sys_flush(phba);
7871}
ed957684 7872
3772a991
JS
7873/**
7874 * lpfc_sli_eratt_read - read sli-3 error attention events
7875 * @phba: Pointer to HBA context.
7876 *
7877 * This function is called to read the SLI3 device error attention registers
7878 * for possible error attention events. The caller must hold the hostlock
7879 * with spin_lock_irq().
7880 *
7881 * This fucntion returns 1 when there is Error Attention in the Host Attention
7882 * Register and returns 0 otherwise.
7883 **/
7884static int
7885lpfc_sli_eratt_read(struct lpfc_hba *phba)
7886{
7887 uint32_t ha_copy;
b4c02652 7888
3772a991
JS
7889 /* Read chip Host Attention (HA) register */
7890 ha_copy = readl(phba->HAregaddr);
7891 if (ha_copy & HA_ERATT) {
7892 /* Read host status register to retrieve error event */
7893 lpfc_sli_read_hs(phba);
b4c02652 7894
3772a991
JS
7895 /* Check if there is a deferred error condition is active */
7896 if ((HS_FFER1 & phba->work_hs) &&
7897 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7898 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
3772a991 7899 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
7900 /* Clear all interrupt enable conditions */
7901 writel(0, phba->HCregaddr);
7902 readl(phba->HCregaddr);
7903 }
7904
7905 /* Set the driver HA work bitmap */
3772a991
JS
7906 phba->work_ha |= HA_ERATT;
7907 /* Indicate polling handles this ERATT */
7908 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
7909 return 1;
7910 }
7911 return 0;
b4c02652
JS
7912}
7913
da0436e9
JS
7914/**
7915 * lpfc_sli4_eratt_read - read sli-4 error attention events
7916 * @phba: Pointer to HBA context.
7917 *
7918 * This function is called to read the SLI4 device error attention registers
7919 * for possible error attention events. The caller must hold the hostlock
7920 * with spin_lock_irq().
7921 *
7922 * This fucntion returns 1 when there is Error Attention in the Host Attention
7923 * Register and returns 0 otherwise.
7924 **/
7925static int
7926lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7927{
7928 uint32_t uerr_sta_hi, uerr_sta_lo;
da0436e9
JS
7929
7930 /* For now, use the SLI4 device internal unrecoverable error
7931 * registers for error attention. This can be changed later.
7932 */
a747c9ce
JS
7933 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7934 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7935 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7936 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7938 "1423 HBA Unrecoverable error: "
7939 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7940 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7941 uerr_sta_lo, uerr_sta_hi,
7942 phba->sli4_hba.ue_mask_lo,
7943 phba->sli4_hba.ue_mask_hi);
7944 phba->work_status[0] = uerr_sta_lo;
7945 phba->work_status[1] = uerr_sta_hi;
7946 /* Set the driver HA work bitmap */
7947 phba->work_ha |= HA_ERATT;
7948 /* Indicate polling handles this ERATT */
7949 phba->hba_flag |= HBA_ERATT_HANDLED;
7950 return 1;
da0436e9
JS
7951 }
7952 return 0;
7953}
7954
e59058c4 7955/**
3621a710 7956 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
7957 * @phba: Pointer to HBA context.
7958 *
3772a991 7959 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
7960 * error attention register bit for error attention events.
7961 *
7962 * This fucntion returns 1 when there is Error Attention in the Host Attention
7963 * Register and returns 0 otherwise.
7964 **/
7965int
7966lpfc_sli_check_eratt(struct lpfc_hba *phba)
7967{
7968 uint32_t ha_copy;
7969
7970 /* If somebody is waiting to handle an eratt, don't process it
7971 * here. The brdkill function will do this.
7972 */
7973 if (phba->link_flag & LS_IGNORE_ERATT)
7974 return 0;
7975
7976 /* Check if interrupt handler handles this ERATT */
7977 spin_lock_irq(&phba->hbalock);
7978 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7979 /* Interrupt handler has handled ERATT */
7980 spin_unlock_irq(&phba->hbalock);
7981 return 0;
7982 }
7983
a257bf90
JS
7984 /*
7985 * If there is deferred error attention, do not check for error
7986 * attention
7987 */
7988 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7989 spin_unlock_irq(&phba->hbalock);
7990 return 0;
7991 }
7992
3772a991
JS
7993 /* If PCI channel is offline, don't process it */
7994 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 7995 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7996 return 0;
7997 }
7998
7999 switch (phba->sli_rev) {
8000 case LPFC_SLI_REV2:
8001 case LPFC_SLI_REV3:
8002 /* Read chip Host Attention (HA) register */
8003 ha_copy = lpfc_sli_eratt_read(phba);
8004 break;
da0436e9
JS
8005 case LPFC_SLI_REV4:
8006 /* Read devcie Uncoverable Error (UERR) registers */
8007 ha_copy = lpfc_sli4_eratt_read(phba);
8008 break;
3772a991
JS
8009 default:
8010 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8011 "0299 Invalid SLI revision (%d)\n",
8012 phba->sli_rev);
8013 ha_copy = 0;
8014 break;
9399627f
JS
8015 }
8016 spin_unlock_irq(&phba->hbalock);
3772a991
JS
8017
8018 return ha_copy;
8019}
8020
8021/**
8022 * lpfc_intr_state_check - Check device state for interrupt handling
8023 * @phba: Pointer to HBA context.
8024 *
8025 * This inline routine checks whether a device or its PCI slot is in a state
8026 * that the interrupt should be handled.
8027 *
8028 * This function returns 0 if the device or the PCI slot is in a state that
8029 * interrupt should be handled, otherwise -EIO.
8030 */
8031static inline int
8032lpfc_intr_state_check(struct lpfc_hba *phba)
8033{
8034 /* If the pci channel is offline, ignore all the interrupts */
8035 if (unlikely(pci_channel_offline(phba->pcidev)))
8036 return -EIO;
8037
8038 /* Update device level interrupt statistics */
8039 phba->sli.slistat.sli_intr++;
8040
8041 /* Ignore all interrupts during initialization. */
8042 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8043 return -EIO;
8044
9399627f
JS
8045 return 0;
8046}
8047
8048/**
3772a991 8049 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
8050 * @irq: Interrupt number.
8051 * @dev_id: The device context pointer.
8052 *
9399627f 8053 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8054 * service routine when device with SLI-3 interface spec is enabled with
8055 * MSI-X multi-message interrupt mode and there are slow-path events in
8056 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8057 * interrupt mode, this function is called as part of the device-level
8058 * interrupt handler. When the PCI slot is in error recovery or the HBA
8059 * is undergoing initialization, the interrupt handler will not process
8060 * the interrupt. The link attention and ELS ring attention events are
8061 * handled by the worker thread. The interrupt handler signals the worker
8062 * thread and returns for these events. This function is called without
8063 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
8064 * structures.
8065 *
8066 * This function returns IRQ_HANDLED when interrupt is handled else it
8067 * returns IRQ_NONE.
e59058c4 8068 **/
dea3101e 8069irqreturn_t
3772a991 8070lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 8071{
2e0fef85 8072 struct lpfc_hba *phba;
a747c9ce 8073 uint32_t ha_copy, hc_copy;
dea3101e 8074 uint32_t work_ha_copy;
8075 unsigned long status;
5b75da2f 8076 unsigned long iflag;
dea3101e 8077 uint32_t control;
8078
92d7f7b0 8079 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
8080 struct lpfc_vport *vport;
8081 struct lpfc_nodelist *ndlp;
8082 struct lpfc_dmabuf *mp;
92d7f7b0
JS
8083 LPFC_MBOXQ_t *pmb;
8084 int rc;
8085
dea3101e 8086 /*
8087 * Get the driver's phba structure from the dev_id and
8088 * assume the HBA is not interrupting.
8089 */
9399627f 8090 phba = (struct lpfc_hba *)dev_id;
dea3101e 8091
8092 if (unlikely(!phba))
8093 return IRQ_NONE;
8094
dea3101e 8095 /*
9399627f
JS
8096 * Stuff needs to be attented to when this function is invoked as an
8097 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 8098 */
9399627f 8099 if (phba->intr_type == MSIX) {
3772a991
JS
8100 /* Check device state for handling interrupt */
8101 if (lpfc_intr_state_check(phba))
9399627f
JS
8102 return IRQ_NONE;
8103 /* Need to read HA REG for slow-path events */
5b75da2f 8104 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 8105 ha_copy = readl(phba->HAregaddr);
9399627f
JS
8106 /* If somebody is waiting to handle an eratt don't process it
8107 * here. The brdkill function will do this.
8108 */
8109 if (phba->link_flag & LS_IGNORE_ERATT)
8110 ha_copy &= ~HA_ERATT;
8111 /* Check the need for handling ERATT in interrupt handler */
8112 if (ha_copy & HA_ERATT) {
8113 if (phba->hba_flag & HBA_ERATT_HANDLED)
8114 /* ERATT polling has handled ERATT */
8115 ha_copy &= ~HA_ERATT;
8116 else
8117 /* Indicate interrupt handler handles ERATT */
8118 phba->hba_flag |= HBA_ERATT_HANDLED;
8119 }
a257bf90
JS
8120
8121 /*
8122 * If there is deferred error attention, do not check for any
8123 * interrupt.
8124 */
8125 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8126 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8127 return IRQ_NONE;
8128 }
8129
9399627f 8130 /* Clear up only attention source related to slow-path */
a747c9ce
JS
8131 hc_copy = readl(phba->HCregaddr);
8132 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8133 HC_LAINT_ENA | HC_ERINT_ENA),
8134 phba->HCregaddr);
9399627f
JS
8135 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8136 phba->HAregaddr);
a747c9ce 8137 writel(hc_copy, phba->HCregaddr);
9399627f 8138 readl(phba->HAregaddr); /* flush */
5b75da2f 8139 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8140 } else
8141 ha_copy = phba->ha_copy;
dea3101e 8142
dea3101e 8143 work_ha_copy = ha_copy & phba->work_ha_mask;
8144
9399627f 8145 if (work_ha_copy) {
dea3101e 8146 if (work_ha_copy & HA_LATT) {
8147 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8148 /*
8149 * Turn off Link Attention interrupts
8150 * until CLEAR_LA done
8151 */
5b75da2f 8152 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8153 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8154 control = readl(phba->HCregaddr);
8155 control &= ~HC_LAINT_ENA;
8156 writel(control, phba->HCregaddr);
8157 readl(phba->HCregaddr); /* flush */
5b75da2f 8158 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8159 }
8160 else
8161 work_ha_copy &= ~HA_LATT;
8162 }
8163
9399627f 8164 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
8165 /*
8166 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8167 * the only slow ring.
8168 */
8169 status = (work_ha_copy &
8170 (HA_RXMASK << (4*LPFC_ELS_RING)));
8171 status >>= (4*LPFC_ELS_RING);
8172 if (status & HA_RXMASK) {
5b75da2f 8173 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 8174 control = readl(phba->HCregaddr);
a58cbd52
JS
8175
8176 lpfc_debugfs_slow_ring_trc(phba,
8177 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8178 control, status,
8179 (uint32_t)phba->sli.slistat.sli_intr);
8180
858c9f6c 8181 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
8182 lpfc_debugfs_slow_ring_trc(phba,
8183 "ISR Disable ring:"
8184 "pwork:x%x hawork:x%x wait:x%x",
8185 phba->work_ha, work_ha_copy,
8186 (uint32_t)((unsigned long)
5e9d9b82 8187 &phba->work_waitq));
a58cbd52 8188
858c9f6c
JS
8189 control &=
8190 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 8191 writel(control, phba->HCregaddr);
8192 readl(phba->HCregaddr); /* flush */
dea3101e 8193 }
a58cbd52
JS
8194 else {
8195 lpfc_debugfs_slow_ring_trc(phba,
8196 "ISR slow ring: pwork:"
8197 "x%x hawork:x%x wait:x%x",
8198 phba->work_ha, work_ha_copy,
8199 (uint32_t)((unsigned long)
5e9d9b82 8200 &phba->work_waitq));
a58cbd52 8201 }
5b75da2f 8202 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8203 }
8204 }
5b75da2f 8205 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8206 if (work_ha_copy & HA_ERATT) {
9399627f 8207 lpfc_sli_read_hs(phba);
a257bf90
JS
8208 /*
8209 * Check if there is a deferred error condition
8210 * is active
8211 */
8212 if ((HS_FFER1 & phba->work_hs) &&
8213 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8214 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8215 phba->hba_flag |= DEFER_ERATT;
8216 /* Clear all interrupt enable conditions */
8217 writel(0, phba->HCregaddr);
8218 readl(phba->HCregaddr);
8219 }
8220 }
8221
9399627f 8222 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8223 pmb = phba->sli.mbox_active;
04c68496 8224 pmbox = &pmb->u.mb;
34b02dcd 8225 mbox = phba->mbox;
858c9f6c 8226 vport = pmb->vport;
92d7f7b0
JS
8227
8228 /* First check out the status word */
8229 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8230 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8231 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8232 /*
8233 * Stray Mailbox Interrupt, mbxCommand <cmd>
8234 * mbxStatus <status>
8235 */
09372820 8236 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8237 LOG_SLI,
e8b62011 8238 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8239 "Interrupt mbxCommand x%x "
8240 "mbxStatus x%x\n",
e8b62011 8241 (vport ? vport->vpi : 0),
92d7f7b0
JS
8242 pmbox->mbxCommand,
8243 pmbox->mbxStatus);
09372820
JS
8244 /* clear mailbox attention bit */
8245 work_ha_copy &= ~HA_MBATT;
8246 } else {
97eab634 8247 phba->sli.mbox_active = NULL;
5b75da2f 8248 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8249 phba->last_completion_time = jiffies;
8250 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8251 if (pmb->mbox_cmpl) {
8252 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8253 MAILBOX_CMD_SIZE);
7a470277
JS
8254 if (pmb->out_ext_byte_len &&
8255 pmb->context2)
8256 lpfc_sli_pcimem_bcopy(
8257 phba->mbox_ext,
8258 pmb->context2,
8259 pmb->out_ext_byte_len);
09372820
JS
8260 }
8261 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8262 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8263
8264 lpfc_debugfs_disc_trc(vport,
8265 LPFC_DISC_TRC_MBOX_VPORT,
8266 "MBOX dflt rpi: : "
8267 "status:x%x rpi:x%x",
8268 (uint32_t)pmbox->mbxStatus,
8269 pmbox->un.varWords[0], 0);
8270
8271 if (!pmbox->mbxStatus) {
8272 mp = (struct lpfc_dmabuf *)
8273 (pmb->context1);
8274 ndlp = (struct lpfc_nodelist *)
8275 pmb->context2;
8276
8277 /* Reg_LOGIN of dflt RPI was
8278 * successful. new lets get
8279 * rid of the RPI using the
8280 * same mbox buffer.
8281 */
8282 lpfc_unreg_login(phba,
8283 vport->vpi,
8284 pmbox->un.varWords[0],
8285 pmb);
8286 pmb->mbox_cmpl =
8287 lpfc_mbx_cmpl_dflt_rpi;
8288 pmb->context1 = mp;
8289 pmb->context2 = ndlp;
8290 pmb->vport = vport;
58da1ffb
JS
8291 rc = lpfc_sli_issue_mbox(phba,
8292 pmb,
8293 MBX_NOWAIT);
8294 if (rc != MBX_BUSY)
8295 lpfc_printf_log(phba,
8296 KERN_ERR,
8297 LOG_MBOX | LOG_SLI,
d7c255b2 8298 "0350 rc should have"
6a9c52cf 8299 "been MBX_BUSY\n");
3772a991
JS
8300 if (rc != MBX_NOT_FINISHED)
8301 goto send_current_mbox;
09372820 8302 }
858c9f6c 8303 }
5b75da2f
JS
8304 spin_lock_irqsave(
8305 &phba->pport->work_port_lock,
8306 iflag);
09372820
JS
8307 phba->pport->work_port_events &=
8308 ~WORKER_MBOX_TMO;
5b75da2f
JS
8309 spin_unlock_irqrestore(
8310 &phba->pport->work_port_lock,
8311 iflag);
09372820 8312 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8313 }
97eab634 8314 } else
5b75da2f 8315 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8316
92d7f7b0
JS
8317 if ((work_ha_copy & HA_MBATT) &&
8318 (phba->sli.mbox_active == NULL)) {
858c9f6c 8319send_current_mbox:
92d7f7b0 8320 /* Process next mailbox command if there is one */
58da1ffb
JS
8321 do {
8322 rc = lpfc_sli_issue_mbox(phba, NULL,
8323 MBX_NOWAIT);
8324 } while (rc == MBX_NOT_FINISHED);
8325 if (rc != MBX_SUCCESS)
8326 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8327 LOG_SLI, "0349 rc should be "
6a9c52cf 8328 "MBX_SUCCESS\n");
92d7f7b0
JS
8329 }
8330
5b75da2f 8331 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8332 phba->work_ha |= work_ha_copy;
5b75da2f 8333 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8334 lpfc_worker_wake_up(phba);
dea3101e 8335 }
9399627f 8336 return IRQ_HANDLED;
dea3101e 8337
3772a991 8338} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8339
8340/**
3772a991 8341 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8342 * @irq: Interrupt number.
8343 * @dev_id: The device context pointer.
8344 *
8345 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8346 * service routine when device with SLI-3 interface spec is enabled with
8347 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8348 * ring event in the HBA. However, when the device is enabled with either
8349 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8350 * device-level interrupt handler. When the PCI slot is in error recovery
8351 * or the HBA is undergoing initialization, the interrupt handler will not
8352 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8353 * the intrrupt context. This function is called without any lock held.
8354 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8355 *
8356 * This function returns IRQ_HANDLED when interrupt is handled else it
8357 * returns IRQ_NONE.
8358 **/
8359irqreturn_t
3772a991 8360lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8361{
8362 struct lpfc_hba *phba;
8363 uint32_t ha_copy;
8364 unsigned long status;
5b75da2f 8365 unsigned long iflag;
9399627f
JS
8366
8367 /* Get the driver's phba structure from the dev_id and
8368 * assume the HBA is not interrupting.
8369 */
8370 phba = (struct lpfc_hba *) dev_id;
8371
8372 if (unlikely(!phba))
8373 return IRQ_NONE;
8374
8375 /*
8376 * Stuff needs to be attented to when this function is invoked as an
8377 * individual interrupt handler in MSI-X multi-message interrupt mode
8378 */
8379 if (phba->intr_type == MSIX) {
3772a991
JS
8380 /* Check device state for handling interrupt */
8381 if (lpfc_intr_state_check(phba))
9399627f
JS
8382 return IRQ_NONE;
8383 /* Need to read HA REG for FCP ring and other ring events */
8384 ha_copy = readl(phba->HAregaddr);
8385 /* Clear up only attention source related to fast-path */
5b75da2f 8386 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8387 /*
8388 * If there is deferred error attention, do not check for
8389 * any interrupt.
8390 */
8391 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8392 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8393 return IRQ_NONE;
8394 }
9399627f
JS
8395 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8396 phba->HAregaddr);
8397 readl(phba->HAregaddr); /* flush */
5b75da2f 8398 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8399 } else
8400 ha_copy = phba->ha_copy;
dea3101e 8401
8402 /*
9399627f 8403 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8404 */
9399627f
JS
8405 ha_copy &= ~(phba->work_ha_mask);
8406
8407 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8408 status >>= (4*LPFC_FCP_RING);
858c9f6c 8409 if (status & HA_RXMASK)
dea3101e 8410 lpfc_sli_handle_fast_ring_event(phba,
8411 &phba->sli.ring[LPFC_FCP_RING],
8412 status);
a4bc3379
JS
8413
8414 if (phba->cfg_multi_ring_support == 2) {
8415 /*
9399627f
JS
8416 * Process all events on extra ring. Take the optimized path
8417 * for extra ring IO.
a4bc3379 8418 */
9399627f 8419 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8420 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8421 if (status & HA_RXMASK) {
a4bc3379
JS
8422 lpfc_sli_handle_fast_ring_event(phba,
8423 &phba->sli.ring[LPFC_EXTRA_RING],
8424 status);
8425 }
8426 }
dea3101e 8427 return IRQ_HANDLED;
3772a991 8428} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8429
8430/**
3772a991 8431 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8432 * @irq: Interrupt number.
8433 * @dev_id: The device context pointer.
8434 *
3772a991
JS
8435 * This function is the HBA device-level interrupt handler to device with
8436 * SLI-3 interface spec, called from the PCI layer when either MSI or
8437 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8438 * requires driver attention. This function invokes the slow-path interrupt
8439 * attention handling function and fast-path interrupt attention handling
8440 * function in turn to process the relevant HBA attention events. This
8441 * function is called without any lock held. It gets the hbalock to access
8442 * and update SLI data structures.
9399627f
JS
8443 *
8444 * This function returns IRQ_HANDLED when interrupt is handled, else it
8445 * returns IRQ_NONE.
8446 **/
8447irqreturn_t
3772a991 8448lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8449{
8450 struct lpfc_hba *phba;
8451 irqreturn_t sp_irq_rc, fp_irq_rc;
8452 unsigned long status1, status2;
a747c9ce 8453 uint32_t hc_copy;
9399627f
JS
8454
8455 /*
8456 * Get the driver's phba structure from the dev_id and
8457 * assume the HBA is not interrupting.
8458 */
8459 phba = (struct lpfc_hba *) dev_id;
8460
8461 if (unlikely(!phba))
8462 return IRQ_NONE;
8463
3772a991
JS
8464 /* Check device state for handling interrupt */
8465 if (lpfc_intr_state_check(phba))
9399627f
JS
8466 return IRQ_NONE;
8467
8468 spin_lock(&phba->hbalock);
8469 phba->ha_copy = readl(phba->HAregaddr);
8470 if (unlikely(!phba->ha_copy)) {
8471 spin_unlock(&phba->hbalock);
8472 return IRQ_NONE;
8473 } else if (phba->ha_copy & HA_ERATT) {
8474 if (phba->hba_flag & HBA_ERATT_HANDLED)
8475 /* ERATT polling has handled ERATT */
8476 phba->ha_copy &= ~HA_ERATT;
8477 else
8478 /* Indicate interrupt handler handles ERATT */
8479 phba->hba_flag |= HBA_ERATT_HANDLED;
8480 }
8481
a257bf90
JS
8482 /*
8483 * If there is deferred error attention, do not check for any interrupt.
8484 */
8485 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 8486 spin_unlock(&phba->hbalock);
a257bf90
JS
8487 return IRQ_NONE;
8488 }
8489
9399627f 8490 /* Clear attention sources except link and error attentions */
a747c9ce
JS
8491 hc_copy = readl(phba->HCregaddr);
8492 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8493 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8494 phba->HCregaddr);
9399627f 8495 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 8496 writel(hc_copy, phba->HCregaddr);
9399627f
JS
8497 readl(phba->HAregaddr); /* flush */
8498 spin_unlock(&phba->hbalock);
8499
8500 /*
8501 * Invokes slow-path host attention interrupt handling as appropriate.
8502 */
8503
8504 /* status of events with mailbox and link attention */
8505 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8506
8507 /* status of events with ELS ring */
8508 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8509 status2 >>= (4*LPFC_ELS_RING);
8510
8511 if (status1 || (status2 & HA_RXMASK))
3772a991 8512 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8513 else
8514 sp_irq_rc = IRQ_NONE;
8515
8516 /*
8517 * Invoke fast-path host attention interrupt handling as appropriate.
8518 */
8519
8520 /* status of events with FCP ring */
8521 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8522 status1 >>= (4*LPFC_FCP_RING);
8523
8524 /* status of events with extra ring */
8525 if (phba->cfg_multi_ring_support == 2) {
8526 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8527 status2 >>= (4*LPFC_EXTRA_RING);
8528 } else
8529 status2 = 0;
8530
8531 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8532 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8533 else
8534 fp_irq_rc = IRQ_NONE;
dea3101e 8535
9399627f
JS
8536 /* Return device-level interrupt handling status */
8537 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8538} /* lpfc_sli_intr_handler */
4f774513
JS
8539
8540/**
8541 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8542 * @phba: pointer to lpfc hba data structure.
8543 *
8544 * This routine is invoked by the worker thread to process all the pending
8545 * SLI4 FCP abort XRI events.
8546 **/
8547void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8548{
8549 struct lpfc_cq_event *cq_event;
8550
8551 /* First, declare the fcp xri abort event has been handled */
8552 spin_lock_irq(&phba->hbalock);
8553 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8554 spin_unlock_irq(&phba->hbalock);
8555 /* Now, handle all the fcp xri abort events */
8556 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8557 /* Get the first event from the head of the event queue */
8558 spin_lock_irq(&phba->hbalock);
8559 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8560 cq_event, struct lpfc_cq_event, list);
8561 spin_unlock_irq(&phba->hbalock);
8562 /* Notify aborted XRI for FCP work queue */
8563 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8564 /* Free the event processed back to the free pool */
8565 lpfc_sli4_cq_event_release(phba, cq_event);
8566 }
8567}
8568
8569/**
8570 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8571 * @phba: pointer to lpfc hba data structure.
8572 *
8573 * This routine is invoked by the worker thread to process all the pending
8574 * SLI4 els abort xri events.
8575 **/
8576void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8577{
8578 struct lpfc_cq_event *cq_event;
8579
8580 /* First, declare the els xri abort event has been handled */
8581 spin_lock_irq(&phba->hbalock);
8582 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8583 spin_unlock_irq(&phba->hbalock);
8584 /* Now, handle all the els xri abort events */
8585 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8586 /* Get the first event from the head of the event queue */
8587 spin_lock_irq(&phba->hbalock);
8588 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8589 cq_event, struct lpfc_cq_event, list);
8590 spin_unlock_irq(&phba->hbalock);
8591 /* Notify aborted XRI for ELS work queue */
8592 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8593 /* Free the event processed back to the free pool */
8594 lpfc_sli4_cq_event_release(phba, cq_event);
8595 }
8596}
8597
341af102
JS
8598/**
8599 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8600 * @phba: pointer to lpfc hba data structure
8601 * @pIocbIn: pointer to the rspiocbq
8602 * @pIocbOut: pointer to the cmdiocbq
8603 * @wcqe: pointer to the complete wcqe
8604 *
8605 * This routine transfers the fields of a command iocbq to a response iocbq
8606 * by copying all the IOCB fields from command iocbq and transferring the
8607 * completion status information from the complete wcqe.
8608 **/
4f774513 8609static void
341af102
JS
8610lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8611 struct lpfc_iocbq *pIocbIn,
4f774513
JS
8612 struct lpfc_iocbq *pIocbOut,
8613 struct lpfc_wcqe_complete *wcqe)
8614{
341af102 8615 unsigned long iflags;
4f774513
JS
8616 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8617
8618 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8619 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
8620 /* Map WCQE parameters into irspiocb parameters */
8621 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8622 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8623 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8624 pIocbIn->iocb.un.fcpi.fcpi_parm =
8625 pIocbOut->iocb.un.fcpi.fcpi_parm -
8626 wcqe->total_data_placed;
8627 else
8628 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 8629 else {
4f774513 8630 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
8631 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8632 }
341af102
JS
8633
8634 /* Pick up HBA exchange busy condition */
8635 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8636 spin_lock_irqsave(&phba->hbalock, iflags);
8637 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8638 spin_unlock_irqrestore(&phba->hbalock, iflags);
8639 }
4f774513
JS
8640}
8641
45ed1190
JS
8642/**
8643 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8644 * @phba: Pointer to HBA context object.
8645 * @wcqe: Pointer to work-queue completion queue entry.
8646 *
8647 * This routine handles an ELS work-queue completion event and construct
8648 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8649 * discovery engine to handle.
8650 *
8651 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8652 **/
8653static struct lpfc_iocbq *
8654lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8655 struct lpfc_iocbq *irspiocbq)
8656{
8657 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8658 struct lpfc_iocbq *cmdiocbq;
8659 struct lpfc_wcqe_complete *wcqe;
8660 unsigned long iflags;
8661
8662 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8663 spin_lock_irqsave(&phba->hbalock, iflags);
8664 pring->stats.iocb_event++;
8665 /* Look up the ELS command IOCB and create pseudo response IOCB */
8666 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8667 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8668 spin_unlock_irqrestore(&phba->hbalock, iflags);
8669
8670 if (unlikely(!cmdiocbq)) {
8671 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8672 "0386 ELS complete with no corresponding "
8673 "cmdiocb: iotag (%d)\n",
8674 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8675 lpfc_sli_release_iocbq(phba, irspiocbq);
8676 return NULL;
8677 }
8678
8679 /* Fake the irspiocbq and copy necessary response information */
341af102 8680 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
8681
8682 return irspiocbq;
8683}
8684
04c68496
JS
8685/**
8686 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8687 * @phba: Pointer to HBA context object.
8688 * @cqe: Pointer to mailbox completion queue entry.
8689 *
8690 * This routine process a mailbox completion queue entry with asynchrous
8691 * event.
8692 *
8693 * Return: true if work posted to worker thread, otherwise false.
8694 **/
8695static bool
8696lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8697{
8698 struct lpfc_cq_event *cq_event;
8699 unsigned long iflags;
8700
8701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8702 "0392 Async Event: word0:x%x, word1:x%x, "
8703 "word2:x%x, word3:x%x\n", mcqe->word0,
8704 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8705
8706 /* Allocate a new internal CQ_EVENT entry */
8707 cq_event = lpfc_sli4_cq_event_alloc(phba);
8708 if (!cq_event) {
8709 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8710 "0394 Failed to allocate CQ_EVENT entry\n");
8711 return false;
8712 }
8713
8714 /* Move the CQE into an asynchronous event entry */
8715 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8716 spin_lock_irqsave(&phba->hbalock, iflags);
8717 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8718 /* Set the async event flag */
8719 phba->hba_flag |= ASYNC_EVENT;
8720 spin_unlock_irqrestore(&phba->hbalock, iflags);
8721
8722 return true;
8723}
8724
8725/**
8726 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8727 * @phba: Pointer to HBA context object.
8728 * @cqe: Pointer to mailbox completion queue entry.
8729 *
8730 * This routine process a mailbox completion queue entry with mailbox
8731 * completion event.
8732 *
8733 * Return: true if work posted to worker thread, otherwise false.
8734 **/
8735static bool
8736lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8737{
8738 uint32_t mcqe_status;
8739 MAILBOX_t *mbox, *pmbox;
8740 struct lpfc_mqe *mqe;
8741 struct lpfc_vport *vport;
8742 struct lpfc_nodelist *ndlp;
8743 struct lpfc_dmabuf *mp;
8744 unsigned long iflags;
8745 LPFC_MBOXQ_t *pmb;
8746 bool workposted = false;
8747 int rc;
8748
8749 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8750 if (!bf_get(lpfc_trailer_completed, mcqe))
8751 goto out_no_mqe_complete;
8752
8753 /* Get the reference to the active mbox command */
8754 spin_lock_irqsave(&phba->hbalock, iflags);
8755 pmb = phba->sli.mbox_active;
8756 if (unlikely(!pmb)) {
8757 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8758 "1832 No pending MBOX command to handle\n");
8759 spin_unlock_irqrestore(&phba->hbalock, iflags);
8760 goto out_no_mqe_complete;
8761 }
8762 spin_unlock_irqrestore(&phba->hbalock, iflags);
8763 mqe = &pmb->u.mqe;
8764 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8765 mbox = phba->mbox;
8766 vport = pmb->vport;
8767
8768 /* Reset heartbeat timer */
8769 phba->last_completion_time = jiffies;
8770 del_timer(&phba->sli.mbox_tmo);
8771
8772 /* Move mbox data to caller's mailbox region, do endian swapping */
8773 if (pmb->mbox_cmpl && mbox)
8774 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8775 /* Set the mailbox status with SLI4 range 0x4000 */
8776 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8777 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8778 bf_set(lpfc_mqe_status, mqe,
8779 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8780
8781 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8782 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8783 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8784 "MBOX dflt rpi: status:x%x rpi:x%x",
8785 mcqe_status,
8786 pmbox->un.varWords[0], 0);
8787 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8788 mp = (struct lpfc_dmabuf *)(pmb->context1);
8789 ndlp = (struct lpfc_nodelist *)pmb->context2;
8790 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8791 * RID of the PPI using the same mbox buffer.
8792 */
8793 lpfc_unreg_login(phba, vport->vpi,
8794 pmbox->un.varWords[0], pmb);
8795 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8796 pmb->context1 = mp;
8797 pmb->context2 = ndlp;
8798 pmb->vport = vport;
8799 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8800 if (rc != MBX_BUSY)
8801 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8802 LOG_SLI, "0385 rc should "
8803 "have been MBX_BUSY\n");
8804 if (rc != MBX_NOT_FINISHED)
8805 goto send_current_mbox;
8806 }
8807 }
8808 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8809 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8810 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8811
8812 /* There is mailbox completion work to do */
8813 spin_lock_irqsave(&phba->hbalock, iflags);
8814 __lpfc_mbox_cmpl_put(phba, pmb);
8815 phba->work_ha |= HA_MBATT;
8816 spin_unlock_irqrestore(&phba->hbalock, iflags);
8817 workposted = true;
8818
8819send_current_mbox:
8820 spin_lock_irqsave(&phba->hbalock, iflags);
8821 /* Release the mailbox command posting token */
8822 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8823 /* Setting active mailbox pointer need to be in sync to flag clear */
8824 phba->sli.mbox_active = NULL;
8825 spin_unlock_irqrestore(&phba->hbalock, iflags);
8826 /* Wake up worker thread to post the next pending mailbox command */
8827 lpfc_worker_wake_up(phba);
8828out_no_mqe_complete:
8829 if (bf_get(lpfc_trailer_consumed, mcqe))
8830 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8831 return workposted;
8832}
8833
8834/**
8835 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8836 * @phba: Pointer to HBA context object.
8837 * @cqe: Pointer to mailbox completion queue entry.
8838 *
8839 * This routine process a mailbox completion queue entry, it invokes the
8840 * proper mailbox complete handling or asynchrous event handling routine
8841 * according to the MCQE's async bit.
8842 *
8843 * Return: true if work posted to worker thread, otherwise false.
8844 **/
8845static bool
8846lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8847{
8848 struct lpfc_mcqe mcqe;
8849 bool workposted;
8850
8851 /* Copy the mailbox MCQE and convert endian order as needed */
8852 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8853
8854 /* Invoke the proper event handling routine */
8855 if (!bf_get(lpfc_trailer_async, &mcqe))
8856 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8857 else
8858 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8859 return workposted;
8860}
8861
4f774513
JS
8862/**
8863 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8864 * @phba: Pointer to HBA context object.
8865 * @wcqe: Pointer to work-queue completion queue entry.
8866 *
8867 * This routine handles an ELS work-queue completion event.
8868 *
8869 * Return: true if work posted to worker thread, otherwise false.
8870 **/
8871static bool
8872lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8873 struct lpfc_wcqe_complete *wcqe)
8874{
4f774513
JS
8875 struct lpfc_iocbq *irspiocbq;
8876 unsigned long iflags;
2a9bf3d0 8877 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
4f774513 8878
45ed1190 8879 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
8880 irspiocbq = lpfc_sli_get_iocbq(phba);
8881 if (!irspiocbq) {
8882 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
8883 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
8884 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
8885 pring->txq_cnt, phba->iocb_cnt,
8886 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
8887 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
45ed1190 8888 return false;
4f774513 8889 }
4f774513 8890
45ed1190
JS
8891 /* Save off the slow-path queue event for work thread to process */
8892 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 8893 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 8894 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
8895 &phba->sli4_hba.sp_queue_event);
8896 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 8897 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 8898
45ed1190 8899 return true;
4f774513
JS
8900}
8901
8902/**
8903 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8904 * @phba: Pointer to HBA context object.
8905 * @wcqe: Pointer to work-queue completion queue entry.
8906 *
8907 * This routine handles slow-path WQ entry comsumed event by invoking the
8908 * proper WQ release routine to the slow-path WQ.
8909 **/
8910static void
8911lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8912 struct lpfc_wcqe_release *wcqe)
8913{
8914 /* Check for the slow-path ELS work queue */
8915 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8916 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8917 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8918 else
8919 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8920 "2579 Slow-path wqe consume event carries "
8921 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8922 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8923 phba->sli4_hba.els_wq->queue_id);
8924}
8925
8926/**
8927 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8928 * @phba: Pointer to HBA context object.
8929 * @cq: Pointer to a WQ completion queue.
8930 * @wcqe: Pointer to work-queue completion queue entry.
8931 *
8932 * This routine handles an XRI abort event.
8933 *
8934 * Return: true if work posted to worker thread, otherwise false.
8935 **/
8936static bool
8937lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8938 struct lpfc_queue *cq,
8939 struct sli4_wcqe_xri_aborted *wcqe)
8940{
8941 bool workposted = false;
8942 struct lpfc_cq_event *cq_event;
8943 unsigned long iflags;
8944
8945 /* Allocate a new internal CQ_EVENT entry */
8946 cq_event = lpfc_sli4_cq_event_alloc(phba);
8947 if (!cq_event) {
8948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8949 "0602 Failed to allocate CQ_EVENT entry\n");
8950 return false;
8951 }
8952
8953 /* Move the CQE into the proper xri abort event list */
8954 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8955 switch (cq->subtype) {
8956 case LPFC_FCP:
8957 spin_lock_irqsave(&phba->hbalock, iflags);
8958 list_add_tail(&cq_event->list,
8959 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8960 /* Set the fcp xri abort event flag */
8961 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8962 spin_unlock_irqrestore(&phba->hbalock, iflags);
8963 workposted = true;
8964 break;
8965 case LPFC_ELS:
8966 spin_lock_irqsave(&phba->hbalock, iflags);
8967 list_add_tail(&cq_event->list,
8968 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8969 /* Set the els xri abort event flag */
8970 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8971 spin_unlock_irqrestore(&phba->hbalock, iflags);
8972 workposted = true;
8973 break;
8974 default:
8975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8976 "0603 Invalid work queue CQE subtype (x%x)\n",
8977 cq->subtype);
8978 workposted = false;
8979 break;
8980 }
8981 return workposted;
8982}
8983
4f774513
JS
8984/**
8985 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8986 * @phba: Pointer to HBA context object.
8987 * @rcqe: Pointer to receive-queue completion queue entry.
8988 *
8989 * This routine process a receive-queue completion queue entry.
8990 *
8991 * Return: true if work posted to worker thread, otherwise false.
8992 **/
8993static bool
4d9ab994 8994lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 8995{
4f774513
JS
8996 bool workposted = false;
8997 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8998 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8999 struct hbq_dmabuf *dma_buf;
9000 uint32_t status;
9001 unsigned long iflags;
9002
4d9ab994 9003 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
9004 goto out;
9005
4d9ab994 9006 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
9007 switch (status) {
9008 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9010 "2537 Receive Frame Truncated!!\n");
9011 case FC_STATUS_RQ_SUCCESS:
5ffc266e 9012 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
9013 spin_lock_irqsave(&phba->hbalock, iflags);
9014 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9015 if (!dma_buf) {
9016 spin_unlock_irqrestore(&phba->hbalock, iflags);
9017 goto out;
9018 }
4d9ab994 9019 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 9020 /* save off the frame for the word thread to process */
4d9ab994 9021 list_add_tail(&dma_buf->cq_event.list,
45ed1190 9022 &phba->sli4_hba.sp_queue_event);
4f774513 9023 /* Frame received */
45ed1190 9024 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
9025 spin_unlock_irqrestore(&phba->hbalock, iflags);
9026 workposted = true;
9027 break;
9028 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9029 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9030 /* Post more buffers if possible */
9031 spin_lock_irqsave(&phba->hbalock, iflags);
9032 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9033 spin_unlock_irqrestore(&phba->hbalock, iflags);
9034 workposted = true;
9035 break;
9036 }
9037out:
9038 return workposted;
4f774513
JS
9039}
9040
4d9ab994
JS
9041/**
9042 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9043 * @phba: Pointer to HBA context object.
9044 * @cq: Pointer to the completion queue.
9045 * @wcqe: Pointer to a completion queue entry.
9046 *
9047 * This routine process a slow-path work-queue or recieve queue completion queue
9048 * entry.
9049 *
9050 * Return: true if work posted to worker thread, otherwise false.
9051 **/
9052static bool
9053lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9054 struct lpfc_cqe *cqe)
9055{
45ed1190 9056 struct lpfc_cqe cqevt;
4d9ab994
JS
9057 bool workposted = false;
9058
9059 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 9060 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
9061
9062 /* Check and process for different type of WCQE and dispatch */
45ed1190 9063 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 9064 case CQE_CODE_COMPL_WQE:
45ed1190 9065 /* Process the WQ/RQ complete event */
bc73905a 9066 phba->last_completion_time = jiffies;
4d9ab994 9067 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 9068 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
9069 break;
9070 case CQE_CODE_RELEASE_WQE:
9071 /* Process the WQ release event */
9072 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 9073 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
9074 break;
9075 case CQE_CODE_XRI_ABORTED:
9076 /* Process the WQ XRI abort event */
bc73905a 9077 phba->last_completion_time = jiffies;
4d9ab994 9078 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 9079 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
9080 break;
9081 case CQE_CODE_RECEIVE:
9082 /* Process the RQ event */
bc73905a 9083 phba->last_completion_time = jiffies;
4d9ab994 9084 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 9085 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
9086 break;
9087 default:
9088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9089 "0388 Not a valid WCQE code: x%x\n",
45ed1190 9090 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
9091 break;
9092 }
9093 return workposted;
9094}
9095
4f774513
JS
9096/**
9097 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9098 * @phba: Pointer to HBA context object.
9099 * @eqe: Pointer to fast-path event queue entry.
9100 *
9101 * This routine process a event queue entry from the slow-path event queue.
9102 * It will check the MajorCode and MinorCode to determine this is for a
9103 * completion event on a completion queue, if not, an error shall be logged
9104 * and just return. Otherwise, it will get to the corresponding completion
9105 * queue and process all the entries on that completion queue, rearm the
9106 * completion queue, and then return.
9107 *
9108 **/
9109static void
9110lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9111{
9112 struct lpfc_queue *cq = NULL, *childq, *speq;
9113 struct lpfc_cqe *cqe;
9114 bool workposted = false;
9115 int ecount = 0;
9116 uint16_t cqid;
9117
cb5172ea 9118 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
9119 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9120 "0359 Not a valid slow-path completion "
9121 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9122 bf_get_le32(lpfc_eqe_major_code, eqe),
9123 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9124 return;
9125 }
9126
9127 /* Get the reference to the corresponding CQ */
cb5172ea 9128 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9129
9130 /* Search for completion queue pointer matching this cqid */
9131 speq = phba->sli4_hba.sp_eq;
9132 list_for_each_entry(childq, &speq->child_list, list) {
9133 if (childq->queue_id == cqid) {
9134 cq = childq;
9135 break;
9136 }
9137 }
9138 if (unlikely(!cq)) {
75baf696
JS
9139 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9141 "0365 Slow-path CQ identifier "
9142 "(%d) does not exist\n", cqid);
4f774513
JS
9143 return;
9144 }
9145
9146 /* Process all the entries to the CQ */
9147 switch (cq->type) {
9148 case LPFC_MCQ:
9149 while ((cqe = lpfc_sli4_cq_get(cq))) {
9150 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9151 if (!(++ecount % LPFC_GET_QE_REL_INT))
9152 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9153 }
9154 break;
9155 case LPFC_WCQ:
9156 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 9157 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
9158 if (!(++ecount % LPFC_GET_QE_REL_INT))
9159 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9160 }
9161 break;
9162 default:
9163 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9164 "0370 Invalid completion queue type (%d)\n",
9165 cq->type);
9166 return;
9167 }
9168
9169 /* Catch the no cq entry condition, log an error */
9170 if (unlikely(ecount == 0))
9171 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9172 "0371 No entry from the CQ: identifier "
9173 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9174
9175 /* In any case, flash and re-arm the RCQ */
9176 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9177
9178 /* wake up worker thread if there are works to be done */
9179 if (workposted)
9180 lpfc_worker_wake_up(phba);
9181}
9182
9183/**
9184 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9185 * @eqe: Pointer to fast-path completion queue entry.
9186 *
9187 * This routine process a fast-path work queue completion entry from fast-path
9188 * event queue for FCP command response completion.
9189 **/
9190static void
9191lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9192 struct lpfc_wcqe_complete *wcqe)
9193{
9194 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9195 struct lpfc_iocbq *cmdiocbq;
9196 struct lpfc_iocbq irspiocbq;
9197 unsigned long iflags;
9198
9199 spin_lock_irqsave(&phba->hbalock, iflags);
9200 pring->stats.iocb_event++;
9201 spin_unlock_irqrestore(&phba->hbalock, iflags);
9202
9203 /* Check for response status */
9204 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9205 /* If resource errors reported from HBA, reduce queue
9206 * depth of the SCSI device.
9207 */
9208 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9209 IOSTAT_LOCAL_REJECT) &&
9210 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9211 phba->lpfc_rampdown_queue_depth(phba);
9212 }
9213 /* Log the error status */
9214 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9215 "0373 FCP complete error: status=x%x, "
9216 "hw_status=x%x, total_data_specified=%d, "
9217 "parameter=x%x, word3=x%x\n",
9218 bf_get(lpfc_wcqe_c_status, wcqe),
9219 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9220 wcqe->total_data_placed, wcqe->parameter,
9221 wcqe->word3);
9222 }
9223
9224 /* Look up the FCP command IOCB and create pseudo response IOCB */
9225 spin_lock_irqsave(&phba->hbalock, iflags);
9226 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9227 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9228 spin_unlock_irqrestore(&phba->hbalock, iflags);
9229 if (unlikely(!cmdiocbq)) {
9230 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9231 "0374 FCP complete with no corresponding "
9232 "cmdiocb: iotag (%d)\n",
9233 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9234 return;
9235 }
9236 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9237 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9238 "0375 FCP cmdiocb not callback function "
9239 "iotag: (%d)\n",
9240 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9241 return;
9242 }
9243
9244 /* Fake the irspiocb and copy necessary response information */
341af102 9245 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 9246
0f65ff68
JS
9247 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9248 spin_lock_irqsave(&phba->hbalock, iflags);
9249 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9250 spin_unlock_irqrestore(&phba->hbalock, iflags);
9251 }
9252
4f774513
JS
9253 /* Pass the cmd_iocb and the rsp state to the upper layer */
9254 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9255}
9256
9257/**
9258 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9259 * @phba: Pointer to HBA context object.
9260 * @cq: Pointer to completion queue.
9261 * @wcqe: Pointer to work-queue completion queue entry.
9262 *
9263 * This routine handles an fast-path WQ entry comsumed event by invoking the
9264 * proper WQ release routine to the slow-path WQ.
9265 **/
9266static void
9267lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9268 struct lpfc_wcqe_release *wcqe)
9269{
9270 struct lpfc_queue *childwq;
9271 bool wqid_matched = false;
9272 uint16_t fcp_wqid;
9273
9274 /* Check for fast-path FCP work queue release */
9275 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9276 list_for_each_entry(childwq, &cq->child_list, list) {
9277 if (childwq->queue_id == fcp_wqid) {
9278 lpfc_sli4_wq_release(childwq,
9279 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9280 wqid_matched = true;
9281 break;
9282 }
9283 }
9284 /* Report warning log message if no match found */
9285 if (wqid_matched != true)
9286 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9287 "2580 Fast-path wqe consume event carries "
9288 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9289}
9290
9291/**
9292 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9293 * @cq: Pointer to the completion queue.
9294 * @eqe: Pointer to fast-path completion queue entry.
9295 *
9296 * This routine process a fast-path work queue completion entry from fast-path
9297 * event queue for FCP command response completion.
9298 **/
9299static int
9300lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9301 struct lpfc_cqe *cqe)
9302{
9303 struct lpfc_wcqe_release wcqe;
9304 bool workposted = false;
9305
9306 /* Copy the work queue CQE and convert endian order if needed */
9307 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9308
9309 /* Check and process for different type of WCQE and dispatch */
9310 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9311 case CQE_CODE_COMPL_WQE:
9312 /* Process the WQ complete event */
98fc5dd9 9313 phba->last_completion_time = jiffies;
4f774513
JS
9314 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9315 (struct lpfc_wcqe_complete *)&wcqe);
9316 break;
9317 case CQE_CODE_RELEASE_WQE:
9318 /* Process the WQ release event */
9319 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9320 (struct lpfc_wcqe_release *)&wcqe);
9321 break;
9322 case CQE_CODE_XRI_ABORTED:
9323 /* Process the WQ XRI abort event */
bc73905a 9324 phba->last_completion_time = jiffies;
4f774513
JS
9325 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9326 (struct sli4_wcqe_xri_aborted *)&wcqe);
9327 break;
9328 default:
9329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9330 "0144 Not a valid WCQE code: x%x\n",
9331 bf_get(lpfc_wcqe_c_code, &wcqe));
9332 break;
9333 }
9334 return workposted;
9335}
9336
9337/**
9338 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9339 * @phba: Pointer to HBA context object.
9340 * @eqe: Pointer to fast-path event queue entry.
9341 *
9342 * This routine process a event queue entry from the fast-path event queue.
9343 * It will check the MajorCode and MinorCode to determine this is for a
9344 * completion event on a completion queue, if not, an error shall be logged
9345 * and just return. Otherwise, it will get to the corresponding completion
9346 * queue and process all the entries on the completion queue, rearm the
9347 * completion queue, and then return.
9348 **/
9349static void
9350lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9351 uint32_t fcp_cqidx)
9352{
9353 struct lpfc_queue *cq;
9354 struct lpfc_cqe *cqe;
9355 bool workposted = false;
9356 uint16_t cqid;
9357 int ecount = 0;
9358
cb5172ea 9359 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
9360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9361 "0366 Not a valid fast-path completion "
9362 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9363 bf_get_le32(lpfc_eqe_major_code, eqe),
9364 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9365 return;
9366 }
9367
9368 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9369 if (unlikely(!cq)) {
75baf696
JS
9370 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9371 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9372 "0367 Fast-path completion queue "
9373 "does not exist\n");
4f774513
JS
9374 return;
9375 }
9376
9377 /* Get the reference to the corresponding CQ */
cb5172ea 9378 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9379 if (unlikely(cqid != cq->queue_id)) {
9380 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9381 "0368 Miss-matched fast-path completion "
9382 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9383 cqid, cq->queue_id);
9384 return;
9385 }
9386
9387 /* Process all the entries to the CQ */
9388 while ((cqe = lpfc_sli4_cq_get(cq))) {
9389 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9390 if (!(++ecount % LPFC_GET_QE_REL_INT))
9391 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9392 }
9393
9394 /* Catch the no cq entry condition */
9395 if (unlikely(ecount == 0))
9396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9397 "0369 No entry from fast-path completion "
9398 "queue fcpcqid=%d\n", cq->queue_id);
9399
9400 /* In any case, flash and re-arm the CQ */
9401 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9402
9403 /* wake up worker thread if there are works to be done */
9404 if (workposted)
9405 lpfc_worker_wake_up(phba);
9406}
9407
9408static void
9409lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9410{
9411 struct lpfc_eqe *eqe;
9412
9413 /* walk all the EQ entries and drop on the floor */
9414 while ((eqe = lpfc_sli4_eq_get(eq)))
9415 ;
9416
9417 /* Clear and re-arm the EQ */
9418 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9419}
9420
9421/**
9422 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9423 * @irq: Interrupt number.
9424 * @dev_id: The device context pointer.
9425 *
9426 * This function is directly called from the PCI layer as an interrupt
9427 * service routine when device with SLI-4 interface spec is enabled with
9428 * MSI-X multi-message interrupt mode and there are slow-path events in
9429 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9430 * interrupt mode, this function is called as part of the device-level
9431 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9432 * undergoing initialization, the interrupt handler will not process the
9433 * interrupt. The link attention and ELS ring attention events are handled
9434 * by the worker thread. The interrupt handler signals the worker thread
9435 * and returns for these events. This function is called without any lock
9436 * held. It gets the hbalock to access and update SLI data structures.
9437 *
9438 * This function returns IRQ_HANDLED when interrupt is handled else it
9439 * returns IRQ_NONE.
9440 **/
9441irqreturn_t
9442lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9443{
9444 struct lpfc_hba *phba;
9445 struct lpfc_queue *speq;
9446 struct lpfc_eqe *eqe;
9447 unsigned long iflag;
9448 int ecount = 0;
9449
9450 /*
9451 * Get the driver's phba structure from the dev_id
9452 */
9453 phba = (struct lpfc_hba *)dev_id;
9454
9455 if (unlikely(!phba))
9456 return IRQ_NONE;
9457
9458 /* Get to the EQ struct associated with this vector */
9459 speq = phba->sli4_hba.sp_eq;
9460
9461 /* Check device state for handling interrupt */
9462 if (unlikely(lpfc_intr_state_check(phba))) {
9463 /* Check again for link_state with lock held */
9464 spin_lock_irqsave(&phba->hbalock, iflag);
9465 if (phba->link_state < LPFC_LINK_DOWN)
9466 /* Flush, clear interrupt, and rearm the EQ */
9467 lpfc_sli4_eq_flush(phba, speq);
9468 spin_unlock_irqrestore(&phba->hbalock, iflag);
9469 return IRQ_NONE;
9470 }
9471
9472 /*
9473 * Process all the event on FCP slow-path EQ
9474 */
9475 while ((eqe = lpfc_sli4_eq_get(speq))) {
9476 lpfc_sli4_sp_handle_eqe(phba, eqe);
9477 if (!(++ecount % LPFC_GET_QE_REL_INT))
9478 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9479 }
9480
9481 /* Always clear and re-arm the slow-path EQ */
9482 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9483
9484 /* Catch the no cq entry condition */
9485 if (unlikely(ecount == 0)) {
9486 if (phba->intr_type == MSIX)
9487 /* MSI-X treated interrupt served as no EQ share INT */
9488 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9489 "0357 MSI-X interrupt with no EQE\n");
9490 else
9491 /* Non MSI-X treated on interrupt as EQ share INT */
9492 return IRQ_NONE;
9493 }
9494
9495 return IRQ_HANDLED;
9496} /* lpfc_sli4_sp_intr_handler */
9497
9498/**
9499 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9500 * @irq: Interrupt number.
9501 * @dev_id: The device context pointer.
9502 *
9503 * This function is directly called from the PCI layer as an interrupt
9504 * service routine when device with SLI-4 interface spec is enabled with
9505 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9506 * ring event in the HBA. However, when the device is enabled with either
9507 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9508 * device-level interrupt handler. When the PCI slot is in error recovery
9509 * or the HBA is undergoing initialization, the interrupt handler will not
9510 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9511 * the intrrupt context. This function is called without any lock held.
9512 * It gets the hbalock to access and update SLI data structures. Note that,
9513 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9514 * equal to that of FCP CQ index.
9515 *
9516 * This function returns IRQ_HANDLED when interrupt is handled else it
9517 * returns IRQ_NONE.
9518 **/
9519irqreturn_t
9520lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9521{
9522 struct lpfc_hba *phba;
9523 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9524 struct lpfc_queue *fpeq;
9525 struct lpfc_eqe *eqe;
9526 unsigned long iflag;
9527 int ecount = 0;
9528 uint32_t fcp_eqidx;
9529
9530 /* Get the driver's phba structure from the dev_id */
9531 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9532 phba = fcp_eq_hdl->phba;
9533 fcp_eqidx = fcp_eq_hdl->idx;
9534
9535 if (unlikely(!phba))
9536 return IRQ_NONE;
9537
9538 /* Get to the EQ struct associated with this vector */
9539 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9540
9541 /* Check device state for handling interrupt */
9542 if (unlikely(lpfc_intr_state_check(phba))) {
9543 /* Check again for link_state with lock held */
9544 spin_lock_irqsave(&phba->hbalock, iflag);
9545 if (phba->link_state < LPFC_LINK_DOWN)
9546 /* Flush, clear interrupt, and rearm the EQ */
9547 lpfc_sli4_eq_flush(phba, fpeq);
9548 spin_unlock_irqrestore(&phba->hbalock, iflag);
9549 return IRQ_NONE;
9550 }
9551
9552 /*
9553 * Process all the event on FCP fast-path EQ
9554 */
9555 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9556 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9557 if (!(++ecount % LPFC_GET_QE_REL_INT))
9558 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9559 }
9560
9561 /* Always clear and re-arm the fast-path EQ */
9562 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9563
9564 if (unlikely(ecount == 0)) {
9565 if (phba->intr_type == MSIX)
9566 /* MSI-X treated interrupt served as no EQ share INT */
9567 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9568 "0358 MSI-X interrupt with no EQE\n");
9569 else
9570 /* Non MSI-X treated on interrupt as EQ share INT */
9571 return IRQ_NONE;
9572 }
9573
9574 return IRQ_HANDLED;
9575} /* lpfc_sli4_fp_intr_handler */
9576
9577/**
9578 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9579 * @irq: Interrupt number.
9580 * @dev_id: The device context pointer.
9581 *
9582 * This function is the device-level interrupt handler to device with SLI-4
9583 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9584 * interrupt mode is enabled and there is an event in the HBA which requires
9585 * driver attention. This function invokes the slow-path interrupt attention
9586 * handling function and fast-path interrupt attention handling function in
9587 * turn to process the relevant HBA attention events. This function is called
9588 * without any lock held. It gets the hbalock to access and update SLI data
9589 * structures.
9590 *
9591 * This function returns IRQ_HANDLED when interrupt is handled, else it
9592 * returns IRQ_NONE.
9593 **/
9594irqreturn_t
9595lpfc_sli4_intr_handler(int irq, void *dev_id)
9596{
9597 struct lpfc_hba *phba;
9598 irqreturn_t sp_irq_rc, fp_irq_rc;
9599 bool fp_handled = false;
9600 uint32_t fcp_eqidx;
9601
9602 /* Get the driver's phba structure from the dev_id */
9603 phba = (struct lpfc_hba *)dev_id;
9604
9605 if (unlikely(!phba))
9606 return IRQ_NONE;
9607
9608 /*
9609 * Invokes slow-path host attention interrupt handling as appropriate.
9610 */
9611 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9612
9613 /*
9614 * Invoke fast-path host attention interrupt handling as appropriate.
9615 */
9616 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9617 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9618 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9619 if (fp_irq_rc == IRQ_HANDLED)
9620 fp_handled |= true;
9621 }
9622
9623 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9624} /* lpfc_sli4_intr_handler */
9625
9626/**
9627 * lpfc_sli4_queue_free - free a queue structure and associated memory
9628 * @queue: The queue structure to free.
9629 *
9630 * This function frees a queue structure and the DMAable memeory used for
9631 * the host resident queue. This function must be called after destroying the
9632 * queue on the HBA.
9633 **/
9634void
9635lpfc_sli4_queue_free(struct lpfc_queue *queue)
9636{
9637 struct lpfc_dmabuf *dmabuf;
9638
9639 if (!queue)
9640 return;
9641
9642 while (!list_empty(&queue->page_list)) {
9643 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9644 list);
49198b37 9645 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
9646 dmabuf->virt, dmabuf->phys);
9647 kfree(dmabuf);
9648 }
9649 kfree(queue);
9650 return;
9651}
9652
9653/**
9654 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9655 * @phba: The HBA that this queue is being created on.
9656 * @entry_size: The size of each queue entry for this queue.
9657 * @entry count: The number of entries that this queue will handle.
9658 *
9659 * This function allocates a queue structure and the DMAable memory used for
9660 * the host resident queue. This function must be called before creating the
9661 * queue on the HBA.
9662 **/
9663struct lpfc_queue *
9664lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9665 uint32_t entry_count)
9666{
9667 struct lpfc_queue *queue;
9668 struct lpfc_dmabuf *dmabuf;
9669 int x, total_qe_count;
9670 void *dma_pointer;
cb5172ea 9671 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 9672
cb5172ea
JS
9673 if (!phba->sli4_hba.pc_sli4_params.supported)
9674 hw_page_size = SLI4_PAGE_SIZE;
9675
4f774513
JS
9676 queue = kzalloc(sizeof(struct lpfc_queue) +
9677 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9678 if (!queue)
9679 return NULL;
cb5172ea
JS
9680 queue->page_count = (ALIGN(entry_size * entry_count,
9681 hw_page_size))/hw_page_size;
4f774513
JS
9682 INIT_LIST_HEAD(&queue->list);
9683 INIT_LIST_HEAD(&queue->page_list);
9684 INIT_LIST_HEAD(&queue->child_list);
9685 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9686 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9687 if (!dmabuf)
9688 goto out_fail;
9689 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 9690 hw_page_size, &dmabuf->phys,
4f774513
JS
9691 GFP_KERNEL);
9692 if (!dmabuf->virt) {
9693 kfree(dmabuf);
9694 goto out_fail;
9695 }
cb5172ea 9696 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9697 dmabuf->buffer_tag = x;
9698 list_add_tail(&dmabuf->list, &queue->page_list);
9699 /* initialize queue's entry array */
9700 dma_pointer = dmabuf->virt;
9701 for (; total_qe_count < entry_count &&
cb5172ea 9702 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
9703 total_qe_count++, dma_pointer += entry_size) {
9704 queue->qe[total_qe_count].address = dma_pointer;
9705 }
9706 }
9707 queue->entry_size = entry_size;
9708 queue->entry_count = entry_count;
9709 queue->phba = phba;
9710
9711 return queue;
9712out_fail:
9713 lpfc_sli4_queue_free(queue);
9714 return NULL;
9715}
9716
9717/**
9718 * lpfc_eq_create - Create an Event Queue on the HBA
9719 * @phba: HBA structure that indicates port to create a queue on.
9720 * @eq: The queue structure to use to create the event queue.
9721 * @imax: The maximum interrupt per second limit.
9722 *
9723 * This function creates an event queue, as detailed in @eq, on a port,
9724 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9725 *
9726 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9727 * is used to get the entry count and entry size that are necessary to
9728 * determine the number of pages to allocate and use for this queue. This
9729 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9730 * event queue. This function is asynchronous and will wait for the mailbox
9731 * command to finish before continuing.
9732 *
9733 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
9734 * memory this function will return -ENOMEM. If the queue create mailbox command
9735 * fails this function will return -ENXIO.
4f774513
JS
9736 **/
9737uint32_t
9738lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9739{
9740 struct lpfc_mbx_eq_create *eq_create;
9741 LPFC_MBOXQ_t *mbox;
9742 int rc, length, status = 0;
9743 struct lpfc_dmabuf *dmabuf;
9744 uint32_t shdr_status, shdr_add_status;
9745 union lpfc_sli4_cfg_shdr *shdr;
9746 uint16_t dmult;
49198b37
JS
9747 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9748
9749 if (!phba->sli4_hba.pc_sli4_params.supported)
9750 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
9751
9752 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9753 if (!mbox)
9754 return -ENOMEM;
9755 length = (sizeof(struct lpfc_mbx_eq_create) -
9756 sizeof(struct lpfc_sli4_cfg_mhdr));
9757 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9758 LPFC_MBOX_OPCODE_EQ_CREATE,
9759 length, LPFC_SLI4_MBX_EMBED);
9760 eq_create = &mbox->u.mqe.un.eq_create;
9761 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9762 eq->page_count);
9763 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9764 LPFC_EQE_SIZE);
9765 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9766 /* Calculate delay multiper from maximum interrupt per second */
9767 dmult = LPFC_DMULT_CONST/imax - 1;
9768 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9769 dmult);
9770 switch (eq->entry_count) {
9771 default:
9772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9773 "0360 Unsupported EQ count. (%d)\n",
9774 eq->entry_count);
9775 if (eq->entry_count < 256)
9776 return -EINVAL;
9777 /* otherwise default to smallest count (drop through) */
9778 case 256:
9779 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9780 LPFC_EQ_CNT_256);
9781 break;
9782 case 512:
9783 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9784 LPFC_EQ_CNT_512);
9785 break;
9786 case 1024:
9787 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9788 LPFC_EQ_CNT_1024);
9789 break;
9790 case 2048:
9791 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9792 LPFC_EQ_CNT_2048);
9793 break;
9794 case 4096:
9795 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9796 LPFC_EQ_CNT_4096);
9797 break;
9798 }
9799 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 9800 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9801 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9802 putPaddrLow(dmabuf->phys);
9803 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9804 putPaddrHigh(dmabuf->phys);
9805 }
9806 mbox->vport = phba->pport;
9807 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9808 mbox->context1 = NULL;
9809 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9810 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9811 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9812 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9813 if (shdr_status || shdr_add_status || rc) {
9814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9815 "2500 EQ_CREATE mailbox failed with "
9816 "status x%x add_status x%x, mbx status x%x\n",
9817 shdr_status, shdr_add_status, rc);
9818 status = -ENXIO;
9819 }
9820 eq->type = LPFC_EQ;
9821 eq->subtype = LPFC_NONE;
9822 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9823 if (eq->queue_id == 0xFFFF)
9824 status = -ENXIO;
9825 eq->host_index = 0;
9826 eq->hba_index = 0;
9827
8fa38513 9828 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9829 return status;
9830}
9831
9832/**
9833 * lpfc_cq_create - Create a Completion Queue on the HBA
9834 * @phba: HBA structure that indicates port to create a queue on.
9835 * @cq: The queue structure to use to create the completion queue.
9836 * @eq: The event queue to bind this completion queue to.
9837 *
9838 * This function creates a completion queue, as detailed in @wq, on a port,
9839 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9840 *
9841 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9842 * is used to get the entry count and entry size that are necessary to
9843 * determine the number of pages to allocate and use for this queue. The @eq
9844 * is used to indicate which event queue to bind this completion queue to. This
9845 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9846 * completion queue. This function is asynchronous and will wait for the mailbox
9847 * command to finish before continuing.
9848 *
9849 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
9850 * memory this function will return -ENOMEM. If the queue create mailbox command
9851 * fails this function will return -ENXIO.
4f774513
JS
9852 **/
9853uint32_t
9854lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9855 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9856{
9857 struct lpfc_mbx_cq_create *cq_create;
9858 struct lpfc_dmabuf *dmabuf;
9859 LPFC_MBOXQ_t *mbox;
9860 int rc, length, status = 0;
9861 uint32_t shdr_status, shdr_add_status;
9862 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
9863 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9864
9865 if (!phba->sli4_hba.pc_sli4_params.supported)
9866 hw_page_size = SLI4_PAGE_SIZE;
9867
4f774513
JS
9868
9869 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9870 if (!mbox)
9871 return -ENOMEM;
9872 length = (sizeof(struct lpfc_mbx_cq_create) -
9873 sizeof(struct lpfc_sli4_cfg_mhdr));
9874 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9875 LPFC_MBOX_OPCODE_CQ_CREATE,
9876 length, LPFC_SLI4_MBX_EMBED);
9877 cq_create = &mbox->u.mqe.un.cq_create;
9878 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9879 cq->page_count);
9880 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9881 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9882 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9883 switch (cq->entry_count) {
9884 default:
9885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9886 "0361 Unsupported CQ count. (%d)\n",
9887 cq->entry_count);
9888 if (cq->entry_count < 256)
9889 return -EINVAL;
9890 /* otherwise default to smallest count (drop through) */
9891 case 256:
9892 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9893 LPFC_CQ_CNT_256);
9894 break;
9895 case 512:
9896 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9897 LPFC_CQ_CNT_512);
9898 break;
9899 case 1024:
9900 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9901 LPFC_CQ_CNT_1024);
9902 break;
9903 }
9904 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 9905 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9906 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9907 putPaddrLow(dmabuf->phys);
9908 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9909 putPaddrHigh(dmabuf->phys);
9910 }
9911 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9912
9913 /* The IOCTL status is embedded in the mailbox subheader. */
9914 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9915 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9916 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9917 if (shdr_status || shdr_add_status || rc) {
9918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9919 "2501 CQ_CREATE mailbox failed with "
9920 "status x%x add_status x%x, mbx status x%x\n",
9921 shdr_status, shdr_add_status, rc);
9922 status = -ENXIO;
9923 goto out;
9924 }
9925 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9926 if (cq->queue_id == 0xFFFF) {
9927 status = -ENXIO;
9928 goto out;
9929 }
9930 /* link the cq onto the parent eq child list */
9931 list_add_tail(&cq->list, &eq->child_list);
9932 /* Set up completion queue's type and subtype */
9933 cq->type = type;
9934 cq->subtype = subtype;
9935 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9936 cq->host_index = 0;
9937 cq->hba_index = 0;
4f774513 9938
8fa38513
JS
9939out:
9940 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9941 return status;
9942}
9943
b19a061a
JS
9944/**
9945 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9946 * @phba: HBA structure that indicates port to create a queue on.
9947 * @mq: The queue structure to use to create the mailbox queue.
9948 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9949 * @cq: The completion queue to associate with this cq.
9950 *
9951 * This function provides failback (fb) functionality when the
9952 * mq_create_ext fails on older FW generations. It's purpose is identical
9953 * to mq_create_ext otherwise.
9954 *
9955 * This routine cannot fail as all attributes were previously accessed and
9956 * initialized in mq_create_ext.
9957 **/
9958static void
9959lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9960 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9961{
9962 struct lpfc_mbx_mq_create *mq_create;
9963 struct lpfc_dmabuf *dmabuf;
9964 int length;
9965
9966 length = (sizeof(struct lpfc_mbx_mq_create) -
9967 sizeof(struct lpfc_sli4_cfg_mhdr));
9968 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9969 LPFC_MBOX_OPCODE_MQ_CREATE,
9970 length, LPFC_SLI4_MBX_EMBED);
9971 mq_create = &mbox->u.mqe.un.mq_create;
9972 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9973 mq->page_count);
9974 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9975 cq->queue_id);
9976 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9977 switch (mq->entry_count) {
9978 case 16:
9979 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9980 LPFC_MQ_CNT_16);
9981 break;
9982 case 32:
9983 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9984 LPFC_MQ_CNT_32);
9985 break;
9986 case 64:
9987 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9988 LPFC_MQ_CNT_64);
9989 break;
9990 case 128:
9991 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9992 LPFC_MQ_CNT_128);
9993 break;
9994 }
9995 list_for_each_entry(dmabuf, &mq->page_list, list) {
9996 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9997 putPaddrLow(dmabuf->phys);
9998 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9999 putPaddrHigh(dmabuf->phys);
10000 }
10001}
10002
04c68496
JS
10003/**
10004 * lpfc_mq_create - Create a mailbox Queue on the HBA
10005 * @phba: HBA structure that indicates port to create a queue on.
10006 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
10007 * @cq: The completion queue to associate with this cq.
10008 * @subtype: The queue's subtype.
04c68496
JS
10009 *
10010 * This function creates a mailbox queue, as detailed in @mq, on a port,
10011 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10012 *
10013 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10014 * is used to get the entry count and entry size that are necessary to
10015 * determine the number of pages to allocate and use for this queue. This
10016 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10017 * mailbox queue. This function is asynchronous and will wait for the mailbox
10018 * command to finish before continuing.
10019 *
10020 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10021 * memory this function will return -ENOMEM. If the queue create mailbox command
10022 * fails this function will return -ENXIO.
04c68496 10023 **/
b19a061a 10024int32_t
04c68496
JS
10025lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10026 struct lpfc_queue *cq, uint32_t subtype)
10027{
10028 struct lpfc_mbx_mq_create *mq_create;
b19a061a 10029 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
10030 struct lpfc_dmabuf *dmabuf;
10031 LPFC_MBOXQ_t *mbox;
10032 int rc, length, status = 0;
10033 uint32_t shdr_status, shdr_add_status;
10034 union lpfc_sli4_cfg_shdr *shdr;
49198b37 10035 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 10036
49198b37
JS
10037 if (!phba->sli4_hba.pc_sli4_params.supported)
10038 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 10039
04c68496
JS
10040 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10041 if (!mbox)
10042 return -ENOMEM;
b19a061a 10043 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
10044 sizeof(struct lpfc_sli4_cfg_mhdr));
10045 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 10046 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 10047 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
10048
10049 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10050 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
04c68496 10051 mq->page_count);
b19a061a
JS
10052 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10053 1);
10054 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10055 &mq_create_ext->u.request, 1);
10056 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10057 &mq_create_ext->u.request, 1);
10058 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10059 cq->queue_id);
10060 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
04c68496
JS
10061 switch (mq->entry_count) {
10062 default:
10063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10064 "0362 Unsupported MQ count. (%d)\n",
10065 mq->entry_count);
10066 if (mq->entry_count < 16)
10067 return -EINVAL;
10068 /* otherwise default to smallest count (drop through) */
10069 case 16:
b19a061a 10070 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10071 LPFC_MQ_CNT_16);
10072 break;
10073 case 32:
b19a061a 10074 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10075 LPFC_MQ_CNT_32);
10076 break;
10077 case 64:
b19a061a 10078 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10079 LPFC_MQ_CNT_64);
10080 break;
10081 case 128:
b19a061a 10082 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10083 LPFC_MQ_CNT_128);
10084 break;
10085 }
10086 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 10087 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 10088 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 10089 putPaddrLow(dmabuf->phys);
b19a061a 10090 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
10091 putPaddrHigh(dmabuf->phys);
10092 }
10093 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
10094 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10095 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10096 &mq_create_ext->u.response);
10097 if (rc != MBX_SUCCESS) {
10098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10099 "2795 MQ_CREATE_EXT failed with "
10100 "status x%x. Failback to MQ_CREATE.\n",
10101 rc);
10102 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10103 mq_create = &mbox->u.mqe.un.mq_create;
10104 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10105 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10106 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10107 &mq_create->u.response);
10108 }
10109
04c68496 10110 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
10111 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10112 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10113 if (shdr_status || shdr_add_status || rc) {
10114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10115 "2502 MQ_CREATE mailbox failed with "
10116 "status x%x add_status x%x, mbx status x%x\n",
10117 shdr_status, shdr_add_status, rc);
10118 status = -ENXIO;
10119 goto out;
10120 }
04c68496
JS
10121 if (mq->queue_id == 0xFFFF) {
10122 status = -ENXIO;
10123 goto out;
10124 }
10125 mq->type = LPFC_MQ;
10126 mq->subtype = subtype;
10127 mq->host_index = 0;
10128 mq->hba_index = 0;
10129
10130 /* link the mq onto the parent cq child list */
10131 list_add_tail(&mq->list, &cq->child_list);
10132out:
8fa38513 10133 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
10134 return status;
10135}
10136
4f774513
JS
10137/**
10138 * lpfc_wq_create - Create a Work Queue on the HBA
10139 * @phba: HBA structure that indicates port to create a queue on.
10140 * @wq: The queue structure to use to create the work queue.
10141 * @cq: The completion queue to bind this work queue to.
10142 * @subtype: The subtype of the work queue indicating its functionality.
10143 *
10144 * This function creates a work queue, as detailed in @wq, on a port, described
10145 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10146 *
10147 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10148 * is used to get the entry count and entry size that are necessary to
10149 * determine the number of pages to allocate and use for this queue. The @cq
10150 * is used to indicate which completion queue to bind this work queue to. This
10151 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10152 * work queue. This function is asynchronous and will wait for the mailbox
10153 * command to finish before continuing.
10154 *
10155 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10156 * memory this function will return -ENOMEM. If the queue create mailbox command
10157 * fails this function will return -ENXIO.
4f774513
JS
10158 **/
10159uint32_t
10160lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10161 struct lpfc_queue *cq, uint32_t subtype)
10162{
10163 struct lpfc_mbx_wq_create *wq_create;
10164 struct lpfc_dmabuf *dmabuf;
10165 LPFC_MBOXQ_t *mbox;
10166 int rc, length, status = 0;
10167 uint32_t shdr_status, shdr_add_status;
10168 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
10169 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10170
10171 if (!phba->sli4_hba.pc_sli4_params.supported)
10172 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
10173
10174 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10175 if (!mbox)
10176 return -ENOMEM;
10177 length = (sizeof(struct lpfc_mbx_wq_create) -
10178 sizeof(struct lpfc_sli4_cfg_mhdr));
10179 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10180 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10181 length, LPFC_SLI4_MBX_EMBED);
10182 wq_create = &mbox->u.mqe.un.wq_create;
10183 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10184 wq->page_count);
10185 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10186 cq->queue_id);
10187 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 10188 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
10189 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10190 putPaddrLow(dmabuf->phys);
10191 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10192 putPaddrHigh(dmabuf->phys);
10193 }
10194 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10195 /* The IOCTL status is embedded in the mailbox subheader. */
10196 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10199 if (shdr_status || shdr_add_status || rc) {
10200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10201 "2503 WQ_CREATE mailbox failed with "
10202 "status x%x add_status x%x, mbx status x%x\n",
10203 shdr_status, shdr_add_status, rc);
10204 status = -ENXIO;
10205 goto out;
10206 }
10207 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10208 if (wq->queue_id == 0xFFFF) {
10209 status = -ENXIO;
10210 goto out;
10211 }
10212 wq->type = LPFC_WQ;
10213 wq->subtype = subtype;
10214 wq->host_index = 0;
10215 wq->hba_index = 0;
10216
10217 /* link the wq onto the parent cq child list */
10218 list_add_tail(&wq->list, &cq->child_list);
10219out:
8fa38513 10220 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10221 return status;
10222}
10223
10224/**
10225 * lpfc_rq_create - Create a Receive Queue on the HBA
10226 * @phba: HBA structure that indicates port to create a queue on.
10227 * @hrq: The queue structure to use to create the header receive queue.
10228 * @drq: The queue structure to use to create the data receive queue.
10229 * @cq: The completion queue to bind this work queue to.
10230 *
10231 * This function creates a receive buffer queue pair , as detailed in @hrq and
10232 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10233 * to the HBA.
10234 *
10235 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10236 * struct is used to get the entry count that is necessary to determine the
10237 * number of pages to use for this queue. The @cq is used to indicate which
10238 * completion queue to bind received buffers that are posted to these queues to.
10239 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10240 * receive queue pair. This function is asynchronous and will wait for the
10241 * mailbox command to finish before continuing.
10242 *
10243 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10244 * memory this function will return -ENOMEM. If the queue create mailbox command
10245 * fails this function will return -ENXIO.
4f774513
JS
10246 **/
10247uint32_t
10248lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10249 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10250{
10251 struct lpfc_mbx_rq_create *rq_create;
10252 struct lpfc_dmabuf *dmabuf;
10253 LPFC_MBOXQ_t *mbox;
10254 int rc, length, status = 0;
10255 uint32_t shdr_status, shdr_add_status;
10256 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
10257 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10258
10259 if (!phba->sli4_hba.pc_sli4_params.supported)
10260 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
10261
10262 if (hrq->entry_count != drq->entry_count)
10263 return -EINVAL;
10264 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10265 if (!mbox)
10266 return -ENOMEM;
10267 length = (sizeof(struct lpfc_mbx_rq_create) -
10268 sizeof(struct lpfc_sli4_cfg_mhdr));
10269 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10270 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10271 length, LPFC_SLI4_MBX_EMBED);
10272 rq_create = &mbox->u.mqe.un.rq_create;
10273 switch (hrq->entry_count) {
10274 default:
10275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10276 "2535 Unsupported RQ count. (%d)\n",
10277 hrq->entry_count);
10278 if (hrq->entry_count < 512)
10279 return -EINVAL;
10280 /* otherwise default to smallest count (drop through) */
10281 case 512:
10282 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10283 LPFC_RQ_RING_SIZE_512);
10284 break;
10285 case 1024:
10286 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10287 LPFC_RQ_RING_SIZE_1024);
10288 break;
10289 case 2048:
10290 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10291 LPFC_RQ_RING_SIZE_2048);
10292 break;
10293 case 4096:
10294 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10295 LPFC_RQ_RING_SIZE_4096);
10296 break;
10297 }
10298 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10299 cq->queue_id);
10300 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10301 hrq->page_count);
10302 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10303 LPFC_HDR_BUF_SIZE);
10304 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 10305 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
10306 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10307 putPaddrLow(dmabuf->phys);
10308 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10309 putPaddrHigh(dmabuf->phys);
10310 }
10311 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10312 /* The IOCTL status is embedded in the mailbox subheader. */
10313 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10314 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10315 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10316 if (shdr_status || shdr_add_status || rc) {
10317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10318 "2504 RQ_CREATE mailbox failed with "
10319 "status x%x add_status x%x, mbx status x%x\n",
10320 shdr_status, shdr_add_status, rc);
10321 status = -ENXIO;
10322 goto out;
10323 }
10324 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10325 if (hrq->queue_id == 0xFFFF) {
10326 status = -ENXIO;
10327 goto out;
10328 }
10329 hrq->type = LPFC_HRQ;
10330 hrq->subtype = subtype;
10331 hrq->host_index = 0;
10332 hrq->hba_index = 0;
10333
10334 /* now create the data queue */
10335 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10336 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10337 length, LPFC_SLI4_MBX_EMBED);
10338 switch (drq->entry_count) {
10339 default:
10340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10341 "2536 Unsupported RQ count. (%d)\n",
10342 drq->entry_count);
10343 if (drq->entry_count < 512)
10344 return -EINVAL;
10345 /* otherwise default to smallest count (drop through) */
10346 case 512:
10347 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10348 LPFC_RQ_RING_SIZE_512);
10349 break;
10350 case 1024:
10351 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10352 LPFC_RQ_RING_SIZE_1024);
10353 break;
10354 case 2048:
10355 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10356 LPFC_RQ_RING_SIZE_2048);
10357 break;
10358 case 4096:
10359 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10360 LPFC_RQ_RING_SIZE_4096);
10361 break;
10362 }
10363 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10364 cq->queue_id);
10365 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10366 drq->page_count);
10367 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10368 LPFC_DATA_BUF_SIZE);
10369 list_for_each_entry(dmabuf, &drq->page_list, list) {
10370 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10371 putPaddrLow(dmabuf->phys);
10372 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10373 putPaddrHigh(dmabuf->phys);
10374 }
10375 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10376 /* The IOCTL status is embedded in the mailbox subheader. */
10377 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10378 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10379 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10380 if (shdr_status || shdr_add_status || rc) {
10381 status = -ENXIO;
10382 goto out;
10383 }
10384 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10385 if (drq->queue_id == 0xFFFF) {
10386 status = -ENXIO;
10387 goto out;
10388 }
10389 drq->type = LPFC_DRQ;
10390 drq->subtype = subtype;
10391 drq->host_index = 0;
10392 drq->hba_index = 0;
10393
10394 /* link the header and data RQs onto the parent cq child list */
10395 list_add_tail(&hrq->list, &cq->child_list);
10396 list_add_tail(&drq->list, &cq->child_list);
10397
10398out:
8fa38513 10399 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10400 return status;
10401}
10402
10403/**
10404 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10405 * @eq: The queue structure associated with the queue to destroy.
10406 *
10407 * This function destroys a queue, as detailed in @eq by sending an mailbox
10408 * command, specific to the type of queue, to the HBA.
10409 *
10410 * The @eq struct is used to get the queue ID of the queue to destroy.
10411 *
10412 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10413 * command fails this function will return -ENXIO.
4f774513
JS
10414 **/
10415uint32_t
10416lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10417{
10418 LPFC_MBOXQ_t *mbox;
10419 int rc, length, status = 0;
10420 uint32_t shdr_status, shdr_add_status;
10421 union lpfc_sli4_cfg_shdr *shdr;
10422
10423 if (!eq)
10424 return -ENODEV;
10425 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10426 if (!mbox)
10427 return -ENOMEM;
10428 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10429 sizeof(struct lpfc_sli4_cfg_mhdr));
10430 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10431 LPFC_MBOX_OPCODE_EQ_DESTROY,
10432 length, LPFC_SLI4_MBX_EMBED);
10433 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10434 eq->queue_id);
10435 mbox->vport = eq->phba->pport;
10436 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10437
10438 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10439 /* The IOCTL status is embedded in the mailbox subheader. */
10440 shdr = (union lpfc_sli4_cfg_shdr *)
10441 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10442 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10443 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10444 if (shdr_status || shdr_add_status || rc) {
10445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10446 "2505 EQ_DESTROY mailbox failed with "
10447 "status x%x add_status x%x, mbx status x%x\n",
10448 shdr_status, shdr_add_status, rc);
10449 status = -ENXIO;
10450 }
10451
10452 /* Remove eq from any list */
10453 list_del_init(&eq->list);
8fa38513 10454 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10455 return status;
10456}
10457
10458/**
10459 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10460 * @cq: The queue structure associated with the queue to destroy.
10461 *
10462 * This function destroys a queue, as detailed in @cq by sending an mailbox
10463 * command, specific to the type of queue, to the HBA.
10464 *
10465 * The @cq struct is used to get the queue ID of the queue to destroy.
10466 *
10467 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10468 * command fails this function will return -ENXIO.
4f774513
JS
10469 **/
10470uint32_t
10471lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10472{
10473 LPFC_MBOXQ_t *mbox;
10474 int rc, length, status = 0;
10475 uint32_t shdr_status, shdr_add_status;
10476 union lpfc_sli4_cfg_shdr *shdr;
10477
10478 if (!cq)
10479 return -ENODEV;
10480 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10481 if (!mbox)
10482 return -ENOMEM;
10483 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10484 sizeof(struct lpfc_sli4_cfg_mhdr));
10485 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10486 LPFC_MBOX_OPCODE_CQ_DESTROY,
10487 length, LPFC_SLI4_MBX_EMBED);
10488 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10489 cq->queue_id);
10490 mbox->vport = cq->phba->pport;
10491 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10492 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10493 /* The IOCTL status is embedded in the mailbox subheader. */
10494 shdr = (union lpfc_sli4_cfg_shdr *)
10495 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10498 if (shdr_status || shdr_add_status || rc) {
10499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10500 "2506 CQ_DESTROY mailbox failed with "
10501 "status x%x add_status x%x, mbx status x%x\n",
10502 shdr_status, shdr_add_status, rc);
10503 status = -ENXIO;
10504 }
10505 /* Remove cq from any list */
10506 list_del_init(&cq->list);
8fa38513 10507 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10508 return status;
10509}
10510
04c68496
JS
10511/**
10512 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10513 * @qm: The queue structure associated with the queue to destroy.
10514 *
10515 * This function destroys a queue, as detailed in @mq by sending an mailbox
10516 * command, specific to the type of queue, to the HBA.
10517 *
10518 * The @mq struct is used to get the queue ID of the queue to destroy.
10519 *
10520 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10521 * command fails this function will return -ENXIO.
04c68496
JS
10522 **/
10523uint32_t
10524lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10525{
10526 LPFC_MBOXQ_t *mbox;
10527 int rc, length, status = 0;
10528 uint32_t shdr_status, shdr_add_status;
10529 union lpfc_sli4_cfg_shdr *shdr;
10530
10531 if (!mq)
10532 return -ENODEV;
10533 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10534 if (!mbox)
10535 return -ENOMEM;
10536 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10537 sizeof(struct lpfc_sli4_cfg_mhdr));
10538 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10539 LPFC_MBOX_OPCODE_MQ_DESTROY,
10540 length, LPFC_SLI4_MBX_EMBED);
10541 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10542 mq->queue_id);
10543 mbox->vport = mq->phba->pport;
10544 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10545 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10546 /* The IOCTL status is embedded in the mailbox subheader. */
10547 shdr = (union lpfc_sli4_cfg_shdr *)
10548 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10549 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10550 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10551 if (shdr_status || shdr_add_status || rc) {
10552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10553 "2507 MQ_DESTROY mailbox failed with "
10554 "status x%x add_status x%x, mbx status x%x\n",
10555 shdr_status, shdr_add_status, rc);
10556 status = -ENXIO;
10557 }
10558 /* Remove mq from any list */
10559 list_del_init(&mq->list);
8fa38513 10560 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10561 return status;
10562}
10563
4f774513
JS
10564/**
10565 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10566 * @wq: The queue structure associated with the queue to destroy.
10567 *
10568 * This function destroys a queue, as detailed in @wq by sending an mailbox
10569 * command, specific to the type of queue, to the HBA.
10570 *
10571 * The @wq struct is used to get the queue ID of the queue to destroy.
10572 *
10573 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10574 * command fails this function will return -ENXIO.
4f774513
JS
10575 **/
10576uint32_t
10577lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10578{
10579 LPFC_MBOXQ_t *mbox;
10580 int rc, length, status = 0;
10581 uint32_t shdr_status, shdr_add_status;
10582 union lpfc_sli4_cfg_shdr *shdr;
10583
10584 if (!wq)
10585 return -ENODEV;
10586 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10587 if (!mbox)
10588 return -ENOMEM;
10589 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10590 sizeof(struct lpfc_sli4_cfg_mhdr));
10591 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10592 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10593 length, LPFC_SLI4_MBX_EMBED);
10594 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10595 wq->queue_id);
10596 mbox->vport = wq->phba->pport;
10597 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10598 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10599 shdr = (union lpfc_sli4_cfg_shdr *)
10600 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10601 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10602 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10603 if (shdr_status || shdr_add_status || rc) {
10604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10605 "2508 WQ_DESTROY mailbox failed with "
10606 "status x%x add_status x%x, mbx status x%x\n",
10607 shdr_status, shdr_add_status, rc);
10608 status = -ENXIO;
10609 }
10610 /* Remove wq from any list */
10611 list_del_init(&wq->list);
8fa38513 10612 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10613 return status;
10614}
10615
10616/**
10617 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10618 * @rq: The queue structure associated with the queue to destroy.
10619 *
10620 * This function destroys a queue, as detailed in @rq by sending an mailbox
10621 * command, specific to the type of queue, to the HBA.
10622 *
10623 * The @rq struct is used to get the queue ID of the queue to destroy.
10624 *
10625 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10626 * command fails this function will return -ENXIO.
4f774513
JS
10627 **/
10628uint32_t
10629lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10630 struct lpfc_queue *drq)
10631{
10632 LPFC_MBOXQ_t *mbox;
10633 int rc, length, status = 0;
10634 uint32_t shdr_status, shdr_add_status;
10635 union lpfc_sli4_cfg_shdr *shdr;
10636
10637 if (!hrq || !drq)
10638 return -ENODEV;
10639 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10640 if (!mbox)
10641 return -ENOMEM;
10642 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10643 sizeof(struct mbox_header));
10644 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10645 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10646 length, LPFC_SLI4_MBX_EMBED);
10647 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10648 hrq->queue_id);
10649 mbox->vport = hrq->phba->pport;
10650 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10651 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10652 /* The IOCTL status is embedded in the mailbox subheader. */
10653 shdr = (union lpfc_sli4_cfg_shdr *)
10654 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10655 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10656 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10657 if (shdr_status || shdr_add_status || rc) {
10658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10659 "2509 RQ_DESTROY mailbox failed with "
10660 "status x%x add_status x%x, mbx status x%x\n",
10661 shdr_status, shdr_add_status, rc);
10662 if (rc != MBX_TIMEOUT)
10663 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10664 return -ENXIO;
10665 }
10666 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10667 drq->queue_id);
10668 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10669 shdr = (union lpfc_sli4_cfg_shdr *)
10670 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10671 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10672 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10673 if (shdr_status || shdr_add_status || rc) {
10674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10675 "2510 RQ_DESTROY mailbox failed with "
10676 "status x%x add_status x%x, mbx status x%x\n",
10677 shdr_status, shdr_add_status, rc);
10678 status = -ENXIO;
10679 }
10680 list_del_init(&hrq->list);
10681 list_del_init(&drq->list);
8fa38513 10682 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10683 return status;
10684}
10685
10686/**
10687 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10688 * @phba: The virtual port for which this call being executed.
10689 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10690 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10691 * @xritag: the xritag that ties this io to the SGL pages.
10692 *
10693 * This routine will post the sgl pages for the IO that has the xritag
10694 * that is in the iocbq structure. The xritag is assigned during iocbq
10695 * creation and persists for as long as the driver is loaded.
10696 * if the caller has fewer than 256 scatter gather segments to map then
10697 * pdma_phys_addr1 should be 0.
10698 * If the caller needs to map more than 256 scatter gather segment then
10699 * pdma_phys_addr1 should be a valid physical address.
10700 * physical address for SGLs must be 64 byte aligned.
10701 * If you are going to map 2 SGL's then the first one must have 256 entries
10702 * the second sgl can have between 1 and 256 entries.
10703 *
10704 * Return codes:
10705 * 0 - Success
10706 * -ENXIO, -ENOMEM - Failure
10707 **/
10708int
10709lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10710 dma_addr_t pdma_phys_addr0,
10711 dma_addr_t pdma_phys_addr1,
10712 uint16_t xritag)
10713{
10714 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10715 LPFC_MBOXQ_t *mbox;
10716 int rc;
10717 uint32_t shdr_status, shdr_add_status;
10718 union lpfc_sli4_cfg_shdr *shdr;
10719
10720 if (xritag == NO_XRI) {
10721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10722 "0364 Invalid param:\n");
10723 return -EINVAL;
10724 }
10725
10726 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10727 if (!mbox)
10728 return -ENOMEM;
10729
10730 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10731 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10732 sizeof(struct lpfc_mbx_post_sgl_pages) -
10733 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10734
10735 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10736 &mbox->u.mqe.un.post_sgl_pages;
10737 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10738 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10739
10740 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10741 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10742 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10743 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10744
10745 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10746 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10747 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10748 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10749 if (!phba->sli4_hba.intr_enable)
10750 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10751 else
10752 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10753 /* The IOCTL status is embedded in the mailbox subheader. */
10754 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10755 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10756 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10757 if (rc != MBX_TIMEOUT)
10758 mempool_free(mbox, phba->mbox_mem_pool);
10759 if (shdr_status || shdr_add_status || rc) {
10760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10761 "2511 POST_SGL mailbox failed with "
10762 "status x%x add_status x%x, mbx status x%x\n",
10763 shdr_status, shdr_add_status, rc);
10764 rc = -ENXIO;
10765 }
10766 return 0;
10767}
10768/**
10769 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10770 * @phba: The virtual port for which this call being executed.
10771 *
10772 * This routine will remove all of the sgl pages registered with the hba.
10773 *
10774 * Return codes:
10775 * 0 - Success
10776 * -ENXIO, -ENOMEM - Failure
10777 **/
10778int
10779lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10780{
10781 LPFC_MBOXQ_t *mbox;
10782 int rc;
10783 uint32_t shdr_status, shdr_add_status;
10784 union lpfc_sli4_cfg_shdr *shdr;
10785
10786 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10787 if (!mbox)
10788 return -ENOMEM;
10789
10790 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10791 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10792 LPFC_SLI4_MBX_EMBED);
10793 if (!phba->sli4_hba.intr_enable)
10794 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10795 else
10796 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10797 /* The IOCTL status is embedded in the mailbox subheader. */
10798 shdr = (union lpfc_sli4_cfg_shdr *)
10799 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10800 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10801 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10802 if (rc != MBX_TIMEOUT)
10803 mempool_free(mbox, phba->mbox_mem_pool);
10804 if (shdr_status || shdr_add_status || rc) {
10805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10806 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10807 "status x%x add_status x%x, mbx status x%x\n",
10808 shdr_status, shdr_add_status, rc);
10809 rc = -ENXIO;
10810 }
10811 return rc;
10812}
10813
10814/**
10815 * lpfc_sli4_next_xritag - Get an xritag for the io
10816 * @phba: Pointer to HBA context object.
10817 *
10818 * This function gets an xritag for the iocb. If there is no unused xritag
10819 * it will return 0xffff.
10820 * The function returns the allocated xritag if successful, else returns zero.
10821 * Zero is not a valid xritag.
10822 * The caller is not required to hold any lock.
10823 **/
10824uint16_t
10825lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10826{
10827 uint16_t xritag;
10828
10829 spin_lock_irq(&phba->hbalock);
10830 xritag = phba->sli4_hba.next_xri;
10831 if ((xritag != (uint16_t) -1) && xritag <
10832 (phba->sli4_hba.max_cfg_param.max_xri
10833 + phba->sli4_hba.max_cfg_param.xri_base)) {
10834 phba->sli4_hba.next_xri++;
10835 phba->sli4_hba.max_cfg_param.xri_used++;
10836 spin_unlock_irq(&phba->hbalock);
10837 return xritag;
10838 }
10839 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10840 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10841 "2004 Failed to allocate XRI.last XRITAG is %d"
10842 " Max XRI is %d, Used XRI is %d\n",
10843 phba->sli4_hba.next_xri,
10844 phba->sli4_hba.max_cfg_param.max_xri,
10845 phba->sli4_hba.max_cfg_param.xri_used);
10846 return -1;
10847}
10848
10849/**
10850 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10851 * @phba: pointer to lpfc hba data structure.
10852 *
10853 * This routine is invoked to post a block of driver's sgl pages to the
10854 * HBA using non-embedded mailbox command. No Lock is held. This routine
10855 * is only called when the driver is loading and after all IO has been
10856 * stopped.
10857 **/
10858int
10859lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10860{
10861 struct lpfc_sglq *sglq_entry;
10862 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10863 struct sgl_page_pairs *sgl_pg_pairs;
10864 void *viraddr;
10865 LPFC_MBOXQ_t *mbox;
10866 uint32_t reqlen, alloclen, pg_pairs;
10867 uint32_t mbox_tmo;
10868 uint16_t xritag_start = 0;
10869 int els_xri_cnt, rc = 0;
10870 uint32_t shdr_status, shdr_add_status;
10871 union lpfc_sli4_cfg_shdr *shdr;
10872
10873 /* The number of sgls to be posted */
10874 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10875
10876 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10877 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 10878 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
10879 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10880 "2559 Block sgl registration required DMA "
10881 "size (%d) great than a page\n", reqlen);
10882 return -ENOMEM;
10883 }
10884 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10885 if (!mbox) {
10886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10887 "2560 Failed to allocate mbox cmd memory\n");
10888 return -ENOMEM;
10889 }
10890
10891 /* Allocate DMA memory and set up the non-embedded mailbox command */
10892 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10893 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10894 LPFC_SLI4_MBX_NEMBED);
10895
10896 if (alloclen < reqlen) {
10897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10898 "0285 Allocated DMA memory size (%d) is "
10899 "less than the requested DMA memory "
10900 "size (%d)\n", alloclen, reqlen);
10901 lpfc_sli4_mbox_cmd_free(phba, mbox);
10902 return -ENOMEM;
10903 }
4f774513 10904 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10905 viraddr = mbox->sge_array->addr[0];
10906
10907 /* Set up the SGL pages in the non-embedded DMA pages */
10908 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10909 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10910
10911 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10912 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10913 /* Set up the sge entry */
10914 sgl_pg_pairs->sgl_pg0_addr_lo =
10915 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10916 sgl_pg_pairs->sgl_pg0_addr_hi =
10917 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10918 sgl_pg_pairs->sgl_pg1_addr_lo =
10919 cpu_to_le32(putPaddrLow(0));
10920 sgl_pg_pairs->sgl_pg1_addr_hi =
10921 cpu_to_le32(putPaddrHigh(0));
10922 /* Keep the first xritag on the list */
10923 if (pg_pairs == 0)
10924 xritag_start = sglq_entry->sli4_xritag;
10925 sgl_pg_pairs++;
10926 }
10927 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 10928 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
10929 /* Perform endian conversion if necessary */
10930 sgl->word0 = cpu_to_le32(sgl->word0);
10931
10932 if (!phba->sli4_hba.intr_enable)
10933 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10934 else {
10935 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10936 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10937 }
10938 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10939 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10940 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10941 if (rc != MBX_TIMEOUT)
10942 lpfc_sli4_mbox_cmd_free(phba, mbox);
10943 if (shdr_status || shdr_add_status || rc) {
10944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10945 "2513 POST_SGL_BLOCK mailbox command failed "
10946 "status x%x add_status x%x mbx status x%x\n",
10947 shdr_status, shdr_add_status, rc);
10948 rc = -ENXIO;
10949 }
10950 return rc;
10951}
10952
10953/**
10954 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10955 * @phba: pointer to lpfc hba data structure.
10956 * @sblist: pointer to scsi buffer list.
10957 * @count: number of scsi buffers on the list.
10958 *
10959 * This routine is invoked to post a block of @count scsi sgl pages from a
10960 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10961 * No Lock is held.
10962 *
10963 **/
10964int
10965lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10966 int cnt)
10967{
10968 struct lpfc_scsi_buf *psb;
10969 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10970 struct sgl_page_pairs *sgl_pg_pairs;
10971 void *viraddr;
10972 LPFC_MBOXQ_t *mbox;
10973 uint32_t reqlen, alloclen, pg_pairs;
10974 uint32_t mbox_tmo;
10975 uint16_t xritag_start = 0;
10976 int rc = 0;
10977 uint32_t shdr_status, shdr_add_status;
10978 dma_addr_t pdma_phys_bpl1;
10979 union lpfc_sli4_cfg_shdr *shdr;
10980
10981 /* Calculate the requested length of the dma memory */
10982 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10983 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 10984 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
10985 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10986 "0217 Block sgl registration required DMA "
10987 "size (%d) great than a page\n", reqlen);
10988 return -ENOMEM;
10989 }
10990 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10991 if (!mbox) {
10992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10993 "0283 Failed to allocate mbox cmd memory\n");
10994 return -ENOMEM;
10995 }
10996
10997 /* Allocate DMA memory and set up the non-embedded mailbox command */
10998 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10999 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11000 LPFC_SLI4_MBX_NEMBED);
11001
11002 if (alloclen < reqlen) {
11003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11004 "2561 Allocated DMA memory size (%d) is "
11005 "less than the requested DMA memory "
11006 "size (%d)\n", alloclen, reqlen);
11007 lpfc_sli4_mbox_cmd_free(phba, mbox);
11008 return -ENOMEM;
11009 }
4f774513 11010 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
11011 viraddr = mbox->sge_array->addr[0];
11012
11013 /* Set up the SGL pages in the non-embedded DMA pages */
11014 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11015 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11016
11017 pg_pairs = 0;
11018 list_for_each_entry(psb, sblist, list) {
11019 /* Set up the sge entry */
11020 sgl_pg_pairs->sgl_pg0_addr_lo =
11021 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11022 sgl_pg_pairs->sgl_pg0_addr_hi =
11023 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11024 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11025 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11026 else
11027 pdma_phys_bpl1 = 0;
11028 sgl_pg_pairs->sgl_pg1_addr_lo =
11029 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11030 sgl_pg_pairs->sgl_pg1_addr_hi =
11031 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11032 /* Keep the first xritag on the list */
11033 if (pg_pairs == 0)
11034 xritag_start = psb->cur_iocbq.sli4_xritag;
11035 sgl_pg_pairs++;
11036 pg_pairs++;
11037 }
11038 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11039 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11040 /* Perform endian conversion if necessary */
11041 sgl->word0 = cpu_to_le32(sgl->word0);
11042
11043 if (!phba->sli4_hba.intr_enable)
11044 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11045 else {
11046 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11047 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11048 }
11049 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11050 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11051 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11052 if (rc != MBX_TIMEOUT)
11053 lpfc_sli4_mbox_cmd_free(phba, mbox);
11054 if (shdr_status || shdr_add_status || rc) {
11055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11056 "2564 POST_SGL_BLOCK mailbox command failed "
11057 "status x%x add_status x%x mbx status x%x\n",
11058 shdr_status, shdr_add_status, rc);
11059 rc = -ENXIO;
11060 }
11061 return rc;
11062}
11063
11064/**
11065 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11066 * @phba: pointer to lpfc_hba struct that the frame was received on
11067 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11068 *
11069 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11070 * valid type of frame that the LPFC driver will handle. This function will
11071 * return a zero if the frame is a valid frame or a non zero value when the
11072 * frame does not pass the check.
11073 **/
11074static int
11075lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11076{
11077 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11078 char *type_names[] = FC_TYPE_NAMES_INIT;
11079 struct fc_vft_header *fc_vft_hdr;
11080
11081 switch (fc_hdr->fh_r_ctl) {
11082 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11083 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11084 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11085 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11086 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11087 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11088 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11089 case FC_RCTL_DD_CMD_STATUS: /* command status */
11090 case FC_RCTL_ELS_REQ: /* extended link services request */
11091 case FC_RCTL_ELS_REP: /* extended link services reply */
11092 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11093 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11094 case FC_RCTL_BA_NOP: /* basic link service NOP */
11095 case FC_RCTL_BA_ABTS: /* basic link service abort */
11096 case FC_RCTL_BA_RMC: /* remove connection */
11097 case FC_RCTL_BA_ACC: /* basic accept */
11098 case FC_RCTL_BA_RJT: /* basic reject */
11099 case FC_RCTL_BA_PRMT:
11100 case FC_RCTL_ACK_1: /* acknowledge_1 */
11101 case FC_RCTL_ACK_0: /* acknowledge_0 */
11102 case FC_RCTL_P_RJT: /* port reject */
11103 case FC_RCTL_F_RJT: /* fabric reject */
11104 case FC_RCTL_P_BSY: /* port busy */
11105 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11106 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11107 case FC_RCTL_LCR: /* link credit reset */
11108 case FC_RCTL_END: /* end */
11109 break;
11110 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11111 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11112 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11113 return lpfc_fc_frame_check(phba, fc_hdr);
11114 default:
11115 goto drop;
11116 }
11117 switch (fc_hdr->fh_type) {
11118 case FC_TYPE_BLS:
11119 case FC_TYPE_ELS:
11120 case FC_TYPE_FCP:
11121 case FC_TYPE_CT:
11122 break;
11123 case FC_TYPE_IP:
11124 case FC_TYPE_ILS:
11125 default:
11126 goto drop;
11127 }
11128 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11129 "2538 Received frame rctl:%s type:%s\n",
11130 rctl_names[fc_hdr->fh_r_ctl],
11131 type_names[fc_hdr->fh_type]);
11132 return 0;
11133drop:
11134 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11135 "2539 Dropped frame rctl:%s type:%s\n",
11136 rctl_names[fc_hdr->fh_r_ctl],
11137 type_names[fc_hdr->fh_type]);
11138 return 1;
11139}
11140
11141/**
11142 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11143 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11144 *
11145 * This function processes the FC header to retrieve the VFI from the VF
11146 * header, if one exists. This function will return the VFI if one exists
11147 * or 0 if no VSAN Header exists.
11148 **/
11149static uint32_t
11150lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11151{
11152 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11153
11154 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11155 return 0;
11156 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11157}
11158
11159/**
11160 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11161 * @phba: Pointer to the HBA structure to search for the vport on
11162 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11163 * @fcfi: The FC Fabric ID that the frame came from
11164 *
11165 * This function searches the @phba for a vport that matches the content of the
11166 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11167 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11168 * returns the matching vport pointer or NULL if unable to match frame to a
11169 * vport.
11170 **/
11171static struct lpfc_vport *
11172lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11173 uint16_t fcfi)
11174{
11175 struct lpfc_vport **vports;
11176 struct lpfc_vport *vport = NULL;
11177 int i;
11178 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11179 fc_hdr->fh_d_id[1] << 8 |
11180 fc_hdr->fh_d_id[2]);
11181
11182 vports = lpfc_create_vport_work_array(phba);
11183 if (vports != NULL)
11184 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11185 if (phba->fcf.fcfi == fcfi &&
11186 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11187 vports[i]->fc_myDID == did) {
11188 vport = vports[i];
11189 break;
11190 }
11191 }
11192 lpfc_destroy_vport_work_array(phba, vports);
11193 return vport;
11194}
11195
45ed1190
JS
11196/**
11197 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11198 * @vport: The vport to work on.
11199 *
11200 * This function updates the receive sequence time stamp for this vport. The
11201 * receive sequence time stamp indicates the time that the last frame of the
11202 * the sequence that has been idle for the longest amount of time was received.
11203 * the driver uses this time stamp to indicate if any received sequences have
11204 * timed out.
11205 **/
11206void
11207lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11208{
11209 struct lpfc_dmabuf *h_buf;
11210 struct hbq_dmabuf *dmabuf = NULL;
11211
11212 /* get the oldest sequence on the rcv list */
11213 h_buf = list_get_first(&vport->rcv_buffer_list,
11214 struct lpfc_dmabuf, list);
11215 if (!h_buf)
11216 return;
11217 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11218 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11219}
11220
11221/**
11222 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11223 * @vport: The vport that the received sequences were sent to.
11224 *
11225 * This function cleans up all outstanding received sequences. This is called
11226 * by the driver when a link event or user action invalidates all the received
11227 * sequences.
11228 **/
11229void
11230lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11231{
11232 struct lpfc_dmabuf *h_buf, *hnext;
11233 struct lpfc_dmabuf *d_buf, *dnext;
11234 struct hbq_dmabuf *dmabuf = NULL;
11235
11236 /* start with the oldest sequence on the rcv list */
11237 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11238 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11239 list_del_init(&dmabuf->hbuf.list);
11240 list_for_each_entry_safe(d_buf, dnext,
11241 &dmabuf->dbuf.list, list) {
11242 list_del_init(&d_buf->list);
11243 lpfc_in_buf_free(vport->phba, d_buf);
11244 }
11245 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11246 }
11247}
11248
11249/**
11250 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11251 * @vport: The vport that the received sequences were sent to.
11252 *
11253 * This function determines whether any received sequences have timed out by
11254 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11255 * indicates that there is at least one timed out sequence this routine will
11256 * go through the received sequences one at a time from most inactive to most
11257 * active to determine which ones need to be cleaned up. Once it has determined
11258 * that a sequence needs to be cleaned up it will simply free up the resources
11259 * without sending an abort.
11260 **/
11261void
11262lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11263{
11264 struct lpfc_dmabuf *h_buf, *hnext;
11265 struct lpfc_dmabuf *d_buf, *dnext;
11266 struct hbq_dmabuf *dmabuf = NULL;
11267 unsigned long timeout;
11268 int abort_count = 0;
11269
11270 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11271 vport->rcv_buffer_time_stamp);
11272 if (list_empty(&vport->rcv_buffer_list) ||
11273 time_before(jiffies, timeout))
11274 return;
11275 /* start with the oldest sequence on the rcv list */
11276 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11277 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11278 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11279 dmabuf->time_stamp);
11280 if (time_before(jiffies, timeout))
11281 break;
11282 abort_count++;
11283 list_del_init(&dmabuf->hbuf.list);
11284 list_for_each_entry_safe(d_buf, dnext,
11285 &dmabuf->dbuf.list, list) {
11286 list_del_init(&d_buf->list);
11287 lpfc_in_buf_free(vport->phba, d_buf);
11288 }
11289 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11290 }
11291 if (abort_count)
11292 lpfc_update_rcv_time_stamp(vport);
11293}
11294
4f774513
JS
11295/**
11296 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11297 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11298 *
11299 * This function searches through the existing incomplete sequences that have
11300 * been sent to this @vport. If the frame matches one of the incomplete
11301 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11302 * make up that sequence. If no sequence is found that matches this frame then
11303 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11304 * This function returns a pointer to the first dmabuf in the sequence list that
11305 * the frame was linked to.
11306 **/
11307static struct hbq_dmabuf *
11308lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11309{
11310 struct fc_frame_header *new_hdr;
11311 struct fc_frame_header *temp_hdr;
11312 struct lpfc_dmabuf *d_buf;
11313 struct lpfc_dmabuf *h_buf;
11314 struct hbq_dmabuf *seq_dmabuf = NULL;
11315 struct hbq_dmabuf *temp_dmabuf = NULL;
11316
4d9ab994 11317 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 11318 dmabuf->time_stamp = jiffies;
4f774513
JS
11319 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11320 /* Use the hdr_buf to find the sequence that this frame belongs to */
11321 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11322 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11323 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11324 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11325 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11326 continue;
11327 /* found a pending sequence that matches this frame */
11328 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11329 break;
11330 }
11331 if (!seq_dmabuf) {
11332 /*
11333 * This indicates first frame received for this sequence.
11334 * Queue the buffer on the vport's rcv_buffer_list.
11335 */
11336 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 11337 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11338 return dmabuf;
11339 }
11340 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
11341 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11342 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
11343 list_del_init(&seq_dmabuf->hbuf.list);
11344 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11345 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 11346 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11347 return dmabuf;
11348 }
45ed1190
JS
11349 /* move this sequence to the tail to indicate a young sequence */
11350 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11351 seq_dmabuf->time_stamp = jiffies;
11352 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
11353 if (list_empty(&seq_dmabuf->dbuf.list)) {
11354 temp_hdr = dmabuf->hbuf.virt;
11355 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11356 return seq_dmabuf;
11357 }
4f774513
JS
11358 /* find the correct place in the sequence to insert this frame */
11359 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11360 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11361 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11362 /*
11363 * If the frame's sequence count is greater than the frame on
11364 * the list then insert the frame right after this frame
11365 */
eeead811
JS
11366 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11367 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
11368 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11369 return seq_dmabuf;
11370 }
11371 }
11372 return NULL;
11373}
11374
6669f9bb
JS
11375/**
11376 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11377 * @vport: pointer to a vitural port
11378 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11379 *
11380 * This function tries to abort from the partially assembed sequence, described
11381 * by the information from basic abbort @dmabuf. It checks to see whether such
11382 * partially assembled sequence held by the driver. If so, it shall free up all
11383 * the frames from the partially assembled sequence.
11384 *
11385 * Return
11386 * true -- if there is matching partially assembled sequence present and all
11387 * the frames freed with the sequence;
11388 * false -- if there is no matching partially assembled sequence present so
11389 * nothing got aborted in the lower layer driver
11390 **/
11391static bool
11392lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11393 struct hbq_dmabuf *dmabuf)
11394{
11395 struct fc_frame_header *new_hdr;
11396 struct fc_frame_header *temp_hdr;
11397 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11398 struct hbq_dmabuf *seq_dmabuf = NULL;
11399
11400 /* Use the hdr_buf to find the sequence that matches this frame */
11401 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11402 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11403 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11404 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11405 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11406 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11407 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11408 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11409 continue;
11410 /* found a pending sequence that matches this frame */
11411 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11412 break;
11413 }
11414
11415 /* Free up all the frames from the partially assembled sequence */
11416 if (seq_dmabuf) {
11417 list_for_each_entry_safe(d_buf, n_buf,
11418 &seq_dmabuf->dbuf.list, list) {
11419 list_del_init(&d_buf->list);
11420 lpfc_in_buf_free(vport->phba, d_buf);
11421 }
11422 return true;
11423 }
11424 return false;
11425}
11426
11427/**
11428 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11429 * @phba: Pointer to HBA context object.
11430 * @cmd_iocbq: pointer to the command iocbq structure.
11431 * @rsp_iocbq: pointer to the response iocbq structure.
11432 *
11433 * This function handles the sequence abort accept iocb command complete
11434 * event. It properly releases the memory allocated to the sequence abort
11435 * accept iocb.
11436 **/
11437static void
11438lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11439 struct lpfc_iocbq *cmd_iocbq,
11440 struct lpfc_iocbq *rsp_iocbq)
11441{
11442 if (cmd_iocbq)
11443 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11444}
11445
11446/**
11447 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11448 * @phba: Pointer to HBA context object.
11449 * @fc_hdr: pointer to a FC frame header.
11450 *
11451 * This function sends a basic accept to a previous unsol sequence abort
11452 * event after aborting the sequence handling.
11453 **/
11454static void
11455lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11456 struct fc_frame_header *fc_hdr)
11457{
11458 struct lpfc_iocbq *ctiocb = NULL;
11459 struct lpfc_nodelist *ndlp;
5ffc266e
JS
11460 uint16_t oxid, rxid;
11461 uint32_t sid, fctl;
6669f9bb
JS
11462 IOCB_t *icmd;
11463
11464 if (!lpfc_is_link_up(phba))
11465 return;
11466
11467 sid = sli4_sid_from_fc_hdr(fc_hdr);
11468 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 11469 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
11470
11471 ndlp = lpfc_findnode_did(phba->pport, sid);
11472 if (!ndlp) {
11473 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11474 "1268 Find ndlp returned NULL for oxid:x%x "
11475 "SID:x%x\n", oxid, sid);
11476 return;
11477 }
11478
11479 /* Allocate buffer for acc iocb */
11480 ctiocb = lpfc_sli_get_iocbq(phba);
11481 if (!ctiocb)
11482 return;
11483
5ffc266e
JS
11484 /* Extract the F_CTL field from FC_HDR */
11485 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11486
6669f9bb 11487 icmd = &ctiocb->iocb;
6669f9bb 11488 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 11489 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
11490 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11491 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11492 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11493
11494 /* Fill in the rest of iocb fields */
11495 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11496 icmd->ulpBdeCount = 0;
11497 icmd->ulpLe = 1;
11498 icmd->ulpClass = CLASS3;
11499 icmd->ulpContext = ndlp->nlp_rpi;
6669f9bb 11500
6669f9bb
JS
11501 ctiocb->iocb_cmpl = NULL;
11502 ctiocb->vport = phba->pport;
11503 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11504
5ffc266e
JS
11505 if (fctl & FC_FC_EX_CTX) {
11506 /* ABTS sent by responder to CT exchange, construction
11507 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11508 * field and RX_ID from ABTS for RX_ID field.
11509 */
11510 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11511 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11512 ctiocb->sli4_xritag = oxid;
11513 } else {
11514 /* ABTS sent by initiator to CT exchange, construction
11515 * of BA_ACC will need to allocate a new XRI as for the
11516 * XRI_TAG and RX_ID fields.
11517 */
11518 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11519 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11520 ctiocb->sli4_xritag = NO_XRI;
11521 }
11522 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11523
6669f9bb
JS
11524 /* Xmit CT abts accept on exchange <xid> */
11525 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11526 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11527 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11528 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11529}
11530
11531/**
11532 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11533 * @vport: Pointer to the vport on which this sequence was received
11534 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11535 *
11536 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11537 * receive sequence is only partially assembed by the driver, it shall abort
11538 * the partially assembled frames for the sequence. Otherwise, if the
11539 * unsolicited receive sequence has been completely assembled and passed to
11540 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11541 * unsolicited sequence has been aborted. After that, it will issue a basic
11542 * accept to accept the abort.
11543 **/
11544void
11545lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11546 struct hbq_dmabuf *dmabuf)
11547{
11548 struct lpfc_hba *phba = vport->phba;
11549 struct fc_frame_header fc_hdr;
5ffc266e 11550 uint32_t fctl;
6669f9bb
JS
11551 bool abts_par;
11552
6669f9bb
JS
11553 /* Make a copy of fc_hdr before the dmabuf being released */
11554 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 11555 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 11556
5ffc266e
JS
11557 if (fctl & FC_FC_EX_CTX) {
11558 /*
11559 * ABTS sent by responder to exchange, just free the buffer
11560 */
6669f9bb 11561 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
11562 } else {
11563 /*
11564 * ABTS sent by initiator to exchange, need to do cleanup
11565 */
11566 /* Try to abort partially assembled seq */
11567 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11568
11569 /* Send abort to ULP if partially seq abort failed */
11570 if (abts_par == false)
11571 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11572 else
11573 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11574 }
6669f9bb
JS
11575 /* Send basic accept (BA_ACC) to the abort requester */
11576 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11577}
11578
4f774513
JS
11579/**
11580 * lpfc_seq_complete - Indicates if a sequence is complete
11581 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11582 *
11583 * This function checks the sequence, starting with the frame described by
11584 * @dmabuf, to see if all the frames associated with this sequence are present.
11585 * the frames associated with this sequence are linked to the @dmabuf using the
11586 * dbuf list. This function looks for two major things. 1) That the first frame
11587 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11588 * set. 3) That there are no holes in the sequence count. The function will
11589 * return 1 when the sequence is complete, otherwise it will return 0.
11590 **/
11591static int
11592lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11593{
11594 struct fc_frame_header *hdr;
11595 struct lpfc_dmabuf *d_buf;
11596 struct hbq_dmabuf *seq_dmabuf;
11597 uint32_t fctl;
11598 int seq_count = 0;
11599
11600 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11601 /* make sure first fame of sequence has a sequence count of zero */
11602 if (hdr->fh_seq_cnt != seq_count)
11603 return 0;
11604 fctl = (hdr->fh_f_ctl[0] << 16 |
11605 hdr->fh_f_ctl[1] << 8 |
11606 hdr->fh_f_ctl[2]);
11607 /* If last frame of sequence we can return success. */
11608 if (fctl & FC_FC_END_SEQ)
11609 return 1;
11610 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11611 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11612 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11613 /* If there is a hole in the sequence count then fail. */
eeead811 11614 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
11615 return 0;
11616 fctl = (hdr->fh_f_ctl[0] << 16 |
11617 hdr->fh_f_ctl[1] << 8 |
11618 hdr->fh_f_ctl[2]);
11619 /* If last frame of sequence we can return success. */
11620 if (fctl & FC_FC_END_SEQ)
11621 return 1;
11622 }
11623 return 0;
11624}
11625
11626/**
11627 * lpfc_prep_seq - Prep sequence for ULP processing
11628 * @vport: Pointer to the vport on which this sequence was received
11629 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11630 *
11631 * This function takes a sequence, described by a list of frames, and creates
11632 * a list of iocbq structures to describe the sequence. This iocbq list will be
11633 * used to issue to the generic unsolicited sequence handler. This routine
11634 * returns a pointer to the first iocbq in the list. If the function is unable
11635 * to allocate an iocbq then it throw out the received frames that were not
11636 * able to be described and return a pointer to the first iocbq. If unable to
11637 * allocate any iocbqs (including the first) this function will return NULL.
11638 **/
11639static struct lpfc_iocbq *
11640lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11641{
11642 struct lpfc_dmabuf *d_buf, *n_buf;
11643 struct lpfc_iocbq *first_iocbq, *iocbq;
11644 struct fc_frame_header *fc_hdr;
11645 uint32_t sid;
eeead811 11646 struct ulp_bde64 *pbde;
4f774513
JS
11647
11648 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11649 /* remove from receive buffer list */
11650 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 11651 lpfc_update_rcv_time_stamp(vport);
4f774513 11652 /* get the Remote Port's SID */
6669f9bb 11653 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11654 /* Get an iocbq struct to fill in. */
11655 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11656 if (first_iocbq) {
11657 /* Initialize the first IOCB. */
8fa38513 11658 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11659 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11660 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11661 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11662 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11663 vport->vpi + vport->phba->vpi_base;
11664 /* put the first buffer into the first IOCBq */
11665 first_iocbq->context2 = &seq_dmabuf->dbuf;
11666 first_iocbq->context3 = NULL;
11667 first_iocbq->iocb.ulpBdeCount = 1;
11668 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11669 LPFC_DATA_BUF_SIZE;
11670 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11671 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11672 bf_get(lpfc_rcqe_length,
11673 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11674 }
11675 iocbq = first_iocbq;
11676 /*
11677 * Each IOCBq can have two Buffers assigned, so go through the list
11678 * of buffers for this sequence and save two buffers in each IOCBq
11679 */
11680 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11681 if (!iocbq) {
11682 lpfc_in_buf_free(vport->phba, d_buf);
11683 continue;
11684 }
11685 if (!iocbq->context3) {
11686 iocbq->context3 = d_buf;
11687 iocbq->iocb.ulpBdeCount++;
eeead811
JS
11688 pbde = (struct ulp_bde64 *)
11689 &iocbq->iocb.unsli3.sli3Words[4];
11690 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
8fa38513 11691 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11692 bf_get(lpfc_rcqe_length,
11693 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11694 } else {
11695 iocbq = lpfc_sli_get_iocbq(vport->phba);
11696 if (!iocbq) {
11697 if (first_iocbq) {
11698 first_iocbq->iocb.ulpStatus =
11699 IOSTAT_FCP_RSP_ERROR;
11700 first_iocbq->iocb.un.ulpWord[4] =
11701 IOERR_NO_RESOURCES;
11702 }
11703 lpfc_in_buf_free(vport->phba, d_buf);
11704 continue;
11705 }
11706 iocbq->context2 = d_buf;
11707 iocbq->context3 = NULL;
11708 iocbq->iocb.ulpBdeCount = 1;
11709 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11710 LPFC_DATA_BUF_SIZE;
8fa38513 11711 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11712 bf_get(lpfc_rcqe_length,
11713 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11714 iocbq->iocb.un.rcvels.remoteID = sid;
11715 list_add_tail(&iocbq->list, &first_iocbq->list);
11716 }
11717 }
11718 return first_iocbq;
11719}
11720
6669f9bb
JS
11721static void
11722lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11723 struct hbq_dmabuf *seq_dmabuf)
11724{
11725 struct fc_frame_header *fc_hdr;
11726 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11727 struct lpfc_hba *phba = vport->phba;
11728
11729 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11730 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11731 if (!iocbq) {
11732 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11733 "2707 Ring %d handler: Failed to allocate "
11734 "iocb Rctl x%x Type x%x received\n",
11735 LPFC_ELS_RING,
11736 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11737 return;
11738 }
11739 if (!lpfc_complete_unsol_iocb(phba,
11740 &phba->sli.ring[LPFC_ELS_RING],
11741 iocbq, fc_hdr->fh_r_ctl,
11742 fc_hdr->fh_type))
11743 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11744 "2540 Ring %d handler: unexpected Rctl "
11745 "x%x Type x%x received\n",
11746 LPFC_ELS_RING,
11747 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11748
11749 /* Free iocb created in lpfc_prep_seq */
11750 list_for_each_entry_safe(curr_iocb, next_iocb,
11751 &iocbq->list, list) {
11752 list_del_init(&curr_iocb->list);
11753 lpfc_sli_release_iocbq(phba, curr_iocb);
11754 }
11755 lpfc_sli_release_iocbq(phba, iocbq);
11756}
11757
4f774513
JS
11758/**
11759 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11760 * @phba: Pointer to HBA context object.
11761 *
11762 * This function is called with no lock held. This function processes all
11763 * the received buffers and gives it to upper layers when a received buffer
11764 * indicates that it is the final frame in the sequence. The interrupt
11765 * service routine processes received buffers at interrupt contexts and adds
11766 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11767 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11768 * appropriate receive function when the final frame in a sequence is received.
11769 **/
4d9ab994
JS
11770void
11771lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11772 struct hbq_dmabuf *dmabuf)
4f774513 11773{
4d9ab994 11774 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11775 struct fc_frame_header *fc_hdr;
11776 struct lpfc_vport *vport;
11777 uint32_t fcfi;
4f774513 11778
4f774513 11779 /* Process each received buffer */
4d9ab994
JS
11780 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11781 /* check to see if this a valid type of frame */
11782 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11783 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11784 return;
11785 }
11786 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11787 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 11788 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
11789 /* throw out the frame */
11790 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11791 return;
11792 }
6669f9bb
JS
11793 /* Handle the basic abort sequence (BA_ABTS) event */
11794 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11795 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11796 return;
11797 }
11798
4d9ab994
JS
11799 /* Link this frame */
11800 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11801 if (!seq_dmabuf) {
11802 /* unable to add frame to vport - throw it out */
11803 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11804 return;
11805 }
11806 /* If not last frame in sequence continue processing frames. */
def9c7a9 11807 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 11808 return;
def9c7a9 11809
6669f9bb
JS
11810 /* Send the complete sequence to the upper layer protocol */
11811 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11812}
6fb120a7
JS
11813
11814/**
11815 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11816 * @phba: pointer to lpfc hba data structure.
11817 *
11818 * This routine is invoked to post rpi header templates to the
11819 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
11820 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11821 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
11822 *
11823 * This routine does not require any locks. It's usage is expected
11824 * to be driver load or reset recovery when the driver is
11825 * sequential.
11826 *
11827 * Return codes
af901ca1 11828 * 0 - successful
d439d286 11829 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
11830 * When this error occurs, the driver is not guaranteed
11831 * to have any rpi regions posted to the device and
11832 * must either attempt to repost the regions or take a
11833 * fatal error.
11834 **/
11835int
11836lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11837{
11838 struct lpfc_rpi_hdr *rpi_page;
11839 uint32_t rc = 0;
11840
11841 /* Post all rpi memory regions to the port. */
11842 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11843 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11844 if (rc != MBX_SUCCESS) {
11845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11846 "2008 Error %d posting all rpi "
11847 "headers\n", rc);
11848 rc = -EIO;
11849 break;
11850 }
11851 }
11852
11853 return rc;
11854}
11855
11856/**
11857 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11858 * @phba: pointer to lpfc hba data structure.
11859 * @rpi_page: pointer to the rpi memory region.
11860 *
11861 * This routine is invoked to post a single rpi header to the
11862 * HBA consistent with the SLI-4 interface spec. This memory region
11863 * maps up to 64 rpi context regions.
11864 *
11865 * Return codes
af901ca1 11866 * 0 - successful
d439d286
JS
11867 * -ENOMEM - No available memory
11868 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
11869 **/
11870int
11871lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11872{
11873 LPFC_MBOXQ_t *mboxq;
11874 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11875 uint32_t rc = 0;
11876 uint32_t mbox_tmo;
11877 uint32_t shdr_status, shdr_add_status;
11878 union lpfc_sli4_cfg_shdr *shdr;
11879
11880 /* The port is notified of the header region via a mailbox command. */
11881 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11882 if (!mboxq) {
11883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11884 "2001 Unable to allocate memory for issuing "
11885 "SLI_CONFIG_SPECIAL mailbox command\n");
11886 return -ENOMEM;
11887 }
11888
11889 /* Post all rpi memory regions to the port. */
11890 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11891 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11892 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11893 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11894 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11895 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11896 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11897 hdr_tmpl, rpi_page->page_count);
11898 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11899 rpi_page->start_rpi);
11900 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11901 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11902 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11903 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11904 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11905 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11906 if (rc != MBX_TIMEOUT)
11907 mempool_free(mboxq, phba->mbox_mem_pool);
11908 if (shdr_status || shdr_add_status || rc) {
11909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11910 "2514 POST_RPI_HDR mailbox failed with "
11911 "status x%x add_status x%x, mbx status x%x\n",
11912 shdr_status, shdr_add_status, rc);
11913 rc = -ENXIO;
11914 }
11915 return rc;
11916}
11917
11918/**
11919 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11920 * @phba: pointer to lpfc hba data structure.
11921 *
11922 * This routine is invoked to post rpi header templates to the
11923 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
11924 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11925 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
11926 *
11927 * Returns
af901ca1 11928 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
11929 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11930 **/
11931int
11932lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11933{
11934 int rpi;
11935 uint16_t max_rpi, rpi_base, rpi_limit;
11936 uint16_t rpi_remaining;
11937 struct lpfc_rpi_hdr *rpi_hdr;
11938
11939 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11940 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11941 rpi_limit = phba->sli4_hba.next_rpi;
11942
11943 /*
11944 * The valid rpi range is not guaranteed to be zero-based. Start
11945 * the search at the rpi_base as reported by the port.
11946 */
11947 spin_lock_irq(&phba->hbalock);
11948 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11949 if (rpi >= rpi_limit || rpi < rpi_base)
11950 rpi = LPFC_RPI_ALLOC_ERROR;
11951 else {
11952 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11953 phba->sli4_hba.max_cfg_param.rpi_used++;
11954 phba->sli4_hba.rpi_count++;
11955 }
11956
11957 /*
11958 * Don't try to allocate more rpi header regions if the device limit
11959 * on available rpis max has been exhausted.
11960 */
11961 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11962 (phba->sli4_hba.rpi_count >= max_rpi)) {
11963 spin_unlock_irq(&phba->hbalock);
11964 return rpi;
11965 }
11966
11967 /*
11968 * If the driver is running low on rpi resources, allocate another
11969 * page now. Note that the next_rpi value is used because
11970 * it represents how many are actually in use whereas max_rpi notes
11971 * how many are supported max by the device.
11972 */
11973 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11974 phba->sli4_hba.rpi_count;
11975 spin_unlock_irq(&phba->hbalock);
11976 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11977 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11978 if (!rpi_hdr) {
11979 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11980 "2002 Error Could not grow rpi "
11981 "count\n");
11982 } else {
11983 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11984 }
11985 }
11986
11987 return rpi;
11988}
11989
d7c47992
JS
11990/**
11991 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11992 * @phba: pointer to lpfc hba data structure.
11993 *
11994 * This routine is invoked to release an rpi to the pool of
11995 * available rpis maintained by the driver.
11996 **/
11997void
11998__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11999{
12000 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12001 phba->sli4_hba.rpi_count--;
12002 phba->sli4_hba.max_cfg_param.rpi_used--;
12003 }
12004}
12005
6fb120a7
JS
12006/**
12007 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12008 * @phba: pointer to lpfc hba data structure.
12009 *
12010 * This routine is invoked to release an rpi to the pool of
12011 * available rpis maintained by the driver.
12012 **/
12013void
12014lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12015{
12016 spin_lock_irq(&phba->hbalock);
d7c47992 12017 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
12018 spin_unlock_irq(&phba->hbalock);
12019}
12020
12021/**
12022 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12023 * @phba: pointer to lpfc hba data structure.
12024 *
12025 * This routine is invoked to remove the memory region that
12026 * provided rpi via a bitmask.
12027 **/
12028void
12029lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12030{
12031 kfree(phba->sli4_hba.rpi_bmask);
12032}
12033
12034/**
12035 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12036 * @phba: pointer to lpfc hba data structure.
12037 *
12038 * This routine is invoked to remove the memory region that
12039 * provided rpi via a bitmask.
12040 **/
12041int
12042lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12043{
12044 LPFC_MBOXQ_t *mboxq;
12045 struct lpfc_hba *phba = ndlp->phba;
12046 int rc;
12047
12048 /* The port is notified of the header region via a mailbox command. */
12049 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12050 if (!mboxq)
12051 return -ENOMEM;
12052
12053 /* Post all rpi memory regions to the port. */
12054 lpfc_resume_rpi(mboxq, ndlp);
12055 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12056 if (rc == MBX_NOT_FINISHED) {
12057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12058 "2010 Resume RPI Mailbox failed "
12059 "status %d, mbxStatus x%x\n", rc,
12060 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12061 mempool_free(mboxq, phba->mbox_mem_pool);
12062 return -EIO;
12063 }
12064 return 0;
12065}
12066
12067/**
12068 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12069 * @phba: pointer to lpfc hba data structure.
12070 * @vpi: vpi value to activate with the port.
12071 *
12072 * This routine is invoked to activate a vpi with the
12073 * port when the host intends to use vports with a
12074 * nonzero vpi.
12075 *
12076 * Returns:
12077 * 0 success
12078 * -Evalue otherwise
12079 **/
12080int
12081lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12082{
12083 LPFC_MBOXQ_t *mboxq;
12084 int rc = 0;
6a9c52cf 12085 int retval = MBX_SUCCESS;
6fb120a7
JS
12086 uint32_t mbox_tmo;
12087
12088 if (vpi == 0)
12089 return -EINVAL;
12090 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12091 if (!mboxq)
12092 return -ENOMEM;
1c6834a7 12093 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
12094 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12095 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
12096 if (rc != MBX_SUCCESS) {
12097 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12098 "2022 INIT VPI Mailbox failed "
12099 "status %d, mbxStatus x%x\n", rc,
12100 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 12101 retval = -EIO;
6fb120a7 12102 }
6a9c52cf
JS
12103 if (rc != MBX_TIMEOUT)
12104 mempool_free(mboxq, phba->mbox_mem_pool);
12105
12106 return retval;
6fb120a7
JS
12107}
12108
12109/**
12110 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12111 * @phba: pointer to lpfc hba data structure.
12112 * @mboxq: Pointer to mailbox object.
12113 *
12114 * This routine is invoked to manually add a single FCF record. The caller
12115 * must pass a completely initialized FCF_Record. This routine takes
12116 * care of the nonembedded mailbox operations.
12117 **/
12118static void
12119lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12120{
12121 void *virt_addr;
12122 union lpfc_sli4_cfg_shdr *shdr;
12123 uint32_t shdr_status, shdr_add_status;
12124
12125 virt_addr = mboxq->sge_array->addr[0];
12126 /* The IOCTL status is embedded in the mailbox subheader. */
12127 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12128 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12129 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12130
12131 if ((shdr_status || shdr_add_status) &&
12132 (shdr_status != STATUS_FCF_IN_USE))
12133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12134 "2558 ADD_FCF_RECORD mailbox failed with "
12135 "status x%x add_status x%x\n",
12136 shdr_status, shdr_add_status);
12137
12138 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12139}
12140
12141/**
12142 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12143 * @phba: pointer to lpfc hba data structure.
12144 * @fcf_record: pointer to the initialized fcf record to add.
12145 *
12146 * This routine is invoked to manually add a single FCF record. The caller
12147 * must pass a completely initialized FCF_Record. This routine takes
12148 * care of the nonembedded mailbox operations.
12149 **/
12150int
12151lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12152{
12153 int rc = 0;
12154 LPFC_MBOXQ_t *mboxq;
12155 uint8_t *bytep;
12156 void *virt_addr;
12157 dma_addr_t phys_addr;
12158 struct lpfc_mbx_sge sge;
12159 uint32_t alloc_len, req_len;
12160 uint32_t fcfindex;
12161
12162 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12163 if (!mboxq) {
12164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12165 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12166 return -ENOMEM;
12167 }
12168
12169 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12170 sizeof(uint32_t);
12171
12172 /* Allocate DMA memory and set up the non-embedded mailbox command */
12173 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12174 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12175 req_len, LPFC_SLI4_MBX_NEMBED);
12176 if (alloc_len < req_len) {
12177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12178 "2523 Allocated DMA memory size (x%x) is "
12179 "less than the requested DMA memory "
12180 "size (x%x)\n", alloc_len, req_len);
12181 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12182 return -ENOMEM;
12183 }
12184
12185 /*
12186 * Get the first SGE entry from the non-embedded DMA memory. This
12187 * routine only uses a single SGE.
12188 */
12189 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12190 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
12191 virt_addr = mboxq->sge_array->addr[0];
12192 /*
12193 * Configure the FCF record for FCFI 0. This is the driver's
12194 * hardcoded default and gets used in nonFIP mode.
12195 */
12196 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12197 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12198 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12199
12200 /*
12201 * Copy the fcf_index and the FCF Record Data. The data starts after
12202 * the FCoE header plus word10. The data copy needs to be endian
12203 * correct.
12204 */
12205 bytep += sizeof(uint32_t);
12206 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12207 mboxq->vport = phba->pport;
12208 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12209 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12210 if (rc == MBX_NOT_FINISHED) {
12211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12212 "2515 ADD_FCF_RECORD mailbox failed with "
12213 "status 0x%x\n", rc);
12214 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12215 rc = -EIO;
12216 } else
12217 rc = 0;
12218
12219 return rc;
12220}
12221
12222/**
12223 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12224 * @phba: pointer to lpfc hba data structure.
12225 * @fcf_record: pointer to the fcf record to write the default data.
12226 * @fcf_index: FCF table entry index.
12227 *
12228 * This routine is invoked to build the driver's default FCF record. The
12229 * values used are hardcoded. This routine handles memory initialization.
12230 *
12231 **/
12232void
12233lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12234 struct fcf_record *fcf_record,
12235 uint16_t fcf_index)
12236{
12237 memset(fcf_record, 0, sizeof(struct fcf_record));
12238 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12239 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12240 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12241 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12242 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12243 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12244 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12245 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12246 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12247 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12248 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12249 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12250 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 12251 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
12252 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12253 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12254 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12255 /* Set the VLAN bit map */
12256 if (phba->valid_vlan) {
12257 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12258 = 1 << (phba->vlan_id % 8);
12259 }
12260}
12261
12262/**
0c9ab6f5 12263 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
12264 * @phba: pointer to lpfc hba data structure.
12265 * @fcf_index: FCF table entry offset.
12266 *
0c9ab6f5
JS
12267 * This routine is invoked to scan the entire FCF table by reading FCF
12268 * record and processing it one at a time starting from the @fcf_index
12269 * for initial FCF discovery or fast FCF failover rediscovery.
12270 *
12271 * Return 0 if the mailbox command is submitted sucessfully, none 0
12272 * otherwise.
6fb120a7
JS
12273 **/
12274int
0c9ab6f5 12275lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
12276{
12277 int rc = 0, error;
12278 LPFC_MBOXQ_t *mboxq;
6fb120a7 12279
32b9793f 12280 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
12281 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12282 if (!mboxq) {
12283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12284 "2000 Failed to allocate mbox for "
12285 "READ_FCF cmd\n");
4d9ab994 12286 error = -ENOMEM;
0c9ab6f5 12287 goto fail_fcf_scan;
6fb120a7 12288 }
ecfd03c6 12289 /* Construct the read FCF record mailbox command */
0c9ab6f5 12290 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
12291 if (rc) {
12292 error = -EINVAL;
0c9ab6f5 12293 goto fail_fcf_scan;
6fb120a7 12294 }
ecfd03c6 12295 /* Issue the mailbox command asynchronously */
6fb120a7 12296 mboxq->vport = phba->pport;
0c9ab6f5 12297 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
6fb120a7 12298 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 12299 if (rc == MBX_NOT_FINISHED)
6fb120a7 12300 error = -EIO;
ecfd03c6 12301 else {
32b9793f
JS
12302 spin_lock_irq(&phba->hbalock);
12303 phba->hba_flag |= FCF_DISC_INPROGRESS;
12304 spin_unlock_irq(&phba->hbalock);
38b92ef8
JS
12305 /* Reset eligible FCF count for new scan */
12306 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 12307 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 12308 error = 0;
32b9793f 12309 }
0c9ab6f5 12310fail_fcf_scan:
4d9ab994
JS
12311 if (error) {
12312 if (mboxq)
12313 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12314 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12315 spin_lock_irq(&phba->hbalock);
12316 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12317 spin_unlock_irq(&phba->hbalock);
12318 }
6fb120a7
JS
12319 return error;
12320}
a0c87cbd 12321
0c9ab6f5
JS
12322/**
12323 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12324 * @phba: pointer to lpfc hba data structure.
12325 * @fcf_index: FCF table entry offset.
12326 *
12327 * This routine is invoked to read an FCF record indicated by @fcf_index
12328 * and to use it for FLOGI round robin FCF failover.
12329 *
12330 * Return 0 if the mailbox command is submitted sucessfully, none 0
12331 * otherwise.
12332 **/
12333int
12334lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12335{
12336 int rc = 0, error;
12337 LPFC_MBOXQ_t *mboxq;
12338
12339 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12340 if (!mboxq) {
12341 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12342 "2763 Failed to allocate mbox for "
12343 "READ_FCF cmd\n");
12344 error = -ENOMEM;
12345 goto fail_fcf_read;
12346 }
12347 /* Construct the read FCF record mailbox command */
12348 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12349 if (rc) {
12350 error = -EINVAL;
12351 goto fail_fcf_read;
12352 }
12353 /* Issue the mailbox command asynchronously */
12354 mboxq->vport = phba->pport;
12355 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12356 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12357 if (rc == MBX_NOT_FINISHED)
12358 error = -EIO;
12359 else
12360 error = 0;
12361
12362fail_fcf_read:
12363 if (error && mboxq)
12364 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12365 return error;
12366}
12367
12368/**
12369 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12370 * @phba: pointer to lpfc hba data structure.
12371 * @fcf_index: FCF table entry offset.
12372 *
12373 * This routine is invoked to read an FCF record indicated by @fcf_index to
12374 * determine whether it's eligible for FLOGI round robin failover list.
12375 *
12376 * Return 0 if the mailbox command is submitted sucessfully, none 0
12377 * otherwise.
12378 **/
12379int
12380lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12381{
12382 int rc = 0, error;
12383 LPFC_MBOXQ_t *mboxq;
12384
12385 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12386 if (!mboxq) {
12387 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12388 "2758 Failed to allocate mbox for "
12389 "READ_FCF cmd\n");
12390 error = -ENOMEM;
12391 goto fail_fcf_read;
12392 }
12393 /* Construct the read FCF record mailbox command */
12394 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12395 if (rc) {
12396 error = -EINVAL;
12397 goto fail_fcf_read;
12398 }
12399 /* Issue the mailbox command asynchronously */
12400 mboxq->vport = phba->pport;
12401 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12402 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12403 if (rc == MBX_NOT_FINISHED)
12404 error = -EIO;
12405 else
12406 error = 0;
12407
12408fail_fcf_read:
12409 if (error && mboxq)
12410 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12411 return error;
12412}
12413
12414/**
12415 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12416 * @phba: pointer to lpfc hba data structure.
12417 *
12418 * This routine is to get the next eligible FCF record index in a round
12419 * robin fashion. If the next eligible FCF record index equals to the
12420 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12421 * shall be returned, otherwise, the next eligible FCF record's index
12422 * shall be returned.
12423 **/
12424uint16_t
12425lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12426{
12427 uint16_t next_fcf_index;
12428
3804dc84
JS
12429 /* Search start from next bit of currently registered FCF index */
12430 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12431 LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
12432 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12433 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
12434 next_fcf_index);
12435
0c9ab6f5
JS
12436 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12437 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12438 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12439 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
3804dc84
JS
12440
12441 /* Check roundrobin failover list empty condition */
12442 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12443 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12444 "2844 No roundrobin failover FCF available\n");
0c9ab6f5 12445 return LPFC_FCOE_FCF_NEXT_NONE;
3804dc84
JS
12446 }
12447
12448 /* Check roundrobin failover index bmask stop condition */
12449 if (next_fcf_index == phba->fcf.fcf_rr_init_indx) {
12450 if (!(phba->fcf.fcf_flag & FCF_REDISC_RRU)) {
12451 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12452 "2847 Round robin failover FCF index "
12453 "search hit stop condition:x%x\n",
12454 next_fcf_index);
12455 return LPFC_FCOE_FCF_NEXT_NONE;
12456 }
12457 /* The roundrobin failover index bmask updated, start over */
12458 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12459 "2848 Round robin failover FCF index bmask "
12460 "updated, start over\n");
12461 spin_lock_irq(&phba->hbalock);
12462 phba->fcf.fcf_flag &= ~FCF_REDISC_RRU;
12463 spin_unlock_irq(&phba->hbalock);
12464 return phba->fcf.fcf_rr_init_indx;
12465 }
0c9ab6f5 12466
3804dc84
JS
12467 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12468 "2845 Get next round robin failover "
12469 "FCF index x%x\n", next_fcf_index);
0c9ab6f5
JS
12470 return next_fcf_index;
12471}
12472
12473/**
12474 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12475 * @phba: pointer to lpfc hba data structure.
12476 *
12477 * This routine sets the FCF record index in to the eligible bmask for
12478 * round robin failover search. It checks to make sure that the index
12479 * does not go beyond the range of the driver allocated bmask dimension
12480 * before setting the bit.
12481 *
12482 * Returns 0 if the index bit successfully set, otherwise, it returns
12483 * -EINVAL.
12484 **/
12485int
12486lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12487{
12488 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12489 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12490 "2610 HBA FCF index reached driver's "
12491 "book keeping dimension: fcf_index:%d, "
12492 "driver_bmask_max:%d\n",
12493 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12494 return -EINVAL;
12495 }
12496 /* Set the eligible FCF record index bmask */
12497 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12498
3804dc84
JS
12499 /* Set the roundrobin index bmask updated */
12500 spin_lock_irq(&phba->hbalock);
12501 phba->fcf.fcf_flag |= FCF_REDISC_RRU;
12502 spin_unlock_irq(&phba->hbalock);
12503
12504 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12505 "2790 Set FCF index x%x to round robin failover "
12506 "bmask\n", fcf_index);
12507
0c9ab6f5
JS
12508 return 0;
12509}
12510
12511/**
3804dc84 12512 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
12513 * @phba: pointer to lpfc hba data structure.
12514 *
12515 * This routine clears the FCF record index from the eligible bmask for
12516 * round robin failover search. It checks to make sure that the index
12517 * does not go beyond the range of the driver allocated bmask dimension
12518 * before clearing the bit.
12519 **/
12520void
12521lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12522{
12523 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12524 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12525 "2762 HBA FCF index goes beyond driver's "
12526 "book keeping dimension: fcf_index:%d, "
12527 "driver_bmask_max:%d\n",
12528 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12529 return;
12530 }
12531 /* Clear the eligible FCF record index bmask */
12532 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
12533
12534 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12535 "2791 Clear FCF index x%x from round robin failover "
12536 "bmask\n", fcf_index);
0c9ab6f5
JS
12537}
12538
ecfd03c6
JS
12539/**
12540 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12541 * @phba: pointer to lpfc hba data structure.
12542 *
12543 * This routine is the completion routine for the rediscover FCF table mailbox
12544 * command. If the mailbox command returned failure, it will try to stop the
12545 * FCF rediscover wait timer.
12546 **/
12547void
12548lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12549{
12550 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12551 uint32_t shdr_status, shdr_add_status;
12552
12553 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12554
12555 shdr_status = bf_get(lpfc_mbox_hdr_status,
12556 &redisc_fcf->header.cfg_shdr.response);
12557 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12558 &redisc_fcf->header.cfg_shdr.response);
12559 if (shdr_status || shdr_add_status) {
0c9ab6f5 12560 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
12561 "2746 Requesting for FCF rediscovery failed "
12562 "status x%x add_status x%x\n",
12563 shdr_status, shdr_add_status);
0c9ab6f5 12564 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 12565 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12566 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
12567 spin_unlock_irq(&phba->hbalock);
12568 /*
12569 * CVL event triggered FCF rediscover request failed,
12570 * last resort to re-try current registered FCF entry.
12571 */
12572 lpfc_retry_pport_discovery(phba);
12573 } else {
12574 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12575 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
12576 spin_unlock_irq(&phba->hbalock);
12577 /*
12578 * DEAD FCF event triggered FCF rediscover request
12579 * failed, last resort to fail over as a link down
12580 * to FCF registration.
12581 */
12582 lpfc_sli4_fcf_dead_failthrough(phba);
12583 }
0c9ab6f5
JS
12584 } else {
12585 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12586 "2775 Start FCF rediscovery quiescent period "
12587 "wait timer before scaning FCF table\n");
ecfd03c6
JS
12588 /*
12589 * Start FCF rediscovery wait timer for pending FCF
12590 * before rescan FCF record table.
12591 */
12592 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 12593 }
ecfd03c6
JS
12594
12595 mempool_free(mbox, phba->mbox_mem_pool);
12596}
12597
12598/**
3804dc84 12599 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
12600 * @phba: pointer to lpfc hba data structure.
12601 *
12602 * This routine is invoked to request for rediscovery of the entire FCF table
12603 * by the port.
12604 **/
12605int
12606lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12607{
12608 LPFC_MBOXQ_t *mbox;
12609 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12610 int rc, length;
12611
0c9ab6f5
JS
12612 /* Cancel retry delay timers to all vports before FCF rediscover */
12613 lpfc_cancel_all_vport_retry_delay_timer(phba);
12614
ecfd03c6
JS
12615 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12616 if (!mbox) {
12617 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12618 "2745 Failed to allocate mbox for "
12619 "requesting FCF rediscover.\n");
12620 return -ENOMEM;
12621 }
12622
12623 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12624 sizeof(struct lpfc_sli4_cfg_mhdr));
12625 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12626 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12627 length, LPFC_SLI4_MBX_EMBED);
12628
12629 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12630 /* Set count to 0 for invalidating the entire FCF database */
12631 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12632
12633 /* Issue the mailbox command asynchronously */
12634 mbox->vport = phba->pport;
12635 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12636 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12637
12638 if (rc == MBX_NOT_FINISHED) {
12639 mempool_free(mbox, phba->mbox_mem_pool);
12640 return -EIO;
12641 }
12642 return 0;
12643}
12644
fc2b989b
JS
12645/**
12646 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12647 * @phba: pointer to lpfc hba data structure.
12648 *
12649 * This function is the failover routine as a last resort to the FCF DEAD
12650 * event when driver failed to perform fast FCF failover.
12651 **/
12652void
12653lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12654{
12655 uint32_t link_state;
12656
12657 /*
12658 * Last resort as FCF DEAD event failover will treat this as
12659 * a link down, but save the link state because we don't want
12660 * it to be changed to Link Down unless it is already down.
12661 */
12662 link_state = phba->link_state;
12663 lpfc_linkdown(phba);
12664 phba->link_state = link_state;
12665
12666 /* Unregister FCF if no devices connected to it */
12667 lpfc_unregister_unused_fcf(phba);
12668}
12669
a0c87cbd
JS
12670/**
12671 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12672 * @phba: pointer to lpfc hba data structure.
12673 *
12674 * This function read region 23 and parse TLV for port status to
12675 * decide if the user disaled the port. If the TLV indicates the
12676 * port is disabled, the hba_flag is set accordingly.
12677 **/
12678void
12679lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12680{
12681 LPFC_MBOXQ_t *pmb = NULL;
12682 MAILBOX_t *mb;
12683 uint8_t *rgn23_data = NULL;
12684 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12685 int rc;
12686
12687 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12688 if (!pmb) {
12689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12690 "2600 lpfc_sli_read_serdes_param failed to"
12691 " allocate mailbox memory\n");
12692 goto out;
12693 }
12694 mb = &pmb->u.mb;
12695
12696 /* Get adapter Region 23 data */
12697 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12698 if (!rgn23_data)
12699 goto out;
12700
12701 do {
12702 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12703 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12704
12705 if (rc != MBX_SUCCESS) {
12706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12707 "2601 lpfc_sli_read_link_ste failed to"
12708 " read config region 23 rc 0x%x Status 0x%x\n",
12709 rc, mb->mbxStatus);
12710 mb->un.varDmp.word_cnt = 0;
12711 }
12712 /*
12713 * dump mem may return a zero when finished or we got a
12714 * mailbox error, either way we are done.
12715 */
12716 if (mb->un.varDmp.word_cnt == 0)
12717 break;
12718 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12719 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12720
12721 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12722 rgn23_data + offset,
12723 mb->un.varDmp.word_cnt);
12724 offset += mb->un.varDmp.word_cnt;
12725 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12726
12727 data_size = offset;
12728 offset = 0;
12729
12730 if (!data_size)
12731 goto out;
12732
12733 /* Check the region signature first */
12734 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12736 "2619 Config region 23 has bad signature\n");
12737 goto out;
12738 }
12739 offset += 4;
12740
12741 /* Check the data structure version */
12742 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12744 "2620 Config region 23 has bad version\n");
12745 goto out;
12746 }
12747 offset += 4;
12748
12749 /* Parse TLV entries in the region */
12750 while (offset < data_size) {
12751 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12752 break;
12753 /*
12754 * If the TLV is not driver specific TLV or driver id is
12755 * not linux driver id, skip the record.
12756 */
12757 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12758 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12759 (rgn23_data[offset + 3] != 0)) {
12760 offset += rgn23_data[offset + 1] * 4 + 4;
12761 continue;
12762 }
12763
12764 /* Driver found a driver specific TLV in the config region */
12765 sub_tlv_len = rgn23_data[offset + 1] * 4;
12766 offset += 4;
12767 tlv_offset = 0;
12768
12769 /*
12770 * Search for configured port state sub-TLV.
12771 */
12772 while ((offset < data_size) &&
12773 (tlv_offset < sub_tlv_len)) {
12774 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12775 offset += 4;
12776 tlv_offset += 4;
12777 break;
12778 }
12779 if (rgn23_data[offset] != PORT_STE_TYPE) {
12780 offset += rgn23_data[offset + 1] * 4 + 4;
12781 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12782 continue;
12783 }
12784
12785 /* This HBA contains PORT_STE configured */
12786 if (!rgn23_data[offset + 2])
12787 phba->hba_flag |= LINK_DISABLED;
12788
12789 goto out;
12790 }
12791 }
12792out:
12793 if (pmb)
12794 mempool_free(pmb, phba->mbox_mem_pool);
12795 kfree(rgn23_data);
12796 return;
12797}
695a814e
JS
12798
12799/**
12800 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12801 * @vport: pointer to vport data structure.
12802 *
12803 * This function iterate through the mailboxq and clean up all REG_LOGIN
12804 * and REG_VPI mailbox commands associated with the vport. This function
12805 * is called when driver want to restart discovery of the vport due to
12806 * a Clear Virtual Link event.
12807 **/
12808void
12809lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12810{
12811 struct lpfc_hba *phba = vport->phba;
12812 LPFC_MBOXQ_t *mb, *nextmb;
12813 struct lpfc_dmabuf *mp;
78730cfe 12814 struct lpfc_nodelist *ndlp;
d439d286 12815 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 12816 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 12817 LIST_HEAD(mbox_cmd_list);
695a814e 12818
d439d286 12819 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
12820 spin_lock_irq(&phba->hbalock);
12821 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12822 if (mb->vport != vport)
12823 continue;
12824
12825 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12826 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12827 continue;
12828
d439d286
JS
12829 list_del(&mb->list);
12830 list_add_tail(&mb->list, &mbox_cmd_list);
12831 }
12832 /* Clean up active mailbox command with the vport */
12833 mb = phba->sli.mbox_active;
12834 if (mb && (mb->vport == vport)) {
12835 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12836 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12837 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12838 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12839 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
12840 /* Put reference count for delayed processing */
12841 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
12842 /* Unregister the RPI when mailbox complete */
12843 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12844 }
12845 }
12846 spin_unlock_irq(&phba->hbalock);
12847
12848 /* Release the cleaned-up mailbox commands */
12849 while (!list_empty(&mbox_cmd_list)) {
12850 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e 12851 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
d7c47992
JS
12852 if (phba->sli_rev == LPFC_SLI_REV4)
12853 __lpfc_sli4_free_rpi(phba,
12854 mb->u.mb.un.varRegLogin.rpi);
695a814e
JS
12855 mp = (struct lpfc_dmabuf *) (mb->context1);
12856 if (mp) {
12857 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12858 kfree(mp);
12859 }
78730cfe 12860 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 12861 mb->context2 = NULL;
78730cfe 12862 if (ndlp) {
ec21b3b0 12863 spin_lock(shost->host_lock);
589a52d6 12864 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 12865 spin_unlock(shost->host_lock);
78730cfe 12866 lpfc_nlp_put(ndlp);
78730cfe 12867 }
695a814e 12868 }
695a814e
JS
12869 mempool_free(mb, phba->mbox_mem_pool);
12870 }
d439d286
JS
12871
12872 /* Release the ndlp with the cleaned-up active mailbox command */
12873 if (act_mbx_ndlp) {
12874 spin_lock(shost->host_lock);
12875 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12876 spin_unlock(shost->host_lock);
12877 lpfc_nlp_put(act_mbx_ndlp);
695a814e 12878 }
695a814e
JS
12879}
12880
2a9bf3d0
JS
12881/**
12882 * lpfc_drain_txq - Drain the txq
12883 * @phba: Pointer to HBA context object.
12884 *
12885 * This function attempt to submit IOCBs on the txq
12886 * to the adapter. For SLI4 adapters, the txq contains
12887 * ELS IOCBs that have been deferred because the there
12888 * are no SGLs. This congestion can occur with large
12889 * vport counts during node discovery.
12890 **/
12891
12892uint32_t
12893lpfc_drain_txq(struct lpfc_hba *phba)
12894{
12895 LIST_HEAD(completions);
12896 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12897 struct lpfc_iocbq *piocbq = 0;
12898 unsigned long iflags = 0;
12899 char *fail_msg = NULL;
12900 struct lpfc_sglq *sglq;
12901 union lpfc_wqe wqe;
12902
12903 spin_lock_irqsave(&phba->hbalock, iflags);
12904 if (pring->txq_cnt > pring->txq_max)
12905 pring->txq_max = pring->txq_cnt;
12906
12907 spin_unlock_irqrestore(&phba->hbalock, iflags);
12908
12909 while (pring->txq_cnt) {
12910 spin_lock_irqsave(&phba->hbalock, iflags);
12911
12912 sglq = __lpfc_sli_get_sglq(phba);
12913 if (!sglq) {
12914 spin_unlock_irqrestore(&phba->hbalock, iflags);
12915 break;
12916 } else {
12917 piocbq = lpfc_sli_ringtx_get(phba, pring);
12918 if (!piocbq) {
12919 /* The txq_cnt out of sync. This should
12920 * never happen
12921 */
12922 sglq = __lpfc_clear_active_sglq(phba,
12923 sglq->sli4_xritag);
12924 spin_unlock_irqrestore(&phba->hbalock, iflags);
12925 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12926 "2823 txq empty and txq_cnt is %d\n ",
12927 pring->txq_cnt);
12928 break;
12929 }
12930 }
12931
12932 /* The xri and iocb resources secured,
12933 * attempt to issue request
12934 */
12935 piocbq->sli4_xritag = sglq->sli4_xritag;
12936 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
12937 fail_msg = "to convert bpl to sgl";
12938 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
12939 fail_msg = "to convert iocb to wqe";
12940 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
12941 fail_msg = " - Wq is full";
12942 else
12943 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
12944
12945 if (fail_msg) {
12946 /* Failed means we can't issue and need to cancel */
12947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12948 "2822 IOCB failed %s iotag 0x%x "
12949 "xri 0x%x\n",
12950 fail_msg,
12951 piocbq->iotag, piocbq->sli4_xritag);
12952 list_add_tail(&piocbq->list, &completions);
12953 }
12954 spin_unlock_irqrestore(&phba->hbalock, iflags);
12955 }
12956
2a9bf3d0
JS
12957 /* Cancel all the IOCBs that cannot be issued */
12958 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12959 IOERR_SLI_ABORTED);
12960
12961 return pring->txq_cnt;
12962}
This page took 1.789064 seconds and 5 git commands to generate.