]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.3.19: Add latest SLI4 Hardware initialization support
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
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
JB
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
JB
20 *******************************************************************/
21
dea3101e
JB
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
JB
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
JB
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e
JB
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
JB
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))
f0d9bccc 98 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
4f774513
JS
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
864 }
865 }
ed957684
JS
866 mempool_free(pmb, phba->mbox_mem_pool);
867 return ret;
dea3101e
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
959 pring->local_getidx, max_cmd_idx);
960
2e0fef85 961 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
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
JB
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
JB
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
JB
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
JB
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
JB
1088 /*
1089 * Issue iocb command to adapter
1090 */
92d7f7b0 1091 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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
JB
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:
dcf2a4e0
JS
1680 case MBX_SECURITY_MGMT:
1681 case MBX_AUTH_PORT:
dea3101e
JB
1682 ret = mbxCommand;
1683 break;
1684 default:
1685 ret = MBX_SHUTDOWN;
1686 break;
1687 }
2e0fef85 1688 return ret;
dea3101e 1689}
e59058c4
JS
1690
1691/**
3621a710 1692 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1693 * @phba: Pointer to HBA context object.
1694 * @pmboxq: Pointer to mailbox command.
1695 *
1696 * This is completion handler function for mailbox commands issued from
1697 * lpfc_sli_issue_mbox_wait function. This function is called by the
1698 * mailbox event handler function with no lock held. This function
1699 * will wake up thread waiting on the wait queue pointed by context1
1700 * of the mailbox.
1701 **/
04c68496 1702void
2e0fef85 1703lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
1704{
1705 wait_queue_head_t *pdone_q;
858c9f6c 1706 unsigned long drvr_flag;
dea3101e
JB
1707
1708 /*
1709 * If pdone_q is empty, the driver thread gave up waiting and
1710 * continued running.
1711 */
7054a606 1712 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1713 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
1714 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1715 if (pdone_q)
1716 wake_up_interruptible(pdone_q);
858c9f6c 1717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
1718 return;
1719}
1720
e59058c4
JS
1721
1722/**
3621a710 1723 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1724 * @phba: Pointer to HBA context object.
1725 * @pmb: Pointer to mailbox object.
1726 *
1727 * This function is the default mailbox completion handler. It
1728 * frees the memory resources associated with the completed mailbox
1729 * command. If the completed command is a REG_LOGIN mailbox command,
1730 * this function will issue a UREG_LOGIN to re-claim the RPI.
1731 **/
dea3101e 1732void
2e0fef85 1733lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1734{
d439d286 1735 struct lpfc_vport *vport = pmb->vport;
dea3101e 1736 struct lpfc_dmabuf *mp;
d439d286 1737 struct lpfc_nodelist *ndlp;
5af5eee7 1738 struct Scsi_Host *shost;
04c68496 1739 uint16_t rpi, vpi;
7054a606
JS
1740 int rc;
1741
dea3101e 1742 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1743
dea3101e
JB
1744 if (mp) {
1745 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1746 kfree(mp);
1747 }
7054a606 1748
04c68496 1749 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
5af5eee7
JS
1750 (phba->sli_rev == LPFC_SLI_REV4) &&
1751 (pmb->u.mb.un.varUnregLogin.rsvd1 == 0x0))
04c68496
JS
1752 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1753
7054a606
JS
1754 /*
1755 * If a REG_LOGIN succeeded after node is destroyed or node
1756 * is in re-discovery driver need to cleanup the RPI.
1757 */
2e0fef85 1758 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1759 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1760 !pmb->u.mb.mbxStatus) {
1761 rpi = pmb->u.mb.un.varWords[0];
1762 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1763 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1764 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1765 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1766 if (rc != MBX_NOT_FINISHED)
1767 return;
1768 }
1769
695a814e
JS
1770 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1771 !(phba->pport->load_flag & FC_UNLOADING) &&
1772 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
1773 shost = lpfc_shost_from_vport(vport);
1774 spin_lock_irq(shost->host_lock);
1775 vport->vpi_state |= LPFC_VPI_REGISTERED;
1776 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
1777 spin_unlock_irq(shost->host_lock);
695a814e
JS
1778 }
1779
d439d286
JS
1780 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
1781 ndlp = (struct lpfc_nodelist *)pmb->context2;
1782 lpfc_nlp_put(ndlp);
1783 pmb->context2 = NULL;
1784 }
1785
dcf2a4e0
JS
1786 /* Check security permission status on INIT_LINK mailbox command */
1787 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
1788 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
1789 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1790 "2860 SLI authentication is required "
1791 "for INIT_LINK but has not done yet\n");
1792
04c68496
JS
1793 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1794 lpfc_sli4_mbox_cmd_free(phba, pmb);
1795 else
1796 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
1797}
1798
e59058c4 1799/**
3621a710 1800 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1801 * @phba: Pointer to HBA context object.
1802 *
1803 * This function is called with no lock held. This function processes all
1804 * the completed mailbox commands and gives it to upper layers. The interrupt
1805 * service routine processes mailbox completion interrupt and adds completed
1806 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1807 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1808 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1809 * function returns the mailbox commands to the upper layer by calling the
1810 * completion handler function of each mailbox.
1811 **/
dea3101e 1812int
2e0fef85 1813lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1814{
92d7f7b0 1815 MAILBOX_t *pmbox;
dea3101e 1816 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1817 int rc;
1818 LIST_HEAD(cmplq);
dea3101e
JB
1819
1820 phba->sli.slistat.mbox_event++;
1821
92d7f7b0
JS
1822 /* Get all completed mailboxe buffers into the cmplq */
1823 spin_lock_irq(&phba->hbalock);
1824 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1825 spin_unlock_irq(&phba->hbalock);
dea3101e 1826
92d7f7b0
JS
1827 /* Get a Mailbox buffer to setup mailbox commands for callback */
1828 do {
1829 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1830 if (pmb == NULL)
1831 break;
2e0fef85 1832
04c68496 1833 pmbox = &pmb->u.mb;
dea3101e 1834
858c9f6c
JS
1835 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1836 if (pmb->vport) {
1837 lpfc_debugfs_disc_trc(pmb->vport,
1838 LPFC_DISC_TRC_MBOX_VPORT,
1839 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1840 (uint32_t)pmbox->mbxCommand,
1841 pmbox->un.varWords[0],
1842 pmbox->un.varWords[1]);
1843 }
1844 else {
1845 lpfc_debugfs_disc_trc(phba->pport,
1846 LPFC_DISC_TRC_MBOX,
1847 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1848 (uint32_t)pmbox->mbxCommand,
1849 pmbox->un.varWords[0],
1850 pmbox->un.varWords[1]);
1851 }
1852 }
1853
dea3101e
JB
1854 /*
1855 * It is a fatal error if unknown mbox command completion.
1856 */
1857 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1858 MBX_SHUTDOWN) {
af901ca1 1859 /* Unknown mailbox command compl */
92d7f7b0 1860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1861 "(%d):0323 Unknown Mailbox command "
04c68496 1862 "x%x (x%x) Cmpl\n",
92d7f7b0 1863 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1864 pmbox->mbxCommand,
1865 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1866 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1867 phba->work_hs = HS_FFER3;
1868 lpfc_handle_eratt(phba);
92d7f7b0 1869 continue;
dea3101e
JB
1870 }
1871
dea3101e
JB
1872 if (pmbox->mbxStatus) {
1873 phba->sli.slistat.mbox_stat_err++;
1874 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1875 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1876 lpfc_printf_log(phba, KERN_INFO,
1877 LOG_MBOX | LOG_SLI,
e8b62011 1878 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1879 "error - RETRYing Data: x%x "
04c68496 1880 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1881 pmb->vport ? pmb->vport->vpi :0,
1882 pmbox->mbxCommand,
04c68496
JS
1883 lpfc_sli4_mbox_opcode_get(phba,
1884 pmb),
92d7f7b0
JS
1885 pmbox->mbxStatus,
1886 pmbox->un.varWords[0],
1887 pmb->vport->port_state);
dea3101e
JB
1888 pmbox->mbxStatus = 0;
1889 pmbox->mbxOwner = OWN_HOST;
dea3101e 1890 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1891 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1892 continue;
dea3101e
JB
1893 }
1894 }
1895
1896 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1897 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1898 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1899 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1900 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1901 pmbox->mbxCommand,
04c68496 1902 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e
JB
1903 pmb->mbox_cmpl,
1904 *((uint32_t *) pmbox),
1905 pmbox->un.varWords[0],
1906 pmbox->un.varWords[1],
1907 pmbox->un.varWords[2],
1908 pmbox->un.varWords[3],
1909 pmbox->un.varWords[4],
1910 pmbox->un.varWords[5],
1911 pmbox->un.varWords[6],
1912 pmbox->un.varWords[7]);
1913
92d7f7b0 1914 if (pmb->mbox_cmpl)
dea3101e 1915 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1916 } while (1);
1917 return 0;
1918}
dea3101e 1919
e59058c4 1920/**
3621a710 1921 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1922 * @phba: Pointer to HBA context object.
1923 * @pring: Pointer to driver SLI ring object.
1924 * @tag: buffer tag.
1925 *
1926 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1927 * is set in the tag the buffer is posted for a particular exchange,
1928 * the function will return the buffer without replacing the buffer.
1929 * If the buffer is for unsolicited ELS or CT traffic, this function
1930 * returns the buffer and also posts another buffer to the firmware.
1931 **/
76bb24ef
JS
1932static struct lpfc_dmabuf *
1933lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1934 struct lpfc_sli_ring *pring,
1935 uint32_t tag)
76bb24ef 1936{
9f1e1b50
JS
1937 struct hbq_dmabuf *hbq_entry;
1938
76bb24ef
JS
1939 if (tag & QUE_BUFTAG_BIT)
1940 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1941 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1942 if (!hbq_entry)
1943 return NULL;
1944 return &hbq_entry->dbuf;
76bb24ef 1945}
57127f15 1946
3772a991
JS
1947/**
1948 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1949 * @phba: Pointer to HBA context object.
1950 * @pring: Pointer to driver SLI ring object.
1951 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1952 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1953 * @fch_type: the type for the first frame of the sequence.
1954 *
1955 * This function is called with no lock held. This function uses the r_ctl and
1956 * type of the received sequence to find the correct callback function to call
1957 * to process the sequence.
1958 **/
1959static int
1960lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1961 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1962 uint32_t fch_type)
1963{
1964 int i;
1965
1966 /* unSolicited Responses */
1967 if (pring->prt[0].profile) {
1968 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1969 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1970 saveq);
1971 return 1;
1972 }
1973 /* We must search, based on rctl / type
1974 for the right routine */
1975 for (i = 0; i < pring->num_mask; i++) {
1976 if ((pring->prt[i].rctl == fch_r_ctl) &&
1977 (pring->prt[i].type == fch_type)) {
1978 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1979 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1980 (phba, pring, saveq);
1981 return 1;
1982 }
1983 }
1984 return 0;
1985}
e59058c4
JS
1986
1987/**
3621a710 1988 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1989 * @phba: Pointer to HBA context object.
1990 * @pring: Pointer to driver SLI ring object.
1991 * @saveq: Pointer to the unsolicited iocb.
1992 *
1993 * This function is called with no lock held by the ring event handler
1994 * when there is an unsolicited iocb posted to the response ring by the
1995 * firmware. This function gets the buffer associated with the iocbs
1996 * and calls the event handler for the ring. This function handles both
1997 * qring buffers and hbq buffers.
1998 * When the function returns 1 the caller can free the iocb object otherwise
1999 * upper layer functions will free the iocb objects.
2000 **/
dea3101e
JB
2001static int
2002lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2003 struct lpfc_iocbq *saveq)
2004{
2005 IOCB_t * irsp;
2006 WORD5 * w5p;
2007 uint32_t Rctl, Type;
3772a991 2008 uint32_t match;
76bb24ef 2009 struct lpfc_iocbq *iocbq;
3163f725 2010 struct lpfc_dmabuf *dmzbuf;
dea3101e
JB
2011
2012 match = 0;
2013 irsp = &(saveq->iocb);
57127f15
JS
2014
2015 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2016 if (pring->lpfc_sli_rcv_async_status)
2017 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2018 else
2019 lpfc_printf_log(phba,
2020 KERN_WARNING,
2021 LOG_SLI,
2022 "0316 Ring %d handler: unexpected "
2023 "ASYNC_STATUS iocb received evt_code "
2024 "0x%x\n",
2025 pring->ringno,
2026 irsp->un.asyncstat.evt_code);
2027 return 1;
2028 }
2029
3163f725
JS
2030 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2031 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2032 if (irsp->ulpBdeCount > 0) {
2033 dmzbuf = lpfc_sli_get_buff(phba, pring,
2034 irsp->un.ulpWord[3]);
2035 lpfc_in_buf_free(phba, dmzbuf);
2036 }
2037
2038 if (irsp->ulpBdeCount > 1) {
2039 dmzbuf = lpfc_sli_get_buff(phba, pring,
2040 irsp->unsli3.sli3Words[3]);
2041 lpfc_in_buf_free(phba, dmzbuf);
2042 }
2043
2044 if (irsp->ulpBdeCount > 2) {
2045 dmzbuf = lpfc_sli_get_buff(phba, pring,
2046 irsp->unsli3.sli3Words[7]);
2047 lpfc_in_buf_free(phba, dmzbuf);
2048 }
2049
2050 return 1;
2051 }
2052
92d7f7b0 2053 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2054 if (irsp->ulpBdeCount != 0) {
2055 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2056 irsp->un.ulpWord[3]);
2057 if (!saveq->context2)
2058 lpfc_printf_log(phba,
2059 KERN_ERR,
2060 LOG_SLI,
2061 "0341 Ring %d Cannot find buffer for "
2062 "an unsolicited iocb. tag 0x%x\n",
2063 pring->ringno,
2064 irsp->un.ulpWord[3]);
76bb24ef
JS
2065 }
2066 if (irsp->ulpBdeCount == 2) {
2067 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2068 irsp->unsli3.sli3Words[7]);
2069 if (!saveq->context3)
2070 lpfc_printf_log(phba,
2071 KERN_ERR,
2072 LOG_SLI,
2073 "0342 Ring %d Cannot find buffer for an"
2074 " unsolicited iocb. tag 0x%x\n",
2075 pring->ringno,
2076 irsp->unsli3.sli3Words[7]);
2077 }
2078 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2079 irsp = &(iocbq->iocb);
76bb24ef
JS
2080 if (irsp->ulpBdeCount != 0) {
2081 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2082 irsp->un.ulpWord[3]);
9c2face6 2083 if (!iocbq->context2)
76bb24ef
JS
2084 lpfc_printf_log(phba,
2085 KERN_ERR,
2086 LOG_SLI,
2087 "0343 Ring %d Cannot find "
2088 "buffer for an unsolicited iocb"
2089 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2090 irsp->un.ulpWord[3]);
76bb24ef
JS
2091 }
2092 if (irsp->ulpBdeCount == 2) {
2093 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2094 irsp->unsli3.sli3Words[7]);
9c2face6 2095 if (!iocbq->context3)
76bb24ef
JS
2096 lpfc_printf_log(phba,
2097 KERN_ERR,
2098 LOG_SLI,
2099 "0344 Ring %d Cannot find "
2100 "buffer for an unsolicited "
2101 "iocb. tag 0x%x\n",
2102 pring->ringno,
2103 irsp->unsli3.sli3Words[7]);
2104 }
2105 }
92d7f7b0 2106 }
9c2face6
JS
2107 if (irsp->ulpBdeCount != 0 &&
2108 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2109 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2110 int found = 0;
2111
2112 /* search continue save q for same XRI */
2113 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2114 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2115 list_add_tail(&saveq->list, &iocbq->list);
2116 found = 1;
2117 break;
2118 }
2119 }
2120 if (!found)
2121 list_add_tail(&saveq->clist,
2122 &pring->iocb_continue_saveq);
2123 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2124 list_del_init(&iocbq->clist);
2125 saveq = iocbq;
2126 irsp = &(saveq->iocb);
2127 } else
2128 return 0;
2129 }
2130 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2131 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2132 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2133 Rctl = FC_RCTL_ELS_REQ;
2134 Type = FC_TYPE_ELS;
9c2face6
JS
2135 } else {
2136 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2137 Rctl = w5p->hcsw.Rctl;
2138 Type = w5p->hcsw.Type;
2139
2140 /* Firmware Workaround */
2141 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2142 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2143 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2144 Rctl = FC_RCTL_ELS_REQ;
2145 Type = FC_TYPE_ELS;
9c2face6
JS
2146 w5p->hcsw.Rctl = Rctl;
2147 w5p->hcsw.Type = Type;
2148 }
2149 }
92d7f7b0 2150
3772a991 2151 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2153 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2154 "Type x%x received\n",
e8b62011 2155 pring->ringno, Rctl, Type);
3772a991 2156
92d7f7b0 2157 return 1;
dea3101e
JB
2158}
2159
e59058c4 2160/**
3621a710 2161 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2162 * @phba: Pointer to HBA context object.
2163 * @pring: Pointer to driver SLI ring object.
2164 * @prspiocb: Pointer to response iocb object.
2165 *
2166 * This function looks up the iocb_lookup table to get the command iocb
2167 * corresponding to the given response iocb using the iotag of the
2168 * response iocb. This function is called with the hbalock held.
2169 * This function returns the command iocb object if it finds the command
2170 * iocb else returns NULL.
2171 **/
dea3101e 2172static struct lpfc_iocbq *
2e0fef85
JS
2173lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2174 struct lpfc_sli_ring *pring,
2175 struct lpfc_iocbq *prspiocb)
dea3101e 2176{
dea3101e
JB
2177 struct lpfc_iocbq *cmd_iocb = NULL;
2178 uint16_t iotag;
2179
604a3e30
JB
2180 iotag = prspiocb->iocb.ulpIoTag;
2181
2182 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2183 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2184 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2185 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2186 pring->txcmplq_cnt--;
2187 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2188 }
604a3e30 2189 return cmd_iocb;
dea3101e
JB
2190 }
2191
dea3101e 2192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2193 "0317 iotag x%x is out off "
604a3e30 2194 "range: max iotag x%x wd0 x%x\n",
e8b62011 2195 iotag, phba->sli.last_iotag,
604a3e30 2196 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2197 return NULL;
2198}
2199
3772a991
JS
2200/**
2201 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2202 * @phba: Pointer to HBA context object.
2203 * @pring: Pointer to driver SLI ring object.
2204 * @iotag: IOCB tag.
2205 *
2206 * This function looks up the iocb_lookup table to get the command iocb
2207 * corresponding to the given iotag. This function is called with the
2208 * hbalock held.
2209 * This function returns the command iocb object if it finds the command
2210 * iocb else returns NULL.
2211 **/
2212static struct lpfc_iocbq *
2213lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2214 struct lpfc_sli_ring *pring, uint16_t iotag)
2215{
2216 struct lpfc_iocbq *cmd_iocb;
2217
2218 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2219 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2220 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2221 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2222 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2223 pring->txcmplq_cnt--;
2224 }
3772a991
JS
2225 return cmd_iocb;
2226 }
2227
2228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2229 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2230 iotag, phba->sli.last_iotag);
2231 return NULL;
2232}
2233
e59058c4 2234/**
3621a710 2235 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2236 * @phba: Pointer to HBA context object.
2237 * @pring: Pointer to driver SLI ring object.
2238 * @saveq: Pointer to the response iocb to be processed.
2239 *
2240 * This function is called by the ring event handler for non-fcp
2241 * rings when there is a new response iocb in the response ring.
2242 * The caller is not required to hold any locks. This function
2243 * gets the command iocb associated with the response iocb and
2244 * calls the completion handler for the command iocb. If there
2245 * is no completion handler, the function will free the resources
2246 * associated with command iocb. If the response iocb is for
2247 * an already aborted command iocb, the status of the completion
2248 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2249 * This function always returns 1.
2250 **/
dea3101e 2251static int
2e0fef85 2252lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2253 struct lpfc_iocbq *saveq)
2254{
2e0fef85 2255 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2256 int rc = 1;
2257 unsigned long iflag;
2258
2259 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2260 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2261 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2262 spin_unlock_irqrestore(&phba->hbalock, iflag);
2263
dea3101e
JB
2264 if (cmdiocbp) {
2265 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2266 /*
2267 * If an ELS command failed send an event to mgmt
2268 * application.
2269 */
2270 if (saveq->iocb.ulpStatus &&
2271 (pring->ringno == LPFC_ELS_RING) &&
2272 (cmdiocbp->iocb.ulpCommand ==
2273 CMD_ELS_REQUEST64_CR))
2274 lpfc_send_els_failure_event(phba,
2275 cmdiocbp, saveq);
2276
dea3101e
JB
2277 /*
2278 * Post all ELS completions to the worker thread.
2279 * All other are passed to the completion callback.
2280 */
2281 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2282 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2283 (cmdiocbp->iocb_flag &
2284 LPFC_DRIVER_ABORTED)) {
2285 spin_lock_irqsave(&phba->hbalock,
2286 iflag);
07951076
JS
2287 cmdiocbp->iocb_flag &=
2288 ~LPFC_DRIVER_ABORTED;
341af102
JS
2289 spin_unlock_irqrestore(&phba->hbalock,
2290 iflag);
07951076
JS
2291 saveq->iocb.ulpStatus =
2292 IOSTAT_LOCAL_REJECT;
2293 saveq->iocb.un.ulpWord[4] =
2294 IOERR_SLI_ABORTED;
0ff10d46
JS
2295
2296 /* Firmware could still be in progress
2297 * of DMAing payload, so don't free data
2298 * buffer till after a hbeat.
2299 */
341af102
JS
2300 spin_lock_irqsave(&phba->hbalock,
2301 iflag);
0ff10d46 2302 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2303 spin_unlock_irqrestore(&phba->hbalock,
2304 iflag);
2305 }
0f65ff68
JS
2306 if (phba->sli_rev == LPFC_SLI_REV4) {
2307 if (saveq->iocb_flag &
2308 LPFC_EXCHANGE_BUSY) {
2309 /* Set cmdiocb flag for the
2310 * exchange busy so sgl (xri)
2311 * will not be released until
2312 * the abort xri is received
2313 * from hba.
2314 */
2315 spin_lock_irqsave(
2316 &phba->hbalock, iflag);
2317 cmdiocbp->iocb_flag |=
2318 LPFC_EXCHANGE_BUSY;
2319 spin_unlock_irqrestore(
2320 &phba->hbalock, iflag);
2321 }
2322 if (cmdiocbp->iocb_flag &
2323 LPFC_DRIVER_ABORTED) {
2324 /*
2325 * Clear LPFC_DRIVER_ABORTED
2326 * bit in case it was driver
2327 * initiated abort.
2328 */
2329 spin_lock_irqsave(
2330 &phba->hbalock, iflag);
2331 cmdiocbp->iocb_flag &=
2332 ~LPFC_DRIVER_ABORTED;
2333 spin_unlock_irqrestore(
2334 &phba->hbalock, iflag);
2335 cmdiocbp->iocb.ulpStatus =
2336 IOSTAT_LOCAL_REJECT;
2337 cmdiocbp->iocb.un.ulpWord[4] =
2338 IOERR_ABORT_REQUESTED;
2339 /*
2340 * For SLI4, irsiocb contains
2341 * NO_XRI in sli_xritag, it
2342 * shall not affect releasing
2343 * sgl (xri) process.
2344 */
2345 saveq->iocb.ulpStatus =
2346 IOSTAT_LOCAL_REJECT;
2347 saveq->iocb.un.ulpWord[4] =
2348 IOERR_SLI_ABORTED;
2349 spin_lock_irqsave(
2350 &phba->hbalock, iflag);
2351 saveq->iocb_flag |=
2352 LPFC_DELAY_MEM_FREE;
2353 spin_unlock_irqrestore(
2354 &phba->hbalock, iflag);
2355 }
07951076 2356 }
dea3101e 2357 }
2e0fef85 2358 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2359 } else
2360 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2361 } else {
2362 /*
2363 * Unknown initiating command based on the response iotag.
2364 * This could be the case on the ELS ring because of
2365 * lpfc_els_abort().
2366 */
2367 if (pring->ringno != LPFC_ELS_RING) {
2368 /*
2369 * Ring <ringno> handler: unexpected completion IoTag
2370 * <IoTag>
2371 */
a257bf90 2372 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2373 "0322 Ring %d handler: "
2374 "unexpected completion IoTag x%x "
2375 "Data: x%x x%x x%x x%x\n",
2376 pring->ringno,
2377 saveq->iocb.ulpIoTag,
2378 saveq->iocb.ulpStatus,
2379 saveq->iocb.un.ulpWord[4],
2380 saveq->iocb.ulpCommand,
2381 saveq->iocb.ulpContext);
dea3101e
JB
2382 }
2383 }
68876920 2384
dea3101e
JB
2385 return rc;
2386}
2387
e59058c4 2388/**
3621a710 2389 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2390 * @phba: Pointer to HBA context object.
2391 * @pring: Pointer to driver SLI ring object.
2392 *
2393 * This function is called from the iocb ring event handlers when
2394 * put pointer is ahead of the get pointer for a ring. This function signal
2395 * an error attention condition to the worker thread and the worker
2396 * thread will transition the HBA to offline state.
2397 **/
2e0fef85
JS
2398static void
2399lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2400{
34b02dcd 2401 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2402 /*
025dfdaf 2403 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2404 * rsp ring <portRspMax>
2405 */
2406 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2407 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2408 "is bigger than rsp ring %d\n",
e8b62011 2409 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2410 pring->numRiocb);
2411
2e0fef85 2412 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2413
2414 /*
2415 * All error attention handlers are posted to
2416 * worker thread
2417 */
2418 phba->work_ha |= HA_ERATT;
2419 phba->work_hs = HS_FFER3;
92d7f7b0 2420
5e9d9b82 2421 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2422
2423 return;
2424}
2425
9399627f 2426/**
3621a710 2427 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2428 * @ptr: Pointer to address of HBA context object.
2429 *
2430 * This function is invoked by the Error Attention polling timer when the
2431 * timer times out. It will check the SLI Error Attention register for
2432 * possible attention events. If so, it will post an Error Attention event
2433 * and wake up worker thread to process it. Otherwise, it will set up the
2434 * Error Attention polling timer for the next poll.
2435 **/
2436void lpfc_poll_eratt(unsigned long ptr)
2437{
2438 struct lpfc_hba *phba;
2439 uint32_t eratt = 0;
2440
2441 phba = (struct lpfc_hba *)ptr;
2442
2443 /* Check chip HA register for error event */
2444 eratt = lpfc_sli_check_eratt(phba);
2445
2446 if (eratt)
2447 /* Tell the worker thread there is work to do */
2448 lpfc_worker_wake_up(phba);
2449 else
2450 /* Restart the timer for next eratt poll */
2451 mod_timer(&phba->eratt_poll, jiffies +
2452 HZ * LPFC_ERATT_POLL_INTERVAL);
2453 return;
2454}
2455
875fbdfe 2456
e59058c4 2457/**
3621a710 2458 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2459 * @phba: Pointer to HBA context object.
2460 * @pring: Pointer to driver SLI ring object.
2461 * @mask: Host attention register mask for this ring.
2462 *
2463 * This function is called from the interrupt context when there is a ring
2464 * event for the fcp ring. The caller does not hold any lock.
2465 * The function processes each response iocb in the response ring until it
2466 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2467 * LE bit set. The function will call the completion handler of the command iocb
2468 * if the response iocb indicates a completion for a command iocb or it is
2469 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2470 * function if this is an unsolicited iocb.
dea3101e 2471 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2472 * to check it explicitly.
2473 */
2474int
2e0fef85
JS
2475lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2476 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2477{
34b02dcd 2478 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2479 IOCB_t *irsp = NULL;
87f6eaff 2480 IOCB_t *entry = NULL;
dea3101e
JB
2481 struct lpfc_iocbq *cmdiocbq = NULL;
2482 struct lpfc_iocbq rspiocbq;
dea3101e
JB
2483 uint32_t status;
2484 uint32_t portRspPut, portRspMax;
2485 int rc = 1;
2486 lpfc_iocb_type type;
2487 unsigned long iflag;
2488 uint32_t rsp_cmpl = 0;
dea3101e 2489
2e0fef85 2490 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2491 pring->stats.iocb_event++;
2492
dea3101e
JB
2493 /*
2494 * The next available response entry should never exceed the maximum
2495 * entries. If it does, treat it as an adapter hardware error.
2496 */
2497 portRspMax = pring->numRiocb;
2498 portRspPut = le32_to_cpu(pgp->rspPutInx);
2499 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2500 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2501 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2502 return 1;
2503 }
45ed1190
JS
2504 if (phba->fcp_ring_in_use) {
2505 spin_unlock_irqrestore(&phba->hbalock, iflag);
2506 return 1;
2507 } else
2508 phba->fcp_ring_in_use = 1;
dea3101e
JB
2509
2510 rmb();
2511 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2512 /*
2513 * Fetch an entry off the ring and copy it into a local data
2514 * structure. The copy involves a byte-swap since the
2515 * network byte order and pci byte orders are different.
2516 */
ed957684 2517 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2518 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2519
2520 if (++pring->rspidx >= portRspMax)
2521 pring->rspidx = 0;
2522
87f6eaff
JSEC
2523 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2524 (uint32_t *) &rspiocbq.iocb,
ed957684 2525 phba->iocb_rsp_size);
a4bc3379 2526 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2527 irsp = &rspiocbq.iocb;
2528
dea3101e
JB
2529 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2530 pring->stats.iocb_rsp++;
2531 rsp_cmpl++;
2532
2533 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2534 /*
2535 * If resource errors reported from HBA, reduce
2536 * queuedepths of the SCSI device.
2537 */
2538 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2539 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2540 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2541 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2542 spin_lock_irqsave(&phba->hbalock, iflag);
2543 }
2544
dea3101e
JB
2545 /* Rsp ring <ringno> error: IOCB */
2546 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2547 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2548 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2549 pring->ringno,
92d7f7b0
JS
2550 irsp->un.ulpWord[0],
2551 irsp->un.ulpWord[1],
2552 irsp->un.ulpWord[2],
2553 irsp->un.ulpWord[3],
2554 irsp->un.ulpWord[4],
2555 irsp->un.ulpWord[5],
d7c255b2
JS
2556 *(uint32_t *)&irsp->un1,
2557 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
2558 }
2559
2560 switch (type) {
2561 case LPFC_ABORT_IOCB:
2562 case LPFC_SOL_IOCB:
2563 /*
2564 * Idle exchange closed via ABTS from port. No iocb
2565 * resources need to be recovered.
2566 */
2567 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2569 "0333 IOCB cmd 0x%x"
dca9479b 2570 " processed. Skipping"
92d7f7b0 2571 " completion\n",
dca9479b 2572 irsp->ulpCommand);
dea3101e
JB
2573 break;
2574 }
2575
604a3e30
JB
2576 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2577 &rspiocbq);
0f65ff68
JS
2578 if (unlikely(!cmdiocbq))
2579 break;
2580 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2581 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2582 if (cmdiocbq->iocb_cmpl) {
2583 spin_unlock_irqrestore(&phba->hbalock, iflag);
2584 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2585 &rspiocbq);
2586 spin_lock_irqsave(&phba->hbalock, iflag);
2587 }
dea3101e 2588 break;
a4bc3379 2589 case LPFC_UNSOL_IOCB:
2e0fef85 2590 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2591 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2592 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2593 break;
dea3101e
JB
2594 default:
2595 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2596 char adaptermsg[LPFC_MAX_ADPTMSG];
2597 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2598 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2599 MAX_MSG_DATA);
898eb71c
JP
2600 dev_warn(&((phba->pcidev)->dev),
2601 "lpfc%d: %s\n",
dea3101e
JB
2602 phba->brd_no, adaptermsg);
2603 } else {
2604 /* Unknown IOCB command */
2605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2606 "0334 Unknown IOCB command "
92d7f7b0 2607 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2608 type, irsp->ulpCommand,
92d7f7b0
JS
2609 irsp->ulpStatus,
2610 irsp->ulpIoTag,
2611 irsp->ulpContext);
dea3101e
JB
2612 }
2613 break;
2614 }
2615
2616 /*
2617 * The response IOCB has been processed. Update the ring
2618 * pointer in SLIM. If the port response put pointer has not
2619 * been updated, sync the pgp->rspPutInx and fetch the new port
2620 * response put pointer.
2621 */
ed957684 2622 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e
JB
2623
2624 if (pring->rspidx == portRspPut)
2625 portRspPut = le32_to_cpu(pgp->rspPutInx);
2626 }
2627
2628 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2629 pring->stats.iocb_rsp_full++;
2630 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2631 writel(status, phba->CAregaddr);
2632 readl(phba->CAregaddr);
2633 }
2634 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2635 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2636 pring->stats.iocb_cmd_empty++;
2637
2638 /* Force update of the local copy of cmdGetInx */
2639 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2640 lpfc_sli_resume_iocb(phba, pring);
2641
2642 if ((pring->lpfc_sli_cmd_available))
2643 (pring->lpfc_sli_cmd_available) (phba, pring);
2644
2645 }
2646
45ed1190 2647 phba->fcp_ring_in_use = 0;
2e0fef85 2648 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2649 return rc;
2650}
2651
e59058c4 2652/**
3772a991
JS
2653 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2654 * @phba: Pointer to HBA context object.
2655 * @pring: Pointer to driver SLI ring object.
2656 * @rspiocbp: Pointer to driver response IOCB object.
2657 *
2658 * This function is called from the worker thread when there is a slow-path
2659 * response IOCB to process. This function chains all the response iocbs until
2660 * seeing the iocb with the LE bit set. The function will call
2661 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2662 * completion of a command iocb. The function will call the
2663 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2664 * The function frees the resources or calls the completion handler if this
2665 * iocb is an abort completion. The function returns NULL when the response
2666 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2667 * this function shall chain the iocb on to the iocb_continueq and return the
2668 * response iocb passed in.
2669 **/
2670static struct lpfc_iocbq *
2671lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2672 struct lpfc_iocbq *rspiocbp)
2673{
2674 struct lpfc_iocbq *saveq;
2675 struct lpfc_iocbq *cmdiocbp;
2676 struct lpfc_iocbq *next_iocb;
2677 IOCB_t *irsp = NULL;
2678 uint32_t free_saveq;
2679 uint8_t iocb_cmd_type;
2680 lpfc_iocb_type type;
2681 unsigned long iflag;
2682 int rc;
2683
2684 spin_lock_irqsave(&phba->hbalock, iflag);
2685 /* First add the response iocb to the countinueq list */
2686 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2687 pring->iocb_continueq_cnt++;
2688
2689 /* Now, determine whetehr the list is completed for processing */
2690 irsp = &rspiocbp->iocb;
2691 if (irsp->ulpLe) {
2692 /*
2693 * By default, the driver expects to free all resources
2694 * associated with this iocb completion.
2695 */
2696 free_saveq = 1;
2697 saveq = list_get_first(&pring->iocb_continueq,
2698 struct lpfc_iocbq, list);
2699 irsp = &(saveq->iocb);
2700 list_del_init(&pring->iocb_continueq);
2701 pring->iocb_continueq_cnt = 0;
2702
2703 pring->stats.iocb_rsp++;
2704
2705 /*
2706 * If resource errors reported from HBA, reduce
2707 * queuedepths of the SCSI device.
2708 */
2709 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2710 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2711 spin_unlock_irqrestore(&phba->hbalock, iflag);
2712 phba->lpfc_rampdown_queue_depth(phba);
2713 spin_lock_irqsave(&phba->hbalock, iflag);
2714 }
2715
2716 if (irsp->ulpStatus) {
2717 /* Rsp ring <ringno> error: IOCB */
2718 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2719 "0328 Rsp Ring %d error: "
2720 "IOCB Data: "
2721 "x%x x%x x%x x%x "
2722 "x%x x%x x%x x%x "
2723 "x%x x%x x%x x%x "
2724 "x%x x%x x%x x%x\n",
2725 pring->ringno,
2726 irsp->un.ulpWord[0],
2727 irsp->un.ulpWord[1],
2728 irsp->un.ulpWord[2],
2729 irsp->un.ulpWord[3],
2730 irsp->un.ulpWord[4],
2731 irsp->un.ulpWord[5],
2732 *(((uint32_t *) irsp) + 6),
2733 *(((uint32_t *) irsp) + 7),
2734 *(((uint32_t *) irsp) + 8),
2735 *(((uint32_t *) irsp) + 9),
2736 *(((uint32_t *) irsp) + 10),
2737 *(((uint32_t *) irsp) + 11),
2738 *(((uint32_t *) irsp) + 12),
2739 *(((uint32_t *) irsp) + 13),
2740 *(((uint32_t *) irsp) + 14),
2741 *(((uint32_t *) irsp) + 15));
2742 }
2743
2744 /*
2745 * Fetch the IOCB command type and call the correct completion
2746 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2747 * get freed back to the lpfc_iocb_list by the discovery
2748 * kernel thread.
2749 */
2750 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2751 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2752 switch (type) {
2753 case LPFC_SOL_IOCB:
2754 spin_unlock_irqrestore(&phba->hbalock, iflag);
2755 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2756 spin_lock_irqsave(&phba->hbalock, iflag);
2757 break;
2758
2759 case LPFC_UNSOL_IOCB:
2760 spin_unlock_irqrestore(&phba->hbalock, iflag);
2761 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2762 spin_lock_irqsave(&phba->hbalock, iflag);
2763 if (!rc)
2764 free_saveq = 0;
2765 break;
2766
2767 case LPFC_ABORT_IOCB:
2768 cmdiocbp = NULL;
2769 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2770 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2771 saveq);
2772 if (cmdiocbp) {
2773 /* Call the specified completion routine */
2774 if (cmdiocbp->iocb_cmpl) {
2775 spin_unlock_irqrestore(&phba->hbalock,
2776 iflag);
2777 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2778 saveq);
2779 spin_lock_irqsave(&phba->hbalock,
2780 iflag);
2781 } else
2782 __lpfc_sli_release_iocbq(phba,
2783 cmdiocbp);
2784 }
2785 break;
2786
2787 case LPFC_UNKNOWN_IOCB:
2788 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2789 char adaptermsg[LPFC_MAX_ADPTMSG];
2790 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2791 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2792 MAX_MSG_DATA);
2793 dev_warn(&((phba->pcidev)->dev),
2794 "lpfc%d: %s\n",
2795 phba->brd_no, adaptermsg);
2796 } else {
2797 /* Unknown IOCB command */
2798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2799 "0335 Unknown IOCB "
2800 "command Data: x%x "
2801 "x%x x%x x%x\n",
2802 irsp->ulpCommand,
2803 irsp->ulpStatus,
2804 irsp->ulpIoTag,
2805 irsp->ulpContext);
2806 }
2807 break;
2808 }
2809
2810 if (free_saveq) {
2811 list_for_each_entry_safe(rspiocbp, next_iocb,
2812 &saveq->list, list) {
2813 list_del(&rspiocbp->list);
2814 __lpfc_sli_release_iocbq(phba, rspiocbp);
2815 }
2816 __lpfc_sli_release_iocbq(phba, saveq);
2817 }
2818 rspiocbp = NULL;
2819 }
2820 spin_unlock_irqrestore(&phba->hbalock, iflag);
2821 return rspiocbp;
2822}
2823
2824/**
2825 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2826 * @phba: Pointer to HBA context object.
2827 * @pring: Pointer to driver SLI ring object.
2828 * @mask: Host attention register mask for this ring.
2829 *
3772a991
JS
2830 * This routine wraps the actual slow_ring event process routine from the
2831 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2832 **/
3772a991 2833void
2e0fef85
JS
2834lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2835 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2836{
2837 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2838}
2839
2840/**
2841 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2842 * @phba: Pointer to HBA context object.
2843 * @pring: Pointer to driver SLI ring object.
2844 * @mask: Host attention register mask for this ring.
2845 *
2846 * This function is called from the worker thread when there is a ring event
2847 * for non-fcp rings. The caller does not hold any lock. The function will
2848 * remove each response iocb in the response ring and calls the handle
2849 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2850 **/
2851static void
2852lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2853 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2854{
34b02dcd 2855 struct lpfc_pgp *pgp;
dea3101e
JB
2856 IOCB_t *entry;
2857 IOCB_t *irsp = NULL;
2858 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2859 uint32_t portRspPut, portRspMax;
dea3101e 2860 unsigned long iflag;
3772a991 2861 uint32_t status;
dea3101e 2862
34b02dcd 2863 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2864 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2865 pring->stats.iocb_event++;
2866
dea3101e
JB
2867 /*
2868 * The next available response entry should never exceed the maximum
2869 * entries. If it does, treat it as an adapter hardware error.
2870 */
2871 portRspMax = pring->numRiocb;
2872 portRspPut = le32_to_cpu(pgp->rspPutInx);
2873 if (portRspPut >= portRspMax) {
2874 /*
025dfdaf 2875 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
2876 * rsp ring <portRspMax>
2877 */
ed957684 2878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2879 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2880 "is bigger than rsp ring %d\n",
e8b62011 2881 pring->ringno, portRspPut, portRspMax);
dea3101e 2882
2e0fef85
JS
2883 phba->link_state = LPFC_HBA_ERROR;
2884 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2885
2886 phba->work_hs = HS_FFER3;
2887 lpfc_handle_eratt(phba);
2888
3772a991 2889 return;
dea3101e
JB
2890 }
2891
2892 rmb();
dea3101e
JB
2893 while (pring->rspidx != portRspPut) {
2894 /*
2895 * Build a completion list and call the appropriate handler.
2896 * The process is to get the next available response iocb, get
2897 * a free iocb from the list, copy the response data into the
2898 * free iocb, insert to the continuation list, and update the
2899 * next response index to slim. This process makes response
2900 * iocb's in the ring available to DMA as fast as possible but
2901 * pays a penalty for a copy operation. Since the iocb is
2902 * only 32 bytes, this penalty is considered small relative to
2903 * the PCI reads for register values and a slim write. When
2904 * the ulpLe field is set, the entire Command has been
2905 * received.
2906 */
ed957684
JS
2907 entry = lpfc_resp_iocb(phba, pring);
2908
858c9f6c 2909 phba->last_completion_time = jiffies;
2e0fef85 2910 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
2911 if (rspiocbp == NULL) {
2912 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2913 "completion.\n", __func__);
dea3101e
JB
2914 break;
2915 }
2916
ed957684
JS
2917 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2918 phba->iocb_rsp_size);
dea3101e
JB
2919 irsp = &rspiocbp->iocb;
2920
2921 if (++pring->rspidx >= portRspMax)
2922 pring->rspidx = 0;
2923
a58cbd52
JS
2924 if (pring->ringno == LPFC_ELS_RING) {
2925 lpfc_debugfs_slow_ring_trc(phba,
2926 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2927 *(((uint32_t *) irsp) + 4),
2928 *(((uint32_t *) irsp) + 6),
2929 *(((uint32_t *) irsp) + 7));
2930 }
2931
ed957684 2932 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2933
3772a991
JS
2934 spin_unlock_irqrestore(&phba->hbalock, iflag);
2935 /* Handle the response IOCB */
2936 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2937 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2938
2939 /*
2940 * If the port response put pointer has not been updated, sync
2941 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2942 * response put pointer.
2943 */
2944 if (pring->rspidx == portRspPut) {
2945 portRspPut = le32_to_cpu(pgp->rspPutInx);
2946 }
2947 } /* while (pring->rspidx != portRspPut) */
2948
92d7f7b0 2949 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
2950 /* At least one response entry has been freed */
2951 pring->stats.iocb_rsp_full++;
2952 /* SET RxRE_RSP in Chip Att register */
2953 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2954 writel(status, phba->CAregaddr);
2955 readl(phba->CAregaddr); /* flush */
2956 }
2957 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2958 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2959 pring->stats.iocb_cmd_empty++;
2960
2961 /* Force update of the local copy of cmdGetInx */
2962 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2963 lpfc_sli_resume_iocb(phba, pring);
2964
2965 if ((pring->lpfc_sli_cmd_available))
2966 (pring->lpfc_sli_cmd_available) (phba, pring);
2967
2968 }
2969
2e0fef85 2970 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2971 return;
dea3101e
JB
2972}
2973
4f774513
JS
2974/**
2975 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2976 * @phba: Pointer to HBA context object.
2977 * @pring: Pointer to driver SLI ring object.
2978 * @mask: Host attention register mask for this ring.
2979 *
2980 * This function is called from the worker thread when there is a pending
2981 * ELS response iocb on the driver internal slow-path response iocb worker
2982 * queue. The caller does not hold any lock. The function will remove each
2983 * response iocb from the response worker queue and calls the handle
2984 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2985 **/
2986static void
2987lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2988 struct lpfc_sli_ring *pring, uint32_t mask)
2989{
2990 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
2991 struct hbq_dmabuf *dmabuf;
2992 struct lpfc_cq_event *cq_event;
4f774513
JS
2993 unsigned long iflag;
2994
45ed1190
JS
2995 spin_lock_irqsave(&phba->hbalock, iflag);
2996 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2997 spin_unlock_irqrestore(&phba->hbalock, iflag);
2998 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
2999 /* Get the response iocb from the head of work queue */
3000 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3001 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3002 cq_event, struct lpfc_cq_event, list);
4f774513 3003 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3004
3005 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3006 case CQE_CODE_COMPL_WQE:
3007 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3008 cq_event);
45ed1190
JS
3009 /* Translate ELS WCQE to response IOCBQ */
3010 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3011 irspiocbq);
3012 if (irspiocbq)
3013 lpfc_sli_sp_handle_rspiocb(phba, pring,
3014 irspiocbq);
4d9ab994
JS
3015 break;
3016 case CQE_CODE_RECEIVE:
3017 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3018 cq_event);
3019 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3020 break;
3021 default:
3022 break;
3023 }
4f774513
JS
3024 }
3025}
3026
e59058c4 3027/**
3621a710 3028 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3029 * @phba: Pointer to HBA context object.
3030 * @pring: Pointer to driver SLI ring object.
3031 *
3032 * This function aborts all iocbs in the given ring and frees all the iocb
3033 * objects in txq. This function issues an abort iocb for all the iocb commands
3034 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3035 * the return of this function. The caller is not required to hold any locks.
3036 **/
2e0fef85 3037void
dea3101e
JB
3038lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3039{
2534ba75 3040 LIST_HEAD(completions);
dea3101e 3041 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3042
92d7f7b0
JS
3043 if (pring->ringno == LPFC_ELS_RING) {
3044 lpfc_fabric_abort_hba(phba);
3045 }
3046
dea3101e
JB
3047 /* Error everything on txq and txcmplq
3048 * First do the txq.
3049 */
2e0fef85 3050 spin_lock_irq(&phba->hbalock);
2534ba75 3051 list_splice_init(&pring->txq, &completions);
dea3101e 3052 pring->txq_cnt = 0;
dea3101e
JB
3053
3054 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3055 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3056 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3057
2e0fef85 3058 spin_unlock_irq(&phba->hbalock);
dea3101e 3059
a257bf90
JS
3060 /* Cancel all the IOCBs from the completions list */
3061 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3062 IOERR_SLI_ABORTED);
dea3101e
JB
3063}
3064
a8e497d5 3065/**
3621a710 3066 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3067 * @phba: Pointer to HBA context object.
3068 *
3069 * This function flushes all iocbs in the fcp ring and frees all the iocb
3070 * objects in txq and txcmplq. This function will not issue abort iocbs
3071 * for all the iocb commands in txcmplq, they will just be returned with
3072 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3073 * slot has been permanently disabled.
3074 **/
3075void
3076lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3077{
3078 LIST_HEAD(txq);
3079 LIST_HEAD(txcmplq);
a8e497d5
JS
3080 struct lpfc_sli *psli = &phba->sli;
3081 struct lpfc_sli_ring *pring;
3082
3083 /* Currently, only one fcp ring */
3084 pring = &psli->ring[psli->fcp_ring];
3085
3086 spin_lock_irq(&phba->hbalock);
3087 /* Retrieve everything on txq */
3088 list_splice_init(&pring->txq, &txq);
3089 pring->txq_cnt = 0;
3090
3091 /* Retrieve everything on the txcmplq */
3092 list_splice_init(&pring->txcmplq, &txcmplq);
3093 pring->txcmplq_cnt = 0;
3094 spin_unlock_irq(&phba->hbalock);
3095
3096 /* Flush the txq */
a257bf90
JS
3097 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3098 IOERR_SLI_DOWN);
a8e497d5
JS
3099
3100 /* Flush the txcmpq */
a257bf90
JS
3101 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3102 IOERR_SLI_DOWN);
a8e497d5
JS
3103}
3104
e59058c4 3105/**
3772a991 3106 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3107 * @phba: Pointer to HBA context object.
3108 * @mask: Bit mask to be checked.
3109 *
3110 * This function reads the host status register and compares
3111 * with the provided bit mask to check if HBA completed
3112 * the restart. This function will wait in a loop for the
3113 * HBA to complete restart. If the HBA does not restart within
3114 * 15 iterations, the function will reset the HBA again. The
3115 * function returns 1 when HBA fail to restart otherwise returns
3116 * zero.
3117 **/
3772a991
JS
3118static int
3119lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3120{
41415862
JW
3121 uint32_t status;
3122 int i = 0;
3123 int retval = 0;
dea3101e 3124
41415862
JW
3125 /* Read the HBA Host Status Register */
3126 status = readl(phba->HSregaddr);
dea3101e 3127
41415862
JW
3128 /*
3129 * Check status register every 100ms for 5 retries, then every
3130 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3131 * every 2.5 sec for 4.
3132 * Break our of the loop if errors occurred during init.
3133 */
3134 while (((status & mask) != mask) &&
3135 !(status & HS_FFERM) &&
3136 i++ < 20) {
dea3101e 3137
41415862
JW
3138 if (i <= 5)
3139 msleep(10);
3140 else if (i <= 10)
3141 msleep(500);
3142 else
3143 msleep(2500);
dea3101e 3144
41415862 3145 if (i == 15) {
2e0fef85 3146 /* Do post */
92d7f7b0 3147 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3148 lpfc_sli_brdrestart(phba);
3149 }
3150 /* Read the HBA Host Status Register */
3151 status = readl(phba->HSregaddr);
3152 }
dea3101e 3153
41415862
JW
3154 /* Check to see if any errors occurred during init */
3155 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3157 "2751 Adapter failed to restart, "
3158 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3159 status,
3160 readl(phba->MBslimaddr + 0xa8),
3161 readl(phba->MBslimaddr + 0xac));
2e0fef85 3162 phba->link_state = LPFC_HBA_ERROR;
41415862 3163 retval = 1;
dea3101e 3164 }
dea3101e 3165
41415862
JW
3166 return retval;
3167}
dea3101e 3168
da0436e9
JS
3169/**
3170 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3171 * @phba: Pointer to HBA context object.
3172 * @mask: Bit mask to be checked.
3173 *
3174 * This function checks the host status register to check if HBA is
3175 * ready. This function will wait in a loop for the HBA to be ready
3176 * If the HBA is not ready , the function will will reset the HBA PCI
3177 * function again. The function returns 1 when HBA fail to be ready
3178 * otherwise returns zero.
3179 **/
3180static int
3181lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3182{
3183 uint32_t status;
3184 int retval = 0;
3185
3186 /* Read the HBA Host Status Register */
3187 status = lpfc_sli4_post_status_check(phba);
3188
3189 if (status) {
3190 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3191 lpfc_sli_brdrestart(phba);
3192 status = lpfc_sli4_post_status_check(phba);
3193 }
3194
3195 /* Check to see if any errors occurred during init */
3196 if (status) {
3197 phba->link_state = LPFC_HBA_ERROR;
3198 retval = 1;
3199 } else
3200 phba->sli4_hba.intr_enable = 0;
3201
3202 return retval;
3203}
3204
3205/**
3206 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3207 * @phba: Pointer to HBA context object.
3208 * @mask: Bit mask to be checked.
3209 *
3210 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3211 * from the API jump table function pointer from the lpfc_hba struct.
3212 **/
3213int
3214lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3215{
3216 return phba->lpfc_sli_brdready(phba, mask);
3217}
3218
9290831f
JS
3219#define BARRIER_TEST_PATTERN (0xdeadbeef)
3220
e59058c4 3221/**
3621a710 3222 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3223 * @phba: Pointer to HBA context object.
3224 *
3225 * This function is called before resetting an HBA. This
3226 * function requests HBA to quiesce DMAs before a reset.
3227 **/
2e0fef85 3228void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3229{
65a29c16
JS
3230 uint32_t __iomem *resp_buf;
3231 uint32_t __iomem *mbox_buf;
9290831f
JS
3232 volatile uint32_t mbox;
3233 uint32_t hc_copy;
3234 int i;
3235 uint8_t hdrtype;
3236
3237 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3238 if (hdrtype != 0x80 ||
3239 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3240 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3241 return;
3242
3243 /*
3244 * Tell the other part of the chip to suspend temporarily all
3245 * its DMA activity.
3246 */
65a29c16 3247 resp_buf = phba->MBslimaddr;
9290831f
JS
3248
3249 /* Disable the error attention */
3250 hc_copy = readl(phba->HCregaddr);
3251 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3252 readl(phba->HCregaddr); /* flush */
2e0fef85 3253 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3254
3255 if (readl(phba->HAregaddr) & HA_ERATT) {
3256 /* Clear Chip error bit */
3257 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3258 phba->pport->stopped = 1;
9290831f
JS
3259 }
3260
3261 mbox = 0;
3262 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3263 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3264
3265 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3266 mbox_buf = phba->MBslimaddr;
9290831f
JS
3267 writel(mbox, mbox_buf);
3268
3269 for (i = 0;
3270 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3271 mdelay(1);
3272
3273 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3274 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3275 phba->pport->stopped)
9290831f
JS
3276 goto restore_hc;
3277 else
3278 goto clear_errat;
3279 }
3280
3281 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3282 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3283 mdelay(1);
3284
3285clear_errat:
3286
3287 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3288 mdelay(1);
3289
3290 if (readl(phba->HAregaddr) & HA_ERATT) {
3291 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3292 phba->pport->stopped = 1;
9290831f
JS
3293 }
3294
3295restore_hc:
2e0fef85 3296 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3297 writel(hc_copy, phba->HCregaddr);
3298 readl(phba->HCregaddr); /* flush */
3299}
3300
e59058c4 3301/**
3621a710 3302 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3303 * @phba: Pointer to HBA context object.
3304 *
3305 * This function issues a kill_board mailbox command and waits for
3306 * the error attention interrupt. This function is called for stopping
3307 * the firmware processing. The caller is not required to hold any
3308 * locks. This function calls lpfc_hba_down_post function to free
3309 * any pending commands after the kill. The function will return 1 when it
3310 * fails to kill the board else will return 0.
3311 **/
41415862 3312int
2e0fef85 3313lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3314{
3315 struct lpfc_sli *psli;
3316 LPFC_MBOXQ_t *pmb;
3317 uint32_t status;
3318 uint32_t ha_copy;
3319 int retval;
3320 int i = 0;
dea3101e 3321
41415862 3322 psli = &phba->sli;
dea3101e 3323
41415862 3324 /* Kill HBA */
ed957684 3325 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3326 "0329 Kill HBA Data: x%x x%x\n",
3327 phba->pport->port_state, psli->sli_flag);
41415862 3328
98c9ea5c
JS
3329 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3330 if (!pmb)
41415862 3331 return 1;
41415862
JW
3332
3333 /* Disable the error attention */
2e0fef85 3334 spin_lock_irq(&phba->hbalock);
41415862
JW
3335 status = readl(phba->HCregaddr);
3336 status &= ~HC_ERINT_ENA;
3337 writel(status, phba->HCregaddr);
3338 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3339 phba->link_flag |= LS_IGNORE_ERATT;
3340 spin_unlock_irq(&phba->hbalock);
41415862
JW
3341
3342 lpfc_kill_board(phba, pmb);
3343 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3344 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3345
3346 if (retval != MBX_SUCCESS) {
3347 if (retval != MBX_BUSY)
3348 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3350 "2752 KILL_BOARD command failed retval %d\n",
3351 retval);
2e0fef85
JS
3352 spin_lock_irq(&phba->hbalock);
3353 phba->link_flag &= ~LS_IGNORE_ERATT;
3354 spin_unlock_irq(&phba->hbalock);
41415862
JW
3355 return 1;
3356 }
3357
f4b4c68f
JS
3358 spin_lock_irq(&phba->hbalock);
3359 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3360 spin_unlock_irq(&phba->hbalock);
9290831f 3361
41415862
JW
3362 mempool_free(pmb, phba->mbox_mem_pool);
3363
3364 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3365 * attention every 100ms for 3 seconds. If we don't get ERATT after
3366 * 3 seconds we still set HBA_ERROR state because the status of the
3367 * board is now undefined.
3368 */
3369 ha_copy = readl(phba->HAregaddr);
3370
3371 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3372 mdelay(100);
3373 ha_copy = readl(phba->HAregaddr);
3374 }
3375
3376 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3377 if (ha_copy & HA_ERATT) {
3378 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3379 phba->pport->stopped = 1;
9290831f 3380 }
2e0fef85 3381 spin_lock_irq(&phba->hbalock);
41415862 3382 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3383 psli->mbox_active = NULL;
2e0fef85
JS
3384 phba->link_flag &= ~LS_IGNORE_ERATT;
3385 spin_unlock_irq(&phba->hbalock);
41415862 3386
41415862 3387 lpfc_hba_down_post(phba);
2e0fef85 3388 phba->link_state = LPFC_HBA_ERROR;
41415862 3389
2e0fef85 3390 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
3391}
3392
e59058c4 3393/**
3772a991 3394 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3395 * @phba: Pointer to HBA context object.
3396 *
3397 * This function resets the HBA by writing HC_INITFF to the control
3398 * register. After the HBA resets, this function resets all the iocb ring
3399 * indices. This function disables PCI layer parity checking during
3400 * the reset.
3401 * This function returns 0 always.
3402 * The caller is not required to hold any locks.
3403 **/
41415862 3404int
2e0fef85 3405lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3406{
41415862 3407 struct lpfc_sli *psli;
dea3101e 3408 struct lpfc_sli_ring *pring;
41415862 3409 uint16_t cfg_value;
dea3101e 3410 int i;
dea3101e 3411
41415862 3412 psli = &phba->sli;
dea3101e 3413
41415862
JW
3414 /* Reset HBA */
3415 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3416 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3417 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
3418
3419 /* perform board reset */
3420 phba->fc_eventTag = 0;
4d9ab994 3421 phba->link_events = 0;
2e0fef85
JS
3422 phba->pport->fc_myDID = 0;
3423 phba->pport->fc_prevDID = 0;
dea3101e 3424
41415862
JW
3425 /* Turn off parity checking and serr during the physical reset */
3426 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3427 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3428 (cfg_value &
3429 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3430
3772a991
JS
3431 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3432
41415862
JW
3433 /* Now toggle INITFF bit in the Host Control Register */
3434 writel(HC_INITFF, phba->HCregaddr);
3435 mdelay(1);
3436 readl(phba->HCregaddr); /* flush */
3437 writel(0, phba->HCregaddr);
3438 readl(phba->HCregaddr); /* flush */
3439
3440 /* Restore PCI cmd register */
3441 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
3442
3443 /* Initialize relevant SLI info */
41415862
JW
3444 for (i = 0; i < psli->num_rings; i++) {
3445 pring = &psli->ring[i];
dea3101e
JB
3446 pring->flag = 0;
3447 pring->rspidx = 0;
3448 pring->next_cmdidx = 0;
3449 pring->local_getidx = 0;
3450 pring->cmdidx = 0;
3451 pring->missbufcnt = 0;
3452 }
dea3101e 3453
2e0fef85 3454 phba->link_state = LPFC_WARM_START;
41415862
JW
3455 return 0;
3456}
3457
e59058c4 3458/**
da0436e9
JS
3459 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3460 * @phba: Pointer to HBA context object.
3461 *
3462 * This function resets a SLI4 HBA. This function disables PCI layer parity
3463 * checking during resets the device. The caller is not required to hold
3464 * any locks.
3465 *
3466 * This function returns 0 always.
3467 **/
3468int
3469lpfc_sli4_brdreset(struct lpfc_hba *phba)
3470{
3471 struct lpfc_sli *psli = &phba->sli;
3472 uint16_t cfg_value;
3473 uint8_t qindx;
3474
3475 /* Reset HBA */
3476 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3477 "0295 Reset HBA Data: x%x x%x\n",
3478 phba->pport->port_state, psli->sli_flag);
3479
3480 /* perform board reset */
3481 phba->fc_eventTag = 0;
4d9ab994 3482 phba->link_events = 0;
da0436e9
JS
3483 phba->pport->fc_myDID = 0;
3484 phba->pport->fc_prevDID = 0;
3485
3486 /* Turn off parity checking and serr during the physical reset */
3487 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3488 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3489 (cfg_value &
3490 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3491
3492 spin_lock_irq(&phba->hbalock);
3493 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3494 phba->fcf.fcf_flag = 0;
3495 /* Clean up the child queue list for the CQs */
3496 list_del_init(&phba->sli4_hba.mbx_wq->list);
3497 list_del_init(&phba->sli4_hba.els_wq->list);
3498 list_del_init(&phba->sli4_hba.hdr_rq->list);
3499 list_del_init(&phba->sli4_hba.dat_rq->list);
3500 list_del_init(&phba->sli4_hba.mbx_cq->list);
3501 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3502 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3503 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3504 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3505 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3506 spin_unlock_irq(&phba->hbalock);
3507
3508 /* Now physically reset the device */
3509 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3510 "0389 Performing PCI function reset!\n");
3511 /* Perform FCoE PCI function reset */
3512 lpfc_pci_function_reset(phba);
3513
3514 return 0;
3515}
3516
3517/**
3518 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3519 * @phba: Pointer to HBA context object.
3520 *
3521 * This function is called in the SLI initialization code path to
3522 * restart the HBA. The caller is not required to hold any lock.
3523 * This function writes MBX_RESTART mailbox command to the SLIM and
3524 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3525 * function to free any pending commands. The function enables
3526 * POST only during the first initialization. The function returns zero.
3527 * The function does not guarantee completion of MBX_RESTART mailbox
3528 * command before the return of this function.
3529 **/
da0436e9
JS
3530static int
3531lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3532{
3533 MAILBOX_t *mb;
3534 struct lpfc_sli *psli;
41415862
JW
3535 volatile uint32_t word0;
3536 void __iomem *to_slim;
0d878419 3537 uint32_t hba_aer_enabled;
41415862 3538
2e0fef85 3539 spin_lock_irq(&phba->hbalock);
41415862 3540
0d878419
JS
3541 /* Take PCIe device Advanced Error Reporting (AER) state */
3542 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3543
41415862
JW
3544 psli = &phba->sli;
3545
3546 /* Restart HBA */
3547 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3548 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3549 phba->pport->port_state, psli->sli_flag);
41415862
JW
3550
3551 word0 = 0;
3552 mb = (MAILBOX_t *) &word0;
3553 mb->mbxCommand = MBX_RESTART;
3554 mb->mbxHc = 1;
3555
9290831f
JS
3556 lpfc_reset_barrier(phba);
3557
41415862
JW
3558 to_slim = phba->MBslimaddr;
3559 writel(*(uint32_t *) mb, to_slim);
3560 readl(to_slim); /* flush */
3561
3562 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3563 if (phba->pport->port_state)
41415862 3564 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3565 else
41415862 3566 word0 = 0; /* This is really setting up word1 */
65a29c16 3567 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3568 writel(*(uint32_t *) mb, to_slim);
3569 readl(to_slim); /* flush */
dea3101e 3570
41415862 3571 lpfc_sli_brdreset(phba);
2e0fef85
JS
3572 phba->pport->stopped = 0;
3573 phba->link_state = LPFC_INIT_START;
da0436e9 3574 phba->hba_flag = 0;
2e0fef85 3575 spin_unlock_irq(&phba->hbalock);
41415862 3576
64ba8818
JS
3577 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3578 psli->stats_start = get_seconds();
3579
eaf15d5b
JS
3580 /* Give the INITFF and Post time to settle. */
3581 mdelay(100);
41415862 3582
0d878419
JS
3583 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3584 if (hba_aer_enabled)
3585 pci_disable_pcie_error_reporting(phba->pcidev);
3586
41415862 3587 lpfc_hba_down_post(phba);
dea3101e
JB
3588
3589 return 0;
3590}
3591
da0436e9
JS
3592/**
3593 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3594 * @phba: Pointer to HBA context object.
3595 *
3596 * This function is called in the SLI initialization code path to restart
3597 * a SLI4 HBA. The caller is not required to hold any lock.
3598 * At the end of the function, it calls lpfc_hba_down_post function to
3599 * free any pending commands.
3600 **/
3601static int
3602lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3603{
3604 struct lpfc_sli *psli = &phba->sli;
75baf696 3605 uint32_t hba_aer_enabled;
da0436e9
JS
3606
3607 /* Restart HBA */
3608 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3609 "0296 Restart HBA Data: x%x x%x\n",
3610 phba->pport->port_state, psli->sli_flag);
3611
75baf696
JS
3612 /* Take PCIe device Advanced Error Reporting (AER) state */
3613 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3614
da0436e9
JS
3615 lpfc_sli4_brdreset(phba);
3616
3617 spin_lock_irq(&phba->hbalock);
3618 phba->pport->stopped = 0;
3619 phba->link_state = LPFC_INIT_START;
3620 phba->hba_flag = 0;
3621 spin_unlock_irq(&phba->hbalock);
3622
3623 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3624 psli->stats_start = get_seconds();
3625
75baf696
JS
3626 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3627 if (hba_aer_enabled)
3628 pci_disable_pcie_error_reporting(phba->pcidev);
3629
da0436e9
JS
3630 lpfc_hba_down_post(phba);
3631
3632 return 0;
3633}
3634
3635/**
3636 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3637 * @phba: Pointer to HBA context object.
3638 *
3639 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3640 * API jump table function pointer from the lpfc_hba struct.
3641**/
3642int
3643lpfc_sli_brdrestart(struct lpfc_hba *phba)
3644{
3645 return phba->lpfc_sli_brdrestart(phba);
3646}
3647
e59058c4 3648/**
3621a710 3649 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3650 * @phba: Pointer to HBA context object.
3651 *
3652 * This function is called after a HBA restart to wait for successful
3653 * restart of the HBA. Successful restart of the HBA is indicated by
3654 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3655 * iteration, the function will restart the HBA again. The function returns
3656 * zero if HBA successfully restarted else returns negative error code.
3657 **/
dea3101e
JB
3658static int
3659lpfc_sli_chipset_init(struct lpfc_hba *phba)
3660{
3661 uint32_t status, i = 0;
3662
3663 /* Read the HBA Host Status Register */
3664 status = readl(phba->HSregaddr);
3665
3666 /* Check status register to see what current state is */
3667 i = 0;
3668 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3669
dcf2a4e0
JS
3670 /* Check every 10ms for 10 retries, then every 100ms for 90
3671 * retries, then every 1 sec for 50 retires for a total of
3672 * ~60 seconds before reset the board again and check every
3673 * 1 sec for 50 retries. The up to 60 seconds before the
3674 * board ready is required by the Falcon FIPS zeroization
3675 * complete, and any reset the board in between shall cause
3676 * restart of zeroization, further delay the board ready.
dea3101e 3677 */
dcf2a4e0 3678 if (i++ >= 200) {
dea3101e
JB
3679 /* Adapter failed to init, timeout, status reg
3680 <status> */
ed957684 3681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3682 "0436 Adapter failed to init, "
09372820
JS
3683 "timeout, status reg x%x, "
3684 "FW Data: A8 x%x AC x%x\n", status,
3685 readl(phba->MBslimaddr + 0xa8),
3686 readl(phba->MBslimaddr + 0xac));
2e0fef85 3687 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3688 return -ETIMEDOUT;
3689 }
3690
3691 /* Check to see if any errors occurred during init */
3692 if (status & HS_FFERM) {
3693 /* ERROR: During chipset initialization */
3694 /* Adapter failed to init, chipset, status reg
3695 <status> */
ed957684 3696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3697 "0437 Adapter failed to init, "
09372820
JS
3698 "chipset, status reg x%x, "
3699 "FW Data: A8 x%x AC x%x\n", status,
3700 readl(phba->MBslimaddr + 0xa8),
3701 readl(phba->MBslimaddr + 0xac));
2e0fef85 3702 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3703 return -EIO;
3704 }
3705
dcf2a4e0 3706 if (i <= 10)
dea3101e 3707 msleep(10);
dcf2a4e0
JS
3708 else if (i <= 100)
3709 msleep(100);
3710 else
3711 msleep(1000);
dea3101e 3712
dcf2a4e0
JS
3713 if (i == 150) {
3714 /* Do post */
92d7f7b0 3715 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3716 lpfc_sli_brdrestart(phba);
dea3101e
JB
3717 }
3718 /* Read the HBA Host Status Register */
3719 status = readl(phba->HSregaddr);
3720 }
3721
3722 /* Check to see if any errors occurred during init */
3723 if (status & HS_FFERM) {
3724 /* ERROR: During chipset initialization */
3725 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3727 "0438 Adapter failed to init, chipset, "
09372820
JS
3728 "status reg x%x, "
3729 "FW Data: A8 x%x AC x%x\n", status,
3730 readl(phba->MBslimaddr + 0xa8),
3731 readl(phba->MBslimaddr + 0xac));
2e0fef85 3732 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3733 return -EIO;
3734 }
3735
3736 /* Clear all interrupt enable conditions */
3737 writel(0, phba->HCregaddr);
3738 readl(phba->HCregaddr); /* flush */
3739
3740 /* setup host attn register */
3741 writel(0xffffffff, phba->HAregaddr);
3742 readl(phba->HAregaddr); /* flush */
3743 return 0;
3744}
3745
e59058c4 3746/**
3621a710 3747 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3748 *
3749 * This function calculates and returns the number of HBQs required to be
3750 * configured.
3751 **/
78b2d852 3752int
ed957684
JS
3753lpfc_sli_hbq_count(void)
3754{
92d7f7b0 3755 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3756}
3757
e59058c4 3758/**
3621a710 3759 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3760 *
3761 * This function adds the number of hbq entries in every HBQ to get
3762 * the total number of hbq entries required for the HBA and returns
3763 * the total count.
3764 **/
ed957684
JS
3765static int
3766lpfc_sli_hbq_entry_count(void)
3767{
3768 int hbq_count = lpfc_sli_hbq_count();
3769 int count = 0;
3770 int i;
3771
3772 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3773 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3774 return count;
3775}
3776
e59058c4 3777/**
3621a710 3778 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3779 *
3780 * This function calculates amount of memory required for all hbq entries
3781 * to be configured and returns the total memory required.
3782 **/
dea3101e 3783int
ed957684
JS
3784lpfc_sli_hbq_size(void)
3785{
3786 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3787}
3788
e59058c4 3789/**
3621a710 3790 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3791 * @phba: Pointer to HBA context object.
3792 *
3793 * This function is called during the SLI initialization to configure
3794 * all the HBQs and post buffers to the HBQ. The caller is not
3795 * required to hold any locks. This function will return zero if successful
3796 * else it will return negative error code.
3797 **/
ed957684
JS
3798static int
3799lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3800{
3801 int hbq_count = lpfc_sli_hbq_count();
3802 LPFC_MBOXQ_t *pmb;
3803 MAILBOX_t *pmbox;
3804 uint32_t hbqno;
3805 uint32_t hbq_entry_index;
ed957684 3806
92d7f7b0
JS
3807 /* Get a Mailbox buffer to setup mailbox
3808 * commands for HBA initialization
3809 */
ed957684
JS
3810 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3811
3812 if (!pmb)
3813 return -ENOMEM;
3814
04c68496 3815 pmbox = &pmb->u.mb;
ed957684
JS
3816
3817 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3818 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3819 phba->hbq_in_use = 1;
ed957684
JS
3820
3821 hbq_entry_index = 0;
3822 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3823 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3824 phba->hbqs[hbqno].hbqPutIdx = 0;
3825 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3826 phba->hbqs[hbqno].entry_count =
92d7f7b0 3827 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3828 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3829 hbq_entry_index, pmb);
ed957684
JS
3830 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3831
3832 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3833 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3834 mbxStatus <status>, ring <num> */
3835
3836 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3837 LOG_SLI | LOG_VPORT,
e8b62011 3838 "1805 Adapter failed to init. "
ed957684 3839 "Data: x%x x%x x%x\n",
e8b62011 3840 pmbox->mbxCommand,
ed957684
JS
3841 pmbox->mbxStatus, hbqno);
3842
3843 phba->link_state = LPFC_HBA_ERROR;
3844 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 3845 return -ENXIO;
ed957684
JS
3846 }
3847 }
3848 phba->hbq_count = hbq_count;
3849
ed957684
JS
3850 mempool_free(pmb, phba->mbox_mem_pool);
3851
92d7f7b0 3852 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3853 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3854 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3855 return 0;
3856}
3857
4f774513
JS
3858/**
3859 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3860 * @phba: Pointer to HBA context object.
3861 *
3862 * This function is called during the SLI initialization to configure
3863 * all the HBQs and post buffers to the HBQ. The caller is not
3864 * required to hold any locks. This function will return zero if successful
3865 * else it will return negative error code.
3866 **/
3867static int
3868lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3869{
3870 phba->hbq_in_use = 1;
3871 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3872 phba->hbq_count = 1;
3873 /* Initially populate or replenish the HBQs */
3874 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3875 return 0;
3876}
3877
e59058c4 3878/**
3621a710 3879 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3880 * @phba: Pointer to HBA context object.
3881 * @sli_mode: sli mode - 2/3
3882 *
3883 * This function is called by the sli intialization code path
3884 * to issue config_port mailbox command. This function restarts the
3885 * HBA firmware and issues a config_port mailbox command to configure
3886 * the SLI interface in the sli mode specified by sli_mode
3887 * variable. The caller is not required to hold any locks.
3888 * The function returns 0 if successful, else returns negative error
3889 * code.
3890 **/
9399627f
JS
3891int
3892lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
3893{
3894 LPFC_MBOXQ_t *pmb;
3895 uint32_t resetcount = 0, rc = 0, done = 0;
3896
3897 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3898 if (!pmb) {
2e0fef85 3899 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3900 return -ENOMEM;
3901 }
3902
ed957684 3903 phba->sli_rev = sli_mode;
dea3101e 3904 while (resetcount < 2 && !done) {
2e0fef85 3905 spin_lock_irq(&phba->hbalock);
1c067a42 3906 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3907 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3908 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3909 lpfc_sli_brdrestart(phba);
dea3101e
JB
3910 rc = lpfc_sli_chipset_init(phba);
3911 if (rc)
3912 break;
3913
2e0fef85 3914 spin_lock_irq(&phba->hbalock);
1c067a42 3915 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3916 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
3917 resetcount++;
3918
ed957684
JS
3919 /* Call pre CONFIG_PORT mailbox command initialization. A
3920 * value of 0 means the call was successful. Any other
3921 * nonzero value is a failure, but if ERESTART is returned,
3922 * the driver may reset the HBA and try again.
3923 */
dea3101e
JB
3924 rc = lpfc_config_port_prep(phba);
3925 if (rc == -ERESTART) {
ed957684 3926 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3927 continue;
34b02dcd 3928 } else if (rc)
dea3101e 3929 break;
2e0fef85 3930 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
3931 lpfc_config_port(phba, pmb);
3932 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3933 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3934 LPFC_SLI3_HBQ_ENABLED |
3935 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
3936 LPFC_SLI3_BG_ENABLED |
3937 LPFC_SLI3_DSS_ENABLED);
ed957684 3938 if (rc != MBX_SUCCESS) {
dea3101e 3939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3940 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3941 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3942 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3943 spin_lock_irq(&phba->hbalock);
04c68496 3944 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3945 spin_unlock_irq(&phba->hbalock);
3946 rc = -ENXIO;
04c68496
JS
3947 } else {
3948 /* Allow asynchronous mailbox command to go through */
3949 spin_lock_irq(&phba->hbalock);
3950 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3951 spin_unlock_irq(&phba->hbalock);
ed957684 3952 done = 1;
04c68496 3953 }
dea3101e 3954 }
ed957684
JS
3955 if (!done) {
3956 rc = -EINVAL;
3957 goto do_prep_failed;
3958 }
04c68496
JS
3959 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3960 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3961 rc = -ENXIO;
3962 goto do_prep_failed;
3963 }
04c68496 3964 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3965 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3966 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3967 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3968 phba->max_vpi : phba->max_vports;
3969
34b02dcd
JS
3970 } else
3971 phba->max_vpi = 0;
bc73905a
JS
3972 phba->fips_level = 0;
3973 phba->fips_spec_rev = 0;
3974 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 3975 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
3976 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
3977 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
3978 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3979 "2850 Security Crypto Active. FIPS x%d "
3980 "(Spec Rev: x%d)",
3981 phba->fips_level, phba->fips_spec_rev);
3982 }
3983 if (pmb->u.mb.un.varCfgPort.sec_err) {
3984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3985 "2856 Config Port Security Crypto "
3986 "Error: x%x ",
3987 pmb->u.mb.un.varCfgPort.sec_err);
3988 }
04c68496 3989 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3990 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3991 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3992 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
3993
3994 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3995 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
3996
3997 if (phba->cfg_enable_bg) {
04c68496 3998 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
3999 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4000 else
4001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4002 "0443 Adapter did not grant "
4003 "BlockGuard\n");
4004 }
34b02dcd 4005 } else {
8f34f4ce 4006 phba->hbq_get = NULL;
34b02dcd 4007 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4008 phba->max_vpi = 0;
ed957684 4009 }
92d7f7b0 4010do_prep_failed:
ed957684
JS
4011 mempool_free(pmb, phba->mbox_mem_pool);
4012 return rc;
4013}
4014
e59058c4
JS
4015
4016/**
3621a710 4017 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4018 * @phba: Pointer to HBA context object.
4019 *
4020 * This function is the main SLI intialization function. This function
4021 * is called by the HBA intialization code, HBA reset code and HBA
4022 * error attention handler code. Caller is not required to hold any
4023 * locks. This function issues config_port mailbox command to configure
4024 * the SLI, setup iocb rings and HBQ rings. In the end the function
4025 * calls the config_port_post function to issue init_link mailbox
4026 * command and to start the discovery. The function will return zero
4027 * if successful, else it will return negative error code.
4028 **/
ed957684
JS
4029int
4030lpfc_sli_hba_setup(struct lpfc_hba *phba)
4031{
4032 uint32_t rc;
92d7f7b0 4033 int mode = 3;
ed957684
JS
4034
4035 switch (lpfc_sli_mode) {
4036 case 2:
78b2d852 4037 if (phba->cfg_enable_npiv) {
92d7f7b0 4038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4039 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4040 "parameter (%d) to auto (0).\n",
e8b62011 4041 lpfc_sli_mode);
92d7f7b0
JS
4042 break;
4043 }
ed957684
JS
4044 mode = 2;
4045 break;
4046 case 0:
4047 case 3:
4048 break;
4049 default:
92d7f7b0 4050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4051 "1819 Unrecognized lpfc_sli_mode "
4052 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4053
4054 break;
4055 }
4056
9399627f
JS
4057 rc = lpfc_sli_config_port(phba, mode);
4058
ed957684 4059 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4061 "1820 Unable to select SLI-3. "
4062 "Not supported by adapter.\n");
ed957684 4063 if (rc && mode != 2)
9399627f 4064 rc = lpfc_sli_config_port(phba, 2);
ed957684 4065 if (rc)
dea3101e
JB
4066 goto lpfc_sli_hba_setup_error;
4067
0d878419
JS
4068 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4069 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4070 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4071 if (!rc) {
4072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4073 "2709 This device supports "
4074 "Advanced Error Reporting (AER)\n");
4075 spin_lock_irq(&phba->hbalock);
4076 phba->hba_flag |= HBA_AER_ENABLED;
4077 spin_unlock_irq(&phba->hbalock);
4078 } else {
4079 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4080 "2708 This device does not support "
4081 "Advanced Error Reporting (AER)\n");
4082 phba->cfg_aer_support = 0;
4083 }
4084 }
4085
ed957684
JS
4086 if (phba->sli_rev == 3) {
4087 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4088 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4089 } else {
4090 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4091 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4092 phba->sli3_options = 0;
ed957684
JS
4093 }
4094
4095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4096 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4097 phba->sli_rev, phba->max_vpi);
ed957684 4098 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4099
4100 if (rc)
4101 goto lpfc_sli_hba_setup_error;
4102
9399627f 4103 /* Init HBQs */
ed957684
JS
4104 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4105 rc = lpfc_sli_hbq_setup(phba);
4106 if (rc)
4107 goto lpfc_sli_hba_setup_error;
4108 }
04c68496 4109 spin_lock_irq(&phba->hbalock);
dea3101e 4110 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4111 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4112
4113 rc = lpfc_config_port_post(phba);
4114 if (rc)
4115 goto lpfc_sli_hba_setup_error;
4116
ed957684
JS
4117 return rc;
4118
92d7f7b0 4119lpfc_sli_hba_setup_error:
2e0fef85 4120 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4122 "0445 Firmware initialization failed\n");
dea3101e
JB
4123 return rc;
4124}
4125
e59058c4 4126/**
da0436e9
JS
4127 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4128 * @phba: Pointer to HBA context object.
4129 * @mboxq: mailbox pointer.
4130 * This function issue a dump mailbox command to read config region
4131 * 23 and parse the records in the region and populate driver
4132 * data structure.
e59058c4 4133 **/
da0436e9
JS
4134static int
4135lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4136 LPFC_MBOXQ_t *mboxq)
dea3101e 4137{
da0436e9
JS
4138 struct lpfc_dmabuf *mp;
4139 struct lpfc_mqe *mqe;
4140 uint32_t data_length;
4141 int rc;
dea3101e 4142
da0436e9
JS
4143 /* Program the default value of vlan_id and fc_map */
4144 phba->valid_vlan = 0;
4145 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4146 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4147 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4148
da0436e9
JS
4149 mqe = &mboxq->u.mqe;
4150 if (lpfc_dump_fcoe_param(phba, mboxq))
4151 return -ENOMEM;
4152
4153 mp = (struct lpfc_dmabuf *) mboxq->context1;
4154 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4155
4156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4157 "(%d):2571 Mailbox cmd x%x Status x%x "
4158 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4159 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4160 "CQ: x%x x%x x%x x%x\n",
4161 mboxq->vport ? mboxq->vport->vpi : 0,
4162 bf_get(lpfc_mqe_command, mqe),
4163 bf_get(lpfc_mqe_status, mqe),
4164 mqe->un.mb_words[0], mqe->un.mb_words[1],
4165 mqe->un.mb_words[2], mqe->un.mb_words[3],
4166 mqe->un.mb_words[4], mqe->un.mb_words[5],
4167 mqe->un.mb_words[6], mqe->un.mb_words[7],
4168 mqe->un.mb_words[8], mqe->un.mb_words[9],
4169 mqe->un.mb_words[10], mqe->un.mb_words[11],
4170 mqe->un.mb_words[12], mqe->un.mb_words[13],
4171 mqe->un.mb_words[14], mqe->un.mb_words[15],
4172 mqe->un.mb_words[16], mqe->un.mb_words[50],
4173 mboxq->mcqe.word0,
4174 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4175 mboxq->mcqe.trailer);
4176
4177 if (rc) {
4178 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4179 kfree(mp);
4180 return -EIO;
4181 }
4182 data_length = mqe->un.mb_words[5];
a0c87cbd 4183 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4184 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4185 kfree(mp);
da0436e9 4186 return -EIO;
d11e31dd 4187 }
dea3101e 4188
da0436e9
JS
4189 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4190 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4191 kfree(mp);
4192 return 0;
4193}
e59058c4
JS
4194
4195/**
da0436e9
JS
4196 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4197 * @phba: pointer to lpfc hba data structure.
4198 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4199 * @vpd: pointer to the memory to hold resulting port vpd data.
4200 * @vpd_size: On input, the number of bytes allocated to @vpd.
4201 * On output, the number of data bytes in @vpd.
e59058c4 4202 *
da0436e9
JS
4203 * This routine executes a READ_REV SLI4 mailbox command. In
4204 * addition, this routine gets the port vpd data.
4205 *
4206 * Return codes
af901ca1 4207 * 0 - successful
d439d286 4208 * -ENOMEM - could not allocated memory.
e59058c4 4209 **/
da0436e9
JS
4210static int
4211lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4212 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4213{
da0436e9
JS
4214 int rc = 0;
4215 uint32_t dma_size;
4216 struct lpfc_dmabuf *dmabuf;
4217 struct lpfc_mqe *mqe;
dea3101e 4218
da0436e9
JS
4219 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4220 if (!dmabuf)
4221 return -ENOMEM;
4222
4223 /*
4224 * Get a DMA buffer for the vpd data resulting from the READ_REV
4225 * mailbox command.
a257bf90 4226 */
da0436e9
JS
4227 dma_size = *vpd_size;
4228 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4229 dma_size,
4230 &dmabuf->phys,
4231 GFP_KERNEL);
4232 if (!dmabuf->virt) {
4233 kfree(dmabuf);
4234 return -ENOMEM;
a257bf90 4235 }
da0436e9 4236 memset(dmabuf->virt, 0, dma_size);
a257bf90 4237
da0436e9
JS
4238 /*
4239 * The SLI4 implementation of READ_REV conflicts at word1,
4240 * bits 31:16 and SLI4 adds vpd functionality not present
4241 * in SLI3. This code corrects the conflicts.
1dcb58e5 4242 */
da0436e9
JS
4243 lpfc_read_rev(phba, mboxq);
4244 mqe = &mboxq->u.mqe;
4245 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4246 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4247 mqe->un.read_rev.word1 &= 0x0000FFFF;
4248 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4249 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4250
4251 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4252 if (rc) {
4253 dma_free_coherent(&phba->pcidev->dev, dma_size,
4254 dmabuf->virt, dmabuf->phys);
def9c7a9 4255 kfree(dmabuf);
da0436e9
JS
4256 return -EIO;
4257 }
1dcb58e5 4258
da0436e9
JS
4259 /*
4260 * The available vpd length cannot be bigger than the
4261 * DMA buffer passed to the port. Catch the less than
4262 * case and update the caller's size.
4263 */
4264 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4265 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4266
d7c47992
JS
4267 memcpy(vpd, dmabuf->virt, *vpd_size);
4268
da0436e9
JS
4269 dma_free_coherent(&phba->pcidev->dev, dma_size,
4270 dmabuf->virt, dmabuf->phys);
4271 kfree(dmabuf);
4272 return 0;
dea3101e
JB
4273}
4274
e59058c4 4275/**
da0436e9
JS
4276 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4277 * @phba: pointer to lpfc hba data structure.
e59058c4 4278 *
da0436e9
JS
4279 * This routine is called to explicitly arm the SLI4 device's completion and
4280 * event queues
4281 **/
4282static void
4283lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4284{
4285 uint8_t fcp_eqidx;
4286
4287 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4288 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4289 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4290 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4291 LPFC_QUEUE_REARM);
4292 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4293 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4294 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4295 LPFC_QUEUE_REARM);
4296}
4297
4298/**
4299 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4300 * @phba: Pointer to HBA context object.
4301 *
4302 * This function is the main SLI4 device intialization PCI function. This
4303 * function is called by the HBA intialization code, HBA reset code and
4304 * HBA error attention handler code. Caller is not required to hold any
4305 * locks.
4306 **/
4307int
4308lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4309{
4310 int rc;
4311 LPFC_MBOXQ_t *mboxq;
4312 struct lpfc_mqe *mqe;
4313 uint8_t *vpd;
4314 uint32_t vpd_size;
4315 uint32_t ftr_rsp = 0;
4316 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4317 struct lpfc_vport *vport = phba->pport;
4318 struct lpfc_dmabuf *mp;
4319
4320 /* Perform a PCI function reset to start from clean */
4321 rc = lpfc_pci_function_reset(phba);
4322 if (unlikely(rc))
4323 return -ENODEV;
4324
4325 /* Check the HBA Host Status Register for readyness */
4326 rc = lpfc_sli4_post_status_check(phba);
4327 if (unlikely(rc))
4328 return -ENODEV;
4329 else {
4330 spin_lock_irq(&phba->hbalock);
4331 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4332 spin_unlock_irq(&phba->hbalock);
4333 }
4334
4335 /*
4336 * Allocate a single mailbox container for initializing the
4337 * port.
4338 */
4339 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4340 if (!mboxq)
4341 return -ENOMEM;
4342
4343 /*
4344 * Continue initialization with default values even if driver failed
4345 * to read FCoE param config regions
4346 */
4347 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4349 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4350
4351 /* Issue READ_REV to collect vpd and FW information. */
49198b37 4352 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
4353 vpd = kzalloc(vpd_size, GFP_KERNEL);
4354 if (!vpd) {
4355 rc = -ENOMEM;
4356 goto out_free_mbox;
4357 }
4358
4359 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4360 if (unlikely(rc))
4361 goto out_free_vpd;
4362
4363 mqe = &mboxq->u.mqe;
f1126688
JS
4364 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4365 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4366 phba->hba_flag |= HBA_FCOE_SUPPORT;
45ed1190
JS
4367
4368 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4369 LPFC_DCBX_CEE_MODE)
4370 phba->hba_flag |= HBA_FIP_SUPPORT;
4371 else
4372 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4373
f1126688
JS
4374 if (phba->sli_rev != LPFC_SLI_REV4 ||
4375 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4377 "0376 READ_REV Error. SLI Level %d "
4378 "FCoE enabled %d\n",
f1126688 4379 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4380 rc = -EIO;
4381 goto out_free_vpd;
4382 }
da0436e9
JS
4383 /*
4384 * Evaluate the read rev and vpd data. Populate the driver
4385 * state with the results. If this routine fails, the failure
4386 * is not fatal as the driver will use generic values.
4387 */
4388 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4389 if (unlikely(!rc)) {
4390 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4391 "0377 Error %d parsing vpd. "
4392 "Using defaults.\n", rc);
4393 rc = 0;
4394 }
4395
f1126688
JS
4396 /* Save information as VPD data */
4397 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4398 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4399 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4400 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4401 &mqe->un.read_rev);
4402 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4403 &mqe->un.read_rev);
4404 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4405 &mqe->un.read_rev);
4406 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4407 &mqe->un.read_rev);
4408 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4409 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4410 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4411 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4412 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4413 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4414 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4415 "(%d):0380 READ_REV Status x%x "
4416 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4417 mboxq->vport ? mboxq->vport->vpi : 0,
4418 bf_get(lpfc_mqe_status, mqe),
4419 phba->vpd.rev.opFwName,
4420 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4421 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4422
4423 /*
4424 * Discover the port's supported feature set and match it against the
4425 * hosts requests.
4426 */
4427 lpfc_request_features(phba, mboxq);
4428 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4429 if (unlikely(rc)) {
4430 rc = -EIO;
4431 goto out_free_vpd;
4432 }
4433
4434 /*
4435 * The port must support FCP initiator mode as this is the
4436 * only mode running in the host.
4437 */
4438 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4439 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4440 "0378 No support for fcpi mode.\n");
4441 ftr_rsp++;
4442 }
4443
4444 /*
4445 * If the port cannot support the host's requested features
4446 * then turn off the global config parameters to disable the
4447 * feature in the driver. This is not a fatal error.
4448 */
4449 if ((phba->cfg_enable_bg) &&
4450 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4451 ftr_rsp++;
4452
4453 if (phba->max_vpi && phba->cfg_enable_npiv &&
4454 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4455 ftr_rsp++;
4456
4457 if (ftr_rsp) {
4458 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4459 "0379 Feature Mismatch Data: x%08x %08x "
4460 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4461 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4462 phba->cfg_enable_npiv, phba->max_vpi);
4463 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4464 phba->cfg_enable_bg = 0;
4465 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4466 phba->cfg_enable_npiv = 0;
4467 }
4468
4469 /* These SLI3 features are assumed in SLI4 */
4470 spin_lock_irq(&phba->hbalock);
4471 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4472 spin_unlock_irq(&phba->hbalock);
4473
4474 /* Read the port's service parameters. */
9f1177a3
JS
4475 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4476 if (rc) {
4477 phba->link_state = LPFC_HBA_ERROR;
4478 rc = -ENOMEM;
4479 goto out_free_vpd;
4480 }
4481
da0436e9
JS
4482 mboxq->vport = vport;
4483 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4484 mp = (struct lpfc_dmabuf *) mboxq->context1;
4485 if (rc == MBX_SUCCESS) {
4486 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4487 rc = 0;
4488 }
4489
4490 /*
4491 * This memory was allocated by the lpfc_read_sparam routine. Release
4492 * it to the mbuf pool.
4493 */
4494 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4495 kfree(mp);
4496 mboxq->context1 = NULL;
4497 if (unlikely(rc)) {
4498 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4499 "0382 READ_SPARAM command failed "
4500 "status %d, mbxStatus x%x\n",
4501 rc, bf_get(lpfc_mqe_status, mqe));
4502 phba->link_state = LPFC_HBA_ERROR;
4503 rc = -EIO;
4504 goto out_free_vpd;
4505 }
4506
4507 if (phba->cfg_soft_wwnn)
4508 u64_to_wwn(phba->cfg_soft_wwnn,
4509 vport->fc_sparam.nodeName.u.wwn);
4510 if (phba->cfg_soft_wwpn)
4511 u64_to_wwn(phba->cfg_soft_wwpn,
4512 vport->fc_sparam.portName.u.wwn);
4513 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4514 sizeof(struct lpfc_name));
4515 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4516 sizeof(struct lpfc_name));
4517
4518 /* Update the fc_host data structures with new wwn. */
4519 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4520 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4521
4522 /* Register SGL pool to the device using non-embedded mailbox command */
4523 rc = lpfc_sli4_post_sgl_list(phba);
4524 if (unlikely(rc)) {
4525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4526 "0582 Error %d during sgl post operation\n",
4527 rc);
da0436e9
JS
4528 rc = -ENODEV;
4529 goto out_free_vpd;
4530 }
4531
4532 /* Register SCSI SGL pool to the device */
4533 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4534 if (unlikely(rc)) {
4535 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4536 "0383 Error %d during scsi sgl post "
4537 "operation\n", rc);
da0436e9
JS
4538 /* Some Scsi buffers were moved to the abort scsi list */
4539 /* A pci function reset will repost them */
4540 rc = -ENODEV;
4541 goto out_free_vpd;
4542 }
4543
4544 /* Post the rpi header region to the device. */
4545 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4546 if (unlikely(rc)) {
4547 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4548 "0393 Error %d during rpi post operation\n",
4549 rc);
4550 rc = -ENODEV;
4551 goto out_free_vpd;
4552 }
da0436e9
JS
4553
4554 /* Set up all the queues to the device */
4555 rc = lpfc_sli4_queue_setup(phba);
4556 if (unlikely(rc)) {
4557 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4558 "0381 Error %d during queue setup.\n ", rc);
4559 goto out_stop_timers;
4560 }
4561
4562 /* Arm the CQs and then EQs on device */
4563 lpfc_sli4_arm_cqeq_intr(phba);
4564
4565 /* Indicate device interrupt mode */
4566 phba->sli4_hba.intr_enable = 1;
4567
4568 /* Allow asynchronous mailbox command to go through */
4569 spin_lock_irq(&phba->hbalock);
4570 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4571 spin_unlock_irq(&phba->hbalock);
4572
4573 /* Post receive buffers to the device */
4574 lpfc_sli4_rb_setup(phba);
4575
fc2b989b
JS
4576 /* Reset HBA FCF states after HBA reset */
4577 phba->fcf.fcf_flag = 0;
4578 phba->fcf.current_rec.flag = 0;
4579
da0436e9 4580 /* Start the ELS watchdog timer */
8fa38513
JS
4581 mod_timer(&vport->els_tmofunc,
4582 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4583
4584 /* Start heart beat timer */
4585 mod_timer(&phba->hb_tmofunc,
4586 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4587 phba->hb_outstanding = 0;
4588 phba->last_completion_time = jiffies;
4589
4590 /* Start error attention (ERATT) polling timer */
4591 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4592
75baf696
JS
4593 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4594 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4595 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4596 if (!rc) {
4597 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4598 "2829 This device supports "
4599 "Advanced Error Reporting (AER)\n");
4600 spin_lock_irq(&phba->hbalock);
4601 phba->hba_flag |= HBA_AER_ENABLED;
4602 spin_unlock_irq(&phba->hbalock);
4603 } else {
4604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4605 "2830 This device does not support "
4606 "Advanced Error Reporting (AER)\n");
4607 phba->cfg_aer_support = 0;
4608 }
4609 }
4610
da0436e9
JS
4611 /*
4612 * The port is ready, set the host's link state to LINK_DOWN
4613 * in preparation for link interrupts.
4614 */
4615 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4616 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4617 lpfc_set_loopback_flag(phba);
4618 /* Change driver state to LPFC_LINK_DOWN right before init link */
4619 spin_lock_irq(&phba->hbalock);
4620 phba->link_state = LPFC_LINK_DOWN;
4621 spin_unlock_irq(&phba->hbalock);
4622 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4623 if (unlikely(rc != MBX_NOT_FINISHED)) {
4624 kfree(vpd);
4625 return 0;
4626 } else
4627 rc = -EIO;
4628
4629 /* Unset all the queues set up in this routine when error out */
4630 if (rc)
4631 lpfc_sli4_queue_unset(phba);
4632
4633out_stop_timers:
4634 if (rc)
4635 lpfc_stop_hba_timers(phba);
4636out_free_vpd:
4637 kfree(vpd);
4638out_free_mbox:
4639 mempool_free(mboxq, phba->mbox_mem_pool);
4640 return rc;
4641}
4642
4643/**
4644 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4645 * @ptr: context object - pointer to hba structure.
4646 *
4647 * This is the callback function for mailbox timer. The mailbox
4648 * timer is armed when a new mailbox command is issued and the timer
4649 * is deleted when the mailbox complete. The function is called by
4650 * the kernel timer code when a mailbox does not complete within
4651 * expected time. This function wakes up the worker thread to
4652 * process the mailbox timeout and returns. All the processing is
4653 * done by the worker thread function lpfc_mbox_timeout_handler.
4654 **/
4655void
4656lpfc_mbox_timeout(unsigned long ptr)
4657{
4658 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4659 unsigned long iflag;
4660 uint32_t tmo_posted;
4661
4662 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4663 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4664 if (!tmo_posted)
4665 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4666 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4667
4668 if (!tmo_posted)
4669 lpfc_worker_wake_up(phba);
4670 return;
4671}
4672
4673
4674/**
4675 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4676 * @phba: Pointer to HBA context object.
4677 *
4678 * This function is called from worker thread when a mailbox command times out.
4679 * The caller is not required to hold any locks. This function will reset the
4680 * HBA and recover all the pending commands.
4681 **/
4682void
4683lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4684{
4685 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4686 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4687 struct lpfc_sli *psli = &phba->sli;
4688 struct lpfc_sli_ring *pring;
4689
4690 /* Check the pmbox pointer first. There is a race condition
4691 * between the mbox timeout handler getting executed in the
4692 * worklist and the mailbox actually completing. When this
4693 * race condition occurs, the mbox_active will be NULL.
4694 */
4695 spin_lock_irq(&phba->hbalock);
4696 if (pmbox == NULL) {
4697 lpfc_printf_log(phba, KERN_WARNING,
4698 LOG_MBOX | LOG_SLI,
4699 "0353 Active Mailbox cleared - mailbox timeout "
4700 "exiting\n");
4701 spin_unlock_irq(&phba->hbalock);
4702 return;
4703 }
4704
4705 /* Mbox cmd <mbxCommand> timeout */
4706 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4707 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4708 mb->mbxCommand,
4709 phba->pport->port_state,
4710 phba->sli.sli_flag,
4711 phba->sli.mbox_active);
4712 spin_unlock_irq(&phba->hbalock);
4713
4714 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4715 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4716 * it to fail all oustanding SCSI IO.
4717 */
4718 spin_lock_irq(&phba->pport->work_port_lock);
4719 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4720 spin_unlock_irq(&phba->pport->work_port_lock);
4721 spin_lock_irq(&phba->hbalock);
4722 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4723 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4724 spin_unlock_irq(&phba->hbalock);
4725
4726 pring = &psli->ring[psli->fcp_ring];
4727 lpfc_sli_abort_iocb_ring(phba, pring);
4728
4729 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4730 "0345 Resetting board due to mailbox timeout\n");
4731
4732 /* Reset the HBA device */
4733 lpfc_reset_hba(phba);
4734}
4735
4736/**
4737 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4738 * @phba: Pointer to HBA context object.
4739 * @pmbox: Pointer to mailbox object.
4740 * @flag: Flag indicating how the mailbox need to be processed.
4741 *
4742 * This function is called by discovery code and HBA management code
4743 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4744 * function gets the hbalock to protect the data structures.
4745 * The mailbox command can be submitted in polling mode, in which case
4746 * this function will wait in a polling loop for the completion of the
4747 * mailbox.
4748 * If the mailbox is submitted in no_wait mode (not polling) the
4749 * function will submit the command and returns immediately without waiting
4750 * for the mailbox completion. The no_wait is supported only when HBA
4751 * is in SLI2/SLI3 mode - interrupts are enabled.
4752 * The SLI interface allows only one mailbox pending at a time. If the
4753 * mailbox is issued in polling mode and there is already a mailbox
4754 * pending, then the function will return an error. If the mailbox is issued
4755 * in NO_WAIT mode and there is a mailbox pending already, the function
4756 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4757 * The sli layer owns the mailbox object until the completion of mailbox
4758 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4759 * return codes the caller owns the mailbox command after the return of
4760 * the function.
e59058c4 4761 **/
3772a991
JS
4762static int
4763lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4764 uint32_t flag)
dea3101e 4765{
dea3101e 4766 MAILBOX_t *mb;
2e0fef85 4767 struct lpfc_sli *psli = &phba->sli;
dea3101e
JB
4768 uint32_t status, evtctr;
4769 uint32_t ha_copy;
4770 int i;
09372820 4771 unsigned long timeout;
dea3101e 4772 unsigned long drvr_flag = 0;
34b02dcd 4773 uint32_t word0, ldata;
dea3101e 4774 void __iomem *to_slim;
58da1ffb
JS
4775 int processing_queue = 0;
4776
4777 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4778 if (!pmbox) {
8568a4d2 4779 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4780 /* processing mbox queue from intr_handler */
3772a991
JS
4781 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4782 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4783 return MBX_SUCCESS;
4784 }
58da1ffb 4785 processing_queue = 1;
58da1ffb
JS
4786 pmbox = lpfc_mbox_get(phba);
4787 if (!pmbox) {
4788 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4789 return MBX_SUCCESS;
4790 }
4791 }
dea3101e 4792
ed957684 4793 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4794 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4795 if(!pmbox->vport) {
58da1ffb 4796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4797 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4798 LOG_MBOX | LOG_VPORT,
e8b62011 4799 "1806 Mbox x%x failed. No vport\n",
3772a991 4800 pmbox->u.mb.mbxCommand);
ed957684 4801 dump_stack();
58da1ffb 4802 goto out_not_finished;
ed957684
JS
4803 }
4804 }
4805
8d63f375 4806 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4807 if (unlikely(pci_channel_offline(phba->pcidev))) {
4808 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4809 goto out_not_finished;
4810 }
8d63f375 4811
a257bf90
JS
4812 /* If HBA has a deferred error attention, fail the iocb. */
4813 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4814 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4815 goto out_not_finished;
4816 }
4817
dea3101e 4818 psli = &phba->sli;
92d7f7b0 4819
3772a991 4820 mb = &pmbox->u.mb;
dea3101e
JB
4821 status = MBX_SUCCESS;
4822
2e0fef85
JS
4823 if (phba->link_state == LPFC_HBA_ERROR) {
4824 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4825
4826 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4828 "(%d):0311 Mailbox command x%x cannot "
4829 "issue Data: x%x x%x\n",
4830 pmbox->vport ? pmbox->vport->vpi : 0,
4831 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4832 goto out_not_finished;
41415862
JW
4833 }
4834
9290831f
JS
4835 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4836 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4838 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4839 "(%d):2528 Mailbox command x%x cannot "
4840 "issue Data: x%x x%x\n",
4841 pmbox->vport ? pmbox->vport->vpi : 0,
4842 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4843 goto out_not_finished;
9290831f
JS
4844 }
4845
dea3101e
JB
4846 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4847 /* Polling for a mbox command when another one is already active
4848 * is not allowed in SLI. Also, the driver must have established
4849 * SLI2 mode to queue and process multiple mbox commands.
4850 */
4851
4852 if (flag & MBX_POLL) {
2e0fef85 4853 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
4854
4855 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4856 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4857 "(%d):2529 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
JB
4863 }
4864
3772a991 4865 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4866 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4867 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4868 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4869 "(%d):2530 Mailbox command x%x "
4870 "cannot issue Data: x%x x%x\n",
4871 pmbox->vport ? pmbox->vport->vpi : 0,
4872 pmbox->u.mb.mbxCommand,
4873 psli->sli_flag, flag);
58da1ffb 4874 goto out_not_finished;
dea3101e
JB
4875 }
4876
dea3101e
JB
4877 /* Another mailbox command is still being processed, queue this
4878 * command to be processed later.
4879 */
4880 lpfc_mbox_put(phba, pmbox);
4881
4882 /* Mbox cmd issue - BUSY */
ed957684 4883 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4884 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4885 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4886 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4887 mb->mbxCommand, phba->pport->port_state,
4888 psli->sli_flag, flag);
dea3101e
JB
4889
4890 psli->slistat.mbox_busy++;
2e0fef85 4891 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4892
858c9f6c
JS
4893 if (pmbox->vport) {
4894 lpfc_debugfs_disc_trc(pmbox->vport,
4895 LPFC_DISC_TRC_MBOX_VPORT,
4896 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4897 (uint32_t)mb->mbxCommand,
4898 mb->un.varWords[0], mb->un.varWords[1]);
4899 }
4900 else {
4901 lpfc_debugfs_disc_trc(phba->pport,
4902 LPFC_DISC_TRC_MBOX,
4903 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4904 (uint32_t)mb->mbxCommand,
4905 mb->un.varWords[0], mb->un.varWords[1]);
4906 }
4907
2e0fef85 4908 return MBX_BUSY;
dea3101e
JB
4909 }
4910
dea3101e
JB
4911 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4912
4913 /* If we are not polling, we MUST be in SLI2 mode */
4914 if (flag != MBX_POLL) {
3772a991 4915 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4916 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4917 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4918 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4919 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4920 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4921 "(%d):2531 Mailbox command x%x "
4922 "cannot issue Data: x%x x%x\n",
4923 pmbox->vport ? pmbox->vport->vpi : 0,
4924 pmbox->u.mb.mbxCommand,
4925 psli->sli_flag, flag);
58da1ffb 4926 goto out_not_finished;
dea3101e
JB
4927 }
4928 /* timeout active mbox command */
a309a6b6
JS
4929 mod_timer(&psli->mbox_tmo, (jiffies +
4930 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e
JB
4931 }
4932
4933 /* Mailbox cmd <cmd> issue */
ed957684 4934 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4935 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4936 "x%x\n",
e8b62011 4937 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4938 mb->mbxCommand, phba->pport->port_state,
4939 psli->sli_flag, flag);
dea3101e 4940
858c9f6c
JS
4941 if (mb->mbxCommand != MBX_HEARTBEAT) {
4942 if (pmbox->vport) {
4943 lpfc_debugfs_disc_trc(pmbox->vport,
4944 LPFC_DISC_TRC_MBOX_VPORT,
4945 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4946 (uint32_t)mb->mbxCommand,
4947 mb->un.varWords[0], mb->un.varWords[1]);
4948 }
4949 else {
4950 lpfc_debugfs_disc_trc(phba->pport,
4951 LPFC_DISC_TRC_MBOX,
4952 "MBOX Send: cmd:x%x mb:x%x x%x",
4953 (uint32_t)mb->mbxCommand,
4954 mb->un.varWords[0], mb->un.varWords[1]);
4955 }
4956 }
4957
dea3101e
JB
4958 psli->slistat.mbox_cmd++;
4959 evtctr = psli->slistat.mbox_event;
4960
4961 /* next set own bit for the adapter and copy over command word */
4962 mb->mbxOwner = OWN_CHIP;
4963
3772a991 4964 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
4965 /* Populate mbox extension offset word. */
4966 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4967 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4968 = (uint8_t *)phba->mbox_ext
4969 - (uint8_t *)phba->mbox;
4970 }
4971
4972 /* Copy the mailbox extension data */
4973 if (pmbox->in_ext_byte_len && pmbox->context2) {
4974 lpfc_sli_pcimem_bcopy(pmbox->context2,
4975 (uint8_t *)phba->mbox_ext,
4976 pmbox->in_ext_byte_len);
4977 }
4978 /* Copy command data to host SLIM area */
34b02dcd 4979 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4980 } else {
7a470277
JS
4981 /* Populate mbox extension offset word. */
4982 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4983 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4984 = MAILBOX_HBA_EXT_OFFSET;
4985
4986 /* Copy the mailbox extension data */
4987 if (pmbox->in_ext_byte_len && pmbox->context2) {
4988 lpfc_memcpy_to_slim(phba->MBslimaddr +
4989 MAILBOX_HBA_EXT_OFFSET,
4990 pmbox->context2, pmbox->in_ext_byte_len);
4991
4992 }
9290831f 4993 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4994 /* copy command data into host mbox for cmpl */
34b02dcd 4995 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e
JB
4996 }
4997
4998 /* First copy mbox command data to HBA SLIM, skip past first
4999 word */
5000 to_slim = phba->MBslimaddr + sizeof (uint32_t);
5001 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
5002 MAILBOX_CMD_SIZE - sizeof (uint32_t));
5003
5004 /* Next copy over first word, with mbxOwner set */
34b02dcd 5005 ldata = *((uint32_t *)mb);
dea3101e
JB
5006 to_slim = phba->MBslimaddr;
5007 writel(ldata, to_slim);
5008 readl(to_slim); /* flush */
5009
5010 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5011 /* switch over to host mailbox */
3772a991 5012 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
5013 }
5014 }
5015
5016 wmb();
dea3101e
JB
5017
5018 switch (flag) {
5019 case MBX_NOWAIT:
09372820 5020 /* Set up reference to mailbox command */
dea3101e 5021 psli->mbox_active = pmbox;
09372820
JS
5022 /* Interrupt board to do it */
5023 writel(CA_MBATT, phba->CAregaddr);
5024 readl(phba->CAregaddr); /* flush */
5025 /* Don't wait for it to finish, just return */
dea3101e
JB
5026 break;
5027
5028 case MBX_POLL:
09372820 5029 /* Set up null reference to mailbox command */
dea3101e 5030 psli->mbox_active = NULL;
09372820
JS
5031 /* Interrupt board to do it */
5032 writel(CA_MBATT, phba->CAregaddr);
5033 readl(phba->CAregaddr); /* flush */
5034
3772a991 5035 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5036 /* First read mbox status word */
34b02dcd 5037 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
5038 word0 = le32_to_cpu(word0);
5039 } else {
5040 /* First read mbox status word */
5041 word0 = readl(phba->MBslimaddr);
5042 }
5043
5044 /* Read the HBA Host Attention Register */
5045 ha_copy = readl(phba->HAregaddr);
09372820
JS
5046 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
5047 mb->mbxCommand) *
5048 1000) + jiffies;
5049 i = 0;
dea3101e 5050 /* Wait for command to complete */
41415862
JW
5051 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5052 (!(ha_copy & HA_MBATT) &&
2e0fef85 5053 (phba->link_state > LPFC_WARM_START))) {
09372820 5054 if (time_after(jiffies, timeout)) {
dea3101e 5055 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 5056 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 5057 drvr_flag);
58da1ffb 5058 goto out_not_finished;
dea3101e
JB
5059 }
5060
5061 /* Check if we took a mbox interrupt while we were
5062 polling */
5063 if (((word0 & OWN_CHIP) != OWN_CHIP)
5064 && (evtctr != psli->slistat.mbox_event))
5065 break;
5066
09372820
JS
5067 if (i++ > 10) {
5068 spin_unlock_irqrestore(&phba->hbalock,
5069 drvr_flag);
5070 msleep(1);
5071 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5072 }
dea3101e 5073
3772a991 5074 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5075 /* First copy command data */
34b02dcd 5076 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
5077 word0 = le32_to_cpu(word0);
5078 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5079 MAILBOX_t *slimmb;
34b02dcd 5080 uint32_t slimword0;
dea3101e
JB
5081 /* Check real SLIM for any errors */
5082 slimword0 = readl(phba->MBslimaddr);
5083 slimmb = (MAILBOX_t *) & slimword0;
5084 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5085 && slimmb->mbxStatus) {
5086 psli->sli_flag &=
3772a991 5087 ~LPFC_SLI_ACTIVE;
dea3101e
JB
5088 word0 = slimword0;
5089 }
5090 }
5091 } else {
5092 /* First copy command data */
5093 word0 = readl(phba->MBslimaddr);
5094 }
5095 /* Read the HBA Host Attention Register */
5096 ha_copy = readl(phba->HAregaddr);
5097 }
5098
3772a991 5099 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5100 /* copy results back to user */
34b02dcd 5101 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
7a470277
JS
5102 /* Copy the mailbox extension data */
5103 if (pmbox->out_ext_byte_len && pmbox->context2) {
5104 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5105 pmbox->context2,
5106 pmbox->out_ext_byte_len);
5107 }
dea3101e
JB
5108 } else {
5109 /* First copy command data */
5110 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5111 MAILBOX_CMD_SIZE);
7a470277
JS
5112 /* Copy the mailbox extension data */
5113 if (pmbox->out_ext_byte_len && pmbox->context2) {
5114 lpfc_memcpy_from_slim(pmbox->context2,
5115 phba->MBslimaddr +
5116 MAILBOX_HBA_EXT_OFFSET,
5117 pmbox->out_ext_byte_len);
dea3101e
JB
5118 }
5119 }
5120
5121 writel(HA_MBATT, phba->HAregaddr);
5122 readl(phba->HAregaddr); /* flush */
5123
5124 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5125 status = mb->mbxStatus;
5126 }
5127
2e0fef85
JS
5128 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5129 return status;
58da1ffb
JS
5130
5131out_not_finished:
5132 if (processing_queue) {
da0436e9 5133 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5134 lpfc_mbox_cmpl_put(phba, pmbox);
5135 }
5136 return MBX_NOT_FINISHED;
dea3101e
JB
5137}
5138
f1126688
JS
5139/**
5140 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5141 * @phba: Pointer to HBA context object.
5142 *
5143 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5144 * the driver internal pending mailbox queue. It will then try to wait out the
5145 * possible outstanding mailbox command before return.
5146 *
5147 * Returns:
5148 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5149 * the outstanding mailbox command timed out.
5150 **/
5151static int
5152lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5153{
5154 struct lpfc_sli *psli = &phba->sli;
5155 uint8_t actcmd = MBX_HEARTBEAT;
5156 int rc = 0;
5157 unsigned long timeout;
5158
5159 /* Mark the asynchronous mailbox command posting as blocked */
5160 spin_lock_irq(&phba->hbalock);
5161 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5162 if (phba->sli.mbox_active)
5163 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5164 spin_unlock_irq(&phba->hbalock);
5165 /* Determine how long we might wait for the active mailbox
5166 * command to be gracefully completed by firmware.
5167 */
5168 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5169 jiffies;
5170 /* Wait for the outstnading mailbox command to complete */
5171 while (phba->sli.mbox_active) {
5172 /* Check active mailbox complete status every 2ms */
5173 msleep(2);
5174 if (time_after(jiffies, timeout)) {
5175 /* Timeout, marked the outstanding cmd not complete */
5176 rc = 1;
5177 break;
5178 }
5179 }
5180
5181 /* Can not cleanly block async mailbox command, fails it */
5182 if (rc) {
5183 spin_lock_irq(&phba->hbalock);
5184 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5185 spin_unlock_irq(&phba->hbalock);
5186 }
5187 return rc;
5188}
5189
5190/**
5191 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5192 * @phba: Pointer to HBA context object.
5193 *
5194 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5195 * commands from the driver internal pending mailbox queue. It makes sure
5196 * that there is no outstanding mailbox command before resuming posting
5197 * asynchronous mailbox commands. If, for any reason, there is outstanding
5198 * mailbox command, it will try to wait it out before resuming asynchronous
5199 * mailbox command posting.
5200 **/
5201static void
5202lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5203{
5204 struct lpfc_sli *psli = &phba->sli;
5205
5206 spin_lock_irq(&phba->hbalock);
5207 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5208 /* Asynchronous mailbox posting is not blocked, do nothing */
5209 spin_unlock_irq(&phba->hbalock);
5210 return;
5211 }
5212
5213 /* Outstanding synchronous mailbox command is guaranteed to be done,
5214 * successful or timeout, after timing-out the outstanding mailbox
5215 * command shall always be removed, so just unblock posting async
5216 * mailbox command and resume
5217 */
5218 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5219 spin_unlock_irq(&phba->hbalock);
5220
5221 /* wake up worker thread to post asynchronlous mailbox command */
5222 lpfc_worker_wake_up(phba);
5223}
5224
da0436e9
JS
5225/**
5226 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5227 * @phba: Pointer to HBA context object.
5228 * @mboxq: Pointer to mailbox object.
5229 *
5230 * The function posts a mailbox to the port. The mailbox is expected
5231 * to be comletely filled in and ready for the port to operate on it.
5232 * This routine executes a synchronous completion operation on the
5233 * mailbox by polling for its completion.
5234 *
5235 * The caller must not be holding any locks when calling this routine.
5236 *
5237 * Returns:
5238 * MBX_SUCCESS - mailbox posted successfully
5239 * Any of the MBX error values.
5240 **/
5241static int
5242lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5243{
5244 int rc = MBX_SUCCESS;
5245 unsigned long iflag;
5246 uint32_t db_ready;
5247 uint32_t mcqe_status;
5248 uint32_t mbx_cmnd;
5249 unsigned long timeout;
5250 struct lpfc_sli *psli = &phba->sli;
5251 struct lpfc_mqe *mb = &mboxq->u.mqe;
5252 struct lpfc_bmbx_create *mbox_rgn;
5253 struct dma_address *dma_address;
5254 struct lpfc_register bmbx_reg;
5255
5256 /*
5257 * Only one mailbox can be active to the bootstrap mailbox region
5258 * at a time and there is no queueing provided.
5259 */
5260 spin_lock_irqsave(&phba->hbalock, iflag);
5261 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5262 spin_unlock_irqrestore(&phba->hbalock, iflag);
5263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5264 "(%d):2532 Mailbox command x%x (x%x) "
5265 "cannot issue Data: x%x x%x\n",
5266 mboxq->vport ? mboxq->vport->vpi : 0,
5267 mboxq->u.mb.mbxCommand,
5268 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5269 psli->sli_flag, MBX_POLL);
5270 return MBXERR_ERROR;
5271 }
5272 /* The server grabs the token and owns it until release */
5273 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5274 phba->sli.mbox_active = mboxq;
5275 spin_unlock_irqrestore(&phba->hbalock, iflag);
5276
5277 /*
5278 * Initialize the bootstrap memory region to avoid stale data areas
5279 * in the mailbox post. Then copy the caller's mailbox contents to
5280 * the bmbx mailbox region.
5281 */
5282 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5283 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5284 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5285 sizeof(struct lpfc_mqe));
5286
5287 /* Post the high mailbox dma address to the port and wait for ready. */
5288 dma_address = &phba->sli4_hba.bmbx.dma_address;
5289 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5290
5291 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5292 * 1000) + jiffies;
5293 do {
5294 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5295 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5296 if (!db_ready)
5297 msleep(2);
5298
5299 if (time_after(jiffies, timeout)) {
5300 rc = MBXERR_ERROR;
5301 goto exit;
5302 }
5303 } while (!db_ready);
5304
5305 /* Post the low mailbox dma address to the port. */
5306 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5307 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5308 * 1000) + jiffies;
5309 do {
5310 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5311 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5312 if (!db_ready)
5313 msleep(2);
5314
5315 if (time_after(jiffies, timeout)) {
5316 rc = MBXERR_ERROR;
5317 goto exit;
5318 }
5319 } while (!db_ready);
5320
5321 /*
5322 * Read the CQ to ensure the mailbox has completed.
5323 * If so, update the mailbox status so that the upper layers
5324 * can complete the request normally.
5325 */
5326 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5327 sizeof(struct lpfc_mqe));
5328 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5329 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5330 sizeof(struct lpfc_mcqe));
5331 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5332
5333 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5334 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5335 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5336 rc = MBXERR_ERROR;
d7c47992
JS
5337 } else
5338 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
5339
5340 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5341 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5342 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5343 " x%x x%x CQ: x%x x%x x%x x%x\n",
5344 mboxq->vport ? mboxq->vport->vpi : 0,
5345 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5346 bf_get(lpfc_mqe_status, mb),
5347 mb->un.mb_words[0], mb->un.mb_words[1],
5348 mb->un.mb_words[2], mb->un.mb_words[3],
5349 mb->un.mb_words[4], mb->un.mb_words[5],
5350 mb->un.mb_words[6], mb->un.mb_words[7],
5351 mb->un.mb_words[8], mb->un.mb_words[9],
5352 mb->un.mb_words[10], mb->un.mb_words[11],
5353 mb->un.mb_words[12], mboxq->mcqe.word0,
5354 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5355 mboxq->mcqe.trailer);
5356exit:
5357 /* We are holding the token, no needed for lock when release */
5358 spin_lock_irqsave(&phba->hbalock, iflag);
5359 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5360 phba->sli.mbox_active = NULL;
5361 spin_unlock_irqrestore(&phba->hbalock, iflag);
5362 return rc;
5363}
5364
5365/**
5366 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5367 * @phba: Pointer to HBA context object.
5368 * @pmbox: Pointer to mailbox object.
5369 * @flag: Flag indicating how the mailbox need to be processed.
5370 *
5371 * This function is called by discovery code and HBA management code to submit
5372 * a mailbox command to firmware with SLI-4 interface spec.
5373 *
5374 * Return codes the caller owns the mailbox command after the return of the
5375 * function.
5376 **/
5377static int
5378lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5379 uint32_t flag)
5380{
5381 struct lpfc_sli *psli = &phba->sli;
5382 unsigned long iflags;
5383 int rc;
5384
8fa38513
JS
5385 rc = lpfc_mbox_dev_check(phba);
5386 if (unlikely(rc)) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5388 "(%d):2544 Mailbox command x%x (x%x) "
5389 "cannot issue Data: x%x x%x\n",
5390 mboxq->vport ? mboxq->vport->vpi : 0,
5391 mboxq->u.mb.mbxCommand,
5392 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5393 psli->sli_flag, flag);
5394 goto out_not_finished;
5395 }
5396
da0436e9
JS
5397 /* Detect polling mode and jump to a handler */
5398 if (!phba->sli4_hba.intr_enable) {
5399 if (flag == MBX_POLL)
5400 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5401 else
5402 rc = -EIO;
5403 if (rc != MBX_SUCCESS)
5404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5405 "(%d):2541 Mailbox command x%x "
5406 "(x%x) cannot issue Data: x%x x%x\n",
5407 mboxq->vport ? mboxq->vport->vpi : 0,
5408 mboxq->u.mb.mbxCommand,
5409 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5410 psli->sli_flag, flag);
5411 return rc;
5412 } else if (flag == MBX_POLL) {
f1126688
JS
5413 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5414 "(%d):2542 Try to issue mailbox command "
5415 "x%x (x%x) synchronously ahead of async"
5416 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5417 mboxq->vport ? mboxq->vport->vpi : 0,
5418 mboxq->u.mb.mbxCommand,
5419 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5420 psli->sli_flag, flag);
f1126688
JS
5421 /* Try to block the asynchronous mailbox posting */
5422 rc = lpfc_sli4_async_mbox_block(phba);
5423 if (!rc) {
5424 /* Successfully blocked, now issue sync mbox cmd */
5425 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5426 if (rc != MBX_SUCCESS)
5427 lpfc_printf_log(phba, KERN_ERR,
5428 LOG_MBOX | LOG_SLI,
5429 "(%d):2597 Mailbox command "
5430 "x%x (x%x) cannot issue "
5431 "Data: x%x x%x\n",
5432 mboxq->vport ?
5433 mboxq->vport->vpi : 0,
5434 mboxq->u.mb.mbxCommand,
5435 lpfc_sli4_mbox_opcode_get(phba,
5436 mboxq),
5437 psli->sli_flag, flag);
5438 /* Unblock the async mailbox posting afterward */
5439 lpfc_sli4_async_mbox_unblock(phba);
5440 }
5441 return rc;
da0436e9
JS
5442 }
5443
5444 /* Now, interrupt mode asynchrous mailbox command */
5445 rc = lpfc_mbox_cmd_check(phba, mboxq);
5446 if (rc) {
5447 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5448 "(%d):2543 Mailbox command x%x (x%x) "
5449 "cannot issue Data: x%x x%x\n",
5450 mboxq->vport ? mboxq->vport->vpi : 0,
5451 mboxq->u.mb.mbxCommand,
5452 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5453 psli->sli_flag, flag);
5454 goto out_not_finished;
5455 }
da0436e9
JS
5456
5457 /* Put the mailbox command to the driver internal FIFO */
5458 psli->slistat.mbox_busy++;
5459 spin_lock_irqsave(&phba->hbalock, iflags);
5460 lpfc_mbox_put(phba, mboxq);
5461 spin_unlock_irqrestore(&phba->hbalock, iflags);
5462 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5463 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5464 "x%x (x%x) x%x x%x x%x\n",
5465 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5466 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5467 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5468 phba->pport->port_state,
5469 psli->sli_flag, MBX_NOWAIT);
5470 /* Wake up worker thread to transport mailbox command from head */
5471 lpfc_worker_wake_up(phba);
5472
5473 return MBX_BUSY;
5474
5475out_not_finished:
5476 return MBX_NOT_FINISHED;
5477}
5478
5479/**
5480 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5481 * @phba: Pointer to HBA context object.
5482 *
5483 * This function is called by worker thread to send a mailbox command to
5484 * SLI4 HBA firmware.
5485 *
5486 **/
5487int
5488lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5489{
5490 struct lpfc_sli *psli = &phba->sli;
5491 LPFC_MBOXQ_t *mboxq;
5492 int rc = MBX_SUCCESS;
5493 unsigned long iflags;
5494 struct lpfc_mqe *mqe;
5495 uint32_t mbx_cmnd;
5496
5497 /* Check interrupt mode before post async mailbox command */
5498 if (unlikely(!phba->sli4_hba.intr_enable))
5499 return MBX_NOT_FINISHED;
5500
5501 /* Check for mailbox command service token */
5502 spin_lock_irqsave(&phba->hbalock, iflags);
5503 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5504 spin_unlock_irqrestore(&phba->hbalock, iflags);
5505 return MBX_NOT_FINISHED;
5506 }
5507 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5508 spin_unlock_irqrestore(&phba->hbalock, iflags);
5509 return MBX_NOT_FINISHED;
5510 }
5511 if (unlikely(phba->sli.mbox_active)) {
5512 spin_unlock_irqrestore(&phba->hbalock, iflags);
5513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5514 "0384 There is pending active mailbox cmd\n");
5515 return MBX_NOT_FINISHED;
5516 }
5517 /* Take the mailbox command service token */
5518 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5519
5520 /* Get the next mailbox command from head of queue */
5521 mboxq = lpfc_mbox_get(phba);
5522
5523 /* If no more mailbox command waiting for post, we're done */
5524 if (!mboxq) {
5525 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5526 spin_unlock_irqrestore(&phba->hbalock, iflags);
5527 return MBX_SUCCESS;
5528 }
5529 phba->sli.mbox_active = mboxq;
5530 spin_unlock_irqrestore(&phba->hbalock, iflags);
5531
5532 /* Check device readiness for posting mailbox command */
5533 rc = lpfc_mbox_dev_check(phba);
5534 if (unlikely(rc))
5535 /* Driver clean routine will clean up pending mailbox */
5536 goto out_not_finished;
5537
5538 /* Prepare the mbox command to be posted */
5539 mqe = &mboxq->u.mqe;
5540 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5541
5542 /* Start timer for the mbox_tmo and log some mailbox post messages */
5543 mod_timer(&psli->mbox_tmo, (jiffies +
5544 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5545
5546 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5547 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5548 "x%x x%x\n",
5549 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5550 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5551 phba->pport->port_state, psli->sli_flag);
5552
5553 if (mbx_cmnd != MBX_HEARTBEAT) {
5554 if (mboxq->vport) {
5555 lpfc_debugfs_disc_trc(mboxq->vport,
5556 LPFC_DISC_TRC_MBOX_VPORT,
5557 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5558 mbx_cmnd, mqe->un.mb_words[0],
5559 mqe->un.mb_words[1]);
5560 } else {
5561 lpfc_debugfs_disc_trc(phba->pport,
5562 LPFC_DISC_TRC_MBOX,
5563 "MBOX Send: cmd:x%x mb:x%x x%x",
5564 mbx_cmnd, mqe->un.mb_words[0],
5565 mqe->un.mb_words[1]);
5566 }
5567 }
5568 psli->slistat.mbox_cmd++;
5569
5570 /* Post the mailbox command to the port */
5571 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5572 if (rc != MBX_SUCCESS) {
5573 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5574 "(%d):2533 Mailbox command x%x (x%x) "
5575 "cannot issue Data: x%x x%x\n",
5576 mboxq->vport ? mboxq->vport->vpi : 0,
5577 mboxq->u.mb.mbxCommand,
5578 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5579 psli->sli_flag, MBX_NOWAIT);
5580 goto out_not_finished;
5581 }
5582
5583 return rc;
5584
5585out_not_finished:
5586 spin_lock_irqsave(&phba->hbalock, iflags);
5587 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5588 __lpfc_mbox_cmpl_put(phba, mboxq);
5589 /* Release the token */
5590 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5591 phba->sli.mbox_active = NULL;
5592 spin_unlock_irqrestore(&phba->hbalock, iflags);
5593
5594 return MBX_NOT_FINISHED;
5595}
5596
5597/**
5598 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5599 * @phba: Pointer to HBA context object.
5600 * @pmbox: Pointer to mailbox object.
5601 * @flag: Flag indicating how the mailbox need to be processed.
5602 *
5603 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5604 * the API jump table function pointer from the lpfc_hba struct.
5605 *
5606 * Return codes the caller owns the mailbox command after the return of the
5607 * function.
5608 **/
5609int
5610lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5611{
5612 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5613}
5614
5615/**
5616 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5617 * @phba: The hba struct for which this call is being executed.
5618 * @dev_grp: The HBA PCI-Device group number.
5619 *
5620 * This routine sets up the mbox interface API function jump table in @phba
5621 * struct.
5622 * Returns: 0 - success, -ENODEV - failure.
5623 **/
5624int
5625lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5626{
5627
5628 switch (dev_grp) {
5629 case LPFC_PCI_DEV_LP:
5630 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5631 phba->lpfc_sli_handle_slow_ring_event =
5632 lpfc_sli_handle_slow_ring_event_s3;
5633 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5634 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5635 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5636 break;
5637 case LPFC_PCI_DEV_OC:
5638 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5639 phba->lpfc_sli_handle_slow_ring_event =
5640 lpfc_sli_handle_slow_ring_event_s4;
5641 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5642 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5643 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5644 break;
5645 default:
5646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5647 "1420 Invalid HBA PCI-device group: 0x%x\n",
5648 dev_grp);
5649 return -ENODEV;
5650 break;
5651 }
5652 return 0;
5653}
5654
e59058c4 5655/**
3621a710 5656 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5657 * @phba: Pointer to HBA context object.
5658 * @pring: Pointer to driver SLI ring object.
5659 * @piocb: Pointer to address of newly added command iocb.
5660 *
5661 * This function is called with hbalock held to add a command
5662 * iocb to the txq when SLI layer cannot submit the command iocb
5663 * to the ring.
5664 **/
2a9bf3d0 5665void
92d7f7b0 5666__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5667 struct lpfc_iocbq *piocb)
dea3101e
JB
5668{
5669 /* Insert the caller's iocb in the txq tail for later processing. */
5670 list_add_tail(&piocb->list, &pring->txq);
5671 pring->txq_cnt++;
dea3101e
JB
5672}
5673
e59058c4 5674/**
3621a710 5675 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5676 * @phba: Pointer to HBA context object.
5677 * @pring: Pointer to driver SLI ring object.
5678 * @piocb: Pointer to address of newly added command iocb.
5679 *
5680 * This function is called with hbalock held before a new
5681 * iocb is submitted to the firmware. This function checks
5682 * txq to flush the iocbs in txq to Firmware before
5683 * submitting new iocbs to the Firmware.
5684 * If there are iocbs in the txq which need to be submitted
5685 * to firmware, lpfc_sli_next_iocb returns the first element
5686 * of the txq after dequeuing it from txq.
5687 * If there is no iocb in the txq then the function will return
5688 * *piocb and *piocb is set to NULL. Caller needs to check
5689 * *piocb to find if there are more commands in the txq.
5690 **/
dea3101e
JB
5691static struct lpfc_iocbq *
5692lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5693 struct lpfc_iocbq **piocb)
dea3101e
JB
5694{
5695 struct lpfc_iocbq * nextiocb;
5696
5697 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5698 if (!nextiocb) {
5699 nextiocb = *piocb;
5700 *piocb = NULL;
5701 }
5702
5703 return nextiocb;
5704}
5705
e59058c4 5706/**
3772a991 5707 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5708 * @phba: Pointer to HBA context object.
3772a991 5709 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5710 * @piocb: Pointer to command iocb.
5711 * @flag: Flag indicating if this command can be put into txq.
5712 *
3772a991
JS
5713 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5714 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5715 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5716 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5717 * this function allows only iocbs for posting buffers. This function finds
5718 * next available slot in the command ring and posts the command to the
5719 * available slot and writes the port attention register to request HBA start
5720 * processing new iocb. If there is no slot available in the ring and
5721 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5722 * the function returns IOCB_BUSY.
e59058c4 5723 *
3772a991
JS
5724 * This function is called with hbalock held. The function will return success
5725 * after it successfully submit the iocb to firmware or after adding to the
5726 * txq.
e59058c4 5727 **/
98c9ea5c 5728static int
3772a991 5729__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
5730 struct lpfc_iocbq *piocb, uint32_t flag)
5731{
5732 struct lpfc_iocbq *nextiocb;
5733 IOCB_t *iocb;
3772a991 5734 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5735
92d7f7b0
JS
5736 if (piocb->iocb_cmpl && (!piocb->vport) &&
5737 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5738 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5739 lpfc_printf_log(phba, KERN_ERR,
5740 LOG_SLI | LOG_VPORT,
e8b62011 5741 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5742 piocb->iocb.ulpCommand);
5743 dump_stack();
5744 return IOCB_ERROR;
5745 }
5746
5747
8d63f375
LV
5748 /* If the PCI channel is in offline state, do not post iocbs. */
5749 if (unlikely(pci_channel_offline(phba->pcidev)))
5750 return IOCB_ERROR;
5751
a257bf90
JS
5752 /* If HBA has a deferred error attention, fail the iocb. */
5753 if (unlikely(phba->hba_flag & DEFER_ERATT))
5754 return IOCB_ERROR;
5755
dea3101e
JB
5756 /*
5757 * We should never get an IOCB if we are in a < LINK_DOWN state
5758 */
2e0fef85 5759 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
5760 return IOCB_ERROR;
5761
5762 /*
5763 * Check to see if we are blocking IOCB processing because of a
0b727fea 5764 * outstanding event.
dea3101e 5765 */
0b727fea 5766 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
5767 goto iocb_busy;
5768
2e0fef85 5769 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5770 /*
2680eeaa 5771 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
5772 * can be issued if the link is not up.
5773 */
5774 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5775 case CMD_GEN_REQUEST64_CR:
5776 case CMD_GEN_REQUEST64_CX:
5777 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5778 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5779 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5780 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5781 MENLO_TRANSPORT_TYPE))
5782
5783 goto iocb_busy;
5784 break;
dea3101e
JB
5785 case CMD_QUE_RING_BUF_CN:
5786 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
5787 /*
5788 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5789 * completion, iocb_cmpl MUST be 0.
5790 */
5791 if (piocb->iocb_cmpl)
5792 piocb->iocb_cmpl = NULL;
5793 /*FALLTHROUGH*/
5794 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5795 case CMD_CLOSE_XRI_CN:
5796 case CMD_CLOSE_XRI_CX:
dea3101e
JB
5797 break;
5798 default:
5799 goto iocb_busy;
5800 }
5801
5802 /*
5803 * For FCP commands, we must be in a state where we can process link
5804 * attention events.
5805 */
5806 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5807 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5808 goto iocb_busy;
92d7f7b0 5809 }
dea3101e 5810
dea3101e
JB
5811 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5812 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5813 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5814
5815 if (iocb)
5816 lpfc_sli_update_ring(phba, pring);
5817 else
5818 lpfc_sli_update_full_ring(phba, pring);
5819
5820 if (!piocb)
5821 return IOCB_SUCCESS;
5822
5823 goto out_busy;
5824
5825 iocb_busy:
5826 pring->stats.iocb_cmd_delay++;
5827
5828 out_busy:
5829
5830 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5831 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
5832 return IOCB_SUCCESS;
5833 }
5834
5835 return IOCB_BUSY;
5836}
5837
3772a991 5838/**
4f774513
JS
5839 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5840 * @phba: Pointer to HBA context object.
5841 * @piocb: Pointer to command iocb.
5842 * @sglq: Pointer to the scatter gather queue object.
5843 *
5844 * This routine converts the bpl or bde that is in the IOCB
5845 * to a sgl list for the sli4 hardware. The physical address
5846 * of the bpl/bde is converted back to a virtual address.
5847 * If the IOCB contains a BPL then the list of BDE's is
5848 * converted to sli4_sge's. If the IOCB contains a single
5849 * BDE then it is converted to a single sli_sge.
5850 * The IOCB is still in cpu endianess so the contents of
5851 * the bpl can be used without byte swapping.
5852 *
5853 * Returns valid XRI = Success, NO_XRI = Failure.
5854**/
5855static uint16_t
5856lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5857 struct lpfc_sglq *sglq)
3772a991 5858{
4f774513
JS
5859 uint16_t xritag = NO_XRI;
5860 struct ulp_bde64 *bpl = NULL;
5861 struct ulp_bde64 bde;
5862 struct sli4_sge *sgl = NULL;
5863 IOCB_t *icmd;
5864 int numBdes = 0;
5865 int i = 0;
63e801ce
JS
5866 uint32_t offset = 0; /* accumulated offset in the sg request list */
5867 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 5868
4f774513
JS
5869 if (!piocbq || !sglq)
5870 return xritag;
5871
5872 sgl = (struct sli4_sge *)sglq->sgl;
5873 icmd = &piocbq->iocb;
5874 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5875 numBdes = icmd->un.genreq64.bdl.bdeSize /
5876 sizeof(struct ulp_bde64);
5877 /* The addrHigh and addrLow fields within the IOCB
5878 * have not been byteswapped yet so there is no
5879 * need to swap them back.
5880 */
5881 bpl = (struct ulp_bde64 *)
5882 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5883
5884 if (!bpl)
5885 return xritag;
5886
5887 for (i = 0; i < numBdes; i++) {
5888 /* Should already be byte swapped. */
28baac74
JS
5889 sgl->addr_hi = bpl->addrHigh;
5890 sgl->addr_lo = bpl->addrLow;
5891
4f774513
JS
5892 if ((i+1) == numBdes)
5893 bf_set(lpfc_sli4_sge_last, sgl, 1);
5894 else
5895 bf_set(lpfc_sli4_sge_last, sgl, 0);
5896 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5897 /* swap the size field back to the cpu so we
5898 * can assign it to the sgl.
5899 */
5900 bde.tus.w = le32_to_cpu(bpl->tus.w);
5901 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
5902 /* The offsets in the sgl need to be accumulated
5903 * separately for the request and reply lists.
5904 * The request is always first, the reply follows.
5905 */
5906 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
5907 /* add up the reply sg entries */
5908 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
5909 inbound++;
5910 /* first inbound? reset the offset */
5911 if (inbound == 1)
5912 offset = 0;
5913 bf_set(lpfc_sli4_sge_offset, sgl, offset);
5914 offset += bde.tus.f.bdeSize;
5915 }
4f774513
JS
5916 bpl++;
5917 sgl++;
5918 }
5919 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5920 /* The addrHigh and addrLow fields of the BDE have not
5921 * been byteswapped yet so they need to be swapped
5922 * before putting them in the sgl.
5923 */
5924 sgl->addr_hi =
5925 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5926 sgl->addr_lo =
5927 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
4f774513
JS
5928 bf_set(lpfc_sli4_sge_last, sgl, 1);
5929 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5930 sgl->sge_len =
5931 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
5932 }
5933 return sglq->sli4_xritag;
3772a991 5934}
92d7f7b0 5935
e59058c4 5936/**
4f774513 5937 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5938 * @phba: Pointer to HBA context object.
e59058c4 5939 *
a93ff37a 5940 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5941 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5942 * held.
4f774513
JS
5943 *
5944 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5945 **/
4f774513 5946static uint32_t
8fa38513 5947lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5948{
8fa38513
JS
5949 ++phba->fcp_qidx;
5950 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5951 phba->fcp_qidx = 0;
92d7f7b0 5952
8fa38513 5953 return phba->fcp_qidx;
92d7f7b0
JS
5954}
5955
e59058c4 5956/**
4f774513 5957 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5958 * @phba: Pointer to HBA context object.
4f774513
JS
5959 * @piocb: Pointer to command iocb.
5960 * @wqe: Pointer to the work queue entry.
e59058c4 5961 *
4f774513
JS
5962 * This routine converts the iocb command to its Work Queue Entry
5963 * equivalent. The wqe pointer should not have any fields set when
5964 * this routine is called because it will memcpy over them.
5965 * This routine does not set the CQ_ID or the WQEC bits in the
5966 * wqe.
e59058c4 5967 *
4f774513 5968 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5969 **/
cf5bf97e 5970static int
4f774513
JS
5971lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5972 union lpfc_wqe *wqe)
cf5bf97e 5973{
5ffc266e 5974 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
5975 uint8_t ct = 0;
5976 uint32_t fip;
5977 uint32_t abort_tag;
5978 uint8_t command_type = ELS_COMMAND_NON_FIP;
5979 uint8_t cmnd;
5980 uint16_t xritag;
dcf2a4e0
JS
5981 uint16_t abrt_iotag;
5982 struct lpfc_iocbq *abrtiocbq;
4f774513 5983 struct ulp_bde64 *bpl = NULL;
f0d9bccc 5984 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
5985 int numBdes, i;
5986 struct ulp_bde64 bde;
4f774513 5987
45ed1190 5988 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 5989 /* The fcp commands will set command type */
0c287589 5990 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5991 command_type = FCP_COMMAND;
c868595d 5992 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
5993 command_type = ELS_COMMAND_FIP;
5994 else
5995 command_type = ELS_COMMAND_NON_FIP;
5996
4f774513
JS
5997 /* Some of the fields are in the right position already */
5998 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5999 abort_tag = (uint32_t) iocbq->iotag;
6000 xritag = iocbq->sli4_xritag;
f0d9bccc 6001 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
4f774513
JS
6002 /* words0-2 bpl convert bde */
6003 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
6004 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6005 sizeof(struct ulp_bde64);
4f774513
JS
6006 bpl = (struct ulp_bde64 *)
6007 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
6008 if (!bpl)
6009 return IOCB_ERROR;
cf5bf97e 6010
4f774513
JS
6011 /* Should already be byte swapped. */
6012 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
6013 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
6014 /* swap the size field back to the cpu so we
6015 * can assign it to the sgl.
6016 */
6017 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
6018 xmit_len = wqe->generic.bde.tus.f.bdeSize;
6019 total_len = 0;
6020 for (i = 0; i < numBdes; i++) {
6021 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6022 total_len += bde.tus.f.bdeSize;
6023 }
4f774513 6024 } else
5ffc266e 6025 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 6026
4f774513
JS
6027 iocbq->iocb.ulpIoTag = iocbq->iotag;
6028 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 6029
4f774513
JS
6030 switch (iocbq->iocb.ulpCommand) {
6031 case CMD_ELS_REQUEST64_CR:
6032 if (!iocbq->iocb.ulpLe) {
6033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6034 "2007 Only Limited Edition cmd Format"
6035 " supported 0x%x\n",
6036 iocbq->iocb.ulpCommand);
6037 return IOCB_ERROR;
6038 }
5ffc266e 6039 wqe->els_req.payload_len = xmit_len;
4f774513
JS
6040 /* Els_reguest64 has a TMO */
6041 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
6042 iocbq->iocb.ulpTimeout);
6043 /* Need a VF for word 4 set the vf bit*/
6044 bf_set(els_req64_vf, &wqe->els_req, 0);
6045 /* And a VFID for word 12 */
6046 bf_set(els_req64_vfid, &wqe->els_req, 0);
6047 /*
6048 * Set ct field to 3, indicates that the context_tag field
6049 * contains the FCFI and remote N_Port_ID is
6050 * in word 5.
6051 */
4f774513 6052 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
6053 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
6054 iocbq->iocb.ulpContext);
6055 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
6056 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 6057 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
6058 if (command_type == ELS_COMMAND_FIP) {
6059 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
6060 >> LPFC_FIP_ELS_ID_SHIFT);
6061 }
f0d9bccc
JS
6062 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
6063 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
6064 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
6065 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
6066 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6067 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
4f774513 6068 break;
5ffc266e 6069 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
6070 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
6071 iocbq->iocb.un.ulpWord[3]);
6072 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
6073 iocbq->iocb.ulpContext);
5ffc266e
JS
6074 /* The entire sequence is transmitted for this IOCB */
6075 xmit_len = total_len;
6076 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513 6077 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
6078 /* word3 iocb=io_tag32 wqe=reserved */
6079 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
6080 /* word4 relative_offset memcpy */
6081 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
6082 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
6083 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
6084 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
6085 LPFC_WQE_IOD_WRITE);
6086 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
6087 LPFC_WQE_LENLOC_WORD12);
6088 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
6089 wqe->xmit_sequence.xmit_len = xmit_len;
6090 command_type = OTHER_COMMAND;
4f774513
JS
6091 break;
6092 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
6093 /* word3 iocb=iotag32 wqe=seq_payload_len */
6094 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
6095 /* word4 iocb=rsvd wqe=rsvd */
6096 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6097 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 6098 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 6099 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
6100 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
6101 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
6102 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
6103 LPFC_WQE_LENLOC_WORD3);
6104 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
4f774513
JS
6105 break;
6106 case CMD_FCP_IWRITE64_CR:
6107 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
6108 /* word3 iocb=iotag wqe=payload_offset_len */
6109 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6110 wqe->fcp_iwrite.payload_offset_len =
6111 xmit_len + sizeof(struct fcp_rsp);
6112 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6113 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6114 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
6115 iocbq->iocb.ulpFCP2Rcvy);
6116 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
6117 /* Always open the exchange */
6118 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
6119 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
6120 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
6121 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
6122 LPFC_WQE_LENLOC_WORD4);
6123 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
6124 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
6125 break;
4f774513 6126 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
6127 /* word3 iocb=iotag wqe=payload_offset_len */
6128 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
6129 wqe->fcp_iread.payload_offset_len =
5ffc266e 6130 xmit_len + sizeof(struct fcp_rsp);
f0d9bccc
JS
6131 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
6132 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
6133 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
6134 iocbq->iocb.ulpFCP2Rcvy);
6135 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688
JS
6136 /* Always open the exchange */
6137 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
f0d9bccc
JS
6138 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
6139 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
6140 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
6141 LPFC_WQE_LENLOC_WORD4);
6142 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
6143 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
6144 break;
4f774513 6145 case CMD_FCP_ICMND64_CR:
f0d9bccc
JS
6146 /* word3 iocb=IO_TAG wqe=reserved */
6147 wqe->fcp_icmd.rsrvd3 = 0;
6148 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 6149 /* Always open the exchange */
f0d9bccc
JS
6150 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
6151 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
6152 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
6153 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
6154 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
6155 LPFC_WQE_LENLOC_NONE);
6156 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
4f774513
JS
6157 break;
6158 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
6159 /* For this command calculate the xmit length of the
6160 * request bde.
6161 */
6162 xmit_len = 0;
6163 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
6164 sizeof(struct ulp_bde64);
6165 for (i = 0; i < numBdes; i++) {
6166 if (bpl[i].tus.f.bdeFlags != BUFF_TYPE_BDE_64)
6167 break;
6168 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
6169 xmit_len += bde.tus.f.bdeSize;
6170 }
f0d9bccc
JS
6171 /* word3 iocb=IO_TAG wqe=request_payload_len */
6172 wqe->gen_req.request_payload_len = xmit_len;
6173 /* word4 iocb=parameter wqe=relative_offset memcpy */
6174 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
6175 /* word6 context tag copied in memcpy */
6176 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6177 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6179 "2015 Invalid CT %x command 0x%x\n",
6180 ct, iocbq->iocb.ulpCommand);
6181 return IOCB_ERROR;
6182 }
f0d9bccc
JS
6183 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
6184 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
6185 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
6186 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
6187 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
6188 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
6189 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
6190 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
4f774513
JS
6191 command_type = OTHER_COMMAND;
6192 break;
6193 case CMD_XMIT_ELS_RSP64_CX:
6194 /* words0-2 BDE memcpy */
f0d9bccc
JS
6195 /* word3 iocb=iotag32 wqe=response_payload_len */
6196 wqe->xmit_els_rsp.response_payload_len = xmit_len;
4f774513 6197 /* word4 iocb=did wge=rsvd. */
f0d9bccc 6198 wqe->xmit_els_rsp.rsvd4 = 0;
4f774513
JS
6199 /* word5 iocb=rsvd wge=did */
6200 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6201 iocbq->iocb.un.elsreq64.remoteID);
f0d9bccc
JS
6202 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
6203 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6204 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
6205 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
6206 iocbq->iocb.ulpContext);
4f774513 6207 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 6208 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
4f774513 6209 iocbq->vport->vpi + phba->vpi_base);
f0d9bccc
JS
6210 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
6211 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
6212 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
6213 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
6214 LPFC_WQE_LENLOC_WORD3);
6215 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
4f774513
JS
6216 command_type = OTHER_COMMAND;
6217 break;
6218 case CMD_CLOSE_XRI_CN:
6219 case CMD_ABORT_XRI_CN:
6220 case CMD_ABORT_XRI_CX:
6221 /* words 0-2 memcpy should be 0 rserved */
6222 /* port will send abts */
dcf2a4e0
JS
6223 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
6224 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
6225 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
6226 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
6227 } else
6228 fip = 0;
6229
6230 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 6231 /*
dcf2a4e0
JS
6232 * The link is down, or the command was ELS_FIP
6233 * so the fw does not need to send abts
4f774513
JS
6234 * on the wire.
6235 */
6236 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6237 else
6238 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6239 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
6240 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
6241 wqe->abort_cmd.rsrvd5 = 0;
6242 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
6243 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6244 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
6245 /*
6246 * The abort handler will send us CMD_ABORT_XRI_CN or
6247 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6248 */
f0d9bccc
JS
6249 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
6250 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
6251 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
6252 LPFC_WQE_LENLOC_NONE);
4f774513
JS
6253 cmnd = CMD_ABORT_XRI_CX;
6254 command_type = OTHER_COMMAND;
6255 xritag = 0;
6256 break;
6669f9bb
JS
6257 case CMD_XMIT_BLS_RSP64_CX:
6258 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6259 * we re-construct this WQE here based on information in
6260 * iocbq from scratch.
6261 */
6262 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 6263 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 6264 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
5ffc266e
JS
6265 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6266 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6267 LPFC_ABTS_UNSOL_INT) {
6268 /* ABTS sent by initiator to CT exchange, the
6269 * RX_ID field will be filled with the newly
6270 * allocated responder XRI.
6271 */
6272 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6273 iocbq->sli4_xritag);
6274 } else {
6275 /* ABTS sent by responder to CT exchange, the
6276 * RX_ID field will be filled with the responder
6277 * RX_ID from ABTS.
6278 */
6279 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6280 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6281 }
6669f9bb
JS
6282 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6283 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6284 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6285 iocbq->iocb.ulpContext);
f0d9bccc
JS
6286 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
6287 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
6288 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
6289 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6290 command_type = OTHER_COMMAND;
6291 break;
4f774513
JS
6292 case CMD_XRI_ABORTED_CX:
6293 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
6294 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6295 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6296 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6297 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6298 default:
6299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6300 "2014 Invalid command 0x%x\n",
6301 iocbq->iocb.ulpCommand);
6302 return IOCB_ERROR;
6303 break;
4f774513 6304 }
f0d9bccc
JS
6305 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
6306 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
6307 wqe->generic.wqe_com.abort_tag = abort_tag;
6308 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
6309 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
6310 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
6311 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
6312 return 0;
6313}
6314
6315/**
6316 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6317 * @phba: Pointer to HBA context object.
6318 * @ring_number: SLI ring number to issue iocb on.
6319 * @piocb: Pointer to command iocb.
6320 * @flag: Flag indicating if this command can be put into txq.
6321 *
6322 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6323 * an iocb command to an HBA with SLI-4 interface spec.
6324 *
6325 * This function is called with hbalock held. The function will return success
6326 * after it successfully submit the iocb to firmware or after adding to the
6327 * txq.
6328 **/
6329static int
6330__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6331 struct lpfc_iocbq *piocb, uint32_t flag)
6332{
6333 struct lpfc_sglq *sglq;
4f774513
JS
6334 union lpfc_wqe wqe;
6335 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
6336
6337 if (piocb->sli4_xritag == NO_XRI) {
6338 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6339 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6340 sglq = NULL;
6341 else {
2a9bf3d0
JS
6342 if (pring->txq_cnt) {
6343 if (!(flag & SLI_IOCB_RET_IOCB)) {
6344 __lpfc_sli_ringtx_put(phba,
6345 pring, piocb);
6346 return IOCB_SUCCESS;
6347 } else {
6348 return IOCB_BUSY;
6349 }
6350 } else {
4f774513 6351 sglq = __lpfc_sli_get_sglq(phba);
2a9bf3d0
JS
6352 if (!sglq) {
6353 if (!(flag & SLI_IOCB_RET_IOCB)) {
6354 __lpfc_sli_ringtx_put(phba,
6355 pring,
6356 piocb);
6357 return IOCB_SUCCESS;
6358 } else
6359 return IOCB_BUSY;
6360 }
6361 }
4f774513
JS
6362 }
6363 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6364 sglq = NULL; /* These IO's already have an XRI and
6365 * a mapped sgl.
6366 */
6367 } else {
6368 /* This is a continuation of a commandi,(CX) so this
6369 * sglq is on the active list
6370 */
6371 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6372 if (!sglq)
6373 return IOCB_ERROR;
6374 }
6375
6376 if (sglq) {
2a9bf3d0
JS
6377 piocb->sli4_xritag = sglq->sli4_xritag;
6378
6379 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
6380 return IOCB_ERROR;
6381 }
6382
6383 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6384 return IOCB_ERROR;
6385
341af102
JS
6386 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6387 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
6388 /*
6389 * For FCP command IOCB, get a new WQ index to distribute
6390 * WQE across the WQsr. On the other hand, for abort IOCB,
6391 * it carries the same WQ index to the original command
6392 * IOCB.
6393 */
341af102 6394 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
6395 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6396 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6397 &wqe))
4f774513
JS
6398 return IOCB_ERROR;
6399 } else {
6400 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6401 return IOCB_ERROR;
6402 }
6403 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6404
6405 return 0;
6406}
6407
6408/**
6409 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6410 *
6411 * This routine wraps the actual lockless version for issusing IOCB function
6412 * pointer from the lpfc_hba struct.
6413 *
6414 * Return codes:
6415 * IOCB_ERROR - Error
6416 * IOCB_SUCCESS - Success
6417 * IOCB_BUSY - Busy
6418 **/
2a9bf3d0 6419int
4f774513
JS
6420__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6421 struct lpfc_iocbq *piocb, uint32_t flag)
6422{
6423 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6424}
6425
6426/**
6427 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6428 * @phba: The hba struct for which this call is being executed.
6429 * @dev_grp: The HBA PCI-Device group number.
6430 *
6431 * This routine sets up the SLI interface API function jump table in @phba
6432 * struct.
6433 * Returns: 0 - success, -ENODEV - failure.
6434 **/
6435int
6436lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6437{
6438
6439 switch (dev_grp) {
6440 case LPFC_PCI_DEV_LP:
6441 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6442 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6443 break;
6444 case LPFC_PCI_DEV_OC:
6445 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6446 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6447 break;
6448 default:
6449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6450 "1419 Invalid HBA PCI-device group: 0x%x\n",
6451 dev_grp);
6452 return -ENODEV;
6453 break;
6454 }
6455 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6456 return 0;
6457}
6458
6459/**
6460 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6461 * @phba: Pointer to HBA context object.
6462 * @pring: Pointer to driver SLI ring object.
6463 * @piocb: Pointer to command iocb.
6464 * @flag: Flag indicating if this command can be put into txq.
6465 *
6466 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6467 * function. This function gets the hbalock and calls
6468 * __lpfc_sli_issue_iocb function and will return the error returned
6469 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6470 * functions which do not hold hbalock.
6471 **/
6472int
6473lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6474 struct lpfc_iocbq *piocb, uint32_t flag)
6475{
6476 unsigned long iflags;
6477 int rc;
6478
6479 spin_lock_irqsave(&phba->hbalock, iflags);
6480 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6481 spin_unlock_irqrestore(&phba->hbalock, iflags);
6482
6483 return rc;
6484}
6485
6486/**
6487 * lpfc_extra_ring_setup - Extra ring setup function
6488 * @phba: Pointer to HBA context object.
6489 *
6490 * This function is called while driver attaches with the
6491 * HBA to setup the extra ring. The extra ring is used
6492 * only when driver needs to support target mode functionality
6493 * or IP over FC functionalities.
6494 *
6495 * This function is called with no lock held.
6496 **/
6497static int
6498lpfc_extra_ring_setup( struct lpfc_hba *phba)
6499{
6500 struct lpfc_sli *psli;
6501 struct lpfc_sli_ring *pring;
6502
6503 psli = &phba->sli;
6504
6505 /* Adjust cmd/rsp ring iocb entries more evenly */
6506
6507 /* Take some away from the FCP ring */
6508 pring = &psli->ring[psli->fcp_ring];
6509 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6510 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6511 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6512 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6513
a4bc3379
JS
6514 /* and give them to the extra ring */
6515 pring = &psli->ring[psli->extra_ring];
6516
cf5bf97e
JW
6517 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6518 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6519 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6520 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6521
6522 /* Setup default profile for this ring */
6523 pring->iotag_max = 4096;
6524 pring->num_mask = 1;
6525 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6526 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6527 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6528 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6529 return 0;
6530}
6531
e59058c4 6532/**
3621a710 6533 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6534 * @phba: Pointer to HBA context object.
6535 * @pring: Pointer to driver SLI ring object.
6536 * @iocbq: Pointer to iocb object.
6537 *
6538 * This function is called by the slow ring event handler
6539 * function when there is an ASYNC event iocb in the ring.
6540 * This function is called with no lock held.
6541 * Currently this function handles only temperature related
6542 * ASYNC events. The function decodes the temperature sensor
6543 * event message and posts events for the management applications.
6544 **/
98c9ea5c 6545static void
57127f15
JS
6546lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6547 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6548{
6549 IOCB_t *icmd;
6550 uint16_t evt_code;
6551 uint16_t temp;
6552 struct temp_event temp_event_data;
6553 struct Scsi_Host *shost;
a257bf90 6554 uint32_t *iocb_w;
57127f15
JS
6555
6556 icmd = &iocbq->iocb;
6557 evt_code = icmd->un.asyncstat.evt_code;
6558 temp = icmd->ulpContext;
6559
6560 if ((evt_code != ASYNC_TEMP_WARN) &&
6561 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6562 iocb_w = (uint32_t *) icmd;
57127f15
JS
6563 lpfc_printf_log(phba,
6564 KERN_ERR,
6565 LOG_SLI,
76bb24ef 6566 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6567 " evt_code 0x%x\n"
a257bf90
JS
6568 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6569 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6570 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6571 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6572 pring->ringno,
a257bf90
JS
6573 icmd->un.asyncstat.evt_code,
6574 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6575 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6576 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6577 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6578
57127f15
JS
6579 return;
6580 }
6581 temp_event_data.data = (uint32_t)temp;
6582 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6583 if (evt_code == ASYNC_TEMP_WARN) {
6584 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6585 lpfc_printf_log(phba,
09372820 6586 KERN_ERR,
57127f15 6587 LOG_TEMP,
76bb24ef 6588 "0347 Adapter is very hot, please take "
57127f15
JS
6589 "corrective action. temperature : %d Celsius\n",
6590 temp);
6591 }
6592 if (evt_code == ASYNC_TEMP_SAFE) {
6593 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6594 lpfc_printf_log(phba,
09372820 6595 KERN_ERR,
57127f15
JS
6596 LOG_TEMP,
6597 "0340 Adapter temperature is OK now. "
6598 "temperature : %d Celsius\n",
6599 temp);
6600 }
6601
6602 /* Send temperature change event to applications */
6603 shost = lpfc_shost_from_vport(phba->pport);
6604 fc_host_post_vendor_event(shost, fc_get_event_number(),
6605 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6606 LPFC_NL_VENDOR_ID);
57127f15
JS
6607
6608}
6609
6610
e59058c4 6611/**
3621a710 6612 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6613 * @phba: Pointer to HBA context object.
6614 *
6615 * lpfc_sli_setup sets up rings of the SLI interface with
6616 * number of iocbs per ring and iotags. This function is
6617 * called while driver attach to the HBA and before the
6618 * interrupts are enabled. So there is no need for locking.
6619 *
6620 * This function always returns 0.
6621 **/
dea3101e
JB
6622int
6623lpfc_sli_setup(struct lpfc_hba *phba)
6624{
ed957684 6625 int i, totiocbsize = 0;
dea3101e
JB
6626 struct lpfc_sli *psli = &phba->sli;
6627 struct lpfc_sli_ring *pring;
6628
6629 psli->num_rings = MAX_CONFIGURED_RINGS;
6630 psli->sli_flag = 0;
6631 psli->fcp_ring = LPFC_FCP_RING;
6632 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6633 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6634
604a3e30
JB
6635 psli->iocbq_lookup = NULL;
6636 psli->iocbq_lookup_len = 0;
6637 psli->last_iotag = 0;
6638
dea3101e
JB
6639 for (i = 0; i < psli->num_rings; i++) {
6640 pring = &psli->ring[i];
6641 switch (i) {
6642 case LPFC_FCP_RING: /* ring 0 - FCP */
6643 /* numCiocb and numRiocb are used in config_port */
6644 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6645 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6646 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6647 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6648 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6649 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6650 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6651 SLI3_IOCB_CMD_SIZE :
6652 SLI2_IOCB_CMD_SIZE;
ed957684 6653 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6654 SLI3_IOCB_RSP_SIZE :
6655 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
6656 pring->iotag_ctr = 0;
6657 pring->iotag_max =
92d7f7b0 6658 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
6659 pring->fast_iotag = pring->iotag_max;
6660 pring->num_mask = 0;
6661 break;
a4bc3379 6662 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e
JB
6663 /* numCiocb and numRiocb are used in config_port */
6664 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6665 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6666 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6667 SLI3_IOCB_CMD_SIZE :
6668 SLI2_IOCB_CMD_SIZE;
ed957684 6669 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6670 SLI3_IOCB_RSP_SIZE :
6671 SLI2_IOCB_RSP_SIZE;
2e0fef85 6672 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
6673 pring->num_mask = 0;
6674 break;
6675 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6676 /* numCiocb and numRiocb are used in config_port */
6677 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6678 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6679 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6680 SLI3_IOCB_CMD_SIZE :
6681 SLI2_IOCB_CMD_SIZE;
ed957684 6682 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6683 SLI3_IOCB_RSP_SIZE :
6684 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
6685 pring->fast_iotag = 0;
6686 pring->iotag_ctr = 0;
6687 pring->iotag_max = 4096;
57127f15
JS
6688 pring->lpfc_sli_rcv_async_status =
6689 lpfc_sli_async_event_handler;
6669f9bb 6690 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6691 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6692 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6693 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6694 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6695 lpfc_els_unsol_event;
dea3101e 6696 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6697 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6698 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6699 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6700 lpfc_els_unsol_event;
dea3101e
JB
6701 pring->prt[2].profile = 0; /* Mask 2 */
6702 /* NameServer Inquiry */
6a9c52cf 6703 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6704 /* NameServer */
6a9c52cf 6705 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6706 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6707 lpfc_ct_unsol_event;
dea3101e
JB
6708 pring->prt[3].profile = 0; /* Mask 3 */
6709 /* NameServer response */
6a9c52cf 6710 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6711 /* NameServer */
6a9c52cf 6712 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6713 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6714 lpfc_ct_unsol_event;
6669f9bb
JS
6715 /* abort unsolicited sequence */
6716 pring->prt[4].profile = 0; /* Mask 4 */
6717 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6718 pring->prt[4].type = FC_TYPE_BLS;
6719 pring->prt[4].lpfc_sli_rcv_unsol_event =
6720 lpfc_sli4_ct_abort_unsol_event;
dea3101e
JB
6721 break;
6722 }
ed957684 6723 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6724 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6725 }
ed957684 6726 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6727 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6728 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6729 "SLI2 SLIM Data: x%x x%lx\n",
6730 phba->brd_no, totiocbsize,
6731 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6732 }
cf5bf97e
JW
6733 if (phba->cfg_multi_ring_support == 2)
6734 lpfc_extra_ring_setup(phba);
dea3101e
JB
6735
6736 return 0;
6737}
6738
e59058c4 6739/**
3621a710 6740 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6741 * @phba: Pointer to HBA context object.
6742 *
6743 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6744 * ring. This function also initializes ring indices of each ring.
6745 * This function is called during the initialization of the SLI
6746 * interface of an HBA.
6747 * This function is called with no lock held and always returns
6748 * 1.
6749 **/
dea3101e 6750int
2e0fef85 6751lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e
JB
6752{
6753 struct lpfc_sli *psli;
6754 struct lpfc_sli_ring *pring;
604a3e30 6755 int i;
dea3101e
JB
6756
6757 psli = &phba->sli;
2e0fef85 6758 spin_lock_irq(&phba->hbalock);
dea3101e 6759 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6760 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
6761 /* Initialize list headers for txq and txcmplq as double linked lists */
6762 for (i = 0; i < psli->num_rings; i++) {
6763 pring = &psli->ring[i];
6764 pring->ringno = i;
6765 pring->next_cmdidx = 0;
6766 pring->local_getidx = 0;
6767 pring->cmdidx = 0;
6768 INIT_LIST_HEAD(&pring->txq);
6769 INIT_LIST_HEAD(&pring->txcmplq);
6770 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6771 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6772 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6773 }
2e0fef85
JS
6774 spin_unlock_irq(&phba->hbalock);
6775 return 1;
dea3101e
JB
6776}
6777
04c68496
JS
6778/**
6779 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6780 * @phba: Pointer to HBA context object.
6781 *
6782 * This routine flushes the mailbox command subsystem. It will unconditionally
6783 * flush all the mailbox commands in the three possible stages in the mailbox
6784 * command sub-system: pending mailbox command queue; the outstanding mailbox
6785 * command; and completed mailbox command queue. It is caller's responsibility
6786 * to make sure that the driver is in the proper state to flush the mailbox
6787 * command sub-system. Namely, the posting of mailbox commands into the
6788 * pending mailbox command queue from the various clients must be stopped;
6789 * either the HBA is in a state that it will never works on the outstanding
6790 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6791 * mailbox command has been completed.
6792 **/
6793static void
6794lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6795{
6796 LIST_HEAD(completions);
6797 struct lpfc_sli *psli = &phba->sli;
6798 LPFC_MBOXQ_t *pmb;
6799 unsigned long iflag;
6800
6801 /* Flush all the mailbox commands in the mbox system */
6802 spin_lock_irqsave(&phba->hbalock, iflag);
6803 /* The pending mailbox command queue */
6804 list_splice_init(&phba->sli.mboxq, &completions);
6805 /* The outstanding active mailbox command */
6806 if (psli->mbox_active) {
6807 list_add_tail(&psli->mbox_active->list, &completions);
6808 psli->mbox_active = NULL;
6809 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6810 }
6811 /* The completed mailbox command queue */
6812 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6813 spin_unlock_irqrestore(&phba->hbalock, iflag);
6814
6815 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6816 while (!list_empty(&completions)) {
6817 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6818 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6819 if (pmb->mbox_cmpl)
6820 pmb->mbox_cmpl(phba, pmb);
6821 }
6822}
6823
e59058c4 6824/**
3621a710 6825 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6826 * @vport: Pointer to virtual port object.
6827 *
6828 * lpfc_sli_host_down is called to clean up the resources
6829 * associated with a vport before destroying virtual
6830 * port data structures.
6831 * This function does following operations:
6832 * - Free discovery resources associated with this virtual
6833 * port.
6834 * - Free iocbs associated with this virtual port in
6835 * the txq.
6836 * - Send abort for all iocb commands associated with this
6837 * vport in txcmplq.
6838 *
6839 * This function is called with no lock held and always returns 1.
6840 **/
92d7f7b0
JS
6841int
6842lpfc_sli_host_down(struct lpfc_vport *vport)
6843{
858c9f6c 6844 LIST_HEAD(completions);
92d7f7b0
JS
6845 struct lpfc_hba *phba = vport->phba;
6846 struct lpfc_sli *psli = &phba->sli;
6847 struct lpfc_sli_ring *pring;
6848 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6849 int i;
6850 unsigned long flags = 0;
6851 uint16_t prev_pring_flag;
6852
6853 lpfc_cleanup_discovery_resources(vport);
6854
6855 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6856 for (i = 0; i < psli->num_rings; i++) {
6857 pring = &psli->ring[i];
6858 prev_pring_flag = pring->flag;
5e9d9b82
JS
6859 /* Only slow rings */
6860 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6861 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6862 /* Set the lpfc data pending flag */
6863 set_bit(LPFC_DATA_READY, &phba->data_flags);
6864 }
92d7f7b0
JS
6865 /*
6866 * Error everything on the txq since these iocbs have not been
6867 * given to the FW yet.
6868 */
92d7f7b0
JS
6869 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6870 if (iocb->vport != vport)
6871 continue;
858c9f6c 6872 list_move_tail(&iocb->list, &completions);
92d7f7b0 6873 pring->txq_cnt--;
92d7f7b0
JS
6874 }
6875
6876 /* Next issue ABTS for everything on the txcmplq */
6877 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6878 list) {
6879 if (iocb->vport != vport)
6880 continue;
6881 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6882 }
6883
6884 pring->flag = prev_pring_flag;
6885 }
6886
6887 spin_unlock_irqrestore(&phba->hbalock, flags);
6888
a257bf90
JS
6889 /* Cancel all the IOCBs from the completions list */
6890 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6891 IOERR_SLI_DOWN);
92d7f7b0
JS
6892 return 1;
6893}
6894
e59058c4 6895/**
3621a710 6896 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6897 * @phba: Pointer to HBA context object.
6898 *
6899 * This function cleans up all iocb, buffers, mailbox commands
6900 * while shutting down the HBA. This function is called with no
6901 * lock held and always returns 1.
6902 * This function does the following to cleanup driver resources:
6903 * - Free discovery resources for each virtual port
6904 * - Cleanup any pending fabric iocbs
6905 * - Iterate through the iocb txq and free each entry
6906 * in the list.
6907 * - Free up any buffer posted to the HBA
6908 * - Free mailbox commands in the mailbox queue.
6909 **/
dea3101e 6910int
2e0fef85 6911lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6912{
2534ba75 6913 LIST_HEAD(completions);
2e0fef85 6914 struct lpfc_sli *psli = &phba->sli;
dea3101e 6915 struct lpfc_sli_ring *pring;
0ff10d46 6916 struct lpfc_dmabuf *buf_ptr;
dea3101e 6917 unsigned long flags = 0;
04c68496
JS
6918 int i;
6919
6920 /* Shutdown the mailbox command sub-system */
6921 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6922
dea3101e
JB
6923 lpfc_hba_down_prep(phba);
6924
92d7f7b0
JS
6925 lpfc_fabric_abort_hba(phba);
6926
2e0fef85 6927 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e
JB
6928 for (i = 0; i < psli->num_rings; i++) {
6929 pring = &psli->ring[i];
5e9d9b82
JS
6930 /* Only slow rings */
6931 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6932 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6933 /* Set the lpfc data pending flag */
6934 set_bit(LPFC_DATA_READY, &phba->data_flags);
6935 }
dea3101e
JB
6936
6937 /*
6938 * Error everything on the txq since these iocbs have not been
6939 * given to the FW yet.
6940 */
2534ba75 6941 list_splice_init(&pring->txq, &completions);
dea3101e
JB
6942 pring->txq_cnt = 0;
6943
2534ba75 6944 }
2e0fef85 6945 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6946
a257bf90
JS
6947 /* Cancel all the IOCBs from the completions list */
6948 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6949 IOERR_SLI_DOWN);
dea3101e 6950
0ff10d46
JS
6951 spin_lock_irqsave(&phba->hbalock, flags);
6952 list_splice_init(&phba->elsbuf, &completions);
6953 phba->elsbuf_cnt = 0;
6954 phba->elsbuf_prev_cnt = 0;
6955 spin_unlock_irqrestore(&phba->hbalock, flags);
6956
6957 while (!list_empty(&completions)) {
6958 list_remove_head(&completions, buf_ptr,
6959 struct lpfc_dmabuf, list);
6960 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6961 kfree(buf_ptr);
6962 }
6963
dea3101e
JB
6964 /* Return any active mbox cmds */
6965 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6966
da0436e9 6967 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6968 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6969 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6970
da0436e9
JS
6971 return 1;
6972}
6973
e59058c4 6974/**
3621a710 6975 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6976 * @srcp: Source memory pointer.
6977 * @destp: Destination memory pointer.
6978 * @cnt: Number of words required to be copied.
6979 *
6980 * This function is used for copying data between driver memory
6981 * and the SLI memory. This function also changes the endianness
6982 * of each word if native endianness is different from SLI
6983 * endianness. This function can be called with or without
6984 * lock.
6985 **/
dea3101e
JB
6986void
6987lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6988{
6989 uint32_t *src = srcp;
6990 uint32_t *dest = destp;
6991 uint32_t ldata;
6992 int i;
6993
6994 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6995 ldata = *src;
6996 ldata = le32_to_cpu(ldata);
6997 *dest = ldata;
6998 src++;
6999 dest++;
7000 }
7001}
7002
e59058c4 7003
a0c87cbd
JS
7004/**
7005 * lpfc_sli_bemem_bcopy - SLI memory copy function
7006 * @srcp: Source memory pointer.
7007 * @destp: Destination memory pointer.
7008 * @cnt: Number of words required to be copied.
7009 *
7010 * This function is used for copying data between a data structure
7011 * with big endian representation to local endianness.
7012 * This function can be called with or without lock.
7013 **/
7014void
7015lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
7016{
7017 uint32_t *src = srcp;
7018 uint32_t *dest = destp;
7019 uint32_t ldata;
7020 int i;
7021
7022 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
7023 ldata = *src;
7024 ldata = be32_to_cpu(ldata);
7025 *dest = ldata;
7026 src++;
7027 dest++;
7028 }
7029}
7030
e59058c4 7031/**
3621a710 7032 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
7033 * @phba: Pointer to HBA context object.
7034 * @pring: Pointer to driver SLI ring object.
7035 * @mp: Pointer to driver buffer object.
7036 *
7037 * This function is called with no lock held.
7038 * It always return zero after adding the buffer to the postbufq
7039 * buffer list.
7040 **/
dea3101e 7041int
2e0fef85
JS
7042lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7043 struct lpfc_dmabuf *mp)
dea3101e
JB
7044{
7045 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
7046 later */
2e0fef85 7047 spin_lock_irq(&phba->hbalock);
dea3101e 7048 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 7049 pring->postbufq_cnt++;
2e0fef85 7050 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
7051 return 0;
7052}
7053
e59058c4 7054/**
3621a710 7055 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
7056 * @phba: Pointer to HBA context object.
7057 *
7058 * When HBQ is enabled, buffers are searched based on tags. This function
7059 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
7060 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
7061 * does not conflict with tags of buffer posted for unsolicited events.
7062 * The function returns the allocated tag. The function is called with
7063 * no locks held.
7064 **/
76bb24ef
JS
7065uint32_t
7066lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
7067{
7068 spin_lock_irq(&phba->hbalock);
7069 phba->buffer_tag_count++;
7070 /*
7071 * Always set the QUE_BUFTAG_BIT to distiguish between
7072 * a tag assigned by HBQ.
7073 */
7074 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
7075 spin_unlock_irq(&phba->hbalock);
7076 return phba->buffer_tag_count;
7077}
7078
e59058c4 7079/**
3621a710 7080 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
7081 * @phba: Pointer to HBA context object.
7082 * @pring: Pointer to driver SLI ring object.
7083 * @tag: Buffer tag.
7084 *
7085 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
7086 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
7087 * iocb is posted to the response ring with the tag of the buffer.
7088 * This function searches the pring->postbufq list using the tag
7089 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
7090 * iocb. If the buffer is found then lpfc_dmabuf object of the
7091 * buffer is returned to the caller else NULL is returned.
7092 * This function is called with no lock held.
7093 **/
76bb24ef
JS
7094struct lpfc_dmabuf *
7095lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7096 uint32_t tag)
7097{
7098 struct lpfc_dmabuf *mp, *next_mp;
7099 struct list_head *slp = &pring->postbufq;
7100
7101 /* Search postbufq, from the begining, looking for a match on tag */
7102 spin_lock_irq(&phba->hbalock);
7103 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7104 if (mp->buffer_tag == tag) {
7105 list_del_init(&mp->list);
7106 pring->postbufq_cnt--;
7107 spin_unlock_irq(&phba->hbalock);
7108 return mp;
7109 }
7110 }
7111
7112 spin_unlock_irq(&phba->hbalock);
7113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 7114 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
7115 "ring %d Data x%lx x%p x%p x%x\n",
7116 pring->ringno, (unsigned long) tag,
7117 slp->next, slp->prev, pring->postbufq_cnt);
7118
7119 return NULL;
7120}
dea3101e 7121
e59058c4 7122/**
3621a710 7123 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
7124 * @phba: Pointer to HBA context object.
7125 * @pring: Pointer to driver SLI ring object.
7126 * @phys: DMA address of the buffer.
7127 *
7128 * This function searches the buffer list using the dma_address
7129 * of unsolicited event to find the driver's lpfc_dmabuf object
7130 * corresponding to the dma_address. The function returns the
7131 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7132 * This function is called by the ct and els unsolicited event
7133 * handlers to get the buffer associated with the unsolicited
7134 * event.
7135 *
7136 * This function is called with no lock held.
7137 **/
dea3101e
JB
7138struct lpfc_dmabuf *
7139lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7140 dma_addr_t phys)
7141{
7142 struct lpfc_dmabuf *mp, *next_mp;
7143 struct list_head *slp = &pring->postbufq;
7144
7145 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 7146 spin_lock_irq(&phba->hbalock);
dea3101e
JB
7147 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7148 if (mp->phys == phys) {
7149 list_del_init(&mp->list);
7150 pring->postbufq_cnt--;
2e0fef85 7151 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
7152 return mp;
7153 }
7154 }
7155
2e0fef85 7156 spin_unlock_irq(&phba->hbalock);
dea3101e 7157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 7158 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 7159 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 7160 pring->ringno, (unsigned long long)phys,
dea3101e
JB
7161 slp->next, slp->prev, pring->postbufq_cnt);
7162 return NULL;
7163}
7164
e59058c4 7165/**
3621a710 7166 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
7167 * @phba: Pointer to HBA context object.
7168 * @cmdiocb: Pointer to driver command iocb object.
7169 * @rspiocb: Pointer to driver response iocb object.
7170 *
7171 * This function is the completion handler for the abort iocbs for
7172 * ELS commands. This function is called from the ELS ring event
7173 * handler with no lock held. This function frees memory resources
7174 * associated with the abort iocb.
7175 **/
dea3101e 7176static void
2e0fef85
JS
7177lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7178 struct lpfc_iocbq *rspiocb)
dea3101e 7179{
2e0fef85 7180 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7181 uint16_t abort_iotag, abort_context;
92d7f7b0 7182 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7183 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7184
7185 abort_iocb = NULL;
2680eeaa
JS
7186
7187 if (irsp->ulpStatus) {
7188 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7189 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7190
2e0fef85 7191 spin_lock_irq(&phba->hbalock);
45ed1190
JS
7192 if (phba->sli_rev < LPFC_SLI_REV4) {
7193 if (abort_iotag != 0 &&
7194 abort_iotag <= phba->sli.last_iotag)
7195 abort_iocb =
7196 phba->sli.iocbq_lookup[abort_iotag];
7197 } else
7198 /* For sli4 the abort_tag is the XRI,
7199 * so the abort routine puts the iotag of the iocb
7200 * being aborted in the context field of the abort
7201 * IOCB.
7202 */
7203 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 7204
58da1ffb
JS
7205 /*
7206 * If the iocb is not found in Firmware queue the iocb
7207 * might have completed already. Do not free it again.
7208 */
9b379605 7209 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
7210 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7211 spin_unlock_irq(&phba->hbalock);
7212 lpfc_sli_release_iocbq(phba, cmdiocb);
7213 return;
7214 }
7215 /* For SLI4 the ulpContext field for abort IOCB
7216 * holds the iotag of the IOCB being aborted so
7217 * the local abort_context needs to be reset to
7218 * match the aborted IOCBs ulpContext.
7219 */
7220 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7221 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 7222 }
2a9bf3d0
JS
7223
7224 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
7225 "0327 Cannot abort els iocb %p "
7226 "with tag %x context %x, abort status %x, "
7227 "abort code %x\n",
7228 abort_iocb, abort_iotag, abort_context,
7229 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa
JS
7230 /*
7231 * make sure we have the right iocbq before taking it
7232 * off the txcmplq and try to call completion routine.
7233 */
2e0fef85
JS
7234 if (!abort_iocb ||
7235 abort_iocb->iocb.ulpContext != abort_context ||
7236 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7237 spin_unlock_irq(&phba->hbalock);
341af102
JS
7238 else if (phba->sli_rev < LPFC_SLI_REV4) {
7239 /*
7240 * leave the SLI4 aborted command on the txcmplq
7241 * list and the command complete WCQE's XB bit
7242 * will tell whether the SGL (XRI) can be released
7243 * immediately or to the aborted SGL list for the
7244 * following abort XRI from the HBA.
7245 */
92d7f7b0 7246 list_del_init(&abort_iocb->list);
2a9bf3d0
JS
7247 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
7248 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
7249 pring->txcmplq_cnt--;
7250 }
2680eeaa 7251
0ff10d46
JS
7252 /* Firmware could still be in progress of DMAing
7253 * payload, so don't free data buffer till after
7254 * a hbeat.
7255 */
7256 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 7257 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
7258 spin_unlock_irq(&phba->hbalock);
7259
92d7f7b0 7260 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 7261 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 7262 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
49198b37
JS
7263 } else
7264 spin_unlock_irq(&phba->hbalock);
2680eeaa
JS
7265 }
7266
604a3e30 7267 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
7268 return;
7269}
7270
e59058c4 7271/**
3621a710 7272 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7273 * @phba: Pointer to HBA context object.
7274 * @cmdiocb: Pointer to driver command iocb object.
7275 * @rspiocb: Pointer to driver response iocb object.
7276 *
7277 * The function is called from SLI ring event handler with no
7278 * lock held. This function is the completion handler for ELS commands
7279 * which are aborted. The function frees memory resources used for
7280 * the aborted ELS commands.
7281 **/
92d7f7b0
JS
7282static void
7283lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7284 struct lpfc_iocbq *rspiocb)
7285{
7286 IOCB_t *irsp = &rspiocb->iocb;
7287
7288 /* ELS cmd tag <ulpIoTag> completes */
7289 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7290 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7291 "x%x x%x x%x\n",
e8b62011 7292 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7293 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7294 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7295 lpfc_ct_free_iocb(phba, cmdiocb);
7296 else
7297 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7298 return;
7299}
7300
e59058c4 7301/**
5af5eee7 7302 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
7303 * @phba: Pointer to HBA context object.
7304 * @pring: Pointer to driver SLI ring object.
7305 * @cmdiocb: Pointer to driver command iocb object.
7306 *
5af5eee7
JS
7307 * This function issues an abort iocb for the provided command iocb down to
7308 * the port. Other than the case the outstanding command iocb is an abort
7309 * request, this function issues abort out unconditionally. This function is
7310 * called with hbalock held. The function returns 0 when it fails due to
7311 * memory allocation failure or when the command iocb is an abort request.
e59058c4 7312 **/
5af5eee7
JS
7313static int
7314lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7315 struct lpfc_iocbq *cmdiocb)
dea3101e 7316{
2e0fef85 7317 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7318 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
7319 IOCB_t *icmd = NULL;
7320 IOCB_t *iabt = NULL;
5af5eee7 7321 int retval;
07951076 7322
92d7f7b0
JS
7323 /*
7324 * There are certain command types we don't want to abort. And we
7325 * don't want to abort commands that are already in the process of
7326 * being aborted.
07951076
JS
7327 */
7328 icmd = &cmdiocb->iocb;
2e0fef85 7329 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7330 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7331 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7332 return 0;
7333
dea3101e 7334 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7335 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
7336 if (abtsiocbp == NULL)
7337 return 0;
dea3101e 7338
07951076 7339 /* This signals the response to set the correct status
341af102 7340 * before calling the completion handler
07951076
JS
7341 */
7342 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7343
dea3101e 7344 iabt = &abtsiocbp->iocb;
07951076
JS
7345 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7346 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 7347 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 7348 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
7349 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7350 }
da0436e9
JS
7351 else
7352 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7353 iabt->ulpLe = 1;
7354 iabt->ulpClass = icmd->ulpClass;
dea3101e 7355
5ffc266e
JS
7356 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7357 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
7358 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7359 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7360
2e0fef85 7361 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7362 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7363 else
7364 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7365
07951076 7366 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7367
e8b62011
JS
7368 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7369 "0339 Abort xri x%x, original iotag x%x, "
7370 "abort cmd iotag x%x\n",
2a9bf3d0 7371 iabt->un.acxri.abortIoTag,
e8b62011 7372 iabt->un.acxri.abortContextTag,
2a9bf3d0 7373 abtsiocbp->iotag);
da0436e9 7374 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7375
d7c255b2
JS
7376 if (retval)
7377 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
7378
7379 /*
7380 * Caller to this routine should check for IOCB_ERROR
7381 * and handle it properly. This routine no longer removes
7382 * iocb off txcmplq and call compl in case of IOCB_ERROR.
7383 */
7384 return retval;
7385}
7386
7387/**
7388 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
7389 * @phba: Pointer to HBA context object.
7390 * @pring: Pointer to driver SLI ring object.
7391 * @cmdiocb: Pointer to driver command iocb object.
7392 *
7393 * This function issues an abort iocb for the provided command iocb. In case
7394 * of unloading, the abort iocb will not be issued to commands on the ELS
7395 * ring. Instead, the callback function shall be changed to those commands
7396 * so that nothing happens when them finishes. This function is called with
7397 * hbalock held. The function returns 0 when the command iocb is an abort
7398 * request.
7399 **/
7400int
7401lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7402 struct lpfc_iocbq *cmdiocb)
7403{
7404 struct lpfc_vport *vport = cmdiocb->vport;
7405 int retval = IOCB_ERROR;
7406 IOCB_t *icmd = NULL;
7407
7408 /*
7409 * There are certain command types we don't want to abort. And we
7410 * don't want to abort commands that are already in the process of
7411 * being aborted.
7412 */
7413 icmd = &cmdiocb->iocb;
7414 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
7415 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7416 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
7417 return 0;
7418
7419 /*
7420 * If we're unloading, don't abort iocb on the ELS ring, but change
7421 * the callback so that nothing happens when it finishes.
7422 */
7423 if ((vport->load_flag & FC_UNLOADING) &&
7424 (pring->ringno == LPFC_ELS_RING)) {
7425 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7426 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7427 else
7428 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
7429 goto abort_iotag_exit;
7430 }
7431
7432 /* Now, we try to issue the abort to the cmdiocb out */
7433 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
7434
07951076 7435abort_iotag_exit:
2e0fef85
JS
7436 /*
7437 * Caller to this routine should check for IOCB_ERROR
7438 * and handle it properly. This routine no longer removes
7439 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7440 */
2e0fef85 7441 return retval;
dea3101e
JB
7442}
7443
5af5eee7
JS
7444/**
7445 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
7446 * @phba: Pointer to HBA context object.
7447 * @pring: Pointer to driver SLI ring object.
7448 *
7449 * This function aborts all iocbs in the given ring and frees all the iocb
7450 * objects in txq. This function issues abort iocbs unconditionally for all
7451 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
7452 * to complete before the return of this function. The caller is not required
7453 * to hold any locks.
7454 **/
7455static void
7456lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
7457{
7458 LIST_HEAD(completions);
7459 struct lpfc_iocbq *iocb, *next_iocb;
7460
7461 if (pring->ringno == LPFC_ELS_RING)
7462 lpfc_fabric_abort_hba(phba);
7463
7464 spin_lock_irq(&phba->hbalock);
7465
7466 /* Take off all the iocbs on txq for cancelling */
7467 list_splice_init(&pring->txq, &completions);
7468 pring->txq_cnt = 0;
7469
7470 /* Next issue ABTS for everything on the txcmplq */
7471 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
7472 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
7473
7474 spin_unlock_irq(&phba->hbalock);
7475
7476 /* Cancel all the IOCBs from the completions list */
7477 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7478 IOERR_SLI_ABORTED);
7479}
7480
7481/**
7482 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
7483 * @phba: pointer to lpfc HBA data structure.
7484 *
7485 * This routine will abort all pending and outstanding iocbs to an HBA.
7486 **/
7487void
7488lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
7489{
7490 struct lpfc_sli *psli = &phba->sli;
7491 struct lpfc_sli_ring *pring;
7492 int i;
7493
7494 for (i = 0; i < psli->num_rings; i++) {
7495 pring = &psli->ring[i];
7496 lpfc_sli_iocb_ring_abort(phba, pring);
7497 }
7498}
7499
e59058c4 7500/**
3621a710 7501 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7502 * @iocbq: Pointer to driver iocb object.
7503 * @vport: Pointer to driver virtual port object.
7504 * @tgt_id: SCSI ID of the target.
7505 * @lun_id: LUN ID of the scsi device.
7506 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7507 *
3621a710 7508 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7509 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7510 * 0 if the filtering criteria is met for the given iocb and will return
7511 * 1 if the filtering criteria is not met.
7512 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7513 * given iocb is for the SCSI device specified by vport, tgt_id and
7514 * lun_id parameter.
7515 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7516 * given iocb is for the SCSI target specified by vport and tgt_id
7517 * parameters.
7518 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7519 * given iocb is for the SCSI host associated with the given vport.
7520 * This function is called with no locks held.
7521 **/
dea3101e 7522static int
51ef4c26
JS
7523lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7524 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7525 lpfc_ctx_cmd ctx_cmd)
dea3101e 7526{
0bd4ca25 7527 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
7528 int rc = 1;
7529
0bd4ca25
JSEC
7530 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7531 return rc;
7532
51ef4c26
JS
7533 if (iocbq->vport != vport)
7534 return rc;
7535
0bd4ca25 7536 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7537
495a714c 7538 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
7539 return rc;
7540
7541 switch (ctx_cmd) {
7542 case LPFC_CTX_LUN:
495a714c
JS
7543 if ((lpfc_cmd->rdata->pnode) &&
7544 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7545 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
7546 rc = 0;
7547 break;
7548 case LPFC_CTX_TGT:
495a714c
JS
7549 if ((lpfc_cmd->rdata->pnode) &&
7550 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
7551 rc = 0;
7552 break;
dea3101e
JB
7553 case LPFC_CTX_HOST:
7554 rc = 0;
7555 break;
7556 default:
7557 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7558 __func__, ctx_cmd);
dea3101e
JB
7559 break;
7560 }
7561
7562 return rc;
7563}
7564
e59058c4 7565/**
3621a710 7566 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7567 * @vport: Pointer to virtual port.
7568 * @tgt_id: SCSI ID of the target.
7569 * @lun_id: LUN ID of the scsi device.
7570 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7571 *
7572 * This function returns number of FCP commands pending for the vport.
7573 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7574 * commands pending on the vport associated with SCSI device specified
7575 * by tgt_id and lun_id parameters.
7576 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7577 * commands pending on the vport associated with SCSI target specified
7578 * by tgt_id parameter.
7579 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7580 * commands pending on the vport.
7581 * This function returns the number of iocbs which satisfy the filter.
7582 * This function is called without any lock held.
7583 **/
dea3101e 7584int
51ef4c26
JS
7585lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7586 lpfc_ctx_cmd ctx_cmd)
dea3101e 7587{
51ef4c26 7588 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7589 struct lpfc_iocbq *iocbq;
7590 int sum, i;
dea3101e 7591
0bd4ca25
JSEC
7592 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7593 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7594
51ef4c26
JS
7595 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7596 ctx_cmd) == 0)
0bd4ca25 7597 sum++;
dea3101e 7598 }
0bd4ca25 7599
dea3101e
JB
7600 return sum;
7601}
7602
e59058c4 7603/**
3621a710 7604 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7605 * @phba: Pointer to HBA context object
7606 * @cmdiocb: Pointer to command iocb object.
7607 * @rspiocb: Pointer to response iocb object.
7608 *
7609 * This function is called when an aborted FCP iocb completes. This
7610 * function is called by the ring event handler with no lock held.
7611 * This function frees the iocb.
7612 **/
5eb95af0 7613void
2e0fef85
JS
7614lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7615 struct lpfc_iocbq *rspiocb)
5eb95af0 7616{
604a3e30 7617 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7618 return;
7619}
7620
e59058c4 7621/**
3621a710 7622 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7623 * @vport: Pointer to virtual port.
7624 * @pring: Pointer to driver SLI ring object.
7625 * @tgt_id: SCSI ID of the target.
7626 * @lun_id: LUN ID of the scsi device.
7627 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7628 *
7629 * This function sends an abort command for every SCSI command
7630 * associated with the given virtual port pending on the ring
7631 * filtered by lpfc_sli_validate_fcp_iocb function.
7632 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7633 * FCP iocbs associated with lun specified by tgt_id and lun_id
7634 * parameters
7635 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7636 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7637 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7638 * FCP iocbs associated with virtual port.
7639 * This function returns number of iocbs it failed to abort.
7640 * This function is called with no locks held.
7641 **/
dea3101e 7642int
51ef4c26
JS
7643lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7644 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7645{
51ef4c26 7646 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7647 struct lpfc_iocbq *iocbq;
7648 struct lpfc_iocbq *abtsiocb;
dea3101e 7649 IOCB_t *cmd = NULL;
dea3101e 7650 int errcnt = 0, ret_val = 0;
0bd4ca25 7651 int i;
dea3101e 7652
0bd4ca25
JSEC
7653 for (i = 1; i <= phba->sli.last_iotag; i++) {
7654 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7655
51ef4c26 7656 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7657 abort_cmd) != 0)
dea3101e
JB
7658 continue;
7659
7660 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7661 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
7662 if (abtsiocb == NULL) {
7663 errcnt++;
7664 continue;
7665 }
dea3101e 7666
0bd4ca25 7667 cmd = &iocbq->iocb;
dea3101e
JB
7668 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7669 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7670 if (phba->sli_rev == LPFC_SLI_REV4)
7671 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7672 else
7673 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
7674 abtsiocb->iocb.ulpLe = 1;
7675 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7676 abtsiocb->vport = phba->pport;
dea3101e 7677
5ffc266e
JS
7678 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7679 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
7680 if (iocbq->iocb_flag & LPFC_IO_FCP)
7681 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7682
2e0fef85 7683 if (lpfc_is_link_up(phba))
dea3101e
JB
7684 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7685 else
7686 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7687
5eb95af0
JSEC
7688 /* Setup callback routine and issue the command. */
7689 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7690 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7691 abtsiocb, 0);
dea3101e 7692 if (ret_val == IOCB_ERROR) {
604a3e30 7693 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
7694 errcnt++;
7695 continue;
7696 }
7697 }
7698
7699 return errcnt;
7700}
7701
e59058c4 7702/**
3621a710 7703 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7704 * @phba: Pointer to HBA context object.
7705 * @cmdiocbq: Pointer to command iocb.
7706 * @rspiocbq: Pointer to response iocb.
7707 *
7708 * This function is the completion handler for iocbs issued using
7709 * lpfc_sli_issue_iocb_wait function. This function is called by the
7710 * ring event handler function without any lock held. This function
7711 * can be called from both worker thread context and interrupt
7712 * context. This function also can be called from other thread which
7713 * cleans up the SLI layer objects.
7714 * This function copy the contents of the response iocb to the
7715 * response iocb memory object provided by the caller of
7716 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7717 * sleeps for the iocb completion.
7718 **/
68876920
JSEC
7719static void
7720lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7721 struct lpfc_iocbq *cmdiocbq,
7722 struct lpfc_iocbq *rspiocbq)
dea3101e 7723{
68876920
JSEC
7724 wait_queue_head_t *pdone_q;
7725 unsigned long iflags;
0f65ff68 7726 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7727
2e0fef85 7728 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7729 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7730 if (cmdiocbq->context2 && rspiocbq)
7731 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7732 &rspiocbq->iocb, sizeof(IOCB_t));
7733
0f65ff68
JS
7734 /* Set the exchange busy flag for task management commands */
7735 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7736 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7737 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7738 cur_iocbq);
7739 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7740 }
7741
68876920 7742 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7743 if (pdone_q)
7744 wake_up(pdone_q);
858c9f6c 7745 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
7746 return;
7747}
7748
d11e31dd
JS
7749/**
7750 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7751 * @phba: Pointer to HBA context object..
7752 * @piocbq: Pointer to command iocb.
7753 * @flag: Flag to test.
7754 *
7755 * This routine grabs the hbalock and then test the iocb_flag to
7756 * see if the passed in flag is set.
7757 * Returns:
7758 * 1 if flag is set.
7759 * 0 if flag is not set.
7760 **/
7761static int
7762lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7763 struct lpfc_iocbq *piocbq, uint32_t flag)
7764{
7765 unsigned long iflags;
7766 int ret;
7767
7768 spin_lock_irqsave(&phba->hbalock, iflags);
7769 ret = piocbq->iocb_flag & flag;
7770 spin_unlock_irqrestore(&phba->hbalock, iflags);
7771 return ret;
7772
7773}
7774
e59058c4 7775/**
3621a710 7776 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7777 * @phba: Pointer to HBA context object..
7778 * @pring: Pointer to sli ring.
7779 * @piocb: Pointer to command iocb.
7780 * @prspiocbq: Pointer to response iocb.
7781 * @timeout: Timeout in number of seconds.
7782 *
7783 * This function issues the iocb to firmware and waits for the
7784 * iocb to complete. If the iocb command is not
7785 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7786 * Caller should not free the iocb resources if this function
7787 * returns IOCB_TIMEDOUT.
7788 * The function waits for the iocb completion using an
7789 * non-interruptible wait.
7790 * This function will sleep while waiting for iocb completion.
7791 * So, this function should not be called from any context which
7792 * does not allow sleeping. Due to the same reason, this function
7793 * cannot be called with interrupt disabled.
7794 * This function assumes that the iocb completions occur while
7795 * this function sleep. So, this function cannot be called from
7796 * the thread which process iocb completion for this ring.
7797 * This function clears the iocb_flag of the iocb object before
7798 * issuing the iocb and the iocb completion handler sets this
7799 * flag and wakes this thread when the iocb completes.
7800 * The contents of the response iocb will be copied to prspiocbq
7801 * by the completion handler when the command completes.
7802 * This function returns IOCB_SUCCESS when success.
7803 * This function is called with no lock held.
7804 **/
dea3101e 7805int
2e0fef85 7806lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7807 uint32_t ring_number,
2e0fef85
JS
7808 struct lpfc_iocbq *piocb,
7809 struct lpfc_iocbq *prspiocbq,
68876920 7810 uint32_t timeout)
dea3101e 7811{
7259f0d0 7812 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7813 long timeleft, timeout_req = 0;
7814 int retval = IOCB_SUCCESS;
875fbdfe 7815 uint32_t creg_val;
2a9bf3d0 7816 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 7817 /*
68876920
JSEC
7818 * If the caller has provided a response iocbq buffer, then context2
7819 * is NULL or its an error.
dea3101e 7820 */
68876920
JSEC
7821 if (prspiocbq) {
7822 if (piocb->context2)
7823 return IOCB_ERROR;
7824 piocb->context2 = prspiocbq;
dea3101e
JB
7825 }
7826
68876920
JSEC
7827 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7828 piocb->context_un.wait_queue = &done_q;
7829 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7830
875fbdfe
JSEC
7831 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7832 creg_val = readl(phba->HCregaddr);
7833 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7834 writel(creg_val, phba->HCregaddr);
7835 readl(phba->HCregaddr); /* flush */
7836 }
7837
2a9bf3d0
JS
7838 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
7839 SLI_IOCB_RET_IOCB);
68876920
JSEC
7840 if (retval == IOCB_SUCCESS) {
7841 timeout_req = timeout * HZ;
68876920 7842 timeleft = wait_event_timeout(done_q,
d11e31dd 7843 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7844 timeout_req);
dea3101e 7845
7054a606
JS
7846 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7847 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7848 "0331 IOCB wake signaled\n");
7054a606 7849 } else if (timeleft == 0) {
68876920 7850 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7851 "0338 IOCB wait timeout error - no "
7852 "wake response Data x%x\n", timeout);
68876920 7853 retval = IOCB_TIMEDOUT;
7054a606 7854 } else {
68876920 7855 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7856 "0330 IOCB wake NOT set, "
7857 "Data x%x x%lx\n",
68876920
JSEC
7858 timeout, (timeleft / jiffies));
7859 retval = IOCB_TIMEDOUT;
dea3101e 7860 }
2a9bf3d0
JS
7861 } else if (retval == IOCB_BUSY) {
7862 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7863 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
7864 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
7865 return retval;
68876920
JSEC
7866 } else {
7867 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7868 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7869 retval);
68876920 7870 retval = IOCB_ERROR;
dea3101e
JB
7871 }
7872
875fbdfe
JSEC
7873 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7874 creg_val = readl(phba->HCregaddr);
7875 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7876 writel(creg_val, phba->HCregaddr);
7877 readl(phba->HCregaddr); /* flush */
7878 }
7879
68876920
JSEC
7880 if (prspiocbq)
7881 piocb->context2 = NULL;
7882
7883 piocb->context_un.wait_queue = NULL;
7884 piocb->iocb_cmpl = NULL;
dea3101e
JB
7885 return retval;
7886}
68876920 7887
e59058c4 7888/**
3621a710 7889 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7890 * @phba: Pointer to HBA context object.
7891 * @pmboxq: Pointer to driver mailbox object.
7892 * @timeout: Timeout in number of seconds.
7893 *
7894 * This function issues the mailbox to firmware and waits for the
7895 * mailbox command to complete. If the mailbox command is not
7896 * completed within timeout seconds, it returns MBX_TIMEOUT.
7897 * The function waits for the mailbox completion using an
7898 * interruptible wait. If the thread is woken up due to a
7899 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7900 * should not free the mailbox resources, if this function returns
7901 * MBX_TIMEOUT.
7902 * This function will sleep while waiting for mailbox completion.
7903 * So, this function should not be called from any context which
7904 * does not allow sleeping. Due to the same reason, this function
7905 * cannot be called with interrupt disabled.
7906 * This function assumes that the mailbox completion occurs while
7907 * this function sleep. So, this function cannot be called from
7908 * the worker thread which processes mailbox completion.
7909 * This function is called in the context of HBA management
7910 * applications.
7911 * This function returns MBX_SUCCESS when successful.
7912 * This function is called with no lock held.
7913 **/
dea3101e 7914int
2e0fef85 7915lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
7916 uint32_t timeout)
7917{
7259f0d0 7918 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7919 int retval;
858c9f6c 7920 unsigned long flag;
dea3101e
JB
7921
7922 /* The caller must leave context1 empty. */
98c9ea5c 7923 if (pmboxq->context1)
2e0fef85 7924 return MBX_NOT_FINISHED;
dea3101e 7925
495a714c 7926 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
7927 /* setup wake call as IOCB callback */
7928 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7929 /* setup context field to pass wait_queue pointer to wake function */
7930 pmboxq->context1 = &done_q;
7931
dea3101e
JB
7932 /* now issue the command */
7933 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7934
7935 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7936 wait_event_interruptible_timeout(done_q,
7937 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7938 timeout * HZ);
7939
858c9f6c 7940 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7941 pmboxq->context1 = NULL;
7054a606
JS
7942 /*
7943 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7944 * else do not free the resources.
7945 */
d7c47992 7946 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 7947 retval = MBX_SUCCESS;
d7c47992
JS
7948 lpfc_sli4_swap_str(phba, pmboxq);
7949 } else {
7054a606 7950 retval = MBX_TIMEOUT;
858c9f6c
JS
7951 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7952 }
7953 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e
JB
7954 }
7955
dea3101e
JB
7956 return retval;
7957}
7958
e59058c4 7959/**
3772a991 7960 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7961 * @phba: Pointer to HBA context.
7962 *
3772a991
JS
7963 * This function is called to shutdown the driver's mailbox sub-system.
7964 * It first marks the mailbox sub-system is in a block state to prevent
7965 * the asynchronous mailbox command from issued off the pending mailbox
7966 * command queue. If the mailbox command sub-system shutdown is due to
7967 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7968 * the mailbox sub-system flush routine to forcefully bring down the
7969 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7970 * as with offline or HBA function reset), this routine will wait for the
7971 * outstanding mailbox command to complete before invoking the mailbox
7972 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7973 **/
3772a991
JS
7974void
7975lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7976{
3772a991
JS
7977 struct lpfc_sli *psli = &phba->sli;
7978 uint8_t actcmd = MBX_HEARTBEAT;
7979 unsigned long timeout;
b4c02652 7980
3772a991
JS
7981 spin_lock_irq(&phba->hbalock);
7982 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7983 spin_unlock_irq(&phba->hbalock);
b4c02652 7984
3772a991 7985 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7986 spin_lock_irq(&phba->hbalock);
3772a991
JS
7987 if (phba->sli.mbox_active)
7988 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7989 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7990 /* Determine how long we might wait for the active mailbox
7991 * command to be gracefully completed by firmware.
7992 */
7993 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7994 1000) + jiffies;
7995 while (phba->sli.mbox_active) {
7996 /* Check active mailbox complete status every 2ms */
7997 msleep(2);
7998 if (time_after(jiffies, timeout))
7999 /* Timeout, let the mailbox flush routine to
8000 * forcefully release active mailbox command
8001 */
8002 break;
8003 }
8004 }
8005 lpfc_sli_mbox_sys_flush(phba);
8006}
ed957684 8007
3772a991
JS
8008/**
8009 * lpfc_sli_eratt_read - read sli-3 error attention events
8010 * @phba: Pointer to HBA context.
8011 *
8012 * This function is called to read the SLI3 device error attention registers
8013 * for possible error attention events. The caller must hold the hostlock
8014 * with spin_lock_irq().
8015 *
8016 * This fucntion returns 1 when there is Error Attention in the Host Attention
8017 * Register and returns 0 otherwise.
8018 **/
8019static int
8020lpfc_sli_eratt_read(struct lpfc_hba *phba)
8021{
8022 uint32_t ha_copy;
b4c02652 8023
3772a991
JS
8024 /* Read chip Host Attention (HA) register */
8025 ha_copy = readl(phba->HAregaddr);
8026 if (ha_copy & HA_ERATT) {
8027 /* Read host status register to retrieve error event */
8028 lpfc_sli_read_hs(phba);
b4c02652 8029
3772a991
JS
8030 /* Check if there is a deferred error condition is active */
8031 if ((HS_FFER1 & phba->work_hs) &&
8032 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 8033 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 8034 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
8035 /* Clear all interrupt enable conditions */
8036 writel(0, phba->HCregaddr);
8037 readl(phba->HCregaddr);
8038 }
8039
8040 /* Set the driver HA work bitmap */
3772a991
JS
8041 phba->work_ha |= HA_ERATT;
8042 /* Indicate polling handles this ERATT */
8043 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
8044 return 1;
8045 }
8046 return 0;
b4c02652
JS
8047}
8048
da0436e9
JS
8049/**
8050 * lpfc_sli4_eratt_read - read sli-4 error attention events
8051 * @phba: Pointer to HBA context.
8052 *
8053 * This function is called to read the SLI4 device error attention registers
8054 * for possible error attention events. The caller must hold the hostlock
8055 * with spin_lock_irq().
8056 *
8057 * This fucntion returns 1 when there is Error Attention in the Host Attention
8058 * Register and returns 0 otherwise.
8059 **/
8060static int
8061lpfc_sli4_eratt_read(struct lpfc_hba *phba)
8062{
8063 uint32_t uerr_sta_hi, uerr_sta_lo;
da0436e9
JS
8064
8065 /* For now, use the SLI4 device internal unrecoverable error
8066 * registers for error attention. This can be changed later.
8067 */
a747c9ce
JS
8068 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
8069 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
8070 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
8071 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
8072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8073 "1423 HBA Unrecoverable error: "
8074 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
8075 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
8076 uerr_sta_lo, uerr_sta_hi,
8077 phba->sli4_hba.ue_mask_lo,
8078 phba->sli4_hba.ue_mask_hi);
8079 phba->work_status[0] = uerr_sta_lo;
8080 phba->work_status[1] = uerr_sta_hi;
8081 /* Set the driver HA work bitmap */
8082 phba->work_ha |= HA_ERATT;
8083 /* Indicate polling handles this ERATT */
8084 phba->hba_flag |= HBA_ERATT_HANDLED;
8085 return 1;
da0436e9
JS
8086 }
8087 return 0;
8088}
8089
e59058c4 8090/**
3621a710 8091 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
8092 * @phba: Pointer to HBA context.
8093 *
3772a991 8094 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
8095 * error attention register bit for error attention events.
8096 *
8097 * This fucntion returns 1 when there is Error Attention in the Host Attention
8098 * Register and returns 0 otherwise.
8099 **/
8100int
8101lpfc_sli_check_eratt(struct lpfc_hba *phba)
8102{
8103 uint32_t ha_copy;
8104
8105 /* If somebody is waiting to handle an eratt, don't process it
8106 * here. The brdkill function will do this.
8107 */
8108 if (phba->link_flag & LS_IGNORE_ERATT)
8109 return 0;
8110
8111 /* Check if interrupt handler handles this ERATT */
8112 spin_lock_irq(&phba->hbalock);
8113 if (phba->hba_flag & HBA_ERATT_HANDLED) {
8114 /* Interrupt handler has handled ERATT */
8115 spin_unlock_irq(&phba->hbalock);
8116 return 0;
8117 }
8118
a257bf90
JS
8119 /*
8120 * If there is deferred error attention, do not check for error
8121 * attention
8122 */
8123 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8124 spin_unlock_irq(&phba->hbalock);
8125 return 0;
8126 }
8127
3772a991
JS
8128 /* If PCI channel is offline, don't process it */
8129 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 8130 spin_unlock_irq(&phba->hbalock);
3772a991
JS
8131 return 0;
8132 }
8133
8134 switch (phba->sli_rev) {
8135 case LPFC_SLI_REV2:
8136 case LPFC_SLI_REV3:
8137 /* Read chip Host Attention (HA) register */
8138 ha_copy = lpfc_sli_eratt_read(phba);
8139 break;
da0436e9
JS
8140 case LPFC_SLI_REV4:
8141 /* Read devcie Uncoverable Error (UERR) registers */
8142 ha_copy = lpfc_sli4_eratt_read(phba);
8143 break;
3772a991
JS
8144 default:
8145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8146 "0299 Invalid SLI revision (%d)\n",
8147 phba->sli_rev);
8148 ha_copy = 0;
8149 break;
9399627f
JS
8150 }
8151 spin_unlock_irq(&phba->hbalock);
3772a991
JS
8152
8153 return ha_copy;
8154}
8155
8156/**
8157 * lpfc_intr_state_check - Check device state for interrupt handling
8158 * @phba: Pointer to HBA context.
8159 *
8160 * This inline routine checks whether a device or its PCI slot is in a state
8161 * that the interrupt should be handled.
8162 *
8163 * This function returns 0 if the device or the PCI slot is in a state that
8164 * interrupt should be handled, otherwise -EIO.
8165 */
8166static inline int
8167lpfc_intr_state_check(struct lpfc_hba *phba)
8168{
8169 /* If the pci channel is offline, ignore all the interrupts */
8170 if (unlikely(pci_channel_offline(phba->pcidev)))
8171 return -EIO;
8172
8173 /* Update device level interrupt statistics */
8174 phba->sli.slistat.sli_intr++;
8175
8176 /* Ignore all interrupts during initialization. */
8177 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8178 return -EIO;
8179
9399627f
JS
8180 return 0;
8181}
8182
8183/**
3772a991 8184 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
8185 * @irq: Interrupt number.
8186 * @dev_id: The device context pointer.
8187 *
9399627f 8188 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8189 * service routine when device with SLI-3 interface spec is enabled with
8190 * MSI-X multi-message interrupt mode and there are slow-path events in
8191 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8192 * interrupt mode, this function is called as part of the device-level
8193 * interrupt handler. When the PCI slot is in error recovery or the HBA
8194 * is undergoing initialization, the interrupt handler will not process
8195 * the interrupt. The link attention and ELS ring attention events are
8196 * handled by the worker thread. The interrupt handler signals the worker
8197 * thread and returns for these events. This function is called without
8198 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
8199 * structures.
8200 *
8201 * This function returns IRQ_HANDLED when interrupt is handled else it
8202 * returns IRQ_NONE.
e59058c4 8203 **/
dea3101e 8204irqreturn_t
3772a991 8205lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 8206{
2e0fef85 8207 struct lpfc_hba *phba;
a747c9ce 8208 uint32_t ha_copy, hc_copy;
dea3101e
JB
8209 uint32_t work_ha_copy;
8210 unsigned long status;
5b75da2f 8211 unsigned long iflag;
dea3101e
JB
8212 uint32_t control;
8213
92d7f7b0 8214 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
8215 struct lpfc_vport *vport;
8216 struct lpfc_nodelist *ndlp;
8217 struct lpfc_dmabuf *mp;
92d7f7b0
JS
8218 LPFC_MBOXQ_t *pmb;
8219 int rc;
8220
dea3101e
JB
8221 /*
8222 * Get the driver's phba structure from the dev_id and
8223 * assume the HBA is not interrupting.
8224 */
9399627f 8225 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
8226
8227 if (unlikely(!phba))
8228 return IRQ_NONE;
8229
dea3101e 8230 /*
9399627f
JS
8231 * Stuff needs to be attented to when this function is invoked as an
8232 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 8233 */
9399627f 8234 if (phba->intr_type == MSIX) {
3772a991
JS
8235 /* Check device state for handling interrupt */
8236 if (lpfc_intr_state_check(phba))
9399627f
JS
8237 return IRQ_NONE;
8238 /* Need to read HA REG for slow-path events */
5b75da2f 8239 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 8240 ha_copy = readl(phba->HAregaddr);
9399627f
JS
8241 /* If somebody is waiting to handle an eratt don't process it
8242 * here. The brdkill function will do this.
8243 */
8244 if (phba->link_flag & LS_IGNORE_ERATT)
8245 ha_copy &= ~HA_ERATT;
8246 /* Check the need for handling ERATT in interrupt handler */
8247 if (ha_copy & HA_ERATT) {
8248 if (phba->hba_flag & HBA_ERATT_HANDLED)
8249 /* ERATT polling has handled ERATT */
8250 ha_copy &= ~HA_ERATT;
8251 else
8252 /* Indicate interrupt handler handles ERATT */
8253 phba->hba_flag |= HBA_ERATT_HANDLED;
8254 }
a257bf90
JS
8255
8256 /*
8257 * If there is deferred error attention, do not check for any
8258 * interrupt.
8259 */
8260 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8261 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8262 return IRQ_NONE;
8263 }
8264
9399627f 8265 /* Clear up only attention source related to slow-path */
a747c9ce
JS
8266 hc_copy = readl(phba->HCregaddr);
8267 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8268 HC_LAINT_ENA | HC_ERINT_ENA),
8269 phba->HCregaddr);
9399627f
JS
8270 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8271 phba->HAregaddr);
a747c9ce 8272 writel(hc_copy, phba->HCregaddr);
9399627f 8273 readl(phba->HAregaddr); /* flush */
5b75da2f 8274 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8275 } else
8276 ha_copy = phba->ha_copy;
dea3101e 8277
dea3101e
JB
8278 work_ha_copy = ha_copy & phba->work_ha_mask;
8279
9399627f 8280 if (work_ha_copy) {
dea3101e
JB
8281 if (work_ha_copy & HA_LATT) {
8282 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8283 /*
8284 * Turn off Link Attention interrupts
8285 * until CLEAR_LA done
8286 */
5b75da2f 8287 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
8288 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8289 control = readl(phba->HCregaddr);
8290 control &= ~HC_LAINT_ENA;
8291 writel(control, phba->HCregaddr);
8292 readl(phba->HCregaddr); /* flush */
5b75da2f 8293 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
8294 }
8295 else
8296 work_ha_copy &= ~HA_LATT;
8297 }
8298
9399627f 8299 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
8300 /*
8301 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8302 * the only slow ring.
8303 */
8304 status = (work_ha_copy &
8305 (HA_RXMASK << (4*LPFC_ELS_RING)));
8306 status >>= (4*LPFC_ELS_RING);
8307 if (status & HA_RXMASK) {
5b75da2f 8308 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 8309 control = readl(phba->HCregaddr);
a58cbd52
JS
8310
8311 lpfc_debugfs_slow_ring_trc(phba,
8312 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8313 control, status,
8314 (uint32_t)phba->sli.slistat.sli_intr);
8315
858c9f6c 8316 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
8317 lpfc_debugfs_slow_ring_trc(phba,
8318 "ISR Disable ring:"
8319 "pwork:x%x hawork:x%x wait:x%x",
8320 phba->work_ha, work_ha_copy,
8321 (uint32_t)((unsigned long)
5e9d9b82 8322 &phba->work_waitq));
a58cbd52 8323
858c9f6c
JS
8324 control &=
8325 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
8326 writel(control, phba->HCregaddr);
8327 readl(phba->HCregaddr); /* flush */
dea3101e 8328 }
a58cbd52
JS
8329 else {
8330 lpfc_debugfs_slow_ring_trc(phba,
8331 "ISR slow ring: pwork:"
8332 "x%x hawork:x%x wait:x%x",
8333 phba->work_ha, work_ha_copy,
8334 (uint32_t)((unsigned long)
5e9d9b82 8335 &phba->work_waitq));
a58cbd52 8336 }
5b75da2f 8337 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
8338 }
8339 }
5b75da2f 8340 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8341 if (work_ha_copy & HA_ERATT) {
9399627f 8342 lpfc_sli_read_hs(phba);
a257bf90
JS
8343 /*
8344 * Check if there is a deferred error condition
8345 * is active
8346 */
8347 if ((HS_FFER1 & phba->work_hs) &&
8348 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
8349 HS_FFER6 | HS_FFER7 | HS_FFER8) &
8350 phba->work_hs)) {
a257bf90
JS
8351 phba->hba_flag |= DEFER_ERATT;
8352 /* Clear all interrupt enable conditions */
8353 writel(0, phba->HCregaddr);
8354 readl(phba->HCregaddr);
8355 }
8356 }
8357
9399627f 8358 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8359 pmb = phba->sli.mbox_active;
04c68496 8360 pmbox = &pmb->u.mb;
34b02dcd 8361 mbox = phba->mbox;
858c9f6c 8362 vport = pmb->vport;
92d7f7b0
JS
8363
8364 /* First check out the status word */
8365 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8366 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8367 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8368 /*
8369 * Stray Mailbox Interrupt, mbxCommand <cmd>
8370 * mbxStatus <status>
8371 */
09372820 8372 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8373 LOG_SLI,
e8b62011 8374 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8375 "Interrupt mbxCommand x%x "
8376 "mbxStatus x%x\n",
e8b62011 8377 (vport ? vport->vpi : 0),
92d7f7b0
JS
8378 pmbox->mbxCommand,
8379 pmbox->mbxStatus);
09372820
JS
8380 /* clear mailbox attention bit */
8381 work_ha_copy &= ~HA_MBATT;
8382 } else {
97eab634 8383 phba->sli.mbox_active = NULL;
5b75da2f 8384 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8385 phba->last_completion_time = jiffies;
8386 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8387 if (pmb->mbox_cmpl) {
8388 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8389 MAILBOX_CMD_SIZE);
7a470277
JS
8390 if (pmb->out_ext_byte_len &&
8391 pmb->context2)
8392 lpfc_sli_pcimem_bcopy(
8393 phba->mbox_ext,
8394 pmb->context2,
8395 pmb->out_ext_byte_len);
09372820
JS
8396 }
8397 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8398 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8399
8400 lpfc_debugfs_disc_trc(vport,
8401 LPFC_DISC_TRC_MBOX_VPORT,
8402 "MBOX dflt rpi: : "
8403 "status:x%x rpi:x%x",
8404 (uint32_t)pmbox->mbxStatus,
8405 pmbox->un.varWords[0], 0);
8406
8407 if (!pmbox->mbxStatus) {
8408 mp = (struct lpfc_dmabuf *)
8409 (pmb->context1);
8410 ndlp = (struct lpfc_nodelist *)
8411 pmb->context2;
8412
8413 /* Reg_LOGIN of dflt RPI was
8414 * successful. new lets get
8415 * rid of the RPI using the
8416 * same mbox buffer.
8417 */
8418 lpfc_unreg_login(phba,
8419 vport->vpi,
8420 pmbox->un.varWords[0],
8421 pmb);
8422 pmb->mbox_cmpl =
8423 lpfc_mbx_cmpl_dflt_rpi;
8424 pmb->context1 = mp;
8425 pmb->context2 = ndlp;
8426 pmb->vport = vport;
58da1ffb
JS
8427 rc = lpfc_sli_issue_mbox(phba,
8428 pmb,
8429 MBX_NOWAIT);
8430 if (rc != MBX_BUSY)
8431 lpfc_printf_log(phba,
8432 KERN_ERR,
8433 LOG_MBOX | LOG_SLI,
d7c255b2 8434 "0350 rc should have"
6a9c52cf 8435 "been MBX_BUSY\n");
3772a991
JS
8436 if (rc != MBX_NOT_FINISHED)
8437 goto send_current_mbox;
09372820 8438 }
858c9f6c 8439 }
5b75da2f
JS
8440 spin_lock_irqsave(
8441 &phba->pport->work_port_lock,
8442 iflag);
09372820
JS
8443 phba->pport->work_port_events &=
8444 ~WORKER_MBOX_TMO;
5b75da2f
JS
8445 spin_unlock_irqrestore(
8446 &phba->pport->work_port_lock,
8447 iflag);
09372820 8448 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8449 }
97eab634 8450 } else
5b75da2f 8451 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8452
92d7f7b0
JS
8453 if ((work_ha_copy & HA_MBATT) &&
8454 (phba->sli.mbox_active == NULL)) {
858c9f6c 8455send_current_mbox:
92d7f7b0 8456 /* Process next mailbox command if there is one */
58da1ffb
JS
8457 do {
8458 rc = lpfc_sli_issue_mbox(phba, NULL,
8459 MBX_NOWAIT);
8460 } while (rc == MBX_NOT_FINISHED);
8461 if (rc != MBX_SUCCESS)
8462 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8463 LOG_SLI, "0349 rc should be "
6a9c52cf 8464 "MBX_SUCCESS\n");
92d7f7b0
JS
8465 }
8466
5b75da2f 8467 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8468 phba->work_ha |= work_ha_copy;
5b75da2f 8469 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8470 lpfc_worker_wake_up(phba);
dea3101e 8471 }
9399627f 8472 return IRQ_HANDLED;
dea3101e 8473
3772a991 8474} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8475
8476/**
3772a991 8477 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8478 * @irq: Interrupt number.
8479 * @dev_id: The device context pointer.
8480 *
8481 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8482 * service routine when device with SLI-3 interface spec is enabled with
8483 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8484 * ring event in the HBA. However, when the device is enabled with either
8485 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8486 * device-level interrupt handler. When the PCI slot is in error recovery
8487 * or the HBA is undergoing initialization, the interrupt handler will not
8488 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8489 * the intrrupt context. This function is called without any lock held.
8490 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8491 *
8492 * This function returns IRQ_HANDLED when interrupt is handled else it
8493 * returns IRQ_NONE.
8494 **/
8495irqreturn_t
3772a991 8496lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8497{
8498 struct lpfc_hba *phba;
8499 uint32_t ha_copy;
8500 unsigned long status;
5b75da2f 8501 unsigned long iflag;
9399627f
JS
8502
8503 /* Get the driver's phba structure from the dev_id and
8504 * assume the HBA is not interrupting.
8505 */
8506 phba = (struct lpfc_hba *) dev_id;
8507
8508 if (unlikely(!phba))
8509 return IRQ_NONE;
8510
8511 /*
8512 * Stuff needs to be attented to when this function is invoked as an
8513 * individual interrupt handler in MSI-X multi-message interrupt mode
8514 */
8515 if (phba->intr_type == MSIX) {
3772a991
JS
8516 /* Check device state for handling interrupt */
8517 if (lpfc_intr_state_check(phba))
9399627f
JS
8518 return IRQ_NONE;
8519 /* Need to read HA REG for FCP ring and other ring events */
8520 ha_copy = readl(phba->HAregaddr);
8521 /* Clear up only attention source related to fast-path */
5b75da2f 8522 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8523 /*
8524 * If there is deferred error attention, do not check for
8525 * any interrupt.
8526 */
8527 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8528 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8529 return IRQ_NONE;
8530 }
9399627f
JS
8531 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8532 phba->HAregaddr);
8533 readl(phba->HAregaddr); /* flush */
5b75da2f 8534 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8535 } else
8536 ha_copy = phba->ha_copy;
dea3101e
JB
8537
8538 /*
9399627f 8539 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8540 */
9399627f
JS
8541 ha_copy &= ~(phba->work_ha_mask);
8542
8543 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8544 status >>= (4*LPFC_FCP_RING);
858c9f6c 8545 if (status & HA_RXMASK)
dea3101e
JB
8546 lpfc_sli_handle_fast_ring_event(phba,
8547 &phba->sli.ring[LPFC_FCP_RING],
8548 status);
a4bc3379
JS
8549
8550 if (phba->cfg_multi_ring_support == 2) {
8551 /*
9399627f
JS
8552 * Process all events on extra ring. Take the optimized path
8553 * for extra ring IO.
a4bc3379 8554 */
9399627f 8555 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8556 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8557 if (status & HA_RXMASK) {
a4bc3379
JS
8558 lpfc_sli_handle_fast_ring_event(phba,
8559 &phba->sli.ring[LPFC_EXTRA_RING],
8560 status);
8561 }
8562 }
dea3101e 8563 return IRQ_HANDLED;
3772a991 8564} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8565
8566/**
3772a991 8567 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8568 * @irq: Interrupt number.
8569 * @dev_id: The device context pointer.
8570 *
3772a991
JS
8571 * This function is the HBA device-level interrupt handler to device with
8572 * SLI-3 interface spec, called from the PCI layer when either MSI or
8573 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8574 * requires driver attention. This function invokes the slow-path interrupt
8575 * attention handling function and fast-path interrupt attention handling
8576 * function in turn to process the relevant HBA attention events. This
8577 * function is called without any lock held. It gets the hbalock to access
8578 * and update SLI data structures.
9399627f
JS
8579 *
8580 * This function returns IRQ_HANDLED when interrupt is handled, else it
8581 * returns IRQ_NONE.
8582 **/
8583irqreturn_t
3772a991 8584lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8585{
8586 struct lpfc_hba *phba;
8587 irqreturn_t sp_irq_rc, fp_irq_rc;
8588 unsigned long status1, status2;
a747c9ce 8589 uint32_t hc_copy;
9399627f
JS
8590
8591 /*
8592 * Get the driver's phba structure from the dev_id and
8593 * assume the HBA is not interrupting.
8594 */
8595 phba = (struct lpfc_hba *) dev_id;
8596
8597 if (unlikely(!phba))
8598 return IRQ_NONE;
8599
3772a991
JS
8600 /* Check device state for handling interrupt */
8601 if (lpfc_intr_state_check(phba))
9399627f
JS
8602 return IRQ_NONE;
8603
8604 spin_lock(&phba->hbalock);
8605 phba->ha_copy = readl(phba->HAregaddr);
8606 if (unlikely(!phba->ha_copy)) {
8607 spin_unlock(&phba->hbalock);
8608 return IRQ_NONE;
8609 } else if (phba->ha_copy & HA_ERATT) {
8610 if (phba->hba_flag & HBA_ERATT_HANDLED)
8611 /* ERATT polling has handled ERATT */
8612 phba->ha_copy &= ~HA_ERATT;
8613 else
8614 /* Indicate interrupt handler handles ERATT */
8615 phba->hba_flag |= HBA_ERATT_HANDLED;
8616 }
8617
a257bf90
JS
8618 /*
8619 * If there is deferred error attention, do not check for any interrupt.
8620 */
8621 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 8622 spin_unlock(&phba->hbalock);
a257bf90
JS
8623 return IRQ_NONE;
8624 }
8625
9399627f 8626 /* Clear attention sources except link and error attentions */
a747c9ce
JS
8627 hc_copy = readl(phba->HCregaddr);
8628 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8629 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8630 phba->HCregaddr);
9399627f 8631 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 8632 writel(hc_copy, phba->HCregaddr);
9399627f
JS
8633 readl(phba->HAregaddr); /* flush */
8634 spin_unlock(&phba->hbalock);
8635
8636 /*
8637 * Invokes slow-path host attention interrupt handling as appropriate.
8638 */
8639
8640 /* status of events with mailbox and link attention */
8641 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8642
8643 /* status of events with ELS ring */
8644 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8645 status2 >>= (4*LPFC_ELS_RING);
8646
8647 if (status1 || (status2 & HA_RXMASK))
3772a991 8648 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8649 else
8650 sp_irq_rc = IRQ_NONE;
8651
8652 /*
8653 * Invoke fast-path host attention interrupt handling as appropriate.
8654 */
8655
8656 /* status of events with FCP ring */
8657 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8658 status1 >>= (4*LPFC_FCP_RING);
8659
8660 /* status of events with extra ring */
8661 if (phba->cfg_multi_ring_support == 2) {
8662 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8663 status2 >>= (4*LPFC_EXTRA_RING);
8664 } else
8665 status2 = 0;
8666
8667 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8668 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8669 else
8670 fp_irq_rc = IRQ_NONE;
dea3101e 8671
9399627f
JS
8672 /* Return device-level interrupt handling status */
8673 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8674} /* lpfc_sli_intr_handler */
4f774513
JS
8675
8676/**
8677 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8678 * @phba: pointer to lpfc hba data structure.
8679 *
8680 * This routine is invoked by the worker thread to process all the pending
8681 * SLI4 FCP abort XRI events.
8682 **/
8683void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8684{
8685 struct lpfc_cq_event *cq_event;
8686
8687 /* First, declare the fcp xri abort event has been handled */
8688 spin_lock_irq(&phba->hbalock);
8689 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8690 spin_unlock_irq(&phba->hbalock);
8691 /* Now, handle all the fcp xri abort events */
8692 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8693 /* Get the first event from the head of the event queue */
8694 spin_lock_irq(&phba->hbalock);
8695 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8696 cq_event, struct lpfc_cq_event, list);
8697 spin_unlock_irq(&phba->hbalock);
8698 /* Notify aborted XRI for FCP work queue */
8699 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8700 /* Free the event processed back to the free pool */
8701 lpfc_sli4_cq_event_release(phba, cq_event);
8702 }
8703}
8704
8705/**
8706 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8707 * @phba: pointer to lpfc hba data structure.
8708 *
8709 * This routine is invoked by the worker thread to process all the pending
8710 * SLI4 els abort xri events.
8711 **/
8712void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8713{
8714 struct lpfc_cq_event *cq_event;
8715
8716 /* First, declare the els xri abort event has been handled */
8717 spin_lock_irq(&phba->hbalock);
8718 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8719 spin_unlock_irq(&phba->hbalock);
8720 /* Now, handle all the els xri abort events */
8721 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8722 /* Get the first event from the head of the event queue */
8723 spin_lock_irq(&phba->hbalock);
8724 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8725 cq_event, struct lpfc_cq_event, list);
8726 spin_unlock_irq(&phba->hbalock);
8727 /* Notify aborted XRI for ELS work queue */
8728 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8729 /* Free the event processed back to the free pool */
8730 lpfc_sli4_cq_event_release(phba, cq_event);
8731 }
8732}
8733
341af102
JS
8734/**
8735 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8736 * @phba: pointer to lpfc hba data structure
8737 * @pIocbIn: pointer to the rspiocbq
8738 * @pIocbOut: pointer to the cmdiocbq
8739 * @wcqe: pointer to the complete wcqe
8740 *
8741 * This routine transfers the fields of a command iocbq to a response iocbq
8742 * by copying all the IOCB fields from command iocbq and transferring the
8743 * completion status information from the complete wcqe.
8744 **/
4f774513 8745static void
341af102
JS
8746lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8747 struct lpfc_iocbq *pIocbIn,
4f774513
JS
8748 struct lpfc_iocbq *pIocbOut,
8749 struct lpfc_wcqe_complete *wcqe)
8750{
341af102 8751 unsigned long iflags;
4f774513
JS
8752 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8753
8754 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8755 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
8756 /* Map WCQE parameters into irspiocb parameters */
8757 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8758 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8759 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8760 pIocbIn->iocb.un.fcpi.fcpi_parm =
8761 pIocbOut->iocb.un.fcpi.fcpi_parm -
8762 wcqe->total_data_placed;
8763 else
8764 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 8765 else {
4f774513 8766 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
8767 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8768 }
341af102
JS
8769
8770 /* Pick up HBA exchange busy condition */
8771 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8772 spin_lock_irqsave(&phba->hbalock, iflags);
8773 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8774 spin_unlock_irqrestore(&phba->hbalock, iflags);
8775 }
4f774513
JS
8776}
8777
45ed1190
JS
8778/**
8779 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8780 * @phba: Pointer to HBA context object.
8781 * @wcqe: Pointer to work-queue completion queue entry.
8782 *
8783 * This routine handles an ELS work-queue completion event and construct
8784 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8785 * discovery engine to handle.
8786 *
8787 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8788 **/
8789static struct lpfc_iocbq *
8790lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8791 struct lpfc_iocbq *irspiocbq)
8792{
8793 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8794 struct lpfc_iocbq *cmdiocbq;
8795 struct lpfc_wcqe_complete *wcqe;
8796 unsigned long iflags;
8797
8798 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8799 spin_lock_irqsave(&phba->hbalock, iflags);
8800 pring->stats.iocb_event++;
8801 /* Look up the ELS command IOCB and create pseudo response IOCB */
8802 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8803 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8804 spin_unlock_irqrestore(&phba->hbalock, iflags);
8805
8806 if (unlikely(!cmdiocbq)) {
8807 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8808 "0386 ELS complete with no corresponding "
8809 "cmdiocb: iotag (%d)\n",
8810 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8811 lpfc_sli_release_iocbq(phba, irspiocbq);
8812 return NULL;
8813 }
8814
8815 /* Fake the irspiocbq and copy necessary response information */
341af102 8816 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
8817
8818 return irspiocbq;
8819}
8820
04c68496
JS
8821/**
8822 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8823 * @phba: Pointer to HBA context object.
8824 * @cqe: Pointer to mailbox completion queue entry.
8825 *
8826 * This routine process a mailbox completion queue entry with asynchrous
8827 * event.
8828 *
8829 * Return: true if work posted to worker thread, otherwise false.
8830 **/
8831static bool
8832lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8833{
8834 struct lpfc_cq_event *cq_event;
8835 unsigned long iflags;
8836
8837 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8838 "0392 Async Event: word0:x%x, word1:x%x, "
8839 "word2:x%x, word3:x%x\n", mcqe->word0,
8840 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8841
8842 /* Allocate a new internal CQ_EVENT entry */
8843 cq_event = lpfc_sli4_cq_event_alloc(phba);
8844 if (!cq_event) {
8845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8846 "0394 Failed to allocate CQ_EVENT entry\n");
8847 return false;
8848 }
8849
8850 /* Move the CQE into an asynchronous event entry */
8851 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8852 spin_lock_irqsave(&phba->hbalock, iflags);
8853 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8854 /* Set the async event flag */
8855 phba->hba_flag |= ASYNC_EVENT;
8856 spin_unlock_irqrestore(&phba->hbalock, iflags);
8857
8858 return true;
8859}
8860
8861/**
8862 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8863 * @phba: Pointer to HBA context object.
8864 * @cqe: Pointer to mailbox completion queue entry.
8865 *
8866 * This routine process a mailbox completion queue entry with mailbox
8867 * completion event.
8868 *
8869 * Return: true if work posted to worker thread, otherwise false.
8870 **/
8871static bool
8872lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8873{
8874 uint32_t mcqe_status;
8875 MAILBOX_t *mbox, *pmbox;
8876 struct lpfc_mqe *mqe;
8877 struct lpfc_vport *vport;
8878 struct lpfc_nodelist *ndlp;
8879 struct lpfc_dmabuf *mp;
8880 unsigned long iflags;
8881 LPFC_MBOXQ_t *pmb;
8882 bool workposted = false;
8883 int rc;
8884
8885 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8886 if (!bf_get(lpfc_trailer_completed, mcqe))
8887 goto out_no_mqe_complete;
8888
8889 /* Get the reference to the active mbox command */
8890 spin_lock_irqsave(&phba->hbalock, iflags);
8891 pmb = phba->sli.mbox_active;
8892 if (unlikely(!pmb)) {
8893 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8894 "1832 No pending MBOX command to handle\n");
8895 spin_unlock_irqrestore(&phba->hbalock, iflags);
8896 goto out_no_mqe_complete;
8897 }
8898 spin_unlock_irqrestore(&phba->hbalock, iflags);
8899 mqe = &pmb->u.mqe;
8900 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8901 mbox = phba->mbox;
8902 vport = pmb->vport;
8903
8904 /* Reset heartbeat timer */
8905 phba->last_completion_time = jiffies;
8906 del_timer(&phba->sli.mbox_tmo);
8907
8908 /* Move mbox data to caller's mailbox region, do endian swapping */
8909 if (pmb->mbox_cmpl && mbox)
8910 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8911 /* Set the mailbox status with SLI4 range 0x4000 */
8912 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8913 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8914 bf_set(lpfc_mqe_status, mqe,
8915 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8916
8917 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8918 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8919 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8920 "MBOX dflt rpi: status:x%x rpi:x%x",
8921 mcqe_status,
8922 pmbox->un.varWords[0], 0);
8923 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8924 mp = (struct lpfc_dmabuf *)(pmb->context1);
8925 ndlp = (struct lpfc_nodelist *)pmb->context2;
8926 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8927 * RID of the PPI using the same mbox buffer.
8928 */
8929 lpfc_unreg_login(phba, vport->vpi,
8930 pmbox->un.varWords[0], pmb);
8931 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8932 pmb->context1 = mp;
8933 pmb->context2 = ndlp;
8934 pmb->vport = vport;
8935 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8936 if (rc != MBX_BUSY)
8937 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8938 LOG_SLI, "0385 rc should "
8939 "have been MBX_BUSY\n");
8940 if (rc != MBX_NOT_FINISHED)
8941 goto send_current_mbox;
8942 }
8943 }
8944 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8945 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8946 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8947
8948 /* There is mailbox completion work to do */
8949 spin_lock_irqsave(&phba->hbalock, iflags);
8950 __lpfc_mbox_cmpl_put(phba, pmb);
8951 phba->work_ha |= HA_MBATT;
8952 spin_unlock_irqrestore(&phba->hbalock, iflags);
8953 workposted = true;
8954
8955send_current_mbox:
8956 spin_lock_irqsave(&phba->hbalock, iflags);
8957 /* Release the mailbox command posting token */
8958 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8959 /* Setting active mailbox pointer need to be in sync to flag clear */
8960 phba->sli.mbox_active = NULL;
8961 spin_unlock_irqrestore(&phba->hbalock, iflags);
8962 /* Wake up worker thread to post the next pending mailbox command */
8963 lpfc_worker_wake_up(phba);
8964out_no_mqe_complete:
8965 if (bf_get(lpfc_trailer_consumed, mcqe))
8966 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8967 return workposted;
8968}
8969
8970/**
8971 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8972 * @phba: Pointer to HBA context object.
8973 * @cqe: Pointer to mailbox completion queue entry.
8974 *
8975 * This routine process a mailbox completion queue entry, it invokes the
8976 * proper mailbox complete handling or asynchrous event handling routine
8977 * according to the MCQE's async bit.
8978 *
8979 * Return: true if work posted to worker thread, otherwise false.
8980 **/
8981static bool
8982lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8983{
8984 struct lpfc_mcqe mcqe;
8985 bool workposted;
8986
8987 /* Copy the mailbox MCQE and convert endian order as needed */
8988 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8989
8990 /* Invoke the proper event handling routine */
8991 if (!bf_get(lpfc_trailer_async, &mcqe))
8992 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8993 else
8994 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8995 return workposted;
8996}
8997
4f774513
JS
8998/**
8999 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
9000 * @phba: Pointer to HBA context object.
9001 * @wcqe: Pointer to work-queue completion queue entry.
9002 *
9003 * This routine handles an ELS work-queue completion event.
9004 *
9005 * Return: true if work posted to worker thread, otherwise false.
9006 **/
9007static bool
9008lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
9009 struct lpfc_wcqe_complete *wcqe)
9010{
4f774513
JS
9011 struct lpfc_iocbq *irspiocbq;
9012 unsigned long iflags;
2a9bf3d0 9013 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
4f774513 9014
45ed1190 9015 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
9016 irspiocbq = lpfc_sli_get_iocbq(phba);
9017 if (!irspiocbq) {
9018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
9019 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
9020 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
9021 pring->txq_cnt, phba->iocb_cnt,
9022 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
9023 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
45ed1190 9024 return false;
4f774513 9025 }
4f774513 9026
45ed1190
JS
9027 /* Save off the slow-path queue event for work thread to process */
9028 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 9029 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 9030 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
9031 &phba->sli4_hba.sp_queue_event);
9032 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 9033 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 9034
45ed1190 9035 return true;
4f774513
JS
9036}
9037
9038/**
9039 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
9040 * @phba: Pointer to HBA context object.
9041 * @wcqe: Pointer to work-queue completion queue entry.
9042 *
9043 * This routine handles slow-path WQ entry comsumed event by invoking the
9044 * proper WQ release routine to the slow-path WQ.
9045 **/
9046static void
9047lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
9048 struct lpfc_wcqe_release *wcqe)
9049{
9050 /* Check for the slow-path ELS work queue */
9051 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
9052 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
9053 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9054 else
9055 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9056 "2579 Slow-path wqe consume event carries "
9057 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
9058 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
9059 phba->sli4_hba.els_wq->queue_id);
9060}
9061
9062/**
9063 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
9064 * @phba: Pointer to HBA context object.
9065 * @cq: Pointer to a WQ completion queue.
9066 * @wcqe: Pointer to work-queue completion queue entry.
9067 *
9068 * This routine handles an XRI abort event.
9069 *
9070 * Return: true if work posted to worker thread, otherwise false.
9071 **/
9072static bool
9073lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
9074 struct lpfc_queue *cq,
9075 struct sli4_wcqe_xri_aborted *wcqe)
9076{
9077 bool workposted = false;
9078 struct lpfc_cq_event *cq_event;
9079 unsigned long iflags;
9080
9081 /* Allocate a new internal CQ_EVENT entry */
9082 cq_event = lpfc_sli4_cq_event_alloc(phba);
9083 if (!cq_event) {
9084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9085 "0602 Failed to allocate CQ_EVENT entry\n");
9086 return false;
9087 }
9088
9089 /* Move the CQE into the proper xri abort event list */
9090 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
9091 switch (cq->subtype) {
9092 case LPFC_FCP:
9093 spin_lock_irqsave(&phba->hbalock, iflags);
9094 list_add_tail(&cq_event->list,
9095 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
9096 /* Set the fcp xri abort event flag */
9097 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
9098 spin_unlock_irqrestore(&phba->hbalock, iflags);
9099 workposted = true;
9100 break;
9101 case LPFC_ELS:
9102 spin_lock_irqsave(&phba->hbalock, iflags);
9103 list_add_tail(&cq_event->list,
9104 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
9105 /* Set the els xri abort event flag */
9106 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
9107 spin_unlock_irqrestore(&phba->hbalock, iflags);
9108 workposted = true;
9109 break;
9110 default:
9111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9112 "0603 Invalid work queue CQE subtype (x%x)\n",
9113 cq->subtype);
9114 workposted = false;
9115 break;
9116 }
9117 return workposted;
9118}
9119
4f774513
JS
9120/**
9121 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
9122 * @phba: Pointer to HBA context object.
9123 * @rcqe: Pointer to receive-queue completion queue entry.
9124 *
9125 * This routine process a receive-queue completion queue entry.
9126 *
9127 * Return: true if work posted to worker thread, otherwise false.
9128 **/
9129static bool
4d9ab994 9130lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 9131{
4f774513
JS
9132 bool workposted = false;
9133 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
9134 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
9135 struct hbq_dmabuf *dma_buf;
9136 uint32_t status;
9137 unsigned long iflags;
9138
4d9ab994 9139 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
9140 goto out;
9141
4d9ab994 9142 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
9143 switch (status) {
9144 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
9145 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9146 "2537 Receive Frame Truncated!!\n");
9147 case FC_STATUS_RQ_SUCCESS:
5ffc266e 9148 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
9149 spin_lock_irqsave(&phba->hbalock, iflags);
9150 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
9151 if (!dma_buf) {
9152 spin_unlock_irqrestore(&phba->hbalock, iflags);
9153 goto out;
9154 }
4d9ab994 9155 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 9156 /* save off the frame for the word thread to process */
4d9ab994 9157 list_add_tail(&dma_buf->cq_event.list,
45ed1190 9158 &phba->sli4_hba.sp_queue_event);
4f774513 9159 /* Frame received */
45ed1190 9160 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
9161 spin_unlock_irqrestore(&phba->hbalock, iflags);
9162 workposted = true;
9163 break;
9164 case FC_STATUS_INSUFF_BUF_NEED_BUF:
9165 case FC_STATUS_INSUFF_BUF_FRM_DISC:
9166 /* Post more buffers if possible */
9167 spin_lock_irqsave(&phba->hbalock, iflags);
9168 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
9169 spin_unlock_irqrestore(&phba->hbalock, iflags);
9170 workposted = true;
9171 break;
9172 }
9173out:
9174 return workposted;
4f774513
JS
9175}
9176
4d9ab994
JS
9177/**
9178 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
9179 * @phba: Pointer to HBA context object.
9180 * @cq: Pointer to the completion queue.
9181 * @wcqe: Pointer to a completion queue entry.
9182 *
9183 * This routine process a slow-path work-queue or recieve queue completion queue
9184 * entry.
9185 *
9186 * Return: true if work posted to worker thread, otherwise false.
9187 **/
9188static bool
9189lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9190 struct lpfc_cqe *cqe)
9191{
45ed1190 9192 struct lpfc_cqe cqevt;
4d9ab994
JS
9193 bool workposted = false;
9194
9195 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 9196 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
9197
9198 /* Check and process for different type of WCQE and dispatch */
45ed1190 9199 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 9200 case CQE_CODE_COMPL_WQE:
45ed1190 9201 /* Process the WQ/RQ complete event */
bc73905a 9202 phba->last_completion_time = jiffies;
4d9ab994 9203 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 9204 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
9205 break;
9206 case CQE_CODE_RELEASE_WQE:
9207 /* Process the WQ release event */
9208 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 9209 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
9210 break;
9211 case CQE_CODE_XRI_ABORTED:
9212 /* Process the WQ XRI abort event */
bc73905a 9213 phba->last_completion_time = jiffies;
4d9ab994 9214 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 9215 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
9216 break;
9217 case CQE_CODE_RECEIVE:
9218 /* Process the RQ event */
bc73905a 9219 phba->last_completion_time = jiffies;
4d9ab994 9220 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 9221 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
9222 break;
9223 default:
9224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9225 "0388 Not a valid WCQE code: x%x\n",
45ed1190 9226 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
9227 break;
9228 }
9229 return workposted;
9230}
9231
4f774513
JS
9232/**
9233 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9234 * @phba: Pointer to HBA context object.
9235 * @eqe: Pointer to fast-path event queue entry.
9236 *
9237 * This routine process a event queue entry from the slow-path event queue.
9238 * It will check the MajorCode and MinorCode to determine this is for a
9239 * completion event on a completion queue, if not, an error shall be logged
9240 * and just return. Otherwise, it will get to the corresponding completion
9241 * queue and process all the entries on that completion queue, rearm the
9242 * completion queue, and then return.
9243 *
9244 **/
9245static void
9246lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9247{
9248 struct lpfc_queue *cq = NULL, *childq, *speq;
9249 struct lpfc_cqe *cqe;
9250 bool workposted = false;
9251 int ecount = 0;
9252 uint16_t cqid;
9253
cb5172ea 9254 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
9255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9256 "0359 Not a valid slow-path completion "
9257 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9258 bf_get_le32(lpfc_eqe_major_code, eqe),
9259 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9260 return;
9261 }
9262
9263 /* Get the reference to the corresponding CQ */
cb5172ea 9264 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9265
9266 /* Search for completion queue pointer matching this cqid */
9267 speq = phba->sli4_hba.sp_eq;
9268 list_for_each_entry(childq, &speq->child_list, list) {
9269 if (childq->queue_id == cqid) {
9270 cq = childq;
9271 break;
9272 }
9273 }
9274 if (unlikely(!cq)) {
75baf696
JS
9275 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9277 "0365 Slow-path CQ identifier "
9278 "(%d) does not exist\n", cqid);
4f774513
JS
9279 return;
9280 }
9281
9282 /* Process all the entries to the CQ */
9283 switch (cq->type) {
9284 case LPFC_MCQ:
9285 while ((cqe = lpfc_sli4_cq_get(cq))) {
9286 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9287 if (!(++ecount % LPFC_GET_QE_REL_INT))
9288 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9289 }
9290 break;
9291 case LPFC_WCQ:
9292 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 9293 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
9294 if (!(++ecount % LPFC_GET_QE_REL_INT))
9295 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9296 }
9297 break;
9298 default:
9299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9300 "0370 Invalid completion queue type (%d)\n",
9301 cq->type);
9302 return;
9303 }
9304
9305 /* Catch the no cq entry condition, log an error */
9306 if (unlikely(ecount == 0))
9307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9308 "0371 No entry from the CQ: identifier "
9309 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9310
9311 /* In any case, flash and re-arm the RCQ */
9312 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9313
9314 /* wake up worker thread if there are works to be done */
9315 if (workposted)
9316 lpfc_worker_wake_up(phba);
9317}
9318
9319/**
9320 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9321 * @eqe: Pointer to fast-path completion queue entry.
9322 *
9323 * This routine process a fast-path work queue completion entry from fast-path
9324 * event queue for FCP command response completion.
9325 **/
9326static void
9327lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9328 struct lpfc_wcqe_complete *wcqe)
9329{
9330 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9331 struct lpfc_iocbq *cmdiocbq;
9332 struct lpfc_iocbq irspiocbq;
9333 unsigned long iflags;
9334
9335 spin_lock_irqsave(&phba->hbalock, iflags);
9336 pring->stats.iocb_event++;
9337 spin_unlock_irqrestore(&phba->hbalock, iflags);
9338
9339 /* Check for response status */
9340 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9341 /* If resource errors reported from HBA, reduce queue
9342 * depth of the SCSI device.
9343 */
9344 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9345 IOSTAT_LOCAL_REJECT) &&
9346 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9347 phba->lpfc_rampdown_queue_depth(phba);
9348 }
9349 /* Log the error status */
9350 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9351 "0373 FCP complete error: status=x%x, "
9352 "hw_status=x%x, total_data_specified=%d, "
9353 "parameter=x%x, word3=x%x\n",
9354 bf_get(lpfc_wcqe_c_status, wcqe),
9355 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9356 wcqe->total_data_placed, wcqe->parameter,
9357 wcqe->word3);
9358 }
9359
9360 /* Look up the FCP command IOCB and create pseudo response IOCB */
9361 spin_lock_irqsave(&phba->hbalock, iflags);
9362 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9363 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9364 spin_unlock_irqrestore(&phba->hbalock, iflags);
9365 if (unlikely(!cmdiocbq)) {
9366 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9367 "0374 FCP complete with no corresponding "
9368 "cmdiocb: iotag (%d)\n",
9369 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9370 return;
9371 }
9372 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9373 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9374 "0375 FCP cmdiocb not callback function "
9375 "iotag: (%d)\n",
9376 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9377 return;
9378 }
9379
9380 /* Fake the irspiocb and copy necessary response information */
341af102 9381 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 9382
0f65ff68
JS
9383 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9384 spin_lock_irqsave(&phba->hbalock, iflags);
9385 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9386 spin_unlock_irqrestore(&phba->hbalock, iflags);
9387 }
9388
4f774513
JS
9389 /* Pass the cmd_iocb and the rsp state to the upper layer */
9390 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9391}
9392
9393/**
9394 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9395 * @phba: Pointer to HBA context object.
9396 * @cq: Pointer to completion queue.
9397 * @wcqe: Pointer to work-queue completion queue entry.
9398 *
9399 * This routine handles an fast-path WQ entry comsumed event by invoking the
9400 * proper WQ release routine to the slow-path WQ.
9401 **/
9402static void
9403lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9404 struct lpfc_wcqe_release *wcqe)
9405{
9406 struct lpfc_queue *childwq;
9407 bool wqid_matched = false;
9408 uint16_t fcp_wqid;
9409
9410 /* Check for fast-path FCP work queue release */
9411 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9412 list_for_each_entry(childwq, &cq->child_list, list) {
9413 if (childwq->queue_id == fcp_wqid) {
9414 lpfc_sli4_wq_release(childwq,
9415 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9416 wqid_matched = true;
9417 break;
9418 }
9419 }
9420 /* Report warning log message if no match found */
9421 if (wqid_matched != true)
9422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9423 "2580 Fast-path wqe consume event carries "
9424 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9425}
9426
9427/**
9428 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9429 * @cq: Pointer to the completion queue.
9430 * @eqe: Pointer to fast-path completion queue entry.
9431 *
9432 * This routine process a fast-path work queue completion entry from fast-path
9433 * event queue for FCP command response completion.
9434 **/
9435static int
9436lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9437 struct lpfc_cqe *cqe)
9438{
9439 struct lpfc_wcqe_release wcqe;
9440 bool workposted = false;
9441
9442 /* Copy the work queue CQE and convert endian order if needed */
9443 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9444
9445 /* Check and process for different type of WCQE and dispatch */
9446 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9447 case CQE_CODE_COMPL_WQE:
9448 /* Process the WQ complete event */
98fc5dd9 9449 phba->last_completion_time = jiffies;
4f774513
JS
9450 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9451 (struct lpfc_wcqe_complete *)&wcqe);
9452 break;
9453 case CQE_CODE_RELEASE_WQE:
9454 /* Process the WQ release event */
9455 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9456 (struct lpfc_wcqe_release *)&wcqe);
9457 break;
9458 case CQE_CODE_XRI_ABORTED:
9459 /* Process the WQ XRI abort event */
bc73905a 9460 phba->last_completion_time = jiffies;
4f774513
JS
9461 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9462 (struct sli4_wcqe_xri_aborted *)&wcqe);
9463 break;
9464 default:
9465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9466 "0144 Not a valid WCQE code: x%x\n",
9467 bf_get(lpfc_wcqe_c_code, &wcqe));
9468 break;
9469 }
9470 return workposted;
9471}
9472
9473/**
9474 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9475 * @phba: Pointer to HBA context object.
9476 * @eqe: Pointer to fast-path event queue entry.
9477 *
9478 * This routine process a event queue entry from the fast-path event queue.
9479 * It will check the MajorCode and MinorCode to determine this is for a
9480 * completion event on a completion queue, if not, an error shall be logged
9481 * and just return. Otherwise, it will get to the corresponding completion
9482 * queue and process all the entries on the completion queue, rearm the
9483 * completion queue, and then return.
9484 **/
9485static void
9486lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9487 uint32_t fcp_cqidx)
9488{
9489 struct lpfc_queue *cq;
9490 struct lpfc_cqe *cqe;
9491 bool workposted = false;
9492 uint16_t cqid;
9493 int ecount = 0;
9494
cb5172ea 9495 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
9496 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9497 "0366 Not a valid fast-path completion "
9498 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9499 bf_get_le32(lpfc_eqe_major_code, eqe),
9500 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9501 return;
9502 }
9503
9504 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9505 if (unlikely(!cq)) {
75baf696
JS
9506 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
9507 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9508 "0367 Fast-path completion queue "
9509 "does not exist\n");
4f774513
JS
9510 return;
9511 }
9512
9513 /* Get the reference to the corresponding CQ */
cb5172ea 9514 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9515 if (unlikely(cqid != cq->queue_id)) {
9516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9517 "0368 Miss-matched fast-path completion "
9518 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9519 cqid, cq->queue_id);
9520 return;
9521 }
9522
9523 /* Process all the entries to the CQ */
9524 while ((cqe = lpfc_sli4_cq_get(cq))) {
9525 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9526 if (!(++ecount % LPFC_GET_QE_REL_INT))
9527 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9528 }
9529
9530 /* Catch the no cq entry condition */
9531 if (unlikely(ecount == 0))
9532 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9533 "0369 No entry from fast-path completion "
9534 "queue fcpcqid=%d\n", cq->queue_id);
9535
9536 /* In any case, flash and re-arm the CQ */
9537 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9538
9539 /* wake up worker thread if there are works to be done */
9540 if (workposted)
9541 lpfc_worker_wake_up(phba);
9542}
9543
9544static void
9545lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9546{
9547 struct lpfc_eqe *eqe;
9548
9549 /* walk all the EQ entries and drop on the floor */
9550 while ((eqe = lpfc_sli4_eq_get(eq)))
9551 ;
9552
9553 /* Clear and re-arm the EQ */
9554 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9555}
9556
9557/**
9558 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9559 * @irq: Interrupt number.
9560 * @dev_id: The device context pointer.
9561 *
9562 * This function is directly called from the PCI layer as an interrupt
9563 * service routine when device with SLI-4 interface spec is enabled with
9564 * MSI-X multi-message interrupt mode and there are slow-path events in
9565 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9566 * interrupt mode, this function is called as part of the device-level
9567 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9568 * undergoing initialization, the interrupt handler will not process the
9569 * interrupt. The link attention and ELS ring attention events are handled
9570 * by the worker thread. The interrupt handler signals the worker thread
9571 * and returns for these events. This function is called without any lock
9572 * held. It gets the hbalock to access and update SLI data structures.
9573 *
9574 * This function returns IRQ_HANDLED when interrupt is handled else it
9575 * returns IRQ_NONE.
9576 **/
9577irqreturn_t
9578lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9579{
9580 struct lpfc_hba *phba;
9581 struct lpfc_queue *speq;
9582 struct lpfc_eqe *eqe;
9583 unsigned long iflag;
9584 int ecount = 0;
9585
9586 /*
9587 * Get the driver's phba structure from the dev_id
9588 */
9589 phba = (struct lpfc_hba *)dev_id;
9590
9591 if (unlikely(!phba))
9592 return IRQ_NONE;
9593
9594 /* Get to the EQ struct associated with this vector */
9595 speq = phba->sli4_hba.sp_eq;
9596
9597 /* Check device state for handling interrupt */
9598 if (unlikely(lpfc_intr_state_check(phba))) {
9599 /* Check again for link_state with lock held */
9600 spin_lock_irqsave(&phba->hbalock, iflag);
9601 if (phba->link_state < LPFC_LINK_DOWN)
9602 /* Flush, clear interrupt, and rearm the EQ */
9603 lpfc_sli4_eq_flush(phba, speq);
9604 spin_unlock_irqrestore(&phba->hbalock, iflag);
9605 return IRQ_NONE;
9606 }
9607
9608 /*
9609 * Process all the event on FCP slow-path EQ
9610 */
9611 while ((eqe = lpfc_sli4_eq_get(speq))) {
9612 lpfc_sli4_sp_handle_eqe(phba, eqe);
9613 if (!(++ecount % LPFC_GET_QE_REL_INT))
9614 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9615 }
9616
9617 /* Always clear and re-arm the slow-path EQ */
9618 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9619
9620 /* Catch the no cq entry condition */
9621 if (unlikely(ecount == 0)) {
9622 if (phba->intr_type == MSIX)
9623 /* MSI-X treated interrupt served as no EQ share INT */
9624 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9625 "0357 MSI-X interrupt with no EQE\n");
9626 else
9627 /* Non MSI-X treated on interrupt as EQ share INT */
9628 return IRQ_NONE;
9629 }
9630
9631 return IRQ_HANDLED;
9632} /* lpfc_sli4_sp_intr_handler */
9633
9634/**
9635 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9636 * @irq: Interrupt number.
9637 * @dev_id: The device context pointer.
9638 *
9639 * This function is directly called from the PCI layer as an interrupt
9640 * service routine when device with SLI-4 interface spec is enabled with
9641 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9642 * ring event in the HBA. However, when the device is enabled with either
9643 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9644 * device-level interrupt handler. When the PCI slot is in error recovery
9645 * or the HBA is undergoing initialization, the interrupt handler will not
9646 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9647 * the intrrupt context. This function is called without any lock held.
9648 * It gets the hbalock to access and update SLI data structures. Note that,
9649 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9650 * equal to that of FCP CQ index.
9651 *
9652 * This function returns IRQ_HANDLED when interrupt is handled else it
9653 * returns IRQ_NONE.
9654 **/
9655irqreturn_t
9656lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9657{
9658 struct lpfc_hba *phba;
9659 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9660 struct lpfc_queue *fpeq;
9661 struct lpfc_eqe *eqe;
9662 unsigned long iflag;
9663 int ecount = 0;
9664 uint32_t fcp_eqidx;
9665
9666 /* Get the driver's phba structure from the dev_id */
9667 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9668 phba = fcp_eq_hdl->phba;
9669 fcp_eqidx = fcp_eq_hdl->idx;
9670
9671 if (unlikely(!phba))
9672 return IRQ_NONE;
9673
9674 /* Get to the EQ struct associated with this vector */
9675 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9676
9677 /* Check device state for handling interrupt */
9678 if (unlikely(lpfc_intr_state_check(phba))) {
9679 /* Check again for link_state with lock held */
9680 spin_lock_irqsave(&phba->hbalock, iflag);
9681 if (phba->link_state < LPFC_LINK_DOWN)
9682 /* Flush, clear interrupt, and rearm the EQ */
9683 lpfc_sli4_eq_flush(phba, fpeq);
9684 spin_unlock_irqrestore(&phba->hbalock, iflag);
9685 return IRQ_NONE;
9686 }
9687
9688 /*
9689 * Process all the event on FCP fast-path EQ
9690 */
9691 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9692 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9693 if (!(++ecount % LPFC_GET_QE_REL_INT))
9694 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9695 }
9696
9697 /* Always clear and re-arm the fast-path EQ */
9698 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9699
9700 if (unlikely(ecount == 0)) {
9701 if (phba->intr_type == MSIX)
9702 /* MSI-X treated interrupt served as no EQ share INT */
9703 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9704 "0358 MSI-X interrupt with no EQE\n");
9705 else
9706 /* Non MSI-X treated on interrupt as EQ share INT */
9707 return IRQ_NONE;
9708 }
9709
9710 return IRQ_HANDLED;
9711} /* lpfc_sli4_fp_intr_handler */
9712
9713/**
9714 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9715 * @irq: Interrupt number.
9716 * @dev_id: The device context pointer.
9717 *
9718 * This function is the device-level interrupt handler to device with SLI-4
9719 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9720 * interrupt mode is enabled and there is an event in the HBA which requires
9721 * driver attention. This function invokes the slow-path interrupt attention
9722 * handling function and fast-path interrupt attention handling function in
9723 * turn to process the relevant HBA attention events. This function is called
9724 * without any lock held. It gets the hbalock to access and update SLI data
9725 * structures.
9726 *
9727 * This function returns IRQ_HANDLED when interrupt is handled, else it
9728 * returns IRQ_NONE.
9729 **/
9730irqreturn_t
9731lpfc_sli4_intr_handler(int irq, void *dev_id)
9732{
9733 struct lpfc_hba *phba;
9734 irqreturn_t sp_irq_rc, fp_irq_rc;
9735 bool fp_handled = false;
9736 uint32_t fcp_eqidx;
9737
9738 /* Get the driver's phba structure from the dev_id */
9739 phba = (struct lpfc_hba *)dev_id;
9740
9741 if (unlikely(!phba))
9742 return IRQ_NONE;
9743
9744 /*
9745 * Invokes slow-path host attention interrupt handling as appropriate.
9746 */
9747 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9748
9749 /*
9750 * Invoke fast-path host attention interrupt handling as appropriate.
9751 */
9752 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9753 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9754 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9755 if (fp_irq_rc == IRQ_HANDLED)
9756 fp_handled |= true;
9757 }
9758
9759 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9760} /* lpfc_sli4_intr_handler */
9761
9762/**
9763 * lpfc_sli4_queue_free - free a queue structure and associated memory
9764 * @queue: The queue structure to free.
9765 *
9766 * This function frees a queue structure and the DMAable memeory used for
9767 * the host resident queue. This function must be called after destroying the
9768 * queue on the HBA.
9769 **/
9770void
9771lpfc_sli4_queue_free(struct lpfc_queue *queue)
9772{
9773 struct lpfc_dmabuf *dmabuf;
9774
9775 if (!queue)
9776 return;
9777
9778 while (!list_empty(&queue->page_list)) {
9779 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9780 list);
49198b37 9781 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
9782 dmabuf->virt, dmabuf->phys);
9783 kfree(dmabuf);
9784 }
9785 kfree(queue);
9786 return;
9787}
9788
9789/**
9790 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9791 * @phba: The HBA that this queue is being created on.
9792 * @entry_size: The size of each queue entry for this queue.
9793 * @entry count: The number of entries that this queue will handle.
9794 *
9795 * This function allocates a queue structure and the DMAable memory used for
9796 * the host resident queue. This function must be called before creating the
9797 * queue on the HBA.
9798 **/
9799struct lpfc_queue *
9800lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9801 uint32_t entry_count)
9802{
9803 struct lpfc_queue *queue;
9804 struct lpfc_dmabuf *dmabuf;
9805 int x, total_qe_count;
9806 void *dma_pointer;
cb5172ea 9807 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 9808
cb5172ea
JS
9809 if (!phba->sli4_hba.pc_sli4_params.supported)
9810 hw_page_size = SLI4_PAGE_SIZE;
9811
4f774513
JS
9812 queue = kzalloc(sizeof(struct lpfc_queue) +
9813 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9814 if (!queue)
9815 return NULL;
cb5172ea
JS
9816 queue->page_count = (ALIGN(entry_size * entry_count,
9817 hw_page_size))/hw_page_size;
4f774513
JS
9818 INIT_LIST_HEAD(&queue->list);
9819 INIT_LIST_HEAD(&queue->page_list);
9820 INIT_LIST_HEAD(&queue->child_list);
9821 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9822 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9823 if (!dmabuf)
9824 goto out_fail;
9825 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 9826 hw_page_size, &dmabuf->phys,
4f774513
JS
9827 GFP_KERNEL);
9828 if (!dmabuf->virt) {
9829 kfree(dmabuf);
9830 goto out_fail;
9831 }
cb5172ea 9832 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9833 dmabuf->buffer_tag = x;
9834 list_add_tail(&dmabuf->list, &queue->page_list);
9835 /* initialize queue's entry array */
9836 dma_pointer = dmabuf->virt;
9837 for (; total_qe_count < entry_count &&
cb5172ea 9838 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
9839 total_qe_count++, dma_pointer += entry_size) {
9840 queue->qe[total_qe_count].address = dma_pointer;
9841 }
9842 }
9843 queue->entry_size = entry_size;
9844 queue->entry_count = entry_count;
9845 queue->phba = phba;
9846
9847 return queue;
9848out_fail:
9849 lpfc_sli4_queue_free(queue);
9850 return NULL;
9851}
9852
9853/**
9854 * lpfc_eq_create - Create an Event Queue on the HBA
9855 * @phba: HBA structure that indicates port to create a queue on.
9856 * @eq: The queue structure to use to create the event queue.
9857 * @imax: The maximum interrupt per second limit.
9858 *
9859 * This function creates an event queue, as detailed in @eq, on a port,
9860 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9861 *
9862 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9863 * is used to get the entry count and entry size that are necessary to
9864 * determine the number of pages to allocate and use for this queue. This
9865 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9866 * event queue. This function is asynchronous and will wait for the mailbox
9867 * command to finish before continuing.
9868 *
9869 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
9870 * memory this function will return -ENOMEM. If the queue create mailbox command
9871 * fails this function will return -ENXIO.
4f774513
JS
9872 **/
9873uint32_t
9874lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9875{
9876 struct lpfc_mbx_eq_create *eq_create;
9877 LPFC_MBOXQ_t *mbox;
9878 int rc, length, status = 0;
9879 struct lpfc_dmabuf *dmabuf;
9880 uint32_t shdr_status, shdr_add_status;
9881 union lpfc_sli4_cfg_shdr *shdr;
9882 uint16_t dmult;
49198b37
JS
9883 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
9884
9885 if (!phba->sli4_hba.pc_sli4_params.supported)
9886 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
9887
9888 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9889 if (!mbox)
9890 return -ENOMEM;
9891 length = (sizeof(struct lpfc_mbx_eq_create) -
9892 sizeof(struct lpfc_sli4_cfg_mhdr));
9893 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9894 LPFC_MBOX_OPCODE_EQ_CREATE,
9895 length, LPFC_SLI4_MBX_EMBED);
9896 eq_create = &mbox->u.mqe.un.eq_create;
9897 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9898 eq->page_count);
9899 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9900 LPFC_EQE_SIZE);
9901 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9902 /* Calculate delay multiper from maximum interrupt per second */
9903 dmult = LPFC_DMULT_CONST/imax - 1;
9904 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9905 dmult);
9906 switch (eq->entry_count) {
9907 default:
9908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9909 "0360 Unsupported EQ count. (%d)\n",
9910 eq->entry_count);
9911 if (eq->entry_count < 256)
9912 return -EINVAL;
9913 /* otherwise default to smallest count (drop through) */
9914 case 256:
9915 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9916 LPFC_EQ_CNT_256);
9917 break;
9918 case 512:
9919 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9920 LPFC_EQ_CNT_512);
9921 break;
9922 case 1024:
9923 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9924 LPFC_EQ_CNT_1024);
9925 break;
9926 case 2048:
9927 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9928 LPFC_EQ_CNT_2048);
9929 break;
9930 case 4096:
9931 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9932 LPFC_EQ_CNT_4096);
9933 break;
9934 }
9935 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 9936 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9937 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9938 putPaddrLow(dmabuf->phys);
9939 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9940 putPaddrHigh(dmabuf->phys);
9941 }
9942 mbox->vport = phba->pport;
9943 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9944 mbox->context1 = NULL;
9945 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9946 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9947 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9948 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9949 if (shdr_status || shdr_add_status || rc) {
9950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9951 "2500 EQ_CREATE mailbox failed with "
9952 "status x%x add_status x%x, mbx status x%x\n",
9953 shdr_status, shdr_add_status, rc);
9954 status = -ENXIO;
9955 }
9956 eq->type = LPFC_EQ;
9957 eq->subtype = LPFC_NONE;
9958 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9959 if (eq->queue_id == 0xFFFF)
9960 status = -ENXIO;
9961 eq->host_index = 0;
9962 eq->hba_index = 0;
9963
8fa38513 9964 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9965 return status;
9966}
9967
9968/**
9969 * lpfc_cq_create - Create a Completion Queue on the HBA
9970 * @phba: HBA structure that indicates port to create a queue on.
9971 * @cq: The queue structure to use to create the completion queue.
9972 * @eq: The event queue to bind this completion queue to.
9973 *
9974 * This function creates a completion queue, as detailed in @wq, on a port,
9975 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9976 *
9977 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9978 * is used to get the entry count and entry size that are necessary to
9979 * determine the number of pages to allocate and use for this queue. The @eq
9980 * is used to indicate which event queue to bind this completion queue to. This
9981 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9982 * completion queue. This function is asynchronous and will wait for the mailbox
9983 * command to finish before continuing.
9984 *
9985 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
9986 * memory this function will return -ENOMEM. If the queue create mailbox command
9987 * fails this function will return -ENXIO.
4f774513
JS
9988 **/
9989uint32_t
9990lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9991 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9992{
9993 struct lpfc_mbx_cq_create *cq_create;
9994 struct lpfc_dmabuf *dmabuf;
9995 LPFC_MBOXQ_t *mbox;
9996 int rc, length, status = 0;
9997 uint32_t shdr_status, shdr_add_status;
9998 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
9999 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10000
10001 if (!phba->sli4_hba.pc_sli4_params.supported)
10002 hw_page_size = SLI4_PAGE_SIZE;
10003
4f774513
JS
10004
10005 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10006 if (!mbox)
10007 return -ENOMEM;
10008 length = (sizeof(struct lpfc_mbx_cq_create) -
10009 sizeof(struct lpfc_sli4_cfg_mhdr));
10010 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10011 LPFC_MBOX_OPCODE_CQ_CREATE,
10012 length, LPFC_SLI4_MBX_EMBED);
10013 cq_create = &mbox->u.mqe.un.cq_create;
10014 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
10015 cq->page_count);
10016 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
10017 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
10018 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
10019 switch (cq->entry_count) {
10020 default:
10021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10022 "0361 Unsupported CQ count. (%d)\n",
10023 cq->entry_count);
10024 if (cq->entry_count < 256)
10025 return -EINVAL;
10026 /* otherwise default to smallest count (drop through) */
10027 case 256:
10028 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10029 LPFC_CQ_CNT_256);
10030 break;
10031 case 512:
10032 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10033 LPFC_CQ_CNT_512);
10034 break;
10035 case 1024:
10036 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
10037 LPFC_CQ_CNT_1024);
10038 break;
10039 }
10040 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 10041 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
10042 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10043 putPaddrLow(dmabuf->phys);
10044 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10045 putPaddrHigh(dmabuf->phys);
10046 }
10047 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10048
10049 /* The IOCTL status is embedded in the mailbox subheader. */
10050 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
10051 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10052 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10053 if (shdr_status || shdr_add_status || rc) {
10054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10055 "2501 CQ_CREATE mailbox failed with "
10056 "status x%x add_status x%x, mbx status x%x\n",
10057 shdr_status, shdr_add_status, rc);
10058 status = -ENXIO;
10059 goto out;
10060 }
10061 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10062 if (cq->queue_id == 0xFFFF) {
10063 status = -ENXIO;
10064 goto out;
10065 }
10066 /* link the cq onto the parent eq child list */
10067 list_add_tail(&cq->list, &eq->child_list);
10068 /* Set up completion queue's type and subtype */
10069 cq->type = type;
10070 cq->subtype = subtype;
10071 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
10072 cq->host_index = 0;
10073 cq->hba_index = 0;
4f774513 10074
8fa38513
JS
10075out:
10076 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10077 return status;
10078}
10079
b19a061a
JS
10080/**
10081 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
10082 * @phba: HBA structure that indicates port to create a queue on.
10083 * @mq: The queue structure to use to create the mailbox queue.
10084 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
10085 * @cq: The completion queue to associate with this cq.
10086 *
10087 * This function provides failback (fb) functionality when the
10088 * mq_create_ext fails on older FW generations. It's purpose is identical
10089 * to mq_create_ext otherwise.
10090 *
10091 * This routine cannot fail as all attributes were previously accessed and
10092 * initialized in mq_create_ext.
10093 **/
10094static void
10095lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
10096 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
10097{
10098 struct lpfc_mbx_mq_create *mq_create;
10099 struct lpfc_dmabuf *dmabuf;
10100 int length;
10101
10102 length = (sizeof(struct lpfc_mbx_mq_create) -
10103 sizeof(struct lpfc_sli4_cfg_mhdr));
10104 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10105 LPFC_MBOX_OPCODE_MQ_CREATE,
10106 length, LPFC_SLI4_MBX_EMBED);
10107 mq_create = &mbox->u.mqe.un.mq_create;
10108 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
10109 mq->page_count);
10110 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
10111 cq->queue_id);
10112 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
10113 switch (mq->entry_count) {
10114 case 16:
10115 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10116 LPFC_MQ_CNT_16);
10117 break;
10118 case 32:
10119 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10120 LPFC_MQ_CNT_32);
10121 break;
10122 case 64:
10123 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10124 LPFC_MQ_CNT_64);
10125 break;
10126 case 128:
10127 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
10128 LPFC_MQ_CNT_128);
10129 break;
10130 }
10131 list_for_each_entry(dmabuf, &mq->page_list, list) {
10132 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10133 putPaddrLow(dmabuf->phys);
10134 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10135 putPaddrHigh(dmabuf->phys);
10136 }
10137}
10138
04c68496
JS
10139/**
10140 * lpfc_mq_create - Create a mailbox Queue on the HBA
10141 * @phba: HBA structure that indicates port to create a queue on.
10142 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
10143 * @cq: The completion queue to associate with this cq.
10144 * @subtype: The queue's subtype.
04c68496
JS
10145 *
10146 * This function creates a mailbox queue, as detailed in @mq, on a port,
10147 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
10148 *
10149 * The @phba struct is used to send mailbox command to HBA. The @cq struct
10150 * is used to get the entry count and entry size that are necessary to
10151 * determine the number of pages to allocate and use for this queue. This
10152 * function will send the MQ_CREATE mailbox command to the HBA to setup the
10153 * mailbox queue. This function is asynchronous and will wait for the mailbox
10154 * command to finish before continuing.
10155 *
10156 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10157 * memory this function will return -ENOMEM. If the queue create mailbox command
10158 * fails this function will return -ENXIO.
04c68496 10159 **/
b19a061a 10160int32_t
04c68496
JS
10161lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
10162 struct lpfc_queue *cq, uint32_t subtype)
10163{
10164 struct lpfc_mbx_mq_create *mq_create;
b19a061a 10165 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
10166 struct lpfc_dmabuf *dmabuf;
10167 LPFC_MBOXQ_t *mbox;
10168 int rc, length, status = 0;
10169 uint32_t shdr_status, shdr_add_status;
10170 union lpfc_sli4_cfg_shdr *shdr;
49198b37 10171 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 10172
49198b37
JS
10173 if (!phba->sli4_hba.pc_sli4_params.supported)
10174 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 10175
04c68496
JS
10176 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10177 if (!mbox)
10178 return -ENOMEM;
b19a061a 10179 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
10180 sizeof(struct lpfc_sli4_cfg_mhdr));
10181 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 10182 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 10183 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
10184
10185 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
10186 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
04c68496 10187 mq->page_count);
b19a061a
JS
10188 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
10189 1);
10190 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
10191 &mq_create_ext->u.request, 1);
10192 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
10193 &mq_create_ext->u.request, 1);
10194 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
10195 cq->queue_id);
10196 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
04c68496
JS
10197 switch (mq->entry_count) {
10198 default:
10199 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10200 "0362 Unsupported MQ count. (%d)\n",
10201 mq->entry_count);
10202 if (mq->entry_count < 16)
10203 return -EINVAL;
10204 /* otherwise default to smallest count (drop through) */
10205 case 16:
b19a061a 10206 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10207 LPFC_MQ_CNT_16);
10208 break;
10209 case 32:
b19a061a 10210 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10211 LPFC_MQ_CNT_32);
10212 break;
10213 case 64:
b19a061a 10214 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10215 LPFC_MQ_CNT_64);
10216 break;
10217 case 128:
b19a061a 10218 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
10219 LPFC_MQ_CNT_128);
10220 break;
10221 }
10222 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 10223 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 10224 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 10225 putPaddrLow(dmabuf->phys);
b19a061a 10226 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
10227 putPaddrHigh(dmabuf->phys);
10228 }
10229 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
10230 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
10231 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10232 &mq_create_ext->u.response);
10233 if (rc != MBX_SUCCESS) {
10234 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10235 "2795 MQ_CREATE_EXT failed with "
10236 "status x%x. Failback to MQ_CREATE.\n",
10237 rc);
10238 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
10239 mq_create = &mbox->u.mqe.un.mq_create;
10240 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10241 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
10242 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
10243 &mq_create->u.response);
10244 }
10245
04c68496 10246 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
10247 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10248 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10249 if (shdr_status || shdr_add_status || rc) {
10250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10251 "2502 MQ_CREATE mailbox failed with "
10252 "status x%x add_status x%x, mbx status x%x\n",
10253 shdr_status, shdr_add_status, rc);
10254 status = -ENXIO;
10255 goto out;
10256 }
04c68496
JS
10257 if (mq->queue_id == 0xFFFF) {
10258 status = -ENXIO;
10259 goto out;
10260 }
10261 mq->type = LPFC_MQ;
10262 mq->subtype = subtype;
10263 mq->host_index = 0;
10264 mq->hba_index = 0;
10265
10266 /* link the mq onto the parent cq child list */
10267 list_add_tail(&mq->list, &cq->child_list);
10268out:
8fa38513 10269 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
10270 return status;
10271}
10272
4f774513
JS
10273/**
10274 * lpfc_wq_create - Create a Work Queue on the HBA
10275 * @phba: HBA structure that indicates port to create a queue on.
10276 * @wq: The queue structure to use to create the work queue.
10277 * @cq: The completion queue to bind this work queue to.
10278 * @subtype: The subtype of the work queue indicating its functionality.
10279 *
10280 * This function creates a work queue, as detailed in @wq, on a port, described
10281 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10282 *
10283 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10284 * is used to get the entry count and entry size that are necessary to
10285 * determine the number of pages to allocate and use for this queue. The @cq
10286 * is used to indicate which completion queue to bind this work queue to. This
10287 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10288 * work queue. This function is asynchronous and will wait for the mailbox
10289 * command to finish before continuing.
10290 *
10291 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10292 * memory this function will return -ENOMEM. If the queue create mailbox command
10293 * fails this function will return -ENXIO.
4f774513
JS
10294 **/
10295uint32_t
10296lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10297 struct lpfc_queue *cq, uint32_t subtype)
10298{
10299 struct lpfc_mbx_wq_create *wq_create;
10300 struct lpfc_dmabuf *dmabuf;
10301 LPFC_MBOXQ_t *mbox;
10302 int rc, length, status = 0;
10303 uint32_t shdr_status, shdr_add_status;
10304 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
10305 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10306
10307 if (!phba->sli4_hba.pc_sli4_params.supported)
10308 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
10309
10310 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10311 if (!mbox)
10312 return -ENOMEM;
10313 length = (sizeof(struct lpfc_mbx_wq_create) -
10314 sizeof(struct lpfc_sli4_cfg_mhdr));
10315 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10316 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10317 length, LPFC_SLI4_MBX_EMBED);
10318 wq_create = &mbox->u.mqe.un.wq_create;
10319 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10320 wq->page_count);
10321 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10322 cq->queue_id);
10323 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 10324 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
10325 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10326 putPaddrLow(dmabuf->phys);
10327 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10328 putPaddrHigh(dmabuf->phys);
10329 }
10330 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10331 /* The IOCTL status is embedded in the mailbox subheader. */
10332 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10333 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10334 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10335 if (shdr_status || shdr_add_status || rc) {
10336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10337 "2503 WQ_CREATE mailbox failed with "
10338 "status x%x add_status x%x, mbx status x%x\n",
10339 shdr_status, shdr_add_status, rc);
10340 status = -ENXIO;
10341 goto out;
10342 }
10343 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10344 if (wq->queue_id == 0xFFFF) {
10345 status = -ENXIO;
10346 goto out;
10347 }
10348 wq->type = LPFC_WQ;
10349 wq->subtype = subtype;
10350 wq->host_index = 0;
10351 wq->hba_index = 0;
10352
10353 /* link the wq onto the parent cq child list */
10354 list_add_tail(&wq->list, &cq->child_list);
10355out:
8fa38513 10356 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10357 return status;
10358}
10359
10360/**
10361 * lpfc_rq_create - Create a Receive Queue on the HBA
10362 * @phba: HBA structure that indicates port to create a queue on.
10363 * @hrq: The queue structure to use to create the header receive queue.
10364 * @drq: The queue structure to use to create the data receive queue.
10365 * @cq: The completion queue to bind this work queue to.
10366 *
10367 * This function creates a receive buffer queue pair , as detailed in @hrq and
10368 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10369 * to the HBA.
10370 *
10371 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10372 * struct is used to get the entry count that is necessary to determine the
10373 * number of pages to use for this queue. The @cq is used to indicate which
10374 * completion queue to bind received buffers that are posted to these queues to.
10375 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10376 * receive queue pair. This function is asynchronous and will wait for the
10377 * mailbox command to finish before continuing.
10378 *
10379 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
10380 * memory this function will return -ENOMEM. If the queue create mailbox command
10381 * fails this function will return -ENXIO.
4f774513
JS
10382 **/
10383uint32_t
10384lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10385 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10386{
10387 struct lpfc_mbx_rq_create *rq_create;
10388 struct lpfc_dmabuf *dmabuf;
10389 LPFC_MBOXQ_t *mbox;
10390 int rc, length, status = 0;
10391 uint32_t shdr_status, shdr_add_status;
10392 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
10393 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
10394
10395 if (!phba->sli4_hba.pc_sli4_params.supported)
10396 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
10397
10398 if (hrq->entry_count != drq->entry_count)
10399 return -EINVAL;
10400 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10401 if (!mbox)
10402 return -ENOMEM;
10403 length = (sizeof(struct lpfc_mbx_rq_create) -
10404 sizeof(struct lpfc_sli4_cfg_mhdr));
10405 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10406 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10407 length, LPFC_SLI4_MBX_EMBED);
10408 rq_create = &mbox->u.mqe.un.rq_create;
10409 switch (hrq->entry_count) {
10410 default:
10411 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10412 "2535 Unsupported RQ count. (%d)\n",
10413 hrq->entry_count);
10414 if (hrq->entry_count < 512)
10415 return -EINVAL;
10416 /* otherwise default to smallest count (drop through) */
10417 case 512:
10418 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10419 LPFC_RQ_RING_SIZE_512);
10420 break;
10421 case 1024:
10422 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10423 LPFC_RQ_RING_SIZE_1024);
10424 break;
10425 case 2048:
10426 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10427 LPFC_RQ_RING_SIZE_2048);
10428 break;
10429 case 4096:
10430 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10431 LPFC_RQ_RING_SIZE_4096);
10432 break;
10433 }
10434 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10435 cq->queue_id);
10436 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10437 hrq->page_count);
10438 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10439 LPFC_HDR_BUF_SIZE);
10440 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 10441 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
10442 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10443 putPaddrLow(dmabuf->phys);
10444 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10445 putPaddrHigh(dmabuf->phys);
10446 }
10447 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10448 /* The IOCTL status is embedded in the mailbox subheader. */
10449 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10450 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10451 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10452 if (shdr_status || shdr_add_status || rc) {
10453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10454 "2504 RQ_CREATE mailbox failed with "
10455 "status x%x add_status x%x, mbx status x%x\n",
10456 shdr_status, shdr_add_status, rc);
10457 status = -ENXIO;
10458 goto out;
10459 }
10460 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10461 if (hrq->queue_id == 0xFFFF) {
10462 status = -ENXIO;
10463 goto out;
10464 }
10465 hrq->type = LPFC_HRQ;
10466 hrq->subtype = subtype;
10467 hrq->host_index = 0;
10468 hrq->hba_index = 0;
10469
10470 /* now create the data queue */
10471 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10472 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10473 length, LPFC_SLI4_MBX_EMBED);
10474 switch (drq->entry_count) {
10475 default:
10476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10477 "2536 Unsupported RQ count. (%d)\n",
10478 drq->entry_count);
10479 if (drq->entry_count < 512)
10480 return -EINVAL;
10481 /* otherwise default to smallest count (drop through) */
10482 case 512:
10483 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10484 LPFC_RQ_RING_SIZE_512);
10485 break;
10486 case 1024:
10487 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10488 LPFC_RQ_RING_SIZE_1024);
10489 break;
10490 case 2048:
10491 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10492 LPFC_RQ_RING_SIZE_2048);
10493 break;
10494 case 4096:
10495 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10496 LPFC_RQ_RING_SIZE_4096);
10497 break;
10498 }
10499 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10500 cq->queue_id);
10501 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10502 drq->page_count);
10503 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10504 LPFC_DATA_BUF_SIZE);
10505 list_for_each_entry(dmabuf, &drq->page_list, list) {
10506 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10507 putPaddrLow(dmabuf->phys);
10508 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10509 putPaddrHigh(dmabuf->phys);
10510 }
10511 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10512 /* The IOCTL status is embedded in the mailbox subheader. */
10513 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10514 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10515 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10516 if (shdr_status || shdr_add_status || rc) {
10517 status = -ENXIO;
10518 goto out;
10519 }
10520 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10521 if (drq->queue_id == 0xFFFF) {
10522 status = -ENXIO;
10523 goto out;
10524 }
10525 drq->type = LPFC_DRQ;
10526 drq->subtype = subtype;
10527 drq->host_index = 0;
10528 drq->hba_index = 0;
10529
10530 /* link the header and data RQs onto the parent cq child list */
10531 list_add_tail(&hrq->list, &cq->child_list);
10532 list_add_tail(&drq->list, &cq->child_list);
10533
10534out:
8fa38513 10535 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10536 return status;
10537}
10538
10539/**
10540 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10541 * @eq: The queue structure associated with the queue to destroy.
10542 *
10543 * This function destroys a queue, as detailed in @eq by sending an mailbox
10544 * command, specific to the type of queue, to the HBA.
10545 *
10546 * The @eq struct is used to get the queue ID of the queue to destroy.
10547 *
10548 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10549 * command fails this function will return -ENXIO.
4f774513
JS
10550 **/
10551uint32_t
10552lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10553{
10554 LPFC_MBOXQ_t *mbox;
10555 int rc, length, status = 0;
10556 uint32_t shdr_status, shdr_add_status;
10557 union lpfc_sli4_cfg_shdr *shdr;
10558
10559 if (!eq)
10560 return -ENODEV;
10561 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10562 if (!mbox)
10563 return -ENOMEM;
10564 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10565 sizeof(struct lpfc_sli4_cfg_mhdr));
10566 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10567 LPFC_MBOX_OPCODE_EQ_DESTROY,
10568 length, LPFC_SLI4_MBX_EMBED);
10569 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10570 eq->queue_id);
10571 mbox->vport = eq->phba->pport;
10572 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10573
10574 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10575 /* The IOCTL status is embedded in the mailbox subheader. */
10576 shdr = (union lpfc_sli4_cfg_shdr *)
10577 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10578 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10579 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10580 if (shdr_status || shdr_add_status || rc) {
10581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10582 "2505 EQ_DESTROY mailbox failed with "
10583 "status x%x add_status x%x, mbx status x%x\n",
10584 shdr_status, shdr_add_status, rc);
10585 status = -ENXIO;
10586 }
10587
10588 /* Remove eq from any list */
10589 list_del_init(&eq->list);
8fa38513 10590 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10591 return status;
10592}
10593
10594/**
10595 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10596 * @cq: The queue structure associated with the queue to destroy.
10597 *
10598 * This function destroys a queue, as detailed in @cq by sending an mailbox
10599 * command, specific to the type of queue, to the HBA.
10600 *
10601 * The @cq struct is used to get the queue ID of the queue to destroy.
10602 *
10603 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10604 * command fails this function will return -ENXIO.
4f774513
JS
10605 **/
10606uint32_t
10607lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10608{
10609 LPFC_MBOXQ_t *mbox;
10610 int rc, length, status = 0;
10611 uint32_t shdr_status, shdr_add_status;
10612 union lpfc_sli4_cfg_shdr *shdr;
10613
10614 if (!cq)
10615 return -ENODEV;
10616 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10617 if (!mbox)
10618 return -ENOMEM;
10619 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10620 sizeof(struct lpfc_sli4_cfg_mhdr));
10621 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10622 LPFC_MBOX_OPCODE_CQ_DESTROY,
10623 length, LPFC_SLI4_MBX_EMBED);
10624 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10625 cq->queue_id);
10626 mbox->vport = cq->phba->pport;
10627 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10628 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10629 /* The IOCTL status is embedded in the mailbox subheader. */
10630 shdr = (union lpfc_sli4_cfg_shdr *)
10631 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10632 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10633 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10634 if (shdr_status || shdr_add_status || rc) {
10635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10636 "2506 CQ_DESTROY mailbox failed with "
10637 "status x%x add_status x%x, mbx status x%x\n",
10638 shdr_status, shdr_add_status, rc);
10639 status = -ENXIO;
10640 }
10641 /* Remove cq from any list */
10642 list_del_init(&cq->list);
8fa38513 10643 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10644 return status;
10645}
10646
04c68496
JS
10647/**
10648 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10649 * @qm: The queue structure associated with the queue to destroy.
10650 *
10651 * This function destroys a queue, as detailed in @mq by sending an mailbox
10652 * command, specific to the type of queue, to the HBA.
10653 *
10654 * The @mq struct is used to get the queue ID of the queue to destroy.
10655 *
10656 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10657 * command fails this function will return -ENXIO.
04c68496
JS
10658 **/
10659uint32_t
10660lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10661{
10662 LPFC_MBOXQ_t *mbox;
10663 int rc, length, status = 0;
10664 uint32_t shdr_status, shdr_add_status;
10665 union lpfc_sli4_cfg_shdr *shdr;
10666
10667 if (!mq)
10668 return -ENODEV;
10669 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10670 if (!mbox)
10671 return -ENOMEM;
10672 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10673 sizeof(struct lpfc_sli4_cfg_mhdr));
10674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10675 LPFC_MBOX_OPCODE_MQ_DESTROY,
10676 length, LPFC_SLI4_MBX_EMBED);
10677 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10678 mq->queue_id);
10679 mbox->vport = mq->phba->pport;
10680 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10681 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10682 /* The IOCTL status is embedded in the mailbox subheader. */
10683 shdr = (union lpfc_sli4_cfg_shdr *)
10684 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10685 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10686 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10687 if (shdr_status || shdr_add_status || rc) {
10688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10689 "2507 MQ_DESTROY mailbox failed with "
10690 "status x%x add_status x%x, mbx status x%x\n",
10691 shdr_status, shdr_add_status, rc);
10692 status = -ENXIO;
10693 }
10694 /* Remove mq from any list */
10695 list_del_init(&mq->list);
8fa38513 10696 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10697 return status;
10698}
10699
4f774513
JS
10700/**
10701 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10702 * @wq: The queue structure associated with the queue to destroy.
10703 *
10704 * This function destroys a queue, as detailed in @wq by sending an mailbox
10705 * command, specific to the type of queue, to the HBA.
10706 *
10707 * The @wq struct is used to get the queue ID of the queue to destroy.
10708 *
10709 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10710 * command fails this function will return -ENXIO.
4f774513
JS
10711 **/
10712uint32_t
10713lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10714{
10715 LPFC_MBOXQ_t *mbox;
10716 int rc, length, status = 0;
10717 uint32_t shdr_status, shdr_add_status;
10718 union lpfc_sli4_cfg_shdr *shdr;
10719
10720 if (!wq)
10721 return -ENODEV;
10722 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10723 if (!mbox)
10724 return -ENOMEM;
10725 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10726 sizeof(struct lpfc_sli4_cfg_mhdr));
10727 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10728 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10729 length, LPFC_SLI4_MBX_EMBED);
10730 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10731 wq->queue_id);
10732 mbox->vport = wq->phba->pport;
10733 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10734 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10735 shdr = (union lpfc_sli4_cfg_shdr *)
10736 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10737 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10739 if (shdr_status || shdr_add_status || rc) {
10740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10741 "2508 WQ_DESTROY mailbox failed with "
10742 "status x%x add_status x%x, mbx status x%x\n",
10743 shdr_status, shdr_add_status, rc);
10744 status = -ENXIO;
10745 }
10746 /* Remove wq from any list */
10747 list_del_init(&wq->list);
8fa38513 10748 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10749 return status;
10750}
10751
10752/**
10753 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10754 * @rq: The queue structure associated with the queue to destroy.
10755 *
10756 * This function destroys a queue, as detailed in @rq by sending an mailbox
10757 * command, specific to the type of queue, to the HBA.
10758 *
10759 * The @rq struct is used to get the queue ID of the queue to destroy.
10760 *
10761 * On success this function will return a zero. If the queue destroy mailbox
d439d286 10762 * command fails this function will return -ENXIO.
4f774513
JS
10763 **/
10764uint32_t
10765lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10766 struct lpfc_queue *drq)
10767{
10768 LPFC_MBOXQ_t *mbox;
10769 int rc, length, status = 0;
10770 uint32_t shdr_status, shdr_add_status;
10771 union lpfc_sli4_cfg_shdr *shdr;
10772
10773 if (!hrq || !drq)
10774 return -ENODEV;
10775 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10776 if (!mbox)
10777 return -ENOMEM;
10778 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10779 sizeof(struct mbox_header));
10780 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10781 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10782 length, LPFC_SLI4_MBX_EMBED);
10783 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10784 hrq->queue_id);
10785 mbox->vport = hrq->phba->pport;
10786 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10787 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10788 /* The IOCTL status is embedded in the mailbox subheader. */
10789 shdr = (union lpfc_sli4_cfg_shdr *)
10790 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10791 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10792 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10793 if (shdr_status || shdr_add_status || rc) {
10794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10795 "2509 RQ_DESTROY mailbox failed with "
10796 "status x%x add_status x%x, mbx status x%x\n",
10797 shdr_status, shdr_add_status, rc);
10798 if (rc != MBX_TIMEOUT)
10799 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10800 return -ENXIO;
10801 }
10802 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10803 drq->queue_id);
10804 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10805 shdr = (union lpfc_sli4_cfg_shdr *)
10806 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10807 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10808 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10809 if (shdr_status || shdr_add_status || rc) {
10810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10811 "2510 RQ_DESTROY mailbox failed with "
10812 "status x%x add_status x%x, mbx status x%x\n",
10813 shdr_status, shdr_add_status, rc);
10814 status = -ENXIO;
10815 }
10816 list_del_init(&hrq->list);
10817 list_del_init(&drq->list);
8fa38513 10818 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10819 return status;
10820}
10821
10822/**
10823 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10824 * @phba: The virtual port for which this call being executed.
10825 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10826 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10827 * @xritag: the xritag that ties this io to the SGL pages.
10828 *
10829 * This routine will post the sgl pages for the IO that has the xritag
10830 * that is in the iocbq structure. The xritag is assigned during iocbq
10831 * creation and persists for as long as the driver is loaded.
10832 * if the caller has fewer than 256 scatter gather segments to map then
10833 * pdma_phys_addr1 should be 0.
10834 * If the caller needs to map more than 256 scatter gather segment then
10835 * pdma_phys_addr1 should be a valid physical address.
10836 * physical address for SGLs must be 64 byte aligned.
10837 * If you are going to map 2 SGL's then the first one must have 256 entries
10838 * the second sgl can have between 1 and 256 entries.
10839 *
10840 * Return codes:
10841 * 0 - Success
10842 * -ENXIO, -ENOMEM - Failure
10843 **/
10844int
10845lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10846 dma_addr_t pdma_phys_addr0,
10847 dma_addr_t pdma_phys_addr1,
10848 uint16_t xritag)
10849{
10850 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10851 LPFC_MBOXQ_t *mbox;
10852 int rc;
10853 uint32_t shdr_status, shdr_add_status;
10854 union lpfc_sli4_cfg_shdr *shdr;
10855
10856 if (xritag == NO_XRI) {
10857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10858 "0364 Invalid param:\n");
10859 return -EINVAL;
10860 }
10861
10862 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10863 if (!mbox)
10864 return -ENOMEM;
10865
10866 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10867 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10868 sizeof(struct lpfc_mbx_post_sgl_pages) -
10869 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10870
10871 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10872 &mbox->u.mqe.un.post_sgl_pages;
10873 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10874 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10875
10876 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10877 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10878 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10879 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10880
10881 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10882 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10883 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10884 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10885 if (!phba->sli4_hba.intr_enable)
10886 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10887 else
10888 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10889 /* The IOCTL status is embedded in the mailbox subheader. */
10890 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10891 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10892 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10893 if (rc != MBX_TIMEOUT)
10894 mempool_free(mbox, phba->mbox_mem_pool);
10895 if (shdr_status || shdr_add_status || rc) {
10896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10897 "2511 POST_SGL mailbox failed with "
10898 "status x%x add_status x%x, mbx status x%x\n",
10899 shdr_status, shdr_add_status, rc);
10900 rc = -ENXIO;
10901 }
10902 return 0;
10903}
4f774513
JS
10904
10905/**
10906 * lpfc_sli4_next_xritag - Get an xritag for the io
10907 * @phba: Pointer to HBA context object.
10908 *
10909 * This function gets an xritag for the iocb. If there is no unused xritag
10910 * it will return 0xffff.
10911 * The function returns the allocated xritag if successful, else returns zero.
10912 * Zero is not a valid xritag.
10913 * The caller is not required to hold any lock.
10914 **/
10915uint16_t
10916lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10917{
10918 uint16_t xritag;
10919
10920 spin_lock_irq(&phba->hbalock);
10921 xritag = phba->sli4_hba.next_xri;
10922 if ((xritag != (uint16_t) -1) && xritag <
10923 (phba->sli4_hba.max_cfg_param.max_xri
10924 + phba->sli4_hba.max_cfg_param.xri_base)) {
10925 phba->sli4_hba.next_xri++;
10926 phba->sli4_hba.max_cfg_param.xri_used++;
10927 spin_unlock_irq(&phba->hbalock);
10928 return xritag;
10929 }
10930 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10931 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10932 "2004 Failed to allocate XRI.last XRITAG is %d"
10933 " Max XRI is %d, Used XRI is %d\n",
10934 phba->sli4_hba.next_xri,
10935 phba->sli4_hba.max_cfg_param.max_xri,
10936 phba->sli4_hba.max_cfg_param.xri_used);
10937 return -1;
10938}
10939
10940/**
10941 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10942 * @phba: pointer to lpfc hba data structure.
10943 *
10944 * This routine is invoked to post a block of driver's sgl pages to the
10945 * HBA using non-embedded mailbox command. No Lock is held. This routine
10946 * is only called when the driver is loading and after all IO has been
10947 * stopped.
10948 **/
10949int
10950lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10951{
10952 struct lpfc_sglq *sglq_entry;
10953 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10954 struct sgl_page_pairs *sgl_pg_pairs;
10955 void *viraddr;
10956 LPFC_MBOXQ_t *mbox;
10957 uint32_t reqlen, alloclen, pg_pairs;
10958 uint32_t mbox_tmo;
10959 uint16_t xritag_start = 0;
10960 int els_xri_cnt, rc = 0;
10961 uint32_t shdr_status, shdr_add_status;
10962 union lpfc_sli4_cfg_shdr *shdr;
10963
10964 /* The number of sgls to be posted */
10965 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10966
10967 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10968 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 10969 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
10970 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10971 "2559 Block sgl registration required DMA "
10972 "size (%d) great than a page\n", reqlen);
10973 return -ENOMEM;
10974 }
10975 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10976 if (!mbox) {
10977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10978 "2560 Failed to allocate mbox cmd memory\n");
10979 return -ENOMEM;
10980 }
10981
10982 /* Allocate DMA memory and set up the non-embedded mailbox command */
10983 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10984 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10985 LPFC_SLI4_MBX_NEMBED);
10986
10987 if (alloclen < reqlen) {
10988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10989 "0285 Allocated DMA memory size (%d) is "
10990 "less than the requested DMA memory "
10991 "size (%d)\n", alloclen, reqlen);
10992 lpfc_sli4_mbox_cmd_free(phba, mbox);
10993 return -ENOMEM;
10994 }
4f774513 10995 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10996 viraddr = mbox->sge_array->addr[0];
10997
10998 /* Set up the SGL pages in the non-embedded DMA pages */
10999 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11000 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11001
11002 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
11003 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
11004 /* Set up the sge entry */
11005 sgl_pg_pairs->sgl_pg0_addr_lo =
11006 cpu_to_le32(putPaddrLow(sglq_entry->phys));
11007 sgl_pg_pairs->sgl_pg0_addr_hi =
11008 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
11009 sgl_pg_pairs->sgl_pg1_addr_lo =
11010 cpu_to_le32(putPaddrLow(0));
11011 sgl_pg_pairs->sgl_pg1_addr_hi =
11012 cpu_to_le32(putPaddrHigh(0));
11013 /* Keep the first xritag on the list */
11014 if (pg_pairs == 0)
11015 xritag_start = sglq_entry->sli4_xritag;
11016 sgl_pg_pairs++;
11017 }
11018 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 11019 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
11020 /* Perform endian conversion if necessary */
11021 sgl->word0 = cpu_to_le32(sgl->word0);
11022
11023 if (!phba->sli4_hba.intr_enable)
11024 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11025 else {
11026 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11027 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11028 }
11029 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11030 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11031 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11032 if (rc != MBX_TIMEOUT)
11033 lpfc_sli4_mbox_cmd_free(phba, mbox);
11034 if (shdr_status || shdr_add_status || rc) {
11035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11036 "2513 POST_SGL_BLOCK mailbox command failed "
11037 "status x%x add_status x%x mbx status x%x\n",
11038 shdr_status, shdr_add_status, rc);
11039 rc = -ENXIO;
11040 }
11041 return rc;
11042}
11043
11044/**
11045 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
11046 * @phba: pointer to lpfc hba data structure.
11047 * @sblist: pointer to scsi buffer list.
11048 * @count: number of scsi buffers on the list.
11049 *
11050 * This routine is invoked to post a block of @count scsi sgl pages from a
11051 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
11052 * No Lock is held.
11053 *
11054 **/
11055int
11056lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
11057 int cnt)
11058{
11059 struct lpfc_scsi_buf *psb;
11060 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
11061 struct sgl_page_pairs *sgl_pg_pairs;
11062 void *viraddr;
11063 LPFC_MBOXQ_t *mbox;
11064 uint32_t reqlen, alloclen, pg_pairs;
11065 uint32_t mbox_tmo;
11066 uint16_t xritag_start = 0;
11067 int rc = 0;
11068 uint32_t shdr_status, shdr_add_status;
11069 dma_addr_t pdma_phys_bpl1;
11070 union lpfc_sli4_cfg_shdr *shdr;
11071
11072 /* Calculate the requested length of the dma memory */
11073 reqlen = cnt * sizeof(struct sgl_page_pairs) +
11074 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 11075 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
11076 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
11077 "0217 Block sgl registration required DMA "
11078 "size (%d) great than a page\n", reqlen);
11079 return -ENOMEM;
11080 }
11081 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11082 if (!mbox) {
11083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11084 "0283 Failed to allocate mbox cmd memory\n");
11085 return -ENOMEM;
11086 }
11087
11088 /* Allocate DMA memory and set up the non-embedded mailbox command */
11089 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
11090 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
11091 LPFC_SLI4_MBX_NEMBED);
11092
11093 if (alloclen < reqlen) {
11094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11095 "2561 Allocated DMA memory size (%d) is "
11096 "less than the requested DMA memory "
11097 "size (%d)\n", alloclen, reqlen);
11098 lpfc_sli4_mbox_cmd_free(phba, mbox);
11099 return -ENOMEM;
11100 }
4f774513 11101 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
11102 viraddr = mbox->sge_array->addr[0];
11103
11104 /* Set up the SGL pages in the non-embedded DMA pages */
11105 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
11106 sgl_pg_pairs = &sgl->sgl_pg_pairs;
11107
11108 pg_pairs = 0;
11109 list_for_each_entry(psb, sblist, list) {
11110 /* Set up the sge entry */
11111 sgl_pg_pairs->sgl_pg0_addr_lo =
11112 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
11113 sgl_pg_pairs->sgl_pg0_addr_hi =
11114 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
11115 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
11116 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
11117 else
11118 pdma_phys_bpl1 = 0;
11119 sgl_pg_pairs->sgl_pg1_addr_lo =
11120 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
11121 sgl_pg_pairs->sgl_pg1_addr_hi =
11122 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
11123 /* Keep the first xritag on the list */
11124 if (pg_pairs == 0)
11125 xritag_start = psb->cur_iocbq.sli4_xritag;
11126 sgl_pg_pairs++;
11127 pg_pairs++;
11128 }
11129 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
11130 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
11131 /* Perform endian conversion if necessary */
11132 sgl->word0 = cpu_to_le32(sgl->word0);
11133
11134 if (!phba->sli4_hba.intr_enable)
11135 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11136 else {
11137 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11138 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
11139 }
11140 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
11141 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11143 if (rc != MBX_TIMEOUT)
11144 lpfc_sli4_mbox_cmd_free(phba, mbox);
11145 if (shdr_status || shdr_add_status || rc) {
11146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11147 "2564 POST_SGL_BLOCK mailbox command failed "
11148 "status x%x add_status x%x mbx status x%x\n",
11149 shdr_status, shdr_add_status, rc);
11150 rc = -ENXIO;
11151 }
11152 return rc;
11153}
11154
11155/**
11156 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
11157 * @phba: pointer to lpfc_hba struct that the frame was received on
11158 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11159 *
11160 * This function checks the fields in the @fc_hdr to see if the FC frame is a
11161 * valid type of frame that the LPFC driver will handle. This function will
11162 * return a zero if the frame is a valid frame or a non zero value when the
11163 * frame does not pass the check.
11164 **/
11165static int
11166lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
11167{
11168 char *rctl_names[] = FC_RCTL_NAMES_INIT;
11169 char *type_names[] = FC_TYPE_NAMES_INIT;
11170 struct fc_vft_header *fc_vft_hdr;
11171
11172 switch (fc_hdr->fh_r_ctl) {
11173 case FC_RCTL_DD_UNCAT: /* uncategorized information */
11174 case FC_RCTL_DD_SOL_DATA: /* solicited data */
11175 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
11176 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
11177 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
11178 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
11179 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
11180 case FC_RCTL_DD_CMD_STATUS: /* command status */
11181 case FC_RCTL_ELS_REQ: /* extended link services request */
11182 case FC_RCTL_ELS_REP: /* extended link services reply */
11183 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
11184 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
11185 case FC_RCTL_BA_NOP: /* basic link service NOP */
11186 case FC_RCTL_BA_ABTS: /* basic link service abort */
11187 case FC_RCTL_BA_RMC: /* remove connection */
11188 case FC_RCTL_BA_ACC: /* basic accept */
11189 case FC_RCTL_BA_RJT: /* basic reject */
11190 case FC_RCTL_BA_PRMT:
11191 case FC_RCTL_ACK_1: /* acknowledge_1 */
11192 case FC_RCTL_ACK_0: /* acknowledge_0 */
11193 case FC_RCTL_P_RJT: /* port reject */
11194 case FC_RCTL_F_RJT: /* fabric reject */
11195 case FC_RCTL_P_BSY: /* port busy */
11196 case FC_RCTL_F_BSY: /* fabric busy to data frame */
11197 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
11198 case FC_RCTL_LCR: /* link credit reset */
11199 case FC_RCTL_END: /* end */
11200 break;
11201 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
11202 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11203 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
11204 return lpfc_fc_frame_check(phba, fc_hdr);
11205 default:
11206 goto drop;
11207 }
11208 switch (fc_hdr->fh_type) {
11209 case FC_TYPE_BLS:
11210 case FC_TYPE_ELS:
11211 case FC_TYPE_FCP:
11212 case FC_TYPE_CT:
11213 break;
11214 case FC_TYPE_IP:
11215 case FC_TYPE_ILS:
11216 default:
11217 goto drop;
11218 }
11219 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11220 "2538 Received frame rctl:%s type:%s\n",
11221 rctl_names[fc_hdr->fh_r_ctl],
11222 type_names[fc_hdr->fh_type]);
11223 return 0;
11224drop:
11225 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11226 "2539 Dropped frame rctl:%s type:%s\n",
11227 rctl_names[fc_hdr->fh_r_ctl],
11228 type_names[fc_hdr->fh_type]);
11229 return 1;
11230}
11231
11232/**
11233 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11234 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11235 *
11236 * This function processes the FC header to retrieve the VFI from the VF
11237 * header, if one exists. This function will return the VFI if one exists
11238 * or 0 if no VSAN Header exists.
11239 **/
11240static uint32_t
11241lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11242{
11243 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11244
11245 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11246 return 0;
11247 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11248}
11249
11250/**
11251 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11252 * @phba: Pointer to the HBA structure to search for the vport on
11253 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11254 * @fcfi: The FC Fabric ID that the frame came from
11255 *
11256 * This function searches the @phba for a vport that matches the content of the
11257 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11258 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11259 * returns the matching vport pointer or NULL if unable to match frame to a
11260 * vport.
11261 **/
11262static struct lpfc_vport *
11263lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11264 uint16_t fcfi)
11265{
11266 struct lpfc_vport **vports;
11267 struct lpfc_vport *vport = NULL;
11268 int i;
11269 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11270 fc_hdr->fh_d_id[1] << 8 |
11271 fc_hdr->fh_d_id[2]);
11272
11273 vports = lpfc_create_vport_work_array(phba);
11274 if (vports != NULL)
11275 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11276 if (phba->fcf.fcfi == fcfi &&
11277 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11278 vports[i]->fc_myDID == did) {
11279 vport = vports[i];
11280 break;
11281 }
11282 }
11283 lpfc_destroy_vport_work_array(phba, vports);
11284 return vport;
11285}
11286
45ed1190
JS
11287/**
11288 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11289 * @vport: The vport to work on.
11290 *
11291 * This function updates the receive sequence time stamp for this vport. The
11292 * receive sequence time stamp indicates the time that the last frame of the
11293 * the sequence that has been idle for the longest amount of time was received.
11294 * the driver uses this time stamp to indicate if any received sequences have
11295 * timed out.
11296 **/
11297void
11298lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11299{
11300 struct lpfc_dmabuf *h_buf;
11301 struct hbq_dmabuf *dmabuf = NULL;
11302
11303 /* get the oldest sequence on the rcv list */
11304 h_buf = list_get_first(&vport->rcv_buffer_list,
11305 struct lpfc_dmabuf, list);
11306 if (!h_buf)
11307 return;
11308 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11309 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11310}
11311
11312/**
11313 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11314 * @vport: The vport that the received sequences were sent to.
11315 *
11316 * This function cleans up all outstanding received sequences. This is called
11317 * by the driver when a link event or user action invalidates all the received
11318 * sequences.
11319 **/
11320void
11321lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11322{
11323 struct lpfc_dmabuf *h_buf, *hnext;
11324 struct lpfc_dmabuf *d_buf, *dnext;
11325 struct hbq_dmabuf *dmabuf = NULL;
11326
11327 /* start with the oldest sequence on the rcv list */
11328 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11329 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11330 list_del_init(&dmabuf->hbuf.list);
11331 list_for_each_entry_safe(d_buf, dnext,
11332 &dmabuf->dbuf.list, list) {
11333 list_del_init(&d_buf->list);
11334 lpfc_in_buf_free(vport->phba, d_buf);
11335 }
11336 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11337 }
11338}
11339
11340/**
11341 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11342 * @vport: The vport that the received sequences were sent to.
11343 *
11344 * This function determines whether any received sequences have timed out by
11345 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11346 * indicates that there is at least one timed out sequence this routine will
11347 * go through the received sequences one at a time from most inactive to most
11348 * active to determine which ones need to be cleaned up. Once it has determined
11349 * that a sequence needs to be cleaned up it will simply free up the resources
11350 * without sending an abort.
11351 **/
11352void
11353lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11354{
11355 struct lpfc_dmabuf *h_buf, *hnext;
11356 struct lpfc_dmabuf *d_buf, *dnext;
11357 struct hbq_dmabuf *dmabuf = NULL;
11358 unsigned long timeout;
11359 int abort_count = 0;
11360
11361 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11362 vport->rcv_buffer_time_stamp);
11363 if (list_empty(&vport->rcv_buffer_list) ||
11364 time_before(jiffies, timeout))
11365 return;
11366 /* start with the oldest sequence on the rcv list */
11367 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11368 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11369 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11370 dmabuf->time_stamp);
11371 if (time_before(jiffies, timeout))
11372 break;
11373 abort_count++;
11374 list_del_init(&dmabuf->hbuf.list);
11375 list_for_each_entry_safe(d_buf, dnext,
11376 &dmabuf->dbuf.list, list) {
11377 list_del_init(&d_buf->list);
11378 lpfc_in_buf_free(vport->phba, d_buf);
11379 }
11380 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11381 }
11382 if (abort_count)
11383 lpfc_update_rcv_time_stamp(vport);
11384}
11385
4f774513
JS
11386/**
11387 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11388 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11389 *
11390 * This function searches through the existing incomplete sequences that have
11391 * been sent to this @vport. If the frame matches one of the incomplete
11392 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11393 * make up that sequence. If no sequence is found that matches this frame then
11394 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11395 * This function returns a pointer to the first dmabuf in the sequence list that
11396 * the frame was linked to.
11397 **/
11398static struct hbq_dmabuf *
11399lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11400{
11401 struct fc_frame_header *new_hdr;
11402 struct fc_frame_header *temp_hdr;
11403 struct lpfc_dmabuf *d_buf;
11404 struct lpfc_dmabuf *h_buf;
11405 struct hbq_dmabuf *seq_dmabuf = NULL;
11406 struct hbq_dmabuf *temp_dmabuf = NULL;
11407
4d9ab994 11408 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 11409 dmabuf->time_stamp = jiffies;
4f774513
JS
11410 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11411 /* Use the hdr_buf to find the sequence that this frame belongs to */
11412 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11413 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11414 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11415 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11416 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11417 continue;
11418 /* found a pending sequence that matches this frame */
11419 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11420 break;
11421 }
11422 if (!seq_dmabuf) {
11423 /*
11424 * This indicates first frame received for this sequence.
11425 * Queue the buffer on the vport's rcv_buffer_list.
11426 */
11427 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 11428 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11429 return dmabuf;
11430 }
11431 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
11432 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11433 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
11434 list_del_init(&seq_dmabuf->hbuf.list);
11435 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11436 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 11437 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11438 return dmabuf;
11439 }
45ed1190
JS
11440 /* move this sequence to the tail to indicate a young sequence */
11441 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11442 seq_dmabuf->time_stamp = jiffies;
11443 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
11444 if (list_empty(&seq_dmabuf->dbuf.list)) {
11445 temp_hdr = dmabuf->hbuf.virt;
11446 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11447 return seq_dmabuf;
11448 }
4f774513
JS
11449 /* find the correct place in the sequence to insert this frame */
11450 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11451 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11452 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11453 /*
11454 * If the frame's sequence count is greater than the frame on
11455 * the list then insert the frame right after this frame
11456 */
eeead811
JS
11457 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11458 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
11459 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11460 return seq_dmabuf;
11461 }
11462 }
11463 return NULL;
11464}
11465
6669f9bb
JS
11466/**
11467 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11468 * @vport: pointer to a vitural port
11469 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11470 *
11471 * This function tries to abort from the partially assembed sequence, described
11472 * by the information from basic abbort @dmabuf. It checks to see whether such
11473 * partially assembled sequence held by the driver. If so, it shall free up all
11474 * the frames from the partially assembled sequence.
11475 *
11476 * Return
11477 * true -- if there is matching partially assembled sequence present and all
11478 * the frames freed with the sequence;
11479 * false -- if there is no matching partially assembled sequence present so
11480 * nothing got aborted in the lower layer driver
11481 **/
11482static bool
11483lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11484 struct hbq_dmabuf *dmabuf)
11485{
11486 struct fc_frame_header *new_hdr;
11487 struct fc_frame_header *temp_hdr;
11488 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11489 struct hbq_dmabuf *seq_dmabuf = NULL;
11490
11491 /* Use the hdr_buf to find the sequence that matches this frame */
11492 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11493 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11494 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11495 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11496 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11497 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11498 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11499 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11500 continue;
11501 /* found a pending sequence that matches this frame */
11502 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11503 break;
11504 }
11505
11506 /* Free up all the frames from the partially assembled sequence */
11507 if (seq_dmabuf) {
11508 list_for_each_entry_safe(d_buf, n_buf,
11509 &seq_dmabuf->dbuf.list, list) {
11510 list_del_init(&d_buf->list);
11511 lpfc_in_buf_free(vport->phba, d_buf);
11512 }
11513 return true;
11514 }
11515 return false;
11516}
11517
11518/**
11519 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11520 * @phba: Pointer to HBA context object.
11521 * @cmd_iocbq: pointer to the command iocbq structure.
11522 * @rsp_iocbq: pointer to the response iocbq structure.
11523 *
11524 * This function handles the sequence abort accept iocb command complete
11525 * event. It properly releases the memory allocated to the sequence abort
11526 * accept iocb.
11527 **/
11528static void
11529lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11530 struct lpfc_iocbq *cmd_iocbq,
11531 struct lpfc_iocbq *rsp_iocbq)
11532{
11533 if (cmd_iocbq)
11534 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11535}
11536
11537/**
11538 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11539 * @phba: Pointer to HBA context object.
11540 * @fc_hdr: pointer to a FC frame header.
11541 *
11542 * This function sends a basic accept to a previous unsol sequence abort
11543 * event after aborting the sequence handling.
11544 **/
11545static void
11546lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11547 struct fc_frame_header *fc_hdr)
11548{
11549 struct lpfc_iocbq *ctiocb = NULL;
11550 struct lpfc_nodelist *ndlp;
5ffc266e
JS
11551 uint16_t oxid, rxid;
11552 uint32_t sid, fctl;
6669f9bb
JS
11553 IOCB_t *icmd;
11554
11555 if (!lpfc_is_link_up(phba))
11556 return;
11557
11558 sid = sli4_sid_from_fc_hdr(fc_hdr);
11559 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 11560 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
11561
11562 ndlp = lpfc_findnode_did(phba->pport, sid);
11563 if (!ndlp) {
11564 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11565 "1268 Find ndlp returned NULL for oxid:x%x "
11566 "SID:x%x\n", oxid, sid);
11567 return;
11568 }
11569
11570 /* Allocate buffer for acc iocb */
11571 ctiocb = lpfc_sli_get_iocbq(phba);
11572 if (!ctiocb)
11573 return;
11574
5ffc266e
JS
11575 /* Extract the F_CTL field from FC_HDR */
11576 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11577
6669f9bb 11578 icmd = &ctiocb->iocb;
6669f9bb 11579 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 11580 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
11581 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11582 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11583 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11584
11585 /* Fill in the rest of iocb fields */
11586 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11587 icmd->ulpBdeCount = 0;
11588 icmd->ulpLe = 1;
11589 icmd->ulpClass = CLASS3;
11590 icmd->ulpContext = ndlp->nlp_rpi;
6669f9bb 11591
6669f9bb
JS
11592 ctiocb->iocb_cmpl = NULL;
11593 ctiocb->vport = phba->pport;
11594 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11595
5ffc266e
JS
11596 if (fctl & FC_FC_EX_CTX) {
11597 /* ABTS sent by responder to CT exchange, construction
11598 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11599 * field and RX_ID from ABTS for RX_ID field.
11600 */
11601 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11602 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11603 ctiocb->sli4_xritag = oxid;
11604 } else {
11605 /* ABTS sent by initiator to CT exchange, construction
11606 * of BA_ACC will need to allocate a new XRI as for the
11607 * XRI_TAG and RX_ID fields.
11608 */
11609 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11610 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11611 ctiocb->sli4_xritag = NO_XRI;
11612 }
11613 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11614
6669f9bb
JS
11615 /* Xmit CT abts accept on exchange <xid> */
11616 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11617 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11618 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11619 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11620}
11621
11622/**
11623 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11624 * @vport: Pointer to the vport on which this sequence was received
11625 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11626 *
11627 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11628 * receive sequence is only partially assembed by the driver, it shall abort
11629 * the partially assembled frames for the sequence. Otherwise, if the
11630 * unsolicited receive sequence has been completely assembled and passed to
11631 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11632 * unsolicited sequence has been aborted. After that, it will issue a basic
11633 * accept to accept the abort.
11634 **/
11635void
11636lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11637 struct hbq_dmabuf *dmabuf)
11638{
11639 struct lpfc_hba *phba = vport->phba;
11640 struct fc_frame_header fc_hdr;
5ffc266e 11641 uint32_t fctl;
6669f9bb
JS
11642 bool abts_par;
11643
6669f9bb
JS
11644 /* Make a copy of fc_hdr before the dmabuf being released */
11645 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 11646 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 11647
5ffc266e
JS
11648 if (fctl & FC_FC_EX_CTX) {
11649 /*
11650 * ABTS sent by responder to exchange, just free the buffer
11651 */
6669f9bb 11652 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
11653 } else {
11654 /*
11655 * ABTS sent by initiator to exchange, need to do cleanup
11656 */
11657 /* Try to abort partially assembled seq */
11658 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11659
11660 /* Send abort to ULP if partially seq abort failed */
11661 if (abts_par == false)
11662 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11663 else
11664 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11665 }
6669f9bb
JS
11666 /* Send basic accept (BA_ACC) to the abort requester */
11667 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11668}
11669
4f774513
JS
11670/**
11671 * lpfc_seq_complete - Indicates if a sequence is complete
11672 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11673 *
11674 * This function checks the sequence, starting with the frame described by
11675 * @dmabuf, to see if all the frames associated with this sequence are present.
11676 * the frames associated with this sequence are linked to the @dmabuf using the
11677 * dbuf list. This function looks for two major things. 1) That the first frame
11678 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11679 * set. 3) That there are no holes in the sequence count. The function will
11680 * return 1 when the sequence is complete, otherwise it will return 0.
11681 **/
11682static int
11683lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11684{
11685 struct fc_frame_header *hdr;
11686 struct lpfc_dmabuf *d_buf;
11687 struct hbq_dmabuf *seq_dmabuf;
11688 uint32_t fctl;
11689 int seq_count = 0;
11690
11691 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11692 /* make sure first fame of sequence has a sequence count of zero */
11693 if (hdr->fh_seq_cnt != seq_count)
11694 return 0;
11695 fctl = (hdr->fh_f_ctl[0] << 16 |
11696 hdr->fh_f_ctl[1] << 8 |
11697 hdr->fh_f_ctl[2]);
11698 /* If last frame of sequence we can return success. */
11699 if (fctl & FC_FC_END_SEQ)
11700 return 1;
11701 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11702 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11703 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11704 /* If there is a hole in the sequence count then fail. */
eeead811 11705 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
11706 return 0;
11707 fctl = (hdr->fh_f_ctl[0] << 16 |
11708 hdr->fh_f_ctl[1] << 8 |
11709 hdr->fh_f_ctl[2]);
11710 /* If last frame of sequence we can return success. */
11711 if (fctl & FC_FC_END_SEQ)
11712 return 1;
11713 }
11714 return 0;
11715}
11716
11717/**
11718 * lpfc_prep_seq - Prep sequence for ULP processing
11719 * @vport: Pointer to the vport on which this sequence was received
11720 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11721 *
11722 * This function takes a sequence, described by a list of frames, and creates
11723 * a list of iocbq structures to describe the sequence. This iocbq list will be
11724 * used to issue to the generic unsolicited sequence handler. This routine
11725 * returns a pointer to the first iocbq in the list. If the function is unable
11726 * to allocate an iocbq then it throw out the received frames that were not
11727 * able to be described and return a pointer to the first iocbq. If unable to
11728 * allocate any iocbqs (including the first) this function will return NULL.
11729 **/
11730static struct lpfc_iocbq *
11731lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11732{
11733 struct lpfc_dmabuf *d_buf, *n_buf;
11734 struct lpfc_iocbq *first_iocbq, *iocbq;
11735 struct fc_frame_header *fc_hdr;
11736 uint32_t sid;
eeead811 11737 struct ulp_bde64 *pbde;
4f774513
JS
11738
11739 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11740 /* remove from receive buffer list */
11741 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 11742 lpfc_update_rcv_time_stamp(vport);
4f774513 11743 /* get the Remote Port's SID */
6669f9bb 11744 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11745 /* Get an iocbq struct to fill in. */
11746 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11747 if (first_iocbq) {
11748 /* Initialize the first IOCB. */
8fa38513 11749 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11750 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11751 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11752 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11753 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11754 vport->vpi + vport->phba->vpi_base;
11755 /* put the first buffer into the first IOCBq */
11756 first_iocbq->context2 = &seq_dmabuf->dbuf;
11757 first_iocbq->context3 = NULL;
11758 first_iocbq->iocb.ulpBdeCount = 1;
11759 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11760 LPFC_DATA_BUF_SIZE;
11761 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11762 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11763 bf_get(lpfc_rcqe_length,
11764 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11765 }
11766 iocbq = first_iocbq;
11767 /*
11768 * Each IOCBq can have two Buffers assigned, so go through the list
11769 * of buffers for this sequence and save two buffers in each IOCBq
11770 */
11771 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11772 if (!iocbq) {
11773 lpfc_in_buf_free(vport->phba, d_buf);
11774 continue;
11775 }
11776 if (!iocbq->context3) {
11777 iocbq->context3 = d_buf;
11778 iocbq->iocb.ulpBdeCount++;
eeead811
JS
11779 pbde = (struct ulp_bde64 *)
11780 &iocbq->iocb.unsli3.sli3Words[4];
11781 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
8fa38513 11782 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11783 bf_get(lpfc_rcqe_length,
11784 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11785 } else {
11786 iocbq = lpfc_sli_get_iocbq(vport->phba);
11787 if (!iocbq) {
11788 if (first_iocbq) {
11789 first_iocbq->iocb.ulpStatus =
11790 IOSTAT_FCP_RSP_ERROR;
11791 first_iocbq->iocb.un.ulpWord[4] =
11792 IOERR_NO_RESOURCES;
11793 }
11794 lpfc_in_buf_free(vport->phba, d_buf);
11795 continue;
11796 }
11797 iocbq->context2 = d_buf;
11798 iocbq->context3 = NULL;
11799 iocbq->iocb.ulpBdeCount = 1;
11800 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11801 LPFC_DATA_BUF_SIZE;
8fa38513 11802 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11803 bf_get(lpfc_rcqe_length,
11804 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11805 iocbq->iocb.un.rcvels.remoteID = sid;
11806 list_add_tail(&iocbq->list, &first_iocbq->list);
11807 }
11808 }
11809 return first_iocbq;
11810}
11811
6669f9bb
JS
11812static void
11813lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11814 struct hbq_dmabuf *seq_dmabuf)
11815{
11816 struct fc_frame_header *fc_hdr;
11817 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11818 struct lpfc_hba *phba = vport->phba;
11819
11820 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11821 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11822 if (!iocbq) {
11823 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11824 "2707 Ring %d handler: Failed to allocate "
11825 "iocb Rctl x%x Type x%x received\n",
11826 LPFC_ELS_RING,
11827 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11828 return;
11829 }
11830 if (!lpfc_complete_unsol_iocb(phba,
11831 &phba->sli.ring[LPFC_ELS_RING],
11832 iocbq, fc_hdr->fh_r_ctl,
11833 fc_hdr->fh_type))
11834 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11835 "2540 Ring %d handler: unexpected Rctl "
11836 "x%x Type x%x received\n",
11837 LPFC_ELS_RING,
11838 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11839
11840 /* Free iocb created in lpfc_prep_seq */
11841 list_for_each_entry_safe(curr_iocb, next_iocb,
11842 &iocbq->list, list) {
11843 list_del_init(&curr_iocb->list);
11844 lpfc_sli_release_iocbq(phba, curr_iocb);
11845 }
11846 lpfc_sli_release_iocbq(phba, iocbq);
11847}
11848
4f774513
JS
11849/**
11850 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11851 * @phba: Pointer to HBA context object.
11852 *
11853 * This function is called with no lock held. This function processes all
11854 * the received buffers and gives it to upper layers when a received buffer
11855 * indicates that it is the final frame in the sequence. The interrupt
11856 * service routine processes received buffers at interrupt contexts and adds
11857 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11858 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11859 * appropriate receive function when the final frame in a sequence is received.
11860 **/
4d9ab994
JS
11861void
11862lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11863 struct hbq_dmabuf *dmabuf)
4f774513 11864{
4d9ab994 11865 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11866 struct fc_frame_header *fc_hdr;
11867 struct lpfc_vport *vport;
11868 uint32_t fcfi;
4f774513 11869
4f774513 11870 /* Process each received buffer */
4d9ab994
JS
11871 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11872 /* check to see if this a valid type of frame */
11873 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11874 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11875 return;
11876 }
11877 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11878 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 11879 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
11880 /* throw out the frame */
11881 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11882 return;
11883 }
6669f9bb
JS
11884 /* Handle the basic abort sequence (BA_ABTS) event */
11885 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11886 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11887 return;
11888 }
11889
4d9ab994
JS
11890 /* Link this frame */
11891 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11892 if (!seq_dmabuf) {
11893 /* unable to add frame to vport - throw it out */
11894 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11895 return;
11896 }
11897 /* If not last frame in sequence continue processing frames. */
def9c7a9 11898 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 11899 return;
def9c7a9 11900
6669f9bb
JS
11901 /* Send the complete sequence to the upper layer protocol */
11902 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11903}
6fb120a7
JS
11904
11905/**
11906 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11907 * @phba: pointer to lpfc hba data structure.
11908 *
11909 * This routine is invoked to post rpi header templates to the
11910 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
11911 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
11912 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
11913 *
11914 * This routine does not require any locks. It's usage is expected
11915 * to be driver load or reset recovery when the driver is
11916 * sequential.
11917 *
11918 * Return codes
af901ca1 11919 * 0 - successful
d439d286 11920 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
11921 * When this error occurs, the driver is not guaranteed
11922 * to have any rpi regions posted to the device and
11923 * must either attempt to repost the regions or take a
11924 * fatal error.
11925 **/
11926int
11927lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11928{
11929 struct lpfc_rpi_hdr *rpi_page;
11930 uint32_t rc = 0;
11931
11932 /* Post all rpi memory regions to the port. */
11933 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11934 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11935 if (rc != MBX_SUCCESS) {
11936 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11937 "2008 Error %d posting all rpi "
11938 "headers\n", rc);
11939 rc = -EIO;
11940 break;
11941 }
11942 }
11943
11944 return rc;
11945}
11946
11947/**
11948 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11949 * @phba: pointer to lpfc hba data structure.
11950 * @rpi_page: pointer to the rpi memory region.
11951 *
11952 * This routine is invoked to post a single rpi header to the
11953 * HBA consistent with the SLI-4 interface spec. This memory region
11954 * maps up to 64 rpi context regions.
11955 *
11956 * Return codes
af901ca1 11957 * 0 - successful
d439d286
JS
11958 * -ENOMEM - No available memory
11959 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
11960 **/
11961int
11962lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11963{
11964 LPFC_MBOXQ_t *mboxq;
11965 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11966 uint32_t rc = 0;
11967 uint32_t mbox_tmo;
11968 uint32_t shdr_status, shdr_add_status;
11969 union lpfc_sli4_cfg_shdr *shdr;
11970
11971 /* The port is notified of the header region via a mailbox command. */
11972 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11973 if (!mboxq) {
11974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11975 "2001 Unable to allocate memory for issuing "
11976 "SLI_CONFIG_SPECIAL mailbox command\n");
11977 return -ENOMEM;
11978 }
11979
11980 /* Post all rpi memory regions to the port. */
11981 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11982 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11983 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11984 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11985 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11986 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11987 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11988 hdr_tmpl, rpi_page->page_count);
11989 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11990 rpi_page->start_rpi);
11991 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11992 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11993 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11994 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11995 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11996 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11997 if (rc != MBX_TIMEOUT)
11998 mempool_free(mboxq, phba->mbox_mem_pool);
11999 if (shdr_status || shdr_add_status || rc) {
12000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12001 "2514 POST_RPI_HDR mailbox failed with "
12002 "status x%x add_status x%x, mbx status x%x\n",
12003 shdr_status, shdr_add_status, rc);
12004 rc = -ENXIO;
12005 }
12006 return rc;
12007}
12008
12009/**
12010 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
12011 * @phba: pointer to lpfc hba data structure.
12012 *
12013 * This routine is invoked to post rpi header templates to the
12014 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
12015 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12016 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
12017 *
12018 * Returns
af901ca1 12019 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
12020 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12021 **/
12022int
12023lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
12024{
12025 int rpi;
12026 uint16_t max_rpi, rpi_base, rpi_limit;
12027 uint16_t rpi_remaining;
12028 struct lpfc_rpi_hdr *rpi_hdr;
12029
12030 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
12031 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
12032 rpi_limit = phba->sli4_hba.next_rpi;
12033
12034 /*
12035 * The valid rpi range is not guaranteed to be zero-based. Start
12036 * the search at the rpi_base as reported by the port.
12037 */
12038 spin_lock_irq(&phba->hbalock);
12039 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
12040 if (rpi >= rpi_limit || rpi < rpi_base)
12041 rpi = LPFC_RPI_ALLOC_ERROR;
12042 else {
12043 set_bit(rpi, phba->sli4_hba.rpi_bmask);
12044 phba->sli4_hba.max_cfg_param.rpi_used++;
12045 phba->sli4_hba.rpi_count++;
12046 }
12047
12048 /*
12049 * Don't try to allocate more rpi header regions if the device limit
12050 * on available rpis max has been exhausted.
12051 */
12052 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
12053 (phba->sli4_hba.rpi_count >= max_rpi)) {
12054 spin_unlock_irq(&phba->hbalock);
12055 return rpi;
12056 }
12057
12058 /*
12059 * If the driver is running low on rpi resources, allocate another
12060 * page now. Note that the next_rpi value is used because
12061 * it represents how many are actually in use whereas max_rpi notes
12062 * how many are supported max by the device.
12063 */
12064 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
12065 phba->sli4_hba.rpi_count;
12066 spin_unlock_irq(&phba->hbalock);
12067 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
12068 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
12069 if (!rpi_hdr) {
12070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12071 "2002 Error Could not grow rpi "
12072 "count\n");
12073 } else {
12074 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
12075 }
12076 }
12077
12078 return rpi;
12079}
12080
d7c47992
JS
12081/**
12082 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12083 * @phba: pointer to lpfc hba data structure.
12084 *
12085 * This routine is invoked to release an rpi to the pool of
12086 * available rpis maintained by the driver.
12087 **/
12088void
12089__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12090{
12091 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
12092 phba->sli4_hba.rpi_count--;
12093 phba->sli4_hba.max_cfg_param.rpi_used--;
12094 }
12095}
12096
6fb120a7
JS
12097/**
12098 * lpfc_sli4_free_rpi - Release an rpi for reuse.
12099 * @phba: pointer to lpfc hba data structure.
12100 *
12101 * This routine is invoked to release an rpi to the pool of
12102 * available rpis maintained by the driver.
12103 **/
12104void
12105lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
12106{
12107 spin_lock_irq(&phba->hbalock);
d7c47992 12108 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
12109 spin_unlock_irq(&phba->hbalock);
12110}
12111
12112/**
12113 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
12114 * @phba: pointer to lpfc hba data structure.
12115 *
12116 * This routine is invoked to remove the memory region that
12117 * provided rpi via a bitmask.
12118 **/
12119void
12120lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
12121{
12122 kfree(phba->sli4_hba.rpi_bmask);
12123}
12124
12125/**
12126 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
12127 * @phba: pointer to lpfc hba data structure.
12128 *
12129 * This routine is invoked to remove the memory region that
12130 * provided rpi via a bitmask.
12131 **/
12132int
12133lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
12134{
12135 LPFC_MBOXQ_t *mboxq;
12136 struct lpfc_hba *phba = ndlp->phba;
12137 int rc;
12138
12139 /* The port is notified of the header region via a mailbox command. */
12140 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12141 if (!mboxq)
12142 return -ENOMEM;
12143
12144 /* Post all rpi memory regions to the port. */
12145 lpfc_resume_rpi(mboxq, ndlp);
12146 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12147 if (rc == MBX_NOT_FINISHED) {
12148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12149 "2010 Resume RPI Mailbox failed "
12150 "status %d, mbxStatus x%x\n", rc,
12151 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
12152 mempool_free(mboxq, phba->mbox_mem_pool);
12153 return -EIO;
12154 }
12155 return 0;
12156}
12157
12158/**
12159 * lpfc_sli4_init_vpi - Initialize a vpi with the port
12160 * @phba: pointer to lpfc hba data structure.
12161 * @vpi: vpi value to activate with the port.
12162 *
12163 * This routine is invoked to activate a vpi with the
12164 * port when the host intends to use vports with a
12165 * nonzero vpi.
12166 *
12167 * Returns:
12168 * 0 success
12169 * -Evalue otherwise
12170 **/
12171int
12172lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
12173{
12174 LPFC_MBOXQ_t *mboxq;
12175 int rc = 0;
6a9c52cf 12176 int retval = MBX_SUCCESS;
6fb120a7
JS
12177 uint32_t mbox_tmo;
12178
12179 if (vpi == 0)
12180 return -EINVAL;
12181 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12182 if (!mboxq)
12183 return -ENOMEM;
1c6834a7 12184 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
12185 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
12186 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
12187 if (rc != MBX_SUCCESS) {
12188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12189 "2022 INIT VPI Mailbox failed "
12190 "status %d, mbxStatus x%x\n", rc,
12191 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 12192 retval = -EIO;
6fb120a7 12193 }
6a9c52cf
JS
12194 if (rc != MBX_TIMEOUT)
12195 mempool_free(mboxq, phba->mbox_mem_pool);
12196
12197 return retval;
6fb120a7
JS
12198}
12199
12200/**
12201 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
12202 * @phba: pointer to lpfc hba data structure.
12203 * @mboxq: Pointer to mailbox object.
12204 *
12205 * This routine is invoked to manually add a single FCF record. The caller
12206 * must pass a completely initialized FCF_Record. This routine takes
12207 * care of the nonembedded mailbox operations.
12208 **/
12209static void
12210lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
12211{
12212 void *virt_addr;
12213 union lpfc_sli4_cfg_shdr *shdr;
12214 uint32_t shdr_status, shdr_add_status;
12215
12216 virt_addr = mboxq->sge_array->addr[0];
12217 /* The IOCTL status is embedded in the mailbox subheader. */
12218 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
12219 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12220 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12221
12222 if ((shdr_status || shdr_add_status) &&
12223 (shdr_status != STATUS_FCF_IN_USE))
12224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12225 "2558 ADD_FCF_RECORD mailbox failed with "
12226 "status x%x add_status x%x\n",
12227 shdr_status, shdr_add_status);
12228
12229 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12230}
12231
12232/**
12233 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12234 * @phba: pointer to lpfc hba data structure.
12235 * @fcf_record: pointer to the initialized fcf record to add.
12236 *
12237 * This routine is invoked to manually add a single FCF record. The caller
12238 * must pass a completely initialized FCF_Record. This routine takes
12239 * care of the nonembedded mailbox operations.
12240 **/
12241int
12242lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12243{
12244 int rc = 0;
12245 LPFC_MBOXQ_t *mboxq;
12246 uint8_t *bytep;
12247 void *virt_addr;
12248 dma_addr_t phys_addr;
12249 struct lpfc_mbx_sge sge;
12250 uint32_t alloc_len, req_len;
12251 uint32_t fcfindex;
12252
12253 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12254 if (!mboxq) {
12255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12256 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12257 return -ENOMEM;
12258 }
12259
12260 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12261 sizeof(uint32_t);
12262
12263 /* Allocate DMA memory and set up the non-embedded mailbox command */
12264 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12265 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12266 req_len, LPFC_SLI4_MBX_NEMBED);
12267 if (alloc_len < req_len) {
12268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12269 "2523 Allocated DMA memory size (x%x) is "
12270 "less than the requested DMA memory "
12271 "size (x%x)\n", alloc_len, req_len);
12272 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12273 return -ENOMEM;
12274 }
12275
12276 /*
12277 * Get the first SGE entry from the non-embedded DMA memory. This
12278 * routine only uses a single SGE.
12279 */
12280 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12281 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
12282 virt_addr = mboxq->sge_array->addr[0];
12283 /*
12284 * Configure the FCF record for FCFI 0. This is the driver's
12285 * hardcoded default and gets used in nonFIP mode.
12286 */
12287 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12288 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12289 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12290
12291 /*
12292 * Copy the fcf_index and the FCF Record Data. The data starts after
12293 * the FCoE header plus word10. The data copy needs to be endian
12294 * correct.
12295 */
12296 bytep += sizeof(uint32_t);
12297 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12298 mboxq->vport = phba->pport;
12299 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12300 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12301 if (rc == MBX_NOT_FINISHED) {
12302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12303 "2515 ADD_FCF_RECORD mailbox failed with "
12304 "status 0x%x\n", rc);
12305 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12306 rc = -EIO;
12307 } else
12308 rc = 0;
12309
12310 return rc;
12311}
12312
12313/**
12314 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12315 * @phba: pointer to lpfc hba data structure.
12316 * @fcf_record: pointer to the fcf record to write the default data.
12317 * @fcf_index: FCF table entry index.
12318 *
12319 * This routine is invoked to build the driver's default FCF record. The
12320 * values used are hardcoded. This routine handles memory initialization.
12321 *
12322 **/
12323void
12324lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12325 struct fcf_record *fcf_record,
12326 uint16_t fcf_index)
12327{
12328 memset(fcf_record, 0, sizeof(struct fcf_record));
12329 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12330 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12331 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12332 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12333 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12334 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12335 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12336 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12337 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12338 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12339 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12340 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12341 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 12342 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
12343 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12344 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12345 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12346 /* Set the VLAN bit map */
12347 if (phba->valid_vlan) {
12348 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12349 = 1 << (phba->vlan_id % 8);
12350 }
12351}
12352
12353/**
0c9ab6f5 12354 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
12355 * @phba: pointer to lpfc hba data structure.
12356 * @fcf_index: FCF table entry offset.
12357 *
0c9ab6f5
JS
12358 * This routine is invoked to scan the entire FCF table by reading FCF
12359 * record and processing it one at a time starting from the @fcf_index
12360 * for initial FCF discovery or fast FCF failover rediscovery.
12361 *
12362 * Return 0 if the mailbox command is submitted sucessfully, none 0
12363 * otherwise.
6fb120a7
JS
12364 **/
12365int
0c9ab6f5 12366lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
12367{
12368 int rc = 0, error;
12369 LPFC_MBOXQ_t *mboxq;
6fb120a7 12370
32b9793f 12371 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
12372 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12373 if (!mboxq) {
12374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12375 "2000 Failed to allocate mbox for "
12376 "READ_FCF cmd\n");
4d9ab994 12377 error = -ENOMEM;
0c9ab6f5 12378 goto fail_fcf_scan;
6fb120a7 12379 }
ecfd03c6 12380 /* Construct the read FCF record mailbox command */
0c9ab6f5 12381 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
12382 if (rc) {
12383 error = -EINVAL;
0c9ab6f5 12384 goto fail_fcf_scan;
6fb120a7 12385 }
ecfd03c6 12386 /* Issue the mailbox command asynchronously */
6fb120a7 12387 mboxq->vport = phba->pport;
0c9ab6f5 12388 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
12389
12390 spin_lock_irq(&phba->hbalock);
12391 phba->hba_flag |= FCF_TS_INPROG;
12392 spin_unlock_irq(&phba->hbalock);
12393
6fb120a7 12394 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 12395 if (rc == MBX_NOT_FINISHED)
6fb120a7 12396 error = -EIO;
ecfd03c6 12397 else {
38b92ef8
JS
12398 /* Reset eligible FCF count for new scan */
12399 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 12400 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 12401 error = 0;
32b9793f 12402 }
0c9ab6f5 12403fail_fcf_scan:
4d9ab994
JS
12404 if (error) {
12405 if (mboxq)
12406 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 12407 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 12408 spin_lock_irq(&phba->hbalock);
a93ff37a 12409 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
12410 spin_unlock_irq(&phba->hbalock);
12411 }
6fb120a7
JS
12412 return error;
12413}
a0c87cbd 12414
0c9ab6f5 12415/**
a93ff37a 12416 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
12417 * @phba: pointer to lpfc hba data structure.
12418 * @fcf_index: FCF table entry offset.
12419 *
12420 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 12421 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5
JS
12422 *
12423 * Return 0 if the mailbox command is submitted sucessfully, none 0
12424 * otherwise.
12425 **/
12426int
12427lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12428{
12429 int rc = 0, error;
12430 LPFC_MBOXQ_t *mboxq;
12431
12432 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12433 if (!mboxq) {
12434 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12435 "2763 Failed to allocate mbox for "
12436 "READ_FCF cmd\n");
12437 error = -ENOMEM;
12438 goto fail_fcf_read;
12439 }
12440 /* Construct the read FCF record mailbox command */
12441 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12442 if (rc) {
12443 error = -EINVAL;
12444 goto fail_fcf_read;
12445 }
12446 /* Issue the mailbox command asynchronously */
12447 mboxq->vport = phba->pport;
12448 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12449 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12450 if (rc == MBX_NOT_FINISHED)
12451 error = -EIO;
12452 else
12453 error = 0;
12454
12455fail_fcf_read:
12456 if (error && mboxq)
12457 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12458 return error;
12459}
12460
12461/**
12462 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12463 * @phba: pointer to lpfc hba data structure.
12464 * @fcf_index: FCF table entry offset.
12465 *
12466 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 12467 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5
JS
12468 *
12469 * Return 0 if the mailbox command is submitted sucessfully, none 0
12470 * otherwise.
12471 **/
12472int
12473lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12474{
12475 int rc = 0, error;
12476 LPFC_MBOXQ_t *mboxq;
12477
12478 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12479 if (!mboxq) {
12480 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12481 "2758 Failed to allocate mbox for "
12482 "READ_FCF cmd\n");
12483 error = -ENOMEM;
12484 goto fail_fcf_read;
12485 }
12486 /* Construct the read FCF record mailbox command */
12487 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12488 if (rc) {
12489 error = -EINVAL;
12490 goto fail_fcf_read;
12491 }
12492 /* Issue the mailbox command asynchronously */
12493 mboxq->vport = phba->pport;
12494 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12495 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12496 if (rc == MBX_NOT_FINISHED)
12497 error = -EIO;
12498 else
12499 error = 0;
12500
12501fail_fcf_read:
12502 if (error && mboxq)
12503 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12504 return error;
12505}
12506
12507/**
12508 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12509 * @phba: pointer to lpfc hba data structure.
12510 *
12511 * This routine is to get the next eligible FCF record index in a round
12512 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 12513 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
12514 * shall be returned, otherwise, the next eligible FCF record's index
12515 * shall be returned.
12516 **/
12517uint16_t
12518lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12519{
12520 uint16_t next_fcf_index;
12521
3804dc84
JS
12522 /* Search start from next bit of currently registered FCF index */
12523 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
12524 LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
12525 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12526 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
12527 next_fcf_index);
12528
0c9ab6f5
JS
12529 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12530 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12531 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12532 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
3804dc84
JS
12533
12534 /* Check roundrobin failover list empty condition */
12535 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12536 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
12537 "2844 No roundrobin failover FCF available\n");
0c9ab6f5 12538 return LPFC_FCOE_FCF_NEXT_NONE;
3804dc84
JS
12539 }
12540
3804dc84 12541 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
12542 "2845 Get next roundrobin failover FCF (x%x)\n",
12543 next_fcf_index);
12544
0c9ab6f5
JS
12545 return next_fcf_index;
12546}
12547
12548/**
12549 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12550 * @phba: pointer to lpfc hba data structure.
12551 *
12552 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 12553 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
12554 * does not go beyond the range of the driver allocated bmask dimension
12555 * before setting the bit.
12556 *
12557 * Returns 0 if the index bit successfully set, otherwise, it returns
12558 * -EINVAL.
12559 **/
12560int
12561lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12562{
12563 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12564 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
12565 "2610 FCF (x%x) reached driver's book "
12566 "keeping dimension:x%x\n",
0c9ab6f5
JS
12567 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12568 return -EINVAL;
12569 }
12570 /* Set the eligible FCF record index bmask */
12571 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12572
3804dc84 12573 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 12574 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
12575 "bmask\n", fcf_index);
12576
0c9ab6f5
JS
12577 return 0;
12578}
12579
12580/**
3804dc84 12581 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
12582 * @phba: pointer to lpfc hba data structure.
12583 *
12584 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 12585 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
12586 * does not go beyond the range of the driver allocated bmask dimension
12587 * before clearing the bit.
12588 **/
12589void
12590lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12591{
12592 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12593 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
12594 "2762 FCF (x%x) reached driver's book "
12595 "keeping dimension:x%x\n",
0c9ab6f5
JS
12596 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12597 return;
12598 }
12599 /* Clear the eligible FCF record index bmask */
12600 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
12601
12602 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 12603 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 12604 "bmask\n", fcf_index);
0c9ab6f5
JS
12605}
12606
ecfd03c6
JS
12607/**
12608 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12609 * @phba: pointer to lpfc hba data structure.
12610 *
12611 * This routine is the completion routine for the rediscover FCF table mailbox
12612 * command. If the mailbox command returned failure, it will try to stop the
12613 * FCF rediscover wait timer.
12614 **/
12615void
12616lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12617{
12618 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12619 uint32_t shdr_status, shdr_add_status;
12620
12621 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12622
12623 shdr_status = bf_get(lpfc_mbox_hdr_status,
12624 &redisc_fcf->header.cfg_shdr.response);
12625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12626 &redisc_fcf->header.cfg_shdr.response);
12627 if (shdr_status || shdr_add_status) {
0c9ab6f5 12628 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
12629 "2746 Requesting for FCF rediscovery failed "
12630 "status x%x add_status x%x\n",
12631 shdr_status, shdr_add_status);
0c9ab6f5 12632 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 12633 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12634 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
12635 spin_unlock_irq(&phba->hbalock);
12636 /*
12637 * CVL event triggered FCF rediscover request failed,
12638 * last resort to re-try current registered FCF entry.
12639 */
12640 lpfc_retry_pport_discovery(phba);
12641 } else {
12642 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12643 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
12644 spin_unlock_irq(&phba->hbalock);
12645 /*
12646 * DEAD FCF event triggered FCF rediscover request
12647 * failed, last resort to fail over as a link down
12648 * to FCF registration.
12649 */
12650 lpfc_sli4_fcf_dead_failthrough(phba);
12651 }
0c9ab6f5
JS
12652 } else {
12653 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 12654 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
12655 /*
12656 * Start FCF rediscovery wait timer for pending FCF
12657 * before rescan FCF record table.
12658 */
12659 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 12660 }
ecfd03c6
JS
12661
12662 mempool_free(mbox, phba->mbox_mem_pool);
12663}
12664
12665/**
3804dc84 12666 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
12667 * @phba: pointer to lpfc hba data structure.
12668 *
12669 * This routine is invoked to request for rediscovery of the entire FCF table
12670 * by the port.
12671 **/
12672int
12673lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12674{
12675 LPFC_MBOXQ_t *mbox;
12676 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12677 int rc, length;
12678
0c9ab6f5
JS
12679 /* Cancel retry delay timers to all vports before FCF rediscover */
12680 lpfc_cancel_all_vport_retry_delay_timer(phba);
12681
ecfd03c6
JS
12682 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12683 if (!mbox) {
12684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12685 "2745 Failed to allocate mbox for "
12686 "requesting FCF rediscover.\n");
12687 return -ENOMEM;
12688 }
12689
12690 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12691 sizeof(struct lpfc_sli4_cfg_mhdr));
12692 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12693 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12694 length, LPFC_SLI4_MBX_EMBED);
12695
12696 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12697 /* Set count to 0 for invalidating the entire FCF database */
12698 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12699
12700 /* Issue the mailbox command asynchronously */
12701 mbox->vport = phba->pport;
12702 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12703 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12704
12705 if (rc == MBX_NOT_FINISHED) {
12706 mempool_free(mbox, phba->mbox_mem_pool);
12707 return -EIO;
12708 }
12709 return 0;
12710}
12711
fc2b989b
JS
12712/**
12713 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12714 * @phba: pointer to lpfc hba data structure.
12715 *
12716 * This function is the failover routine as a last resort to the FCF DEAD
12717 * event when driver failed to perform fast FCF failover.
12718 **/
12719void
12720lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12721{
12722 uint32_t link_state;
12723
12724 /*
12725 * Last resort as FCF DEAD event failover will treat this as
12726 * a link down, but save the link state because we don't want
12727 * it to be changed to Link Down unless it is already down.
12728 */
12729 link_state = phba->link_state;
12730 lpfc_linkdown(phba);
12731 phba->link_state = link_state;
12732
12733 /* Unregister FCF if no devices connected to it */
12734 lpfc_unregister_unused_fcf(phba);
12735}
12736
a0c87cbd
JS
12737/**
12738 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12739 * @phba: pointer to lpfc hba data structure.
12740 *
12741 * This function read region 23 and parse TLV for port status to
12742 * decide if the user disaled the port. If the TLV indicates the
12743 * port is disabled, the hba_flag is set accordingly.
12744 **/
12745void
12746lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12747{
12748 LPFC_MBOXQ_t *pmb = NULL;
12749 MAILBOX_t *mb;
12750 uint8_t *rgn23_data = NULL;
12751 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12752 int rc;
12753
12754 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12755 if (!pmb) {
12756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12757 "2600 lpfc_sli_read_serdes_param failed to"
12758 " allocate mailbox memory\n");
12759 goto out;
12760 }
12761 mb = &pmb->u.mb;
12762
12763 /* Get adapter Region 23 data */
12764 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12765 if (!rgn23_data)
12766 goto out;
12767
12768 do {
12769 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12770 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12771
12772 if (rc != MBX_SUCCESS) {
12773 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12774 "2601 lpfc_sli_read_link_ste failed to"
12775 " read config region 23 rc 0x%x Status 0x%x\n",
12776 rc, mb->mbxStatus);
12777 mb->un.varDmp.word_cnt = 0;
12778 }
12779 /*
12780 * dump mem may return a zero when finished or we got a
12781 * mailbox error, either way we are done.
12782 */
12783 if (mb->un.varDmp.word_cnt == 0)
12784 break;
12785 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12786 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12787
12788 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12789 rgn23_data + offset,
12790 mb->un.varDmp.word_cnt);
12791 offset += mb->un.varDmp.word_cnt;
12792 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12793
12794 data_size = offset;
12795 offset = 0;
12796
12797 if (!data_size)
12798 goto out;
12799
12800 /* Check the region signature first */
12801 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12803 "2619 Config region 23 has bad signature\n");
12804 goto out;
12805 }
12806 offset += 4;
12807
12808 /* Check the data structure version */
12809 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12811 "2620 Config region 23 has bad version\n");
12812 goto out;
12813 }
12814 offset += 4;
12815
12816 /* Parse TLV entries in the region */
12817 while (offset < data_size) {
12818 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12819 break;
12820 /*
12821 * If the TLV is not driver specific TLV or driver id is
12822 * not linux driver id, skip the record.
12823 */
12824 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12825 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12826 (rgn23_data[offset + 3] != 0)) {
12827 offset += rgn23_data[offset + 1] * 4 + 4;
12828 continue;
12829 }
12830
12831 /* Driver found a driver specific TLV in the config region */
12832 sub_tlv_len = rgn23_data[offset + 1] * 4;
12833 offset += 4;
12834 tlv_offset = 0;
12835
12836 /*
12837 * Search for configured port state sub-TLV.
12838 */
12839 while ((offset < data_size) &&
12840 (tlv_offset < sub_tlv_len)) {
12841 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12842 offset += 4;
12843 tlv_offset += 4;
12844 break;
12845 }
12846 if (rgn23_data[offset] != PORT_STE_TYPE) {
12847 offset += rgn23_data[offset + 1] * 4 + 4;
12848 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12849 continue;
12850 }
12851
12852 /* This HBA contains PORT_STE configured */
12853 if (!rgn23_data[offset + 2])
12854 phba->hba_flag |= LINK_DISABLED;
12855
12856 goto out;
12857 }
12858 }
12859out:
12860 if (pmb)
12861 mempool_free(pmb, phba->mbox_mem_pool);
12862 kfree(rgn23_data);
12863 return;
12864}
695a814e
JS
12865
12866/**
12867 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12868 * @vport: pointer to vport data structure.
12869 *
12870 * This function iterate through the mailboxq and clean up all REG_LOGIN
12871 * and REG_VPI mailbox commands associated with the vport. This function
12872 * is called when driver want to restart discovery of the vport due to
12873 * a Clear Virtual Link event.
12874 **/
12875void
12876lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12877{
12878 struct lpfc_hba *phba = vport->phba;
12879 LPFC_MBOXQ_t *mb, *nextmb;
12880 struct lpfc_dmabuf *mp;
78730cfe 12881 struct lpfc_nodelist *ndlp;
d439d286 12882 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 12883 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 12884 LIST_HEAD(mbox_cmd_list);
63e801ce 12885 uint8_t restart_loop;
695a814e 12886
d439d286 12887 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
12888 spin_lock_irq(&phba->hbalock);
12889 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12890 if (mb->vport != vport)
12891 continue;
12892
12893 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12894 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12895 continue;
12896
d439d286
JS
12897 list_del(&mb->list);
12898 list_add_tail(&mb->list, &mbox_cmd_list);
12899 }
12900 /* Clean up active mailbox command with the vport */
12901 mb = phba->sli.mbox_active;
12902 if (mb && (mb->vport == vport)) {
12903 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12904 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12905 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12906 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12907 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
12908 /* Put reference count for delayed processing */
12909 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
12910 /* Unregister the RPI when mailbox complete */
12911 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12912 }
12913 }
63e801ce
JS
12914 /* Cleanup any mailbox completions which are not yet processed */
12915 do {
12916 restart_loop = 0;
12917 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
12918 /*
12919 * If this mailox is already processed or it is
12920 * for another vport ignore it.
12921 */
12922 if ((mb->vport != vport) ||
12923 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
12924 continue;
12925
12926 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12927 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12928 continue;
12929
12930 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12931 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12932 ndlp = (struct lpfc_nodelist *)mb->context2;
12933 /* Unregister the RPI when mailbox complete */
12934 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
12935 restart_loop = 1;
12936 spin_unlock_irq(&phba->hbalock);
12937 spin_lock(shost->host_lock);
12938 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12939 spin_unlock(shost->host_lock);
12940 spin_lock_irq(&phba->hbalock);
12941 break;
12942 }
12943 }
12944 } while (restart_loop);
12945
d439d286
JS
12946 spin_unlock_irq(&phba->hbalock);
12947
12948 /* Release the cleaned-up mailbox commands */
12949 while (!list_empty(&mbox_cmd_list)) {
12950 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e 12951 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
d7c47992
JS
12952 if (phba->sli_rev == LPFC_SLI_REV4)
12953 __lpfc_sli4_free_rpi(phba,
12954 mb->u.mb.un.varRegLogin.rpi);
695a814e
JS
12955 mp = (struct lpfc_dmabuf *) (mb->context1);
12956 if (mp) {
12957 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12958 kfree(mp);
12959 }
78730cfe 12960 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 12961 mb->context2 = NULL;
78730cfe 12962 if (ndlp) {
ec21b3b0 12963 spin_lock(shost->host_lock);
589a52d6 12964 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 12965 spin_unlock(shost->host_lock);
78730cfe 12966 lpfc_nlp_put(ndlp);
78730cfe 12967 }
695a814e 12968 }
695a814e
JS
12969 mempool_free(mb, phba->mbox_mem_pool);
12970 }
d439d286
JS
12971
12972 /* Release the ndlp with the cleaned-up active mailbox command */
12973 if (act_mbx_ndlp) {
12974 spin_lock(shost->host_lock);
12975 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
12976 spin_unlock(shost->host_lock);
12977 lpfc_nlp_put(act_mbx_ndlp);
695a814e 12978 }
695a814e
JS
12979}
12980
2a9bf3d0
JS
12981/**
12982 * lpfc_drain_txq - Drain the txq
12983 * @phba: Pointer to HBA context object.
12984 *
12985 * This function attempt to submit IOCBs on the txq
12986 * to the adapter. For SLI4 adapters, the txq contains
12987 * ELS IOCBs that have been deferred because the there
12988 * are no SGLs. This congestion can occur with large
12989 * vport counts during node discovery.
12990 **/
12991
12992uint32_t
12993lpfc_drain_txq(struct lpfc_hba *phba)
12994{
12995 LIST_HEAD(completions);
12996 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
12997 struct lpfc_iocbq *piocbq = 0;
12998 unsigned long iflags = 0;
12999 char *fail_msg = NULL;
13000 struct lpfc_sglq *sglq;
13001 union lpfc_wqe wqe;
13002
13003 spin_lock_irqsave(&phba->hbalock, iflags);
13004 if (pring->txq_cnt > pring->txq_max)
13005 pring->txq_max = pring->txq_cnt;
13006
13007 spin_unlock_irqrestore(&phba->hbalock, iflags);
13008
13009 while (pring->txq_cnt) {
13010 spin_lock_irqsave(&phba->hbalock, iflags);
13011
13012 sglq = __lpfc_sli_get_sglq(phba);
13013 if (!sglq) {
13014 spin_unlock_irqrestore(&phba->hbalock, iflags);
13015 break;
13016 } else {
13017 piocbq = lpfc_sli_ringtx_get(phba, pring);
13018 if (!piocbq) {
13019 /* The txq_cnt out of sync. This should
13020 * never happen
13021 */
13022 sglq = __lpfc_clear_active_sglq(phba,
13023 sglq->sli4_xritag);
13024 spin_unlock_irqrestore(&phba->hbalock, iflags);
13025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13026 "2823 txq empty and txq_cnt is %d\n ",
13027 pring->txq_cnt);
13028 break;
13029 }
13030 }
13031
13032 /* The xri and iocb resources secured,
13033 * attempt to issue request
13034 */
13035 piocbq->sli4_xritag = sglq->sli4_xritag;
13036 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
13037 fail_msg = "to convert bpl to sgl";
13038 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
13039 fail_msg = "to convert iocb to wqe";
13040 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
13041 fail_msg = " - Wq is full";
13042 else
13043 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
13044
13045 if (fail_msg) {
13046 /* Failed means we can't issue and need to cancel */
13047 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13048 "2822 IOCB failed %s iotag 0x%x "
13049 "xri 0x%x\n",
13050 fail_msg,
13051 piocbq->iotag, piocbq->sli4_xritag);
13052 list_add_tail(&piocbq->list, &completions);
13053 }
13054 spin_unlock_irqrestore(&phba->hbalock, iflags);
13055 }
13056
2a9bf3d0
JS
13057 /* Cancel all the IOCBs that cannot be issued */
13058 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
13059 IOERR_SLI_ABORTED);
13060
13061 return pring->txq_cnt;
13062}