]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.3.12: T10-PI/DIF changes
[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))
98 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
99
100 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
101
102 /* Update the host index before invoking device */
103 host_index = q->host_index;
104 q->host_index = ((q->host_index + 1) % q->entry_count);
105
106 /* Ring Doorbell */
107 doorbell.word0 = 0;
108 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
109 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
110 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
111 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
112 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
113
114 return 0;
115}
116
117/**
118 * lpfc_sli4_wq_release - Updates internal hba index for WQ
119 * @q: The Work Queue to operate on.
120 * @index: The index to advance the hba index to.
121 *
122 * This routine will update the HBA index of a queue to reflect consumption of
123 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
124 * an entry the host calls this function to update the queue's internal
125 * pointers. This routine returns the number of entries that were consumed by
126 * the HBA.
127 **/
128static uint32_t
129lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
130{
131 uint32_t released = 0;
132
133 if (q->hba_index == index)
134 return 0;
135 do {
136 q->hba_index = ((q->hba_index + 1) % q->entry_count);
137 released++;
138 } while (q->hba_index != index);
139 return released;
140}
141
142/**
143 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
144 * @q: The Mailbox Queue to operate on.
145 * @wqe: The Mailbox Queue Entry to put on the Work queue.
146 *
147 * This routine will copy the contents of @mqe to the next available entry on
148 * the @q. This function will then ring the Work Queue Doorbell to signal the
149 * HBA to start processing the Work Queue Entry. This function returns 0 if
150 * successful. If no entries are available on @q then this function will return
151 * -ENOMEM.
152 * The caller is expected to hold the hbalock when calling this routine.
153 **/
154static uint32_t
155lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
156{
157 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
158 struct lpfc_register doorbell;
159 uint32_t host_index;
160
161 /* If the host has not yet processed the next entry then we are done */
162 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
163 return -ENOMEM;
164 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
165 /* Save off the mailbox pointer for completion */
166 q->phba->mbox = (MAILBOX_t *)temp_mqe;
167
168 /* Update the host index before invoking device */
169 host_index = q->host_index;
170 q->host_index = ((q->host_index + 1) % q->entry_count);
171
172 /* Ring Doorbell */
173 doorbell.word0 = 0;
174 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
175 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
176 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
177 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
178 return 0;
179}
180
181/**
182 * lpfc_sli4_mq_release - Updates internal hba index for MQ
183 * @q: The Mailbox Queue to operate on.
184 *
185 * This routine will update the HBA index of a queue to reflect consumption of
186 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
187 * an entry the host calls this function to update the queue's internal
188 * pointers. This routine returns the number of entries that were consumed by
189 * the HBA.
190 **/
191static uint32_t
192lpfc_sli4_mq_release(struct lpfc_queue *q)
193{
194 /* Clear the mailbox pointer for completion */
195 q->phba->mbox = NULL;
196 q->hba_index = ((q->hba_index + 1) % q->entry_count);
197 return 1;
198}
199
200/**
201 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
202 * @q: The Event Queue to get the first valid EQE from
203 *
204 * This routine will get the first valid Event Queue Entry from @q, update
205 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
206 * the Queue (no more work to do), or the Queue is full of EQEs that have been
207 * processed, but not popped back to the HBA then this routine will return NULL.
208 **/
209static struct lpfc_eqe *
210lpfc_sli4_eq_get(struct lpfc_queue *q)
211{
212 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
213
214 /* If the next EQE is not valid then we are done */
cb5172ea 215 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
216 return NULL;
217 /* If the host has not yet processed the next entry then we are done */
218 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
219 return NULL;
220
221 q->hba_index = ((q->hba_index + 1) % q->entry_count);
222 return eqe;
223}
224
225/**
226 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
227 * @q: The Event Queue that the host has completed processing for.
228 * @arm: Indicates whether the host wants to arms this CQ.
229 *
230 * This routine will mark all Event Queue Entries on @q, from the last
231 * known completed entry to the last entry that was processed, as completed
232 * by clearing the valid bit for each completion queue entry. Then it will
233 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
234 * The internal host index in the @q will be updated by this routine to indicate
235 * that the host has finished processing the entries. The @arm parameter
236 * indicates that the queue should be rearmed when ringing the doorbell.
237 *
238 * This function will return the number of EQEs that were popped.
239 **/
240uint32_t
241lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
242{
243 uint32_t released = 0;
244 struct lpfc_eqe *temp_eqe;
245 struct lpfc_register doorbell;
246
247 /* while there are valid entries */
248 while (q->hba_index != q->host_index) {
249 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 250 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
251 released++;
252 q->host_index = ((q->host_index + 1) % q->entry_count);
253 }
254 if (unlikely(released == 0 && !arm))
255 return 0;
256
257 /* ring doorbell for number popped */
258 doorbell.word0 = 0;
259 if (arm) {
260 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
261 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
262 }
263 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
264 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
265 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
266 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
267 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
268 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
269 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
270 return released;
271}
272
273/**
274 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
275 * @q: The Completion Queue to get the first valid CQE from
276 *
277 * This routine will get the first valid Completion Queue Entry from @q, update
278 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
279 * the Queue (no more work to do), or the Queue is full of CQEs that have been
280 * processed, but not popped back to the HBA then this routine will return NULL.
281 **/
282static struct lpfc_cqe *
283lpfc_sli4_cq_get(struct lpfc_queue *q)
284{
285 struct lpfc_cqe *cqe;
286
287 /* If the next CQE is not valid then we are done */
cb5172ea 288 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
289 return NULL;
290 /* If the host has not yet processed the next entry then we are done */
291 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
292 return NULL;
293
294 cqe = q->qe[q->hba_index].cqe;
295 q->hba_index = ((q->hba_index + 1) % q->entry_count);
296 return cqe;
297}
298
299/**
300 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
301 * @q: The Completion Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Completion queue entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of CQEs that were released.
313 **/
314uint32_t
315lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
316{
317 uint32_t released = 0;
318 struct lpfc_cqe *temp_qe;
319 struct lpfc_register doorbell;
320
321 /* while there are valid entries */
322 while (q->hba_index != q->host_index) {
323 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 324 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
325 released++;
326 q->host_index = ((q->host_index + 1) % q->entry_count);
327 }
328 if (unlikely(released == 0 && !arm))
329 return 0;
330
331 /* ring doorbell for number popped */
332 doorbell.word0 = 0;
333 if (arm)
334 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
335 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
336 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
337 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
338 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340}
341
342/**
343 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
344 * @q: The Header Receive Queue to operate on.
345 * @wqe: The Receive Queue Entry to put on the Receive queue.
346 *
347 * This routine will copy the contents of @wqe to the next available entry on
348 * the @q. This function will then ring the Receive Queue Doorbell to signal the
349 * HBA to start processing the Receive Queue Entry. This function returns the
350 * index that the rqe was copied to if successful. If no entries are available
351 * on @q then this function will return -ENOMEM.
352 * The caller is expected to hold the hbalock when calling this routine.
353 **/
354static int
355lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
356 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
357{
358 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
359 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
360 struct lpfc_register doorbell;
361 int put_index = hq->host_index;
362
363 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
364 return -EINVAL;
365 if (hq->host_index != dq->host_index)
366 return -EINVAL;
367 /* If the host has not yet processed the next entry then we are done */
368 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
369 return -EBUSY;
370 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
371 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
372
373 /* Update the host index to point to the next slot */
374 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
375 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
376
377 /* Ring The Header Receive Queue Doorbell */
378 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
379 doorbell.word0 = 0;
380 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
381 LPFC_RQ_POST_BATCH);
382 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
383 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
384 }
385 return put_index;
386}
387
388/**
389 * lpfc_sli4_rq_release - Updates internal hba index for RQ
390 * @q: The Header Receive Queue to operate on.
391 *
392 * This routine will update the HBA index of a queue to reflect consumption of
393 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
394 * consumed an entry the host calls this function to update the queue's
395 * internal pointers. This routine returns the number of entries that were
396 * consumed by the HBA.
397 **/
398static uint32_t
399lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
400{
401 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
402 return 0;
403 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
404 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
405 return 1;
406}
407
e59058c4 408/**
3621a710 409 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
410 * @phba: Pointer to HBA context object.
411 * @pring: Pointer to driver SLI ring object.
412 *
413 * This function returns pointer to next command iocb entry
414 * in the command ring. The caller must hold hbalock to prevent
415 * other threads consume the next command iocb.
416 * SLI-2/SLI-3 provide different sized iocbs.
417 **/
ed957684
JS
418static inline IOCB_t *
419lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
420{
421 return (IOCB_t *) (((char *) pring->cmdringaddr) +
422 pring->cmdidx * phba->iocb_cmd_size);
423}
424
e59058c4 425/**
3621a710 426 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
427 * @phba: Pointer to HBA context object.
428 * @pring: Pointer to driver SLI ring object.
429 *
430 * This function returns pointer to next response iocb entry
431 * in the response ring. The caller must hold hbalock to make sure
432 * that no other thread consume the next response iocb.
433 * SLI-2/SLI-3 provide different sized iocbs.
434 **/
ed957684
JS
435static inline IOCB_t *
436lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
437{
438 return (IOCB_t *) (((char *) pring->rspringaddr) +
439 pring->rspidx * phba->iocb_rsp_size);
440}
441
e59058c4 442/**
3621a710 443 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
444 * @phba: Pointer to HBA context object.
445 *
446 * This function is called with hbalock held. This function
447 * allocates a new driver iocb object from the iocb pool. If the
448 * allocation is successful, it returns pointer to the newly
449 * allocated iocb object else it returns NULL.
450 **/
2e0fef85
JS
451static struct lpfc_iocbq *
452__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
453{
454 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
455 struct lpfc_iocbq * iocbq = NULL;
456
457 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
458 return iocbq;
459}
460
da0436e9
JS
461/**
462 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
463 * @phba: Pointer to HBA context object.
464 * @xritag: XRI value.
465 *
466 * This function clears the sglq pointer from the array of acive
467 * sglq's. The xritag that is passed in is used to index into the
468 * array. Before the xritag can be used it needs to be adjusted
469 * by subtracting the xribase.
470 *
471 * Returns sglq ponter = success, NULL = Failure.
472 **/
473static struct lpfc_sglq *
474__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
475{
476 uint16_t adj_xri;
477 struct lpfc_sglq *sglq;
478 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
479 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
480 return NULL;
481 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
482 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
483 return sglq;
484}
485
486/**
487 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
488 * @phba: Pointer to HBA context object.
489 * @xritag: XRI value.
490 *
491 * This function returns the sglq pointer from the array of acive
492 * sglq's. The xritag that is passed in is used to index into the
493 * array. Before the xritag can be used it needs to be adjusted
494 * by subtracting the xribase.
495 *
496 * Returns sglq ponter = success, NULL = Failure.
497 **/
0f65ff68 498struct lpfc_sglq *
da0436e9
JS
499__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
500{
501 uint16_t adj_xri;
502 struct lpfc_sglq *sglq;
503 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
504 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
505 return NULL;
506 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
507 return sglq;
508}
509
510/**
511 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
512 * @phba: Pointer to HBA context object.
513 *
514 * This function is called with hbalock held. This function
515 * Gets a new driver sglq object from the sglq list. If the
516 * list is not empty then it is successful, it returns pointer to the newly
517 * allocated sglq object else it returns NULL.
518 **/
519static struct lpfc_sglq *
520__lpfc_sli_get_sglq(struct lpfc_hba *phba)
521{
522 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
523 struct lpfc_sglq *sglq = NULL;
524 uint16_t adj_xri;
525 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
6a9c52cf
JS
526 if (!sglq)
527 return NULL;
da0436e9
JS
528 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
529 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
0f65ff68 530 sglq->state = SGL_ALLOCATED;
da0436e9
JS
531 return sglq;
532}
533
e59058c4 534/**
3621a710 535 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
536 * @phba: Pointer to HBA context object.
537 *
538 * This function is called with no lock held. This function
539 * allocates a new driver iocb object from the iocb pool. If the
540 * allocation is successful, it returns pointer to the newly
541 * allocated iocb object else it returns NULL.
542 **/
2e0fef85
JS
543struct lpfc_iocbq *
544lpfc_sli_get_iocbq(struct lpfc_hba *phba)
545{
546 struct lpfc_iocbq * iocbq = NULL;
547 unsigned long iflags;
548
549 spin_lock_irqsave(&phba->hbalock, iflags);
550 iocbq = __lpfc_sli_get_iocbq(phba);
551 spin_unlock_irqrestore(&phba->hbalock, iflags);
552 return iocbq;
553}
554
4f774513
JS
555/**
556 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
557 * @phba: Pointer to HBA context object.
558 * @iocbq: Pointer to driver iocb object.
559 *
560 * This function is called with hbalock held to release driver
561 * iocb object to the iocb pool. The iotag in the iocb object
562 * does not change for each use of the iocb object. This function
563 * clears all other fields of the iocb object when it is freed.
564 * The sqlq structure that holds the xritag and phys and virtual
565 * mappings for the scatter gather list is retrieved from the
566 * active array of sglq. The get of the sglq pointer also clears
567 * the entry in the array. If the status of the IO indiactes that
568 * this IO was aborted then the sglq entry it put on the
569 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
570 * IO has good status or fails for any other reason then the sglq
571 * entry is added to the free list (lpfc_sgl_list).
572 **/
573static void
574__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
575{
576 struct lpfc_sglq *sglq;
577 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
578 unsigned long iflag;
579
580 if (iocbq->sli4_xritag == NO_XRI)
581 sglq = NULL;
582 else
583 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
584 if (sglq) {
0f65ff68
JS
585 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
586 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
587 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
588 iflag);
589 list_add(&sglq->list,
590 &phba->sli4_hba.lpfc_abts_els_sgl_list);
591 spin_unlock_irqrestore(
592 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
593 } else {
594 sglq->state = SGL_FREED;
4f774513 595 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
0f65ff68 596 }
4f774513
JS
597 }
598
599
600 /*
601 * Clean all volatile data fields, preserve iotag and node struct.
602 */
603 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
604 iocbq->sli4_xritag = NO_XRI;
605 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
606}
607
e59058c4 608/**
3772a991 609 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
610 * @phba: Pointer to HBA context object.
611 * @iocbq: Pointer to driver iocb object.
612 *
613 * This function is called with hbalock held to release driver
614 * iocb object to the iocb pool. The iotag in the iocb object
615 * does not change for each use of the iocb object. This function
616 * clears all other fields of the iocb object when it is freed.
617 **/
a6ababd2 618static void
3772a991 619__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 620{
2e0fef85 621 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
622
623 /*
624 * Clean all volatile data fields, preserve iotag and node struct.
625 */
626 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 627 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
628 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
629}
630
3772a991
JS
631/**
632 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
633 * @phba: Pointer to HBA context object.
634 * @iocbq: Pointer to driver iocb object.
635 *
636 * This function is called with hbalock held to release driver
637 * iocb object to the iocb pool. The iotag in the iocb object
638 * does not change for each use of the iocb object. This function
639 * clears all other fields of the iocb object when it is freed.
640 **/
641static void
642__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
643{
644 phba->__lpfc_sli_release_iocbq(phba, iocbq);
645}
646
e59058c4 647/**
3621a710 648 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
649 * @phba: Pointer to HBA context object.
650 * @iocbq: Pointer to driver iocb object.
651 *
652 * This function is called with no lock held to release the iocb to
653 * iocb pool.
654 **/
2e0fef85
JS
655void
656lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
657{
658 unsigned long iflags;
659
660 /*
661 * Clean all volatile data fields, preserve iotag and node struct.
662 */
663 spin_lock_irqsave(&phba->hbalock, iflags);
664 __lpfc_sli_release_iocbq(phba, iocbq);
665 spin_unlock_irqrestore(&phba->hbalock, iflags);
666}
667
a257bf90
JS
668/**
669 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
670 * @phba: Pointer to HBA context object.
671 * @iocblist: List of IOCBs.
672 * @ulpstatus: ULP status in IOCB command field.
673 * @ulpWord4: ULP word-4 in IOCB command field.
674 *
675 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
676 * on the list by invoking the complete callback function associated with the
677 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
678 * fields.
679 **/
680void
681lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
682 uint32_t ulpstatus, uint32_t ulpWord4)
683{
684 struct lpfc_iocbq *piocb;
685
686 while (!list_empty(iocblist)) {
687 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
688
689 if (!piocb->iocb_cmpl)
690 lpfc_sli_release_iocbq(phba, piocb);
691 else {
692 piocb->iocb.ulpStatus = ulpstatus;
693 piocb->iocb.un.ulpWord[4] = ulpWord4;
694 (piocb->iocb_cmpl) (phba, piocb, piocb);
695 }
696 }
697 return;
698}
699
e59058c4 700/**
3621a710
JS
701 * lpfc_sli_iocb_cmd_type - Get the iocb type
702 * @iocb_cmnd: iocb command code.
e59058c4
JS
703 *
704 * This function is called by ring event handler function to get the iocb type.
705 * This function translates the iocb command to an iocb command type used to
706 * decide the final disposition of each completed IOCB.
707 * The function returns
708 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
709 * LPFC_SOL_IOCB if it is a solicited iocb completion
710 * LPFC_ABORT_IOCB if it is an abort iocb
711 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
712 *
713 * The caller is not required to hold any lock.
714 **/
dea3101e
JB
715static lpfc_iocb_type
716lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
717{
718 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
719
720 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
721 return 0;
722
723 switch (iocb_cmnd) {
724 case CMD_XMIT_SEQUENCE_CR:
725 case CMD_XMIT_SEQUENCE_CX:
726 case CMD_XMIT_BCAST_CN:
727 case CMD_XMIT_BCAST_CX:
728 case CMD_ELS_REQUEST_CR:
729 case CMD_ELS_REQUEST_CX:
730 case CMD_CREATE_XRI_CR:
731 case CMD_CREATE_XRI_CX:
732 case CMD_GET_RPI_CN:
733 case CMD_XMIT_ELS_RSP_CX:
734 case CMD_GET_RPI_CR:
735 case CMD_FCP_IWRITE_CR:
736 case CMD_FCP_IWRITE_CX:
737 case CMD_FCP_IREAD_CR:
738 case CMD_FCP_IREAD_CX:
739 case CMD_FCP_ICMND_CR:
740 case CMD_FCP_ICMND_CX:
f5603511
JS
741 case CMD_FCP_TSEND_CX:
742 case CMD_FCP_TRSP_CX:
743 case CMD_FCP_TRECEIVE_CX:
744 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
745 case CMD_ADAPTER_MSG:
746 case CMD_ADAPTER_DUMP:
747 case CMD_XMIT_SEQUENCE64_CR:
748 case CMD_XMIT_SEQUENCE64_CX:
749 case CMD_XMIT_BCAST64_CN:
750 case CMD_XMIT_BCAST64_CX:
751 case CMD_ELS_REQUEST64_CR:
752 case CMD_ELS_REQUEST64_CX:
753 case CMD_FCP_IWRITE64_CR:
754 case CMD_FCP_IWRITE64_CX:
755 case CMD_FCP_IREAD64_CR:
756 case CMD_FCP_IREAD64_CX:
757 case CMD_FCP_ICMND64_CR:
758 case CMD_FCP_ICMND64_CX:
f5603511
JS
759 case CMD_FCP_TSEND64_CX:
760 case CMD_FCP_TRSP64_CX:
761 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
762 case CMD_GEN_REQUEST64_CR:
763 case CMD_GEN_REQUEST64_CX:
764 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
765 case DSSCMD_IWRITE64_CR:
766 case DSSCMD_IWRITE64_CX:
767 case DSSCMD_IREAD64_CR:
768 case DSSCMD_IREAD64_CX:
dea3101e
JB
769 type = LPFC_SOL_IOCB;
770 break;
771 case CMD_ABORT_XRI_CN:
772 case CMD_ABORT_XRI_CX:
773 case CMD_CLOSE_XRI_CN:
774 case CMD_CLOSE_XRI_CX:
775 case CMD_XRI_ABORTED_CX:
776 case CMD_ABORT_MXRI64_CN:
6669f9bb 777 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
778 type = LPFC_ABORT_IOCB;
779 break;
780 case CMD_RCV_SEQUENCE_CX:
781 case CMD_RCV_ELS_REQ_CX:
782 case CMD_RCV_SEQUENCE64_CX:
783 case CMD_RCV_ELS_REQ64_CX:
57127f15 784 case CMD_ASYNC_STATUS:
ed957684
JS
785 case CMD_IOCB_RCV_SEQ64_CX:
786 case CMD_IOCB_RCV_ELS64_CX:
787 case CMD_IOCB_RCV_CONT64_CX:
3163f725 788 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
789 type = LPFC_UNSOL_IOCB;
790 break;
3163f725
JS
791 case CMD_IOCB_XMIT_MSEQ64_CR:
792 case CMD_IOCB_XMIT_MSEQ64_CX:
793 case CMD_IOCB_RCV_SEQ_LIST64_CX:
794 case CMD_IOCB_RCV_ELS_LIST64_CX:
795 case CMD_IOCB_CLOSE_EXTENDED_CN:
796 case CMD_IOCB_ABORT_EXTENDED_CN:
797 case CMD_IOCB_RET_HBQE64_CN:
798 case CMD_IOCB_FCP_IBIDIR64_CR:
799 case CMD_IOCB_FCP_IBIDIR64_CX:
800 case CMD_IOCB_FCP_ITASKMGT64_CX:
801 case CMD_IOCB_LOGENTRY_CN:
802 case CMD_IOCB_LOGENTRY_ASYNC_CN:
803 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 804 __func__, iocb_cmnd);
3163f725
JS
805 type = LPFC_UNKNOWN_IOCB;
806 break;
dea3101e
JB
807 default:
808 type = LPFC_UNKNOWN_IOCB;
809 break;
810 }
811
812 return type;
813}
814
e59058c4 815/**
3621a710 816 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
817 * @phba: Pointer to HBA context object.
818 *
819 * This function is called from SLI initialization code
820 * to configure every ring of the HBA's SLI interface. The
821 * caller is not required to hold any lock. This function issues
822 * a config_ring mailbox command for each ring.
823 * This function returns zero if successful else returns a negative
824 * error code.
825 **/
dea3101e 826static int
ed957684 827lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
828{
829 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
830 LPFC_MBOXQ_t *pmb;
831 MAILBOX_t *pmbox;
832 int i, rc, ret = 0;
dea3101e 833
ed957684
JS
834 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
835 if (!pmb)
836 return -ENOMEM;
04c68496 837 pmbox = &pmb->u.mb;
ed957684 838 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 839 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
840 lpfc_config_ring(phba, i, pmb);
841 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
842 if (rc != MBX_SUCCESS) {
92d7f7b0 843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 844 "0446 Adapter failed to init (%d), "
dea3101e
JB
845 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
846 "ring %d\n",
e8b62011
JS
847 rc, pmbox->mbxCommand,
848 pmbox->mbxStatus, i);
2e0fef85 849 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
850 ret = -ENXIO;
851 break;
dea3101e
JB
852 }
853 }
ed957684
JS
854 mempool_free(pmb, phba->mbox_mem_pool);
855 return ret;
dea3101e
JB
856}
857
e59058c4 858/**
3621a710 859 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
860 * @phba: Pointer to HBA context object.
861 * @pring: Pointer to driver SLI ring object.
862 * @piocb: Pointer to the driver iocb object.
863 *
864 * This function is called with hbalock held. The function adds the
865 * new iocb to txcmplq of the given ring. This function always returns
866 * 0. If this function is called for ELS ring, this function checks if
867 * there is a vport associated with the ELS command. This function also
868 * starts els_tmofunc timer if this is an ELS command.
869 **/
dea3101e 870static int
2e0fef85
JS
871lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
872 struct lpfc_iocbq *piocb)
dea3101e 873{
dea3101e
JB
874 list_add_tail(&piocb->list, &pring->txcmplq);
875 pring->txcmplq_cnt++;
92d7f7b0
JS
876 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
877 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
878 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
879 if (!piocb->vport)
880 BUG();
881 else
882 mod_timer(&piocb->vport->els_tmofunc,
883 jiffies + HZ * (phba->fc_ratov << 1));
884 }
885
dea3101e 886
2e0fef85 887 return 0;
dea3101e
JB
888}
889
e59058c4 890/**
3621a710 891 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
892 * @phba: Pointer to HBA context object.
893 * @pring: Pointer to driver SLI ring object.
894 *
895 * This function is called with hbalock held to get next
896 * iocb in txq of the given ring. If there is any iocb in
897 * the txq, the function returns first iocb in the list after
898 * removing the iocb from the list, else it returns NULL.
899 **/
dea3101e 900static struct lpfc_iocbq *
2e0fef85 901lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 902{
dea3101e
JB
903 struct lpfc_iocbq *cmd_iocb;
904
858c9f6c
JS
905 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
906 if (cmd_iocb != NULL)
dea3101e 907 pring->txq_cnt--;
2e0fef85 908 return cmd_iocb;
dea3101e
JB
909}
910
e59058c4 911/**
3621a710 912 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
913 * @phba: Pointer to HBA context object.
914 * @pring: Pointer to driver SLI ring object.
915 *
916 * This function is called with hbalock held and the caller must post the
917 * iocb without releasing the lock. If the caller releases the lock,
918 * iocb slot returned by the function is not guaranteed to be available.
919 * The function returns pointer to the next available iocb slot if there
920 * is available slot in the ring, else it returns NULL.
921 * If the get index of the ring is ahead of the put index, the function
922 * will post an error attention event to the worker thread to take the
923 * HBA to offline state.
924 **/
dea3101e
JB
925static IOCB_t *
926lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
927{
34b02dcd 928 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 929 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e
JB
930 if ((pring->next_cmdidx == pring->cmdidx) &&
931 (++pring->next_cmdidx >= max_cmd_idx))
932 pring->next_cmdidx = 0;
933
934 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
935
936 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
937
938 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 940 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 941 "is bigger than cmd ring %d\n",
e8b62011 942 pring->ringno,
dea3101e
JB
943 pring->local_getidx, max_cmd_idx);
944
2e0fef85 945 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
946 /*
947 * All error attention handlers are posted to
948 * worker thread
949 */
950 phba->work_ha |= HA_ERATT;
951 phba->work_hs = HS_FFER3;
92d7f7b0 952
5e9d9b82 953 lpfc_worker_wake_up(phba);
dea3101e
JB
954
955 return NULL;
956 }
957
958 if (pring->local_getidx == pring->next_cmdidx)
959 return NULL;
960 }
961
ed957684 962 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
963}
964
e59058c4 965/**
3621a710 966 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
967 * @phba: Pointer to HBA context object.
968 * @iocbq: Pointer to driver iocb object.
969 *
970 * This function gets an iotag for the iocb. If there is no unused iotag and
971 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
972 * array and assigns a new iotag.
973 * The function returns the allocated iotag if successful, else returns zero.
974 * Zero is not a valid iotag.
975 * The caller is not required to hold any lock.
976 **/
604a3e30 977uint16_t
2e0fef85 978lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 979{
2e0fef85
JS
980 struct lpfc_iocbq **new_arr;
981 struct lpfc_iocbq **old_arr;
604a3e30
JB
982 size_t new_len;
983 struct lpfc_sli *psli = &phba->sli;
984 uint16_t iotag;
dea3101e 985
2e0fef85 986 spin_lock_irq(&phba->hbalock);
604a3e30
JB
987 iotag = psli->last_iotag;
988 if(++iotag < psli->iocbq_lookup_len) {
989 psli->last_iotag = iotag;
990 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 991 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
992 iocbq->iotag = iotag;
993 return iotag;
2e0fef85 994 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
995 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
996 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
997 spin_unlock_irq(&phba->hbalock);
998 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
999 GFP_KERNEL);
1000 if (new_arr) {
2e0fef85 1001 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1002 old_arr = psli->iocbq_lookup;
1003 if (new_len <= psli->iocbq_lookup_len) {
1004 /* highly unprobable case */
1005 kfree(new_arr);
1006 iotag = psli->last_iotag;
1007 if(++iotag < psli->iocbq_lookup_len) {
1008 psli->last_iotag = iotag;
1009 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1010 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1011 iocbq->iotag = iotag;
1012 return iotag;
1013 }
2e0fef85 1014 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1015 return 0;
1016 }
1017 if (psli->iocbq_lookup)
1018 memcpy(new_arr, old_arr,
1019 ((psli->last_iotag + 1) *
311464ec 1020 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1021 psli->iocbq_lookup = new_arr;
1022 psli->iocbq_lookup_len = new_len;
1023 psli->last_iotag = iotag;
1024 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1025 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1026 iocbq->iotag = iotag;
1027 kfree(old_arr);
1028 return iotag;
1029 }
8f6d98d2 1030 } else
2e0fef85 1031 spin_unlock_irq(&phba->hbalock);
dea3101e 1032
604a3e30 1033 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
e8b62011
JS
1034 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1035 psli->last_iotag);
dea3101e 1036
604a3e30 1037 return 0;
dea3101e
JB
1038}
1039
e59058c4 1040/**
3621a710 1041 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1042 * @phba: Pointer to HBA context object.
1043 * @pring: Pointer to driver SLI ring object.
1044 * @iocb: Pointer to iocb slot in the ring.
1045 * @nextiocb: Pointer to driver iocb object which need to be
1046 * posted to firmware.
1047 *
1048 * This function is called with hbalock held to post a new iocb to
1049 * the firmware. This function copies the new iocb to ring iocb slot and
1050 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1051 * a completion call back for this iocb else the function will free the
1052 * iocb object.
1053 **/
dea3101e
JB
1054static void
1055lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1056 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1057{
1058 /*
604a3e30 1059 * Set up an iotag
dea3101e 1060 */
604a3e30 1061 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1062
e2a0a9d6 1063
a58cbd52
JS
1064 if (pring->ringno == LPFC_ELS_RING) {
1065 lpfc_debugfs_slow_ring_trc(phba,
1066 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1067 *(((uint32_t *) &nextiocb->iocb) + 4),
1068 *(((uint32_t *) &nextiocb->iocb) + 6),
1069 *(((uint32_t *) &nextiocb->iocb) + 7));
1070 }
1071
dea3101e
JB
1072 /*
1073 * Issue iocb command to adapter
1074 */
92d7f7b0 1075 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1076 wmb();
1077 pring->stats.iocb_cmd++;
1078
1079 /*
1080 * If there is no completion routine to call, we can release the
1081 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1082 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1083 */
1084 if (nextiocb->iocb_cmpl)
1085 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1086 else
2e0fef85 1087 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1088
1089 /*
1090 * Let the HBA know what IOCB slot will be the next one the
1091 * driver will put a command into.
1092 */
1093 pring->cmdidx = pring->next_cmdidx;
ed957684 1094 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1095}
1096
e59058c4 1097/**
3621a710 1098 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1099 * @phba: Pointer to HBA context object.
1100 * @pring: Pointer to driver SLI ring object.
1101 *
1102 * The caller is not required to hold any lock for calling this function.
1103 * This function updates the chip attention bits for the ring to inform firmware
1104 * that there are pending work to be done for this ring and requests an
1105 * interrupt when there is space available in the ring. This function is
1106 * called when the driver is unable to post more iocbs to the ring due
1107 * to unavailability of space in the ring.
1108 **/
dea3101e 1109static void
2e0fef85 1110lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1111{
1112 int ringno = pring->ringno;
1113
1114 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1115
1116 wmb();
1117
1118 /*
1119 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1120 * The HBA will tell us when an IOCB entry is available.
1121 */
1122 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1123 readl(phba->CAregaddr); /* flush */
1124
1125 pring->stats.iocb_cmd_full++;
1126}
1127
e59058c4 1128/**
3621a710 1129 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1130 * @phba: Pointer to HBA context object.
1131 * @pring: Pointer to driver SLI ring object.
1132 *
1133 * This function updates the chip attention register bit for the
1134 * given ring to inform HBA that there is more work to be done
1135 * in this ring. The caller is not required to hold any lock.
1136 **/
dea3101e 1137static void
2e0fef85 1138lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1139{
1140 int ringno = pring->ringno;
1141
1142 /*
1143 * Tell the HBA that there is work to do in this ring.
1144 */
34b02dcd
JS
1145 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1146 wmb();
1147 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1148 readl(phba->CAregaddr); /* flush */
1149 }
dea3101e
JB
1150}
1151
e59058c4 1152/**
3621a710 1153 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1154 * @phba: Pointer to HBA context object.
1155 * @pring: Pointer to driver SLI ring object.
1156 *
1157 * This function is called with hbalock held to post pending iocbs
1158 * in the txq to the firmware. This function is called when driver
1159 * detects space available in the ring.
1160 **/
dea3101e 1161static void
2e0fef85 1162lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1163{
1164 IOCB_t *iocb;
1165 struct lpfc_iocbq *nextiocb;
1166
1167 /*
1168 * Check to see if:
1169 * (a) there is anything on the txq to send
1170 * (b) link is up
1171 * (c) link attention events can be processed (fcp ring only)
1172 * (d) IOCB processing is not blocked by the outstanding mbox command.
1173 */
1174 if (pring->txq_cnt &&
2e0fef85 1175 lpfc_is_link_up(phba) &&
dea3101e 1176 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1177 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1178
1179 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1180 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1181 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1182
1183 if (iocb)
1184 lpfc_sli_update_ring(phba, pring);
1185 else
1186 lpfc_sli_update_full_ring(phba, pring);
1187 }
1188
1189 return;
1190}
1191
e59058c4 1192/**
3621a710 1193 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1194 * @phba: Pointer to HBA context object.
1195 * @hbqno: HBQ number.
1196 *
1197 * This function is called with hbalock held to get the next
1198 * available slot for the given HBQ. If there is free slot
1199 * available for the HBQ it will return pointer to the next available
1200 * HBQ entry else it will return NULL.
1201 **/
a6ababd2 1202static struct lpfc_hbq_entry *
ed957684
JS
1203lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1204{
1205 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1206
1207 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1208 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1209 hbqp->next_hbqPutIdx = 0;
1210
1211 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1212 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1213 uint32_t getidx = le32_to_cpu(raw_index);
1214
1215 hbqp->local_hbqGetIdx = getidx;
1216
1217 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1218 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1219 LOG_SLI | LOG_VPORT,
e8b62011 1220 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1221 "%u is > than hbqp->entry_count %u\n",
e8b62011 1222 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1223 hbqp->entry_count);
1224
1225 phba->link_state = LPFC_HBA_ERROR;
1226 return NULL;
1227 }
1228
1229 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1230 return NULL;
1231 }
1232
51ef4c26
JS
1233 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1234 hbqp->hbqPutIdx;
ed957684
JS
1235}
1236
e59058c4 1237/**
3621a710 1238 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1239 * @phba: Pointer to HBA context object.
1240 *
1241 * This function is called with no lock held to free all the
1242 * hbq buffers while uninitializing the SLI interface. It also
1243 * frees the HBQ buffers returned by the firmware but not yet
1244 * processed by the upper layers.
1245 **/
ed957684
JS
1246void
1247lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1248{
92d7f7b0
JS
1249 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1250 struct hbq_dmabuf *hbq_buf;
3163f725 1251 unsigned long flags;
51ef4c26 1252 int i, hbq_count;
3163f725 1253 uint32_t hbqno;
ed957684 1254
51ef4c26 1255 hbq_count = lpfc_sli_hbq_count();
ed957684 1256 /* Return all memory used by all HBQs */
3163f725 1257 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1258 for (i = 0; i < hbq_count; ++i) {
1259 list_for_each_entry_safe(dmabuf, next_dmabuf,
1260 &phba->hbqs[i].hbq_buffer_list, list) {
1261 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1262 list_del(&hbq_buf->dbuf.list);
1263 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1264 }
a8adb832 1265 phba->hbqs[i].buffer_count = 0;
ed957684 1266 }
3163f725 1267 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1268 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1269 list) {
3163f725
JS
1270 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271 list_del(&hbq_buf->dbuf.list);
1272 if (hbq_buf->tag == -1) {
1273 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274 (phba, hbq_buf);
1275 } else {
1276 hbqno = hbq_buf->tag >> 16;
1277 if (hbqno >= LPFC_MAX_HBQS)
1278 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1279 (phba, hbq_buf);
1280 else
1281 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1282 hbq_buf);
1283 }
1284 }
1285
1286 /* Mark the HBQs not in use */
1287 phba->hbq_in_use = 0;
1288 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1289}
1290
e59058c4 1291/**
3621a710 1292 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1293 * @phba: Pointer to HBA context object.
1294 * @hbqno: HBQ number.
1295 * @hbq_buf: Pointer to HBQ buffer.
1296 *
1297 * This function is called with the hbalock held to post a
1298 * hbq buffer to the firmware. If the function finds an empty
1299 * slot in the HBQ, it will post the buffer. The function will return
1300 * pointer to the hbq entry if it successfully post the buffer
1301 * else it will return NULL.
1302 **/
3772a991 1303static int
ed957684 1304lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1305 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1306{
1307 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1308}
1309
1310/**
1311 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1312 * @phba: Pointer to HBA context object.
1313 * @hbqno: HBQ number.
1314 * @hbq_buf: Pointer to HBQ buffer.
1315 *
1316 * This function is called with the hbalock held to post a hbq buffer to the
1317 * firmware. If the function finds an empty slot in the HBQ, it will post the
1318 * buffer and place it on the hbq_buffer_list. The function will return zero if
1319 * it successfully post the buffer else it will return an error.
1320 **/
1321static int
1322lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1323 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1324{
1325 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1326 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1327
1328 /* Get next HBQ entry slot to use */
1329 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1330 if (hbqe) {
1331 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1332
92d7f7b0
JS
1333 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1334 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1335 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1336 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1337 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1338 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1339 /* Sync SLIM */
ed957684
JS
1340 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1341 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1342 /* flush */
ed957684 1343 readl(phba->hbq_put + hbqno);
51ef4c26 1344 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1345 return 0;
1346 } else
1347 return -ENOMEM;
ed957684
JS
1348}
1349
4f774513
JS
1350/**
1351 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1352 * @phba: Pointer to HBA context object.
1353 * @hbqno: HBQ number.
1354 * @hbq_buf: Pointer to HBQ buffer.
1355 *
1356 * This function is called with the hbalock held to post an RQE to the SLI4
1357 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1358 * the hbq_buffer_list and return zero, otherwise it will return an error.
1359 **/
1360static int
1361lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1362 struct hbq_dmabuf *hbq_buf)
1363{
1364 int rc;
1365 struct lpfc_rqe hrqe;
1366 struct lpfc_rqe drqe;
1367
1368 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1369 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1370 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1371 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1372 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1373 &hrqe, &drqe);
1374 if (rc < 0)
1375 return rc;
1376 hbq_buf->tag = rc;
1377 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1378 return 0;
1379}
1380
e59058c4 1381/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1382static struct lpfc_hbq_init lpfc_els_hbq = {
1383 .rn = 1,
def9c7a9 1384 .entry_count = 256,
92d7f7b0
JS
1385 .mask_count = 0,
1386 .profile = 0,
51ef4c26 1387 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1388 .buffer_count = 0,
a257bf90
JS
1389 .init_count = 40,
1390 .add_count = 40,
92d7f7b0 1391};
ed957684 1392
e59058c4 1393/* HBQ for the extra ring if needed */
51ef4c26
JS
1394static struct lpfc_hbq_init lpfc_extra_hbq = {
1395 .rn = 1,
1396 .entry_count = 200,
1397 .mask_count = 0,
1398 .profile = 0,
1399 .ring_mask = (1 << LPFC_EXTRA_RING),
1400 .buffer_count = 0,
1401 .init_count = 0,
1402 .add_count = 5,
1403};
1404
e59058c4 1405/* Array of HBQs */
78b2d852 1406struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1407 &lpfc_els_hbq,
51ef4c26 1408 &lpfc_extra_hbq,
92d7f7b0 1409};
ed957684 1410
e59058c4 1411/**
3621a710 1412 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1413 * @phba: Pointer to HBA context object.
1414 * @hbqno: HBQ number.
1415 * @count: Number of HBQ buffers to be posted.
1416 *
d7c255b2
JS
1417 * This function is called with no lock held to post more hbq buffers to the
1418 * given HBQ. The function returns the number of HBQ buffers successfully
1419 * posted.
e59058c4 1420 **/
311464ec 1421static int
92d7f7b0 1422lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1423{
d7c255b2 1424 uint32_t i, posted = 0;
3163f725 1425 unsigned long flags;
92d7f7b0 1426 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1427 LIST_HEAD(hbq_buf_list);
eafe1df9 1428 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1429 return 0;
51ef4c26 1430
d7c255b2
JS
1431 if ((phba->hbqs[hbqno].buffer_count + count) >
1432 lpfc_hbq_defs[hbqno]->entry_count)
1433 count = lpfc_hbq_defs[hbqno]->entry_count -
1434 phba->hbqs[hbqno].buffer_count;
1435 if (!count)
1436 return 0;
1437 /* Allocate HBQ entries */
1438 for (i = 0; i < count; i++) {
1439 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1440 if (!hbq_buffer)
1441 break;
1442 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1443 }
3163f725
JS
1444 /* Check whether HBQ is still in use */
1445 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1446 if (!phba->hbq_in_use)
d7c255b2
JS
1447 goto err;
1448 while (!list_empty(&hbq_buf_list)) {
1449 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1450 dbuf.list);
1451 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1452 (hbqno << 16));
3772a991 1453 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1454 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1455 posted++;
1456 } else
51ef4c26 1457 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1458 }
3163f725 1459 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1460 return posted;
1461err:
eafe1df9 1462 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1463 while (!list_empty(&hbq_buf_list)) {
1464 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1465 dbuf.list);
1466 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467 }
1468 return 0;
ed957684
JS
1469}
1470
e59058c4 1471/**
3621a710 1472 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1473 * @phba: Pointer to HBA context object.
1474 * @qno: HBQ number.
1475 *
1476 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1477 * is called with no lock held. The function returns the number of HBQ entries
1478 * successfully allocated.
e59058c4 1479 **/
92d7f7b0
JS
1480int
1481lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1482{
def9c7a9
JS
1483 if (phba->sli_rev == LPFC_SLI_REV4)
1484 return 0;
1485 else
1486 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1487 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1488}
ed957684 1489
e59058c4 1490/**
3621a710 1491 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1492 * @phba: Pointer to HBA context object.
1493 * @qno: HBQ queue number.
1494 *
1495 * This function is called from SLI initialization code path with
1496 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1497 * function returns the number of HBQ entries successfully allocated.
e59058c4 1498 **/
a6ababd2 1499static int
92d7f7b0
JS
1500lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1501{
def9c7a9
JS
1502 if (phba->sli_rev == LPFC_SLI_REV4)
1503 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1504 lpfc_hbq_defs[qno]->entry_count);
1505 else
1506 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1507 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1508}
1509
3772a991
JS
1510/**
1511 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1512 * @phba: Pointer to HBA context object.
1513 * @hbqno: HBQ number.
1514 *
1515 * This function removes the first hbq buffer on an hbq list and returns a
1516 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1517 **/
1518static struct hbq_dmabuf *
1519lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1520{
1521 struct lpfc_dmabuf *d_buf;
1522
1523 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1524 if (!d_buf)
1525 return NULL;
1526 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1527}
1528
e59058c4 1529/**
3621a710 1530 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1531 * @phba: Pointer to HBA context object.
1532 * @tag: Tag of the hbq buffer.
1533 *
1534 * This function is called with hbalock held. This function searches
1535 * for the hbq buffer associated with the given tag in the hbq buffer
1536 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1537 * it returns NULL.
1538 **/
a6ababd2 1539static struct hbq_dmabuf *
92d7f7b0 1540lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1541{
92d7f7b0
JS
1542 struct lpfc_dmabuf *d_buf;
1543 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1544 uint32_t hbqno;
1545
1546 hbqno = tag >> 16;
a0a74e45 1547 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1548 return NULL;
ed957684 1549
3772a991 1550 spin_lock_irq(&phba->hbalock);
51ef4c26 1551 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1552 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1553 if (hbq_buf->tag == tag) {
3772a991 1554 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1555 return hbq_buf;
ed957684
JS
1556 }
1557 }
3772a991 1558 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1560 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1561 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1562 return NULL;
ed957684
JS
1563}
1564
e59058c4 1565/**
3621a710 1566 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1567 * @phba: Pointer to HBA context object.
1568 * @hbq_buffer: Pointer to HBQ buffer.
1569 *
1570 * This function is called with hbalock. This function gives back
1571 * the hbq buffer to firmware. If the HBQ does not have space to
1572 * post the buffer, it will free the buffer.
1573 **/
ed957684 1574void
51ef4c26 1575lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1576{
1577 uint32_t hbqno;
1578
51ef4c26
JS
1579 if (hbq_buffer) {
1580 hbqno = hbq_buffer->tag >> 16;
3772a991 1581 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1582 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1583 }
1584}
1585
e59058c4 1586/**
3621a710 1587 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1588 * @mbxCommand: mailbox command code.
1589 *
1590 * This function is called by the mailbox event handler function to verify
1591 * that the completed mailbox command is a legitimate mailbox command. If the
1592 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1593 * and the mailbox event handler will take the HBA offline.
1594 **/
dea3101e
JB
1595static int
1596lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1597{
1598 uint8_t ret;
1599
1600 switch (mbxCommand) {
1601 case MBX_LOAD_SM:
1602 case MBX_READ_NV:
1603 case MBX_WRITE_NV:
a8adb832 1604 case MBX_WRITE_VPARMS:
dea3101e
JB
1605 case MBX_RUN_BIU_DIAG:
1606 case MBX_INIT_LINK:
1607 case MBX_DOWN_LINK:
1608 case MBX_CONFIG_LINK:
1609 case MBX_CONFIG_RING:
1610 case MBX_RESET_RING:
1611 case MBX_READ_CONFIG:
1612 case MBX_READ_RCONFIG:
1613 case MBX_READ_SPARM:
1614 case MBX_READ_STATUS:
1615 case MBX_READ_RPI:
1616 case MBX_READ_XRI:
1617 case MBX_READ_REV:
1618 case MBX_READ_LNK_STAT:
1619 case MBX_REG_LOGIN:
1620 case MBX_UNREG_LOGIN:
1621 case MBX_READ_LA:
1622 case MBX_CLEAR_LA:
1623 case MBX_DUMP_MEMORY:
1624 case MBX_DUMP_CONTEXT:
1625 case MBX_RUN_DIAGS:
1626 case MBX_RESTART:
1627 case MBX_UPDATE_CFG:
1628 case MBX_DOWN_LOAD:
1629 case MBX_DEL_LD_ENTRY:
1630 case MBX_RUN_PROGRAM:
1631 case MBX_SET_MASK:
09372820 1632 case MBX_SET_VARIABLE:
dea3101e 1633 case MBX_UNREG_D_ID:
41415862 1634 case MBX_KILL_BOARD:
dea3101e 1635 case MBX_CONFIG_FARP:
41415862 1636 case MBX_BEACON:
dea3101e
JB
1637 case MBX_LOAD_AREA:
1638 case MBX_RUN_BIU_DIAG64:
1639 case MBX_CONFIG_PORT:
1640 case MBX_READ_SPARM64:
1641 case MBX_READ_RPI64:
1642 case MBX_REG_LOGIN64:
1643 case MBX_READ_LA64:
09372820 1644 case MBX_WRITE_WWN:
dea3101e
JB
1645 case MBX_SET_DEBUG:
1646 case MBX_LOAD_EXP_ROM:
57127f15 1647 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
1648 case MBX_REG_VPI:
1649 case MBX_UNREG_VPI:
858c9f6c 1650 case MBX_HEARTBEAT:
84774a4d
JS
1651 case MBX_PORT_CAPABILITIES:
1652 case MBX_PORT_IOV_CONTROL:
04c68496
JS
1653 case MBX_SLI4_CONFIG:
1654 case MBX_SLI4_REQ_FTRS:
1655 case MBX_REG_FCFI:
1656 case MBX_UNREG_FCFI:
1657 case MBX_REG_VFI:
1658 case MBX_UNREG_VFI:
1659 case MBX_INIT_VPI:
1660 case MBX_INIT_VFI:
1661 case MBX_RESUME_RPI:
dea3101e
JB
1662 ret = mbxCommand;
1663 break;
1664 default:
1665 ret = MBX_SHUTDOWN;
1666 break;
1667 }
2e0fef85 1668 return ret;
dea3101e 1669}
e59058c4
JS
1670
1671/**
3621a710 1672 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1673 * @phba: Pointer to HBA context object.
1674 * @pmboxq: Pointer to mailbox command.
1675 *
1676 * This is completion handler function for mailbox commands issued from
1677 * lpfc_sli_issue_mbox_wait function. This function is called by the
1678 * mailbox event handler function with no lock held. This function
1679 * will wake up thread waiting on the wait queue pointed by context1
1680 * of the mailbox.
1681 **/
04c68496 1682void
2e0fef85 1683lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
1684{
1685 wait_queue_head_t *pdone_q;
858c9f6c 1686 unsigned long drvr_flag;
dea3101e
JB
1687
1688 /*
1689 * If pdone_q is empty, the driver thread gave up waiting and
1690 * continued running.
1691 */
7054a606 1692 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1693 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
1694 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1695 if (pdone_q)
1696 wake_up_interruptible(pdone_q);
858c9f6c 1697 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
1698 return;
1699}
1700
e59058c4
JS
1701
1702/**
3621a710 1703 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1704 * @phba: Pointer to HBA context object.
1705 * @pmb: Pointer to mailbox object.
1706 *
1707 * This function is the default mailbox completion handler. It
1708 * frees the memory resources associated with the completed mailbox
1709 * command. If the completed command is a REG_LOGIN mailbox command,
1710 * this function will issue a UREG_LOGIN to re-claim the RPI.
1711 **/
dea3101e 1712void
2e0fef85 1713lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e
JB
1714{
1715 struct lpfc_dmabuf *mp;
04c68496 1716 uint16_t rpi, vpi;
7054a606 1717 int rc;
695a814e 1718 struct lpfc_vport *vport = pmb->vport;
7054a606 1719
dea3101e 1720 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1721
dea3101e
JB
1722 if (mp) {
1723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1724 kfree(mp);
1725 }
7054a606 1726
04c68496
JS
1727 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1728 (phba->sli_rev == LPFC_SLI_REV4))
1729 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1730
7054a606
JS
1731 /*
1732 * If a REG_LOGIN succeeded after node is destroyed or node
1733 * is in re-discovery driver need to cleanup the RPI.
1734 */
2e0fef85 1735 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1736 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1737 !pmb->u.mb.mbxStatus) {
1738 rpi = pmb->u.mb.un.varWords[0];
1739 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1740 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1741 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1742 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1743 if (rc != MBX_NOT_FINISHED)
1744 return;
1745 }
1746
695a814e
JS
1747 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1748 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1749 !(phba->pport->load_flag & FC_UNLOADING) &&
1750 !pmb->u.mb.mbxStatus) {
1751 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1752 pmb->vport = vport;
1753 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1754 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1755 if (rc != MBX_NOT_FINISHED)
1756 return;
1757 }
1758
04c68496
JS
1759 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1760 lpfc_sli4_mbox_cmd_free(phba, pmb);
1761 else
1762 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
1763}
1764
e59058c4 1765/**
3621a710 1766 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1767 * @phba: Pointer to HBA context object.
1768 *
1769 * This function is called with no lock held. This function processes all
1770 * the completed mailbox commands and gives it to upper layers. The interrupt
1771 * service routine processes mailbox completion interrupt and adds completed
1772 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1773 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1774 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1775 * function returns the mailbox commands to the upper layer by calling the
1776 * completion handler function of each mailbox.
1777 **/
dea3101e 1778int
2e0fef85 1779lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1780{
92d7f7b0 1781 MAILBOX_t *pmbox;
dea3101e 1782 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1783 int rc;
1784 LIST_HEAD(cmplq);
dea3101e
JB
1785
1786 phba->sli.slistat.mbox_event++;
1787
92d7f7b0
JS
1788 /* Get all completed mailboxe buffers into the cmplq */
1789 spin_lock_irq(&phba->hbalock);
1790 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1791 spin_unlock_irq(&phba->hbalock);
dea3101e 1792
92d7f7b0
JS
1793 /* Get a Mailbox buffer to setup mailbox commands for callback */
1794 do {
1795 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1796 if (pmb == NULL)
1797 break;
2e0fef85 1798
04c68496 1799 pmbox = &pmb->u.mb;
dea3101e 1800
858c9f6c
JS
1801 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1802 if (pmb->vport) {
1803 lpfc_debugfs_disc_trc(pmb->vport,
1804 LPFC_DISC_TRC_MBOX_VPORT,
1805 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1806 (uint32_t)pmbox->mbxCommand,
1807 pmbox->un.varWords[0],
1808 pmbox->un.varWords[1]);
1809 }
1810 else {
1811 lpfc_debugfs_disc_trc(phba->pport,
1812 LPFC_DISC_TRC_MBOX,
1813 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1814 (uint32_t)pmbox->mbxCommand,
1815 pmbox->un.varWords[0],
1816 pmbox->un.varWords[1]);
1817 }
1818 }
1819
dea3101e
JB
1820 /*
1821 * It is a fatal error if unknown mbox command completion.
1822 */
1823 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1824 MBX_SHUTDOWN) {
af901ca1 1825 /* Unknown mailbox command compl */
92d7f7b0 1826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1827 "(%d):0323 Unknown Mailbox command "
04c68496 1828 "x%x (x%x) Cmpl\n",
92d7f7b0 1829 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1830 pmbox->mbxCommand,
1831 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1832 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1833 phba->work_hs = HS_FFER3;
1834 lpfc_handle_eratt(phba);
92d7f7b0 1835 continue;
dea3101e
JB
1836 }
1837
dea3101e
JB
1838 if (pmbox->mbxStatus) {
1839 phba->sli.slistat.mbox_stat_err++;
1840 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1841 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1842 lpfc_printf_log(phba, KERN_INFO,
1843 LOG_MBOX | LOG_SLI,
e8b62011 1844 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1845 "error - RETRYing Data: x%x "
04c68496 1846 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1847 pmb->vport ? pmb->vport->vpi :0,
1848 pmbox->mbxCommand,
04c68496
JS
1849 lpfc_sli4_mbox_opcode_get(phba,
1850 pmb),
92d7f7b0
JS
1851 pmbox->mbxStatus,
1852 pmbox->un.varWords[0],
1853 pmb->vport->port_state);
dea3101e
JB
1854 pmbox->mbxStatus = 0;
1855 pmbox->mbxOwner = OWN_HOST;
dea3101e 1856 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1857 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1858 continue;
dea3101e
JB
1859 }
1860 }
1861
1862 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1863 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1864 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1865 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1866 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1867 pmbox->mbxCommand,
04c68496 1868 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e
JB
1869 pmb->mbox_cmpl,
1870 *((uint32_t *) pmbox),
1871 pmbox->un.varWords[0],
1872 pmbox->un.varWords[1],
1873 pmbox->un.varWords[2],
1874 pmbox->un.varWords[3],
1875 pmbox->un.varWords[4],
1876 pmbox->un.varWords[5],
1877 pmbox->un.varWords[6],
1878 pmbox->un.varWords[7]);
1879
92d7f7b0 1880 if (pmb->mbox_cmpl)
dea3101e 1881 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1882 } while (1);
1883 return 0;
1884}
dea3101e 1885
e59058c4 1886/**
3621a710 1887 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1888 * @phba: Pointer to HBA context object.
1889 * @pring: Pointer to driver SLI ring object.
1890 * @tag: buffer tag.
1891 *
1892 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1893 * is set in the tag the buffer is posted for a particular exchange,
1894 * the function will return the buffer without replacing the buffer.
1895 * If the buffer is for unsolicited ELS or CT traffic, this function
1896 * returns the buffer and also posts another buffer to the firmware.
1897 **/
76bb24ef
JS
1898static struct lpfc_dmabuf *
1899lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1900 struct lpfc_sli_ring *pring,
1901 uint32_t tag)
76bb24ef 1902{
9f1e1b50
JS
1903 struct hbq_dmabuf *hbq_entry;
1904
76bb24ef
JS
1905 if (tag & QUE_BUFTAG_BIT)
1906 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1907 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1908 if (!hbq_entry)
1909 return NULL;
1910 return &hbq_entry->dbuf;
76bb24ef 1911}
57127f15 1912
3772a991
JS
1913/**
1914 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1915 * @phba: Pointer to HBA context object.
1916 * @pring: Pointer to driver SLI ring object.
1917 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1918 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1919 * @fch_type: the type for the first frame of the sequence.
1920 *
1921 * This function is called with no lock held. This function uses the r_ctl and
1922 * type of the received sequence to find the correct callback function to call
1923 * to process the sequence.
1924 **/
1925static int
1926lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1927 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1928 uint32_t fch_type)
1929{
1930 int i;
1931
1932 /* unSolicited Responses */
1933 if (pring->prt[0].profile) {
1934 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1935 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1936 saveq);
1937 return 1;
1938 }
1939 /* We must search, based on rctl / type
1940 for the right routine */
1941 for (i = 0; i < pring->num_mask; i++) {
1942 if ((pring->prt[i].rctl == fch_r_ctl) &&
1943 (pring->prt[i].type == fch_type)) {
1944 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1945 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1946 (phba, pring, saveq);
1947 return 1;
1948 }
1949 }
1950 return 0;
1951}
e59058c4
JS
1952
1953/**
3621a710 1954 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1955 * @phba: Pointer to HBA context object.
1956 * @pring: Pointer to driver SLI ring object.
1957 * @saveq: Pointer to the unsolicited iocb.
1958 *
1959 * This function is called with no lock held by the ring event handler
1960 * when there is an unsolicited iocb posted to the response ring by the
1961 * firmware. This function gets the buffer associated with the iocbs
1962 * and calls the event handler for the ring. This function handles both
1963 * qring buffers and hbq buffers.
1964 * When the function returns 1 the caller can free the iocb object otherwise
1965 * upper layer functions will free the iocb objects.
1966 **/
dea3101e
JB
1967static int
1968lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1969 struct lpfc_iocbq *saveq)
1970{
1971 IOCB_t * irsp;
1972 WORD5 * w5p;
1973 uint32_t Rctl, Type;
3772a991 1974 uint32_t match;
76bb24ef 1975 struct lpfc_iocbq *iocbq;
3163f725 1976 struct lpfc_dmabuf *dmzbuf;
dea3101e
JB
1977
1978 match = 0;
1979 irsp = &(saveq->iocb);
57127f15
JS
1980
1981 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1982 if (pring->lpfc_sli_rcv_async_status)
1983 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1984 else
1985 lpfc_printf_log(phba,
1986 KERN_WARNING,
1987 LOG_SLI,
1988 "0316 Ring %d handler: unexpected "
1989 "ASYNC_STATUS iocb received evt_code "
1990 "0x%x\n",
1991 pring->ringno,
1992 irsp->un.asyncstat.evt_code);
1993 return 1;
1994 }
1995
3163f725
JS
1996 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1997 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1998 if (irsp->ulpBdeCount > 0) {
1999 dmzbuf = lpfc_sli_get_buff(phba, pring,
2000 irsp->un.ulpWord[3]);
2001 lpfc_in_buf_free(phba, dmzbuf);
2002 }
2003
2004 if (irsp->ulpBdeCount > 1) {
2005 dmzbuf = lpfc_sli_get_buff(phba, pring,
2006 irsp->unsli3.sli3Words[3]);
2007 lpfc_in_buf_free(phba, dmzbuf);
2008 }
2009
2010 if (irsp->ulpBdeCount > 2) {
2011 dmzbuf = lpfc_sli_get_buff(phba, pring,
2012 irsp->unsli3.sli3Words[7]);
2013 lpfc_in_buf_free(phba, dmzbuf);
2014 }
2015
2016 return 1;
2017 }
2018
92d7f7b0 2019 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2020 if (irsp->ulpBdeCount != 0) {
2021 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2022 irsp->un.ulpWord[3]);
2023 if (!saveq->context2)
2024 lpfc_printf_log(phba,
2025 KERN_ERR,
2026 LOG_SLI,
2027 "0341 Ring %d Cannot find buffer for "
2028 "an unsolicited iocb. tag 0x%x\n",
2029 pring->ringno,
2030 irsp->un.ulpWord[3]);
76bb24ef
JS
2031 }
2032 if (irsp->ulpBdeCount == 2) {
2033 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2034 irsp->unsli3.sli3Words[7]);
2035 if (!saveq->context3)
2036 lpfc_printf_log(phba,
2037 KERN_ERR,
2038 LOG_SLI,
2039 "0342 Ring %d Cannot find buffer for an"
2040 " unsolicited iocb. tag 0x%x\n",
2041 pring->ringno,
2042 irsp->unsli3.sli3Words[7]);
2043 }
2044 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2045 irsp = &(iocbq->iocb);
76bb24ef
JS
2046 if (irsp->ulpBdeCount != 0) {
2047 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2048 irsp->un.ulpWord[3]);
9c2face6 2049 if (!iocbq->context2)
76bb24ef
JS
2050 lpfc_printf_log(phba,
2051 KERN_ERR,
2052 LOG_SLI,
2053 "0343 Ring %d Cannot find "
2054 "buffer for an unsolicited iocb"
2055 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2056 irsp->un.ulpWord[3]);
76bb24ef
JS
2057 }
2058 if (irsp->ulpBdeCount == 2) {
2059 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2060 irsp->unsli3.sli3Words[7]);
9c2face6 2061 if (!iocbq->context3)
76bb24ef
JS
2062 lpfc_printf_log(phba,
2063 KERN_ERR,
2064 LOG_SLI,
2065 "0344 Ring %d Cannot find "
2066 "buffer for an unsolicited "
2067 "iocb. tag 0x%x\n",
2068 pring->ringno,
2069 irsp->unsli3.sli3Words[7]);
2070 }
2071 }
92d7f7b0 2072 }
9c2face6
JS
2073 if (irsp->ulpBdeCount != 0 &&
2074 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2075 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2076 int found = 0;
2077
2078 /* search continue save q for same XRI */
2079 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2080 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2081 list_add_tail(&saveq->list, &iocbq->list);
2082 found = 1;
2083 break;
2084 }
2085 }
2086 if (!found)
2087 list_add_tail(&saveq->clist,
2088 &pring->iocb_continue_saveq);
2089 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2090 list_del_init(&iocbq->clist);
2091 saveq = iocbq;
2092 irsp = &(saveq->iocb);
2093 } else
2094 return 0;
2095 }
2096 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2097 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2098 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2099 Rctl = FC_RCTL_ELS_REQ;
2100 Type = FC_TYPE_ELS;
9c2face6
JS
2101 } else {
2102 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2103 Rctl = w5p->hcsw.Rctl;
2104 Type = w5p->hcsw.Type;
2105
2106 /* Firmware Workaround */
2107 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2108 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2109 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2110 Rctl = FC_RCTL_ELS_REQ;
2111 Type = FC_TYPE_ELS;
9c2face6
JS
2112 w5p->hcsw.Rctl = Rctl;
2113 w5p->hcsw.Type = Type;
2114 }
2115 }
92d7f7b0 2116
3772a991 2117 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2118 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2119 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2120 "Type x%x received\n",
e8b62011 2121 pring->ringno, Rctl, Type);
3772a991 2122
92d7f7b0 2123 return 1;
dea3101e
JB
2124}
2125
e59058c4 2126/**
3621a710 2127 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2128 * @phba: Pointer to HBA context object.
2129 * @pring: Pointer to driver SLI ring object.
2130 * @prspiocb: Pointer to response iocb object.
2131 *
2132 * This function looks up the iocb_lookup table to get the command iocb
2133 * corresponding to the given response iocb using the iotag of the
2134 * response iocb. This function is called with the hbalock held.
2135 * This function returns the command iocb object if it finds the command
2136 * iocb else returns NULL.
2137 **/
dea3101e 2138static struct lpfc_iocbq *
2e0fef85
JS
2139lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2140 struct lpfc_sli_ring *pring,
2141 struct lpfc_iocbq *prspiocb)
dea3101e 2142{
dea3101e
JB
2143 struct lpfc_iocbq *cmd_iocb = NULL;
2144 uint16_t iotag;
2145
604a3e30
JB
2146 iotag = prspiocb->iocb.ulpIoTag;
2147
2148 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2149 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2150 list_del_init(&cmd_iocb->list);
604a3e30
JB
2151 pring->txcmplq_cnt--;
2152 return cmd_iocb;
dea3101e
JB
2153 }
2154
dea3101e 2155 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2156 "0317 iotag x%x is out off "
604a3e30 2157 "range: max iotag x%x wd0 x%x\n",
e8b62011 2158 iotag, phba->sli.last_iotag,
604a3e30 2159 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2160 return NULL;
2161}
2162
3772a991
JS
2163/**
2164 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2165 * @phba: Pointer to HBA context object.
2166 * @pring: Pointer to driver SLI ring object.
2167 * @iotag: IOCB tag.
2168 *
2169 * This function looks up the iocb_lookup table to get the command iocb
2170 * corresponding to the given iotag. This function is called with the
2171 * hbalock held.
2172 * This function returns the command iocb object if it finds the command
2173 * iocb else returns NULL.
2174 **/
2175static struct lpfc_iocbq *
2176lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2177 struct lpfc_sli_ring *pring, uint16_t iotag)
2178{
2179 struct lpfc_iocbq *cmd_iocb;
2180
2181 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2182 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2183 list_del_init(&cmd_iocb->list);
2184 pring->txcmplq_cnt--;
2185 return cmd_iocb;
2186 }
2187
2188 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2189 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2190 iotag, phba->sli.last_iotag);
2191 return NULL;
2192}
2193
e59058c4 2194/**
3621a710 2195 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2196 * @phba: Pointer to HBA context object.
2197 * @pring: Pointer to driver SLI ring object.
2198 * @saveq: Pointer to the response iocb to be processed.
2199 *
2200 * This function is called by the ring event handler for non-fcp
2201 * rings when there is a new response iocb in the response ring.
2202 * The caller is not required to hold any locks. This function
2203 * gets the command iocb associated with the response iocb and
2204 * calls the completion handler for the command iocb. If there
2205 * is no completion handler, the function will free the resources
2206 * associated with command iocb. If the response iocb is for
2207 * an already aborted command iocb, the status of the completion
2208 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2209 * This function always returns 1.
2210 **/
dea3101e 2211static int
2e0fef85 2212lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2213 struct lpfc_iocbq *saveq)
2214{
2e0fef85 2215 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2216 int rc = 1;
2217 unsigned long iflag;
2218
2219 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2220 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2221 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2222 spin_unlock_irqrestore(&phba->hbalock, iflag);
2223
dea3101e
JB
2224 if (cmdiocbp) {
2225 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2226 /*
2227 * If an ELS command failed send an event to mgmt
2228 * application.
2229 */
2230 if (saveq->iocb.ulpStatus &&
2231 (pring->ringno == LPFC_ELS_RING) &&
2232 (cmdiocbp->iocb.ulpCommand ==
2233 CMD_ELS_REQUEST64_CR))
2234 lpfc_send_els_failure_event(phba,
2235 cmdiocbp, saveq);
2236
dea3101e
JB
2237 /*
2238 * Post all ELS completions to the worker thread.
2239 * All other are passed to the completion callback.
2240 */
2241 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2242 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2243 (cmdiocbp->iocb_flag &
2244 LPFC_DRIVER_ABORTED)) {
2245 spin_lock_irqsave(&phba->hbalock,
2246 iflag);
07951076
JS
2247 cmdiocbp->iocb_flag &=
2248 ~LPFC_DRIVER_ABORTED;
341af102
JS
2249 spin_unlock_irqrestore(&phba->hbalock,
2250 iflag);
07951076
JS
2251 saveq->iocb.ulpStatus =
2252 IOSTAT_LOCAL_REJECT;
2253 saveq->iocb.un.ulpWord[4] =
2254 IOERR_SLI_ABORTED;
0ff10d46
JS
2255
2256 /* Firmware could still be in progress
2257 * of DMAing payload, so don't free data
2258 * buffer till after a hbeat.
2259 */
341af102
JS
2260 spin_lock_irqsave(&phba->hbalock,
2261 iflag);
0ff10d46 2262 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2263 spin_unlock_irqrestore(&phba->hbalock,
2264 iflag);
2265 }
0f65ff68
JS
2266 if (phba->sli_rev == LPFC_SLI_REV4) {
2267 if (saveq->iocb_flag &
2268 LPFC_EXCHANGE_BUSY) {
2269 /* Set cmdiocb flag for the
2270 * exchange busy so sgl (xri)
2271 * will not be released until
2272 * the abort xri is received
2273 * from hba.
2274 */
2275 spin_lock_irqsave(
2276 &phba->hbalock, iflag);
2277 cmdiocbp->iocb_flag |=
2278 LPFC_EXCHANGE_BUSY;
2279 spin_unlock_irqrestore(
2280 &phba->hbalock, iflag);
2281 }
2282 if (cmdiocbp->iocb_flag &
2283 LPFC_DRIVER_ABORTED) {
2284 /*
2285 * Clear LPFC_DRIVER_ABORTED
2286 * bit in case it was driver
2287 * initiated abort.
2288 */
2289 spin_lock_irqsave(
2290 &phba->hbalock, iflag);
2291 cmdiocbp->iocb_flag &=
2292 ~LPFC_DRIVER_ABORTED;
2293 spin_unlock_irqrestore(
2294 &phba->hbalock, iflag);
2295 cmdiocbp->iocb.ulpStatus =
2296 IOSTAT_LOCAL_REJECT;
2297 cmdiocbp->iocb.un.ulpWord[4] =
2298 IOERR_ABORT_REQUESTED;
2299 /*
2300 * For SLI4, irsiocb contains
2301 * NO_XRI in sli_xritag, it
2302 * shall not affect releasing
2303 * sgl (xri) process.
2304 */
2305 saveq->iocb.ulpStatus =
2306 IOSTAT_LOCAL_REJECT;
2307 saveq->iocb.un.ulpWord[4] =
2308 IOERR_SLI_ABORTED;
2309 spin_lock_irqsave(
2310 &phba->hbalock, iflag);
2311 saveq->iocb_flag |=
2312 LPFC_DELAY_MEM_FREE;
2313 spin_unlock_irqrestore(
2314 &phba->hbalock, iflag);
2315 }
07951076 2316 }
dea3101e 2317 }
2e0fef85 2318 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2319 } else
2320 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2321 } else {
2322 /*
2323 * Unknown initiating command based on the response iotag.
2324 * This could be the case on the ELS ring because of
2325 * lpfc_els_abort().
2326 */
2327 if (pring->ringno != LPFC_ELS_RING) {
2328 /*
2329 * Ring <ringno> handler: unexpected completion IoTag
2330 * <IoTag>
2331 */
a257bf90 2332 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2333 "0322 Ring %d handler: "
2334 "unexpected completion IoTag x%x "
2335 "Data: x%x x%x x%x x%x\n",
2336 pring->ringno,
2337 saveq->iocb.ulpIoTag,
2338 saveq->iocb.ulpStatus,
2339 saveq->iocb.un.ulpWord[4],
2340 saveq->iocb.ulpCommand,
2341 saveq->iocb.ulpContext);
dea3101e
JB
2342 }
2343 }
68876920 2344
dea3101e
JB
2345 return rc;
2346}
2347
e59058c4 2348/**
3621a710 2349 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2350 * @phba: Pointer to HBA context object.
2351 * @pring: Pointer to driver SLI ring object.
2352 *
2353 * This function is called from the iocb ring event handlers when
2354 * put pointer is ahead of the get pointer for a ring. This function signal
2355 * an error attention condition to the worker thread and the worker
2356 * thread will transition the HBA to offline state.
2357 **/
2e0fef85
JS
2358static void
2359lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2360{
34b02dcd 2361 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2362 /*
025dfdaf 2363 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2364 * rsp ring <portRspMax>
2365 */
2366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2367 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2368 "is bigger than rsp ring %d\n",
e8b62011 2369 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2370 pring->numRiocb);
2371
2e0fef85 2372 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2373
2374 /*
2375 * All error attention handlers are posted to
2376 * worker thread
2377 */
2378 phba->work_ha |= HA_ERATT;
2379 phba->work_hs = HS_FFER3;
92d7f7b0 2380
5e9d9b82 2381 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2382
2383 return;
2384}
2385
9399627f 2386/**
3621a710 2387 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2388 * @ptr: Pointer to address of HBA context object.
2389 *
2390 * This function is invoked by the Error Attention polling timer when the
2391 * timer times out. It will check the SLI Error Attention register for
2392 * possible attention events. If so, it will post an Error Attention event
2393 * and wake up worker thread to process it. Otherwise, it will set up the
2394 * Error Attention polling timer for the next poll.
2395 **/
2396void lpfc_poll_eratt(unsigned long ptr)
2397{
2398 struct lpfc_hba *phba;
2399 uint32_t eratt = 0;
2400
2401 phba = (struct lpfc_hba *)ptr;
2402
2403 /* Check chip HA register for error event */
2404 eratt = lpfc_sli_check_eratt(phba);
2405
2406 if (eratt)
2407 /* Tell the worker thread there is work to do */
2408 lpfc_worker_wake_up(phba);
2409 else
2410 /* Restart the timer for next eratt poll */
2411 mod_timer(&phba->eratt_poll, jiffies +
2412 HZ * LPFC_ERATT_POLL_INTERVAL);
2413 return;
2414}
2415
875fbdfe 2416
e59058c4 2417/**
3621a710 2418 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2419 * @phba: Pointer to HBA context object.
2420 * @pring: Pointer to driver SLI ring object.
2421 * @mask: Host attention register mask for this ring.
2422 *
2423 * This function is called from the interrupt context when there is a ring
2424 * event for the fcp ring. The caller does not hold any lock.
2425 * The function processes each response iocb in the response ring until it
2426 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2427 * LE bit set. The function will call the completion handler of the command iocb
2428 * if the response iocb indicates a completion for a command iocb or it is
2429 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2430 * function if this is an unsolicited iocb.
dea3101e 2431 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2432 * to check it explicitly.
2433 */
2434int
2e0fef85
JS
2435lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2436 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2437{
34b02dcd 2438 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2439 IOCB_t *irsp = NULL;
87f6eaff 2440 IOCB_t *entry = NULL;
dea3101e
JB
2441 struct lpfc_iocbq *cmdiocbq = NULL;
2442 struct lpfc_iocbq rspiocbq;
dea3101e
JB
2443 uint32_t status;
2444 uint32_t portRspPut, portRspMax;
2445 int rc = 1;
2446 lpfc_iocb_type type;
2447 unsigned long iflag;
2448 uint32_t rsp_cmpl = 0;
dea3101e 2449
2e0fef85 2450 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2451 pring->stats.iocb_event++;
2452
dea3101e
JB
2453 /*
2454 * The next available response entry should never exceed the maximum
2455 * entries. If it does, treat it as an adapter hardware error.
2456 */
2457 portRspMax = pring->numRiocb;
2458 portRspPut = le32_to_cpu(pgp->rspPutInx);
2459 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2460 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2461 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2462 return 1;
2463 }
45ed1190
JS
2464 if (phba->fcp_ring_in_use) {
2465 spin_unlock_irqrestore(&phba->hbalock, iflag);
2466 return 1;
2467 } else
2468 phba->fcp_ring_in_use = 1;
dea3101e
JB
2469
2470 rmb();
2471 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2472 /*
2473 * Fetch an entry off the ring and copy it into a local data
2474 * structure. The copy involves a byte-swap since the
2475 * network byte order and pci byte orders are different.
2476 */
ed957684 2477 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2478 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2479
2480 if (++pring->rspidx >= portRspMax)
2481 pring->rspidx = 0;
2482
87f6eaff
JSEC
2483 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2484 (uint32_t *) &rspiocbq.iocb,
ed957684 2485 phba->iocb_rsp_size);
a4bc3379 2486 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2487 irsp = &rspiocbq.iocb;
2488
dea3101e
JB
2489 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2490 pring->stats.iocb_rsp++;
2491 rsp_cmpl++;
2492
2493 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2494 /*
2495 * If resource errors reported from HBA, reduce
2496 * queuedepths of the SCSI device.
2497 */
2498 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2499 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2500 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2501 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2502 spin_lock_irqsave(&phba->hbalock, iflag);
2503 }
2504
dea3101e
JB
2505 /* Rsp ring <ringno> error: IOCB */
2506 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2507 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2508 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2509 pring->ringno,
92d7f7b0
JS
2510 irsp->un.ulpWord[0],
2511 irsp->un.ulpWord[1],
2512 irsp->un.ulpWord[2],
2513 irsp->un.ulpWord[3],
2514 irsp->un.ulpWord[4],
2515 irsp->un.ulpWord[5],
d7c255b2
JS
2516 *(uint32_t *)&irsp->un1,
2517 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
2518 }
2519
2520 switch (type) {
2521 case LPFC_ABORT_IOCB:
2522 case LPFC_SOL_IOCB:
2523 /*
2524 * Idle exchange closed via ABTS from port. No iocb
2525 * resources need to be recovered.
2526 */
2527 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2529 "0333 IOCB cmd 0x%x"
dca9479b 2530 " processed. Skipping"
92d7f7b0 2531 " completion\n",
dca9479b 2532 irsp->ulpCommand);
dea3101e
JB
2533 break;
2534 }
2535
604a3e30
JB
2536 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2537 &rspiocbq);
0f65ff68
JS
2538 if (unlikely(!cmdiocbq))
2539 break;
2540 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2541 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2542 if (cmdiocbq->iocb_cmpl) {
2543 spin_unlock_irqrestore(&phba->hbalock, iflag);
2544 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2545 &rspiocbq);
2546 spin_lock_irqsave(&phba->hbalock, iflag);
2547 }
dea3101e 2548 break;
a4bc3379 2549 case LPFC_UNSOL_IOCB:
2e0fef85 2550 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2551 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2552 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2553 break;
dea3101e
JB
2554 default:
2555 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2556 char adaptermsg[LPFC_MAX_ADPTMSG];
2557 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2558 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2559 MAX_MSG_DATA);
898eb71c
JP
2560 dev_warn(&((phba->pcidev)->dev),
2561 "lpfc%d: %s\n",
dea3101e
JB
2562 phba->brd_no, adaptermsg);
2563 } else {
2564 /* Unknown IOCB command */
2565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2566 "0334 Unknown IOCB command "
92d7f7b0 2567 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2568 type, irsp->ulpCommand,
92d7f7b0
JS
2569 irsp->ulpStatus,
2570 irsp->ulpIoTag,
2571 irsp->ulpContext);
dea3101e
JB
2572 }
2573 break;
2574 }
2575
2576 /*
2577 * The response IOCB has been processed. Update the ring
2578 * pointer in SLIM. If the port response put pointer has not
2579 * been updated, sync the pgp->rspPutInx and fetch the new port
2580 * response put pointer.
2581 */
ed957684 2582 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e
JB
2583
2584 if (pring->rspidx == portRspPut)
2585 portRspPut = le32_to_cpu(pgp->rspPutInx);
2586 }
2587
2588 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2589 pring->stats.iocb_rsp_full++;
2590 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2591 writel(status, phba->CAregaddr);
2592 readl(phba->CAregaddr);
2593 }
2594 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2595 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2596 pring->stats.iocb_cmd_empty++;
2597
2598 /* Force update of the local copy of cmdGetInx */
2599 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2600 lpfc_sli_resume_iocb(phba, pring);
2601
2602 if ((pring->lpfc_sli_cmd_available))
2603 (pring->lpfc_sli_cmd_available) (phba, pring);
2604
2605 }
2606
45ed1190 2607 phba->fcp_ring_in_use = 0;
2e0fef85 2608 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2609 return rc;
2610}
2611
e59058c4 2612/**
3772a991
JS
2613 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2614 * @phba: Pointer to HBA context object.
2615 * @pring: Pointer to driver SLI ring object.
2616 * @rspiocbp: Pointer to driver response IOCB object.
2617 *
2618 * This function is called from the worker thread when there is a slow-path
2619 * response IOCB to process. This function chains all the response iocbs until
2620 * seeing the iocb with the LE bit set. The function will call
2621 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2622 * completion of a command iocb. The function will call the
2623 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2624 * The function frees the resources or calls the completion handler if this
2625 * iocb is an abort completion. The function returns NULL when the response
2626 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2627 * this function shall chain the iocb on to the iocb_continueq and return the
2628 * response iocb passed in.
2629 **/
2630static struct lpfc_iocbq *
2631lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2632 struct lpfc_iocbq *rspiocbp)
2633{
2634 struct lpfc_iocbq *saveq;
2635 struct lpfc_iocbq *cmdiocbp;
2636 struct lpfc_iocbq *next_iocb;
2637 IOCB_t *irsp = NULL;
2638 uint32_t free_saveq;
2639 uint8_t iocb_cmd_type;
2640 lpfc_iocb_type type;
2641 unsigned long iflag;
2642 int rc;
2643
2644 spin_lock_irqsave(&phba->hbalock, iflag);
2645 /* First add the response iocb to the countinueq list */
2646 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2647 pring->iocb_continueq_cnt++;
2648
2649 /* Now, determine whetehr the list is completed for processing */
2650 irsp = &rspiocbp->iocb;
2651 if (irsp->ulpLe) {
2652 /*
2653 * By default, the driver expects to free all resources
2654 * associated with this iocb completion.
2655 */
2656 free_saveq = 1;
2657 saveq = list_get_first(&pring->iocb_continueq,
2658 struct lpfc_iocbq, list);
2659 irsp = &(saveq->iocb);
2660 list_del_init(&pring->iocb_continueq);
2661 pring->iocb_continueq_cnt = 0;
2662
2663 pring->stats.iocb_rsp++;
2664
2665 /*
2666 * If resource errors reported from HBA, reduce
2667 * queuedepths of the SCSI device.
2668 */
2669 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2670 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2671 spin_unlock_irqrestore(&phba->hbalock, iflag);
2672 phba->lpfc_rampdown_queue_depth(phba);
2673 spin_lock_irqsave(&phba->hbalock, iflag);
2674 }
2675
2676 if (irsp->ulpStatus) {
2677 /* Rsp ring <ringno> error: IOCB */
2678 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2679 "0328 Rsp Ring %d error: "
2680 "IOCB Data: "
2681 "x%x x%x x%x x%x "
2682 "x%x x%x x%x x%x "
2683 "x%x x%x x%x x%x "
2684 "x%x x%x x%x x%x\n",
2685 pring->ringno,
2686 irsp->un.ulpWord[0],
2687 irsp->un.ulpWord[1],
2688 irsp->un.ulpWord[2],
2689 irsp->un.ulpWord[3],
2690 irsp->un.ulpWord[4],
2691 irsp->un.ulpWord[5],
2692 *(((uint32_t *) irsp) + 6),
2693 *(((uint32_t *) irsp) + 7),
2694 *(((uint32_t *) irsp) + 8),
2695 *(((uint32_t *) irsp) + 9),
2696 *(((uint32_t *) irsp) + 10),
2697 *(((uint32_t *) irsp) + 11),
2698 *(((uint32_t *) irsp) + 12),
2699 *(((uint32_t *) irsp) + 13),
2700 *(((uint32_t *) irsp) + 14),
2701 *(((uint32_t *) irsp) + 15));
2702 }
2703
2704 /*
2705 * Fetch the IOCB command type and call the correct completion
2706 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2707 * get freed back to the lpfc_iocb_list by the discovery
2708 * kernel thread.
2709 */
2710 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2711 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2712 switch (type) {
2713 case LPFC_SOL_IOCB:
2714 spin_unlock_irqrestore(&phba->hbalock, iflag);
2715 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2716 spin_lock_irqsave(&phba->hbalock, iflag);
2717 break;
2718
2719 case LPFC_UNSOL_IOCB:
2720 spin_unlock_irqrestore(&phba->hbalock, iflag);
2721 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2722 spin_lock_irqsave(&phba->hbalock, iflag);
2723 if (!rc)
2724 free_saveq = 0;
2725 break;
2726
2727 case LPFC_ABORT_IOCB:
2728 cmdiocbp = NULL;
2729 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2730 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2731 saveq);
2732 if (cmdiocbp) {
2733 /* Call the specified completion routine */
2734 if (cmdiocbp->iocb_cmpl) {
2735 spin_unlock_irqrestore(&phba->hbalock,
2736 iflag);
2737 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2738 saveq);
2739 spin_lock_irqsave(&phba->hbalock,
2740 iflag);
2741 } else
2742 __lpfc_sli_release_iocbq(phba,
2743 cmdiocbp);
2744 }
2745 break;
2746
2747 case LPFC_UNKNOWN_IOCB:
2748 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2749 char adaptermsg[LPFC_MAX_ADPTMSG];
2750 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2751 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2752 MAX_MSG_DATA);
2753 dev_warn(&((phba->pcidev)->dev),
2754 "lpfc%d: %s\n",
2755 phba->brd_no, adaptermsg);
2756 } else {
2757 /* Unknown IOCB command */
2758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2759 "0335 Unknown IOCB "
2760 "command Data: x%x "
2761 "x%x x%x x%x\n",
2762 irsp->ulpCommand,
2763 irsp->ulpStatus,
2764 irsp->ulpIoTag,
2765 irsp->ulpContext);
2766 }
2767 break;
2768 }
2769
2770 if (free_saveq) {
2771 list_for_each_entry_safe(rspiocbp, next_iocb,
2772 &saveq->list, list) {
2773 list_del(&rspiocbp->list);
2774 __lpfc_sli_release_iocbq(phba, rspiocbp);
2775 }
2776 __lpfc_sli_release_iocbq(phba, saveq);
2777 }
2778 rspiocbp = NULL;
2779 }
2780 spin_unlock_irqrestore(&phba->hbalock, iflag);
2781 return rspiocbp;
2782}
2783
2784/**
2785 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2786 * @phba: Pointer to HBA context object.
2787 * @pring: Pointer to driver SLI ring object.
2788 * @mask: Host attention register mask for this ring.
2789 *
3772a991
JS
2790 * This routine wraps the actual slow_ring event process routine from the
2791 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2792 **/
3772a991 2793void
2e0fef85
JS
2794lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2795 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2796{
2797 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2798}
2799
2800/**
2801 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2802 * @phba: Pointer to HBA context object.
2803 * @pring: Pointer to driver SLI ring object.
2804 * @mask: Host attention register mask for this ring.
2805 *
2806 * This function is called from the worker thread when there is a ring event
2807 * for non-fcp rings. The caller does not hold any lock. The function will
2808 * remove each response iocb in the response ring and calls the handle
2809 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2810 **/
2811static void
2812lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2813 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2814{
34b02dcd 2815 struct lpfc_pgp *pgp;
dea3101e
JB
2816 IOCB_t *entry;
2817 IOCB_t *irsp = NULL;
2818 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2819 uint32_t portRspPut, portRspMax;
dea3101e 2820 unsigned long iflag;
3772a991 2821 uint32_t status;
dea3101e 2822
34b02dcd 2823 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2824 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2825 pring->stats.iocb_event++;
2826
dea3101e
JB
2827 /*
2828 * The next available response entry should never exceed the maximum
2829 * entries. If it does, treat it as an adapter hardware error.
2830 */
2831 portRspMax = pring->numRiocb;
2832 portRspPut = le32_to_cpu(pgp->rspPutInx);
2833 if (portRspPut >= portRspMax) {
2834 /*
025dfdaf 2835 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
2836 * rsp ring <portRspMax>
2837 */
ed957684 2838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2839 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2840 "is bigger than rsp ring %d\n",
e8b62011 2841 pring->ringno, portRspPut, portRspMax);
dea3101e 2842
2e0fef85
JS
2843 phba->link_state = LPFC_HBA_ERROR;
2844 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2845
2846 phba->work_hs = HS_FFER3;
2847 lpfc_handle_eratt(phba);
2848
3772a991 2849 return;
dea3101e
JB
2850 }
2851
2852 rmb();
dea3101e
JB
2853 while (pring->rspidx != portRspPut) {
2854 /*
2855 * Build a completion list and call the appropriate handler.
2856 * The process is to get the next available response iocb, get
2857 * a free iocb from the list, copy the response data into the
2858 * free iocb, insert to the continuation list, and update the
2859 * next response index to slim. This process makes response
2860 * iocb's in the ring available to DMA as fast as possible but
2861 * pays a penalty for a copy operation. Since the iocb is
2862 * only 32 bytes, this penalty is considered small relative to
2863 * the PCI reads for register values and a slim write. When
2864 * the ulpLe field is set, the entire Command has been
2865 * received.
2866 */
ed957684
JS
2867 entry = lpfc_resp_iocb(phba, pring);
2868
858c9f6c 2869 phba->last_completion_time = jiffies;
2e0fef85 2870 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
2871 if (rspiocbp == NULL) {
2872 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2873 "completion.\n", __func__);
dea3101e
JB
2874 break;
2875 }
2876
ed957684
JS
2877 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2878 phba->iocb_rsp_size);
dea3101e
JB
2879 irsp = &rspiocbp->iocb;
2880
2881 if (++pring->rspidx >= portRspMax)
2882 pring->rspidx = 0;
2883
a58cbd52
JS
2884 if (pring->ringno == LPFC_ELS_RING) {
2885 lpfc_debugfs_slow_ring_trc(phba,
2886 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2887 *(((uint32_t *) irsp) + 4),
2888 *(((uint32_t *) irsp) + 6),
2889 *(((uint32_t *) irsp) + 7));
2890 }
2891
ed957684 2892 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2893
3772a991
JS
2894 spin_unlock_irqrestore(&phba->hbalock, iflag);
2895 /* Handle the response IOCB */
2896 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2897 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2898
2899 /*
2900 * If the port response put pointer has not been updated, sync
2901 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2902 * response put pointer.
2903 */
2904 if (pring->rspidx == portRspPut) {
2905 portRspPut = le32_to_cpu(pgp->rspPutInx);
2906 }
2907 } /* while (pring->rspidx != portRspPut) */
2908
92d7f7b0 2909 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
2910 /* At least one response entry has been freed */
2911 pring->stats.iocb_rsp_full++;
2912 /* SET RxRE_RSP in Chip Att register */
2913 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2914 writel(status, phba->CAregaddr);
2915 readl(phba->CAregaddr); /* flush */
2916 }
2917 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2918 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2919 pring->stats.iocb_cmd_empty++;
2920
2921 /* Force update of the local copy of cmdGetInx */
2922 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2923 lpfc_sli_resume_iocb(phba, pring);
2924
2925 if ((pring->lpfc_sli_cmd_available))
2926 (pring->lpfc_sli_cmd_available) (phba, pring);
2927
2928 }
2929
2e0fef85 2930 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2931 return;
dea3101e
JB
2932}
2933
4f774513
JS
2934/**
2935 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2936 * @phba: Pointer to HBA context object.
2937 * @pring: Pointer to driver SLI ring object.
2938 * @mask: Host attention register mask for this ring.
2939 *
2940 * This function is called from the worker thread when there is a pending
2941 * ELS response iocb on the driver internal slow-path response iocb worker
2942 * queue. The caller does not hold any lock. The function will remove each
2943 * response iocb from the response worker queue and calls the handle
2944 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2945 **/
2946static void
2947lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2948 struct lpfc_sli_ring *pring, uint32_t mask)
2949{
2950 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
2951 struct hbq_dmabuf *dmabuf;
2952 struct lpfc_cq_event *cq_event;
4f774513
JS
2953 unsigned long iflag;
2954
45ed1190
JS
2955 spin_lock_irqsave(&phba->hbalock, iflag);
2956 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2957 spin_unlock_irqrestore(&phba->hbalock, iflag);
2958 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
2959 /* Get the response iocb from the head of work queue */
2960 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 2961 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 2962 cq_event, struct lpfc_cq_event, list);
4f774513 2963 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
2964
2965 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2966 case CQE_CODE_COMPL_WQE:
2967 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2968 cq_event);
45ed1190
JS
2969 /* Translate ELS WCQE to response IOCBQ */
2970 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2971 irspiocbq);
2972 if (irspiocbq)
2973 lpfc_sli_sp_handle_rspiocb(phba, pring,
2974 irspiocbq);
4d9ab994
JS
2975 break;
2976 case CQE_CODE_RECEIVE:
2977 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2978 cq_event);
2979 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2980 break;
2981 default:
2982 break;
2983 }
4f774513
JS
2984 }
2985}
2986
e59058c4 2987/**
3621a710 2988 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
2989 * @phba: Pointer to HBA context object.
2990 * @pring: Pointer to driver SLI ring object.
2991 *
2992 * This function aborts all iocbs in the given ring and frees all the iocb
2993 * objects in txq. This function issues an abort iocb for all the iocb commands
2994 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2995 * the return of this function. The caller is not required to hold any locks.
2996 **/
2e0fef85 2997void
dea3101e
JB
2998lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2999{
2534ba75 3000 LIST_HEAD(completions);
dea3101e 3001 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3002
92d7f7b0
JS
3003 if (pring->ringno == LPFC_ELS_RING) {
3004 lpfc_fabric_abort_hba(phba);
3005 }
3006
dea3101e
JB
3007 /* Error everything on txq and txcmplq
3008 * First do the txq.
3009 */
2e0fef85 3010 spin_lock_irq(&phba->hbalock);
2534ba75 3011 list_splice_init(&pring->txq, &completions);
dea3101e 3012 pring->txq_cnt = 0;
dea3101e
JB
3013
3014 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3015 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3016 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3017
2e0fef85 3018 spin_unlock_irq(&phba->hbalock);
dea3101e 3019
a257bf90
JS
3020 /* Cancel all the IOCBs from the completions list */
3021 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3022 IOERR_SLI_ABORTED);
dea3101e
JB
3023}
3024
a8e497d5 3025/**
3621a710 3026 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3027 * @phba: Pointer to HBA context object.
3028 *
3029 * This function flushes all iocbs in the fcp ring and frees all the iocb
3030 * objects in txq and txcmplq. This function will not issue abort iocbs
3031 * for all the iocb commands in txcmplq, they will just be returned with
3032 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3033 * slot has been permanently disabled.
3034 **/
3035void
3036lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3037{
3038 LIST_HEAD(txq);
3039 LIST_HEAD(txcmplq);
a8e497d5
JS
3040 struct lpfc_sli *psli = &phba->sli;
3041 struct lpfc_sli_ring *pring;
3042
3043 /* Currently, only one fcp ring */
3044 pring = &psli->ring[psli->fcp_ring];
3045
3046 spin_lock_irq(&phba->hbalock);
3047 /* Retrieve everything on txq */
3048 list_splice_init(&pring->txq, &txq);
3049 pring->txq_cnt = 0;
3050
3051 /* Retrieve everything on the txcmplq */
3052 list_splice_init(&pring->txcmplq, &txcmplq);
3053 pring->txcmplq_cnt = 0;
3054 spin_unlock_irq(&phba->hbalock);
3055
3056 /* Flush the txq */
a257bf90
JS
3057 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3058 IOERR_SLI_DOWN);
a8e497d5
JS
3059
3060 /* Flush the txcmpq */
a257bf90
JS
3061 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3062 IOERR_SLI_DOWN);
a8e497d5
JS
3063}
3064
e59058c4 3065/**
3772a991 3066 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3067 * @phba: Pointer to HBA context object.
3068 * @mask: Bit mask to be checked.
3069 *
3070 * This function reads the host status register and compares
3071 * with the provided bit mask to check if HBA completed
3072 * the restart. This function will wait in a loop for the
3073 * HBA to complete restart. If the HBA does not restart within
3074 * 15 iterations, the function will reset the HBA again. The
3075 * function returns 1 when HBA fail to restart otherwise returns
3076 * zero.
3077 **/
3772a991
JS
3078static int
3079lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3080{
41415862
JW
3081 uint32_t status;
3082 int i = 0;
3083 int retval = 0;
dea3101e 3084
41415862
JW
3085 /* Read the HBA Host Status Register */
3086 status = readl(phba->HSregaddr);
dea3101e 3087
41415862
JW
3088 /*
3089 * Check status register every 100ms for 5 retries, then every
3090 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3091 * every 2.5 sec for 4.
3092 * Break our of the loop if errors occurred during init.
3093 */
3094 while (((status & mask) != mask) &&
3095 !(status & HS_FFERM) &&
3096 i++ < 20) {
dea3101e 3097
41415862
JW
3098 if (i <= 5)
3099 msleep(10);
3100 else if (i <= 10)
3101 msleep(500);
3102 else
3103 msleep(2500);
dea3101e 3104
41415862 3105 if (i == 15) {
2e0fef85 3106 /* Do post */
92d7f7b0 3107 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3108 lpfc_sli_brdrestart(phba);
3109 }
3110 /* Read the HBA Host Status Register */
3111 status = readl(phba->HSregaddr);
3112 }
dea3101e 3113
41415862
JW
3114 /* Check to see if any errors occurred during init */
3115 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3117 "2751 Adapter failed to restart, "
3118 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3119 status,
3120 readl(phba->MBslimaddr + 0xa8),
3121 readl(phba->MBslimaddr + 0xac));
2e0fef85 3122 phba->link_state = LPFC_HBA_ERROR;
41415862 3123 retval = 1;
dea3101e 3124 }
dea3101e 3125
41415862
JW
3126 return retval;
3127}
dea3101e 3128
da0436e9
JS
3129/**
3130 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3131 * @phba: Pointer to HBA context object.
3132 * @mask: Bit mask to be checked.
3133 *
3134 * This function checks the host status register to check if HBA is
3135 * ready. This function will wait in a loop for the HBA to be ready
3136 * If the HBA is not ready , the function will will reset the HBA PCI
3137 * function again. The function returns 1 when HBA fail to be ready
3138 * otherwise returns zero.
3139 **/
3140static int
3141lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3142{
3143 uint32_t status;
3144 int retval = 0;
3145
3146 /* Read the HBA Host Status Register */
3147 status = lpfc_sli4_post_status_check(phba);
3148
3149 if (status) {
3150 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3151 lpfc_sli_brdrestart(phba);
3152 status = lpfc_sli4_post_status_check(phba);
3153 }
3154
3155 /* Check to see if any errors occurred during init */
3156 if (status) {
3157 phba->link_state = LPFC_HBA_ERROR;
3158 retval = 1;
3159 } else
3160 phba->sli4_hba.intr_enable = 0;
3161
3162 return retval;
3163}
3164
3165/**
3166 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3167 * @phba: Pointer to HBA context object.
3168 * @mask: Bit mask to be checked.
3169 *
3170 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3171 * from the API jump table function pointer from the lpfc_hba struct.
3172 **/
3173int
3174lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3175{
3176 return phba->lpfc_sli_brdready(phba, mask);
3177}
3178
9290831f
JS
3179#define BARRIER_TEST_PATTERN (0xdeadbeef)
3180
e59058c4 3181/**
3621a710 3182 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3183 * @phba: Pointer to HBA context object.
3184 *
3185 * This function is called before resetting an HBA. This
3186 * function requests HBA to quiesce DMAs before a reset.
3187 **/
2e0fef85 3188void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3189{
65a29c16
JS
3190 uint32_t __iomem *resp_buf;
3191 uint32_t __iomem *mbox_buf;
9290831f
JS
3192 volatile uint32_t mbox;
3193 uint32_t hc_copy;
3194 int i;
3195 uint8_t hdrtype;
3196
3197 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3198 if (hdrtype != 0x80 ||
3199 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3200 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3201 return;
3202
3203 /*
3204 * Tell the other part of the chip to suspend temporarily all
3205 * its DMA activity.
3206 */
65a29c16 3207 resp_buf = phba->MBslimaddr;
9290831f
JS
3208
3209 /* Disable the error attention */
3210 hc_copy = readl(phba->HCregaddr);
3211 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3212 readl(phba->HCregaddr); /* flush */
2e0fef85 3213 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3214
3215 if (readl(phba->HAregaddr) & HA_ERATT) {
3216 /* Clear Chip error bit */
3217 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3218 phba->pport->stopped = 1;
9290831f
JS
3219 }
3220
3221 mbox = 0;
3222 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3223 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3224
3225 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3226 mbox_buf = phba->MBslimaddr;
9290831f
JS
3227 writel(mbox, mbox_buf);
3228
3229 for (i = 0;
3230 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3231 mdelay(1);
3232
3233 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3234 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3235 phba->pport->stopped)
9290831f
JS
3236 goto restore_hc;
3237 else
3238 goto clear_errat;
3239 }
3240
3241 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3242 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3243 mdelay(1);
3244
3245clear_errat:
3246
3247 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3248 mdelay(1);
3249
3250 if (readl(phba->HAregaddr) & HA_ERATT) {
3251 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3252 phba->pport->stopped = 1;
9290831f
JS
3253 }
3254
3255restore_hc:
2e0fef85 3256 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3257 writel(hc_copy, phba->HCregaddr);
3258 readl(phba->HCregaddr); /* flush */
3259}
3260
e59058c4 3261/**
3621a710 3262 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3263 * @phba: Pointer to HBA context object.
3264 *
3265 * This function issues a kill_board mailbox command and waits for
3266 * the error attention interrupt. This function is called for stopping
3267 * the firmware processing. The caller is not required to hold any
3268 * locks. This function calls lpfc_hba_down_post function to free
3269 * any pending commands after the kill. The function will return 1 when it
3270 * fails to kill the board else will return 0.
3271 **/
41415862 3272int
2e0fef85 3273lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3274{
3275 struct lpfc_sli *psli;
3276 LPFC_MBOXQ_t *pmb;
3277 uint32_t status;
3278 uint32_t ha_copy;
3279 int retval;
3280 int i = 0;
dea3101e 3281
41415862 3282 psli = &phba->sli;
dea3101e 3283
41415862 3284 /* Kill HBA */
ed957684 3285 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3286 "0329 Kill HBA Data: x%x x%x\n",
3287 phba->pport->port_state, psli->sli_flag);
41415862 3288
98c9ea5c
JS
3289 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3290 if (!pmb)
41415862 3291 return 1;
41415862
JW
3292
3293 /* Disable the error attention */
2e0fef85 3294 spin_lock_irq(&phba->hbalock);
41415862
JW
3295 status = readl(phba->HCregaddr);
3296 status &= ~HC_ERINT_ENA;
3297 writel(status, phba->HCregaddr);
3298 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3299 phba->link_flag |= LS_IGNORE_ERATT;
3300 spin_unlock_irq(&phba->hbalock);
41415862
JW
3301
3302 lpfc_kill_board(phba, pmb);
3303 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3304 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3305
3306 if (retval != MBX_SUCCESS) {
3307 if (retval != MBX_BUSY)
3308 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3310 "2752 KILL_BOARD command failed retval %d\n",
3311 retval);
2e0fef85
JS
3312 spin_lock_irq(&phba->hbalock);
3313 phba->link_flag &= ~LS_IGNORE_ERATT;
3314 spin_unlock_irq(&phba->hbalock);
41415862
JW
3315 return 1;
3316 }
3317
f4b4c68f
JS
3318 spin_lock_irq(&phba->hbalock);
3319 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3320 spin_unlock_irq(&phba->hbalock);
9290831f 3321
41415862
JW
3322 mempool_free(pmb, phba->mbox_mem_pool);
3323
3324 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3325 * attention every 100ms for 3 seconds. If we don't get ERATT after
3326 * 3 seconds we still set HBA_ERROR state because the status of the
3327 * board is now undefined.
3328 */
3329 ha_copy = readl(phba->HAregaddr);
3330
3331 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3332 mdelay(100);
3333 ha_copy = readl(phba->HAregaddr);
3334 }
3335
3336 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3337 if (ha_copy & HA_ERATT) {
3338 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3339 phba->pport->stopped = 1;
9290831f 3340 }
2e0fef85 3341 spin_lock_irq(&phba->hbalock);
41415862 3342 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3343 psli->mbox_active = NULL;
2e0fef85
JS
3344 phba->link_flag &= ~LS_IGNORE_ERATT;
3345 spin_unlock_irq(&phba->hbalock);
41415862 3346
41415862 3347 lpfc_hba_down_post(phba);
2e0fef85 3348 phba->link_state = LPFC_HBA_ERROR;
41415862 3349
2e0fef85 3350 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
3351}
3352
e59058c4 3353/**
3772a991 3354 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3355 * @phba: Pointer to HBA context object.
3356 *
3357 * This function resets the HBA by writing HC_INITFF to the control
3358 * register. After the HBA resets, this function resets all the iocb ring
3359 * indices. This function disables PCI layer parity checking during
3360 * the reset.
3361 * This function returns 0 always.
3362 * The caller is not required to hold any locks.
3363 **/
41415862 3364int
2e0fef85 3365lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3366{
41415862 3367 struct lpfc_sli *psli;
dea3101e 3368 struct lpfc_sli_ring *pring;
41415862 3369 uint16_t cfg_value;
dea3101e 3370 int i;
dea3101e 3371
41415862 3372 psli = &phba->sli;
dea3101e 3373
41415862
JW
3374 /* Reset HBA */
3375 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3376 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3377 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
3378
3379 /* perform board reset */
3380 phba->fc_eventTag = 0;
4d9ab994 3381 phba->link_events = 0;
2e0fef85
JS
3382 phba->pport->fc_myDID = 0;
3383 phba->pport->fc_prevDID = 0;
dea3101e 3384
41415862
JW
3385 /* Turn off parity checking and serr during the physical reset */
3386 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3387 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3388 (cfg_value &
3389 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3390
3772a991
JS
3391 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3392
41415862
JW
3393 /* Now toggle INITFF bit in the Host Control Register */
3394 writel(HC_INITFF, phba->HCregaddr);
3395 mdelay(1);
3396 readl(phba->HCregaddr); /* flush */
3397 writel(0, phba->HCregaddr);
3398 readl(phba->HCregaddr); /* flush */
3399
3400 /* Restore PCI cmd register */
3401 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
3402
3403 /* Initialize relevant SLI info */
41415862
JW
3404 for (i = 0; i < psli->num_rings; i++) {
3405 pring = &psli->ring[i];
dea3101e
JB
3406 pring->flag = 0;
3407 pring->rspidx = 0;
3408 pring->next_cmdidx = 0;
3409 pring->local_getidx = 0;
3410 pring->cmdidx = 0;
3411 pring->missbufcnt = 0;
3412 }
dea3101e 3413
2e0fef85 3414 phba->link_state = LPFC_WARM_START;
41415862
JW
3415 return 0;
3416}
3417
e59058c4 3418/**
da0436e9
JS
3419 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3420 * @phba: Pointer to HBA context object.
3421 *
3422 * This function resets a SLI4 HBA. This function disables PCI layer parity
3423 * checking during resets the device. The caller is not required to hold
3424 * any locks.
3425 *
3426 * This function returns 0 always.
3427 **/
3428int
3429lpfc_sli4_brdreset(struct lpfc_hba *phba)
3430{
3431 struct lpfc_sli *psli = &phba->sli;
3432 uint16_t cfg_value;
3433 uint8_t qindx;
3434
3435 /* Reset HBA */
3436 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3437 "0295 Reset HBA Data: x%x x%x\n",
3438 phba->pport->port_state, psli->sli_flag);
3439
3440 /* perform board reset */
3441 phba->fc_eventTag = 0;
4d9ab994 3442 phba->link_events = 0;
da0436e9
JS
3443 phba->pport->fc_myDID = 0;
3444 phba->pport->fc_prevDID = 0;
3445
3446 /* Turn off parity checking and serr during the physical reset */
3447 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3448 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3449 (cfg_value &
3450 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3451
3452 spin_lock_irq(&phba->hbalock);
3453 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3454 phba->fcf.fcf_flag = 0;
3455 /* Clean up the child queue list for the CQs */
3456 list_del_init(&phba->sli4_hba.mbx_wq->list);
3457 list_del_init(&phba->sli4_hba.els_wq->list);
3458 list_del_init(&phba->sli4_hba.hdr_rq->list);
3459 list_del_init(&phba->sli4_hba.dat_rq->list);
3460 list_del_init(&phba->sli4_hba.mbx_cq->list);
3461 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3462 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3463 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3464 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3465 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3466 spin_unlock_irq(&phba->hbalock);
3467
3468 /* Now physically reset the device */
3469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3470 "0389 Performing PCI function reset!\n");
3471 /* Perform FCoE PCI function reset */
3472 lpfc_pci_function_reset(phba);
3473
3474 return 0;
3475}
3476
3477/**
3478 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3479 * @phba: Pointer to HBA context object.
3480 *
3481 * This function is called in the SLI initialization code path to
3482 * restart the HBA. The caller is not required to hold any lock.
3483 * This function writes MBX_RESTART mailbox command to the SLIM and
3484 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3485 * function to free any pending commands. The function enables
3486 * POST only during the first initialization. The function returns zero.
3487 * The function does not guarantee completion of MBX_RESTART mailbox
3488 * command before the return of this function.
3489 **/
da0436e9
JS
3490static int
3491lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3492{
3493 MAILBOX_t *mb;
3494 struct lpfc_sli *psli;
41415862
JW
3495 volatile uint32_t word0;
3496 void __iomem *to_slim;
0d878419 3497 uint32_t hba_aer_enabled;
41415862 3498
2e0fef85 3499 spin_lock_irq(&phba->hbalock);
41415862 3500
0d878419
JS
3501 /* Take PCIe device Advanced Error Reporting (AER) state */
3502 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3503
41415862
JW
3504 psli = &phba->sli;
3505
3506 /* Restart HBA */
3507 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3508 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3509 phba->pport->port_state, psli->sli_flag);
41415862
JW
3510
3511 word0 = 0;
3512 mb = (MAILBOX_t *) &word0;
3513 mb->mbxCommand = MBX_RESTART;
3514 mb->mbxHc = 1;
3515
9290831f
JS
3516 lpfc_reset_barrier(phba);
3517
41415862
JW
3518 to_slim = phba->MBslimaddr;
3519 writel(*(uint32_t *) mb, to_slim);
3520 readl(to_slim); /* flush */
3521
3522 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3523 if (phba->pport->port_state)
41415862 3524 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3525 else
41415862 3526 word0 = 0; /* This is really setting up word1 */
65a29c16 3527 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3528 writel(*(uint32_t *) mb, to_slim);
3529 readl(to_slim); /* flush */
dea3101e 3530
41415862 3531 lpfc_sli_brdreset(phba);
2e0fef85
JS
3532 phba->pport->stopped = 0;
3533 phba->link_state = LPFC_INIT_START;
da0436e9 3534 phba->hba_flag = 0;
2e0fef85 3535 spin_unlock_irq(&phba->hbalock);
41415862 3536
64ba8818
JS
3537 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3538 psli->stats_start = get_seconds();
3539
eaf15d5b
JS
3540 /* Give the INITFF and Post time to settle. */
3541 mdelay(100);
41415862 3542
0d878419
JS
3543 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3544 if (hba_aer_enabled)
3545 pci_disable_pcie_error_reporting(phba->pcidev);
3546
41415862 3547 lpfc_hba_down_post(phba);
dea3101e
JB
3548
3549 return 0;
3550}
3551
da0436e9
JS
3552/**
3553 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3554 * @phba: Pointer to HBA context object.
3555 *
3556 * This function is called in the SLI initialization code path to restart
3557 * a SLI4 HBA. The caller is not required to hold any lock.
3558 * At the end of the function, it calls lpfc_hba_down_post function to
3559 * free any pending commands.
3560 **/
3561static int
3562lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3563{
3564 struct lpfc_sli *psli = &phba->sli;
3565
3566
3567 /* Restart HBA */
3568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3569 "0296 Restart HBA Data: x%x x%x\n",
3570 phba->pport->port_state, psli->sli_flag);
3571
3572 lpfc_sli4_brdreset(phba);
3573
3574 spin_lock_irq(&phba->hbalock);
3575 phba->pport->stopped = 0;
3576 phba->link_state = LPFC_INIT_START;
3577 phba->hba_flag = 0;
3578 spin_unlock_irq(&phba->hbalock);
3579
3580 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3581 psli->stats_start = get_seconds();
3582
3583 lpfc_hba_down_post(phba);
3584
3585 return 0;
3586}
3587
3588/**
3589 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3590 * @phba: Pointer to HBA context object.
3591 *
3592 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3593 * API jump table function pointer from the lpfc_hba struct.
3594**/
3595int
3596lpfc_sli_brdrestart(struct lpfc_hba *phba)
3597{
3598 return phba->lpfc_sli_brdrestart(phba);
3599}
3600
e59058c4 3601/**
3621a710 3602 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3603 * @phba: Pointer to HBA context object.
3604 *
3605 * This function is called after a HBA restart to wait for successful
3606 * restart of the HBA. Successful restart of the HBA is indicated by
3607 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3608 * iteration, the function will restart the HBA again. The function returns
3609 * zero if HBA successfully restarted else returns negative error code.
3610 **/
dea3101e
JB
3611static int
3612lpfc_sli_chipset_init(struct lpfc_hba *phba)
3613{
3614 uint32_t status, i = 0;
3615
3616 /* Read the HBA Host Status Register */
3617 status = readl(phba->HSregaddr);
3618
3619 /* Check status register to see what current state is */
3620 i = 0;
3621 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3622
3623 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3624 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3625 * 4.
3626 */
3627 if (i++ >= 20) {
3628 /* Adapter failed to init, timeout, status reg
3629 <status> */
ed957684 3630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3631 "0436 Adapter failed to init, "
09372820
JS
3632 "timeout, status reg x%x, "
3633 "FW Data: A8 x%x AC x%x\n", status,
3634 readl(phba->MBslimaddr + 0xa8),
3635 readl(phba->MBslimaddr + 0xac));
2e0fef85 3636 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3637 return -ETIMEDOUT;
3638 }
3639
3640 /* Check to see if any errors occurred during init */
3641 if (status & HS_FFERM) {
3642 /* ERROR: During chipset initialization */
3643 /* Adapter failed to init, chipset, status reg
3644 <status> */
ed957684 3645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3646 "0437 Adapter failed to init, "
09372820
JS
3647 "chipset, status reg x%x, "
3648 "FW Data: A8 x%x AC x%x\n", status,
3649 readl(phba->MBslimaddr + 0xa8),
3650 readl(phba->MBslimaddr + 0xac));
2e0fef85 3651 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3652 return -EIO;
3653 }
3654
3655 if (i <= 5) {
3656 msleep(10);
3657 } else if (i <= 10) {
3658 msleep(500);
3659 } else {
3660 msleep(2500);
3661 }
3662
3663 if (i == 15) {
2e0fef85 3664 /* Do post */
92d7f7b0 3665 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3666 lpfc_sli_brdrestart(phba);
dea3101e
JB
3667 }
3668 /* Read the HBA Host Status Register */
3669 status = readl(phba->HSregaddr);
3670 }
3671
3672 /* Check to see if any errors occurred during init */
3673 if (status & HS_FFERM) {
3674 /* ERROR: During chipset initialization */
3675 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3677 "0438 Adapter failed to init, chipset, "
09372820
JS
3678 "status reg x%x, "
3679 "FW Data: A8 x%x AC x%x\n", status,
3680 readl(phba->MBslimaddr + 0xa8),
3681 readl(phba->MBslimaddr + 0xac));
2e0fef85 3682 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3683 return -EIO;
3684 }
3685
3686 /* Clear all interrupt enable conditions */
3687 writel(0, phba->HCregaddr);
3688 readl(phba->HCregaddr); /* flush */
3689
3690 /* setup host attn register */
3691 writel(0xffffffff, phba->HAregaddr);
3692 readl(phba->HAregaddr); /* flush */
3693 return 0;
3694}
3695
e59058c4 3696/**
3621a710 3697 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3698 *
3699 * This function calculates and returns the number of HBQs required to be
3700 * configured.
3701 **/
78b2d852 3702int
ed957684
JS
3703lpfc_sli_hbq_count(void)
3704{
92d7f7b0 3705 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3706}
3707
e59058c4 3708/**
3621a710 3709 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3710 *
3711 * This function adds the number of hbq entries in every HBQ to get
3712 * the total number of hbq entries required for the HBA and returns
3713 * the total count.
3714 **/
ed957684
JS
3715static int
3716lpfc_sli_hbq_entry_count(void)
3717{
3718 int hbq_count = lpfc_sli_hbq_count();
3719 int count = 0;
3720 int i;
3721
3722 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3723 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3724 return count;
3725}
3726
e59058c4 3727/**
3621a710 3728 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3729 *
3730 * This function calculates amount of memory required for all hbq entries
3731 * to be configured and returns the total memory required.
3732 **/
dea3101e 3733int
ed957684
JS
3734lpfc_sli_hbq_size(void)
3735{
3736 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3737}
3738
e59058c4 3739/**
3621a710 3740 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3741 * @phba: Pointer to HBA context object.
3742 *
3743 * This function is called during the SLI initialization to configure
3744 * all the HBQs and post buffers to the HBQ. The caller is not
3745 * required to hold any locks. This function will return zero if successful
3746 * else it will return negative error code.
3747 **/
ed957684
JS
3748static int
3749lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3750{
3751 int hbq_count = lpfc_sli_hbq_count();
3752 LPFC_MBOXQ_t *pmb;
3753 MAILBOX_t *pmbox;
3754 uint32_t hbqno;
3755 uint32_t hbq_entry_index;
ed957684 3756
92d7f7b0
JS
3757 /* Get a Mailbox buffer to setup mailbox
3758 * commands for HBA initialization
3759 */
ed957684
JS
3760 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3761
3762 if (!pmb)
3763 return -ENOMEM;
3764
04c68496 3765 pmbox = &pmb->u.mb;
ed957684
JS
3766
3767 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3768 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3769 phba->hbq_in_use = 1;
ed957684
JS
3770
3771 hbq_entry_index = 0;
3772 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3773 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3774 phba->hbqs[hbqno].hbqPutIdx = 0;
3775 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3776 phba->hbqs[hbqno].entry_count =
92d7f7b0 3777 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3778 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3779 hbq_entry_index, pmb);
ed957684
JS
3780 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3781
3782 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3783 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3784 mbxStatus <status>, ring <num> */
3785
3786 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3787 LOG_SLI | LOG_VPORT,
e8b62011 3788 "1805 Adapter failed to init. "
ed957684 3789 "Data: x%x x%x x%x\n",
e8b62011 3790 pmbox->mbxCommand,
ed957684
JS
3791 pmbox->mbxStatus, hbqno);
3792
3793 phba->link_state = LPFC_HBA_ERROR;
3794 mempool_free(pmb, phba->mbox_mem_pool);
ed957684
JS
3795 return ENXIO;
3796 }
3797 }
3798 phba->hbq_count = hbq_count;
3799
ed957684
JS
3800 mempool_free(pmb, phba->mbox_mem_pool);
3801
92d7f7b0 3802 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3803 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3804 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3805 return 0;
3806}
3807
4f774513
JS
3808/**
3809 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3810 * @phba: Pointer to HBA context object.
3811 *
3812 * This function is called during the SLI initialization to configure
3813 * all the HBQs and post buffers to the HBQ. The caller is not
3814 * required to hold any locks. This function will return zero if successful
3815 * else it will return negative error code.
3816 **/
3817static int
3818lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3819{
3820 phba->hbq_in_use = 1;
3821 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3822 phba->hbq_count = 1;
3823 /* Initially populate or replenish the HBQs */
3824 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3825 return 0;
3826}
3827
e59058c4 3828/**
3621a710 3829 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3830 * @phba: Pointer to HBA context object.
3831 * @sli_mode: sli mode - 2/3
3832 *
3833 * This function is called by the sli intialization code path
3834 * to issue config_port mailbox command. This function restarts the
3835 * HBA firmware and issues a config_port mailbox command to configure
3836 * the SLI interface in the sli mode specified by sli_mode
3837 * variable. The caller is not required to hold any locks.
3838 * The function returns 0 if successful, else returns negative error
3839 * code.
3840 **/
9399627f
JS
3841int
3842lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
3843{
3844 LPFC_MBOXQ_t *pmb;
3845 uint32_t resetcount = 0, rc = 0, done = 0;
3846
3847 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3848 if (!pmb) {
2e0fef85 3849 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
3850 return -ENOMEM;
3851 }
3852
ed957684 3853 phba->sli_rev = sli_mode;
dea3101e 3854 while (resetcount < 2 && !done) {
2e0fef85 3855 spin_lock_irq(&phba->hbalock);
1c067a42 3856 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3857 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3858 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3859 lpfc_sli_brdrestart(phba);
dea3101e
JB
3860 rc = lpfc_sli_chipset_init(phba);
3861 if (rc)
3862 break;
3863
2e0fef85 3864 spin_lock_irq(&phba->hbalock);
1c067a42 3865 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3866 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
3867 resetcount++;
3868
ed957684
JS
3869 /* Call pre CONFIG_PORT mailbox command initialization. A
3870 * value of 0 means the call was successful. Any other
3871 * nonzero value is a failure, but if ERESTART is returned,
3872 * the driver may reset the HBA and try again.
3873 */
dea3101e
JB
3874 rc = lpfc_config_port_prep(phba);
3875 if (rc == -ERESTART) {
ed957684 3876 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3877 continue;
34b02dcd 3878 } else if (rc)
dea3101e 3879 break;
2e0fef85 3880 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
3881 lpfc_config_port(phba, pmb);
3882 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3883 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3884 LPFC_SLI3_HBQ_ENABLED |
3885 LPFC_SLI3_CRP_ENABLED |
e2a0a9d6
JS
3886 LPFC_SLI3_INB_ENABLED |
3887 LPFC_SLI3_BG_ENABLED);
ed957684 3888 if (rc != MBX_SUCCESS) {
dea3101e 3889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3890 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3891 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3892 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3893 spin_lock_irq(&phba->hbalock);
04c68496 3894 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3895 spin_unlock_irq(&phba->hbalock);
3896 rc = -ENXIO;
04c68496
JS
3897 } else {
3898 /* Allow asynchronous mailbox command to go through */
3899 spin_lock_irq(&phba->hbalock);
3900 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3901 spin_unlock_irq(&phba->hbalock);
ed957684 3902 done = 1;
04c68496 3903 }
dea3101e 3904 }
ed957684
JS
3905 if (!done) {
3906 rc = -EINVAL;
3907 goto do_prep_failed;
3908 }
04c68496
JS
3909 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3910 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3911 rc = -ENXIO;
3912 goto do_prep_failed;
3913 }
04c68496 3914 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3915 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3916 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3917 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3918 phba->max_vpi : phba->max_vports;
3919
34b02dcd
JS
3920 } else
3921 phba->max_vpi = 0;
04c68496
JS
3922 if (pmb->u.mb.un.varCfgPort.gdss)
3923 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3924 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3925 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3926 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3927 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
04c68496 3928 if (pmb->u.mb.un.varCfgPort.ginb) {
34b02dcd 3929 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
8f34f4ce 3930 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
34b02dcd
JS
3931 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3932 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3933 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3934 phba->inb_last_counter =
3935 phba->mbox->us.s3_inb_pgp.counter;
3936 } else {
8f34f4ce 3937 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
34b02dcd
JS
3938 phba->port_gp = phba->mbox->us.s3_pgp.port;
3939 phba->inb_ha_copy = NULL;
3940 phba->inb_counter = NULL;
3941 }
e2a0a9d6
JS
3942
3943 if (phba->cfg_enable_bg) {
04c68496 3944 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
3945 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3946 else
3947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3948 "0443 Adapter did not grant "
3949 "BlockGuard\n");
3950 }
34b02dcd 3951 } else {
8f34f4ce 3952 phba->hbq_get = NULL;
34b02dcd
JS
3953 phba->port_gp = phba->mbox->us.s2.port;
3954 phba->inb_ha_copy = NULL;
3955 phba->inb_counter = NULL;
d7c255b2 3956 phba->max_vpi = 0;
ed957684 3957 }
92d7f7b0 3958do_prep_failed:
ed957684
JS
3959 mempool_free(pmb, phba->mbox_mem_pool);
3960 return rc;
3961}
3962
e59058c4
JS
3963
3964/**
3621a710 3965 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
3966 * @phba: Pointer to HBA context object.
3967 *
3968 * This function is the main SLI intialization function. This function
3969 * is called by the HBA intialization code, HBA reset code and HBA
3970 * error attention handler code. Caller is not required to hold any
3971 * locks. This function issues config_port mailbox command to configure
3972 * the SLI, setup iocb rings and HBQ rings. In the end the function
3973 * calls the config_port_post function to issue init_link mailbox
3974 * command and to start the discovery. The function will return zero
3975 * if successful, else it will return negative error code.
3976 **/
ed957684
JS
3977int
3978lpfc_sli_hba_setup(struct lpfc_hba *phba)
3979{
3980 uint32_t rc;
92d7f7b0 3981 int mode = 3;
ed957684
JS
3982
3983 switch (lpfc_sli_mode) {
3984 case 2:
78b2d852 3985 if (phba->cfg_enable_npiv) {
92d7f7b0 3986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 3987 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 3988 "parameter (%d) to auto (0).\n",
e8b62011 3989 lpfc_sli_mode);
92d7f7b0
JS
3990 break;
3991 }
ed957684
JS
3992 mode = 2;
3993 break;
3994 case 0:
3995 case 3:
3996 break;
3997 default:
92d7f7b0 3998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
3999 "1819 Unrecognized lpfc_sli_mode "
4000 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4001
4002 break;
4003 }
4004
9399627f
JS
4005 rc = lpfc_sli_config_port(phba, mode);
4006
ed957684 4007 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4009 "1820 Unable to select SLI-3. "
4010 "Not supported by adapter.\n");
ed957684 4011 if (rc && mode != 2)
9399627f 4012 rc = lpfc_sli_config_port(phba, 2);
ed957684 4013 if (rc)
dea3101e
JB
4014 goto lpfc_sli_hba_setup_error;
4015
0d878419
JS
4016 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4017 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4018 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4019 if (!rc) {
4020 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4021 "2709 This device supports "
4022 "Advanced Error Reporting (AER)\n");
4023 spin_lock_irq(&phba->hbalock);
4024 phba->hba_flag |= HBA_AER_ENABLED;
4025 spin_unlock_irq(&phba->hbalock);
4026 } else {
4027 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4028 "2708 This device does not support "
4029 "Advanced Error Reporting (AER)\n");
4030 phba->cfg_aer_support = 0;
4031 }
4032 }
4033
ed957684
JS
4034 if (phba->sli_rev == 3) {
4035 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4036 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4037 } else {
4038 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4039 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4040 phba->sli3_options = 0;
ed957684
JS
4041 }
4042
4043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4044 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4045 phba->sli_rev, phba->max_vpi);
ed957684 4046 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4047
4048 if (rc)
4049 goto lpfc_sli_hba_setup_error;
4050
9399627f 4051 /* Init HBQs */
ed957684
JS
4052 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4053 rc = lpfc_sli_hbq_setup(phba);
4054 if (rc)
4055 goto lpfc_sli_hba_setup_error;
4056 }
04c68496 4057 spin_lock_irq(&phba->hbalock);
dea3101e 4058 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4059 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4060
4061 rc = lpfc_config_port_post(phba);
4062 if (rc)
4063 goto lpfc_sli_hba_setup_error;
4064
ed957684
JS
4065 return rc;
4066
92d7f7b0 4067lpfc_sli_hba_setup_error:
2e0fef85 4068 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4070 "0445 Firmware initialization failed\n");
dea3101e
JB
4071 return rc;
4072}
4073
e59058c4 4074/**
da0436e9
JS
4075 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4076 * @phba: Pointer to HBA context object.
4077 * @mboxq: mailbox pointer.
4078 * This function issue a dump mailbox command to read config region
4079 * 23 and parse the records in the region and populate driver
4080 * data structure.
e59058c4 4081 **/
da0436e9
JS
4082static int
4083lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4084 LPFC_MBOXQ_t *mboxq)
dea3101e 4085{
da0436e9
JS
4086 struct lpfc_dmabuf *mp;
4087 struct lpfc_mqe *mqe;
4088 uint32_t data_length;
4089 int rc;
dea3101e 4090
da0436e9
JS
4091 /* Program the default value of vlan_id and fc_map */
4092 phba->valid_vlan = 0;
4093 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4094 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4095 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4096
da0436e9
JS
4097 mqe = &mboxq->u.mqe;
4098 if (lpfc_dump_fcoe_param(phba, mboxq))
4099 return -ENOMEM;
4100
4101 mp = (struct lpfc_dmabuf *) mboxq->context1;
4102 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4103
4104 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4105 "(%d):2571 Mailbox cmd x%x Status x%x "
4106 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4108 "CQ: x%x x%x x%x x%x\n",
4109 mboxq->vport ? mboxq->vport->vpi : 0,
4110 bf_get(lpfc_mqe_command, mqe),
4111 bf_get(lpfc_mqe_status, mqe),
4112 mqe->un.mb_words[0], mqe->un.mb_words[1],
4113 mqe->un.mb_words[2], mqe->un.mb_words[3],
4114 mqe->un.mb_words[4], mqe->un.mb_words[5],
4115 mqe->un.mb_words[6], mqe->un.mb_words[7],
4116 mqe->un.mb_words[8], mqe->un.mb_words[9],
4117 mqe->un.mb_words[10], mqe->un.mb_words[11],
4118 mqe->un.mb_words[12], mqe->un.mb_words[13],
4119 mqe->un.mb_words[14], mqe->un.mb_words[15],
4120 mqe->un.mb_words[16], mqe->un.mb_words[50],
4121 mboxq->mcqe.word0,
4122 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4123 mboxq->mcqe.trailer);
4124
4125 if (rc) {
4126 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4127 kfree(mp);
4128 return -EIO;
4129 }
4130 data_length = mqe->un.mb_words[5];
a0c87cbd 4131 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4132 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4133 kfree(mp);
da0436e9 4134 return -EIO;
d11e31dd 4135 }
dea3101e 4136
da0436e9
JS
4137 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4138 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4139 kfree(mp);
4140 return 0;
4141}
e59058c4
JS
4142
4143/**
da0436e9
JS
4144 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4145 * @phba: pointer to lpfc hba data structure.
4146 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4147 * @vpd: pointer to the memory to hold resulting port vpd data.
4148 * @vpd_size: On input, the number of bytes allocated to @vpd.
4149 * On output, the number of data bytes in @vpd.
e59058c4 4150 *
da0436e9
JS
4151 * This routine executes a READ_REV SLI4 mailbox command. In
4152 * addition, this routine gets the port vpd data.
4153 *
4154 * Return codes
af901ca1 4155 * 0 - successful
da0436e9 4156 * ENOMEM - could not allocated memory.
e59058c4 4157 **/
da0436e9
JS
4158static int
4159lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4160 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4161{
da0436e9
JS
4162 int rc = 0;
4163 uint32_t dma_size;
4164 struct lpfc_dmabuf *dmabuf;
4165 struct lpfc_mqe *mqe;
dea3101e 4166
da0436e9
JS
4167 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4168 if (!dmabuf)
4169 return -ENOMEM;
4170
4171 /*
4172 * Get a DMA buffer for the vpd data resulting from the READ_REV
4173 * mailbox command.
a257bf90 4174 */
da0436e9
JS
4175 dma_size = *vpd_size;
4176 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4177 dma_size,
4178 &dmabuf->phys,
4179 GFP_KERNEL);
4180 if (!dmabuf->virt) {
4181 kfree(dmabuf);
4182 return -ENOMEM;
a257bf90 4183 }
da0436e9 4184 memset(dmabuf->virt, 0, dma_size);
a257bf90 4185
da0436e9
JS
4186 /*
4187 * The SLI4 implementation of READ_REV conflicts at word1,
4188 * bits 31:16 and SLI4 adds vpd functionality not present
4189 * in SLI3. This code corrects the conflicts.
1dcb58e5 4190 */
da0436e9
JS
4191 lpfc_read_rev(phba, mboxq);
4192 mqe = &mboxq->u.mqe;
4193 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4194 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4195 mqe->un.read_rev.word1 &= 0x0000FFFF;
4196 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4197 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4198
4199 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4200 if (rc) {
4201 dma_free_coherent(&phba->pcidev->dev, dma_size,
4202 dmabuf->virt, dmabuf->phys);
def9c7a9 4203 kfree(dmabuf);
da0436e9
JS
4204 return -EIO;
4205 }
1dcb58e5 4206
da0436e9
JS
4207 /*
4208 * The available vpd length cannot be bigger than the
4209 * DMA buffer passed to the port. Catch the less than
4210 * case and update the caller's size.
4211 */
4212 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4213 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4214
da0436e9
JS
4215 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4216 dma_free_coherent(&phba->pcidev->dev, dma_size,
4217 dmabuf->virt, dmabuf->phys);
4218 kfree(dmabuf);
4219 return 0;
dea3101e
JB
4220}
4221
e59058c4 4222/**
da0436e9
JS
4223 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4224 * @phba: pointer to lpfc hba data structure.
e59058c4 4225 *
da0436e9
JS
4226 * This routine is called to explicitly arm the SLI4 device's completion and
4227 * event queues
4228 **/
4229static void
4230lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4231{
4232 uint8_t fcp_eqidx;
4233
4234 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4235 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4236 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4237 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4238 LPFC_QUEUE_REARM);
4239 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4240 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4241 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4242 LPFC_QUEUE_REARM);
4243}
4244
4245/**
4246 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4247 * @phba: Pointer to HBA context object.
4248 *
4249 * This function is the main SLI4 device intialization PCI function. This
4250 * function is called by the HBA intialization code, HBA reset code and
4251 * HBA error attention handler code. Caller is not required to hold any
4252 * locks.
4253 **/
4254int
4255lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4256{
4257 int rc;
4258 LPFC_MBOXQ_t *mboxq;
4259 struct lpfc_mqe *mqe;
4260 uint8_t *vpd;
4261 uint32_t vpd_size;
4262 uint32_t ftr_rsp = 0;
4263 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4264 struct lpfc_vport *vport = phba->pport;
4265 struct lpfc_dmabuf *mp;
4266
4267 /* Perform a PCI function reset to start from clean */
4268 rc = lpfc_pci_function_reset(phba);
4269 if (unlikely(rc))
4270 return -ENODEV;
4271
4272 /* Check the HBA Host Status Register for readyness */
4273 rc = lpfc_sli4_post_status_check(phba);
4274 if (unlikely(rc))
4275 return -ENODEV;
4276 else {
4277 spin_lock_irq(&phba->hbalock);
4278 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4279 spin_unlock_irq(&phba->hbalock);
4280 }
4281
4282 /*
4283 * Allocate a single mailbox container for initializing the
4284 * port.
4285 */
4286 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4287 if (!mboxq)
4288 return -ENOMEM;
4289
4290 /*
4291 * Continue initialization with default values even if driver failed
4292 * to read FCoE param config regions
4293 */
4294 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4296 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4297
4298 /* Issue READ_REV to collect vpd and FW information. */
4299 vpd_size = PAGE_SIZE;
4300 vpd = kzalloc(vpd_size, GFP_KERNEL);
4301 if (!vpd) {
4302 rc = -ENOMEM;
4303 goto out_free_mbox;
4304 }
4305
4306 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4307 if (unlikely(rc))
4308 goto out_free_vpd;
4309
4310 mqe = &mboxq->u.mqe;
f1126688
JS
4311 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4312 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4313 phba->hba_flag |= HBA_FCOE_SUPPORT;
45ed1190
JS
4314
4315 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4316 LPFC_DCBX_CEE_MODE)
4317 phba->hba_flag |= HBA_FIP_SUPPORT;
4318 else
4319 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4320
f1126688
JS
4321 if (phba->sli_rev != LPFC_SLI_REV4 ||
4322 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4324 "0376 READ_REV Error. SLI Level %d "
4325 "FCoE enabled %d\n",
f1126688 4326 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4327 rc = -EIO;
4328 goto out_free_vpd;
4329 }
da0436e9
JS
4330 /*
4331 * Evaluate the read rev and vpd data. Populate the driver
4332 * state with the results. If this routine fails, the failure
4333 * is not fatal as the driver will use generic values.
4334 */
4335 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4336 if (unlikely(!rc)) {
4337 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4338 "0377 Error %d parsing vpd. "
4339 "Using defaults.\n", rc);
4340 rc = 0;
4341 }
4342
f1126688
JS
4343 /* Save information as VPD data */
4344 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4345 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4346 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4347 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4348 &mqe->un.read_rev);
4349 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4350 &mqe->un.read_rev);
4351 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4352 &mqe->un.read_rev);
4353 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4354 &mqe->un.read_rev);
4355 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4356 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4357 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4358 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4359 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4360 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4361 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4362 "(%d):0380 READ_REV Status x%x "
4363 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4364 mboxq->vport ? mboxq->vport->vpi : 0,
4365 bf_get(lpfc_mqe_status, mqe),
4366 phba->vpd.rev.opFwName,
4367 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4368 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4369
4370 /*
4371 * Discover the port's supported feature set and match it against the
4372 * hosts requests.
4373 */
4374 lpfc_request_features(phba, mboxq);
4375 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4376 if (unlikely(rc)) {
4377 rc = -EIO;
4378 goto out_free_vpd;
4379 }
4380
4381 /*
4382 * The port must support FCP initiator mode as this is the
4383 * only mode running in the host.
4384 */
4385 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4386 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4387 "0378 No support for fcpi mode.\n");
4388 ftr_rsp++;
4389 }
4390
4391 /*
4392 * If the port cannot support the host's requested features
4393 * then turn off the global config parameters to disable the
4394 * feature in the driver. This is not a fatal error.
4395 */
4396 if ((phba->cfg_enable_bg) &&
4397 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4398 ftr_rsp++;
4399
4400 if (phba->max_vpi && phba->cfg_enable_npiv &&
4401 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4402 ftr_rsp++;
4403
4404 if (ftr_rsp) {
4405 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4406 "0379 Feature Mismatch Data: x%08x %08x "
4407 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4408 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4409 phba->cfg_enable_npiv, phba->max_vpi);
4410 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4411 phba->cfg_enable_bg = 0;
4412 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4413 phba->cfg_enable_npiv = 0;
4414 }
4415
4416 /* These SLI3 features are assumed in SLI4 */
4417 spin_lock_irq(&phba->hbalock);
4418 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4419 spin_unlock_irq(&phba->hbalock);
4420
4421 /* Read the port's service parameters. */
9f1177a3
JS
4422 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4423 if (rc) {
4424 phba->link_state = LPFC_HBA_ERROR;
4425 rc = -ENOMEM;
4426 goto out_free_vpd;
4427 }
4428
da0436e9
JS
4429 mboxq->vport = vport;
4430 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4431 mp = (struct lpfc_dmabuf *) mboxq->context1;
4432 if (rc == MBX_SUCCESS) {
4433 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4434 rc = 0;
4435 }
4436
4437 /*
4438 * This memory was allocated by the lpfc_read_sparam routine. Release
4439 * it to the mbuf pool.
4440 */
4441 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4442 kfree(mp);
4443 mboxq->context1 = NULL;
4444 if (unlikely(rc)) {
4445 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4446 "0382 READ_SPARAM command failed "
4447 "status %d, mbxStatus x%x\n",
4448 rc, bf_get(lpfc_mqe_status, mqe));
4449 phba->link_state = LPFC_HBA_ERROR;
4450 rc = -EIO;
4451 goto out_free_vpd;
4452 }
4453
4454 if (phba->cfg_soft_wwnn)
4455 u64_to_wwn(phba->cfg_soft_wwnn,
4456 vport->fc_sparam.nodeName.u.wwn);
4457 if (phba->cfg_soft_wwpn)
4458 u64_to_wwn(phba->cfg_soft_wwpn,
4459 vport->fc_sparam.portName.u.wwn);
4460 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4461 sizeof(struct lpfc_name));
4462 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4463 sizeof(struct lpfc_name));
4464
4465 /* Update the fc_host data structures with new wwn. */
4466 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4467 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4468
4469 /* Register SGL pool to the device using non-embedded mailbox command */
4470 rc = lpfc_sli4_post_sgl_list(phba);
4471 if (unlikely(rc)) {
4472 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4473 "0582 Error %d during sgl post operation\n",
4474 rc);
da0436e9
JS
4475 rc = -ENODEV;
4476 goto out_free_vpd;
4477 }
4478
4479 /* Register SCSI SGL pool to the device */
4480 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4481 if (unlikely(rc)) {
4482 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4483 "0383 Error %d during scsi sgl post "
4484 "operation\n", rc);
da0436e9
JS
4485 /* Some Scsi buffers were moved to the abort scsi list */
4486 /* A pci function reset will repost them */
4487 rc = -ENODEV;
4488 goto out_free_vpd;
4489 }
4490
4491 /* Post the rpi header region to the device. */
4492 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4493 if (unlikely(rc)) {
4494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4495 "0393 Error %d during rpi post operation\n",
4496 rc);
4497 rc = -ENODEV;
4498 goto out_free_vpd;
4499 }
da0436e9
JS
4500
4501 /* Set up all the queues to the device */
4502 rc = lpfc_sli4_queue_setup(phba);
4503 if (unlikely(rc)) {
4504 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4505 "0381 Error %d during queue setup.\n ", rc);
4506 goto out_stop_timers;
4507 }
4508
4509 /* Arm the CQs and then EQs on device */
4510 lpfc_sli4_arm_cqeq_intr(phba);
4511
4512 /* Indicate device interrupt mode */
4513 phba->sli4_hba.intr_enable = 1;
4514
4515 /* Allow asynchronous mailbox command to go through */
4516 spin_lock_irq(&phba->hbalock);
4517 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4518 spin_unlock_irq(&phba->hbalock);
4519
4520 /* Post receive buffers to the device */
4521 lpfc_sli4_rb_setup(phba);
4522
fc2b989b
JS
4523 /* Reset HBA FCF states after HBA reset */
4524 phba->fcf.fcf_flag = 0;
4525 phba->fcf.current_rec.flag = 0;
4526
da0436e9 4527 /* Start the ELS watchdog timer */
8fa38513
JS
4528 mod_timer(&vport->els_tmofunc,
4529 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4530
4531 /* Start heart beat timer */
4532 mod_timer(&phba->hb_tmofunc,
4533 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4534 phba->hb_outstanding = 0;
4535 phba->last_completion_time = jiffies;
4536
4537 /* Start error attention (ERATT) polling timer */
4538 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4539
4540 /*
4541 * The port is ready, set the host's link state to LINK_DOWN
4542 * in preparation for link interrupts.
4543 */
4544 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4545 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4546 lpfc_set_loopback_flag(phba);
4547 /* Change driver state to LPFC_LINK_DOWN right before init link */
4548 spin_lock_irq(&phba->hbalock);
4549 phba->link_state = LPFC_LINK_DOWN;
4550 spin_unlock_irq(&phba->hbalock);
4551 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4552 if (unlikely(rc != MBX_NOT_FINISHED)) {
4553 kfree(vpd);
4554 return 0;
4555 } else
4556 rc = -EIO;
4557
4558 /* Unset all the queues set up in this routine when error out */
4559 if (rc)
4560 lpfc_sli4_queue_unset(phba);
4561
4562out_stop_timers:
4563 if (rc)
4564 lpfc_stop_hba_timers(phba);
4565out_free_vpd:
4566 kfree(vpd);
4567out_free_mbox:
4568 mempool_free(mboxq, phba->mbox_mem_pool);
4569 return rc;
4570}
4571
4572/**
4573 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4574 * @ptr: context object - pointer to hba structure.
4575 *
4576 * This is the callback function for mailbox timer. The mailbox
4577 * timer is armed when a new mailbox command is issued and the timer
4578 * is deleted when the mailbox complete. The function is called by
4579 * the kernel timer code when a mailbox does not complete within
4580 * expected time. This function wakes up the worker thread to
4581 * process the mailbox timeout and returns. All the processing is
4582 * done by the worker thread function lpfc_mbox_timeout_handler.
4583 **/
4584void
4585lpfc_mbox_timeout(unsigned long ptr)
4586{
4587 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4588 unsigned long iflag;
4589 uint32_t tmo_posted;
4590
4591 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4592 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4593 if (!tmo_posted)
4594 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4595 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4596
4597 if (!tmo_posted)
4598 lpfc_worker_wake_up(phba);
4599 return;
4600}
4601
4602
4603/**
4604 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4605 * @phba: Pointer to HBA context object.
4606 *
4607 * This function is called from worker thread when a mailbox command times out.
4608 * The caller is not required to hold any locks. This function will reset the
4609 * HBA and recover all the pending commands.
4610 **/
4611void
4612lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4613{
4614 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4615 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4616 struct lpfc_sli *psli = &phba->sli;
4617 struct lpfc_sli_ring *pring;
4618
4619 /* Check the pmbox pointer first. There is a race condition
4620 * between the mbox timeout handler getting executed in the
4621 * worklist and the mailbox actually completing. When this
4622 * race condition occurs, the mbox_active will be NULL.
4623 */
4624 spin_lock_irq(&phba->hbalock);
4625 if (pmbox == NULL) {
4626 lpfc_printf_log(phba, KERN_WARNING,
4627 LOG_MBOX | LOG_SLI,
4628 "0353 Active Mailbox cleared - mailbox timeout "
4629 "exiting\n");
4630 spin_unlock_irq(&phba->hbalock);
4631 return;
4632 }
4633
4634 /* Mbox cmd <mbxCommand> timeout */
4635 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4636 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4637 mb->mbxCommand,
4638 phba->pport->port_state,
4639 phba->sli.sli_flag,
4640 phba->sli.mbox_active);
4641 spin_unlock_irq(&phba->hbalock);
4642
4643 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4644 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4645 * it to fail all oustanding SCSI IO.
4646 */
4647 spin_lock_irq(&phba->pport->work_port_lock);
4648 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4649 spin_unlock_irq(&phba->pport->work_port_lock);
4650 spin_lock_irq(&phba->hbalock);
4651 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4652 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4653 spin_unlock_irq(&phba->hbalock);
4654
4655 pring = &psli->ring[psli->fcp_ring];
4656 lpfc_sli_abort_iocb_ring(phba, pring);
4657
4658 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4659 "0345 Resetting board due to mailbox timeout\n");
4660
4661 /* Reset the HBA device */
4662 lpfc_reset_hba(phba);
4663}
4664
4665/**
4666 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4667 * @phba: Pointer to HBA context object.
4668 * @pmbox: Pointer to mailbox object.
4669 * @flag: Flag indicating how the mailbox need to be processed.
4670 *
4671 * This function is called by discovery code and HBA management code
4672 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4673 * function gets the hbalock to protect the data structures.
4674 * The mailbox command can be submitted in polling mode, in which case
4675 * this function will wait in a polling loop for the completion of the
4676 * mailbox.
4677 * If the mailbox is submitted in no_wait mode (not polling) the
4678 * function will submit the command and returns immediately without waiting
4679 * for the mailbox completion. The no_wait is supported only when HBA
4680 * is in SLI2/SLI3 mode - interrupts are enabled.
4681 * The SLI interface allows only one mailbox pending at a time. If the
4682 * mailbox is issued in polling mode and there is already a mailbox
4683 * pending, then the function will return an error. If the mailbox is issued
4684 * in NO_WAIT mode and there is a mailbox pending already, the function
4685 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4686 * The sli layer owns the mailbox object until the completion of mailbox
4687 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4688 * return codes the caller owns the mailbox command after the return of
4689 * the function.
e59058c4 4690 **/
3772a991
JS
4691static int
4692lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4693 uint32_t flag)
dea3101e 4694{
dea3101e 4695 MAILBOX_t *mb;
2e0fef85 4696 struct lpfc_sli *psli = &phba->sli;
dea3101e
JB
4697 uint32_t status, evtctr;
4698 uint32_t ha_copy;
4699 int i;
09372820 4700 unsigned long timeout;
dea3101e 4701 unsigned long drvr_flag = 0;
34b02dcd 4702 uint32_t word0, ldata;
dea3101e 4703 void __iomem *to_slim;
58da1ffb
JS
4704 int processing_queue = 0;
4705
4706 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4707 if (!pmbox) {
8568a4d2 4708 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4709 /* processing mbox queue from intr_handler */
3772a991
JS
4710 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4711 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4712 return MBX_SUCCESS;
4713 }
58da1ffb 4714 processing_queue = 1;
58da1ffb
JS
4715 pmbox = lpfc_mbox_get(phba);
4716 if (!pmbox) {
4717 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4718 return MBX_SUCCESS;
4719 }
4720 }
dea3101e 4721
ed957684 4722 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4723 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4724 if(!pmbox->vport) {
58da1ffb 4725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4726 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4727 LOG_MBOX | LOG_VPORT,
e8b62011 4728 "1806 Mbox x%x failed. No vport\n",
3772a991 4729 pmbox->u.mb.mbxCommand);
ed957684 4730 dump_stack();
58da1ffb 4731 goto out_not_finished;
ed957684
JS
4732 }
4733 }
4734
8d63f375 4735 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4736 if (unlikely(pci_channel_offline(phba->pcidev))) {
4737 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4738 goto out_not_finished;
4739 }
8d63f375 4740
a257bf90
JS
4741 /* If HBA has a deferred error attention, fail the iocb. */
4742 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4743 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4744 goto out_not_finished;
4745 }
4746
dea3101e 4747 psli = &phba->sli;
92d7f7b0 4748
3772a991 4749 mb = &pmbox->u.mb;
dea3101e
JB
4750 status = MBX_SUCCESS;
4751
2e0fef85
JS
4752 if (phba->link_state == LPFC_HBA_ERROR) {
4753 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4754
4755 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4757 "(%d):0311 Mailbox command x%x cannot "
4758 "issue Data: x%x x%x\n",
4759 pmbox->vport ? pmbox->vport->vpi : 0,
4760 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4761 goto out_not_finished;
41415862
JW
4762 }
4763
9290831f
JS
4764 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4765 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4766 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4767 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4768 "(%d):2528 Mailbox command x%x cannot "
4769 "issue Data: x%x x%x\n",
4770 pmbox->vport ? pmbox->vport->vpi : 0,
4771 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4772 goto out_not_finished;
9290831f
JS
4773 }
4774
dea3101e
JB
4775 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4776 /* Polling for a mbox command when another one is already active
4777 * is not allowed in SLI. Also, the driver must have established
4778 * SLI2 mode to queue and process multiple mbox commands.
4779 */
4780
4781 if (flag & MBX_POLL) {
2e0fef85 4782 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
4783
4784 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4786 "(%d):2529 Mailbox command x%x "
4787 "cannot issue Data: x%x x%x\n",
4788 pmbox->vport ? pmbox->vport->vpi : 0,
4789 pmbox->u.mb.mbxCommand,
4790 psli->sli_flag, flag);
58da1ffb 4791 goto out_not_finished;
dea3101e
JB
4792 }
4793
3772a991 4794 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4795 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4796 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4797 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4798 "(%d):2530 Mailbox command x%x "
4799 "cannot issue Data: x%x x%x\n",
4800 pmbox->vport ? pmbox->vport->vpi : 0,
4801 pmbox->u.mb.mbxCommand,
4802 psli->sli_flag, flag);
58da1ffb 4803 goto out_not_finished;
dea3101e
JB
4804 }
4805
dea3101e
JB
4806 /* Another mailbox command is still being processed, queue this
4807 * command to be processed later.
4808 */
4809 lpfc_mbox_put(phba, pmbox);
4810
4811 /* Mbox cmd issue - BUSY */
ed957684 4812 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4813 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4814 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4815 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4816 mb->mbxCommand, phba->pport->port_state,
4817 psli->sli_flag, flag);
dea3101e
JB
4818
4819 psli->slistat.mbox_busy++;
2e0fef85 4820 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4821
858c9f6c
JS
4822 if (pmbox->vport) {
4823 lpfc_debugfs_disc_trc(pmbox->vport,
4824 LPFC_DISC_TRC_MBOX_VPORT,
4825 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4826 (uint32_t)mb->mbxCommand,
4827 mb->un.varWords[0], mb->un.varWords[1]);
4828 }
4829 else {
4830 lpfc_debugfs_disc_trc(phba->pport,
4831 LPFC_DISC_TRC_MBOX,
4832 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4833 (uint32_t)mb->mbxCommand,
4834 mb->un.varWords[0], mb->un.varWords[1]);
4835 }
4836
2e0fef85 4837 return MBX_BUSY;
dea3101e
JB
4838 }
4839
dea3101e
JB
4840 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4841
4842 /* If we are not polling, we MUST be in SLI2 mode */
4843 if (flag != MBX_POLL) {
3772a991 4844 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4845 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4846 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4847 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4848 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4849 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4850 "(%d):2531 Mailbox command x%x "
4851 "cannot issue Data: x%x x%x\n",
4852 pmbox->vport ? pmbox->vport->vpi : 0,
4853 pmbox->u.mb.mbxCommand,
4854 psli->sli_flag, flag);
58da1ffb 4855 goto out_not_finished;
dea3101e
JB
4856 }
4857 /* timeout active mbox command */
a309a6b6
JS
4858 mod_timer(&psli->mbox_tmo, (jiffies +
4859 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e
JB
4860 }
4861
4862 /* Mailbox cmd <cmd> issue */
ed957684 4863 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4864 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4865 "x%x\n",
e8b62011 4866 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4867 mb->mbxCommand, phba->pport->port_state,
4868 psli->sli_flag, flag);
dea3101e 4869
858c9f6c
JS
4870 if (mb->mbxCommand != MBX_HEARTBEAT) {
4871 if (pmbox->vport) {
4872 lpfc_debugfs_disc_trc(pmbox->vport,
4873 LPFC_DISC_TRC_MBOX_VPORT,
4874 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4875 (uint32_t)mb->mbxCommand,
4876 mb->un.varWords[0], mb->un.varWords[1]);
4877 }
4878 else {
4879 lpfc_debugfs_disc_trc(phba->pport,
4880 LPFC_DISC_TRC_MBOX,
4881 "MBOX Send: cmd:x%x mb:x%x x%x",
4882 (uint32_t)mb->mbxCommand,
4883 mb->un.varWords[0], mb->un.varWords[1]);
4884 }
4885 }
4886
dea3101e
JB
4887 psli->slistat.mbox_cmd++;
4888 evtctr = psli->slistat.mbox_event;
4889
4890 /* next set own bit for the adapter and copy over command word */
4891 mb->mbxOwner = OWN_CHIP;
4892
3772a991 4893 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
4894 /* Populate mbox extension offset word. */
4895 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
4896 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4897 = (uint8_t *)phba->mbox_ext
4898 - (uint8_t *)phba->mbox;
4899 }
4900
4901 /* Copy the mailbox extension data */
4902 if (pmbox->in_ext_byte_len && pmbox->context2) {
4903 lpfc_sli_pcimem_bcopy(pmbox->context2,
4904 (uint8_t *)phba->mbox_ext,
4905 pmbox->in_ext_byte_len);
4906 }
4907 /* Copy command data to host SLIM area */
34b02dcd 4908 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4909 } else {
7a470277
JS
4910 /* Populate mbox extension offset word. */
4911 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
4912 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
4913 = MAILBOX_HBA_EXT_OFFSET;
4914
4915 /* Copy the mailbox extension data */
4916 if (pmbox->in_ext_byte_len && pmbox->context2) {
4917 lpfc_memcpy_to_slim(phba->MBslimaddr +
4918 MAILBOX_HBA_EXT_OFFSET,
4919 pmbox->context2, pmbox->in_ext_byte_len);
4920
4921 }
9290831f 4922 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4923 /* copy command data into host mbox for cmpl */
34b02dcd 4924 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e
JB
4925 }
4926
4927 /* First copy mbox command data to HBA SLIM, skip past first
4928 word */
4929 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4930 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4931 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4932
4933 /* Next copy over first word, with mbxOwner set */
34b02dcd 4934 ldata = *((uint32_t *)mb);
dea3101e
JB
4935 to_slim = phba->MBslimaddr;
4936 writel(ldata, to_slim);
4937 readl(to_slim); /* flush */
4938
4939 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4940 /* switch over to host mailbox */
3772a991 4941 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
4942 }
4943 }
4944
4945 wmb();
dea3101e
JB
4946
4947 switch (flag) {
4948 case MBX_NOWAIT:
09372820 4949 /* Set up reference to mailbox command */
dea3101e 4950 psli->mbox_active = pmbox;
09372820
JS
4951 /* Interrupt board to do it */
4952 writel(CA_MBATT, phba->CAregaddr);
4953 readl(phba->CAregaddr); /* flush */
4954 /* Don't wait for it to finish, just return */
dea3101e
JB
4955 break;
4956
4957 case MBX_POLL:
09372820 4958 /* Set up null reference to mailbox command */
dea3101e 4959 psli->mbox_active = NULL;
09372820
JS
4960 /* Interrupt board to do it */
4961 writel(CA_MBATT, phba->CAregaddr);
4962 readl(phba->CAregaddr); /* flush */
4963
3772a991 4964 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4965 /* First read mbox status word */
34b02dcd 4966 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
4967 word0 = le32_to_cpu(word0);
4968 } else {
4969 /* First read mbox status word */
4970 word0 = readl(phba->MBslimaddr);
4971 }
4972
4973 /* Read the HBA Host Attention Register */
4974 ha_copy = readl(phba->HAregaddr);
09372820
JS
4975 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4976 mb->mbxCommand) *
4977 1000) + jiffies;
4978 i = 0;
dea3101e 4979 /* Wait for command to complete */
41415862
JW
4980 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4981 (!(ha_copy & HA_MBATT) &&
2e0fef85 4982 (phba->link_state > LPFC_WARM_START))) {
09372820 4983 if (time_after(jiffies, timeout)) {
dea3101e 4984 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4985 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 4986 drvr_flag);
58da1ffb 4987 goto out_not_finished;
dea3101e
JB
4988 }
4989
4990 /* Check if we took a mbox interrupt while we were
4991 polling */
4992 if (((word0 & OWN_CHIP) != OWN_CHIP)
4993 && (evtctr != psli->slistat.mbox_event))
4994 break;
4995
09372820
JS
4996 if (i++ > 10) {
4997 spin_unlock_irqrestore(&phba->hbalock,
4998 drvr_flag);
4999 msleep(1);
5000 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5001 }
dea3101e 5002
3772a991 5003 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5004 /* First copy command data */
34b02dcd 5005 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
5006 word0 = le32_to_cpu(word0);
5007 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5008 MAILBOX_t *slimmb;
34b02dcd 5009 uint32_t slimword0;
dea3101e
JB
5010 /* Check real SLIM for any errors */
5011 slimword0 = readl(phba->MBslimaddr);
5012 slimmb = (MAILBOX_t *) & slimword0;
5013 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5014 && slimmb->mbxStatus) {
5015 psli->sli_flag &=
3772a991 5016 ~LPFC_SLI_ACTIVE;
dea3101e
JB
5017 word0 = slimword0;
5018 }
5019 }
5020 } else {
5021 /* First copy command data */
5022 word0 = readl(phba->MBslimaddr);
5023 }
5024 /* Read the HBA Host Attention Register */
5025 ha_copy = readl(phba->HAregaddr);
5026 }
5027
3772a991 5028 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5029 /* copy results back to user */
34b02dcd 5030 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
7a470277
JS
5031 /* Copy the mailbox extension data */
5032 if (pmbox->out_ext_byte_len && pmbox->context2) {
5033 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
5034 pmbox->context2,
5035 pmbox->out_ext_byte_len);
5036 }
dea3101e
JB
5037 } else {
5038 /* First copy command data */
5039 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5040 MAILBOX_CMD_SIZE);
7a470277
JS
5041 /* Copy the mailbox extension data */
5042 if (pmbox->out_ext_byte_len && pmbox->context2) {
5043 lpfc_memcpy_from_slim(pmbox->context2,
5044 phba->MBslimaddr +
5045 MAILBOX_HBA_EXT_OFFSET,
5046 pmbox->out_ext_byte_len);
dea3101e
JB
5047 }
5048 }
5049
5050 writel(HA_MBATT, phba->HAregaddr);
5051 readl(phba->HAregaddr); /* flush */
5052
5053 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5054 status = mb->mbxStatus;
5055 }
5056
2e0fef85
JS
5057 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5058 return status;
58da1ffb
JS
5059
5060out_not_finished:
5061 if (processing_queue) {
da0436e9 5062 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5063 lpfc_mbox_cmpl_put(phba, pmbox);
5064 }
5065 return MBX_NOT_FINISHED;
dea3101e
JB
5066}
5067
f1126688
JS
5068/**
5069 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5070 * @phba: Pointer to HBA context object.
5071 *
5072 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5073 * the driver internal pending mailbox queue. It will then try to wait out the
5074 * possible outstanding mailbox command before return.
5075 *
5076 * Returns:
5077 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5078 * the outstanding mailbox command timed out.
5079 **/
5080static int
5081lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5082{
5083 struct lpfc_sli *psli = &phba->sli;
5084 uint8_t actcmd = MBX_HEARTBEAT;
5085 int rc = 0;
5086 unsigned long timeout;
5087
5088 /* Mark the asynchronous mailbox command posting as blocked */
5089 spin_lock_irq(&phba->hbalock);
5090 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5091 if (phba->sli.mbox_active)
5092 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5093 spin_unlock_irq(&phba->hbalock);
5094 /* Determine how long we might wait for the active mailbox
5095 * command to be gracefully completed by firmware.
5096 */
5097 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5098 jiffies;
5099 /* Wait for the outstnading mailbox command to complete */
5100 while (phba->sli.mbox_active) {
5101 /* Check active mailbox complete status every 2ms */
5102 msleep(2);
5103 if (time_after(jiffies, timeout)) {
5104 /* Timeout, marked the outstanding cmd not complete */
5105 rc = 1;
5106 break;
5107 }
5108 }
5109
5110 /* Can not cleanly block async mailbox command, fails it */
5111 if (rc) {
5112 spin_lock_irq(&phba->hbalock);
5113 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5114 spin_unlock_irq(&phba->hbalock);
5115 }
5116 return rc;
5117}
5118
5119/**
5120 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5121 * @phba: Pointer to HBA context object.
5122 *
5123 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5124 * commands from the driver internal pending mailbox queue. It makes sure
5125 * that there is no outstanding mailbox command before resuming posting
5126 * asynchronous mailbox commands. If, for any reason, there is outstanding
5127 * mailbox command, it will try to wait it out before resuming asynchronous
5128 * mailbox command posting.
5129 **/
5130static void
5131lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5132{
5133 struct lpfc_sli *psli = &phba->sli;
5134
5135 spin_lock_irq(&phba->hbalock);
5136 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5137 /* Asynchronous mailbox posting is not blocked, do nothing */
5138 spin_unlock_irq(&phba->hbalock);
5139 return;
5140 }
5141
5142 /* Outstanding synchronous mailbox command is guaranteed to be done,
5143 * successful or timeout, after timing-out the outstanding mailbox
5144 * command shall always be removed, so just unblock posting async
5145 * mailbox command and resume
5146 */
5147 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5148 spin_unlock_irq(&phba->hbalock);
5149
5150 /* wake up worker thread to post asynchronlous mailbox command */
5151 lpfc_worker_wake_up(phba);
5152}
5153
da0436e9
JS
5154/**
5155 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5156 * @phba: Pointer to HBA context object.
5157 * @mboxq: Pointer to mailbox object.
5158 *
5159 * The function posts a mailbox to the port. The mailbox is expected
5160 * to be comletely filled in and ready for the port to operate on it.
5161 * This routine executes a synchronous completion operation on the
5162 * mailbox by polling for its completion.
5163 *
5164 * The caller must not be holding any locks when calling this routine.
5165 *
5166 * Returns:
5167 * MBX_SUCCESS - mailbox posted successfully
5168 * Any of the MBX error values.
5169 **/
5170static int
5171lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5172{
5173 int rc = MBX_SUCCESS;
5174 unsigned long iflag;
5175 uint32_t db_ready;
5176 uint32_t mcqe_status;
5177 uint32_t mbx_cmnd;
5178 unsigned long timeout;
5179 struct lpfc_sli *psli = &phba->sli;
5180 struct lpfc_mqe *mb = &mboxq->u.mqe;
5181 struct lpfc_bmbx_create *mbox_rgn;
5182 struct dma_address *dma_address;
5183 struct lpfc_register bmbx_reg;
5184
5185 /*
5186 * Only one mailbox can be active to the bootstrap mailbox region
5187 * at a time and there is no queueing provided.
5188 */
5189 spin_lock_irqsave(&phba->hbalock, iflag);
5190 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5191 spin_unlock_irqrestore(&phba->hbalock, iflag);
5192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5193 "(%d):2532 Mailbox command x%x (x%x) "
5194 "cannot issue Data: x%x x%x\n",
5195 mboxq->vport ? mboxq->vport->vpi : 0,
5196 mboxq->u.mb.mbxCommand,
5197 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5198 psli->sli_flag, MBX_POLL);
5199 return MBXERR_ERROR;
5200 }
5201 /* The server grabs the token and owns it until release */
5202 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5203 phba->sli.mbox_active = mboxq;
5204 spin_unlock_irqrestore(&phba->hbalock, iflag);
5205
5206 /*
5207 * Initialize the bootstrap memory region to avoid stale data areas
5208 * in the mailbox post. Then copy the caller's mailbox contents to
5209 * the bmbx mailbox region.
5210 */
5211 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5212 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5213 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5214 sizeof(struct lpfc_mqe));
5215
5216 /* Post the high mailbox dma address to the port and wait for ready. */
5217 dma_address = &phba->sli4_hba.bmbx.dma_address;
5218 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5219
5220 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5221 * 1000) + jiffies;
5222 do {
5223 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5224 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5225 if (!db_ready)
5226 msleep(2);
5227
5228 if (time_after(jiffies, timeout)) {
5229 rc = MBXERR_ERROR;
5230 goto exit;
5231 }
5232 } while (!db_ready);
5233
5234 /* Post the low mailbox dma address to the port. */
5235 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5236 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5237 * 1000) + jiffies;
5238 do {
5239 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5240 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5241 if (!db_ready)
5242 msleep(2);
5243
5244 if (time_after(jiffies, timeout)) {
5245 rc = MBXERR_ERROR;
5246 goto exit;
5247 }
5248 } while (!db_ready);
5249
5250 /*
5251 * Read the CQ to ensure the mailbox has completed.
5252 * If so, update the mailbox status so that the upper layers
5253 * can complete the request normally.
5254 */
5255 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5256 sizeof(struct lpfc_mqe));
5257 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5258 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5259 sizeof(struct lpfc_mcqe));
5260 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5261
5262 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5263 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5264 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5265 rc = MBXERR_ERROR;
5266 }
5267
5268 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5269 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5270 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5271 " x%x x%x CQ: x%x x%x x%x x%x\n",
5272 mboxq->vport ? mboxq->vport->vpi : 0,
5273 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5274 bf_get(lpfc_mqe_status, mb),
5275 mb->un.mb_words[0], mb->un.mb_words[1],
5276 mb->un.mb_words[2], mb->un.mb_words[3],
5277 mb->un.mb_words[4], mb->un.mb_words[5],
5278 mb->un.mb_words[6], mb->un.mb_words[7],
5279 mb->un.mb_words[8], mb->un.mb_words[9],
5280 mb->un.mb_words[10], mb->un.mb_words[11],
5281 mb->un.mb_words[12], mboxq->mcqe.word0,
5282 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5283 mboxq->mcqe.trailer);
5284exit:
5285 /* We are holding the token, no needed for lock when release */
5286 spin_lock_irqsave(&phba->hbalock, iflag);
5287 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5288 phba->sli.mbox_active = NULL;
5289 spin_unlock_irqrestore(&phba->hbalock, iflag);
5290 return rc;
5291}
5292
5293/**
5294 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5295 * @phba: Pointer to HBA context object.
5296 * @pmbox: Pointer to mailbox object.
5297 * @flag: Flag indicating how the mailbox need to be processed.
5298 *
5299 * This function is called by discovery code and HBA management code to submit
5300 * a mailbox command to firmware with SLI-4 interface spec.
5301 *
5302 * Return codes the caller owns the mailbox command after the return of the
5303 * function.
5304 **/
5305static int
5306lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5307 uint32_t flag)
5308{
5309 struct lpfc_sli *psli = &phba->sli;
5310 unsigned long iflags;
5311 int rc;
5312
8fa38513
JS
5313 rc = lpfc_mbox_dev_check(phba);
5314 if (unlikely(rc)) {
5315 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5316 "(%d):2544 Mailbox command x%x (x%x) "
5317 "cannot issue Data: x%x x%x\n",
5318 mboxq->vport ? mboxq->vport->vpi : 0,
5319 mboxq->u.mb.mbxCommand,
5320 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5321 psli->sli_flag, flag);
5322 goto out_not_finished;
5323 }
5324
da0436e9
JS
5325 /* Detect polling mode and jump to a handler */
5326 if (!phba->sli4_hba.intr_enable) {
5327 if (flag == MBX_POLL)
5328 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5329 else
5330 rc = -EIO;
5331 if (rc != MBX_SUCCESS)
5332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5333 "(%d):2541 Mailbox command x%x "
5334 "(x%x) cannot issue Data: x%x x%x\n",
5335 mboxq->vport ? mboxq->vport->vpi : 0,
5336 mboxq->u.mb.mbxCommand,
5337 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5338 psli->sli_flag, flag);
5339 return rc;
5340 } else if (flag == MBX_POLL) {
f1126688
JS
5341 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5342 "(%d):2542 Try to issue mailbox command "
5343 "x%x (x%x) synchronously ahead of async"
5344 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5345 mboxq->vport ? mboxq->vport->vpi : 0,
5346 mboxq->u.mb.mbxCommand,
5347 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5348 psli->sli_flag, flag);
f1126688
JS
5349 /* Try to block the asynchronous mailbox posting */
5350 rc = lpfc_sli4_async_mbox_block(phba);
5351 if (!rc) {
5352 /* Successfully blocked, now issue sync mbox cmd */
5353 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5354 if (rc != MBX_SUCCESS)
5355 lpfc_printf_log(phba, KERN_ERR,
5356 LOG_MBOX | LOG_SLI,
5357 "(%d):2597 Mailbox command "
5358 "x%x (x%x) cannot issue "
5359 "Data: x%x x%x\n",
5360 mboxq->vport ?
5361 mboxq->vport->vpi : 0,
5362 mboxq->u.mb.mbxCommand,
5363 lpfc_sli4_mbox_opcode_get(phba,
5364 mboxq),
5365 psli->sli_flag, flag);
5366 /* Unblock the async mailbox posting afterward */
5367 lpfc_sli4_async_mbox_unblock(phba);
5368 }
5369 return rc;
da0436e9
JS
5370 }
5371
5372 /* Now, interrupt mode asynchrous mailbox command */
5373 rc = lpfc_mbox_cmd_check(phba, mboxq);
5374 if (rc) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5376 "(%d):2543 Mailbox command x%x (x%x) "
5377 "cannot issue Data: x%x x%x\n",
5378 mboxq->vport ? mboxq->vport->vpi : 0,
5379 mboxq->u.mb.mbxCommand,
5380 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5381 psli->sli_flag, flag);
5382 goto out_not_finished;
5383 }
da0436e9
JS
5384
5385 /* Put the mailbox command to the driver internal FIFO */
5386 psli->slistat.mbox_busy++;
5387 spin_lock_irqsave(&phba->hbalock, iflags);
5388 lpfc_mbox_put(phba, mboxq);
5389 spin_unlock_irqrestore(&phba->hbalock, iflags);
5390 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5391 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5392 "x%x (x%x) x%x x%x x%x\n",
5393 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5394 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5395 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5396 phba->pport->port_state,
5397 psli->sli_flag, MBX_NOWAIT);
5398 /* Wake up worker thread to transport mailbox command from head */
5399 lpfc_worker_wake_up(phba);
5400
5401 return MBX_BUSY;
5402
5403out_not_finished:
5404 return MBX_NOT_FINISHED;
5405}
5406
5407/**
5408 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5409 * @phba: Pointer to HBA context object.
5410 *
5411 * This function is called by worker thread to send a mailbox command to
5412 * SLI4 HBA firmware.
5413 *
5414 **/
5415int
5416lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5417{
5418 struct lpfc_sli *psli = &phba->sli;
5419 LPFC_MBOXQ_t *mboxq;
5420 int rc = MBX_SUCCESS;
5421 unsigned long iflags;
5422 struct lpfc_mqe *mqe;
5423 uint32_t mbx_cmnd;
5424
5425 /* Check interrupt mode before post async mailbox command */
5426 if (unlikely(!phba->sli4_hba.intr_enable))
5427 return MBX_NOT_FINISHED;
5428
5429 /* Check for mailbox command service token */
5430 spin_lock_irqsave(&phba->hbalock, iflags);
5431 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5432 spin_unlock_irqrestore(&phba->hbalock, iflags);
5433 return MBX_NOT_FINISHED;
5434 }
5435 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5436 spin_unlock_irqrestore(&phba->hbalock, iflags);
5437 return MBX_NOT_FINISHED;
5438 }
5439 if (unlikely(phba->sli.mbox_active)) {
5440 spin_unlock_irqrestore(&phba->hbalock, iflags);
5441 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5442 "0384 There is pending active mailbox cmd\n");
5443 return MBX_NOT_FINISHED;
5444 }
5445 /* Take the mailbox command service token */
5446 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5447
5448 /* Get the next mailbox command from head of queue */
5449 mboxq = lpfc_mbox_get(phba);
5450
5451 /* If no more mailbox command waiting for post, we're done */
5452 if (!mboxq) {
5453 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5454 spin_unlock_irqrestore(&phba->hbalock, iflags);
5455 return MBX_SUCCESS;
5456 }
5457 phba->sli.mbox_active = mboxq;
5458 spin_unlock_irqrestore(&phba->hbalock, iflags);
5459
5460 /* Check device readiness for posting mailbox command */
5461 rc = lpfc_mbox_dev_check(phba);
5462 if (unlikely(rc))
5463 /* Driver clean routine will clean up pending mailbox */
5464 goto out_not_finished;
5465
5466 /* Prepare the mbox command to be posted */
5467 mqe = &mboxq->u.mqe;
5468 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5469
5470 /* Start timer for the mbox_tmo and log some mailbox post messages */
5471 mod_timer(&psli->mbox_tmo, (jiffies +
5472 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5473
5474 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5475 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5476 "x%x x%x\n",
5477 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5478 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5479 phba->pport->port_state, psli->sli_flag);
5480
5481 if (mbx_cmnd != MBX_HEARTBEAT) {
5482 if (mboxq->vport) {
5483 lpfc_debugfs_disc_trc(mboxq->vport,
5484 LPFC_DISC_TRC_MBOX_VPORT,
5485 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5486 mbx_cmnd, mqe->un.mb_words[0],
5487 mqe->un.mb_words[1]);
5488 } else {
5489 lpfc_debugfs_disc_trc(phba->pport,
5490 LPFC_DISC_TRC_MBOX,
5491 "MBOX Send: cmd:x%x mb:x%x x%x",
5492 mbx_cmnd, mqe->un.mb_words[0],
5493 mqe->un.mb_words[1]);
5494 }
5495 }
5496 psli->slistat.mbox_cmd++;
5497
5498 /* Post the mailbox command to the port */
5499 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5500 if (rc != MBX_SUCCESS) {
5501 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5502 "(%d):2533 Mailbox command x%x (x%x) "
5503 "cannot issue Data: x%x x%x\n",
5504 mboxq->vport ? mboxq->vport->vpi : 0,
5505 mboxq->u.mb.mbxCommand,
5506 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5507 psli->sli_flag, MBX_NOWAIT);
5508 goto out_not_finished;
5509 }
5510
5511 return rc;
5512
5513out_not_finished:
5514 spin_lock_irqsave(&phba->hbalock, iflags);
5515 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5516 __lpfc_mbox_cmpl_put(phba, mboxq);
5517 /* Release the token */
5518 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5519 phba->sli.mbox_active = NULL;
5520 spin_unlock_irqrestore(&phba->hbalock, iflags);
5521
5522 return MBX_NOT_FINISHED;
5523}
5524
5525/**
5526 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5527 * @phba: Pointer to HBA context object.
5528 * @pmbox: Pointer to mailbox object.
5529 * @flag: Flag indicating how the mailbox need to be processed.
5530 *
5531 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5532 * the API jump table function pointer from the lpfc_hba struct.
5533 *
5534 * Return codes the caller owns the mailbox command after the return of the
5535 * function.
5536 **/
5537int
5538lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5539{
5540 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5541}
5542
5543/**
5544 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5545 * @phba: The hba struct for which this call is being executed.
5546 * @dev_grp: The HBA PCI-Device group number.
5547 *
5548 * This routine sets up the mbox interface API function jump table in @phba
5549 * struct.
5550 * Returns: 0 - success, -ENODEV - failure.
5551 **/
5552int
5553lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5554{
5555
5556 switch (dev_grp) {
5557 case LPFC_PCI_DEV_LP:
5558 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5559 phba->lpfc_sli_handle_slow_ring_event =
5560 lpfc_sli_handle_slow_ring_event_s3;
5561 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5562 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5563 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5564 break;
5565 case LPFC_PCI_DEV_OC:
5566 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5567 phba->lpfc_sli_handle_slow_ring_event =
5568 lpfc_sli_handle_slow_ring_event_s4;
5569 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5570 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5571 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5572 break;
5573 default:
5574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5575 "1420 Invalid HBA PCI-device group: 0x%x\n",
5576 dev_grp);
5577 return -ENODEV;
5578 break;
5579 }
5580 return 0;
5581}
5582
e59058c4 5583/**
3621a710 5584 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5585 * @phba: Pointer to HBA context object.
5586 * @pring: Pointer to driver SLI ring object.
5587 * @piocb: Pointer to address of newly added command iocb.
5588 *
5589 * This function is called with hbalock held to add a command
5590 * iocb to the txq when SLI layer cannot submit the command iocb
5591 * to the ring.
5592 **/
858c9f6c 5593static void
92d7f7b0 5594__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5595 struct lpfc_iocbq *piocb)
dea3101e
JB
5596{
5597 /* Insert the caller's iocb in the txq tail for later processing. */
5598 list_add_tail(&piocb->list, &pring->txq);
5599 pring->txq_cnt++;
dea3101e
JB
5600}
5601
e59058c4 5602/**
3621a710 5603 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5604 * @phba: Pointer to HBA context object.
5605 * @pring: Pointer to driver SLI ring object.
5606 * @piocb: Pointer to address of newly added command iocb.
5607 *
5608 * This function is called with hbalock held before a new
5609 * iocb is submitted to the firmware. This function checks
5610 * txq to flush the iocbs in txq to Firmware before
5611 * submitting new iocbs to the Firmware.
5612 * If there are iocbs in the txq which need to be submitted
5613 * to firmware, lpfc_sli_next_iocb returns the first element
5614 * of the txq after dequeuing it from txq.
5615 * If there is no iocb in the txq then the function will return
5616 * *piocb and *piocb is set to NULL. Caller needs to check
5617 * *piocb to find if there are more commands in the txq.
5618 **/
dea3101e
JB
5619static struct lpfc_iocbq *
5620lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5621 struct lpfc_iocbq **piocb)
dea3101e
JB
5622{
5623 struct lpfc_iocbq * nextiocb;
5624
5625 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5626 if (!nextiocb) {
5627 nextiocb = *piocb;
5628 *piocb = NULL;
5629 }
5630
5631 return nextiocb;
5632}
5633
e59058c4 5634/**
3772a991 5635 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5636 * @phba: Pointer to HBA context object.
3772a991 5637 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5638 * @piocb: Pointer to command iocb.
5639 * @flag: Flag indicating if this command can be put into txq.
5640 *
3772a991
JS
5641 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5642 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5643 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5644 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5645 * this function allows only iocbs for posting buffers. This function finds
5646 * next available slot in the command ring and posts the command to the
5647 * available slot and writes the port attention register to request HBA start
5648 * processing new iocb. If there is no slot available in the ring and
5649 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5650 * the function returns IOCB_BUSY.
e59058c4 5651 *
3772a991
JS
5652 * This function is called with hbalock held. The function will return success
5653 * after it successfully submit the iocb to firmware or after adding to the
5654 * txq.
e59058c4 5655 **/
98c9ea5c 5656static int
3772a991 5657__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
5658 struct lpfc_iocbq *piocb, uint32_t flag)
5659{
5660 struct lpfc_iocbq *nextiocb;
5661 IOCB_t *iocb;
3772a991 5662 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5663
92d7f7b0
JS
5664 if (piocb->iocb_cmpl && (!piocb->vport) &&
5665 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5666 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5667 lpfc_printf_log(phba, KERN_ERR,
5668 LOG_SLI | LOG_VPORT,
e8b62011 5669 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5670 piocb->iocb.ulpCommand);
5671 dump_stack();
5672 return IOCB_ERROR;
5673 }
5674
5675
8d63f375
LV
5676 /* If the PCI channel is in offline state, do not post iocbs. */
5677 if (unlikely(pci_channel_offline(phba->pcidev)))
5678 return IOCB_ERROR;
5679
a257bf90
JS
5680 /* If HBA has a deferred error attention, fail the iocb. */
5681 if (unlikely(phba->hba_flag & DEFER_ERATT))
5682 return IOCB_ERROR;
5683
dea3101e
JB
5684 /*
5685 * We should never get an IOCB if we are in a < LINK_DOWN state
5686 */
2e0fef85 5687 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
5688 return IOCB_ERROR;
5689
5690 /*
5691 * Check to see if we are blocking IOCB processing because of a
0b727fea 5692 * outstanding event.
dea3101e 5693 */
0b727fea 5694 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
5695 goto iocb_busy;
5696
2e0fef85 5697 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5698 /*
2680eeaa 5699 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
5700 * can be issued if the link is not up.
5701 */
5702 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5703 case CMD_GEN_REQUEST64_CR:
5704 case CMD_GEN_REQUEST64_CX:
5705 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5706 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5707 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5708 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5709 MENLO_TRANSPORT_TYPE))
5710
5711 goto iocb_busy;
5712 break;
dea3101e
JB
5713 case CMD_QUE_RING_BUF_CN:
5714 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
5715 /*
5716 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5717 * completion, iocb_cmpl MUST be 0.
5718 */
5719 if (piocb->iocb_cmpl)
5720 piocb->iocb_cmpl = NULL;
5721 /*FALLTHROUGH*/
5722 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5723 case CMD_CLOSE_XRI_CN:
5724 case CMD_CLOSE_XRI_CX:
dea3101e
JB
5725 break;
5726 default:
5727 goto iocb_busy;
5728 }
5729
5730 /*
5731 * For FCP commands, we must be in a state where we can process link
5732 * attention events.
5733 */
5734 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5735 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5736 goto iocb_busy;
92d7f7b0 5737 }
dea3101e 5738
dea3101e
JB
5739 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5740 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5741 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5742
5743 if (iocb)
5744 lpfc_sli_update_ring(phba, pring);
5745 else
5746 lpfc_sli_update_full_ring(phba, pring);
5747
5748 if (!piocb)
5749 return IOCB_SUCCESS;
5750
5751 goto out_busy;
5752
5753 iocb_busy:
5754 pring->stats.iocb_cmd_delay++;
5755
5756 out_busy:
5757
5758 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5759 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
5760 return IOCB_SUCCESS;
5761 }
5762
5763 return IOCB_BUSY;
5764}
5765
3772a991 5766/**
4f774513
JS
5767 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5768 * @phba: Pointer to HBA context object.
5769 * @piocb: Pointer to command iocb.
5770 * @sglq: Pointer to the scatter gather queue object.
5771 *
5772 * This routine converts the bpl or bde that is in the IOCB
5773 * to a sgl list for the sli4 hardware. The physical address
5774 * of the bpl/bde is converted back to a virtual address.
5775 * If the IOCB contains a BPL then the list of BDE's is
5776 * converted to sli4_sge's. If the IOCB contains a single
5777 * BDE then it is converted to a single sli_sge.
5778 * The IOCB is still in cpu endianess so the contents of
5779 * the bpl can be used without byte swapping.
5780 *
5781 * Returns valid XRI = Success, NO_XRI = Failure.
5782**/
5783static uint16_t
5784lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5785 struct lpfc_sglq *sglq)
3772a991 5786{
4f774513
JS
5787 uint16_t xritag = NO_XRI;
5788 struct ulp_bde64 *bpl = NULL;
5789 struct ulp_bde64 bde;
5790 struct sli4_sge *sgl = NULL;
5791 IOCB_t *icmd;
5792 int numBdes = 0;
5793 int i = 0;
3772a991 5794
4f774513
JS
5795 if (!piocbq || !sglq)
5796 return xritag;
5797
5798 sgl = (struct sli4_sge *)sglq->sgl;
5799 icmd = &piocbq->iocb;
5800 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5801 numBdes = icmd->un.genreq64.bdl.bdeSize /
5802 sizeof(struct ulp_bde64);
5803 /* The addrHigh and addrLow fields within the IOCB
5804 * have not been byteswapped yet so there is no
5805 * need to swap them back.
5806 */
5807 bpl = (struct ulp_bde64 *)
5808 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5809
5810 if (!bpl)
5811 return xritag;
5812
5813 for (i = 0; i < numBdes; i++) {
5814 /* Should already be byte swapped. */
28baac74
JS
5815 sgl->addr_hi = bpl->addrHigh;
5816 sgl->addr_lo = bpl->addrLow;
5817
4f774513
JS
5818 if ((i+1) == numBdes)
5819 bf_set(lpfc_sli4_sge_last, sgl, 1);
5820 else
5821 bf_set(lpfc_sli4_sge_last, sgl, 0);
5822 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5823 /* swap the size field back to the cpu so we
5824 * can assign it to the sgl.
5825 */
5826 bde.tus.w = le32_to_cpu(bpl->tus.w);
5827 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
4f774513
JS
5828 bpl++;
5829 sgl++;
5830 }
5831 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5832 /* The addrHigh and addrLow fields of the BDE have not
5833 * been byteswapped yet so they need to be swapped
5834 * before putting them in the sgl.
5835 */
5836 sgl->addr_hi =
5837 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5838 sgl->addr_lo =
5839 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
4f774513
JS
5840 bf_set(lpfc_sli4_sge_last, sgl, 1);
5841 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5842 sgl->sge_len =
5843 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
5844 }
5845 return sglq->sli4_xritag;
3772a991 5846}
92d7f7b0 5847
e59058c4 5848/**
4f774513 5849 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5850 * @phba: Pointer to HBA context object.
e59058c4 5851 *
4f774513 5852 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5853 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5854 * held.
4f774513
JS
5855 *
5856 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5857 **/
4f774513 5858static uint32_t
8fa38513 5859lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5860{
8fa38513
JS
5861 ++phba->fcp_qidx;
5862 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5863 phba->fcp_qidx = 0;
92d7f7b0 5864
8fa38513 5865 return phba->fcp_qidx;
92d7f7b0
JS
5866}
5867
e59058c4 5868/**
4f774513 5869 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5870 * @phba: Pointer to HBA context object.
4f774513
JS
5871 * @piocb: Pointer to command iocb.
5872 * @wqe: Pointer to the work queue entry.
e59058c4 5873 *
4f774513
JS
5874 * This routine converts the iocb command to its Work Queue Entry
5875 * equivalent. The wqe pointer should not have any fields set when
5876 * this routine is called because it will memcpy over them.
5877 * This routine does not set the CQ_ID or the WQEC bits in the
5878 * wqe.
e59058c4 5879 *
4f774513 5880 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5881 **/
cf5bf97e 5882static int
4f774513
JS
5883lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5884 union lpfc_wqe *wqe)
cf5bf97e 5885{
5ffc266e 5886 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
5887 uint8_t ct = 0;
5888 uint32_t fip;
5889 uint32_t abort_tag;
5890 uint8_t command_type = ELS_COMMAND_NON_FIP;
5891 uint8_t cmnd;
5892 uint16_t xritag;
5893 struct ulp_bde64 *bpl = NULL;
c868595d 5894 uint32_t els_id = ELS_ID_DEFAULT;
5ffc266e
JS
5895 int numBdes, i;
5896 struct ulp_bde64 bde;
4f774513 5897
45ed1190 5898 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 5899 /* The fcp commands will set command type */
0c287589 5900 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5901 command_type = FCP_COMMAND;
c868595d 5902 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
5903 command_type = ELS_COMMAND_FIP;
5904 else
5905 command_type = ELS_COMMAND_NON_FIP;
5906
4f774513
JS
5907 /* Some of the fields are in the right position already */
5908 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5909 abort_tag = (uint32_t) iocbq->iotag;
5910 xritag = iocbq->sli4_xritag;
5911 wqe->words[7] = 0; /* The ct field has moved so reset */
5912 /* words0-2 bpl convert bde */
5913 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
5914 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5915 sizeof(struct ulp_bde64);
4f774513
JS
5916 bpl = (struct ulp_bde64 *)
5917 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5918 if (!bpl)
5919 return IOCB_ERROR;
cf5bf97e 5920
4f774513
JS
5921 /* Should already be byte swapped. */
5922 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5923 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5924 /* swap the size field back to the cpu so we
5925 * can assign it to the sgl.
5926 */
5927 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
5928 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5929 total_len = 0;
5930 for (i = 0; i < numBdes; i++) {
5931 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5932 total_len += bde.tus.f.bdeSize;
5933 }
4f774513 5934 } else
5ffc266e 5935 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 5936
4f774513
JS
5937 iocbq->iocb.ulpIoTag = iocbq->iotag;
5938 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 5939
4f774513
JS
5940 switch (iocbq->iocb.ulpCommand) {
5941 case CMD_ELS_REQUEST64_CR:
5942 if (!iocbq->iocb.ulpLe) {
5943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5944 "2007 Only Limited Edition cmd Format"
5945 " supported 0x%x\n",
5946 iocbq->iocb.ulpCommand);
5947 return IOCB_ERROR;
5948 }
5ffc266e 5949 wqe->els_req.payload_len = xmit_len;
4f774513
JS
5950 /* Els_reguest64 has a TMO */
5951 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5952 iocbq->iocb.ulpTimeout);
5953 /* Need a VF for word 4 set the vf bit*/
5954 bf_set(els_req64_vf, &wqe->els_req, 0);
5955 /* And a VFID for word 12 */
5956 bf_set(els_req64_vfid, &wqe->els_req, 0);
5957 /*
5958 * Set ct field to 3, indicates that the context_tag field
5959 * contains the FCFI and remote N_Port_ID is
5960 * in word 5.
5961 */
5962
5963 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5964 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5965 iocbq->iocb.ulpContext);
5966
4f774513
JS
5967 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5968 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5969 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
5970
5971 if (command_type == ELS_COMMAND_FIP) {
5972 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5973 >> LPFC_FIP_ELS_ID_SHIFT);
5974 }
5975 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5976
4f774513 5977 break;
5ffc266e
JS
5978 case CMD_XMIT_SEQUENCE64_CX:
5979 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5980 iocbq->iocb.un.ulpWord[3]);
5981 wqe->generic.word3 = 0;
5982 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5ffc266e
JS
5983 /* The entire sequence is transmitted for this IOCB */
5984 xmit_len = total_len;
5985 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513
JS
5986 case CMD_XMIT_SEQUENCE64_CR:
5987 /* word3 iocb=io_tag32 wqe=payload_offset */
5988 /* payload offset used for multilpe outstanding
5989 * sequences on the same exchange
5990 */
5991 wqe->words[3] = 0;
5992 /* word4 relative_offset memcpy */
5993 /* word5 r_ctl/df_ctl memcpy */
5994 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5ffc266e
JS
5995 wqe->xmit_sequence.xmit_len = xmit_len;
5996 command_type = OTHER_COMMAND;
4f774513
JS
5997 break;
5998 case CMD_XMIT_BCAST64_CN:
5999 /* word3 iocb=iotag32 wqe=payload_len */
6000 wqe->words[3] = 0; /* no definition for this in wqe */
6001 /* word4 iocb=rsvd wqe=rsvd */
6002 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
6003 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
6004 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6005 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6006 break;
6007 case CMD_FCP_IWRITE64_CR:
6008 command_type = FCP_COMMAND_DATA_OUT;
6009 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
6010 * confusing.
6011 * word3 is payload_len: byte offset to the sgl entry for the
6012 * fcp_command.
6013 * word4 is total xfer len, same as the IOCB->ulpParameter.
6014 * word5 is initial xfer len 0 = wait for xfer-ready
6015 */
6016
6017 /* Always wait for xfer-ready before sending data */
6018 wqe->fcp_iwrite.initial_xfer_len = 0;
6019 /* word 4 (xfer length) should have been set on the memcpy */
6020
6021 /* allow write to fall through to read */
6022 case CMD_FCP_IREAD64_CR:
6023 /* FCP_CMD is always the 1st sgl entry */
6024 wqe->fcp_iread.payload_len =
5ffc266e 6025 xmit_len + sizeof(struct fcp_rsp);
4f774513
JS
6026
6027 /* word 4 (xfer length) should have been set on the memcpy */
6028
6029 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6030 iocbq->iocb.ulpFCP2Rcvy);
6031 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6032 /* The XC bit and the XS bit are similar. The driver never
6033 * tracked whether or not the exchange was previouslly open.
6034 * XC = Exchange create, 0 is create. 1 is already open.
6035 * XS = link cmd: 1 do not close the exchange after command.
6036 * XS = 0 close exchange when command completes.
6037 * The only time we would not set the XC bit is when the XS bit
6038 * is set and we are sending our 2nd or greater command on
6039 * this exchange.
6040 */
f1126688
JS
6041 /* Always open the exchange */
6042 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
4f774513 6043
f1126688
JS
6044 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6045 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6046 break;
4f774513
JS
6047 case CMD_FCP_ICMND64_CR:
6048 /* Always open the exchange */
6049 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6050
f1126688 6051 wqe->words[4] = 0;
4f774513 6052 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
f1126688 6053 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
4f774513
JS
6054 break;
6055 case CMD_GEN_REQUEST64_CR:
6056 /* word3 command length is described as byte offset to the
6057 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6058 * sgl[0] = cmnd
6059 * sgl[1] = rsp.
6060 *
6061 */
5ffc266e 6062 wqe->gen_req.command_len = xmit_len;
4f774513
JS
6063 /* Word4 parameter copied in the memcpy */
6064 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6065 /* word6 context tag copied in memcpy */
6066 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6067 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6069 "2015 Invalid CT %x command 0x%x\n",
6070 ct, iocbq->iocb.ulpCommand);
6071 return IOCB_ERROR;
6072 }
6073 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6074 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6075 iocbq->iocb.ulpTimeout);
6076
6077 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6078 command_type = OTHER_COMMAND;
6079 break;
6080 case CMD_XMIT_ELS_RSP64_CX:
6081 /* words0-2 BDE memcpy */
6082 /* word3 iocb=iotag32 wqe=rsvd */
6083 wqe->words[3] = 0;
6084 /* word4 iocb=did wge=rsvd. */
6085 wqe->words[4] = 0;
6086 /* word5 iocb=rsvd wge=did */
6087 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6088 iocbq->iocb.un.elsreq64.remoteID);
6089
6090 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6091 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6092
6093 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6094 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6095 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6096 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6097 iocbq->vport->vpi + phba->vpi_base);
6098 command_type = OTHER_COMMAND;
6099 break;
6100 case CMD_CLOSE_XRI_CN:
6101 case CMD_ABORT_XRI_CN:
6102 case CMD_ABORT_XRI_CX:
6103 /* words 0-2 memcpy should be 0 rserved */
6104 /* port will send abts */
6105 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6106 /*
6107 * The link is down so the fw does not need to send abts
6108 * on the wire.
6109 */
6110 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6111 else
6112 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6113 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
4f774513
JS
6114 wqe->words[5] = 0;
6115 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6116 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6117 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
6118 /*
6119 * The abort handler will send us CMD_ABORT_XRI_CN or
6120 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6121 */
6122 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6123 cmnd = CMD_ABORT_XRI_CX;
6124 command_type = OTHER_COMMAND;
6125 xritag = 0;
6126 break;
6669f9bb
JS
6127 case CMD_XMIT_BLS_RSP64_CX:
6128 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6129 * we re-construct this WQE here based on information in
6130 * iocbq from scratch.
6131 */
6132 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 6133 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 6134 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
5ffc266e
JS
6135 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6136 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6137 LPFC_ABTS_UNSOL_INT) {
6138 /* ABTS sent by initiator to CT exchange, the
6139 * RX_ID field will be filled with the newly
6140 * allocated responder XRI.
6141 */
6142 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6143 iocbq->sli4_xritag);
6144 } else {
6145 /* ABTS sent by responder to CT exchange, the
6146 * RX_ID field will be filled with the responder
6147 * RX_ID from ABTS.
6148 */
6149 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6150 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6151 }
6669f9bb
JS
6152 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6153 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6154 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6155 iocbq->iocb.ulpContext);
6156 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6157 command_type = OTHER_COMMAND;
6158 break;
4f774513
JS
6159 case CMD_XRI_ABORTED_CX:
6160 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6161 /* words0-2 are all 0's no bde */
6162 /* word3 and word4 are rsvrd */
6163 wqe->words[3] = 0;
6164 wqe->words[4] = 0;
6165 /* word5 iocb=rsvd wge=did */
6166 /* There is no remote port id in the IOCB? */
6167 /* Let this fall through and fail */
6168 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6169 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6170 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6171 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6172 default:
6173 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6174 "2014 Invalid command 0x%x\n",
6175 iocbq->iocb.ulpCommand);
6176 return IOCB_ERROR;
6177 break;
6178
6179 }
6180 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6181 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6182 wqe->generic.abort_tag = abort_tag;
6183 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6184 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6185 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6186 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6187
6188 return 0;
6189}
6190
6191/**
6192 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6193 * @phba: Pointer to HBA context object.
6194 * @ring_number: SLI ring number to issue iocb on.
6195 * @piocb: Pointer to command iocb.
6196 * @flag: Flag indicating if this command can be put into txq.
6197 *
6198 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6199 * an iocb command to an HBA with SLI-4 interface spec.
6200 *
6201 * This function is called with hbalock held. The function will return success
6202 * after it successfully submit the iocb to firmware or after adding to the
6203 * txq.
6204 **/
6205static int
6206__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6207 struct lpfc_iocbq *piocb, uint32_t flag)
6208{
6209 struct lpfc_sglq *sglq;
6210 uint16_t xritag;
6211 union lpfc_wqe wqe;
6212 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
6213
6214 if (piocb->sli4_xritag == NO_XRI) {
6215 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6216 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6217 sglq = NULL;
6218 else {
6219 sglq = __lpfc_sli_get_sglq(phba);
6220 if (!sglq)
6221 return IOCB_ERROR;
6222 piocb->sli4_xritag = sglq->sli4_xritag;
6223 }
6224 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6225 sglq = NULL; /* These IO's already have an XRI and
6226 * a mapped sgl.
6227 */
6228 } else {
6229 /* This is a continuation of a commandi,(CX) so this
6230 * sglq is on the active list
6231 */
6232 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6233 if (!sglq)
6234 return IOCB_ERROR;
6235 }
6236
6237 if (sglq) {
6238 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6239 if (xritag != sglq->sli4_xritag)
6240 return IOCB_ERROR;
6241 }
6242
6243 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6244 return IOCB_ERROR;
6245
341af102
JS
6246 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6247 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
6248 /*
6249 * For FCP command IOCB, get a new WQ index to distribute
6250 * WQE across the WQsr. On the other hand, for abort IOCB,
6251 * it carries the same WQ index to the original command
6252 * IOCB.
6253 */
341af102 6254 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
6255 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6256 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6257 &wqe))
4f774513
JS
6258 return IOCB_ERROR;
6259 } else {
6260 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6261 return IOCB_ERROR;
6262 }
6263 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6264
6265 return 0;
6266}
6267
6268/**
6269 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6270 *
6271 * This routine wraps the actual lockless version for issusing IOCB function
6272 * pointer from the lpfc_hba struct.
6273 *
6274 * Return codes:
6275 * IOCB_ERROR - Error
6276 * IOCB_SUCCESS - Success
6277 * IOCB_BUSY - Busy
6278 **/
6279static inline int
6280__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6281 struct lpfc_iocbq *piocb, uint32_t flag)
6282{
6283 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6284}
6285
6286/**
6287 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6288 * @phba: The hba struct for which this call is being executed.
6289 * @dev_grp: The HBA PCI-Device group number.
6290 *
6291 * This routine sets up the SLI interface API function jump table in @phba
6292 * struct.
6293 * Returns: 0 - success, -ENODEV - failure.
6294 **/
6295int
6296lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6297{
6298
6299 switch (dev_grp) {
6300 case LPFC_PCI_DEV_LP:
6301 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6302 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6303 break;
6304 case LPFC_PCI_DEV_OC:
6305 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6306 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6307 break;
6308 default:
6309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6310 "1419 Invalid HBA PCI-device group: 0x%x\n",
6311 dev_grp);
6312 return -ENODEV;
6313 break;
6314 }
6315 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6316 return 0;
6317}
6318
6319/**
6320 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6321 * @phba: Pointer to HBA context object.
6322 * @pring: Pointer to driver SLI ring object.
6323 * @piocb: Pointer to command iocb.
6324 * @flag: Flag indicating if this command can be put into txq.
6325 *
6326 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6327 * function. This function gets the hbalock and calls
6328 * __lpfc_sli_issue_iocb function and will return the error returned
6329 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6330 * functions which do not hold hbalock.
6331 **/
6332int
6333lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6334 struct lpfc_iocbq *piocb, uint32_t flag)
6335{
6336 unsigned long iflags;
6337 int rc;
6338
6339 spin_lock_irqsave(&phba->hbalock, iflags);
6340 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6341 spin_unlock_irqrestore(&phba->hbalock, iflags);
6342
6343 return rc;
6344}
6345
6346/**
6347 * lpfc_extra_ring_setup - Extra ring setup function
6348 * @phba: Pointer to HBA context object.
6349 *
6350 * This function is called while driver attaches with the
6351 * HBA to setup the extra ring. The extra ring is used
6352 * only when driver needs to support target mode functionality
6353 * or IP over FC functionalities.
6354 *
6355 * This function is called with no lock held.
6356 **/
6357static int
6358lpfc_extra_ring_setup( struct lpfc_hba *phba)
6359{
6360 struct lpfc_sli *psli;
6361 struct lpfc_sli_ring *pring;
6362
6363 psli = &phba->sli;
6364
6365 /* Adjust cmd/rsp ring iocb entries more evenly */
6366
6367 /* Take some away from the FCP ring */
6368 pring = &psli->ring[psli->fcp_ring];
6369 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6370 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6371 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6372 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6373
a4bc3379
JS
6374 /* and give them to the extra ring */
6375 pring = &psli->ring[psli->extra_ring];
6376
cf5bf97e
JW
6377 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6378 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6379 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6380 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6381
6382 /* Setup default profile for this ring */
6383 pring->iotag_max = 4096;
6384 pring->num_mask = 1;
6385 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6386 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6387 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6388 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6389 return 0;
6390}
6391
e59058c4 6392/**
3621a710 6393 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6394 * @phba: Pointer to HBA context object.
6395 * @pring: Pointer to driver SLI ring object.
6396 * @iocbq: Pointer to iocb object.
6397 *
6398 * This function is called by the slow ring event handler
6399 * function when there is an ASYNC event iocb in the ring.
6400 * This function is called with no lock held.
6401 * Currently this function handles only temperature related
6402 * ASYNC events. The function decodes the temperature sensor
6403 * event message and posts events for the management applications.
6404 **/
98c9ea5c 6405static void
57127f15
JS
6406lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6407 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6408{
6409 IOCB_t *icmd;
6410 uint16_t evt_code;
6411 uint16_t temp;
6412 struct temp_event temp_event_data;
6413 struct Scsi_Host *shost;
a257bf90 6414 uint32_t *iocb_w;
57127f15
JS
6415
6416 icmd = &iocbq->iocb;
6417 evt_code = icmd->un.asyncstat.evt_code;
6418 temp = icmd->ulpContext;
6419
6420 if ((evt_code != ASYNC_TEMP_WARN) &&
6421 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6422 iocb_w = (uint32_t *) icmd;
57127f15
JS
6423 lpfc_printf_log(phba,
6424 KERN_ERR,
6425 LOG_SLI,
76bb24ef 6426 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6427 " evt_code 0x%x\n"
a257bf90
JS
6428 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6429 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6430 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6431 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6432 pring->ringno,
a257bf90
JS
6433 icmd->un.asyncstat.evt_code,
6434 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6435 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6436 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6437 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6438
57127f15
JS
6439 return;
6440 }
6441 temp_event_data.data = (uint32_t)temp;
6442 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6443 if (evt_code == ASYNC_TEMP_WARN) {
6444 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6445 lpfc_printf_log(phba,
09372820 6446 KERN_ERR,
57127f15 6447 LOG_TEMP,
76bb24ef 6448 "0347 Adapter is very hot, please take "
57127f15
JS
6449 "corrective action. temperature : %d Celsius\n",
6450 temp);
6451 }
6452 if (evt_code == ASYNC_TEMP_SAFE) {
6453 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6454 lpfc_printf_log(phba,
09372820 6455 KERN_ERR,
57127f15
JS
6456 LOG_TEMP,
6457 "0340 Adapter temperature is OK now. "
6458 "temperature : %d Celsius\n",
6459 temp);
6460 }
6461
6462 /* Send temperature change event to applications */
6463 shost = lpfc_shost_from_vport(phba->pport);
6464 fc_host_post_vendor_event(shost, fc_get_event_number(),
6465 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6466 LPFC_NL_VENDOR_ID);
57127f15
JS
6467
6468}
6469
6470
e59058c4 6471/**
3621a710 6472 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6473 * @phba: Pointer to HBA context object.
6474 *
6475 * lpfc_sli_setup sets up rings of the SLI interface with
6476 * number of iocbs per ring and iotags. This function is
6477 * called while driver attach to the HBA and before the
6478 * interrupts are enabled. So there is no need for locking.
6479 *
6480 * This function always returns 0.
6481 **/
dea3101e
JB
6482int
6483lpfc_sli_setup(struct lpfc_hba *phba)
6484{
ed957684 6485 int i, totiocbsize = 0;
dea3101e
JB
6486 struct lpfc_sli *psli = &phba->sli;
6487 struct lpfc_sli_ring *pring;
6488
6489 psli->num_rings = MAX_CONFIGURED_RINGS;
6490 psli->sli_flag = 0;
6491 psli->fcp_ring = LPFC_FCP_RING;
6492 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6493 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6494
604a3e30
JB
6495 psli->iocbq_lookup = NULL;
6496 psli->iocbq_lookup_len = 0;
6497 psli->last_iotag = 0;
6498
dea3101e
JB
6499 for (i = 0; i < psli->num_rings; i++) {
6500 pring = &psli->ring[i];
6501 switch (i) {
6502 case LPFC_FCP_RING: /* ring 0 - FCP */
6503 /* numCiocb and numRiocb are used in config_port */
6504 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6505 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6506 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6507 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6508 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6509 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6510 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6511 SLI3_IOCB_CMD_SIZE :
6512 SLI2_IOCB_CMD_SIZE;
ed957684 6513 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6514 SLI3_IOCB_RSP_SIZE :
6515 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
6516 pring->iotag_ctr = 0;
6517 pring->iotag_max =
92d7f7b0 6518 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
6519 pring->fast_iotag = pring->iotag_max;
6520 pring->num_mask = 0;
6521 break;
a4bc3379 6522 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e
JB
6523 /* numCiocb and numRiocb are used in config_port */
6524 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6525 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6526 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6527 SLI3_IOCB_CMD_SIZE :
6528 SLI2_IOCB_CMD_SIZE;
ed957684 6529 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6530 SLI3_IOCB_RSP_SIZE :
6531 SLI2_IOCB_RSP_SIZE;
2e0fef85 6532 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
6533 pring->num_mask = 0;
6534 break;
6535 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6536 /* numCiocb and numRiocb are used in config_port */
6537 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6538 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6539 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6540 SLI3_IOCB_CMD_SIZE :
6541 SLI2_IOCB_CMD_SIZE;
ed957684 6542 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6543 SLI3_IOCB_RSP_SIZE :
6544 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
6545 pring->fast_iotag = 0;
6546 pring->iotag_ctr = 0;
6547 pring->iotag_max = 4096;
57127f15
JS
6548 pring->lpfc_sli_rcv_async_status =
6549 lpfc_sli_async_event_handler;
6669f9bb 6550 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6551 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6552 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6553 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6554 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6555 lpfc_els_unsol_event;
dea3101e 6556 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6557 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6558 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6559 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6560 lpfc_els_unsol_event;
dea3101e
JB
6561 pring->prt[2].profile = 0; /* Mask 2 */
6562 /* NameServer Inquiry */
6a9c52cf 6563 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6564 /* NameServer */
6a9c52cf 6565 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6566 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6567 lpfc_ct_unsol_event;
dea3101e
JB
6568 pring->prt[3].profile = 0; /* Mask 3 */
6569 /* NameServer response */
6a9c52cf 6570 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6571 /* NameServer */
6a9c52cf 6572 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6573 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6574 lpfc_ct_unsol_event;
6669f9bb
JS
6575 /* abort unsolicited sequence */
6576 pring->prt[4].profile = 0; /* Mask 4 */
6577 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6578 pring->prt[4].type = FC_TYPE_BLS;
6579 pring->prt[4].lpfc_sli_rcv_unsol_event =
6580 lpfc_sli4_ct_abort_unsol_event;
dea3101e
JB
6581 break;
6582 }
ed957684 6583 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6584 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6585 }
ed957684 6586 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6587 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6588 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6589 "SLI2 SLIM Data: x%x x%lx\n",
6590 phba->brd_no, totiocbsize,
6591 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6592 }
cf5bf97e
JW
6593 if (phba->cfg_multi_ring_support == 2)
6594 lpfc_extra_ring_setup(phba);
dea3101e
JB
6595
6596 return 0;
6597}
6598
e59058c4 6599/**
3621a710 6600 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6601 * @phba: Pointer to HBA context object.
6602 *
6603 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6604 * ring. This function also initializes ring indices of each ring.
6605 * This function is called during the initialization of the SLI
6606 * interface of an HBA.
6607 * This function is called with no lock held and always returns
6608 * 1.
6609 **/
dea3101e 6610int
2e0fef85 6611lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e
JB
6612{
6613 struct lpfc_sli *psli;
6614 struct lpfc_sli_ring *pring;
604a3e30 6615 int i;
dea3101e
JB
6616
6617 psli = &phba->sli;
2e0fef85 6618 spin_lock_irq(&phba->hbalock);
dea3101e 6619 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6620 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
6621 /* Initialize list headers for txq and txcmplq as double linked lists */
6622 for (i = 0; i < psli->num_rings; i++) {
6623 pring = &psli->ring[i];
6624 pring->ringno = i;
6625 pring->next_cmdidx = 0;
6626 pring->local_getidx = 0;
6627 pring->cmdidx = 0;
6628 INIT_LIST_HEAD(&pring->txq);
6629 INIT_LIST_HEAD(&pring->txcmplq);
6630 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6631 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6632 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6633 }
2e0fef85
JS
6634 spin_unlock_irq(&phba->hbalock);
6635 return 1;
dea3101e
JB
6636}
6637
04c68496
JS
6638/**
6639 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6640 * @phba: Pointer to HBA context object.
6641 *
6642 * This routine flushes the mailbox command subsystem. It will unconditionally
6643 * flush all the mailbox commands in the three possible stages in the mailbox
6644 * command sub-system: pending mailbox command queue; the outstanding mailbox
6645 * command; and completed mailbox command queue. It is caller's responsibility
6646 * to make sure that the driver is in the proper state to flush the mailbox
6647 * command sub-system. Namely, the posting of mailbox commands into the
6648 * pending mailbox command queue from the various clients must be stopped;
6649 * either the HBA is in a state that it will never works on the outstanding
6650 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6651 * mailbox command has been completed.
6652 **/
6653static void
6654lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6655{
6656 LIST_HEAD(completions);
6657 struct lpfc_sli *psli = &phba->sli;
6658 LPFC_MBOXQ_t *pmb;
6659 unsigned long iflag;
6660
6661 /* Flush all the mailbox commands in the mbox system */
6662 spin_lock_irqsave(&phba->hbalock, iflag);
6663 /* The pending mailbox command queue */
6664 list_splice_init(&phba->sli.mboxq, &completions);
6665 /* The outstanding active mailbox command */
6666 if (psli->mbox_active) {
6667 list_add_tail(&psli->mbox_active->list, &completions);
6668 psli->mbox_active = NULL;
6669 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6670 }
6671 /* The completed mailbox command queue */
6672 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6673 spin_unlock_irqrestore(&phba->hbalock, iflag);
6674
6675 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6676 while (!list_empty(&completions)) {
6677 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6678 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6679 if (pmb->mbox_cmpl)
6680 pmb->mbox_cmpl(phba, pmb);
6681 }
6682}
6683
e59058c4 6684/**
3621a710 6685 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6686 * @vport: Pointer to virtual port object.
6687 *
6688 * lpfc_sli_host_down is called to clean up the resources
6689 * associated with a vport before destroying virtual
6690 * port data structures.
6691 * This function does following operations:
6692 * - Free discovery resources associated with this virtual
6693 * port.
6694 * - Free iocbs associated with this virtual port in
6695 * the txq.
6696 * - Send abort for all iocb commands associated with this
6697 * vport in txcmplq.
6698 *
6699 * This function is called with no lock held and always returns 1.
6700 **/
92d7f7b0
JS
6701int
6702lpfc_sli_host_down(struct lpfc_vport *vport)
6703{
858c9f6c 6704 LIST_HEAD(completions);
92d7f7b0
JS
6705 struct lpfc_hba *phba = vport->phba;
6706 struct lpfc_sli *psli = &phba->sli;
6707 struct lpfc_sli_ring *pring;
6708 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6709 int i;
6710 unsigned long flags = 0;
6711 uint16_t prev_pring_flag;
6712
6713 lpfc_cleanup_discovery_resources(vport);
6714
6715 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6716 for (i = 0; i < psli->num_rings; i++) {
6717 pring = &psli->ring[i];
6718 prev_pring_flag = pring->flag;
5e9d9b82
JS
6719 /* Only slow rings */
6720 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6721 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6722 /* Set the lpfc data pending flag */
6723 set_bit(LPFC_DATA_READY, &phba->data_flags);
6724 }
92d7f7b0
JS
6725 /*
6726 * Error everything on the txq since these iocbs have not been
6727 * given to the FW yet.
6728 */
92d7f7b0
JS
6729 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6730 if (iocb->vport != vport)
6731 continue;
858c9f6c 6732 list_move_tail(&iocb->list, &completions);
92d7f7b0 6733 pring->txq_cnt--;
92d7f7b0
JS
6734 }
6735
6736 /* Next issue ABTS for everything on the txcmplq */
6737 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6738 list) {
6739 if (iocb->vport != vport)
6740 continue;
6741 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6742 }
6743
6744 pring->flag = prev_pring_flag;
6745 }
6746
6747 spin_unlock_irqrestore(&phba->hbalock, flags);
6748
a257bf90
JS
6749 /* Cancel all the IOCBs from the completions list */
6750 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6751 IOERR_SLI_DOWN);
92d7f7b0
JS
6752 return 1;
6753}
6754
e59058c4 6755/**
3621a710 6756 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6757 * @phba: Pointer to HBA context object.
6758 *
6759 * This function cleans up all iocb, buffers, mailbox commands
6760 * while shutting down the HBA. This function is called with no
6761 * lock held and always returns 1.
6762 * This function does the following to cleanup driver resources:
6763 * - Free discovery resources for each virtual port
6764 * - Cleanup any pending fabric iocbs
6765 * - Iterate through the iocb txq and free each entry
6766 * in the list.
6767 * - Free up any buffer posted to the HBA
6768 * - Free mailbox commands in the mailbox queue.
6769 **/
dea3101e 6770int
2e0fef85 6771lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6772{
2534ba75 6773 LIST_HEAD(completions);
2e0fef85 6774 struct lpfc_sli *psli = &phba->sli;
dea3101e 6775 struct lpfc_sli_ring *pring;
0ff10d46 6776 struct lpfc_dmabuf *buf_ptr;
dea3101e 6777 unsigned long flags = 0;
04c68496
JS
6778 int i;
6779
6780 /* Shutdown the mailbox command sub-system */
6781 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6782
dea3101e
JB
6783 lpfc_hba_down_prep(phba);
6784
92d7f7b0
JS
6785 lpfc_fabric_abort_hba(phba);
6786
2e0fef85 6787 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e
JB
6788 for (i = 0; i < psli->num_rings; i++) {
6789 pring = &psli->ring[i];
5e9d9b82
JS
6790 /* Only slow rings */
6791 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6792 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6793 /* Set the lpfc data pending flag */
6794 set_bit(LPFC_DATA_READY, &phba->data_flags);
6795 }
dea3101e
JB
6796
6797 /*
6798 * Error everything on the txq since these iocbs have not been
6799 * given to the FW yet.
6800 */
2534ba75 6801 list_splice_init(&pring->txq, &completions);
dea3101e
JB
6802 pring->txq_cnt = 0;
6803
2534ba75 6804 }
2e0fef85 6805 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6806
a257bf90
JS
6807 /* Cancel all the IOCBs from the completions list */
6808 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6809 IOERR_SLI_DOWN);
dea3101e 6810
0ff10d46
JS
6811 spin_lock_irqsave(&phba->hbalock, flags);
6812 list_splice_init(&phba->elsbuf, &completions);
6813 phba->elsbuf_cnt = 0;
6814 phba->elsbuf_prev_cnt = 0;
6815 spin_unlock_irqrestore(&phba->hbalock, flags);
6816
6817 while (!list_empty(&completions)) {
6818 list_remove_head(&completions, buf_ptr,
6819 struct lpfc_dmabuf, list);
6820 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6821 kfree(buf_ptr);
6822 }
6823
dea3101e
JB
6824 /* Return any active mbox cmds */
6825 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6826
da0436e9 6827 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6828 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6829 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6830
da0436e9
JS
6831 return 1;
6832}
6833
6834/**
6835 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6836 * @phba: Pointer to HBA context object.
6837 *
6838 * This function cleans up all queues, iocb, buffers, mailbox commands while
6839 * shutting down the SLI4 HBA FCoE function. This function is called with no
6840 * lock held and always returns 1.
6841 *
6842 * This function does the following to cleanup driver FCoE function resources:
6843 * - Free discovery resources for each virtual port
6844 * - Cleanup any pending fabric iocbs
6845 * - Iterate through the iocb txq and free each entry in the list.
6846 * - Free up any buffer posted to the HBA.
6847 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6848 * - Free mailbox commands in the mailbox queue.
6849 **/
6850int
6851lpfc_sli4_hba_down(struct lpfc_hba *phba)
6852{
6853 /* Stop the SLI4 device port */
6854 lpfc_stop_port(phba);
6855
6856 /* Tear down the queues in the HBA */
6857 lpfc_sli4_queue_unset(phba);
6858
6859 /* unregister default FCFI from the HBA */
6860 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
92d7f7b0 6861
dea3101e
JB
6862 return 1;
6863}
6864
e59058c4 6865/**
3621a710 6866 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6867 * @srcp: Source memory pointer.
6868 * @destp: Destination memory pointer.
6869 * @cnt: Number of words required to be copied.
6870 *
6871 * This function is used for copying data between driver memory
6872 * and the SLI memory. This function also changes the endianness
6873 * of each word if native endianness is different from SLI
6874 * endianness. This function can be called with or without
6875 * lock.
6876 **/
dea3101e
JB
6877void
6878lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6879{
6880 uint32_t *src = srcp;
6881 uint32_t *dest = destp;
6882 uint32_t ldata;
6883 int i;
6884
6885 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6886 ldata = *src;
6887 ldata = le32_to_cpu(ldata);
6888 *dest = ldata;
6889 src++;
6890 dest++;
6891 }
6892}
6893
e59058c4 6894
a0c87cbd
JS
6895/**
6896 * lpfc_sli_bemem_bcopy - SLI memory copy function
6897 * @srcp: Source memory pointer.
6898 * @destp: Destination memory pointer.
6899 * @cnt: Number of words required to be copied.
6900 *
6901 * This function is used for copying data between a data structure
6902 * with big endian representation to local endianness.
6903 * This function can be called with or without lock.
6904 **/
6905void
6906lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6907{
6908 uint32_t *src = srcp;
6909 uint32_t *dest = destp;
6910 uint32_t ldata;
6911 int i;
6912
6913 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6914 ldata = *src;
6915 ldata = be32_to_cpu(ldata);
6916 *dest = ldata;
6917 src++;
6918 dest++;
6919 }
6920}
6921
e59058c4 6922/**
3621a710 6923 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
6924 * @phba: Pointer to HBA context object.
6925 * @pring: Pointer to driver SLI ring object.
6926 * @mp: Pointer to driver buffer object.
6927 *
6928 * This function is called with no lock held.
6929 * It always return zero after adding the buffer to the postbufq
6930 * buffer list.
6931 **/
dea3101e 6932int
2e0fef85
JS
6933lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6934 struct lpfc_dmabuf *mp)
dea3101e
JB
6935{
6936 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6937 later */
2e0fef85 6938 spin_lock_irq(&phba->hbalock);
dea3101e 6939 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 6940 pring->postbufq_cnt++;
2e0fef85 6941 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
6942 return 0;
6943}
6944
e59058c4 6945/**
3621a710 6946 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
6947 * @phba: Pointer to HBA context object.
6948 *
6949 * When HBQ is enabled, buffers are searched based on tags. This function
6950 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6951 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6952 * does not conflict with tags of buffer posted for unsolicited events.
6953 * The function returns the allocated tag. The function is called with
6954 * no locks held.
6955 **/
76bb24ef
JS
6956uint32_t
6957lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6958{
6959 spin_lock_irq(&phba->hbalock);
6960 phba->buffer_tag_count++;
6961 /*
6962 * Always set the QUE_BUFTAG_BIT to distiguish between
6963 * a tag assigned by HBQ.
6964 */
6965 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6966 spin_unlock_irq(&phba->hbalock);
6967 return phba->buffer_tag_count;
6968}
6969
e59058c4 6970/**
3621a710 6971 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
6972 * @phba: Pointer to HBA context object.
6973 * @pring: Pointer to driver SLI ring object.
6974 * @tag: Buffer tag.
6975 *
6976 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6977 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6978 * iocb is posted to the response ring with the tag of the buffer.
6979 * This function searches the pring->postbufq list using the tag
6980 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6981 * iocb. If the buffer is found then lpfc_dmabuf object of the
6982 * buffer is returned to the caller else NULL is returned.
6983 * This function is called with no lock held.
6984 **/
76bb24ef
JS
6985struct lpfc_dmabuf *
6986lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6987 uint32_t tag)
6988{
6989 struct lpfc_dmabuf *mp, *next_mp;
6990 struct list_head *slp = &pring->postbufq;
6991
6992 /* Search postbufq, from the begining, looking for a match on tag */
6993 spin_lock_irq(&phba->hbalock);
6994 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6995 if (mp->buffer_tag == tag) {
6996 list_del_init(&mp->list);
6997 pring->postbufq_cnt--;
6998 spin_unlock_irq(&phba->hbalock);
6999 return mp;
7000 }
7001 }
7002
7003 spin_unlock_irq(&phba->hbalock);
7004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 7005 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
7006 "ring %d Data x%lx x%p x%p x%x\n",
7007 pring->ringno, (unsigned long) tag,
7008 slp->next, slp->prev, pring->postbufq_cnt);
7009
7010 return NULL;
7011}
dea3101e 7012
e59058c4 7013/**
3621a710 7014 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
7015 * @phba: Pointer to HBA context object.
7016 * @pring: Pointer to driver SLI ring object.
7017 * @phys: DMA address of the buffer.
7018 *
7019 * This function searches the buffer list using the dma_address
7020 * of unsolicited event to find the driver's lpfc_dmabuf object
7021 * corresponding to the dma_address. The function returns the
7022 * lpfc_dmabuf object if a buffer is found else it returns NULL.
7023 * This function is called by the ct and els unsolicited event
7024 * handlers to get the buffer associated with the unsolicited
7025 * event.
7026 *
7027 * This function is called with no lock held.
7028 **/
dea3101e
JB
7029struct lpfc_dmabuf *
7030lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7031 dma_addr_t phys)
7032{
7033 struct lpfc_dmabuf *mp, *next_mp;
7034 struct list_head *slp = &pring->postbufq;
7035
7036 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 7037 spin_lock_irq(&phba->hbalock);
dea3101e
JB
7038 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7039 if (mp->phys == phys) {
7040 list_del_init(&mp->list);
7041 pring->postbufq_cnt--;
2e0fef85 7042 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
7043 return mp;
7044 }
7045 }
7046
2e0fef85 7047 spin_unlock_irq(&phba->hbalock);
dea3101e 7048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 7049 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 7050 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 7051 pring->ringno, (unsigned long long)phys,
dea3101e
JB
7052 slp->next, slp->prev, pring->postbufq_cnt);
7053 return NULL;
7054}
7055
e59058c4 7056/**
3621a710 7057 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
7058 * @phba: Pointer to HBA context object.
7059 * @cmdiocb: Pointer to driver command iocb object.
7060 * @rspiocb: Pointer to driver response iocb object.
7061 *
7062 * This function is the completion handler for the abort iocbs for
7063 * ELS commands. This function is called from the ELS ring event
7064 * handler with no lock held. This function frees memory resources
7065 * associated with the abort iocb.
7066 **/
dea3101e 7067static void
2e0fef85
JS
7068lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7069 struct lpfc_iocbq *rspiocb)
dea3101e 7070{
2e0fef85 7071 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7072 uint16_t abort_iotag, abort_context;
92d7f7b0 7073 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7074 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7075
7076 abort_iocb = NULL;
2680eeaa
JS
7077
7078 if (irsp->ulpStatus) {
7079 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7080 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7081
2e0fef85 7082 spin_lock_irq(&phba->hbalock);
45ed1190
JS
7083 if (phba->sli_rev < LPFC_SLI_REV4) {
7084 if (abort_iotag != 0 &&
7085 abort_iotag <= phba->sli.last_iotag)
7086 abort_iocb =
7087 phba->sli.iocbq_lookup[abort_iotag];
7088 } else
7089 /* For sli4 the abort_tag is the XRI,
7090 * so the abort routine puts the iotag of the iocb
7091 * being aborted in the context field of the abort
7092 * IOCB.
7093 */
7094 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 7095
92d7f7b0 7096 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
e8b62011 7097 "0327 Cannot abort els iocb %p "
92d7f7b0
JS
7098 "with tag %x context %x, abort status %x, "
7099 "abort code %x\n",
e8b62011
JS
7100 abort_iocb, abort_iotag, abort_context,
7101 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa 7102
58da1ffb
JS
7103 /*
7104 * If the iocb is not found in Firmware queue the iocb
7105 * might have completed already. Do not free it again.
7106 */
9b379605 7107 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
7108 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7109 spin_unlock_irq(&phba->hbalock);
7110 lpfc_sli_release_iocbq(phba, cmdiocb);
7111 return;
7112 }
7113 /* For SLI4 the ulpContext field for abort IOCB
7114 * holds the iotag of the IOCB being aborted so
7115 * the local abort_context needs to be reset to
7116 * match the aborted IOCBs ulpContext.
7117 */
7118 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7119 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 7120 }
2680eeaa
JS
7121 /*
7122 * make sure we have the right iocbq before taking it
7123 * off the txcmplq and try to call completion routine.
7124 */
2e0fef85
JS
7125 if (!abort_iocb ||
7126 abort_iocb->iocb.ulpContext != abort_context ||
7127 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7128 spin_unlock_irq(&phba->hbalock);
341af102
JS
7129 else if (phba->sli_rev < LPFC_SLI_REV4) {
7130 /*
7131 * leave the SLI4 aborted command on the txcmplq
7132 * list and the command complete WCQE's XB bit
7133 * will tell whether the SGL (XRI) can be released
7134 * immediately or to the aborted SGL list for the
7135 * following abort XRI from the HBA.
7136 */
92d7f7b0 7137 list_del_init(&abort_iocb->list);
2680eeaa 7138 pring->txcmplq_cnt--;
2e0fef85 7139 spin_unlock_irq(&phba->hbalock);
2680eeaa 7140
0ff10d46
JS
7141 /* Firmware could still be in progress of DMAing
7142 * payload, so don't free data buffer till after
7143 * a hbeat.
7144 */
341af102 7145 spin_lock_irq(&phba->hbalock);
0ff10d46 7146 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 7147 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
7148 spin_unlock_irq(&phba->hbalock);
7149
92d7f7b0 7150 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 7151 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 7152 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
2680eeaa
JS
7153 }
7154 }
7155
604a3e30 7156 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
7157 return;
7158}
7159
e59058c4 7160/**
3621a710 7161 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7162 * @phba: Pointer to HBA context object.
7163 * @cmdiocb: Pointer to driver command iocb object.
7164 * @rspiocb: Pointer to driver response iocb object.
7165 *
7166 * The function is called from SLI ring event handler with no
7167 * lock held. This function is the completion handler for ELS commands
7168 * which are aborted. The function frees memory resources used for
7169 * the aborted ELS commands.
7170 **/
92d7f7b0
JS
7171static void
7172lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7173 struct lpfc_iocbq *rspiocb)
7174{
7175 IOCB_t *irsp = &rspiocb->iocb;
7176
7177 /* ELS cmd tag <ulpIoTag> completes */
7178 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7179 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7180 "x%x x%x x%x\n",
e8b62011 7181 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7182 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7183 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7184 lpfc_ct_free_iocb(phba, cmdiocb);
7185 else
7186 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7187 return;
7188}
7189
e59058c4 7190/**
3621a710 7191 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
e59058c4
JS
7192 * @phba: Pointer to HBA context object.
7193 * @pring: Pointer to driver SLI ring object.
7194 * @cmdiocb: Pointer to driver command iocb object.
7195 *
7196 * This function issues an abort iocb for the provided command
7197 * iocb. This function is called with hbalock held.
7198 * The function returns 0 when it fails due to memory allocation
7199 * failure or when the command iocb is an abort request.
7200 **/
dea3101e 7201int
2e0fef85
JS
7202lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7203 struct lpfc_iocbq *cmdiocb)
dea3101e 7204{
2e0fef85 7205 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7206 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
7207 IOCB_t *icmd = NULL;
7208 IOCB_t *iabt = NULL;
07951076
JS
7209 int retval = IOCB_ERROR;
7210
92d7f7b0
JS
7211 /*
7212 * There are certain command types we don't want to abort. And we
7213 * don't want to abort commands that are already in the process of
7214 * being aborted.
07951076
JS
7215 */
7216 icmd = &cmdiocb->iocb;
2e0fef85 7217 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7218 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7219 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7220 return 0;
7221
858c9f6c
JS
7222 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7223 * callback so that nothing happens when it finishes.
07951076 7224 */
858c9f6c
JS
7225 if ((vport->load_flag & FC_UNLOADING) &&
7226 (pring->ringno == LPFC_ELS_RING)) {
92d7f7b0
JS
7227 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7228 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7229 else
7230 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
07951076 7231 goto abort_iotag_exit;
92d7f7b0 7232 }
dea3101e
JB
7233
7234 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7235 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
7236 if (abtsiocbp == NULL)
7237 return 0;
dea3101e 7238
07951076 7239 /* This signals the response to set the correct status
341af102 7240 * before calling the completion handler
07951076
JS
7241 */
7242 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7243
dea3101e 7244 iabt = &abtsiocbp->iocb;
07951076
JS
7245 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7246 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 7247 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 7248 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
7249 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7250 }
da0436e9
JS
7251 else
7252 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7253 iabt->ulpLe = 1;
7254 iabt->ulpClass = icmd->ulpClass;
dea3101e 7255
5ffc266e
JS
7256 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7257 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
7258 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7259 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7260
2e0fef85 7261 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7262 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7263 else
7264 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7265
07951076 7266 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7267
e8b62011
JS
7268 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7269 "0339 Abort xri x%x, original iotag x%x, "
7270 "abort cmd iotag x%x\n",
7271 iabt->un.acxri.abortContextTag,
7272 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
da0436e9 7273 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7274
d7c255b2
JS
7275 if (retval)
7276 __lpfc_sli_release_iocbq(phba, abtsiocbp);
07951076 7277abort_iotag_exit:
2e0fef85
JS
7278 /*
7279 * Caller to this routine should check for IOCB_ERROR
7280 * and handle it properly. This routine no longer removes
7281 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7282 */
2e0fef85 7283 return retval;
dea3101e
JB
7284}
7285
e59058c4 7286/**
3621a710 7287 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7288 * @iocbq: Pointer to driver iocb object.
7289 * @vport: Pointer to driver virtual port object.
7290 * @tgt_id: SCSI ID of the target.
7291 * @lun_id: LUN ID of the scsi device.
7292 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7293 *
3621a710 7294 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7295 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7296 * 0 if the filtering criteria is met for the given iocb and will return
7297 * 1 if the filtering criteria is not met.
7298 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7299 * given iocb is for the SCSI device specified by vport, tgt_id and
7300 * lun_id parameter.
7301 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7302 * given iocb is for the SCSI target specified by vport and tgt_id
7303 * parameters.
7304 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7305 * given iocb is for the SCSI host associated with the given vport.
7306 * This function is called with no locks held.
7307 **/
dea3101e 7308static int
51ef4c26
JS
7309lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7310 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7311 lpfc_ctx_cmd ctx_cmd)
dea3101e 7312{
0bd4ca25 7313 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
7314 int rc = 1;
7315
0bd4ca25
JSEC
7316 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7317 return rc;
7318
51ef4c26
JS
7319 if (iocbq->vport != vport)
7320 return rc;
7321
0bd4ca25 7322 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7323
495a714c 7324 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
7325 return rc;
7326
7327 switch (ctx_cmd) {
7328 case LPFC_CTX_LUN:
495a714c
JS
7329 if ((lpfc_cmd->rdata->pnode) &&
7330 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7331 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
7332 rc = 0;
7333 break;
7334 case LPFC_CTX_TGT:
495a714c
JS
7335 if ((lpfc_cmd->rdata->pnode) &&
7336 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
7337 rc = 0;
7338 break;
dea3101e
JB
7339 case LPFC_CTX_HOST:
7340 rc = 0;
7341 break;
7342 default:
7343 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7344 __func__, ctx_cmd);
dea3101e
JB
7345 break;
7346 }
7347
7348 return rc;
7349}
7350
e59058c4 7351/**
3621a710 7352 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7353 * @vport: Pointer to virtual port.
7354 * @tgt_id: SCSI ID of the target.
7355 * @lun_id: LUN ID of the scsi device.
7356 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7357 *
7358 * This function returns number of FCP commands pending for the vport.
7359 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7360 * commands pending on the vport associated with SCSI device specified
7361 * by tgt_id and lun_id parameters.
7362 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7363 * commands pending on the vport associated with SCSI target specified
7364 * by tgt_id parameter.
7365 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7366 * commands pending on the vport.
7367 * This function returns the number of iocbs which satisfy the filter.
7368 * This function is called without any lock held.
7369 **/
dea3101e 7370int
51ef4c26
JS
7371lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7372 lpfc_ctx_cmd ctx_cmd)
dea3101e 7373{
51ef4c26 7374 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7375 struct lpfc_iocbq *iocbq;
7376 int sum, i;
dea3101e 7377
0bd4ca25
JSEC
7378 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7379 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7380
51ef4c26
JS
7381 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7382 ctx_cmd) == 0)
0bd4ca25 7383 sum++;
dea3101e 7384 }
0bd4ca25 7385
dea3101e
JB
7386 return sum;
7387}
7388
e59058c4 7389/**
3621a710 7390 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7391 * @phba: Pointer to HBA context object
7392 * @cmdiocb: Pointer to command iocb object.
7393 * @rspiocb: Pointer to response iocb object.
7394 *
7395 * This function is called when an aborted FCP iocb completes. This
7396 * function is called by the ring event handler with no lock held.
7397 * This function frees the iocb.
7398 **/
5eb95af0 7399void
2e0fef85
JS
7400lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7401 struct lpfc_iocbq *rspiocb)
5eb95af0 7402{
604a3e30 7403 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7404 return;
7405}
7406
e59058c4 7407/**
3621a710 7408 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7409 * @vport: Pointer to virtual port.
7410 * @pring: Pointer to driver SLI ring object.
7411 * @tgt_id: SCSI ID of the target.
7412 * @lun_id: LUN ID of the scsi device.
7413 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7414 *
7415 * This function sends an abort command for every SCSI command
7416 * associated with the given virtual port pending on the ring
7417 * filtered by lpfc_sli_validate_fcp_iocb function.
7418 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7419 * FCP iocbs associated with lun specified by tgt_id and lun_id
7420 * parameters
7421 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7422 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7423 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7424 * FCP iocbs associated with virtual port.
7425 * This function returns number of iocbs it failed to abort.
7426 * This function is called with no locks held.
7427 **/
dea3101e 7428int
51ef4c26
JS
7429lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7430 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7431{
51ef4c26 7432 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7433 struct lpfc_iocbq *iocbq;
7434 struct lpfc_iocbq *abtsiocb;
dea3101e 7435 IOCB_t *cmd = NULL;
dea3101e 7436 int errcnt = 0, ret_val = 0;
0bd4ca25 7437 int i;
dea3101e 7438
0bd4ca25
JSEC
7439 for (i = 1; i <= phba->sli.last_iotag; i++) {
7440 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7441
51ef4c26 7442 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7443 abort_cmd) != 0)
dea3101e
JB
7444 continue;
7445
7446 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7447 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
7448 if (abtsiocb == NULL) {
7449 errcnt++;
7450 continue;
7451 }
dea3101e 7452
0bd4ca25 7453 cmd = &iocbq->iocb;
dea3101e
JB
7454 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7455 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7456 if (phba->sli_rev == LPFC_SLI_REV4)
7457 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7458 else
7459 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
7460 abtsiocb->iocb.ulpLe = 1;
7461 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7462 abtsiocb->vport = phba->pport;
dea3101e 7463
5ffc266e
JS
7464 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7465 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
7466 if (iocbq->iocb_flag & LPFC_IO_FCP)
7467 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7468
2e0fef85 7469 if (lpfc_is_link_up(phba))
dea3101e
JB
7470 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7471 else
7472 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7473
5eb95af0
JSEC
7474 /* Setup callback routine and issue the command. */
7475 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7476 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7477 abtsiocb, 0);
dea3101e 7478 if (ret_val == IOCB_ERROR) {
604a3e30 7479 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
7480 errcnt++;
7481 continue;
7482 }
7483 }
7484
7485 return errcnt;
7486}
7487
e59058c4 7488/**
3621a710 7489 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7490 * @phba: Pointer to HBA context object.
7491 * @cmdiocbq: Pointer to command iocb.
7492 * @rspiocbq: Pointer to response iocb.
7493 *
7494 * This function is the completion handler for iocbs issued using
7495 * lpfc_sli_issue_iocb_wait function. This function is called by the
7496 * ring event handler function without any lock held. This function
7497 * can be called from both worker thread context and interrupt
7498 * context. This function also can be called from other thread which
7499 * cleans up the SLI layer objects.
7500 * This function copy the contents of the response iocb to the
7501 * response iocb memory object provided by the caller of
7502 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7503 * sleeps for the iocb completion.
7504 **/
68876920
JSEC
7505static void
7506lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7507 struct lpfc_iocbq *cmdiocbq,
7508 struct lpfc_iocbq *rspiocbq)
dea3101e 7509{
68876920
JSEC
7510 wait_queue_head_t *pdone_q;
7511 unsigned long iflags;
0f65ff68 7512 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7513
2e0fef85 7514 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7515 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7516 if (cmdiocbq->context2 && rspiocbq)
7517 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7518 &rspiocbq->iocb, sizeof(IOCB_t));
7519
0f65ff68
JS
7520 /* Set the exchange busy flag for task management commands */
7521 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7522 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7523 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7524 cur_iocbq);
7525 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7526 }
7527
68876920 7528 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7529 if (pdone_q)
7530 wake_up(pdone_q);
858c9f6c 7531 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
7532 return;
7533}
7534
d11e31dd
JS
7535/**
7536 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7537 * @phba: Pointer to HBA context object..
7538 * @piocbq: Pointer to command iocb.
7539 * @flag: Flag to test.
7540 *
7541 * This routine grabs the hbalock and then test the iocb_flag to
7542 * see if the passed in flag is set.
7543 * Returns:
7544 * 1 if flag is set.
7545 * 0 if flag is not set.
7546 **/
7547static int
7548lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7549 struct lpfc_iocbq *piocbq, uint32_t flag)
7550{
7551 unsigned long iflags;
7552 int ret;
7553
7554 spin_lock_irqsave(&phba->hbalock, iflags);
7555 ret = piocbq->iocb_flag & flag;
7556 spin_unlock_irqrestore(&phba->hbalock, iflags);
7557 return ret;
7558
7559}
7560
e59058c4 7561/**
3621a710 7562 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7563 * @phba: Pointer to HBA context object..
7564 * @pring: Pointer to sli ring.
7565 * @piocb: Pointer to command iocb.
7566 * @prspiocbq: Pointer to response iocb.
7567 * @timeout: Timeout in number of seconds.
7568 *
7569 * This function issues the iocb to firmware and waits for the
7570 * iocb to complete. If the iocb command is not
7571 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7572 * Caller should not free the iocb resources if this function
7573 * returns IOCB_TIMEDOUT.
7574 * The function waits for the iocb completion using an
7575 * non-interruptible wait.
7576 * This function will sleep while waiting for iocb completion.
7577 * So, this function should not be called from any context which
7578 * does not allow sleeping. Due to the same reason, this function
7579 * cannot be called with interrupt disabled.
7580 * This function assumes that the iocb completions occur while
7581 * this function sleep. So, this function cannot be called from
7582 * the thread which process iocb completion for this ring.
7583 * This function clears the iocb_flag of the iocb object before
7584 * issuing the iocb and the iocb completion handler sets this
7585 * flag and wakes this thread when the iocb completes.
7586 * The contents of the response iocb will be copied to prspiocbq
7587 * by the completion handler when the command completes.
7588 * This function returns IOCB_SUCCESS when success.
7589 * This function is called with no lock held.
7590 **/
dea3101e 7591int
2e0fef85 7592lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7593 uint32_t ring_number,
2e0fef85
JS
7594 struct lpfc_iocbq *piocb,
7595 struct lpfc_iocbq *prspiocbq,
68876920 7596 uint32_t timeout)
dea3101e 7597{
7259f0d0 7598 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7599 long timeleft, timeout_req = 0;
7600 int retval = IOCB_SUCCESS;
875fbdfe 7601 uint32_t creg_val;
dea3101e
JB
7602
7603 /*
68876920
JSEC
7604 * If the caller has provided a response iocbq buffer, then context2
7605 * is NULL or its an error.
dea3101e 7606 */
68876920
JSEC
7607 if (prspiocbq) {
7608 if (piocb->context2)
7609 return IOCB_ERROR;
7610 piocb->context2 = prspiocbq;
dea3101e
JB
7611 }
7612
68876920
JSEC
7613 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7614 piocb->context_un.wait_queue = &done_q;
7615 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7616
875fbdfe
JSEC
7617 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7618 creg_val = readl(phba->HCregaddr);
7619 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7620 writel(creg_val, phba->HCregaddr);
7621 readl(phba->HCregaddr); /* flush */
7622 }
7623
da0436e9 7624 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
68876920
JSEC
7625 if (retval == IOCB_SUCCESS) {
7626 timeout_req = timeout * HZ;
68876920 7627 timeleft = wait_event_timeout(done_q,
d11e31dd 7628 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7629 timeout_req);
dea3101e 7630
7054a606
JS
7631 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7632 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7633 "0331 IOCB wake signaled\n");
7054a606 7634 } else if (timeleft == 0) {
68876920 7635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7636 "0338 IOCB wait timeout error - no "
7637 "wake response Data x%x\n", timeout);
68876920 7638 retval = IOCB_TIMEDOUT;
7054a606 7639 } else {
68876920 7640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7641 "0330 IOCB wake NOT set, "
7642 "Data x%x x%lx\n",
68876920
JSEC
7643 timeout, (timeleft / jiffies));
7644 retval = IOCB_TIMEDOUT;
dea3101e 7645 }
68876920
JSEC
7646 } else {
7647 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7648 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7649 retval);
68876920 7650 retval = IOCB_ERROR;
dea3101e
JB
7651 }
7652
875fbdfe
JSEC
7653 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7654 creg_val = readl(phba->HCregaddr);
7655 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7656 writel(creg_val, phba->HCregaddr);
7657 readl(phba->HCregaddr); /* flush */
7658 }
7659
68876920
JSEC
7660 if (prspiocbq)
7661 piocb->context2 = NULL;
7662
7663 piocb->context_un.wait_queue = NULL;
7664 piocb->iocb_cmpl = NULL;
dea3101e
JB
7665 return retval;
7666}
68876920 7667
e59058c4 7668/**
3621a710 7669 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7670 * @phba: Pointer to HBA context object.
7671 * @pmboxq: Pointer to driver mailbox object.
7672 * @timeout: Timeout in number of seconds.
7673 *
7674 * This function issues the mailbox to firmware and waits for the
7675 * mailbox command to complete. If the mailbox command is not
7676 * completed within timeout seconds, it returns MBX_TIMEOUT.
7677 * The function waits for the mailbox completion using an
7678 * interruptible wait. If the thread is woken up due to a
7679 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7680 * should not free the mailbox resources, if this function returns
7681 * MBX_TIMEOUT.
7682 * This function will sleep while waiting for mailbox completion.
7683 * So, this function should not be called from any context which
7684 * does not allow sleeping. Due to the same reason, this function
7685 * cannot be called with interrupt disabled.
7686 * This function assumes that the mailbox completion occurs while
7687 * this function sleep. So, this function cannot be called from
7688 * the worker thread which processes mailbox completion.
7689 * This function is called in the context of HBA management
7690 * applications.
7691 * This function returns MBX_SUCCESS when successful.
7692 * This function is called with no lock held.
7693 **/
dea3101e 7694int
2e0fef85 7695lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
7696 uint32_t timeout)
7697{
7259f0d0 7698 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7699 int retval;
858c9f6c 7700 unsigned long flag;
dea3101e
JB
7701
7702 /* The caller must leave context1 empty. */
98c9ea5c 7703 if (pmboxq->context1)
2e0fef85 7704 return MBX_NOT_FINISHED;
dea3101e 7705
495a714c 7706 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
7707 /* setup wake call as IOCB callback */
7708 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7709 /* setup context field to pass wait_queue pointer to wake function */
7710 pmboxq->context1 = &done_q;
7711
dea3101e
JB
7712 /* now issue the command */
7713 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7714
7715 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7716 wait_event_interruptible_timeout(done_q,
7717 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7718 timeout * HZ);
7719
858c9f6c 7720 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7721 pmboxq->context1 = NULL;
7054a606
JS
7722 /*
7723 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7724 * else do not free the resources.
7725 */
7726 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea3101e 7727 retval = MBX_SUCCESS;
858c9f6c 7728 else {
7054a606 7729 retval = MBX_TIMEOUT;
858c9f6c
JS
7730 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7731 }
7732 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e
JB
7733 }
7734
dea3101e
JB
7735 return retval;
7736}
7737
e59058c4 7738/**
3772a991 7739 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7740 * @phba: Pointer to HBA context.
7741 *
3772a991
JS
7742 * This function is called to shutdown the driver's mailbox sub-system.
7743 * It first marks the mailbox sub-system is in a block state to prevent
7744 * the asynchronous mailbox command from issued off the pending mailbox
7745 * command queue. If the mailbox command sub-system shutdown is due to
7746 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7747 * the mailbox sub-system flush routine to forcefully bring down the
7748 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7749 * as with offline or HBA function reset), this routine will wait for the
7750 * outstanding mailbox command to complete before invoking the mailbox
7751 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7752 **/
3772a991
JS
7753void
7754lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7755{
3772a991
JS
7756 struct lpfc_sli *psli = &phba->sli;
7757 uint8_t actcmd = MBX_HEARTBEAT;
7758 unsigned long timeout;
b4c02652 7759
3772a991
JS
7760 spin_lock_irq(&phba->hbalock);
7761 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7762 spin_unlock_irq(&phba->hbalock);
b4c02652 7763
3772a991 7764 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7765 spin_lock_irq(&phba->hbalock);
3772a991
JS
7766 if (phba->sli.mbox_active)
7767 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7768 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7769 /* Determine how long we might wait for the active mailbox
7770 * command to be gracefully completed by firmware.
7771 */
7772 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7773 1000) + jiffies;
7774 while (phba->sli.mbox_active) {
7775 /* Check active mailbox complete status every 2ms */
7776 msleep(2);
7777 if (time_after(jiffies, timeout))
7778 /* Timeout, let the mailbox flush routine to
7779 * forcefully release active mailbox command
7780 */
7781 break;
7782 }
7783 }
7784 lpfc_sli_mbox_sys_flush(phba);
7785}
ed957684 7786
3772a991
JS
7787/**
7788 * lpfc_sli_eratt_read - read sli-3 error attention events
7789 * @phba: Pointer to HBA context.
7790 *
7791 * This function is called to read the SLI3 device error attention registers
7792 * for possible error attention events. The caller must hold the hostlock
7793 * with spin_lock_irq().
7794 *
7795 * This fucntion returns 1 when there is Error Attention in the Host Attention
7796 * Register and returns 0 otherwise.
7797 **/
7798static int
7799lpfc_sli_eratt_read(struct lpfc_hba *phba)
7800{
7801 uint32_t ha_copy;
b4c02652 7802
3772a991
JS
7803 /* Read chip Host Attention (HA) register */
7804 ha_copy = readl(phba->HAregaddr);
7805 if (ha_copy & HA_ERATT) {
7806 /* Read host status register to retrieve error event */
7807 lpfc_sli_read_hs(phba);
b4c02652 7808
3772a991
JS
7809 /* Check if there is a deferred error condition is active */
7810 if ((HS_FFER1 & phba->work_hs) &&
7811 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7812 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
3772a991 7813 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
7814 /* Clear all interrupt enable conditions */
7815 writel(0, phba->HCregaddr);
7816 readl(phba->HCregaddr);
7817 }
7818
7819 /* Set the driver HA work bitmap */
3772a991
JS
7820 phba->work_ha |= HA_ERATT;
7821 /* Indicate polling handles this ERATT */
7822 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
7823 return 1;
7824 }
7825 return 0;
b4c02652
JS
7826}
7827
da0436e9
JS
7828/**
7829 * lpfc_sli4_eratt_read - read sli-4 error attention events
7830 * @phba: Pointer to HBA context.
7831 *
7832 * This function is called to read the SLI4 device error attention registers
7833 * for possible error attention events. The caller must hold the hostlock
7834 * with spin_lock_irq().
7835 *
7836 * This fucntion returns 1 when there is Error Attention in the Host Attention
7837 * Register and returns 0 otherwise.
7838 **/
7839static int
7840lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7841{
7842 uint32_t uerr_sta_hi, uerr_sta_lo;
da0436e9
JS
7843
7844 /* For now, use the SLI4 device internal unrecoverable error
7845 * registers for error attention. This can be changed later.
7846 */
a747c9ce
JS
7847 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7848 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7849 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7850 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7852 "1423 HBA Unrecoverable error: "
7853 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7854 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7855 uerr_sta_lo, uerr_sta_hi,
7856 phba->sli4_hba.ue_mask_lo,
7857 phba->sli4_hba.ue_mask_hi);
7858 phba->work_status[0] = uerr_sta_lo;
7859 phba->work_status[1] = uerr_sta_hi;
7860 /* Set the driver HA work bitmap */
7861 phba->work_ha |= HA_ERATT;
7862 /* Indicate polling handles this ERATT */
7863 phba->hba_flag |= HBA_ERATT_HANDLED;
7864 return 1;
da0436e9
JS
7865 }
7866 return 0;
7867}
7868
e59058c4 7869/**
3621a710 7870 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
7871 * @phba: Pointer to HBA context.
7872 *
3772a991 7873 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
7874 * error attention register bit for error attention events.
7875 *
7876 * This fucntion returns 1 when there is Error Attention in the Host Attention
7877 * Register and returns 0 otherwise.
7878 **/
7879int
7880lpfc_sli_check_eratt(struct lpfc_hba *phba)
7881{
7882 uint32_t ha_copy;
7883
7884 /* If somebody is waiting to handle an eratt, don't process it
7885 * here. The brdkill function will do this.
7886 */
7887 if (phba->link_flag & LS_IGNORE_ERATT)
7888 return 0;
7889
7890 /* Check if interrupt handler handles this ERATT */
7891 spin_lock_irq(&phba->hbalock);
7892 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7893 /* Interrupt handler has handled ERATT */
7894 spin_unlock_irq(&phba->hbalock);
7895 return 0;
7896 }
7897
a257bf90
JS
7898 /*
7899 * If there is deferred error attention, do not check for error
7900 * attention
7901 */
7902 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7903 spin_unlock_irq(&phba->hbalock);
7904 return 0;
7905 }
7906
3772a991
JS
7907 /* If PCI channel is offline, don't process it */
7908 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 7909 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7910 return 0;
7911 }
7912
7913 switch (phba->sli_rev) {
7914 case LPFC_SLI_REV2:
7915 case LPFC_SLI_REV3:
7916 /* Read chip Host Attention (HA) register */
7917 ha_copy = lpfc_sli_eratt_read(phba);
7918 break;
da0436e9
JS
7919 case LPFC_SLI_REV4:
7920 /* Read devcie Uncoverable Error (UERR) registers */
7921 ha_copy = lpfc_sli4_eratt_read(phba);
7922 break;
3772a991
JS
7923 default:
7924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7925 "0299 Invalid SLI revision (%d)\n",
7926 phba->sli_rev);
7927 ha_copy = 0;
7928 break;
9399627f
JS
7929 }
7930 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7931
7932 return ha_copy;
7933}
7934
7935/**
7936 * lpfc_intr_state_check - Check device state for interrupt handling
7937 * @phba: Pointer to HBA context.
7938 *
7939 * This inline routine checks whether a device or its PCI slot is in a state
7940 * that the interrupt should be handled.
7941 *
7942 * This function returns 0 if the device or the PCI slot is in a state that
7943 * interrupt should be handled, otherwise -EIO.
7944 */
7945static inline int
7946lpfc_intr_state_check(struct lpfc_hba *phba)
7947{
7948 /* If the pci channel is offline, ignore all the interrupts */
7949 if (unlikely(pci_channel_offline(phba->pcidev)))
7950 return -EIO;
7951
7952 /* Update device level interrupt statistics */
7953 phba->sli.slistat.sli_intr++;
7954
7955 /* Ignore all interrupts during initialization. */
7956 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7957 return -EIO;
7958
9399627f
JS
7959 return 0;
7960}
7961
7962/**
3772a991 7963 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
7964 * @irq: Interrupt number.
7965 * @dev_id: The device context pointer.
7966 *
9399627f 7967 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
7968 * service routine when device with SLI-3 interface spec is enabled with
7969 * MSI-X multi-message interrupt mode and there are slow-path events in
7970 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7971 * interrupt mode, this function is called as part of the device-level
7972 * interrupt handler. When the PCI slot is in error recovery or the HBA
7973 * is undergoing initialization, the interrupt handler will not process
7974 * the interrupt. The link attention and ELS ring attention events are
7975 * handled by the worker thread. The interrupt handler signals the worker
7976 * thread and returns for these events. This function is called without
7977 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
7978 * structures.
7979 *
7980 * This function returns IRQ_HANDLED when interrupt is handled else it
7981 * returns IRQ_NONE.
e59058c4 7982 **/
dea3101e 7983irqreturn_t
3772a991 7984lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 7985{
2e0fef85 7986 struct lpfc_hba *phba;
a747c9ce 7987 uint32_t ha_copy, hc_copy;
dea3101e
JB
7988 uint32_t work_ha_copy;
7989 unsigned long status;
5b75da2f 7990 unsigned long iflag;
dea3101e
JB
7991 uint32_t control;
7992
92d7f7b0 7993 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
7994 struct lpfc_vport *vport;
7995 struct lpfc_nodelist *ndlp;
7996 struct lpfc_dmabuf *mp;
92d7f7b0
JS
7997 LPFC_MBOXQ_t *pmb;
7998 int rc;
7999
dea3101e
JB
8000 /*
8001 * Get the driver's phba structure from the dev_id and
8002 * assume the HBA is not interrupting.
8003 */
9399627f 8004 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
8005
8006 if (unlikely(!phba))
8007 return IRQ_NONE;
8008
dea3101e 8009 /*
9399627f
JS
8010 * Stuff needs to be attented to when this function is invoked as an
8011 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 8012 */
9399627f 8013 if (phba->intr_type == MSIX) {
3772a991
JS
8014 /* Check device state for handling interrupt */
8015 if (lpfc_intr_state_check(phba))
9399627f
JS
8016 return IRQ_NONE;
8017 /* Need to read HA REG for slow-path events */
5b75da2f 8018 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 8019 ha_copy = readl(phba->HAregaddr);
9399627f
JS
8020 /* If somebody is waiting to handle an eratt don't process it
8021 * here. The brdkill function will do this.
8022 */
8023 if (phba->link_flag & LS_IGNORE_ERATT)
8024 ha_copy &= ~HA_ERATT;
8025 /* Check the need for handling ERATT in interrupt handler */
8026 if (ha_copy & HA_ERATT) {
8027 if (phba->hba_flag & HBA_ERATT_HANDLED)
8028 /* ERATT polling has handled ERATT */
8029 ha_copy &= ~HA_ERATT;
8030 else
8031 /* Indicate interrupt handler handles ERATT */
8032 phba->hba_flag |= HBA_ERATT_HANDLED;
8033 }
a257bf90
JS
8034
8035 /*
8036 * If there is deferred error attention, do not check for any
8037 * interrupt.
8038 */
8039 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8040 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8041 return IRQ_NONE;
8042 }
8043
9399627f 8044 /* Clear up only attention source related to slow-path */
a747c9ce
JS
8045 hc_copy = readl(phba->HCregaddr);
8046 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8047 HC_LAINT_ENA | HC_ERINT_ENA),
8048 phba->HCregaddr);
9399627f
JS
8049 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8050 phba->HAregaddr);
a747c9ce 8051 writel(hc_copy, phba->HCregaddr);
9399627f 8052 readl(phba->HAregaddr); /* flush */
5b75da2f 8053 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8054 } else
8055 ha_copy = phba->ha_copy;
dea3101e 8056
dea3101e
JB
8057 work_ha_copy = ha_copy & phba->work_ha_mask;
8058
9399627f 8059 if (work_ha_copy) {
dea3101e
JB
8060 if (work_ha_copy & HA_LATT) {
8061 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8062 /*
8063 * Turn off Link Attention interrupts
8064 * until CLEAR_LA done
8065 */
5b75da2f 8066 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
8067 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8068 control = readl(phba->HCregaddr);
8069 control &= ~HC_LAINT_ENA;
8070 writel(control, phba->HCregaddr);
8071 readl(phba->HCregaddr); /* flush */
5b75da2f 8072 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
8073 }
8074 else
8075 work_ha_copy &= ~HA_LATT;
8076 }
8077
9399627f 8078 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
8079 /*
8080 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8081 * the only slow ring.
8082 */
8083 status = (work_ha_copy &
8084 (HA_RXMASK << (4*LPFC_ELS_RING)));
8085 status >>= (4*LPFC_ELS_RING);
8086 if (status & HA_RXMASK) {
5b75da2f 8087 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 8088 control = readl(phba->HCregaddr);
a58cbd52
JS
8089
8090 lpfc_debugfs_slow_ring_trc(phba,
8091 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8092 control, status,
8093 (uint32_t)phba->sli.slistat.sli_intr);
8094
858c9f6c 8095 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
8096 lpfc_debugfs_slow_ring_trc(phba,
8097 "ISR Disable ring:"
8098 "pwork:x%x hawork:x%x wait:x%x",
8099 phba->work_ha, work_ha_copy,
8100 (uint32_t)((unsigned long)
5e9d9b82 8101 &phba->work_waitq));
a58cbd52 8102
858c9f6c
JS
8103 control &=
8104 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
8105 writel(control, phba->HCregaddr);
8106 readl(phba->HCregaddr); /* flush */
dea3101e 8107 }
a58cbd52
JS
8108 else {
8109 lpfc_debugfs_slow_ring_trc(phba,
8110 "ISR slow ring: pwork:"
8111 "x%x hawork:x%x wait:x%x",
8112 phba->work_ha, work_ha_copy,
8113 (uint32_t)((unsigned long)
5e9d9b82 8114 &phba->work_waitq));
a58cbd52 8115 }
5b75da2f 8116 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
8117 }
8118 }
5b75da2f 8119 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8120 if (work_ha_copy & HA_ERATT) {
9399627f 8121 lpfc_sli_read_hs(phba);
a257bf90
JS
8122 /*
8123 * Check if there is a deferred error condition
8124 * is active
8125 */
8126 if ((HS_FFER1 & phba->work_hs) &&
8127 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8128 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8129 phba->hba_flag |= DEFER_ERATT;
8130 /* Clear all interrupt enable conditions */
8131 writel(0, phba->HCregaddr);
8132 readl(phba->HCregaddr);
8133 }
8134 }
8135
9399627f 8136 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8137 pmb = phba->sli.mbox_active;
04c68496 8138 pmbox = &pmb->u.mb;
34b02dcd 8139 mbox = phba->mbox;
858c9f6c 8140 vport = pmb->vport;
92d7f7b0
JS
8141
8142 /* First check out the status word */
8143 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8144 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8145 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8146 /*
8147 * Stray Mailbox Interrupt, mbxCommand <cmd>
8148 * mbxStatus <status>
8149 */
09372820 8150 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8151 LOG_SLI,
e8b62011 8152 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8153 "Interrupt mbxCommand x%x "
8154 "mbxStatus x%x\n",
e8b62011 8155 (vport ? vport->vpi : 0),
92d7f7b0
JS
8156 pmbox->mbxCommand,
8157 pmbox->mbxStatus);
09372820
JS
8158 /* clear mailbox attention bit */
8159 work_ha_copy &= ~HA_MBATT;
8160 } else {
97eab634 8161 phba->sli.mbox_active = NULL;
5b75da2f 8162 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8163 phba->last_completion_time = jiffies;
8164 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8165 if (pmb->mbox_cmpl) {
8166 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8167 MAILBOX_CMD_SIZE);
7a470277
JS
8168 if (pmb->out_ext_byte_len &&
8169 pmb->context2)
8170 lpfc_sli_pcimem_bcopy(
8171 phba->mbox_ext,
8172 pmb->context2,
8173 pmb->out_ext_byte_len);
09372820
JS
8174 }
8175 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8176 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8177
8178 lpfc_debugfs_disc_trc(vport,
8179 LPFC_DISC_TRC_MBOX_VPORT,
8180 "MBOX dflt rpi: : "
8181 "status:x%x rpi:x%x",
8182 (uint32_t)pmbox->mbxStatus,
8183 pmbox->un.varWords[0], 0);
8184
8185 if (!pmbox->mbxStatus) {
8186 mp = (struct lpfc_dmabuf *)
8187 (pmb->context1);
8188 ndlp = (struct lpfc_nodelist *)
8189 pmb->context2;
8190
8191 /* Reg_LOGIN of dflt RPI was
8192 * successful. new lets get
8193 * rid of the RPI using the
8194 * same mbox buffer.
8195 */
8196 lpfc_unreg_login(phba,
8197 vport->vpi,
8198 pmbox->un.varWords[0],
8199 pmb);
8200 pmb->mbox_cmpl =
8201 lpfc_mbx_cmpl_dflt_rpi;
8202 pmb->context1 = mp;
8203 pmb->context2 = ndlp;
8204 pmb->vport = vport;
58da1ffb
JS
8205 rc = lpfc_sli_issue_mbox(phba,
8206 pmb,
8207 MBX_NOWAIT);
8208 if (rc != MBX_BUSY)
8209 lpfc_printf_log(phba,
8210 KERN_ERR,
8211 LOG_MBOX | LOG_SLI,
d7c255b2 8212 "0350 rc should have"
6a9c52cf 8213 "been MBX_BUSY\n");
3772a991
JS
8214 if (rc != MBX_NOT_FINISHED)
8215 goto send_current_mbox;
09372820 8216 }
858c9f6c 8217 }
5b75da2f
JS
8218 spin_lock_irqsave(
8219 &phba->pport->work_port_lock,
8220 iflag);
09372820
JS
8221 phba->pport->work_port_events &=
8222 ~WORKER_MBOX_TMO;
5b75da2f
JS
8223 spin_unlock_irqrestore(
8224 &phba->pport->work_port_lock,
8225 iflag);
09372820 8226 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8227 }
97eab634 8228 } else
5b75da2f 8229 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8230
92d7f7b0
JS
8231 if ((work_ha_copy & HA_MBATT) &&
8232 (phba->sli.mbox_active == NULL)) {
858c9f6c 8233send_current_mbox:
92d7f7b0 8234 /* Process next mailbox command if there is one */
58da1ffb
JS
8235 do {
8236 rc = lpfc_sli_issue_mbox(phba, NULL,
8237 MBX_NOWAIT);
8238 } while (rc == MBX_NOT_FINISHED);
8239 if (rc != MBX_SUCCESS)
8240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8241 LOG_SLI, "0349 rc should be "
6a9c52cf 8242 "MBX_SUCCESS\n");
92d7f7b0
JS
8243 }
8244
5b75da2f 8245 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8246 phba->work_ha |= work_ha_copy;
5b75da2f 8247 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8248 lpfc_worker_wake_up(phba);
dea3101e 8249 }
9399627f 8250 return IRQ_HANDLED;
dea3101e 8251
3772a991 8252} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8253
8254/**
3772a991 8255 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8256 * @irq: Interrupt number.
8257 * @dev_id: The device context pointer.
8258 *
8259 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8260 * service routine when device with SLI-3 interface spec is enabled with
8261 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8262 * ring event in the HBA. However, when the device is enabled with either
8263 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8264 * device-level interrupt handler. When the PCI slot is in error recovery
8265 * or the HBA is undergoing initialization, the interrupt handler will not
8266 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8267 * the intrrupt context. This function is called without any lock held.
8268 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8269 *
8270 * This function returns IRQ_HANDLED when interrupt is handled else it
8271 * returns IRQ_NONE.
8272 **/
8273irqreturn_t
3772a991 8274lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8275{
8276 struct lpfc_hba *phba;
8277 uint32_t ha_copy;
8278 unsigned long status;
5b75da2f 8279 unsigned long iflag;
9399627f
JS
8280
8281 /* Get the driver's phba structure from the dev_id and
8282 * assume the HBA is not interrupting.
8283 */
8284 phba = (struct lpfc_hba *) dev_id;
8285
8286 if (unlikely(!phba))
8287 return IRQ_NONE;
8288
8289 /*
8290 * Stuff needs to be attented to when this function is invoked as an
8291 * individual interrupt handler in MSI-X multi-message interrupt mode
8292 */
8293 if (phba->intr_type == MSIX) {
3772a991
JS
8294 /* Check device state for handling interrupt */
8295 if (lpfc_intr_state_check(phba))
9399627f
JS
8296 return IRQ_NONE;
8297 /* Need to read HA REG for FCP ring and other ring events */
8298 ha_copy = readl(phba->HAregaddr);
8299 /* Clear up only attention source related to fast-path */
5b75da2f 8300 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8301 /*
8302 * If there is deferred error attention, do not check for
8303 * any interrupt.
8304 */
8305 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8306 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8307 return IRQ_NONE;
8308 }
9399627f
JS
8309 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8310 phba->HAregaddr);
8311 readl(phba->HAregaddr); /* flush */
5b75da2f 8312 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8313 } else
8314 ha_copy = phba->ha_copy;
dea3101e
JB
8315
8316 /*
9399627f 8317 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8318 */
9399627f
JS
8319 ha_copy &= ~(phba->work_ha_mask);
8320
8321 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8322 status >>= (4*LPFC_FCP_RING);
858c9f6c 8323 if (status & HA_RXMASK)
dea3101e
JB
8324 lpfc_sli_handle_fast_ring_event(phba,
8325 &phba->sli.ring[LPFC_FCP_RING],
8326 status);
a4bc3379
JS
8327
8328 if (phba->cfg_multi_ring_support == 2) {
8329 /*
9399627f
JS
8330 * Process all events on extra ring. Take the optimized path
8331 * for extra ring IO.
a4bc3379 8332 */
9399627f 8333 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8334 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8335 if (status & HA_RXMASK) {
a4bc3379
JS
8336 lpfc_sli_handle_fast_ring_event(phba,
8337 &phba->sli.ring[LPFC_EXTRA_RING],
8338 status);
8339 }
8340 }
dea3101e 8341 return IRQ_HANDLED;
3772a991 8342} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8343
8344/**
3772a991 8345 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8346 * @irq: Interrupt number.
8347 * @dev_id: The device context pointer.
8348 *
3772a991
JS
8349 * This function is the HBA device-level interrupt handler to device with
8350 * SLI-3 interface spec, called from the PCI layer when either MSI or
8351 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8352 * requires driver attention. This function invokes the slow-path interrupt
8353 * attention handling function and fast-path interrupt attention handling
8354 * function in turn to process the relevant HBA attention events. This
8355 * function is called without any lock held. It gets the hbalock to access
8356 * and update SLI data structures.
9399627f
JS
8357 *
8358 * This function returns IRQ_HANDLED when interrupt is handled, else it
8359 * returns IRQ_NONE.
8360 **/
8361irqreturn_t
3772a991 8362lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8363{
8364 struct lpfc_hba *phba;
8365 irqreturn_t sp_irq_rc, fp_irq_rc;
8366 unsigned long status1, status2;
a747c9ce 8367 uint32_t hc_copy;
9399627f
JS
8368
8369 /*
8370 * Get the driver's phba structure from the dev_id and
8371 * assume the HBA is not interrupting.
8372 */
8373 phba = (struct lpfc_hba *) dev_id;
8374
8375 if (unlikely(!phba))
8376 return IRQ_NONE;
8377
3772a991
JS
8378 /* Check device state for handling interrupt */
8379 if (lpfc_intr_state_check(phba))
9399627f
JS
8380 return IRQ_NONE;
8381
8382 spin_lock(&phba->hbalock);
8383 phba->ha_copy = readl(phba->HAregaddr);
8384 if (unlikely(!phba->ha_copy)) {
8385 spin_unlock(&phba->hbalock);
8386 return IRQ_NONE;
8387 } else if (phba->ha_copy & HA_ERATT) {
8388 if (phba->hba_flag & HBA_ERATT_HANDLED)
8389 /* ERATT polling has handled ERATT */
8390 phba->ha_copy &= ~HA_ERATT;
8391 else
8392 /* Indicate interrupt handler handles ERATT */
8393 phba->hba_flag |= HBA_ERATT_HANDLED;
8394 }
8395
a257bf90
JS
8396 /*
8397 * If there is deferred error attention, do not check for any interrupt.
8398 */
8399 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8400 spin_unlock_irq(&phba->hbalock);
8401 return IRQ_NONE;
8402 }
8403
9399627f 8404 /* Clear attention sources except link and error attentions */
a747c9ce
JS
8405 hc_copy = readl(phba->HCregaddr);
8406 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8407 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8408 phba->HCregaddr);
9399627f 8409 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 8410 writel(hc_copy, phba->HCregaddr);
9399627f
JS
8411 readl(phba->HAregaddr); /* flush */
8412 spin_unlock(&phba->hbalock);
8413
8414 /*
8415 * Invokes slow-path host attention interrupt handling as appropriate.
8416 */
8417
8418 /* status of events with mailbox and link attention */
8419 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8420
8421 /* status of events with ELS ring */
8422 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8423 status2 >>= (4*LPFC_ELS_RING);
8424
8425 if (status1 || (status2 & HA_RXMASK))
3772a991 8426 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8427 else
8428 sp_irq_rc = IRQ_NONE;
8429
8430 /*
8431 * Invoke fast-path host attention interrupt handling as appropriate.
8432 */
8433
8434 /* status of events with FCP ring */
8435 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8436 status1 >>= (4*LPFC_FCP_RING);
8437
8438 /* status of events with extra ring */
8439 if (phba->cfg_multi_ring_support == 2) {
8440 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8441 status2 >>= (4*LPFC_EXTRA_RING);
8442 } else
8443 status2 = 0;
8444
8445 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8446 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8447 else
8448 fp_irq_rc = IRQ_NONE;
dea3101e 8449
9399627f
JS
8450 /* Return device-level interrupt handling status */
8451 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8452} /* lpfc_sli_intr_handler */
4f774513
JS
8453
8454/**
8455 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8456 * @phba: pointer to lpfc hba data structure.
8457 *
8458 * This routine is invoked by the worker thread to process all the pending
8459 * SLI4 FCP abort XRI events.
8460 **/
8461void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8462{
8463 struct lpfc_cq_event *cq_event;
8464
8465 /* First, declare the fcp xri abort event has been handled */
8466 spin_lock_irq(&phba->hbalock);
8467 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8468 spin_unlock_irq(&phba->hbalock);
8469 /* Now, handle all the fcp xri abort events */
8470 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8471 /* Get the first event from the head of the event queue */
8472 spin_lock_irq(&phba->hbalock);
8473 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8474 cq_event, struct lpfc_cq_event, list);
8475 spin_unlock_irq(&phba->hbalock);
8476 /* Notify aborted XRI for FCP work queue */
8477 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8478 /* Free the event processed back to the free pool */
8479 lpfc_sli4_cq_event_release(phba, cq_event);
8480 }
8481}
8482
8483/**
8484 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8485 * @phba: pointer to lpfc hba data structure.
8486 *
8487 * This routine is invoked by the worker thread to process all the pending
8488 * SLI4 els abort xri events.
8489 **/
8490void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8491{
8492 struct lpfc_cq_event *cq_event;
8493
8494 /* First, declare the els xri abort event has been handled */
8495 spin_lock_irq(&phba->hbalock);
8496 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8497 spin_unlock_irq(&phba->hbalock);
8498 /* Now, handle all the els xri abort events */
8499 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8500 /* Get the first event from the head of the event queue */
8501 spin_lock_irq(&phba->hbalock);
8502 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8503 cq_event, struct lpfc_cq_event, list);
8504 spin_unlock_irq(&phba->hbalock);
8505 /* Notify aborted XRI for ELS work queue */
8506 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8507 /* Free the event processed back to the free pool */
8508 lpfc_sli4_cq_event_release(phba, cq_event);
8509 }
8510}
8511
341af102
JS
8512/**
8513 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8514 * @phba: pointer to lpfc hba data structure
8515 * @pIocbIn: pointer to the rspiocbq
8516 * @pIocbOut: pointer to the cmdiocbq
8517 * @wcqe: pointer to the complete wcqe
8518 *
8519 * This routine transfers the fields of a command iocbq to a response iocbq
8520 * by copying all the IOCB fields from command iocbq and transferring the
8521 * completion status information from the complete wcqe.
8522 **/
4f774513 8523static void
341af102
JS
8524lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8525 struct lpfc_iocbq *pIocbIn,
4f774513
JS
8526 struct lpfc_iocbq *pIocbOut,
8527 struct lpfc_wcqe_complete *wcqe)
8528{
341af102 8529 unsigned long iflags;
4f774513
JS
8530 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8531
8532 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8533 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
8534 /* Map WCQE parameters into irspiocb parameters */
8535 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8536 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8537 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8538 pIocbIn->iocb.un.fcpi.fcpi_parm =
8539 pIocbOut->iocb.un.fcpi.fcpi_parm -
8540 wcqe->total_data_placed;
8541 else
8542 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 8543 else {
4f774513 8544 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
8545 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8546 }
341af102
JS
8547
8548 /* Pick up HBA exchange busy condition */
8549 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8550 spin_lock_irqsave(&phba->hbalock, iflags);
8551 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8552 spin_unlock_irqrestore(&phba->hbalock, iflags);
8553 }
4f774513
JS
8554}
8555
45ed1190
JS
8556/**
8557 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8558 * @phba: Pointer to HBA context object.
8559 * @wcqe: Pointer to work-queue completion queue entry.
8560 *
8561 * This routine handles an ELS work-queue completion event and construct
8562 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8563 * discovery engine to handle.
8564 *
8565 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8566 **/
8567static struct lpfc_iocbq *
8568lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8569 struct lpfc_iocbq *irspiocbq)
8570{
8571 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8572 struct lpfc_iocbq *cmdiocbq;
8573 struct lpfc_wcqe_complete *wcqe;
8574 unsigned long iflags;
8575
8576 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8577 spin_lock_irqsave(&phba->hbalock, iflags);
8578 pring->stats.iocb_event++;
8579 /* Look up the ELS command IOCB and create pseudo response IOCB */
8580 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8581 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8582 spin_unlock_irqrestore(&phba->hbalock, iflags);
8583
8584 if (unlikely(!cmdiocbq)) {
8585 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8586 "0386 ELS complete with no corresponding "
8587 "cmdiocb: iotag (%d)\n",
8588 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8589 lpfc_sli_release_iocbq(phba, irspiocbq);
8590 return NULL;
8591 }
8592
8593 /* Fake the irspiocbq and copy necessary response information */
341af102 8594 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
8595
8596 return irspiocbq;
8597}
8598
04c68496
JS
8599/**
8600 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8601 * @phba: Pointer to HBA context object.
8602 * @cqe: Pointer to mailbox completion queue entry.
8603 *
8604 * This routine process a mailbox completion queue entry with asynchrous
8605 * event.
8606 *
8607 * Return: true if work posted to worker thread, otherwise false.
8608 **/
8609static bool
8610lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8611{
8612 struct lpfc_cq_event *cq_event;
8613 unsigned long iflags;
8614
8615 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8616 "0392 Async Event: word0:x%x, word1:x%x, "
8617 "word2:x%x, word3:x%x\n", mcqe->word0,
8618 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8619
8620 /* Allocate a new internal CQ_EVENT entry */
8621 cq_event = lpfc_sli4_cq_event_alloc(phba);
8622 if (!cq_event) {
8623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8624 "0394 Failed to allocate CQ_EVENT entry\n");
8625 return false;
8626 }
8627
8628 /* Move the CQE into an asynchronous event entry */
8629 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8630 spin_lock_irqsave(&phba->hbalock, iflags);
8631 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8632 /* Set the async event flag */
8633 phba->hba_flag |= ASYNC_EVENT;
8634 spin_unlock_irqrestore(&phba->hbalock, iflags);
8635
8636 return true;
8637}
8638
8639/**
8640 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8641 * @phba: Pointer to HBA context object.
8642 * @cqe: Pointer to mailbox completion queue entry.
8643 *
8644 * This routine process a mailbox completion queue entry with mailbox
8645 * completion event.
8646 *
8647 * Return: true if work posted to worker thread, otherwise false.
8648 **/
8649static bool
8650lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8651{
8652 uint32_t mcqe_status;
8653 MAILBOX_t *mbox, *pmbox;
8654 struct lpfc_mqe *mqe;
8655 struct lpfc_vport *vport;
8656 struct lpfc_nodelist *ndlp;
8657 struct lpfc_dmabuf *mp;
8658 unsigned long iflags;
8659 LPFC_MBOXQ_t *pmb;
8660 bool workposted = false;
8661 int rc;
8662
8663 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8664 if (!bf_get(lpfc_trailer_completed, mcqe))
8665 goto out_no_mqe_complete;
8666
8667 /* Get the reference to the active mbox command */
8668 spin_lock_irqsave(&phba->hbalock, iflags);
8669 pmb = phba->sli.mbox_active;
8670 if (unlikely(!pmb)) {
8671 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8672 "1832 No pending MBOX command to handle\n");
8673 spin_unlock_irqrestore(&phba->hbalock, iflags);
8674 goto out_no_mqe_complete;
8675 }
8676 spin_unlock_irqrestore(&phba->hbalock, iflags);
8677 mqe = &pmb->u.mqe;
8678 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8679 mbox = phba->mbox;
8680 vport = pmb->vport;
8681
8682 /* Reset heartbeat timer */
8683 phba->last_completion_time = jiffies;
8684 del_timer(&phba->sli.mbox_tmo);
8685
8686 /* Move mbox data to caller's mailbox region, do endian swapping */
8687 if (pmb->mbox_cmpl && mbox)
8688 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8689 /* Set the mailbox status with SLI4 range 0x4000 */
8690 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8691 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8692 bf_set(lpfc_mqe_status, mqe,
8693 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8694
8695 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8696 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8697 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8698 "MBOX dflt rpi: status:x%x rpi:x%x",
8699 mcqe_status,
8700 pmbox->un.varWords[0], 0);
8701 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8702 mp = (struct lpfc_dmabuf *)(pmb->context1);
8703 ndlp = (struct lpfc_nodelist *)pmb->context2;
8704 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8705 * RID of the PPI using the same mbox buffer.
8706 */
8707 lpfc_unreg_login(phba, vport->vpi,
8708 pmbox->un.varWords[0], pmb);
8709 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8710 pmb->context1 = mp;
8711 pmb->context2 = ndlp;
8712 pmb->vport = vport;
8713 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8714 if (rc != MBX_BUSY)
8715 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8716 LOG_SLI, "0385 rc should "
8717 "have been MBX_BUSY\n");
8718 if (rc != MBX_NOT_FINISHED)
8719 goto send_current_mbox;
8720 }
8721 }
8722 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8723 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8724 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8725
8726 /* There is mailbox completion work to do */
8727 spin_lock_irqsave(&phba->hbalock, iflags);
8728 __lpfc_mbox_cmpl_put(phba, pmb);
8729 phba->work_ha |= HA_MBATT;
8730 spin_unlock_irqrestore(&phba->hbalock, iflags);
8731 workposted = true;
8732
8733send_current_mbox:
8734 spin_lock_irqsave(&phba->hbalock, iflags);
8735 /* Release the mailbox command posting token */
8736 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8737 /* Setting active mailbox pointer need to be in sync to flag clear */
8738 phba->sli.mbox_active = NULL;
8739 spin_unlock_irqrestore(&phba->hbalock, iflags);
8740 /* Wake up worker thread to post the next pending mailbox command */
8741 lpfc_worker_wake_up(phba);
8742out_no_mqe_complete:
8743 if (bf_get(lpfc_trailer_consumed, mcqe))
8744 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8745 return workposted;
8746}
8747
8748/**
8749 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8750 * @phba: Pointer to HBA context object.
8751 * @cqe: Pointer to mailbox completion queue entry.
8752 *
8753 * This routine process a mailbox completion queue entry, it invokes the
8754 * proper mailbox complete handling or asynchrous event handling routine
8755 * according to the MCQE's async bit.
8756 *
8757 * Return: true if work posted to worker thread, otherwise false.
8758 **/
8759static bool
8760lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8761{
8762 struct lpfc_mcqe mcqe;
8763 bool workposted;
8764
8765 /* Copy the mailbox MCQE and convert endian order as needed */
8766 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8767
8768 /* Invoke the proper event handling routine */
8769 if (!bf_get(lpfc_trailer_async, &mcqe))
8770 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8771 else
8772 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8773 return workposted;
8774}
8775
4f774513
JS
8776/**
8777 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8778 * @phba: Pointer to HBA context object.
8779 * @wcqe: Pointer to work-queue completion queue entry.
8780 *
8781 * This routine handles an ELS work-queue completion event.
8782 *
8783 * Return: true if work posted to worker thread, otherwise false.
8784 **/
8785static bool
8786lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8787 struct lpfc_wcqe_complete *wcqe)
8788{
4f774513
JS
8789 struct lpfc_iocbq *irspiocbq;
8790 unsigned long iflags;
4f774513 8791
45ed1190 8792 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
8793 irspiocbq = lpfc_sli_get_iocbq(phba);
8794 if (!irspiocbq) {
8795 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8796 "0387 Failed to allocate an iocbq\n");
45ed1190 8797 return false;
4f774513 8798 }
4f774513 8799
45ed1190
JS
8800 /* Save off the slow-path queue event for work thread to process */
8801 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 8802 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 8803 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
8804 &phba->sli4_hba.sp_queue_event);
8805 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 8806 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 8807
45ed1190 8808 return true;
4f774513
JS
8809}
8810
8811/**
8812 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8813 * @phba: Pointer to HBA context object.
8814 * @wcqe: Pointer to work-queue completion queue entry.
8815 *
8816 * This routine handles slow-path WQ entry comsumed event by invoking the
8817 * proper WQ release routine to the slow-path WQ.
8818 **/
8819static void
8820lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8821 struct lpfc_wcqe_release *wcqe)
8822{
8823 /* Check for the slow-path ELS work queue */
8824 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8825 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8826 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8827 else
8828 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8829 "2579 Slow-path wqe consume event carries "
8830 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8831 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8832 phba->sli4_hba.els_wq->queue_id);
8833}
8834
8835/**
8836 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8837 * @phba: Pointer to HBA context object.
8838 * @cq: Pointer to a WQ completion queue.
8839 * @wcqe: Pointer to work-queue completion queue entry.
8840 *
8841 * This routine handles an XRI abort event.
8842 *
8843 * Return: true if work posted to worker thread, otherwise false.
8844 **/
8845static bool
8846lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8847 struct lpfc_queue *cq,
8848 struct sli4_wcqe_xri_aborted *wcqe)
8849{
8850 bool workposted = false;
8851 struct lpfc_cq_event *cq_event;
8852 unsigned long iflags;
8853
8854 /* Allocate a new internal CQ_EVENT entry */
8855 cq_event = lpfc_sli4_cq_event_alloc(phba);
8856 if (!cq_event) {
8857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8858 "0602 Failed to allocate CQ_EVENT entry\n");
8859 return false;
8860 }
8861
8862 /* Move the CQE into the proper xri abort event list */
8863 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8864 switch (cq->subtype) {
8865 case LPFC_FCP:
8866 spin_lock_irqsave(&phba->hbalock, iflags);
8867 list_add_tail(&cq_event->list,
8868 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8869 /* Set the fcp xri abort event flag */
8870 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8871 spin_unlock_irqrestore(&phba->hbalock, iflags);
8872 workposted = true;
8873 break;
8874 case LPFC_ELS:
8875 spin_lock_irqsave(&phba->hbalock, iflags);
8876 list_add_tail(&cq_event->list,
8877 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8878 /* Set the els xri abort event flag */
8879 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8880 spin_unlock_irqrestore(&phba->hbalock, iflags);
8881 workposted = true;
8882 break;
8883 default:
8884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8885 "0603 Invalid work queue CQE subtype (x%x)\n",
8886 cq->subtype);
8887 workposted = false;
8888 break;
8889 }
8890 return workposted;
8891}
8892
4f774513
JS
8893/**
8894 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8895 * @phba: Pointer to HBA context object.
8896 * @rcqe: Pointer to receive-queue completion queue entry.
8897 *
8898 * This routine process a receive-queue completion queue entry.
8899 *
8900 * Return: true if work posted to worker thread, otherwise false.
8901 **/
8902static bool
4d9ab994 8903lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 8904{
4f774513
JS
8905 bool workposted = false;
8906 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8907 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8908 struct hbq_dmabuf *dma_buf;
8909 uint32_t status;
8910 unsigned long iflags;
8911
4d9ab994 8912 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
8913 goto out;
8914
4d9ab994 8915 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
8916 switch (status) {
8917 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8919 "2537 Receive Frame Truncated!!\n");
8920 case FC_STATUS_RQ_SUCCESS:
5ffc266e 8921 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
8922 spin_lock_irqsave(&phba->hbalock, iflags);
8923 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8924 if (!dma_buf) {
8925 spin_unlock_irqrestore(&phba->hbalock, iflags);
8926 goto out;
8927 }
4d9ab994 8928 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 8929 /* save off the frame for the word thread to process */
4d9ab994 8930 list_add_tail(&dma_buf->cq_event.list,
45ed1190 8931 &phba->sli4_hba.sp_queue_event);
4f774513 8932 /* Frame received */
45ed1190 8933 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
8934 spin_unlock_irqrestore(&phba->hbalock, iflags);
8935 workposted = true;
8936 break;
8937 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8938 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8939 /* Post more buffers if possible */
8940 spin_lock_irqsave(&phba->hbalock, iflags);
8941 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8942 spin_unlock_irqrestore(&phba->hbalock, iflags);
8943 workposted = true;
8944 break;
8945 }
8946out:
8947 return workposted;
4f774513
JS
8948}
8949
4d9ab994
JS
8950/**
8951 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8952 * @phba: Pointer to HBA context object.
8953 * @cq: Pointer to the completion queue.
8954 * @wcqe: Pointer to a completion queue entry.
8955 *
8956 * This routine process a slow-path work-queue or recieve queue completion queue
8957 * entry.
8958 *
8959 * Return: true if work posted to worker thread, otherwise false.
8960 **/
8961static bool
8962lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8963 struct lpfc_cqe *cqe)
8964{
45ed1190 8965 struct lpfc_cqe cqevt;
4d9ab994
JS
8966 bool workposted = false;
8967
8968 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 8969 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
8970
8971 /* Check and process for different type of WCQE and dispatch */
45ed1190 8972 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 8973 case CQE_CODE_COMPL_WQE:
45ed1190 8974 /* Process the WQ/RQ complete event */
4d9ab994 8975 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 8976 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
8977 break;
8978 case CQE_CODE_RELEASE_WQE:
8979 /* Process the WQ release event */
8980 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 8981 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
8982 break;
8983 case CQE_CODE_XRI_ABORTED:
8984 /* Process the WQ XRI abort event */
8985 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 8986 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
8987 break;
8988 case CQE_CODE_RECEIVE:
8989 /* Process the RQ event */
8990 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 8991 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
8992 break;
8993 default:
8994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8995 "0388 Not a valid WCQE code: x%x\n",
45ed1190 8996 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
8997 break;
8998 }
8999 return workposted;
9000}
9001
4f774513
JS
9002/**
9003 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
9004 * @phba: Pointer to HBA context object.
9005 * @eqe: Pointer to fast-path event queue entry.
9006 *
9007 * This routine process a event queue entry from the slow-path event queue.
9008 * It will check the MajorCode and MinorCode to determine this is for a
9009 * completion event on a completion queue, if not, an error shall be logged
9010 * and just return. Otherwise, it will get to the corresponding completion
9011 * queue and process all the entries on that completion queue, rearm the
9012 * completion queue, and then return.
9013 *
9014 **/
9015static void
9016lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
9017{
9018 struct lpfc_queue *cq = NULL, *childq, *speq;
9019 struct lpfc_cqe *cqe;
9020 bool workposted = false;
9021 int ecount = 0;
9022 uint16_t cqid;
9023
cb5172ea 9024 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
9025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9026 "0359 Not a valid slow-path completion "
9027 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9028 bf_get_le32(lpfc_eqe_major_code, eqe),
9029 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9030 return;
9031 }
9032
9033 /* Get the reference to the corresponding CQ */
cb5172ea 9034 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9035
9036 /* Search for completion queue pointer matching this cqid */
9037 speq = phba->sli4_hba.sp_eq;
9038 list_for_each_entry(childq, &speq->child_list, list) {
9039 if (childq->queue_id == cqid) {
9040 cq = childq;
9041 break;
9042 }
9043 }
9044 if (unlikely(!cq)) {
9045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9046 "0365 Slow-path CQ identifier (%d) does "
9047 "not exist\n", cqid);
9048 return;
9049 }
9050
9051 /* Process all the entries to the CQ */
9052 switch (cq->type) {
9053 case LPFC_MCQ:
9054 while ((cqe = lpfc_sli4_cq_get(cq))) {
9055 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9056 if (!(++ecount % LPFC_GET_QE_REL_INT))
9057 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9058 }
9059 break;
9060 case LPFC_WCQ:
9061 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 9062 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
9063 if (!(++ecount % LPFC_GET_QE_REL_INT))
9064 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9065 }
9066 break;
9067 default:
9068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9069 "0370 Invalid completion queue type (%d)\n",
9070 cq->type);
9071 return;
9072 }
9073
9074 /* Catch the no cq entry condition, log an error */
9075 if (unlikely(ecount == 0))
9076 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9077 "0371 No entry from the CQ: identifier "
9078 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9079
9080 /* In any case, flash and re-arm the RCQ */
9081 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9082
9083 /* wake up worker thread if there are works to be done */
9084 if (workposted)
9085 lpfc_worker_wake_up(phba);
9086}
9087
9088/**
9089 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9090 * @eqe: Pointer to fast-path completion queue entry.
9091 *
9092 * This routine process a fast-path work queue completion entry from fast-path
9093 * event queue for FCP command response completion.
9094 **/
9095static void
9096lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9097 struct lpfc_wcqe_complete *wcqe)
9098{
9099 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9100 struct lpfc_iocbq *cmdiocbq;
9101 struct lpfc_iocbq irspiocbq;
9102 unsigned long iflags;
9103
9104 spin_lock_irqsave(&phba->hbalock, iflags);
9105 pring->stats.iocb_event++;
9106 spin_unlock_irqrestore(&phba->hbalock, iflags);
9107
9108 /* Check for response status */
9109 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9110 /* If resource errors reported from HBA, reduce queue
9111 * depth of the SCSI device.
9112 */
9113 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9114 IOSTAT_LOCAL_REJECT) &&
9115 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9116 phba->lpfc_rampdown_queue_depth(phba);
9117 }
9118 /* Log the error status */
9119 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9120 "0373 FCP complete error: status=x%x, "
9121 "hw_status=x%x, total_data_specified=%d, "
9122 "parameter=x%x, word3=x%x\n",
9123 bf_get(lpfc_wcqe_c_status, wcqe),
9124 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9125 wcqe->total_data_placed, wcqe->parameter,
9126 wcqe->word3);
9127 }
9128
9129 /* Look up the FCP command IOCB and create pseudo response IOCB */
9130 spin_lock_irqsave(&phba->hbalock, iflags);
9131 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9132 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9133 spin_unlock_irqrestore(&phba->hbalock, iflags);
9134 if (unlikely(!cmdiocbq)) {
9135 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9136 "0374 FCP complete with no corresponding "
9137 "cmdiocb: iotag (%d)\n",
9138 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9139 return;
9140 }
9141 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9142 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9143 "0375 FCP cmdiocb not callback function "
9144 "iotag: (%d)\n",
9145 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9146 return;
9147 }
9148
9149 /* Fake the irspiocb and copy necessary response information */
341af102 9150 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 9151
0f65ff68
JS
9152 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9153 spin_lock_irqsave(&phba->hbalock, iflags);
9154 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9155 spin_unlock_irqrestore(&phba->hbalock, iflags);
9156 }
9157
4f774513
JS
9158 /* Pass the cmd_iocb and the rsp state to the upper layer */
9159 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9160}
9161
9162/**
9163 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9164 * @phba: Pointer to HBA context object.
9165 * @cq: Pointer to completion queue.
9166 * @wcqe: Pointer to work-queue completion queue entry.
9167 *
9168 * This routine handles an fast-path WQ entry comsumed event by invoking the
9169 * proper WQ release routine to the slow-path WQ.
9170 **/
9171static void
9172lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9173 struct lpfc_wcqe_release *wcqe)
9174{
9175 struct lpfc_queue *childwq;
9176 bool wqid_matched = false;
9177 uint16_t fcp_wqid;
9178
9179 /* Check for fast-path FCP work queue release */
9180 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9181 list_for_each_entry(childwq, &cq->child_list, list) {
9182 if (childwq->queue_id == fcp_wqid) {
9183 lpfc_sli4_wq_release(childwq,
9184 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9185 wqid_matched = true;
9186 break;
9187 }
9188 }
9189 /* Report warning log message if no match found */
9190 if (wqid_matched != true)
9191 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9192 "2580 Fast-path wqe consume event carries "
9193 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9194}
9195
9196/**
9197 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9198 * @cq: Pointer to the completion queue.
9199 * @eqe: Pointer to fast-path completion queue entry.
9200 *
9201 * This routine process a fast-path work queue completion entry from fast-path
9202 * event queue for FCP command response completion.
9203 **/
9204static int
9205lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9206 struct lpfc_cqe *cqe)
9207{
9208 struct lpfc_wcqe_release wcqe;
9209 bool workposted = false;
9210
9211 /* Copy the work queue CQE and convert endian order if needed */
9212 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9213
9214 /* Check and process for different type of WCQE and dispatch */
9215 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9216 case CQE_CODE_COMPL_WQE:
9217 /* Process the WQ complete event */
9218 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9219 (struct lpfc_wcqe_complete *)&wcqe);
9220 break;
9221 case CQE_CODE_RELEASE_WQE:
9222 /* Process the WQ release event */
9223 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9224 (struct lpfc_wcqe_release *)&wcqe);
9225 break;
9226 case CQE_CODE_XRI_ABORTED:
9227 /* Process the WQ XRI abort event */
9228 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9229 (struct sli4_wcqe_xri_aborted *)&wcqe);
9230 break;
9231 default:
9232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9233 "0144 Not a valid WCQE code: x%x\n",
9234 bf_get(lpfc_wcqe_c_code, &wcqe));
9235 break;
9236 }
9237 return workposted;
9238}
9239
9240/**
9241 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9242 * @phba: Pointer to HBA context object.
9243 * @eqe: Pointer to fast-path event queue entry.
9244 *
9245 * This routine process a event queue entry from the fast-path event queue.
9246 * It will check the MajorCode and MinorCode to determine this is for a
9247 * completion event on a completion queue, if not, an error shall be logged
9248 * and just return. Otherwise, it will get to the corresponding completion
9249 * queue and process all the entries on the completion queue, rearm the
9250 * completion queue, and then return.
9251 **/
9252static void
9253lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9254 uint32_t fcp_cqidx)
9255{
9256 struct lpfc_queue *cq;
9257 struct lpfc_cqe *cqe;
9258 bool workposted = false;
9259 uint16_t cqid;
9260 int ecount = 0;
9261
cb5172ea 9262 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
9263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9264 "0366 Not a valid fast-path completion "
9265 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
9266 bf_get_le32(lpfc_eqe_major_code, eqe),
9267 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
9268 return;
9269 }
9270
9271 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9272 if (unlikely(!cq)) {
9273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9274 "0367 Fast-path completion queue does not "
9275 "exist\n");
9276 return;
9277 }
9278
9279 /* Get the reference to the corresponding CQ */
cb5172ea 9280 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
9281 if (unlikely(cqid != cq->queue_id)) {
9282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9283 "0368 Miss-matched fast-path completion "
9284 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9285 cqid, cq->queue_id);
9286 return;
9287 }
9288
9289 /* Process all the entries to the CQ */
9290 while ((cqe = lpfc_sli4_cq_get(cq))) {
9291 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9292 if (!(++ecount % LPFC_GET_QE_REL_INT))
9293 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9294 }
9295
9296 /* Catch the no cq entry condition */
9297 if (unlikely(ecount == 0))
9298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9299 "0369 No entry from fast-path completion "
9300 "queue fcpcqid=%d\n", cq->queue_id);
9301
9302 /* In any case, flash and re-arm the CQ */
9303 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9304
9305 /* wake up worker thread if there are works to be done */
9306 if (workposted)
9307 lpfc_worker_wake_up(phba);
9308}
9309
9310static void
9311lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9312{
9313 struct lpfc_eqe *eqe;
9314
9315 /* walk all the EQ entries and drop on the floor */
9316 while ((eqe = lpfc_sli4_eq_get(eq)))
9317 ;
9318
9319 /* Clear and re-arm the EQ */
9320 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9321}
9322
9323/**
9324 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9325 * @irq: Interrupt number.
9326 * @dev_id: The device context pointer.
9327 *
9328 * This function is directly called from the PCI layer as an interrupt
9329 * service routine when device with SLI-4 interface spec is enabled with
9330 * MSI-X multi-message interrupt mode and there are slow-path events in
9331 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9332 * interrupt mode, this function is called as part of the device-level
9333 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9334 * undergoing initialization, the interrupt handler will not process the
9335 * interrupt. The link attention and ELS ring attention events are handled
9336 * by the worker thread. The interrupt handler signals the worker thread
9337 * and returns for these events. This function is called without any lock
9338 * held. It gets the hbalock to access and update SLI data structures.
9339 *
9340 * This function returns IRQ_HANDLED when interrupt is handled else it
9341 * returns IRQ_NONE.
9342 **/
9343irqreturn_t
9344lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9345{
9346 struct lpfc_hba *phba;
9347 struct lpfc_queue *speq;
9348 struct lpfc_eqe *eqe;
9349 unsigned long iflag;
9350 int ecount = 0;
9351
9352 /*
9353 * Get the driver's phba structure from the dev_id
9354 */
9355 phba = (struct lpfc_hba *)dev_id;
9356
9357 if (unlikely(!phba))
9358 return IRQ_NONE;
9359
9360 /* Get to the EQ struct associated with this vector */
9361 speq = phba->sli4_hba.sp_eq;
9362
9363 /* Check device state for handling interrupt */
9364 if (unlikely(lpfc_intr_state_check(phba))) {
9365 /* Check again for link_state with lock held */
9366 spin_lock_irqsave(&phba->hbalock, iflag);
9367 if (phba->link_state < LPFC_LINK_DOWN)
9368 /* Flush, clear interrupt, and rearm the EQ */
9369 lpfc_sli4_eq_flush(phba, speq);
9370 spin_unlock_irqrestore(&phba->hbalock, iflag);
9371 return IRQ_NONE;
9372 }
9373
9374 /*
9375 * Process all the event on FCP slow-path EQ
9376 */
9377 while ((eqe = lpfc_sli4_eq_get(speq))) {
9378 lpfc_sli4_sp_handle_eqe(phba, eqe);
9379 if (!(++ecount % LPFC_GET_QE_REL_INT))
9380 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9381 }
9382
9383 /* Always clear and re-arm the slow-path EQ */
9384 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9385
9386 /* Catch the no cq entry condition */
9387 if (unlikely(ecount == 0)) {
9388 if (phba->intr_type == MSIX)
9389 /* MSI-X treated interrupt served as no EQ share INT */
9390 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9391 "0357 MSI-X interrupt with no EQE\n");
9392 else
9393 /* Non MSI-X treated on interrupt as EQ share INT */
9394 return IRQ_NONE;
9395 }
9396
9397 return IRQ_HANDLED;
9398} /* lpfc_sli4_sp_intr_handler */
9399
9400/**
9401 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9402 * @irq: Interrupt number.
9403 * @dev_id: The device context pointer.
9404 *
9405 * This function is directly called from the PCI layer as an interrupt
9406 * service routine when device with SLI-4 interface spec is enabled with
9407 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9408 * ring event in the HBA. However, when the device is enabled with either
9409 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9410 * device-level interrupt handler. When the PCI slot is in error recovery
9411 * or the HBA is undergoing initialization, the interrupt handler will not
9412 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9413 * the intrrupt context. This function is called without any lock held.
9414 * It gets the hbalock to access and update SLI data structures. Note that,
9415 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9416 * equal to that of FCP CQ index.
9417 *
9418 * This function returns IRQ_HANDLED when interrupt is handled else it
9419 * returns IRQ_NONE.
9420 **/
9421irqreturn_t
9422lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9423{
9424 struct lpfc_hba *phba;
9425 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9426 struct lpfc_queue *fpeq;
9427 struct lpfc_eqe *eqe;
9428 unsigned long iflag;
9429 int ecount = 0;
9430 uint32_t fcp_eqidx;
9431
9432 /* Get the driver's phba structure from the dev_id */
9433 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9434 phba = fcp_eq_hdl->phba;
9435 fcp_eqidx = fcp_eq_hdl->idx;
9436
9437 if (unlikely(!phba))
9438 return IRQ_NONE;
9439
9440 /* Get to the EQ struct associated with this vector */
9441 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9442
9443 /* Check device state for handling interrupt */
9444 if (unlikely(lpfc_intr_state_check(phba))) {
9445 /* Check again for link_state with lock held */
9446 spin_lock_irqsave(&phba->hbalock, iflag);
9447 if (phba->link_state < LPFC_LINK_DOWN)
9448 /* Flush, clear interrupt, and rearm the EQ */
9449 lpfc_sli4_eq_flush(phba, fpeq);
9450 spin_unlock_irqrestore(&phba->hbalock, iflag);
9451 return IRQ_NONE;
9452 }
9453
9454 /*
9455 * Process all the event on FCP fast-path EQ
9456 */
9457 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9458 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9459 if (!(++ecount % LPFC_GET_QE_REL_INT))
9460 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9461 }
9462
9463 /* Always clear and re-arm the fast-path EQ */
9464 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9465
9466 if (unlikely(ecount == 0)) {
9467 if (phba->intr_type == MSIX)
9468 /* MSI-X treated interrupt served as no EQ share INT */
9469 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9470 "0358 MSI-X interrupt with no EQE\n");
9471 else
9472 /* Non MSI-X treated on interrupt as EQ share INT */
9473 return IRQ_NONE;
9474 }
9475
9476 return IRQ_HANDLED;
9477} /* lpfc_sli4_fp_intr_handler */
9478
9479/**
9480 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9481 * @irq: Interrupt number.
9482 * @dev_id: The device context pointer.
9483 *
9484 * This function is the device-level interrupt handler to device with SLI-4
9485 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9486 * interrupt mode is enabled and there is an event in the HBA which requires
9487 * driver attention. This function invokes the slow-path interrupt attention
9488 * handling function and fast-path interrupt attention handling function in
9489 * turn to process the relevant HBA attention events. This function is called
9490 * without any lock held. It gets the hbalock to access and update SLI data
9491 * structures.
9492 *
9493 * This function returns IRQ_HANDLED when interrupt is handled, else it
9494 * returns IRQ_NONE.
9495 **/
9496irqreturn_t
9497lpfc_sli4_intr_handler(int irq, void *dev_id)
9498{
9499 struct lpfc_hba *phba;
9500 irqreturn_t sp_irq_rc, fp_irq_rc;
9501 bool fp_handled = false;
9502 uint32_t fcp_eqidx;
9503
9504 /* Get the driver's phba structure from the dev_id */
9505 phba = (struct lpfc_hba *)dev_id;
9506
9507 if (unlikely(!phba))
9508 return IRQ_NONE;
9509
9510 /*
9511 * Invokes slow-path host attention interrupt handling as appropriate.
9512 */
9513 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9514
9515 /*
9516 * Invoke fast-path host attention interrupt handling as appropriate.
9517 */
9518 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9519 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9520 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9521 if (fp_irq_rc == IRQ_HANDLED)
9522 fp_handled |= true;
9523 }
9524
9525 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9526} /* lpfc_sli4_intr_handler */
9527
9528/**
9529 * lpfc_sli4_queue_free - free a queue structure and associated memory
9530 * @queue: The queue structure to free.
9531 *
9532 * This function frees a queue structure and the DMAable memeory used for
9533 * the host resident queue. This function must be called after destroying the
9534 * queue on the HBA.
9535 **/
9536void
9537lpfc_sli4_queue_free(struct lpfc_queue *queue)
9538{
9539 struct lpfc_dmabuf *dmabuf;
9540
9541 if (!queue)
9542 return;
9543
9544 while (!list_empty(&queue->page_list)) {
9545 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9546 list);
9547 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9548 dmabuf->virt, dmabuf->phys);
9549 kfree(dmabuf);
9550 }
9551 kfree(queue);
9552 return;
9553}
9554
9555/**
9556 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9557 * @phba: The HBA that this queue is being created on.
9558 * @entry_size: The size of each queue entry for this queue.
9559 * @entry count: The number of entries that this queue will handle.
9560 *
9561 * This function allocates a queue structure and the DMAable memory used for
9562 * the host resident queue. This function must be called before creating the
9563 * queue on the HBA.
9564 **/
9565struct lpfc_queue *
9566lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9567 uint32_t entry_count)
9568{
9569 struct lpfc_queue *queue;
9570 struct lpfc_dmabuf *dmabuf;
9571 int x, total_qe_count;
9572 void *dma_pointer;
cb5172ea 9573 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513
JS
9574
9575
cb5172ea
JS
9576 if (!phba->sli4_hba.pc_sli4_params.supported)
9577 hw_page_size = SLI4_PAGE_SIZE;
9578
4f774513
JS
9579 queue = kzalloc(sizeof(struct lpfc_queue) +
9580 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9581 if (!queue)
9582 return NULL;
cb5172ea
JS
9583 queue->page_count = (ALIGN(entry_size * entry_count,
9584 hw_page_size))/hw_page_size;
4f774513
JS
9585 INIT_LIST_HEAD(&queue->list);
9586 INIT_LIST_HEAD(&queue->page_list);
9587 INIT_LIST_HEAD(&queue->child_list);
9588 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9589 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9590 if (!dmabuf)
9591 goto out_fail;
9592 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 9593 hw_page_size, &dmabuf->phys,
4f774513
JS
9594 GFP_KERNEL);
9595 if (!dmabuf->virt) {
9596 kfree(dmabuf);
9597 goto out_fail;
9598 }
cb5172ea 9599 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
9600 dmabuf->buffer_tag = x;
9601 list_add_tail(&dmabuf->list, &queue->page_list);
9602 /* initialize queue's entry array */
9603 dma_pointer = dmabuf->virt;
9604 for (; total_qe_count < entry_count &&
cb5172ea 9605 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
9606 total_qe_count++, dma_pointer += entry_size) {
9607 queue->qe[total_qe_count].address = dma_pointer;
9608 }
9609 }
9610 queue->entry_size = entry_size;
9611 queue->entry_count = entry_count;
9612 queue->phba = phba;
9613
9614 return queue;
9615out_fail:
9616 lpfc_sli4_queue_free(queue);
9617 return NULL;
9618}
9619
9620/**
9621 * lpfc_eq_create - Create an Event Queue on the HBA
9622 * @phba: HBA structure that indicates port to create a queue on.
9623 * @eq: The queue structure to use to create the event queue.
9624 * @imax: The maximum interrupt per second limit.
9625 *
9626 * This function creates an event queue, as detailed in @eq, on a port,
9627 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9628 *
9629 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9630 * is used to get the entry count and entry size that are necessary to
9631 * determine the number of pages to allocate and use for this queue. This
9632 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9633 * event queue. This function is asynchronous and will wait for the mailbox
9634 * command to finish before continuing.
9635 *
9636 * On success this function will return a zero. If unable to allocate enough
9637 * memory this function will return ENOMEM. If the queue create mailbox command
9638 * fails this function will return ENXIO.
9639 **/
9640uint32_t
9641lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9642{
9643 struct lpfc_mbx_eq_create *eq_create;
9644 LPFC_MBOXQ_t *mbox;
9645 int rc, length, status = 0;
9646 struct lpfc_dmabuf *dmabuf;
9647 uint32_t shdr_status, shdr_add_status;
9648 union lpfc_sli4_cfg_shdr *shdr;
9649 uint16_t dmult;
9650
9651 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9652 if (!mbox)
9653 return -ENOMEM;
9654 length = (sizeof(struct lpfc_mbx_eq_create) -
9655 sizeof(struct lpfc_sli4_cfg_mhdr));
9656 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9657 LPFC_MBOX_OPCODE_EQ_CREATE,
9658 length, LPFC_SLI4_MBX_EMBED);
9659 eq_create = &mbox->u.mqe.un.eq_create;
9660 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9661 eq->page_count);
9662 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9663 LPFC_EQE_SIZE);
9664 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9665 /* Calculate delay multiper from maximum interrupt per second */
9666 dmult = LPFC_DMULT_CONST/imax - 1;
9667 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9668 dmult);
9669 switch (eq->entry_count) {
9670 default:
9671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9672 "0360 Unsupported EQ count. (%d)\n",
9673 eq->entry_count);
9674 if (eq->entry_count < 256)
9675 return -EINVAL;
9676 /* otherwise default to smallest count (drop through) */
9677 case 256:
9678 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9679 LPFC_EQ_CNT_256);
9680 break;
9681 case 512:
9682 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9683 LPFC_EQ_CNT_512);
9684 break;
9685 case 1024:
9686 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9687 LPFC_EQ_CNT_1024);
9688 break;
9689 case 2048:
9690 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9691 LPFC_EQ_CNT_2048);
9692 break;
9693 case 4096:
9694 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9695 LPFC_EQ_CNT_4096);
9696 break;
9697 }
9698 list_for_each_entry(dmabuf, &eq->page_list, list) {
9699 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9700 putPaddrLow(dmabuf->phys);
9701 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9702 putPaddrHigh(dmabuf->phys);
9703 }
9704 mbox->vport = phba->pport;
9705 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9706 mbox->context1 = NULL;
9707 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9708 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9709 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9710 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9711 if (shdr_status || shdr_add_status || rc) {
9712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9713 "2500 EQ_CREATE mailbox failed with "
9714 "status x%x add_status x%x, mbx status x%x\n",
9715 shdr_status, shdr_add_status, rc);
9716 status = -ENXIO;
9717 }
9718 eq->type = LPFC_EQ;
9719 eq->subtype = LPFC_NONE;
9720 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9721 if (eq->queue_id == 0xFFFF)
9722 status = -ENXIO;
9723 eq->host_index = 0;
9724 eq->hba_index = 0;
9725
8fa38513 9726 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9727 return status;
9728}
9729
9730/**
9731 * lpfc_cq_create - Create a Completion Queue on the HBA
9732 * @phba: HBA structure that indicates port to create a queue on.
9733 * @cq: The queue structure to use to create the completion queue.
9734 * @eq: The event queue to bind this completion queue to.
9735 *
9736 * This function creates a completion queue, as detailed in @wq, on a port,
9737 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9738 *
9739 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9740 * is used to get the entry count and entry size that are necessary to
9741 * determine the number of pages to allocate and use for this queue. The @eq
9742 * is used to indicate which event queue to bind this completion queue to. This
9743 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9744 * completion queue. This function is asynchronous and will wait for the mailbox
9745 * command to finish before continuing.
9746 *
9747 * On success this function will return a zero. If unable to allocate enough
9748 * memory this function will return ENOMEM. If the queue create mailbox command
9749 * fails this function will return ENXIO.
9750 **/
9751uint32_t
9752lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9753 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9754{
9755 struct lpfc_mbx_cq_create *cq_create;
9756 struct lpfc_dmabuf *dmabuf;
9757 LPFC_MBOXQ_t *mbox;
9758 int rc, length, status = 0;
9759 uint32_t shdr_status, shdr_add_status;
9760 union lpfc_sli4_cfg_shdr *shdr;
9761
9762 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9763 if (!mbox)
9764 return -ENOMEM;
9765 length = (sizeof(struct lpfc_mbx_cq_create) -
9766 sizeof(struct lpfc_sli4_cfg_mhdr));
9767 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9768 LPFC_MBOX_OPCODE_CQ_CREATE,
9769 length, LPFC_SLI4_MBX_EMBED);
9770 cq_create = &mbox->u.mqe.un.cq_create;
9771 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9772 cq->page_count);
9773 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9774 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9775 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9776 switch (cq->entry_count) {
9777 default:
9778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9779 "0361 Unsupported CQ count. (%d)\n",
9780 cq->entry_count);
9781 if (cq->entry_count < 256)
9782 return -EINVAL;
9783 /* otherwise default to smallest count (drop through) */
9784 case 256:
9785 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9786 LPFC_CQ_CNT_256);
9787 break;
9788 case 512:
9789 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9790 LPFC_CQ_CNT_512);
9791 break;
9792 case 1024:
9793 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9794 LPFC_CQ_CNT_1024);
9795 break;
9796 }
9797 list_for_each_entry(dmabuf, &cq->page_list, list) {
9798 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9799 putPaddrLow(dmabuf->phys);
9800 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9801 putPaddrHigh(dmabuf->phys);
9802 }
9803 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9804
9805 /* The IOCTL status is embedded in the mailbox subheader. */
9806 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9807 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9808 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9809 if (shdr_status || shdr_add_status || rc) {
9810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9811 "2501 CQ_CREATE mailbox failed with "
9812 "status x%x add_status x%x, mbx status x%x\n",
9813 shdr_status, shdr_add_status, rc);
9814 status = -ENXIO;
9815 goto out;
9816 }
9817 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9818 if (cq->queue_id == 0xFFFF) {
9819 status = -ENXIO;
9820 goto out;
9821 }
9822 /* link the cq onto the parent eq child list */
9823 list_add_tail(&cq->list, &eq->child_list);
9824 /* Set up completion queue's type and subtype */
9825 cq->type = type;
9826 cq->subtype = subtype;
9827 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9828 cq->host_index = 0;
9829 cq->hba_index = 0;
4f774513 9830
8fa38513
JS
9831out:
9832 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9833 return status;
9834}
9835
b19a061a
JS
9836/**
9837 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
9838 * @phba: HBA structure that indicates port to create a queue on.
9839 * @mq: The queue structure to use to create the mailbox queue.
9840 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
9841 * @cq: The completion queue to associate with this cq.
9842 *
9843 * This function provides failback (fb) functionality when the
9844 * mq_create_ext fails on older FW generations. It's purpose is identical
9845 * to mq_create_ext otherwise.
9846 *
9847 * This routine cannot fail as all attributes were previously accessed and
9848 * initialized in mq_create_ext.
9849 **/
9850static void
9851lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
9852 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
9853{
9854 struct lpfc_mbx_mq_create *mq_create;
9855 struct lpfc_dmabuf *dmabuf;
9856 int length;
9857
9858 length = (sizeof(struct lpfc_mbx_mq_create) -
9859 sizeof(struct lpfc_sli4_cfg_mhdr));
9860 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9861 LPFC_MBOX_OPCODE_MQ_CREATE,
9862 length, LPFC_SLI4_MBX_EMBED);
9863 mq_create = &mbox->u.mqe.un.mq_create;
9864 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9865 mq->page_count);
9866 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9867 cq->queue_id);
9868 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9869 switch (mq->entry_count) {
9870 case 16:
9871 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9872 LPFC_MQ_CNT_16);
9873 break;
9874 case 32:
9875 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9876 LPFC_MQ_CNT_32);
9877 break;
9878 case 64:
9879 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9880 LPFC_MQ_CNT_64);
9881 break;
9882 case 128:
9883 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9884 LPFC_MQ_CNT_128);
9885 break;
9886 }
9887 list_for_each_entry(dmabuf, &mq->page_list, list) {
9888 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9889 putPaddrLow(dmabuf->phys);
9890 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9891 putPaddrHigh(dmabuf->phys);
9892 }
9893}
9894
04c68496
JS
9895/**
9896 * lpfc_mq_create - Create a mailbox Queue on the HBA
9897 * @phba: HBA structure that indicates port to create a queue on.
9898 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
9899 * @cq: The completion queue to associate with this cq.
9900 * @subtype: The queue's subtype.
04c68496
JS
9901 *
9902 * This function creates a mailbox queue, as detailed in @mq, on a port,
9903 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9904 *
9905 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9906 * is used to get the entry count and entry size that are necessary to
9907 * determine the number of pages to allocate and use for this queue. This
9908 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9909 * mailbox queue. This function is asynchronous and will wait for the mailbox
9910 * command to finish before continuing.
9911 *
9912 * On success this function will return a zero. If unable to allocate enough
9913 * memory this function will return ENOMEM. If the queue create mailbox command
9914 * fails this function will return ENXIO.
9915 **/
b19a061a 9916int32_t
04c68496
JS
9917lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9918 struct lpfc_queue *cq, uint32_t subtype)
9919{
9920 struct lpfc_mbx_mq_create *mq_create;
b19a061a 9921 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
9922 struct lpfc_dmabuf *dmabuf;
9923 LPFC_MBOXQ_t *mbox;
9924 int rc, length, status = 0;
9925 uint32_t shdr_status, shdr_add_status;
9926 union lpfc_sli4_cfg_shdr *shdr;
9927
b19a061a 9928
04c68496
JS
9929 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9930 if (!mbox)
9931 return -ENOMEM;
b19a061a 9932 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
9933 sizeof(struct lpfc_sli4_cfg_mhdr));
9934 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 9935 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 9936 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
9937
9938 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
9939 bf_set(lpfc_mbx_mq_create_ext_num_pages, &mq_create_ext->u.request,
04c68496 9940 mq->page_count);
b19a061a
JS
9941 bf_set(lpfc_mbx_mq_create_ext_async_evt_link, &mq_create_ext->u.request,
9942 1);
9943 bf_set(lpfc_mbx_mq_create_ext_async_evt_fcfste,
9944 &mq_create_ext->u.request, 1);
9945 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
9946 &mq_create_ext->u.request, 1);
9947 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
9948 cq->queue_id);
9949 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
04c68496
JS
9950 switch (mq->entry_count) {
9951 default:
9952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9953 "0362 Unsupported MQ count. (%d)\n",
9954 mq->entry_count);
9955 if (mq->entry_count < 16)
9956 return -EINVAL;
9957 /* otherwise default to smallest count (drop through) */
9958 case 16:
b19a061a 9959 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
9960 LPFC_MQ_CNT_16);
9961 break;
9962 case 32:
b19a061a 9963 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
9964 LPFC_MQ_CNT_32);
9965 break;
9966 case 64:
b19a061a 9967 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
9968 LPFC_MQ_CNT_64);
9969 break;
9970 case 128:
b19a061a 9971 bf_set(lpfc_mq_context_count, &mq_create_ext->u.request.context,
04c68496
JS
9972 LPFC_MQ_CNT_128);
9973 break;
9974 }
9975 list_for_each_entry(dmabuf, &mq->page_list, list) {
b19a061a 9976 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 9977 putPaddrLow(dmabuf->phys);
b19a061a 9978 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
9979 putPaddrHigh(dmabuf->phys);
9980 }
9981 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
9982 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
9983 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9984 &mq_create_ext->u.response);
9985 if (rc != MBX_SUCCESS) {
9986 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9987 "2795 MQ_CREATE_EXT failed with "
9988 "status x%x. Failback to MQ_CREATE.\n",
9989 rc);
9990 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
9991 mq_create = &mbox->u.mqe.un.mq_create;
9992 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9993 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9994 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
9995 &mq_create->u.response);
9996 }
9997
04c68496 9998 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
9999 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10000 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10001 if (shdr_status || shdr_add_status || rc) {
10002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10003 "2502 MQ_CREATE mailbox failed with "
10004 "status x%x add_status x%x, mbx status x%x\n",
10005 shdr_status, shdr_add_status, rc);
10006 status = -ENXIO;
10007 goto out;
10008 }
04c68496
JS
10009 if (mq->queue_id == 0xFFFF) {
10010 status = -ENXIO;
10011 goto out;
10012 }
10013 mq->type = LPFC_MQ;
10014 mq->subtype = subtype;
10015 mq->host_index = 0;
10016 mq->hba_index = 0;
10017
10018 /* link the mq onto the parent cq child list */
10019 list_add_tail(&mq->list, &cq->child_list);
10020out:
8fa38513 10021 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
10022 return status;
10023}
10024
4f774513
JS
10025/**
10026 * lpfc_wq_create - Create a Work Queue on the HBA
10027 * @phba: HBA structure that indicates port to create a queue on.
10028 * @wq: The queue structure to use to create the work queue.
10029 * @cq: The completion queue to bind this work queue to.
10030 * @subtype: The subtype of the work queue indicating its functionality.
10031 *
10032 * This function creates a work queue, as detailed in @wq, on a port, described
10033 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
10034 *
10035 * The @phba struct is used to send mailbox command to HBA. The @wq struct
10036 * is used to get the entry count and entry size that are necessary to
10037 * determine the number of pages to allocate and use for this queue. The @cq
10038 * is used to indicate which completion queue to bind this work queue to. This
10039 * function will send the WQ_CREATE mailbox command to the HBA to setup the
10040 * work queue. This function is asynchronous and will wait for the mailbox
10041 * command to finish before continuing.
10042 *
10043 * On success this function will return a zero. If unable to allocate enough
10044 * memory this function will return ENOMEM. If the queue create mailbox command
10045 * fails this function will return ENXIO.
10046 **/
10047uint32_t
10048lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
10049 struct lpfc_queue *cq, uint32_t subtype)
10050{
10051 struct lpfc_mbx_wq_create *wq_create;
10052 struct lpfc_dmabuf *dmabuf;
10053 LPFC_MBOXQ_t *mbox;
10054 int rc, length, status = 0;
10055 uint32_t shdr_status, shdr_add_status;
10056 union lpfc_sli4_cfg_shdr *shdr;
10057
10058 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10059 if (!mbox)
10060 return -ENOMEM;
10061 length = (sizeof(struct lpfc_mbx_wq_create) -
10062 sizeof(struct lpfc_sli4_cfg_mhdr));
10063 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10064 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
10065 length, LPFC_SLI4_MBX_EMBED);
10066 wq_create = &mbox->u.mqe.un.wq_create;
10067 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
10068 wq->page_count);
10069 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
10070 cq->queue_id);
10071 list_for_each_entry(dmabuf, &wq->page_list, list) {
10072 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10073 putPaddrLow(dmabuf->phys);
10074 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10075 putPaddrHigh(dmabuf->phys);
10076 }
10077 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10078 /* The IOCTL status is embedded in the mailbox subheader. */
10079 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
10080 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10081 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10082 if (shdr_status || shdr_add_status || rc) {
10083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10084 "2503 WQ_CREATE mailbox failed with "
10085 "status x%x add_status x%x, mbx status x%x\n",
10086 shdr_status, shdr_add_status, rc);
10087 status = -ENXIO;
10088 goto out;
10089 }
10090 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
10091 if (wq->queue_id == 0xFFFF) {
10092 status = -ENXIO;
10093 goto out;
10094 }
10095 wq->type = LPFC_WQ;
10096 wq->subtype = subtype;
10097 wq->host_index = 0;
10098 wq->hba_index = 0;
10099
10100 /* link the wq onto the parent cq child list */
10101 list_add_tail(&wq->list, &cq->child_list);
10102out:
8fa38513 10103 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10104 return status;
10105}
10106
10107/**
10108 * lpfc_rq_create - Create a Receive Queue on the HBA
10109 * @phba: HBA structure that indicates port to create a queue on.
10110 * @hrq: The queue structure to use to create the header receive queue.
10111 * @drq: The queue structure to use to create the data receive queue.
10112 * @cq: The completion queue to bind this work queue to.
10113 *
10114 * This function creates a receive buffer queue pair , as detailed in @hrq and
10115 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
10116 * to the HBA.
10117 *
10118 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
10119 * struct is used to get the entry count that is necessary to determine the
10120 * number of pages to use for this queue. The @cq is used to indicate which
10121 * completion queue to bind received buffers that are posted to these queues to.
10122 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
10123 * receive queue pair. This function is asynchronous and will wait for the
10124 * mailbox command to finish before continuing.
10125 *
10126 * On success this function will return a zero. If unable to allocate enough
10127 * memory this function will return ENOMEM. If the queue create mailbox command
10128 * fails this function will return ENXIO.
10129 **/
10130uint32_t
10131lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10132 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10133{
10134 struct lpfc_mbx_rq_create *rq_create;
10135 struct lpfc_dmabuf *dmabuf;
10136 LPFC_MBOXQ_t *mbox;
10137 int rc, length, status = 0;
10138 uint32_t shdr_status, shdr_add_status;
10139 union lpfc_sli4_cfg_shdr *shdr;
10140
10141 if (hrq->entry_count != drq->entry_count)
10142 return -EINVAL;
10143 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10144 if (!mbox)
10145 return -ENOMEM;
10146 length = (sizeof(struct lpfc_mbx_rq_create) -
10147 sizeof(struct lpfc_sli4_cfg_mhdr));
10148 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10149 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10150 length, LPFC_SLI4_MBX_EMBED);
10151 rq_create = &mbox->u.mqe.un.rq_create;
10152 switch (hrq->entry_count) {
10153 default:
10154 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10155 "2535 Unsupported RQ count. (%d)\n",
10156 hrq->entry_count);
10157 if (hrq->entry_count < 512)
10158 return -EINVAL;
10159 /* otherwise default to smallest count (drop through) */
10160 case 512:
10161 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10162 LPFC_RQ_RING_SIZE_512);
10163 break;
10164 case 1024:
10165 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10166 LPFC_RQ_RING_SIZE_1024);
10167 break;
10168 case 2048:
10169 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10170 LPFC_RQ_RING_SIZE_2048);
10171 break;
10172 case 4096:
10173 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10174 LPFC_RQ_RING_SIZE_4096);
10175 break;
10176 }
10177 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10178 cq->queue_id);
10179 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10180 hrq->page_count);
10181 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10182 LPFC_HDR_BUF_SIZE);
10183 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10184 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10185 putPaddrLow(dmabuf->phys);
10186 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10187 putPaddrHigh(dmabuf->phys);
10188 }
10189 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10190 /* The IOCTL status is embedded in the mailbox subheader. */
10191 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10192 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10193 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10194 if (shdr_status || shdr_add_status || rc) {
10195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10196 "2504 RQ_CREATE mailbox failed with "
10197 "status x%x add_status x%x, mbx status x%x\n",
10198 shdr_status, shdr_add_status, rc);
10199 status = -ENXIO;
10200 goto out;
10201 }
10202 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10203 if (hrq->queue_id == 0xFFFF) {
10204 status = -ENXIO;
10205 goto out;
10206 }
10207 hrq->type = LPFC_HRQ;
10208 hrq->subtype = subtype;
10209 hrq->host_index = 0;
10210 hrq->hba_index = 0;
10211
10212 /* now create the data queue */
10213 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10214 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10215 length, LPFC_SLI4_MBX_EMBED);
10216 switch (drq->entry_count) {
10217 default:
10218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10219 "2536 Unsupported RQ count. (%d)\n",
10220 drq->entry_count);
10221 if (drq->entry_count < 512)
10222 return -EINVAL;
10223 /* otherwise default to smallest count (drop through) */
10224 case 512:
10225 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10226 LPFC_RQ_RING_SIZE_512);
10227 break;
10228 case 1024:
10229 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10230 LPFC_RQ_RING_SIZE_1024);
10231 break;
10232 case 2048:
10233 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10234 LPFC_RQ_RING_SIZE_2048);
10235 break;
10236 case 4096:
10237 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10238 LPFC_RQ_RING_SIZE_4096);
10239 break;
10240 }
10241 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10242 cq->queue_id);
10243 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10244 drq->page_count);
10245 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10246 LPFC_DATA_BUF_SIZE);
10247 list_for_each_entry(dmabuf, &drq->page_list, list) {
10248 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10249 putPaddrLow(dmabuf->phys);
10250 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10251 putPaddrHigh(dmabuf->phys);
10252 }
10253 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10254 /* The IOCTL status is embedded in the mailbox subheader. */
10255 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10256 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10257 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10258 if (shdr_status || shdr_add_status || rc) {
10259 status = -ENXIO;
10260 goto out;
10261 }
10262 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10263 if (drq->queue_id == 0xFFFF) {
10264 status = -ENXIO;
10265 goto out;
10266 }
10267 drq->type = LPFC_DRQ;
10268 drq->subtype = subtype;
10269 drq->host_index = 0;
10270 drq->hba_index = 0;
10271
10272 /* link the header and data RQs onto the parent cq child list */
10273 list_add_tail(&hrq->list, &cq->child_list);
10274 list_add_tail(&drq->list, &cq->child_list);
10275
10276out:
8fa38513 10277 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10278 return status;
10279}
10280
10281/**
10282 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10283 * @eq: The queue structure associated with the queue to destroy.
10284 *
10285 * This function destroys a queue, as detailed in @eq by sending an mailbox
10286 * command, specific to the type of queue, to the HBA.
10287 *
10288 * The @eq struct is used to get the queue ID of the queue to destroy.
10289 *
10290 * On success this function will return a zero. If the queue destroy mailbox
10291 * command fails this function will return ENXIO.
10292 **/
10293uint32_t
10294lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10295{
10296 LPFC_MBOXQ_t *mbox;
10297 int rc, length, status = 0;
10298 uint32_t shdr_status, shdr_add_status;
10299 union lpfc_sli4_cfg_shdr *shdr;
10300
10301 if (!eq)
10302 return -ENODEV;
10303 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10304 if (!mbox)
10305 return -ENOMEM;
10306 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10307 sizeof(struct lpfc_sli4_cfg_mhdr));
10308 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10309 LPFC_MBOX_OPCODE_EQ_DESTROY,
10310 length, LPFC_SLI4_MBX_EMBED);
10311 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10312 eq->queue_id);
10313 mbox->vport = eq->phba->pport;
10314 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10315
10316 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10317 /* The IOCTL status is embedded in the mailbox subheader. */
10318 shdr = (union lpfc_sli4_cfg_shdr *)
10319 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10320 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10321 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10322 if (shdr_status || shdr_add_status || rc) {
10323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10324 "2505 EQ_DESTROY mailbox failed with "
10325 "status x%x add_status x%x, mbx status x%x\n",
10326 shdr_status, shdr_add_status, rc);
10327 status = -ENXIO;
10328 }
10329
10330 /* Remove eq from any list */
10331 list_del_init(&eq->list);
8fa38513 10332 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10333 return status;
10334}
10335
10336/**
10337 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10338 * @cq: The queue structure associated with the queue to destroy.
10339 *
10340 * This function destroys a queue, as detailed in @cq by sending an mailbox
10341 * command, specific to the type of queue, to the HBA.
10342 *
10343 * The @cq struct is used to get the queue ID of the queue to destroy.
10344 *
10345 * On success this function will return a zero. If the queue destroy mailbox
10346 * command fails this function will return ENXIO.
10347 **/
10348uint32_t
10349lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10350{
10351 LPFC_MBOXQ_t *mbox;
10352 int rc, length, status = 0;
10353 uint32_t shdr_status, shdr_add_status;
10354 union lpfc_sli4_cfg_shdr *shdr;
10355
10356 if (!cq)
10357 return -ENODEV;
10358 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10359 if (!mbox)
10360 return -ENOMEM;
10361 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10362 sizeof(struct lpfc_sli4_cfg_mhdr));
10363 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10364 LPFC_MBOX_OPCODE_CQ_DESTROY,
10365 length, LPFC_SLI4_MBX_EMBED);
10366 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10367 cq->queue_id);
10368 mbox->vport = cq->phba->pport;
10369 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10370 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10371 /* The IOCTL status is embedded in the mailbox subheader. */
10372 shdr = (union lpfc_sli4_cfg_shdr *)
10373 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10374 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10375 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10376 if (shdr_status || shdr_add_status || rc) {
10377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10378 "2506 CQ_DESTROY mailbox failed with "
10379 "status x%x add_status x%x, mbx status x%x\n",
10380 shdr_status, shdr_add_status, rc);
10381 status = -ENXIO;
10382 }
10383 /* Remove cq from any list */
10384 list_del_init(&cq->list);
8fa38513 10385 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10386 return status;
10387}
10388
04c68496
JS
10389/**
10390 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10391 * @qm: The queue structure associated with the queue to destroy.
10392 *
10393 * This function destroys a queue, as detailed in @mq by sending an mailbox
10394 * command, specific to the type of queue, to the HBA.
10395 *
10396 * The @mq struct is used to get the queue ID of the queue to destroy.
10397 *
10398 * On success this function will return a zero. If the queue destroy mailbox
10399 * command fails this function will return ENXIO.
10400 **/
10401uint32_t
10402lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10403{
10404 LPFC_MBOXQ_t *mbox;
10405 int rc, length, status = 0;
10406 uint32_t shdr_status, shdr_add_status;
10407 union lpfc_sli4_cfg_shdr *shdr;
10408
10409 if (!mq)
10410 return -ENODEV;
10411 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10412 if (!mbox)
10413 return -ENOMEM;
10414 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10415 sizeof(struct lpfc_sli4_cfg_mhdr));
10416 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10417 LPFC_MBOX_OPCODE_MQ_DESTROY,
10418 length, LPFC_SLI4_MBX_EMBED);
10419 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10420 mq->queue_id);
10421 mbox->vport = mq->phba->pport;
10422 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10423 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10424 /* The IOCTL status is embedded in the mailbox subheader. */
10425 shdr = (union lpfc_sli4_cfg_shdr *)
10426 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10427 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10428 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10429 if (shdr_status || shdr_add_status || rc) {
10430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10431 "2507 MQ_DESTROY mailbox failed with "
10432 "status x%x add_status x%x, mbx status x%x\n",
10433 shdr_status, shdr_add_status, rc);
10434 status = -ENXIO;
10435 }
10436 /* Remove mq from any list */
10437 list_del_init(&mq->list);
8fa38513 10438 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10439 return status;
10440}
10441
4f774513
JS
10442/**
10443 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10444 * @wq: The queue structure associated with the queue to destroy.
10445 *
10446 * This function destroys a queue, as detailed in @wq by sending an mailbox
10447 * command, specific to the type of queue, to the HBA.
10448 *
10449 * The @wq struct is used to get the queue ID of the queue to destroy.
10450 *
10451 * On success this function will return a zero. If the queue destroy mailbox
10452 * command fails this function will return ENXIO.
10453 **/
10454uint32_t
10455lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10456{
10457 LPFC_MBOXQ_t *mbox;
10458 int rc, length, status = 0;
10459 uint32_t shdr_status, shdr_add_status;
10460 union lpfc_sli4_cfg_shdr *shdr;
10461
10462 if (!wq)
10463 return -ENODEV;
10464 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10465 if (!mbox)
10466 return -ENOMEM;
10467 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10468 sizeof(struct lpfc_sli4_cfg_mhdr));
10469 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10470 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10471 length, LPFC_SLI4_MBX_EMBED);
10472 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10473 wq->queue_id);
10474 mbox->vport = wq->phba->pport;
10475 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10476 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10477 shdr = (union lpfc_sli4_cfg_shdr *)
10478 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10479 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10480 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10481 if (shdr_status || shdr_add_status || rc) {
10482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10483 "2508 WQ_DESTROY mailbox failed with "
10484 "status x%x add_status x%x, mbx status x%x\n",
10485 shdr_status, shdr_add_status, rc);
10486 status = -ENXIO;
10487 }
10488 /* Remove wq from any list */
10489 list_del_init(&wq->list);
8fa38513 10490 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10491 return status;
10492}
10493
10494/**
10495 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10496 * @rq: The queue structure associated with the queue to destroy.
10497 *
10498 * This function destroys a queue, as detailed in @rq by sending an mailbox
10499 * command, specific to the type of queue, to the HBA.
10500 *
10501 * The @rq struct is used to get the queue ID of the queue to destroy.
10502 *
10503 * On success this function will return a zero. If the queue destroy mailbox
10504 * command fails this function will return ENXIO.
10505 **/
10506uint32_t
10507lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10508 struct lpfc_queue *drq)
10509{
10510 LPFC_MBOXQ_t *mbox;
10511 int rc, length, status = 0;
10512 uint32_t shdr_status, shdr_add_status;
10513 union lpfc_sli4_cfg_shdr *shdr;
10514
10515 if (!hrq || !drq)
10516 return -ENODEV;
10517 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10518 if (!mbox)
10519 return -ENOMEM;
10520 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10521 sizeof(struct mbox_header));
10522 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10523 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10524 length, LPFC_SLI4_MBX_EMBED);
10525 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10526 hrq->queue_id);
10527 mbox->vport = hrq->phba->pport;
10528 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10529 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10530 /* The IOCTL status is embedded in the mailbox subheader. */
10531 shdr = (union lpfc_sli4_cfg_shdr *)
10532 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10533 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10534 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10535 if (shdr_status || shdr_add_status || rc) {
10536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10537 "2509 RQ_DESTROY mailbox failed with "
10538 "status x%x add_status x%x, mbx status x%x\n",
10539 shdr_status, shdr_add_status, rc);
10540 if (rc != MBX_TIMEOUT)
10541 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10542 return -ENXIO;
10543 }
10544 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10545 drq->queue_id);
10546 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10547 shdr = (union lpfc_sli4_cfg_shdr *)
10548 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10549 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10550 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10551 if (shdr_status || shdr_add_status || rc) {
10552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10553 "2510 RQ_DESTROY mailbox failed with "
10554 "status x%x add_status x%x, mbx status x%x\n",
10555 shdr_status, shdr_add_status, rc);
10556 status = -ENXIO;
10557 }
10558 list_del_init(&hrq->list);
10559 list_del_init(&drq->list);
8fa38513 10560 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10561 return status;
10562}
10563
10564/**
10565 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10566 * @phba: The virtual port for which this call being executed.
10567 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10568 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10569 * @xritag: the xritag that ties this io to the SGL pages.
10570 *
10571 * This routine will post the sgl pages for the IO that has the xritag
10572 * that is in the iocbq structure. The xritag is assigned during iocbq
10573 * creation and persists for as long as the driver is loaded.
10574 * if the caller has fewer than 256 scatter gather segments to map then
10575 * pdma_phys_addr1 should be 0.
10576 * If the caller needs to map more than 256 scatter gather segment then
10577 * pdma_phys_addr1 should be a valid physical address.
10578 * physical address for SGLs must be 64 byte aligned.
10579 * If you are going to map 2 SGL's then the first one must have 256 entries
10580 * the second sgl can have between 1 and 256 entries.
10581 *
10582 * Return codes:
10583 * 0 - Success
10584 * -ENXIO, -ENOMEM - Failure
10585 **/
10586int
10587lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10588 dma_addr_t pdma_phys_addr0,
10589 dma_addr_t pdma_phys_addr1,
10590 uint16_t xritag)
10591{
10592 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10593 LPFC_MBOXQ_t *mbox;
10594 int rc;
10595 uint32_t shdr_status, shdr_add_status;
10596 union lpfc_sli4_cfg_shdr *shdr;
10597
10598 if (xritag == NO_XRI) {
10599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10600 "0364 Invalid param:\n");
10601 return -EINVAL;
10602 }
10603
10604 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10605 if (!mbox)
10606 return -ENOMEM;
10607
10608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10609 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10610 sizeof(struct lpfc_mbx_post_sgl_pages) -
10611 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10612
10613 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10614 &mbox->u.mqe.un.post_sgl_pages;
10615 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10616 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10617
10618 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10619 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10620 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10621 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10622
10623 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10624 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10625 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10626 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10627 if (!phba->sli4_hba.intr_enable)
10628 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10629 else
10630 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10631 /* The IOCTL status is embedded in the mailbox subheader. */
10632 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10633 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10634 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10635 if (rc != MBX_TIMEOUT)
10636 mempool_free(mbox, phba->mbox_mem_pool);
10637 if (shdr_status || shdr_add_status || rc) {
10638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10639 "2511 POST_SGL mailbox failed with "
10640 "status x%x add_status x%x, mbx status x%x\n",
10641 shdr_status, shdr_add_status, rc);
10642 rc = -ENXIO;
10643 }
10644 return 0;
10645}
10646/**
10647 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10648 * @phba: The virtual port for which this call being executed.
10649 *
10650 * This routine will remove all of the sgl pages registered with the hba.
10651 *
10652 * Return codes:
10653 * 0 - Success
10654 * -ENXIO, -ENOMEM - Failure
10655 **/
10656int
10657lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10658{
10659 LPFC_MBOXQ_t *mbox;
10660 int rc;
10661 uint32_t shdr_status, shdr_add_status;
10662 union lpfc_sli4_cfg_shdr *shdr;
10663
10664 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10665 if (!mbox)
10666 return -ENOMEM;
10667
10668 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10669 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10670 LPFC_SLI4_MBX_EMBED);
10671 if (!phba->sli4_hba.intr_enable)
10672 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10673 else
10674 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10675 /* The IOCTL status is embedded in the mailbox subheader. */
10676 shdr = (union lpfc_sli4_cfg_shdr *)
10677 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10678 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10679 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10680 if (rc != MBX_TIMEOUT)
10681 mempool_free(mbox, phba->mbox_mem_pool);
10682 if (shdr_status || shdr_add_status || rc) {
10683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10684 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10685 "status x%x add_status x%x, mbx status x%x\n",
10686 shdr_status, shdr_add_status, rc);
10687 rc = -ENXIO;
10688 }
10689 return rc;
10690}
10691
10692/**
10693 * lpfc_sli4_next_xritag - Get an xritag for the io
10694 * @phba: Pointer to HBA context object.
10695 *
10696 * This function gets an xritag for the iocb. If there is no unused xritag
10697 * it will return 0xffff.
10698 * The function returns the allocated xritag if successful, else returns zero.
10699 * Zero is not a valid xritag.
10700 * The caller is not required to hold any lock.
10701 **/
10702uint16_t
10703lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10704{
10705 uint16_t xritag;
10706
10707 spin_lock_irq(&phba->hbalock);
10708 xritag = phba->sli4_hba.next_xri;
10709 if ((xritag != (uint16_t) -1) && xritag <
10710 (phba->sli4_hba.max_cfg_param.max_xri
10711 + phba->sli4_hba.max_cfg_param.xri_base)) {
10712 phba->sli4_hba.next_xri++;
10713 phba->sli4_hba.max_cfg_param.xri_used++;
10714 spin_unlock_irq(&phba->hbalock);
10715 return xritag;
10716 }
10717 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10718 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10719 "2004 Failed to allocate XRI.last XRITAG is %d"
10720 " Max XRI is %d, Used XRI is %d\n",
10721 phba->sli4_hba.next_xri,
10722 phba->sli4_hba.max_cfg_param.max_xri,
10723 phba->sli4_hba.max_cfg_param.xri_used);
10724 return -1;
10725}
10726
10727/**
10728 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10729 * @phba: pointer to lpfc hba data structure.
10730 *
10731 * This routine is invoked to post a block of driver's sgl pages to the
10732 * HBA using non-embedded mailbox command. No Lock is held. This routine
10733 * is only called when the driver is loading and after all IO has been
10734 * stopped.
10735 **/
10736int
10737lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10738{
10739 struct lpfc_sglq *sglq_entry;
10740 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10741 struct sgl_page_pairs *sgl_pg_pairs;
10742 void *viraddr;
10743 LPFC_MBOXQ_t *mbox;
10744 uint32_t reqlen, alloclen, pg_pairs;
10745 uint32_t mbox_tmo;
10746 uint16_t xritag_start = 0;
10747 int els_xri_cnt, rc = 0;
10748 uint32_t shdr_status, shdr_add_status;
10749 union lpfc_sli4_cfg_shdr *shdr;
10750
10751 /* The number of sgls to be posted */
10752 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10753
10754 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10755 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10756 if (reqlen > PAGE_SIZE) {
10757 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10758 "2559 Block sgl registration required DMA "
10759 "size (%d) great than a page\n", reqlen);
10760 return -ENOMEM;
10761 }
10762 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10763 if (!mbox) {
10764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10765 "2560 Failed to allocate mbox cmd memory\n");
10766 return -ENOMEM;
10767 }
10768
10769 /* Allocate DMA memory and set up the non-embedded mailbox command */
10770 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10771 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10772 LPFC_SLI4_MBX_NEMBED);
10773
10774 if (alloclen < reqlen) {
10775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10776 "0285 Allocated DMA memory size (%d) is "
10777 "less than the requested DMA memory "
10778 "size (%d)\n", alloclen, reqlen);
10779 lpfc_sli4_mbox_cmd_free(phba, mbox);
10780 return -ENOMEM;
10781 }
4f774513 10782 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10783 viraddr = mbox->sge_array->addr[0];
10784
10785 /* Set up the SGL pages in the non-embedded DMA pages */
10786 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10787 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10788
10789 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10790 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10791 /* Set up the sge entry */
10792 sgl_pg_pairs->sgl_pg0_addr_lo =
10793 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10794 sgl_pg_pairs->sgl_pg0_addr_hi =
10795 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10796 sgl_pg_pairs->sgl_pg1_addr_lo =
10797 cpu_to_le32(putPaddrLow(0));
10798 sgl_pg_pairs->sgl_pg1_addr_hi =
10799 cpu_to_le32(putPaddrHigh(0));
10800 /* Keep the first xritag on the list */
10801 if (pg_pairs == 0)
10802 xritag_start = sglq_entry->sli4_xritag;
10803 sgl_pg_pairs++;
10804 }
10805 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 10806 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
10807 /* Perform endian conversion if necessary */
10808 sgl->word0 = cpu_to_le32(sgl->word0);
10809
10810 if (!phba->sli4_hba.intr_enable)
10811 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10812 else {
10813 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10814 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10815 }
10816 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10817 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10818 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10819 if (rc != MBX_TIMEOUT)
10820 lpfc_sli4_mbox_cmd_free(phba, mbox);
10821 if (shdr_status || shdr_add_status || rc) {
10822 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10823 "2513 POST_SGL_BLOCK mailbox command failed "
10824 "status x%x add_status x%x mbx status x%x\n",
10825 shdr_status, shdr_add_status, rc);
10826 rc = -ENXIO;
10827 }
10828 return rc;
10829}
10830
10831/**
10832 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10833 * @phba: pointer to lpfc hba data structure.
10834 * @sblist: pointer to scsi buffer list.
10835 * @count: number of scsi buffers on the list.
10836 *
10837 * This routine is invoked to post a block of @count scsi sgl pages from a
10838 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10839 * No Lock is held.
10840 *
10841 **/
10842int
10843lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10844 int cnt)
10845{
10846 struct lpfc_scsi_buf *psb;
10847 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10848 struct sgl_page_pairs *sgl_pg_pairs;
10849 void *viraddr;
10850 LPFC_MBOXQ_t *mbox;
10851 uint32_t reqlen, alloclen, pg_pairs;
10852 uint32_t mbox_tmo;
10853 uint16_t xritag_start = 0;
10854 int rc = 0;
10855 uint32_t shdr_status, shdr_add_status;
10856 dma_addr_t pdma_phys_bpl1;
10857 union lpfc_sli4_cfg_shdr *shdr;
10858
10859 /* Calculate the requested length of the dma memory */
10860 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10861 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10862 if (reqlen > PAGE_SIZE) {
10863 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10864 "0217 Block sgl registration required DMA "
10865 "size (%d) great than a page\n", reqlen);
10866 return -ENOMEM;
10867 }
10868 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10869 if (!mbox) {
10870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10871 "0283 Failed to allocate mbox cmd memory\n");
10872 return -ENOMEM;
10873 }
10874
10875 /* Allocate DMA memory and set up the non-embedded mailbox command */
10876 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10877 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10878 LPFC_SLI4_MBX_NEMBED);
10879
10880 if (alloclen < reqlen) {
10881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10882 "2561 Allocated DMA memory size (%d) is "
10883 "less than the requested DMA memory "
10884 "size (%d)\n", alloclen, reqlen);
10885 lpfc_sli4_mbox_cmd_free(phba, mbox);
10886 return -ENOMEM;
10887 }
4f774513 10888 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10889 viraddr = mbox->sge_array->addr[0];
10890
10891 /* Set up the SGL pages in the non-embedded DMA pages */
10892 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10893 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10894
10895 pg_pairs = 0;
10896 list_for_each_entry(psb, sblist, list) {
10897 /* Set up the sge entry */
10898 sgl_pg_pairs->sgl_pg0_addr_lo =
10899 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10900 sgl_pg_pairs->sgl_pg0_addr_hi =
10901 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10902 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10903 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10904 else
10905 pdma_phys_bpl1 = 0;
10906 sgl_pg_pairs->sgl_pg1_addr_lo =
10907 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10908 sgl_pg_pairs->sgl_pg1_addr_hi =
10909 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10910 /* Keep the first xritag on the list */
10911 if (pg_pairs == 0)
10912 xritag_start = psb->cur_iocbq.sli4_xritag;
10913 sgl_pg_pairs++;
10914 pg_pairs++;
10915 }
10916 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10917 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10918 /* Perform endian conversion if necessary */
10919 sgl->word0 = cpu_to_le32(sgl->word0);
10920
10921 if (!phba->sli4_hba.intr_enable)
10922 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10923 else {
10924 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10925 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10926 }
10927 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10928 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10929 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10930 if (rc != MBX_TIMEOUT)
10931 lpfc_sli4_mbox_cmd_free(phba, mbox);
10932 if (shdr_status || shdr_add_status || rc) {
10933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10934 "2564 POST_SGL_BLOCK mailbox command failed "
10935 "status x%x add_status x%x mbx status x%x\n",
10936 shdr_status, shdr_add_status, rc);
10937 rc = -ENXIO;
10938 }
10939 return rc;
10940}
10941
10942/**
10943 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10944 * @phba: pointer to lpfc_hba struct that the frame was received on
10945 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10946 *
10947 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10948 * valid type of frame that the LPFC driver will handle. This function will
10949 * return a zero if the frame is a valid frame or a non zero value when the
10950 * frame does not pass the check.
10951 **/
10952static int
10953lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10954{
10955 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10956 char *type_names[] = FC_TYPE_NAMES_INIT;
10957 struct fc_vft_header *fc_vft_hdr;
10958
10959 switch (fc_hdr->fh_r_ctl) {
10960 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10961 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10962 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10963 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10964 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10965 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10966 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10967 case FC_RCTL_DD_CMD_STATUS: /* command status */
10968 case FC_RCTL_ELS_REQ: /* extended link services request */
10969 case FC_RCTL_ELS_REP: /* extended link services reply */
10970 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10971 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10972 case FC_RCTL_BA_NOP: /* basic link service NOP */
10973 case FC_RCTL_BA_ABTS: /* basic link service abort */
10974 case FC_RCTL_BA_RMC: /* remove connection */
10975 case FC_RCTL_BA_ACC: /* basic accept */
10976 case FC_RCTL_BA_RJT: /* basic reject */
10977 case FC_RCTL_BA_PRMT:
10978 case FC_RCTL_ACK_1: /* acknowledge_1 */
10979 case FC_RCTL_ACK_0: /* acknowledge_0 */
10980 case FC_RCTL_P_RJT: /* port reject */
10981 case FC_RCTL_F_RJT: /* fabric reject */
10982 case FC_RCTL_P_BSY: /* port busy */
10983 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10984 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10985 case FC_RCTL_LCR: /* link credit reset */
10986 case FC_RCTL_END: /* end */
10987 break;
10988 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10989 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10990 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10991 return lpfc_fc_frame_check(phba, fc_hdr);
10992 default:
10993 goto drop;
10994 }
10995 switch (fc_hdr->fh_type) {
10996 case FC_TYPE_BLS:
10997 case FC_TYPE_ELS:
10998 case FC_TYPE_FCP:
10999 case FC_TYPE_CT:
11000 break;
11001 case FC_TYPE_IP:
11002 case FC_TYPE_ILS:
11003 default:
11004 goto drop;
11005 }
11006 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11007 "2538 Received frame rctl:%s type:%s\n",
11008 rctl_names[fc_hdr->fh_r_ctl],
11009 type_names[fc_hdr->fh_type]);
11010 return 0;
11011drop:
11012 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11013 "2539 Dropped frame rctl:%s type:%s\n",
11014 rctl_names[fc_hdr->fh_r_ctl],
11015 type_names[fc_hdr->fh_type]);
11016 return 1;
11017}
11018
11019/**
11020 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
11021 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11022 *
11023 * This function processes the FC header to retrieve the VFI from the VF
11024 * header, if one exists. This function will return the VFI if one exists
11025 * or 0 if no VSAN Header exists.
11026 **/
11027static uint32_t
11028lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
11029{
11030 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
11031
11032 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
11033 return 0;
11034 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
11035}
11036
11037/**
11038 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
11039 * @phba: Pointer to the HBA structure to search for the vport on
11040 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
11041 * @fcfi: The FC Fabric ID that the frame came from
11042 *
11043 * This function searches the @phba for a vport that matches the content of the
11044 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
11045 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
11046 * returns the matching vport pointer or NULL if unable to match frame to a
11047 * vport.
11048 **/
11049static struct lpfc_vport *
11050lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
11051 uint16_t fcfi)
11052{
11053 struct lpfc_vport **vports;
11054 struct lpfc_vport *vport = NULL;
11055 int i;
11056 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
11057 fc_hdr->fh_d_id[1] << 8 |
11058 fc_hdr->fh_d_id[2]);
11059
11060 vports = lpfc_create_vport_work_array(phba);
11061 if (vports != NULL)
11062 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
11063 if (phba->fcf.fcfi == fcfi &&
11064 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
11065 vports[i]->fc_myDID == did) {
11066 vport = vports[i];
11067 break;
11068 }
11069 }
11070 lpfc_destroy_vport_work_array(phba, vports);
11071 return vport;
11072}
11073
45ed1190
JS
11074/**
11075 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
11076 * @vport: The vport to work on.
11077 *
11078 * This function updates the receive sequence time stamp for this vport. The
11079 * receive sequence time stamp indicates the time that the last frame of the
11080 * the sequence that has been idle for the longest amount of time was received.
11081 * the driver uses this time stamp to indicate if any received sequences have
11082 * timed out.
11083 **/
11084void
11085lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
11086{
11087 struct lpfc_dmabuf *h_buf;
11088 struct hbq_dmabuf *dmabuf = NULL;
11089
11090 /* get the oldest sequence on the rcv list */
11091 h_buf = list_get_first(&vport->rcv_buffer_list,
11092 struct lpfc_dmabuf, list);
11093 if (!h_buf)
11094 return;
11095 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11096 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
11097}
11098
11099/**
11100 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
11101 * @vport: The vport that the received sequences were sent to.
11102 *
11103 * This function cleans up all outstanding received sequences. This is called
11104 * by the driver when a link event or user action invalidates all the received
11105 * sequences.
11106 **/
11107void
11108lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
11109{
11110 struct lpfc_dmabuf *h_buf, *hnext;
11111 struct lpfc_dmabuf *d_buf, *dnext;
11112 struct hbq_dmabuf *dmabuf = NULL;
11113
11114 /* start with the oldest sequence on the rcv list */
11115 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11116 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11117 list_del_init(&dmabuf->hbuf.list);
11118 list_for_each_entry_safe(d_buf, dnext,
11119 &dmabuf->dbuf.list, list) {
11120 list_del_init(&d_buf->list);
11121 lpfc_in_buf_free(vport->phba, d_buf);
11122 }
11123 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11124 }
11125}
11126
11127/**
11128 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11129 * @vport: The vport that the received sequences were sent to.
11130 *
11131 * This function determines whether any received sequences have timed out by
11132 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11133 * indicates that there is at least one timed out sequence this routine will
11134 * go through the received sequences one at a time from most inactive to most
11135 * active to determine which ones need to be cleaned up. Once it has determined
11136 * that a sequence needs to be cleaned up it will simply free up the resources
11137 * without sending an abort.
11138 **/
11139void
11140lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11141{
11142 struct lpfc_dmabuf *h_buf, *hnext;
11143 struct lpfc_dmabuf *d_buf, *dnext;
11144 struct hbq_dmabuf *dmabuf = NULL;
11145 unsigned long timeout;
11146 int abort_count = 0;
11147
11148 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11149 vport->rcv_buffer_time_stamp);
11150 if (list_empty(&vport->rcv_buffer_list) ||
11151 time_before(jiffies, timeout))
11152 return;
11153 /* start with the oldest sequence on the rcv list */
11154 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11155 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11156 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11157 dmabuf->time_stamp);
11158 if (time_before(jiffies, timeout))
11159 break;
11160 abort_count++;
11161 list_del_init(&dmabuf->hbuf.list);
11162 list_for_each_entry_safe(d_buf, dnext,
11163 &dmabuf->dbuf.list, list) {
11164 list_del_init(&d_buf->list);
11165 lpfc_in_buf_free(vport->phba, d_buf);
11166 }
11167 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11168 }
11169 if (abort_count)
11170 lpfc_update_rcv_time_stamp(vport);
11171}
11172
4f774513
JS
11173/**
11174 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11175 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11176 *
11177 * This function searches through the existing incomplete sequences that have
11178 * been sent to this @vport. If the frame matches one of the incomplete
11179 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11180 * make up that sequence. If no sequence is found that matches this frame then
11181 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11182 * This function returns a pointer to the first dmabuf in the sequence list that
11183 * the frame was linked to.
11184 **/
11185static struct hbq_dmabuf *
11186lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11187{
11188 struct fc_frame_header *new_hdr;
11189 struct fc_frame_header *temp_hdr;
11190 struct lpfc_dmabuf *d_buf;
11191 struct lpfc_dmabuf *h_buf;
11192 struct hbq_dmabuf *seq_dmabuf = NULL;
11193 struct hbq_dmabuf *temp_dmabuf = NULL;
11194
4d9ab994 11195 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 11196 dmabuf->time_stamp = jiffies;
4f774513
JS
11197 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11198 /* Use the hdr_buf to find the sequence that this frame belongs to */
11199 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11200 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11201 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11202 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11203 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11204 continue;
11205 /* found a pending sequence that matches this frame */
11206 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11207 break;
11208 }
11209 if (!seq_dmabuf) {
11210 /*
11211 * This indicates first frame received for this sequence.
11212 * Queue the buffer on the vport's rcv_buffer_list.
11213 */
11214 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 11215 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11216 return dmabuf;
11217 }
11218 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
11219 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11220 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
11221 list_del_init(&seq_dmabuf->hbuf.list);
11222 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11223 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 11224 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11225 return dmabuf;
11226 }
45ed1190
JS
11227 /* move this sequence to the tail to indicate a young sequence */
11228 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11229 seq_dmabuf->time_stamp = jiffies;
11230 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
11231 if (list_empty(&seq_dmabuf->dbuf.list)) {
11232 temp_hdr = dmabuf->hbuf.virt;
11233 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11234 return seq_dmabuf;
11235 }
4f774513
JS
11236 /* find the correct place in the sequence to insert this frame */
11237 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11238 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11239 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11240 /*
11241 * If the frame's sequence count is greater than the frame on
11242 * the list then insert the frame right after this frame
11243 */
eeead811
JS
11244 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11245 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
11246 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11247 return seq_dmabuf;
11248 }
11249 }
11250 return NULL;
11251}
11252
6669f9bb
JS
11253/**
11254 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11255 * @vport: pointer to a vitural port
11256 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11257 *
11258 * This function tries to abort from the partially assembed sequence, described
11259 * by the information from basic abbort @dmabuf. It checks to see whether such
11260 * partially assembled sequence held by the driver. If so, it shall free up all
11261 * the frames from the partially assembled sequence.
11262 *
11263 * Return
11264 * true -- if there is matching partially assembled sequence present and all
11265 * the frames freed with the sequence;
11266 * false -- if there is no matching partially assembled sequence present so
11267 * nothing got aborted in the lower layer driver
11268 **/
11269static bool
11270lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11271 struct hbq_dmabuf *dmabuf)
11272{
11273 struct fc_frame_header *new_hdr;
11274 struct fc_frame_header *temp_hdr;
11275 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11276 struct hbq_dmabuf *seq_dmabuf = NULL;
11277
11278 /* Use the hdr_buf to find the sequence that matches this frame */
11279 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11280 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11281 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11282 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11283 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11284 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11285 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11286 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11287 continue;
11288 /* found a pending sequence that matches this frame */
11289 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11290 break;
11291 }
11292
11293 /* Free up all the frames from the partially assembled sequence */
11294 if (seq_dmabuf) {
11295 list_for_each_entry_safe(d_buf, n_buf,
11296 &seq_dmabuf->dbuf.list, list) {
11297 list_del_init(&d_buf->list);
11298 lpfc_in_buf_free(vport->phba, d_buf);
11299 }
11300 return true;
11301 }
11302 return false;
11303}
11304
11305/**
11306 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11307 * @phba: Pointer to HBA context object.
11308 * @cmd_iocbq: pointer to the command iocbq structure.
11309 * @rsp_iocbq: pointer to the response iocbq structure.
11310 *
11311 * This function handles the sequence abort accept iocb command complete
11312 * event. It properly releases the memory allocated to the sequence abort
11313 * accept iocb.
11314 **/
11315static void
11316lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11317 struct lpfc_iocbq *cmd_iocbq,
11318 struct lpfc_iocbq *rsp_iocbq)
11319{
11320 if (cmd_iocbq)
11321 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11322}
11323
11324/**
11325 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11326 * @phba: Pointer to HBA context object.
11327 * @fc_hdr: pointer to a FC frame header.
11328 *
11329 * This function sends a basic accept to a previous unsol sequence abort
11330 * event after aborting the sequence handling.
11331 **/
11332static void
11333lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11334 struct fc_frame_header *fc_hdr)
11335{
11336 struct lpfc_iocbq *ctiocb = NULL;
11337 struct lpfc_nodelist *ndlp;
5ffc266e
JS
11338 uint16_t oxid, rxid;
11339 uint32_t sid, fctl;
6669f9bb
JS
11340 IOCB_t *icmd;
11341
11342 if (!lpfc_is_link_up(phba))
11343 return;
11344
11345 sid = sli4_sid_from_fc_hdr(fc_hdr);
11346 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 11347 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
11348
11349 ndlp = lpfc_findnode_did(phba->pport, sid);
11350 if (!ndlp) {
11351 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11352 "1268 Find ndlp returned NULL for oxid:x%x "
11353 "SID:x%x\n", oxid, sid);
11354 return;
11355 }
11356
11357 /* Allocate buffer for acc iocb */
11358 ctiocb = lpfc_sli_get_iocbq(phba);
11359 if (!ctiocb)
11360 return;
11361
5ffc266e
JS
11362 /* Extract the F_CTL field from FC_HDR */
11363 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11364
6669f9bb 11365 icmd = &ctiocb->iocb;
6669f9bb 11366 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 11367 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
11368 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11369 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11370 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11371
11372 /* Fill in the rest of iocb fields */
11373 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11374 icmd->ulpBdeCount = 0;
11375 icmd->ulpLe = 1;
11376 icmd->ulpClass = CLASS3;
11377 icmd->ulpContext = ndlp->nlp_rpi;
6669f9bb 11378
6669f9bb
JS
11379 ctiocb->iocb_cmpl = NULL;
11380 ctiocb->vport = phba->pport;
11381 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11382
5ffc266e
JS
11383 if (fctl & FC_FC_EX_CTX) {
11384 /* ABTS sent by responder to CT exchange, construction
11385 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11386 * field and RX_ID from ABTS for RX_ID field.
11387 */
11388 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11389 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11390 ctiocb->sli4_xritag = oxid;
11391 } else {
11392 /* ABTS sent by initiator to CT exchange, construction
11393 * of BA_ACC will need to allocate a new XRI as for the
11394 * XRI_TAG and RX_ID fields.
11395 */
11396 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11397 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11398 ctiocb->sli4_xritag = NO_XRI;
11399 }
11400 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11401
6669f9bb
JS
11402 /* Xmit CT abts accept on exchange <xid> */
11403 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11404 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11405 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11406 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11407}
11408
11409/**
11410 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11411 * @vport: Pointer to the vport on which this sequence was received
11412 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11413 *
11414 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11415 * receive sequence is only partially assembed by the driver, it shall abort
11416 * the partially assembled frames for the sequence. Otherwise, if the
11417 * unsolicited receive sequence has been completely assembled and passed to
11418 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11419 * unsolicited sequence has been aborted. After that, it will issue a basic
11420 * accept to accept the abort.
11421 **/
11422void
11423lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11424 struct hbq_dmabuf *dmabuf)
11425{
11426 struct lpfc_hba *phba = vport->phba;
11427 struct fc_frame_header fc_hdr;
5ffc266e 11428 uint32_t fctl;
6669f9bb
JS
11429 bool abts_par;
11430
6669f9bb
JS
11431 /* Make a copy of fc_hdr before the dmabuf being released */
11432 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 11433 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 11434
5ffc266e
JS
11435 if (fctl & FC_FC_EX_CTX) {
11436 /*
11437 * ABTS sent by responder to exchange, just free the buffer
11438 */
6669f9bb 11439 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
11440 } else {
11441 /*
11442 * ABTS sent by initiator to exchange, need to do cleanup
11443 */
11444 /* Try to abort partially assembled seq */
11445 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11446
11447 /* Send abort to ULP if partially seq abort failed */
11448 if (abts_par == false)
11449 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11450 else
11451 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11452 }
6669f9bb
JS
11453 /* Send basic accept (BA_ACC) to the abort requester */
11454 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11455}
11456
4f774513
JS
11457/**
11458 * lpfc_seq_complete - Indicates if a sequence is complete
11459 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11460 *
11461 * This function checks the sequence, starting with the frame described by
11462 * @dmabuf, to see if all the frames associated with this sequence are present.
11463 * the frames associated with this sequence are linked to the @dmabuf using the
11464 * dbuf list. This function looks for two major things. 1) That the first frame
11465 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11466 * set. 3) That there are no holes in the sequence count. The function will
11467 * return 1 when the sequence is complete, otherwise it will return 0.
11468 **/
11469static int
11470lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11471{
11472 struct fc_frame_header *hdr;
11473 struct lpfc_dmabuf *d_buf;
11474 struct hbq_dmabuf *seq_dmabuf;
11475 uint32_t fctl;
11476 int seq_count = 0;
11477
11478 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11479 /* make sure first fame of sequence has a sequence count of zero */
11480 if (hdr->fh_seq_cnt != seq_count)
11481 return 0;
11482 fctl = (hdr->fh_f_ctl[0] << 16 |
11483 hdr->fh_f_ctl[1] << 8 |
11484 hdr->fh_f_ctl[2]);
11485 /* If last frame of sequence we can return success. */
11486 if (fctl & FC_FC_END_SEQ)
11487 return 1;
11488 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11489 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11490 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11491 /* If there is a hole in the sequence count then fail. */
eeead811 11492 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
11493 return 0;
11494 fctl = (hdr->fh_f_ctl[0] << 16 |
11495 hdr->fh_f_ctl[1] << 8 |
11496 hdr->fh_f_ctl[2]);
11497 /* If last frame of sequence we can return success. */
11498 if (fctl & FC_FC_END_SEQ)
11499 return 1;
11500 }
11501 return 0;
11502}
11503
11504/**
11505 * lpfc_prep_seq - Prep sequence for ULP processing
11506 * @vport: Pointer to the vport on which this sequence was received
11507 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11508 *
11509 * This function takes a sequence, described by a list of frames, and creates
11510 * a list of iocbq structures to describe the sequence. This iocbq list will be
11511 * used to issue to the generic unsolicited sequence handler. This routine
11512 * returns a pointer to the first iocbq in the list. If the function is unable
11513 * to allocate an iocbq then it throw out the received frames that were not
11514 * able to be described and return a pointer to the first iocbq. If unable to
11515 * allocate any iocbqs (including the first) this function will return NULL.
11516 **/
11517static struct lpfc_iocbq *
11518lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11519{
11520 struct lpfc_dmabuf *d_buf, *n_buf;
11521 struct lpfc_iocbq *first_iocbq, *iocbq;
11522 struct fc_frame_header *fc_hdr;
11523 uint32_t sid;
eeead811 11524 struct ulp_bde64 *pbde;
4f774513
JS
11525
11526 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11527 /* remove from receive buffer list */
11528 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 11529 lpfc_update_rcv_time_stamp(vport);
4f774513 11530 /* get the Remote Port's SID */
6669f9bb 11531 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11532 /* Get an iocbq struct to fill in. */
11533 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11534 if (first_iocbq) {
11535 /* Initialize the first IOCB. */
8fa38513 11536 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11537 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11538 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11539 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11540 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11541 vport->vpi + vport->phba->vpi_base;
11542 /* put the first buffer into the first IOCBq */
11543 first_iocbq->context2 = &seq_dmabuf->dbuf;
11544 first_iocbq->context3 = NULL;
11545 first_iocbq->iocb.ulpBdeCount = 1;
11546 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11547 LPFC_DATA_BUF_SIZE;
11548 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11549 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11550 bf_get(lpfc_rcqe_length,
11551 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11552 }
11553 iocbq = first_iocbq;
11554 /*
11555 * Each IOCBq can have two Buffers assigned, so go through the list
11556 * of buffers for this sequence and save two buffers in each IOCBq
11557 */
11558 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11559 if (!iocbq) {
11560 lpfc_in_buf_free(vport->phba, d_buf);
11561 continue;
11562 }
11563 if (!iocbq->context3) {
11564 iocbq->context3 = d_buf;
11565 iocbq->iocb.ulpBdeCount++;
eeead811
JS
11566 pbde = (struct ulp_bde64 *)
11567 &iocbq->iocb.unsli3.sli3Words[4];
11568 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
8fa38513 11569 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11570 bf_get(lpfc_rcqe_length,
11571 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11572 } else {
11573 iocbq = lpfc_sli_get_iocbq(vport->phba);
11574 if (!iocbq) {
11575 if (first_iocbq) {
11576 first_iocbq->iocb.ulpStatus =
11577 IOSTAT_FCP_RSP_ERROR;
11578 first_iocbq->iocb.un.ulpWord[4] =
11579 IOERR_NO_RESOURCES;
11580 }
11581 lpfc_in_buf_free(vport->phba, d_buf);
11582 continue;
11583 }
11584 iocbq->context2 = d_buf;
11585 iocbq->context3 = NULL;
11586 iocbq->iocb.ulpBdeCount = 1;
11587 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11588 LPFC_DATA_BUF_SIZE;
8fa38513 11589 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11590 bf_get(lpfc_rcqe_length,
11591 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11592 iocbq->iocb.un.rcvels.remoteID = sid;
11593 list_add_tail(&iocbq->list, &first_iocbq->list);
11594 }
11595 }
11596 return first_iocbq;
11597}
11598
6669f9bb
JS
11599static void
11600lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11601 struct hbq_dmabuf *seq_dmabuf)
11602{
11603 struct fc_frame_header *fc_hdr;
11604 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11605 struct lpfc_hba *phba = vport->phba;
11606
11607 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11608 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11609 if (!iocbq) {
11610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11611 "2707 Ring %d handler: Failed to allocate "
11612 "iocb Rctl x%x Type x%x received\n",
11613 LPFC_ELS_RING,
11614 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11615 return;
11616 }
11617 if (!lpfc_complete_unsol_iocb(phba,
11618 &phba->sli.ring[LPFC_ELS_RING],
11619 iocbq, fc_hdr->fh_r_ctl,
11620 fc_hdr->fh_type))
11621 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11622 "2540 Ring %d handler: unexpected Rctl "
11623 "x%x Type x%x received\n",
11624 LPFC_ELS_RING,
11625 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11626
11627 /* Free iocb created in lpfc_prep_seq */
11628 list_for_each_entry_safe(curr_iocb, next_iocb,
11629 &iocbq->list, list) {
11630 list_del_init(&curr_iocb->list);
11631 lpfc_sli_release_iocbq(phba, curr_iocb);
11632 }
11633 lpfc_sli_release_iocbq(phba, iocbq);
11634}
11635
4f774513
JS
11636/**
11637 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11638 * @phba: Pointer to HBA context object.
11639 *
11640 * This function is called with no lock held. This function processes all
11641 * the received buffers and gives it to upper layers when a received buffer
11642 * indicates that it is the final frame in the sequence. The interrupt
11643 * service routine processes received buffers at interrupt contexts and adds
11644 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11645 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11646 * appropriate receive function when the final frame in a sequence is received.
11647 **/
4d9ab994
JS
11648void
11649lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11650 struct hbq_dmabuf *dmabuf)
4f774513 11651{
4d9ab994 11652 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11653 struct fc_frame_header *fc_hdr;
11654 struct lpfc_vport *vport;
11655 uint32_t fcfi;
4f774513 11656
4f774513 11657 /* Process each received buffer */
4d9ab994
JS
11658 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11659 /* check to see if this a valid type of frame */
11660 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11661 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11662 return;
11663 }
11664 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11665 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 11666 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
11667 /* throw out the frame */
11668 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11669 return;
11670 }
6669f9bb
JS
11671 /* Handle the basic abort sequence (BA_ABTS) event */
11672 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11673 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11674 return;
11675 }
11676
4d9ab994
JS
11677 /* Link this frame */
11678 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11679 if (!seq_dmabuf) {
11680 /* unable to add frame to vport - throw it out */
11681 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11682 return;
11683 }
11684 /* If not last frame in sequence continue processing frames. */
def9c7a9 11685 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 11686 return;
def9c7a9 11687
6669f9bb
JS
11688 /* Send the complete sequence to the upper layer protocol */
11689 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11690}
6fb120a7
JS
11691
11692/**
11693 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11694 * @phba: pointer to lpfc hba data structure.
11695 *
11696 * This routine is invoked to post rpi header templates to the
11697 * HBA consistent with the SLI-4 interface spec. This routine
11698 * posts a PAGE_SIZE memory region to the port to hold up to
11699 * PAGE_SIZE modulo 64 rpi context headers.
11700 *
11701 * This routine does not require any locks. It's usage is expected
11702 * to be driver load or reset recovery when the driver is
11703 * sequential.
11704 *
11705 * Return codes
af901ca1 11706 * 0 - successful
6fb120a7
JS
11707 * EIO - The mailbox failed to complete successfully.
11708 * When this error occurs, the driver is not guaranteed
11709 * to have any rpi regions posted to the device and
11710 * must either attempt to repost the regions or take a
11711 * fatal error.
11712 **/
11713int
11714lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11715{
11716 struct lpfc_rpi_hdr *rpi_page;
11717 uint32_t rc = 0;
11718
11719 /* Post all rpi memory regions to the port. */
11720 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11721 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11722 if (rc != MBX_SUCCESS) {
11723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11724 "2008 Error %d posting all rpi "
11725 "headers\n", rc);
11726 rc = -EIO;
11727 break;
11728 }
11729 }
11730
11731 return rc;
11732}
11733
11734/**
11735 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11736 * @phba: pointer to lpfc hba data structure.
11737 * @rpi_page: pointer to the rpi memory region.
11738 *
11739 * This routine is invoked to post a single rpi header to the
11740 * HBA consistent with the SLI-4 interface spec. This memory region
11741 * maps up to 64 rpi context regions.
11742 *
11743 * Return codes
af901ca1 11744 * 0 - successful
6fb120a7
JS
11745 * ENOMEM - No available memory
11746 * EIO - The mailbox failed to complete successfully.
11747 **/
11748int
11749lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11750{
11751 LPFC_MBOXQ_t *mboxq;
11752 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11753 uint32_t rc = 0;
11754 uint32_t mbox_tmo;
11755 uint32_t shdr_status, shdr_add_status;
11756 union lpfc_sli4_cfg_shdr *shdr;
11757
11758 /* The port is notified of the header region via a mailbox command. */
11759 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11760 if (!mboxq) {
11761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11762 "2001 Unable to allocate memory for issuing "
11763 "SLI_CONFIG_SPECIAL mailbox command\n");
11764 return -ENOMEM;
11765 }
11766
11767 /* Post all rpi memory regions to the port. */
11768 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11769 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11770 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11771 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11772 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11773 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11774 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11775 hdr_tmpl, rpi_page->page_count);
11776 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11777 rpi_page->start_rpi);
11778 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11779 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11780 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11781 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11782 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11783 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11784 if (rc != MBX_TIMEOUT)
11785 mempool_free(mboxq, phba->mbox_mem_pool);
11786 if (shdr_status || shdr_add_status || rc) {
11787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11788 "2514 POST_RPI_HDR mailbox failed with "
11789 "status x%x add_status x%x, mbx status x%x\n",
11790 shdr_status, shdr_add_status, rc);
11791 rc = -ENXIO;
11792 }
11793 return rc;
11794}
11795
11796/**
11797 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11798 * @phba: pointer to lpfc hba data structure.
11799 *
11800 * This routine is invoked to post rpi header templates to the
11801 * HBA consistent with the SLI-4 interface spec. This routine
11802 * posts a PAGE_SIZE memory region to the port to hold up to
11803 * PAGE_SIZE modulo 64 rpi context headers.
11804 *
11805 * Returns
af901ca1 11806 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
11807 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11808 **/
11809int
11810lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11811{
11812 int rpi;
11813 uint16_t max_rpi, rpi_base, rpi_limit;
11814 uint16_t rpi_remaining;
11815 struct lpfc_rpi_hdr *rpi_hdr;
11816
11817 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11818 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11819 rpi_limit = phba->sli4_hba.next_rpi;
11820
11821 /*
11822 * The valid rpi range is not guaranteed to be zero-based. Start
11823 * the search at the rpi_base as reported by the port.
11824 */
11825 spin_lock_irq(&phba->hbalock);
11826 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11827 if (rpi >= rpi_limit || rpi < rpi_base)
11828 rpi = LPFC_RPI_ALLOC_ERROR;
11829 else {
11830 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11831 phba->sli4_hba.max_cfg_param.rpi_used++;
11832 phba->sli4_hba.rpi_count++;
11833 }
11834
11835 /*
11836 * Don't try to allocate more rpi header regions if the device limit
11837 * on available rpis max has been exhausted.
11838 */
11839 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11840 (phba->sli4_hba.rpi_count >= max_rpi)) {
11841 spin_unlock_irq(&phba->hbalock);
11842 return rpi;
11843 }
11844
11845 /*
11846 * If the driver is running low on rpi resources, allocate another
11847 * page now. Note that the next_rpi value is used because
11848 * it represents how many are actually in use whereas max_rpi notes
11849 * how many are supported max by the device.
11850 */
11851 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11852 phba->sli4_hba.rpi_count;
11853 spin_unlock_irq(&phba->hbalock);
11854 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11855 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11856 if (!rpi_hdr) {
11857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11858 "2002 Error Could not grow rpi "
11859 "count\n");
11860 } else {
11861 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11862 }
11863 }
11864
11865 return rpi;
11866}
11867
11868/**
11869 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11870 * @phba: pointer to lpfc hba data structure.
11871 *
11872 * This routine is invoked to release an rpi to the pool of
11873 * available rpis maintained by the driver.
11874 **/
11875void
11876lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11877{
11878 spin_lock_irq(&phba->hbalock);
11879 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11880 phba->sli4_hba.rpi_count--;
11881 phba->sli4_hba.max_cfg_param.rpi_used--;
11882 spin_unlock_irq(&phba->hbalock);
11883}
11884
11885/**
11886 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11887 * @phba: pointer to lpfc hba data structure.
11888 *
11889 * This routine is invoked to remove the memory region that
11890 * provided rpi via a bitmask.
11891 **/
11892void
11893lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11894{
11895 kfree(phba->sli4_hba.rpi_bmask);
11896}
11897
11898/**
11899 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11900 * @phba: pointer to lpfc hba data structure.
11901 *
11902 * This routine is invoked to remove the memory region that
11903 * provided rpi via a bitmask.
11904 **/
11905int
11906lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11907{
11908 LPFC_MBOXQ_t *mboxq;
11909 struct lpfc_hba *phba = ndlp->phba;
11910 int rc;
11911
11912 /* The port is notified of the header region via a mailbox command. */
11913 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11914 if (!mboxq)
11915 return -ENOMEM;
11916
11917 /* Post all rpi memory regions to the port. */
11918 lpfc_resume_rpi(mboxq, ndlp);
11919 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11920 if (rc == MBX_NOT_FINISHED) {
11921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11922 "2010 Resume RPI Mailbox failed "
11923 "status %d, mbxStatus x%x\n", rc,
11924 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11925 mempool_free(mboxq, phba->mbox_mem_pool);
11926 return -EIO;
11927 }
11928 return 0;
11929}
11930
11931/**
11932 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11933 * @phba: pointer to lpfc hba data structure.
11934 * @vpi: vpi value to activate with the port.
11935 *
11936 * This routine is invoked to activate a vpi with the
11937 * port when the host intends to use vports with a
11938 * nonzero vpi.
11939 *
11940 * Returns:
11941 * 0 success
11942 * -Evalue otherwise
11943 **/
11944int
11945lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11946{
11947 LPFC_MBOXQ_t *mboxq;
11948 int rc = 0;
6a9c52cf 11949 int retval = MBX_SUCCESS;
6fb120a7
JS
11950 uint32_t mbox_tmo;
11951
11952 if (vpi == 0)
11953 return -EINVAL;
11954 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11955 if (!mboxq)
11956 return -ENOMEM;
1c6834a7 11957 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
11958 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11959 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
11960 if (rc != MBX_SUCCESS) {
11961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11962 "2022 INIT VPI Mailbox failed "
11963 "status %d, mbxStatus x%x\n", rc,
11964 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 11965 retval = -EIO;
6fb120a7 11966 }
6a9c52cf
JS
11967 if (rc != MBX_TIMEOUT)
11968 mempool_free(mboxq, phba->mbox_mem_pool);
11969
11970 return retval;
6fb120a7
JS
11971}
11972
11973/**
11974 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11975 * @phba: pointer to lpfc hba data structure.
11976 * @mboxq: Pointer to mailbox object.
11977 *
11978 * This routine is invoked to manually add a single FCF record. The caller
11979 * must pass a completely initialized FCF_Record. This routine takes
11980 * care of the nonembedded mailbox operations.
11981 **/
11982static void
11983lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11984{
11985 void *virt_addr;
11986 union lpfc_sli4_cfg_shdr *shdr;
11987 uint32_t shdr_status, shdr_add_status;
11988
11989 virt_addr = mboxq->sge_array->addr[0];
11990 /* The IOCTL status is embedded in the mailbox subheader. */
11991 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11992 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11993 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11994
11995 if ((shdr_status || shdr_add_status) &&
11996 (shdr_status != STATUS_FCF_IN_USE))
11997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11998 "2558 ADD_FCF_RECORD mailbox failed with "
11999 "status x%x add_status x%x\n",
12000 shdr_status, shdr_add_status);
12001
12002 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12003}
12004
12005/**
12006 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
12007 * @phba: pointer to lpfc hba data structure.
12008 * @fcf_record: pointer to the initialized fcf record to add.
12009 *
12010 * This routine is invoked to manually add a single FCF record. The caller
12011 * must pass a completely initialized FCF_Record. This routine takes
12012 * care of the nonembedded mailbox operations.
12013 **/
12014int
12015lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
12016{
12017 int rc = 0;
12018 LPFC_MBOXQ_t *mboxq;
12019 uint8_t *bytep;
12020 void *virt_addr;
12021 dma_addr_t phys_addr;
12022 struct lpfc_mbx_sge sge;
12023 uint32_t alloc_len, req_len;
12024 uint32_t fcfindex;
12025
12026 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12027 if (!mboxq) {
12028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12029 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
12030 return -ENOMEM;
12031 }
12032
12033 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
12034 sizeof(uint32_t);
12035
12036 /* Allocate DMA memory and set up the non-embedded mailbox command */
12037 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
12038 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
12039 req_len, LPFC_SLI4_MBX_NEMBED);
12040 if (alloc_len < req_len) {
12041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12042 "2523 Allocated DMA memory size (x%x) is "
12043 "less than the requested DMA memory "
12044 "size (x%x)\n", alloc_len, req_len);
12045 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12046 return -ENOMEM;
12047 }
12048
12049 /*
12050 * Get the first SGE entry from the non-embedded DMA memory. This
12051 * routine only uses a single SGE.
12052 */
12053 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
12054 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
12055 virt_addr = mboxq->sge_array->addr[0];
12056 /*
12057 * Configure the FCF record for FCFI 0. This is the driver's
12058 * hardcoded default and gets used in nonFIP mode.
12059 */
12060 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
12061 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
12062 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
12063
12064 /*
12065 * Copy the fcf_index and the FCF Record Data. The data starts after
12066 * the FCoE header plus word10. The data copy needs to be endian
12067 * correct.
12068 */
12069 bytep += sizeof(uint32_t);
12070 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
12071 mboxq->vport = phba->pport;
12072 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
12073 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12074 if (rc == MBX_NOT_FINISHED) {
12075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12076 "2515 ADD_FCF_RECORD mailbox failed with "
12077 "status 0x%x\n", rc);
12078 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12079 rc = -EIO;
12080 } else
12081 rc = 0;
12082
12083 return rc;
12084}
12085
12086/**
12087 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
12088 * @phba: pointer to lpfc hba data structure.
12089 * @fcf_record: pointer to the fcf record to write the default data.
12090 * @fcf_index: FCF table entry index.
12091 *
12092 * This routine is invoked to build the driver's default FCF record. The
12093 * values used are hardcoded. This routine handles memory initialization.
12094 *
12095 **/
12096void
12097lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
12098 struct fcf_record *fcf_record,
12099 uint16_t fcf_index)
12100{
12101 memset(fcf_record, 0, sizeof(struct fcf_record));
12102 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
12103 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
12104 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
12105 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
12106 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
12107 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
12108 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
12109 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
12110 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
12111 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
12112 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
12113 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
12114 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 12115 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
12116 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
12117 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
12118 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
12119 /* Set the VLAN bit map */
12120 if (phba->valid_vlan) {
12121 fcf_record->vlan_bitmap[phba->vlan_id / 8]
12122 = 1 << (phba->vlan_id % 8);
12123 }
12124}
12125
12126/**
0c9ab6f5 12127 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
12128 * @phba: pointer to lpfc hba data structure.
12129 * @fcf_index: FCF table entry offset.
12130 *
0c9ab6f5
JS
12131 * This routine is invoked to scan the entire FCF table by reading FCF
12132 * record and processing it one at a time starting from the @fcf_index
12133 * for initial FCF discovery or fast FCF failover rediscovery.
12134 *
12135 * Return 0 if the mailbox command is submitted sucessfully, none 0
12136 * otherwise.
6fb120a7
JS
12137 **/
12138int
0c9ab6f5 12139lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
12140{
12141 int rc = 0, error;
12142 LPFC_MBOXQ_t *mboxq;
6fb120a7 12143
32b9793f 12144 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
12145 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12146 if (!mboxq) {
12147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12148 "2000 Failed to allocate mbox for "
12149 "READ_FCF cmd\n");
4d9ab994 12150 error = -ENOMEM;
0c9ab6f5 12151 goto fail_fcf_scan;
6fb120a7 12152 }
ecfd03c6 12153 /* Construct the read FCF record mailbox command */
0c9ab6f5 12154 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
12155 if (rc) {
12156 error = -EINVAL;
0c9ab6f5 12157 goto fail_fcf_scan;
6fb120a7 12158 }
ecfd03c6 12159 /* Issue the mailbox command asynchronously */
6fb120a7 12160 mboxq->vport = phba->pport;
0c9ab6f5 12161 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
6fb120a7 12162 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 12163 if (rc == MBX_NOT_FINISHED)
6fb120a7 12164 error = -EIO;
ecfd03c6 12165 else {
32b9793f
JS
12166 spin_lock_irq(&phba->hbalock);
12167 phba->hba_flag |= FCF_DISC_INPROGRESS;
12168 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 12169 /* Reset FCF round robin index bmask for new scan */
999d813f 12170 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST) {
0c9ab6f5
JS
12171 memset(phba->fcf.fcf_rr_bmask, 0,
12172 sizeof(*phba->fcf.fcf_rr_bmask));
999d813f
JS
12173 phba->fcf.eligible_fcf_cnt = 0;
12174 }
6fb120a7 12175 error = 0;
32b9793f 12176 }
0c9ab6f5 12177fail_fcf_scan:
4d9ab994
JS
12178 if (error) {
12179 if (mboxq)
12180 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12181 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12182 spin_lock_irq(&phba->hbalock);
12183 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12184 spin_unlock_irq(&phba->hbalock);
12185 }
6fb120a7
JS
12186 return error;
12187}
a0c87cbd 12188
0c9ab6f5
JS
12189/**
12190 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for round robin fcf.
12191 * @phba: pointer to lpfc hba data structure.
12192 * @fcf_index: FCF table entry offset.
12193 *
12194 * This routine is invoked to read an FCF record indicated by @fcf_index
12195 * and to use it for FLOGI round robin FCF failover.
12196 *
12197 * Return 0 if the mailbox command is submitted sucessfully, none 0
12198 * otherwise.
12199 **/
12200int
12201lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12202{
12203 int rc = 0, error;
12204 LPFC_MBOXQ_t *mboxq;
12205
12206 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12207 if (!mboxq) {
12208 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12209 "2763 Failed to allocate mbox for "
12210 "READ_FCF cmd\n");
12211 error = -ENOMEM;
12212 goto fail_fcf_read;
12213 }
12214 /* Construct the read FCF record mailbox command */
12215 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12216 if (rc) {
12217 error = -EINVAL;
12218 goto fail_fcf_read;
12219 }
12220 /* Issue the mailbox command asynchronously */
12221 mboxq->vport = phba->pport;
12222 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
12223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12224 if (rc == MBX_NOT_FINISHED)
12225 error = -EIO;
12226 else
12227 error = 0;
12228
12229fail_fcf_read:
12230 if (error && mboxq)
12231 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12232 return error;
12233}
12234
12235/**
12236 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
12237 * @phba: pointer to lpfc hba data structure.
12238 * @fcf_index: FCF table entry offset.
12239 *
12240 * This routine is invoked to read an FCF record indicated by @fcf_index to
12241 * determine whether it's eligible for FLOGI round robin failover list.
12242 *
12243 * Return 0 if the mailbox command is submitted sucessfully, none 0
12244 * otherwise.
12245 **/
12246int
12247lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
12248{
12249 int rc = 0, error;
12250 LPFC_MBOXQ_t *mboxq;
12251
12252 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12253 if (!mboxq) {
12254 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
12255 "2758 Failed to allocate mbox for "
12256 "READ_FCF cmd\n");
12257 error = -ENOMEM;
12258 goto fail_fcf_read;
12259 }
12260 /* Construct the read FCF record mailbox command */
12261 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
12262 if (rc) {
12263 error = -EINVAL;
12264 goto fail_fcf_read;
12265 }
12266 /* Issue the mailbox command asynchronously */
12267 mboxq->vport = phba->pport;
12268 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
12269 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
12270 if (rc == MBX_NOT_FINISHED)
12271 error = -EIO;
12272 else
12273 error = 0;
12274
12275fail_fcf_read:
12276 if (error && mboxq)
12277 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12278 return error;
12279}
12280
12281/**
12282 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
12283 * @phba: pointer to lpfc hba data structure.
12284 *
12285 * This routine is to get the next eligible FCF record index in a round
12286 * robin fashion. If the next eligible FCF record index equals to the
12287 * initial round robin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
12288 * shall be returned, otherwise, the next eligible FCF record's index
12289 * shall be returned.
12290 **/
12291uint16_t
12292lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
12293{
12294 uint16_t next_fcf_index;
12295
12296 /* Search from the currently registered FCF index */
12297 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12298 LPFC_SLI4_FCF_TBL_INDX_MAX,
12299 phba->fcf.current_rec.fcf_indx);
12300 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
12301 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
12302 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
12303 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
12304 /* Round robin failover stop condition */
12305 if (next_fcf_index == phba->fcf.fcf_rr_init_indx)
12306 return LPFC_FCOE_FCF_NEXT_NONE;
12307
12308 return next_fcf_index;
12309}
12310
12311/**
12312 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
12313 * @phba: pointer to lpfc hba data structure.
12314 *
12315 * This routine sets the FCF record index in to the eligible bmask for
12316 * round robin failover search. It checks to make sure that the index
12317 * does not go beyond the range of the driver allocated bmask dimension
12318 * before setting the bit.
12319 *
12320 * Returns 0 if the index bit successfully set, otherwise, it returns
12321 * -EINVAL.
12322 **/
12323int
12324lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
12325{
12326 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12327 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12328 "2610 HBA FCF index reached driver's "
12329 "book keeping dimension: fcf_index:%d, "
12330 "driver_bmask_max:%d\n",
12331 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12332 return -EINVAL;
12333 }
12334 /* Set the eligible FCF record index bmask */
12335 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12336
12337 return 0;
12338}
12339
12340/**
12341 * lpfc_sli4_fcf_rr_index_set - Clear bmask from eligible fcf record index
12342 * @phba: pointer to lpfc hba data structure.
12343 *
12344 * This routine clears the FCF record index from the eligible bmask for
12345 * round robin failover search. It checks to make sure that the index
12346 * does not go beyond the range of the driver allocated bmask dimension
12347 * before clearing the bit.
12348 **/
12349void
12350lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
12351{
12352 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
12353 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
12354 "2762 HBA FCF index goes beyond driver's "
12355 "book keeping dimension: fcf_index:%d, "
12356 "driver_bmask_max:%d\n",
12357 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
12358 return;
12359 }
12360 /* Clear the eligible FCF record index bmask */
12361 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
12362}
12363
ecfd03c6
JS
12364/**
12365 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12366 * @phba: pointer to lpfc hba data structure.
12367 *
12368 * This routine is the completion routine for the rediscover FCF table mailbox
12369 * command. If the mailbox command returned failure, it will try to stop the
12370 * FCF rediscover wait timer.
12371 **/
12372void
12373lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12374{
12375 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12376 uint32_t shdr_status, shdr_add_status;
12377
12378 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12379
12380 shdr_status = bf_get(lpfc_mbox_hdr_status,
12381 &redisc_fcf->header.cfg_shdr.response);
12382 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12383 &redisc_fcf->header.cfg_shdr.response);
12384 if (shdr_status || shdr_add_status) {
0c9ab6f5 12385 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
12386 "2746 Requesting for FCF rediscovery failed "
12387 "status x%x add_status x%x\n",
12388 shdr_status, shdr_add_status);
0c9ab6f5 12389 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 12390 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12391 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
12392 spin_unlock_irq(&phba->hbalock);
12393 /*
12394 * CVL event triggered FCF rediscover request failed,
12395 * last resort to re-try current registered FCF entry.
12396 */
12397 lpfc_retry_pport_discovery(phba);
12398 } else {
12399 spin_lock_irq(&phba->hbalock);
0c9ab6f5 12400 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
12401 spin_unlock_irq(&phba->hbalock);
12402 /*
12403 * DEAD FCF event triggered FCF rediscover request
12404 * failed, last resort to fail over as a link down
12405 * to FCF registration.
12406 */
12407 lpfc_sli4_fcf_dead_failthrough(phba);
12408 }
0c9ab6f5
JS
12409 } else {
12410 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
12411 "2775 Start FCF rediscovery quiescent period "
12412 "wait timer before scaning FCF table\n");
ecfd03c6
JS
12413 /*
12414 * Start FCF rediscovery wait timer for pending FCF
12415 * before rescan FCF record table.
12416 */
12417 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 12418 }
ecfd03c6
JS
12419
12420 mempool_free(mbox, phba->mbox_mem_pool);
12421}
12422
12423/**
12424 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12425 * @phba: pointer to lpfc hba data structure.
12426 *
12427 * This routine is invoked to request for rediscovery of the entire FCF table
12428 * by the port.
12429 **/
12430int
12431lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12432{
12433 LPFC_MBOXQ_t *mbox;
12434 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12435 int rc, length;
12436
0c9ab6f5
JS
12437 /* Cancel retry delay timers to all vports before FCF rediscover */
12438 lpfc_cancel_all_vport_retry_delay_timer(phba);
12439
ecfd03c6
JS
12440 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12441 if (!mbox) {
12442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12443 "2745 Failed to allocate mbox for "
12444 "requesting FCF rediscover.\n");
12445 return -ENOMEM;
12446 }
12447
12448 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12449 sizeof(struct lpfc_sli4_cfg_mhdr));
12450 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12451 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12452 length, LPFC_SLI4_MBX_EMBED);
12453
12454 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12455 /* Set count to 0 for invalidating the entire FCF database */
12456 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12457
12458 /* Issue the mailbox command asynchronously */
12459 mbox->vport = phba->pport;
12460 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12461 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12462
12463 if (rc == MBX_NOT_FINISHED) {
12464 mempool_free(mbox, phba->mbox_mem_pool);
12465 return -EIO;
12466 }
12467 return 0;
12468}
12469
fc2b989b
JS
12470/**
12471 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12472 * @phba: pointer to lpfc hba data structure.
12473 *
12474 * This function is the failover routine as a last resort to the FCF DEAD
12475 * event when driver failed to perform fast FCF failover.
12476 **/
12477void
12478lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12479{
12480 uint32_t link_state;
12481
12482 /*
12483 * Last resort as FCF DEAD event failover will treat this as
12484 * a link down, but save the link state because we don't want
12485 * it to be changed to Link Down unless it is already down.
12486 */
12487 link_state = phba->link_state;
12488 lpfc_linkdown(phba);
12489 phba->link_state = link_state;
12490
12491 /* Unregister FCF if no devices connected to it */
12492 lpfc_unregister_unused_fcf(phba);
12493}
12494
a0c87cbd
JS
12495/**
12496 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12497 * @phba: pointer to lpfc hba data structure.
12498 *
12499 * This function read region 23 and parse TLV for port status to
12500 * decide if the user disaled the port. If the TLV indicates the
12501 * port is disabled, the hba_flag is set accordingly.
12502 **/
12503void
12504lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12505{
12506 LPFC_MBOXQ_t *pmb = NULL;
12507 MAILBOX_t *mb;
12508 uint8_t *rgn23_data = NULL;
12509 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12510 int rc;
12511
12512 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12513 if (!pmb) {
12514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12515 "2600 lpfc_sli_read_serdes_param failed to"
12516 " allocate mailbox memory\n");
12517 goto out;
12518 }
12519 mb = &pmb->u.mb;
12520
12521 /* Get adapter Region 23 data */
12522 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12523 if (!rgn23_data)
12524 goto out;
12525
12526 do {
12527 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12528 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12529
12530 if (rc != MBX_SUCCESS) {
12531 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12532 "2601 lpfc_sli_read_link_ste failed to"
12533 " read config region 23 rc 0x%x Status 0x%x\n",
12534 rc, mb->mbxStatus);
12535 mb->un.varDmp.word_cnt = 0;
12536 }
12537 /*
12538 * dump mem may return a zero when finished or we got a
12539 * mailbox error, either way we are done.
12540 */
12541 if (mb->un.varDmp.word_cnt == 0)
12542 break;
12543 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12544 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12545
12546 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12547 rgn23_data + offset,
12548 mb->un.varDmp.word_cnt);
12549 offset += mb->un.varDmp.word_cnt;
12550 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12551
12552 data_size = offset;
12553 offset = 0;
12554
12555 if (!data_size)
12556 goto out;
12557
12558 /* Check the region signature first */
12559 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12561 "2619 Config region 23 has bad signature\n");
12562 goto out;
12563 }
12564 offset += 4;
12565
12566 /* Check the data structure version */
12567 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12569 "2620 Config region 23 has bad version\n");
12570 goto out;
12571 }
12572 offset += 4;
12573
12574 /* Parse TLV entries in the region */
12575 while (offset < data_size) {
12576 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12577 break;
12578 /*
12579 * If the TLV is not driver specific TLV or driver id is
12580 * not linux driver id, skip the record.
12581 */
12582 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12583 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12584 (rgn23_data[offset + 3] != 0)) {
12585 offset += rgn23_data[offset + 1] * 4 + 4;
12586 continue;
12587 }
12588
12589 /* Driver found a driver specific TLV in the config region */
12590 sub_tlv_len = rgn23_data[offset + 1] * 4;
12591 offset += 4;
12592 tlv_offset = 0;
12593
12594 /*
12595 * Search for configured port state sub-TLV.
12596 */
12597 while ((offset < data_size) &&
12598 (tlv_offset < sub_tlv_len)) {
12599 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12600 offset += 4;
12601 tlv_offset += 4;
12602 break;
12603 }
12604 if (rgn23_data[offset] != PORT_STE_TYPE) {
12605 offset += rgn23_data[offset + 1] * 4 + 4;
12606 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12607 continue;
12608 }
12609
12610 /* This HBA contains PORT_STE configured */
12611 if (!rgn23_data[offset + 2])
12612 phba->hba_flag |= LINK_DISABLED;
12613
12614 goto out;
12615 }
12616 }
12617out:
12618 if (pmb)
12619 mempool_free(pmb, phba->mbox_mem_pool);
12620 kfree(rgn23_data);
12621 return;
12622}
695a814e
JS
12623
12624/**
12625 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12626 * @vport: pointer to vport data structure.
12627 *
12628 * This function iterate through the mailboxq and clean up all REG_LOGIN
12629 * and REG_VPI mailbox commands associated with the vport. This function
12630 * is called when driver want to restart discovery of the vport due to
12631 * a Clear Virtual Link event.
12632 **/
12633void
12634lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12635{
12636 struct lpfc_hba *phba = vport->phba;
12637 LPFC_MBOXQ_t *mb, *nextmb;
12638 struct lpfc_dmabuf *mp;
12639
12640 spin_lock_irq(&phba->hbalock);
12641 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12642 if (mb->vport != vport)
12643 continue;
12644
12645 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12646 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12647 continue;
12648
12649 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12650 mp = (struct lpfc_dmabuf *) (mb->context1);
12651 if (mp) {
12652 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12653 kfree(mp);
12654 }
12655 }
12656 list_del(&mb->list);
12657 mempool_free(mb, phba->mbox_mem_pool);
12658 }
12659 mb = phba->sli.mbox_active;
12660 if (mb && (mb->vport == vport)) {
12661 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12662 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12663 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12664 }
12665 spin_unlock_irq(&phba->hbalock);
12666}
12667