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