mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / scsi / lpfc / lpfc_sli.c
blob2d1ffd157c2812050b87a8614733c348dd9df0c0
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
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. *
20 *******************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
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"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
72 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
75 static IOCB_t *
76 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
78 return &iocbq->iocb;
81 /**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93 static uint32_t
94 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
96 union lpfc_wqe *temp_wqe;
97 struct lpfc_register doorbell;
98 uint32_t host_index;
99 uint32_t idx;
101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
106 /* If the host has not yet processed the next entry then we are done */
107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
109 q->WQ_overflow++;
110 return -ENOMEM;
112 q->WQ_posted++;
113 /* set consumption flag every once in a while */
114 if (!((q->host_index + 1) % q->entry_repost))
115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
123 q->host_index = idx;
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
127 if (q->db_format == LPFC_DB_LIST_FORMAT) {
128 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
129 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
130 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
131 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
132 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
134 } else {
135 return -EINVAL;
137 writel(doorbell.word0, q->db_regaddr);
139 return 0;
143 * lpfc_sli4_wq_release - Updates internal hba index for WQ
144 * @q: The Work Queue to operate on.
145 * @index: The index to advance the hba index to.
147 * This routine will update the HBA index of a queue to reflect consumption of
148 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
149 * an entry the host calls this function to update the queue's internal
150 * pointers. This routine returns the number of entries that were consumed by
151 * the HBA.
153 static uint32_t
154 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
156 uint32_t released = 0;
158 /* sanity check on queue memory */
159 if (unlikely(!q))
160 return 0;
162 if (q->hba_index == index)
163 return 0;
164 do {
165 q->hba_index = ((q->hba_index + 1) % q->entry_count);
166 released++;
167 } while (q->hba_index != index);
168 return released;
172 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
173 * @q: The Mailbox Queue to operate on.
174 * @wqe: The Mailbox Queue Entry to put on the Work queue.
176 * This routine will copy the contents of @mqe to the next available entry on
177 * the @q. This function will then ring the Work Queue Doorbell to signal the
178 * HBA to start processing the Work Queue Entry. This function returns 0 if
179 * successful. If no entries are available on @q then this function will return
180 * -ENOMEM.
181 * The caller is expected to hold the hbalock when calling this routine.
183 static uint32_t
184 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
186 struct lpfc_mqe *temp_mqe;
187 struct lpfc_register doorbell;
188 uint32_t host_index;
190 /* sanity check on queue memory */
191 if (unlikely(!q))
192 return -ENOMEM;
193 temp_mqe = q->qe[q->host_index].mqe;
195 /* If the host has not yet processed the next entry then we are done */
196 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
197 return -ENOMEM;
198 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
199 /* Save off the mailbox pointer for completion */
200 q->phba->mbox = (MAILBOX_t *)temp_mqe;
202 /* Update the host index before invoking device */
203 host_index = q->host_index;
204 q->host_index = ((q->host_index + 1) % q->entry_count);
206 /* Ring Doorbell */
207 doorbell.word0 = 0;
208 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
209 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
210 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
211 return 0;
215 * lpfc_sli4_mq_release - Updates internal hba index for MQ
216 * @q: The Mailbox Queue to operate on.
218 * This routine will update the HBA index of a queue to reflect consumption of
219 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
220 * an entry the host calls this function to update the queue's internal
221 * pointers. This routine returns the number of entries that were consumed by
222 * the HBA.
224 static uint32_t
225 lpfc_sli4_mq_release(struct lpfc_queue *q)
227 /* sanity check on queue memory */
228 if (unlikely(!q))
229 return 0;
231 /* Clear the mailbox pointer for completion */
232 q->phba->mbox = NULL;
233 q->hba_index = ((q->hba_index + 1) % q->entry_count);
234 return 1;
238 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
239 * @q: The Event Queue to get the first valid EQE from
241 * This routine will get the first valid Event Queue Entry from @q, update
242 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
243 * the Queue (no more work to do), or the Queue is full of EQEs that have been
244 * processed, but not popped back to the HBA then this routine will return NULL.
246 static struct lpfc_eqe *
247 lpfc_sli4_eq_get(struct lpfc_queue *q)
249 struct lpfc_eqe *eqe;
250 uint32_t idx;
252 /* sanity check on queue memory */
253 if (unlikely(!q))
254 return NULL;
255 eqe = q->qe[q->hba_index].eqe;
257 /* If the next EQE is not valid then we are done */
258 if (!bf_get_le32(lpfc_eqe_valid, eqe))
259 return NULL;
260 /* If the host has not yet processed the next entry then we are done */
261 idx = ((q->hba_index + 1) % q->entry_count);
262 if (idx == q->host_index)
263 return NULL;
265 q->hba_index = idx;
268 * insert barrier for instruction interlock : data from the hardware
269 * must have the valid bit checked before it can be copied and acted
270 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
271 * instructions allowing action on content before valid bit checked,
272 * add barrier here as well. May not be needed as "content" is a
273 * single 32-bit entity here (vs multi word structure for cq's).
275 mb();
276 return eqe;
280 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
281 * @q: The Event Queue to disable interrupts
284 static inline void
285 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
287 struct lpfc_register doorbell;
289 doorbell.word0 = 0;
290 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
291 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
292 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
293 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
294 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
295 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
299 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
300 * @q: The Event Queue that the host has completed processing for.
301 * @arm: Indicates whether the host wants to arms this CQ.
303 * This routine will mark all Event Queue Entries on @q, from the last
304 * known completed entry to the last entry that was processed, as completed
305 * by clearing the valid bit for each completion queue entry. Then it will
306 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
307 * The internal host index in the @q will be updated by this routine to indicate
308 * that the host has finished processing the entries. The @arm parameter
309 * indicates that the queue should be rearmed when ringing the doorbell.
311 * This function will return the number of EQEs that were popped.
313 uint32_t
314 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
316 uint32_t released = 0;
317 struct lpfc_eqe *temp_eqe;
318 struct lpfc_register doorbell;
320 /* sanity check on queue memory */
321 if (unlikely(!q))
322 return 0;
324 /* while there are valid entries */
325 while (q->hba_index != q->host_index) {
326 temp_eqe = q->qe[q->host_index].eqe;
327 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
328 released++;
329 q->host_index = ((q->host_index + 1) % q->entry_count);
331 if (unlikely(released == 0 && !arm))
332 return 0;
334 /* ring doorbell for number popped */
335 doorbell.word0 = 0;
336 if (arm) {
337 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
338 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
340 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
341 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
342 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
343 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
344 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
345 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
346 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
347 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
348 readl(q->phba->sli4_hba.EQCQDBregaddr);
349 return released;
353 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
354 * @q: The Completion Queue to get the first valid CQE from
356 * This routine will get the first valid Completion Queue Entry from @q, update
357 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
358 * the Queue (no more work to do), or the Queue is full of CQEs that have been
359 * processed, but not popped back to the HBA then this routine will return NULL.
361 static struct lpfc_cqe *
362 lpfc_sli4_cq_get(struct lpfc_queue *q)
364 struct lpfc_cqe *cqe;
365 uint32_t idx;
367 /* sanity check on queue memory */
368 if (unlikely(!q))
369 return NULL;
371 /* If the next CQE is not valid then we are done */
372 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
373 return NULL;
374 /* If the host has not yet processed the next entry then we are done */
375 idx = ((q->hba_index + 1) % q->entry_count);
376 if (idx == q->host_index)
377 return NULL;
379 cqe = q->qe[q->hba_index].cqe;
380 q->hba_index = idx;
383 * insert barrier for instruction interlock : data from the hardware
384 * must have the valid bit checked before it can be copied and acted
385 * upon. Speculative instructions were allowing a bcopy at the start
386 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
387 * after our return, to copy data before the valid bit check above
388 * was done. As such, some of the copied data was stale. The barrier
389 * ensures the check is before any data is copied.
391 mb();
392 return cqe;
396 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
397 * @q: The Completion Queue that the host has completed processing for.
398 * @arm: Indicates whether the host wants to arms this CQ.
400 * This routine will mark all Completion queue entries on @q, from the last
401 * known completed entry to the last entry that was processed, as completed
402 * by clearing the valid bit for each completion queue entry. Then it will
403 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
404 * The internal host index in the @q will be updated by this routine to indicate
405 * that the host has finished processing the entries. The @arm parameter
406 * indicates that the queue should be rearmed when ringing the doorbell.
408 * This function will return the number of CQEs that were released.
410 uint32_t
411 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
413 uint32_t released = 0;
414 struct lpfc_cqe *temp_qe;
415 struct lpfc_register doorbell;
417 /* sanity check on queue memory */
418 if (unlikely(!q))
419 return 0;
420 /* while there are valid entries */
421 while (q->hba_index != q->host_index) {
422 temp_qe = q->qe[q->host_index].cqe;
423 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
424 released++;
425 q->host_index = ((q->host_index + 1) % q->entry_count);
427 if (unlikely(released == 0 && !arm))
428 return 0;
430 /* ring doorbell for number popped */
431 doorbell.word0 = 0;
432 if (arm)
433 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
434 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
435 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
436 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
437 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
438 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
439 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
440 return released;
444 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
445 * @q: The Header Receive Queue to operate on.
446 * @wqe: The Receive Queue Entry to put on the Receive queue.
448 * This routine will copy the contents of @wqe to the next available entry on
449 * the @q. This function will then ring the Receive Queue Doorbell to signal the
450 * HBA to start processing the Receive Queue Entry. This function returns the
451 * index that the rqe was copied to if successful. If no entries are available
452 * on @q then this function will return -ENOMEM.
453 * The caller is expected to hold the hbalock when calling this routine.
455 static int
456 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
457 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
459 struct lpfc_rqe *temp_hrqe;
460 struct lpfc_rqe *temp_drqe;
461 struct lpfc_register doorbell;
462 int put_index;
464 /* sanity check on queue memory */
465 if (unlikely(!hq) || unlikely(!dq))
466 return -ENOMEM;
467 put_index = hq->host_index;
468 temp_hrqe = hq->qe[hq->host_index].rqe;
469 temp_drqe = dq->qe[dq->host_index].rqe;
471 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
472 return -EINVAL;
473 if (hq->host_index != dq->host_index)
474 return -EINVAL;
475 /* If the host has not yet processed the next entry then we are done */
476 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
477 return -EBUSY;
478 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
479 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
481 /* Update the host index to point to the next slot */
482 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
483 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
485 /* Ring The Header Receive Queue Doorbell */
486 if (!(hq->host_index % hq->entry_repost)) {
487 doorbell.word0 = 0;
488 if (hq->db_format == LPFC_DB_RING_FORMAT) {
489 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
490 hq->entry_repost);
491 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
492 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
493 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
494 hq->entry_repost);
495 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
496 hq->host_index);
497 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
498 } else {
499 return -EINVAL;
501 writel(doorbell.word0, hq->db_regaddr);
503 return put_index;
507 * lpfc_sli4_rq_release - Updates internal hba index for RQ
508 * @q: The Header Receive Queue to operate on.
510 * This routine will update the HBA index of a queue to reflect consumption of
511 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
512 * consumed an entry the host calls this function to update the queue's
513 * internal pointers. This routine returns the number of entries that were
514 * consumed by the HBA.
516 static uint32_t
517 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
519 /* sanity check on queue memory */
520 if (unlikely(!hq) || unlikely(!dq))
521 return 0;
523 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
524 return 0;
525 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
526 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
527 return 1;
531 * lpfc_cmd_iocb - Get next command iocb entry in the ring
532 * @phba: Pointer to HBA context object.
533 * @pring: Pointer to driver SLI ring object.
535 * This function returns pointer to next command iocb entry
536 * in the command ring. The caller must hold hbalock to prevent
537 * other threads consume the next command iocb.
538 * SLI-2/SLI-3 provide different sized iocbs.
540 static inline IOCB_t *
541 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
543 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
544 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
548 * lpfc_resp_iocb - Get next response iocb entry in the ring
549 * @phba: Pointer to HBA context object.
550 * @pring: Pointer to driver SLI ring object.
552 * This function returns pointer to next response iocb entry
553 * in the response ring. The caller must hold hbalock to make sure
554 * that no other thread consume the next response iocb.
555 * SLI-2/SLI-3 provide different sized iocbs.
557 static inline IOCB_t *
558 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
560 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
561 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
565 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
566 * @phba: Pointer to HBA context object.
568 * This function is called with hbalock held. This function
569 * allocates a new driver iocb object from the iocb pool. If the
570 * allocation is successful, it returns pointer to the newly
571 * allocated iocb object else it returns NULL.
573 struct lpfc_iocbq *
574 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
576 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
577 struct lpfc_iocbq * iocbq = NULL;
579 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
580 if (iocbq)
581 phba->iocb_cnt++;
582 if (phba->iocb_cnt > phba->iocb_max)
583 phba->iocb_max = phba->iocb_cnt;
584 return iocbq;
588 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
589 * @phba: Pointer to HBA context object.
590 * @xritag: XRI value.
592 * This function clears the sglq pointer from the array of acive
593 * sglq's. The xritag that is passed in is used to index into the
594 * array. Before the xritag can be used it needs to be adjusted
595 * by subtracting the xribase.
597 * Returns sglq ponter = success, NULL = Failure.
599 static struct lpfc_sglq *
600 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
602 struct lpfc_sglq *sglq;
604 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
605 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
606 return sglq;
610 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
611 * @phba: Pointer to HBA context object.
612 * @xritag: XRI value.
614 * This function returns the sglq pointer from the array of acive
615 * sglq's. The xritag that is passed in is used to index into the
616 * array. Before the xritag can be used it needs to be adjusted
617 * by subtracting the xribase.
619 * Returns sglq ponter = success, NULL = Failure.
621 struct lpfc_sglq *
622 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
624 struct lpfc_sglq *sglq;
626 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
627 return sglq;
631 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
632 * @phba: Pointer to HBA context object.
633 * @xritag: xri used in this exchange.
634 * @rrq: The RRQ to be cleared.
637 void
638 lpfc_clr_rrq_active(struct lpfc_hba *phba,
639 uint16_t xritag,
640 struct lpfc_node_rrq *rrq)
642 struct lpfc_nodelist *ndlp = NULL;
644 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
645 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
647 /* The target DID could have been swapped (cable swap)
648 * we should use the ndlp from the findnode if it is
649 * available.
651 if ((!ndlp) && rrq->ndlp)
652 ndlp = rrq->ndlp;
654 if (!ndlp)
655 goto out;
657 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
658 rrq->send_rrq = 0;
659 rrq->xritag = 0;
660 rrq->rrq_stop_time = 0;
662 out:
663 mempool_free(rrq, phba->rrq_pool);
667 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
668 * @phba: Pointer to HBA context object.
670 * This function is called with hbalock held. This function
671 * Checks if stop_time (ratov from setting rrq active) has
672 * been reached, if it has and the send_rrq flag is set then
673 * it will call lpfc_send_rrq. If the send_rrq flag is not set
674 * then it will just call the routine to clear the rrq and
675 * free the rrq resource.
676 * The timer is set to the next rrq that is going to expire before
677 * leaving the routine.
680 void
681 lpfc_handle_rrq_active(struct lpfc_hba *phba)
683 struct lpfc_node_rrq *rrq;
684 struct lpfc_node_rrq *nextrrq;
685 unsigned long next_time;
686 unsigned long iflags;
687 LIST_HEAD(send_rrq);
689 spin_lock_irqsave(&phba->hbalock, iflags);
690 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
691 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
692 list_for_each_entry_safe(rrq, nextrrq,
693 &phba->active_rrq_list, list) {
694 if (time_after(jiffies, rrq->rrq_stop_time))
695 list_move(&rrq->list, &send_rrq);
696 else if (time_before(rrq->rrq_stop_time, next_time))
697 next_time = rrq->rrq_stop_time;
699 spin_unlock_irqrestore(&phba->hbalock, iflags);
700 if (!list_empty(&phba->active_rrq_list))
701 mod_timer(&phba->rrq_tmr, next_time);
702 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
703 list_del(&rrq->list);
704 if (!rrq->send_rrq)
705 /* this call will free the rrq */
706 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
707 else if (lpfc_send_rrq(phba, rrq)) {
708 /* if we send the rrq then the completion handler
709 * will clear the bit in the xribitmap.
711 lpfc_clr_rrq_active(phba, rrq->xritag,
712 rrq);
718 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
719 * @vport: Pointer to vport context object.
720 * @xri: The xri used in the exchange.
721 * @did: The targets DID for this exchange.
723 * returns NULL = rrq not found in the phba->active_rrq_list.
724 * rrq = rrq for this xri and target.
726 struct lpfc_node_rrq *
727 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
729 struct lpfc_hba *phba = vport->phba;
730 struct lpfc_node_rrq *rrq;
731 struct lpfc_node_rrq *nextrrq;
732 unsigned long iflags;
734 if (phba->sli_rev != LPFC_SLI_REV4)
735 return NULL;
736 spin_lock_irqsave(&phba->hbalock, iflags);
737 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
738 if (rrq->vport == vport && rrq->xritag == xri &&
739 rrq->nlp_DID == did){
740 list_del(&rrq->list);
741 spin_unlock_irqrestore(&phba->hbalock, iflags);
742 return rrq;
745 spin_unlock_irqrestore(&phba->hbalock, iflags);
746 return NULL;
750 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
751 * @vport: Pointer to vport context object.
752 * @ndlp: Pointer to the lpfc_node_list structure.
753 * If ndlp is NULL Remove all active RRQs for this vport from the
754 * phba->active_rrq_list and clear the rrq.
755 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
757 void
758 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
761 struct lpfc_hba *phba = vport->phba;
762 struct lpfc_node_rrq *rrq;
763 struct lpfc_node_rrq *nextrrq;
764 unsigned long iflags;
765 LIST_HEAD(rrq_list);
767 if (phba->sli_rev != LPFC_SLI_REV4)
768 return;
769 if (!ndlp) {
770 lpfc_sli4_vport_delete_els_xri_aborted(vport);
771 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
773 spin_lock_irqsave(&phba->hbalock, iflags);
774 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
775 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
776 list_move(&rrq->list, &rrq_list);
777 spin_unlock_irqrestore(&phba->hbalock, iflags);
779 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
780 list_del(&rrq->list);
781 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
786 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
787 * @phba: Pointer to HBA context object.
789 * Remove all rrqs from the phba->active_rrq_list and free them by
790 * calling __lpfc_clr_active_rrq
793 void
794 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
796 struct lpfc_node_rrq *rrq;
797 struct lpfc_node_rrq *nextrrq;
798 unsigned long next_time;
799 unsigned long iflags;
800 LIST_HEAD(rrq_list);
802 if (phba->sli_rev != LPFC_SLI_REV4)
803 return;
804 spin_lock_irqsave(&phba->hbalock, iflags);
805 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
806 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2));
807 list_splice_init(&phba->active_rrq_list, &rrq_list);
808 spin_unlock_irqrestore(&phba->hbalock, iflags);
810 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
811 list_del(&rrq->list);
812 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
814 if (!list_empty(&phba->active_rrq_list))
815 mod_timer(&phba->rrq_tmr, next_time);
820 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
821 * @phba: Pointer to HBA context object.
822 * @ndlp: Targets nodelist pointer for this exchange.
823 * @xritag the xri in the bitmap to test.
825 * This function is called with hbalock held. This function
826 * returns 0 = rrq not active for this xri
827 * 1 = rrq is valid for this xri.
830 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
831 uint16_t xritag)
833 if (!ndlp)
834 return 0;
835 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
836 return 1;
837 else
838 return 0;
842 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
843 * @phba: Pointer to HBA context object.
844 * @ndlp: nodelist pointer for this target.
845 * @xritag: xri used in this exchange.
846 * @rxid: Remote Exchange ID.
847 * @send_rrq: Flag used to determine if we should send rrq els cmd.
849 * This function takes the hbalock.
850 * The active bit is always set in the active rrq xri_bitmap even
851 * if there is no slot avaiable for the other rrq information.
853 * returns 0 rrq actived for this xri
854 * < 0 No memory or invalid ndlp.
857 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
858 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
860 unsigned long iflags;
861 struct lpfc_node_rrq *rrq;
862 int empty;
864 if (!ndlp)
865 return -EINVAL;
867 if (!phba->cfg_enable_rrq)
868 return -EINVAL;
870 spin_lock_irqsave(&phba->hbalock, iflags);
871 if (phba->pport->load_flag & FC_UNLOADING) {
872 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
873 goto out;
877 * set the active bit even if there is no mem available.
879 if (NLP_CHK_FREE_REQ(ndlp))
880 goto out;
882 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
883 goto out;
885 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
886 goto out;
888 spin_unlock_irqrestore(&phba->hbalock, iflags);
889 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
890 if (!rrq) {
891 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
892 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
893 " DID:0x%x Send:%d\n",
894 xritag, rxid, ndlp->nlp_DID, send_rrq);
895 return -EINVAL;
897 if (phba->cfg_enable_rrq == 1)
898 rrq->send_rrq = send_rrq;
899 else
900 rrq->send_rrq = 0;
901 rrq->xritag = xritag;
902 rrq->rrq_stop_time = jiffies +
903 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
904 rrq->ndlp = ndlp;
905 rrq->nlp_DID = ndlp->nlp_DID;
906 rrq->vport = ndlp->vport;
907 rrq->rxid = rxid;
908 spin_lock_irqsave(&phba->hbalock, iflags);
909 empty = list_empty(&phba->active_rrq_list);
910 list_add_tail(&rrq->list, &phba->active_rrq_list);
911 phba->hba_flag |= HBA_RRQ_ACTIVE;
912 if (empty)
913 lpfc_worker_wake_up(phba);
914 spin_unlock_irqrestore(&phba->hbalock, iflags);
915 return 0;
916 out:
917 spin_unlock_irqrestore(&phba->hbalock, iflags);
918 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
919 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
920 " DID:0x%x Send:%d\n",
921 xritag, rxid, ndlp->nlp_DID, send_rrq);
922 return -EINVAL;
926 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
927 * @phba: Pointer to HBA context object.
928 * @piocb: Pointer to the iocbq.
930 * This function is called with hbalock held. This function
931 * gets a new driver sglq object from the sglq list. If the
932 * list is not empty then it is successful, it returns pointer to the newly
933 * allocated sglq object else it returns NULL.
935 static struct lpfc_sglq *
936 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
938 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
939 struct lpfc_sglq *sglq = NULL;
940 struct lpfc_sglq *start_sglq = NULL;
941 struct lpfc_scsi_buf *lpfc_cmd;
942 struct lpfc_nodelist *ndlp;
943 int found = 0;
945 if (piocbq->iocb_flag & LPFC_IO_FCP) {
946 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
947 ndlp = lpfc_cmd->rdata->pnode;
948 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
949 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
950 ndlp = piocbq->context_un.ndlp;
951 else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
952 ndlp = piocbq->context_un.ndlp;
953 else
954 ndlp = piocbq->context1;
956 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
957 start_sglq = sglq;
958 while (!found) {
959 if (!sglq)
960 return NULL;
961 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
962 /* This xri has an rrq outstanding for this DID.
963 * put it back in the list and get another xri.
965 list_add_tail(&sglq->list, lpfc_sgl_list);
966 sglq = NULL;
967 list_remove_head(lpfc_sgl_list, sglq,
968 struct lpfc_sglq, list);
969 if (sglq == start_sglq) {
970 sglq = NULL;
971 break;
972 } else
973 continue;
975 sglq->ndlp = ndlp;
976 found = 1;
977 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
978 sglq->state = SGL_ALLOCATED;
980 return sglq;
984 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
985 * @phba: Pointer to HBA context object.
987 * This function is called with no lock held. This function
988 * allocates a new driver iocb object from the iocb pool. If the
989 * allocation is successful, it returns pointer to the newly
990 * allocated iocb object else it returns NULL.
992 struct lpfc_iocbq *
993 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
995 struct lpfc_iocbq * iocbq = NULL;
996 unsigned long iflags;
998 spin_lock_irqsave(&phba->hbalock, iflags);
999 iocbq = __lpfc_sli_get_iocbq(phba);
1000 spin_unlock_irqrestore(&phba->hbalock, iflags);
1001 return iocbq;
1005 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1006 * @phba: Pointer to HBA context object.
1007 * @iocbq: Pointer to driver iocb object.
1009 * This function is called with hbalock held to release driver
1010 * iocb object to the iocb pool. The iotag in the iocb object
1011 * does not change for each use of the iocb object. This function
1012 * clears all other fields of the iocb object when it is freed.
1013 * The sqlq structure that holds the xritag and phys and virtual
1014 * mappings for the scatter gather list is retrieved from the
1015 * active array of sglq. The get of the sglq pointer also clears
1016 * the entry in the array. If the status of the IO indiactes that
1017 * this IO was aborted then the sglq entry it put on the
1018 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1019 * IO has good status or fails for any other reason then the sglq
1020 * entry is added to the free list (lpfc_sgl_list).
1022 static void
1023 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1025 struct lpfc_sglq *sglq;
1026 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1027 unsigned long iflag = 0;
1028 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
1030 if (iocbq->sli4_xritag == NO_XRI)
1031 sglq = NULL;
1032 else
1033 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1036 if (sglq) {
1037 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1038 (sglq->state != SGL_XRI_ABORTED)) {
1039 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1040 iflag);
1041 list_add(&sglq->list,
1042 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1043 spin_unlock_irqrestore(
1044 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1045 } else {
1046 sglq->state = SGL_FREED;
1047 sglq->ndlp = NULL;
1048 list_add_tail(&sglq->list,
1049 &phba->sli4_hba.lpfc_sgl_list);
1051 /* Check if TXQ queue needs to be serviced */
1052 if (!list_empty(&pring->txq))
1053 lpfc_worker_wake_up(phba);
1059 * Clean all volatile data fields, preserve iotag and node struct.
1061 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1062 iocbq->sli4_lxritag = NO_XRI;
1063 iocbq->sli4_xritag = NO_XRI;
1064 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1069 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1070 * @phba: Pointer to HBA context object.
1071 * @iocbq: Pointer to driver iocb object.
1073 * This function is called with hbalock held to release driver
1074 * iocb object to the iocb pool. The iotag in the iocb object
1075 * does not change for each use of the iocb object. This function
1076 * clears all other fields of the iocb object when it is freed.
1078 static void
1079 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1081 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1085 * Clean all volatile data fields, preserve iotag and node struct.
1087 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1088 iocbq->sli4_xritag = NO_XRI;
1089 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1093 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1094 * @phba: Pointer to HBA context object.
1095 * @iocbq: Pointer to driver iocb object.
1097 * This function is called with hbalock held to release driver
1098 * iocb object to the iocb pool. The iotag in the iocb object
1099 * does not change for each use of the iocb object. This function
1100 * clears all other fields of the iocb object when it is freed.
1102 static void
1103 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1105 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1106 phba->iocb_cnt--;
1110 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1111 * @phba: Pointer to HBA context object.
1112 * @iocbq: Pointer to driver iocb object.
1114 * This function is called with no lock held to release the iocb to
1115 * iocb pool.
1117 void
1118 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1120 unsigned long iflags;
1123 * Clean all volatile data fields, preserve iotag and node struct.
1125 spin_lock_irqsave(&phba->hbalock, iflags);
1126 __lpfc_sli_release_iocbq(phba, iocbq);
1127 spin_unlock_irqrestore(&phba->hbalock, iflags);
1131 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1132 * @phba: Pointer to HBA context object.
1133 * @iocblist: List of IOCBs.
1134 * @ulpstatus: ULP status in IOCB command field.
1135 * @ulpWord4: ULP word-4 in IOCB command field.
1137 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1138 * on the list by invoking the complete callback function associated with the
1139 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1140 * fields.
1142 void
1143 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1144 uint32_t ulpstatus, uint32_t ulpWord4)
1146 struct lpfc_iocbq *piocb;
1148 while (!list_empty(iocblist)) {
1149 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1150 if (!piocb->iocb_cmpl)
1151 lpfc_sli_release_iocbq(phba, piocb);
1152 else {
1153 piocb->iocb.ulpStatus = ulpstatus;
1154 piocb->iocb.un.ulpWord[4] = ulpWord4;
1155 (piocb->iocb_cmpl) (phba, piocb, piocb);
1158 return;
1162 * lpfc_sli_iocb_cmd_type - Get the iocb type
1163 * @iocb_cmnd: iocb command code.
1165 * This function is called by ring event handler function to get the iocb type.
1166 * This function translates the iocb command to an iocb command type used to
1167 * decide the final disposition of each completed IOCB.
1168 * The function returns
1169 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1170 * LPFC_SOL_IOCB if it is a solicited iocb completion
1171 * LPFC_ABORT_IOCB if it is an abort iocb
1172 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1174 * The caller is not required to hold any lock.
1176 static lpfc_iocb_type
1177 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1179 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1181 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1182 return 0;
1184 switch (iocb_cmnd) {
1185 case CMD_XMIT_SEQUENCE_CR:
1186 case CMD_XMIT_SEQUENCE_CX:
1187 case CMD_XMIT_BCAST_CN:
1188 case CMD_XMIT_BCAST_CX:
1189 case CMD_ELS_REQUEST_CR:
1190 case CMD_ELS_REQUEST_CX:
1191 case CMD_CREATE_XRI_CR:
1192 case CMD_CREATE_XRI_CX:
1193 case CMD_GET_RPI_CN:
1194 case CMD_XMIT_ELS_RSP_CX:
1195 case CMD_GET_RPI_CR:
1196 case CMD_FCP_IWRITE_CR:
1197 case CMD_FCP_IWRITE_CX:
1198 case CMD_FCP_IREAD_CR:
1199 case CMD_FCP_IREAD_CX:
1200 case CMD_FCP_ICMND_CR:
1201 case CMD_FCP_ICMND_CX:
1202 case CMD_FCP_TSEND_CX:
1203 case CMD_FCP_TRSP_CX:
1204 case CMD_FCP_TRECEIVE_CX:
1205 case CMD_FCP_AUTO_TRSP_CX:
1206 case CMD_ADAPTER_MSG:
1207 case CMD_ADAPTER_DUMP:
1208 case CMD_XMIT_SEQUENCE64_CR:
1209 case CMD_XMIT_SEQUENCE64_CX:
1210 case CMD_XMIT_BCAST64_CN:
1211 case CMD_XMIT_BCAST64_CX:
1212 case CMD_ELS_REQUEST64_CR:
1213 case CMD_ELS_REQUEST64_CX:
1214 case CMD_FCP_IWRITE64_CR:
1215 case CMD_FCP_IWRITE64_CX:
1216 case CMD_FCP_IREAD64_CR:
1217 case CMD_FCP_IREAD64_CX:
1218 case CMD_FCP_ICMND64_CR:
1219 case CMD_FCP_ICMND64_CX:
1220 case CMD_FCP_TSEND64_CX:
1221 case CMD_FCP_TRSP64_CX:
1222 case CMD_FCP_TRECEIVE64_CX:
1223 case CMD_GEN_REQUEST64_CR:
1224 case CMD_GEN_REQUEST64_CX:
1225 case CMD_XMIT_ELS_RSP64_CX:
1226 case DSSCMD_IWRITE64_CR:
1227 case DSSCMD_IWRITE64_CX:
1228 case DSSCMD_IREAD64_CR:
1229 case DSSCMD_IREAD64_CX:
1230 type = LPFC_SOL_IOCB;
1231 break;
1232 case CMD_ABORT_XRI_CN:
1233 case CMD_ABORT_XRI_CX:
1234 case CMD_CLOSE_XRI_CN:
1235 case CMD_CLOSE_XRI_CX:
1236 case CMD_XRI_ABORTED_CX:
1237 case CMD_ABORT_MXRI64_CN:
1238 case CMD_XMIT_BLS_RSP64_CX:
1239 type = LPFC_ABORT_IOCB;
1240 break;
1241 case CMD_RCV_SEQUENCE_CX:
1242 case CMD_RCV_ELS_REQ_CX:
1243 case CMD_RCV_SEQUENCE64_CX:
1244 case CMD_RCV_ELS_REQ64_CX:
1245 case CMD_ASYNC_STATUS:
1246 case CMD_IOCB_RCV_SEQ64_CX:
1247 case CMD_IOCB_RCV_ELS64_CX:
1248 case CMD_IOCB_RCV_CONT64_CX:
1249 case CMD_IOCB_RET_XRI64_CX:
1250 type = LPFC_UNSOL_IOCB;
1251 break;
1252 case CMD_IOCB_XMIT_MSEQ64_CR:
1253 case CMD_IOCB_XMIT_MSEQ64_CX:
1254 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1255 case CMD_IOCB_RCV_ELS_LIST64_CX:
1256 case CMD_IOCB_CLOSE_EXTENDED_CN:
1257 case CMD_IOCB_ABORT_EXTENDED_CN:
1258 case CMD_IOCB_RET_HBQE64_CN:
1259 case CMD_IOCB_FCP_IBIDIR64_CR:
1260 case CMD_IOCB_FCP_IBIDIR64_CX:
1261 case CMD_IOCB_FCP_ITASKMGT64_CX:
1262 case CMD_IOCB_LOGENTRY_CN:
1263 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1264 printk("%s - Unhandled SLI-3 Command x%x\n",
1265 __func__, iocb_cmnd);
1266 type = LPFC_UNKNOWN_IOCB;
1267 break;
1268 default:
1269 type = LPFC_UNKNOWN_IOCB;
1270 break;
1273 return type;
1277 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1278 * @phba: Pointer to HBA context object.
1280 * This function is called from SLI initialization code
1281 * to configure every ring of the HBA's SLI interface. The
1282 * caller is not required to hold any lock. This function issues
1283 * a config_ring mailbox command for each ring.
1284 * This function returns zero if successful else returns a negative
1285 * error code.
1287 static int
1288 lpfc_sli_ring_map(struct lpfc_hba *phba)
1290 struct lpfc_sli *psli = &phba->sli;
1291 LPFC_MBOXQ_t *pmb;
1292 MAILBOX_t *pmbox;
1293 int i, rc, ret = 0;
1295 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1296 if (!pmb)
1297 return -ENOMEM;
1298 pmbox = &pmb->u.mb;
1299 phba->link_state = LPFC_INIT_MBX_CMDS;
1300 for (i = 0; i < psli->num_rings; i++) {
1301 lpfc_config_ring(phba, i, pmb);
1302 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1303 if (rc != MBX_SUCCESS) {
1304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1305 "0446 Adapter failed to init (%d), "
1306 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1307 "ring %d\n",
1308 rc, pmbox->mbxCommand,
1309 pmbox->mbxStatus, i);
1310 phba->link_state = LPFC_HBA_ERROR;
1311 ret = -ENXIO;
1312 break;
1315 mempool_free(pmb, phba->mbox_mem_pool);
1316 return ret;
1320 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1321 * @phba: Pointer to HBA context object.
1322 * @pring: Pointer to driver SLI ring object.
1323 * @piocb: Pointer to the driver iocb object.
1325 * This function is called with hbalock held. The function adds the
1326 * new iocb to txcmplq of the given ring. This function always returns
1327 * 0. If this function is called for ELS ring, this function checks if
1328 * there is a vport associated with the ELS command. This function also
1329 * starts els_tmofunc timer if this is an ELS command.
1331 static int
1332 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1333 struct lpfc_iocbq *piocb)
1335 list_add_tail(&piocb->list, &pring->txcmplq);
1336 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1338 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1339 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1340 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1341 if (!piocb->vport)
1342 BUG();
1343 else
1344 mod_timer(&piocb->vport->els_tmofunc,
1345 jiffies +
1346 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1350 return 0;
1354 * lpfc_sli_ringtx_get - Get first element of the txq
1355 * @phba: Pointer to HBA context object.
1356 * @pring: Pointer to driver SLI ring object.
1358 * This function is called with hbalock held to get next
1359 * iocb in txq of the given ring. If there is any iocb in
1360 * the txq, the function returns first iocb in the list after
1361 * removing the iocb from the list, else it returns NULL.
1363 struct lpfc_iocbq *
1364 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1366 struct lpfc_iocbq *cmd_iocb;
1368 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1369 return cmd_iocb;
1373 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1374 * @phba: Pointer to HBA context object.
1375 * @pring: Pointer to driver SLI ring object.
1377 * This function is called with hbalock held and the caller must post the
1378 * iocb without releasing the lock. If the caller releases the lock,
1379 * iocb slot returned by the function is not guaranteed to be available.
1380 * The function returns pointer to the next available iocb slot if there
1381 * is available slot in the ring, else it returns NULL.
1382 * If the get index of the ring is ahead of the put index, the function
1383 * will post an error attention event to the worker thread to take the
1384 * HBA to offline state.
1386 static IOCB_t *
1387 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1389 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1390 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1391 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1392 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1393 pring->sli.sli3.next_cmdidx = 0;
1395 if (unlikely(pring->sli.sli3.local_getidx ==
1396 pring->sli.sli3.next_cmdidx)) {
1398 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1400 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1402 "0315 Ring %d issue: portCmdGet %d "
1403 "is bigger than cmd ring %d\n",
1404 pring->ringno,
1405 pring->sli.sli3.local_getidx,
1406 max_cmd_idx);
1408 phba->link_state = LPFC_HBA_ERROR;
1410 * All error attention handlers are posted to
1411 * worker thread
1413 phba->work_ha |= HA_ERATT;
1414 phba->work_hs = HS_FFER3;
1416 lpfc_worker_wake_up(phba);
1418 return NULL;
1421 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1422 return NULL;
1425 return lpfc_cmd_iocb(phba, pring);
1429 * lpfc_sli_next_iotag - Get an iotag for the iocb
1430 * @phba: Pointer to HBA context object.
1431 * @iocbq: Pointer to driver iocb object.
1433 * This function gets an iotag for the iocb. If there is no unused iotag and
1434 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1435 * array and assigns a new iotag.
1436 * The function returns the allocated iotag if successful, else returns zero.
1437 * Zero is not a valid iotag.
1438 * The caller is not required to hold any lock.
1440 uint16_t
1441 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1443 struct lpfc_iocbq **new_arr;
1444 struct lpfc_iocbq **old_arr;
1445 size_t new_len;
1446 struct lpfc_sli *psli = &phba->sli;
1447 uint16_t iotag;
1449 spin_lock_irq(&phba->hbalock);
1450 iotag = psli->last_iotag;
1451 if(++iotag < psli->iocbq_lookup_len) {
1452 psli->last_iotag = iotag;
1453 psli->iocbq_lookup[iotag] = iocbq;
1454 spin_unlock_irq(&phba->hbalock);
1455 iocbq->iotag = iotag;
1456 return iotag;
1457 } else if (psli->iocbq_lookup_len < (0xffff
1458 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1459 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1460 spin_unlock_irq(&phba->hbalock);
1461 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1462 GFP_KERNEL);
1463 if (new_arr) {
1464 spin_lock_irq(&phba->hbalock);
1465 old_arr = psli->iocbq_lookup;
1466 if (new_len <= psli->iocbq_lookup_len) {
1467 /* highly unprobable case */
1468 kfree(new_arr);
1469 iotag = psli->last_iotag;
1470 if(++iotag < psli->iocbq_lookup_len) {
1471 psli->last_iotag = iotag;
1472 psli->iocbq_lookup[iotag] = iocbq;
1473 spin_unlock_irq(&phba->hbalock);
1474 iocbq->iotag = iotag;
1475 return iotag;
1477 spin_unlock_irq(&phba->hbalock);
1478 return 0;
1480 if (psli->iocbq_lookup)
1481 memcpy(new_arr, old_arr,
1482 ((psli->last_iotag + 1) *
1483 sizeof (struct lpfc_iocbq *)));
1484 psli->iocbq_lookup = new_arr;
1485 psli->iocbq_lookup_len = new_len;
1486 psli->last_iotag = iotag;
1487 psli->iocbq_lookup[iotag] = iocbq;
1488 spin_unlock_irq(&phba->hbalock);
1489 iocbq->iotag = iotag;
1490 kfree(old_arr);
1491 return iotag;
1493 } else
1494 spin_unlock_irq(&phba->hbalock);
1496 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1497 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1498 psli->last_iotag);
1500 return 0;
1504 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1505 * @phba: Pointer to HBA context object.
1506 * @pring: Pointer to driver SLI ring object.
1507 * @iocb: Pointer to iocb slot in the ring.
1508 * @nextiocb: Pointer to driver iocb object which need to be
1509 * posted to firmware.
1511 * This function is called with hbalock held to post a new iocb to
1512 * the firmware. This function copies the new iocb to ring iocb slot and
1513 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1514 * a completion call back for this iocb else the function will free the
1515 * iocb object.
1517 static void
1518 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1519 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1522 * Set up an iotag
1524 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1527 if (pring->ringno == LPFC_ELS_RING) {
1528 lpfc_debugfs_slow_ring_trc(phba,
1529 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1530 *(((uint32_t *) &nextiocb->iocb) + 4),
1531 *(((uint32_t *) &nextiocb->iocb) + 6),
1532 *(((uint32_t *) &nextiocb->iocb) + 7));
1536 * Issue iocb command to adapter
1538 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1539 wmb();
1540 pring->stats.iocb_cmd++;
1543 * If there is no completion routine to call, we can release the
1544 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1545 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1547 if (nextiocb->iocb_cmpl)
1548 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1549 else
1550 __lpfc_sli_release_iocbq(phba, nextiocb);
1553 * Let the HBA know what IOCB slot will be the next one the
1554 * driver will put a command into.
1556 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1557 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1561 * lpfc_sli_update_full_ring - Update the chip attention register
1562 * @phba: Pointer to HBA context object.
1563 * @pring: Pointer to driver SLI ring object.
1565 * The caller is not required to hold any lock for calling this function.
1566 * This function updates the chip attention bits for the ring to inform firmware
1567 * that there are pending work to be done for this ring and requests an
1568 * interrupt when there is space available in the ring. This function is
1569 * called when the driver is unable to post more iocbs to the ring due
1570 * to unavailability of space in the ring.
1572 static void
1573 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1575 int ringno = pring->ringno;
1577 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1579 wmb();
1582 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1583 * The HBA will tell us when an IOCB entry is available.
1585 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1586 readl(phba->CAregaddr); /* flush */
1588 pring->stats.iocb_cmd_full++;
1592 * lpfc_sli_update_ring - Update chip attention register
1593 * @phba: Pointer to HBA context object.
1594 * @pring: Pointer to driver SLI ring object.
1596 * This function updates the chip attention register bit for the
1597 * given ring to inform HBA that there is more work to be done
1598 * in this ring. The caller is not required to hold any lock.
1600 static void
1601 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1603 int ringno = pring->ringno;
1606 * Tell the HBA that there is work to do in this ring.
1608 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1609 wmb();
1610 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1611 readl(phba->CAregaddr); /* flush */
1616 * lpfc_sli_resume_iocb - Process iocbs in the txq
1617 * @phba: Pointer to HBA context object.
1618 * @pring: Pointer to driver SLI ring object.
1620 * This function is called with hbalock held to post pending iocbs
1621 * in the txq to the firmware. This function is called when driver
1622 * detects space available in the ring.
1624 static void
1625 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1627 IOCB_t *iocb;
1628 struct lpfc_iocbq *nextiocb;
1631 * Check to see if:
1632 * (a) there is anything on the txq to send
1633 * (b) link is up
1634 * (c) link attention events can be processed (fcp ring only)
1635 * (d) IOCB processing is not blocked by the outstanding mbox command.
1638 if (lpfc_is_link_up(phba) &&
1639 (!list_empty(&pring->txq)) &&
1640 (pring->ringno != phba->sli.fcp_ring ||
1641 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1643 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1644 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1645 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1647 if (iocb)
1648 lpfc_sli_update_ring(phba, pring);
1649 else
1650 lpfc_sli_update_full_ring(phba, pring);
1653 return;
1657 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1658 * @phba: Pointer to HBA context object.
1659 * @hbqno: HBQ number.
1661 * This function is called with hbalock held to get the next
1662 * available slot for the given HBQ. If there is free slot
1663 * available for the HBQ it will return pointer to the next available
1664 * HBQ entry else it will return NULL.
1666 static struct lpfc_hbq_entry *
1667 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1669 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1671 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1672 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1673 hbqp->next_hbqPutIdx = 0;
1675 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1676 uint32_t raw_index = phba->hbq_get[hbqno];
1677 uint32_t getidx = le32_to_cpu(raw_index);
1679 hbqp->local_hbqGetIdx = getidx;
1681 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1682 lpfc_printf_log(phba, KERN_ERR,
1683 LOG_SLI | LOG_VPORT,
1684 "1802 HBQ %d: local_hbqGetIdx "
1685 "%u is > than hbqp->entry_count %u\n",
1686 hbqno, hbqp->local_hbqGetIdx,
1687 hbqp->entry_count);
1689 phba->link_state = LPFC_HBA_ERROR;
1690 return NULL;
1693 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1694 return NULL;
1697 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1698 hbqp->hbqPutIdx;
1702 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1703 * @phba: Pointer to HBA context object.
1705 * This function is called with no lock held to free all the
1706 * hbq buffers while uninitializing the SLI interface. It also
1707 * frees the HBQ buffers returned by the firmware but not yet
1708 * processed by the upper layers.
1710 void
1711 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1713 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1714 struct hbq_dmabuf *hbq_buf;
1715 unsigned long flags;
1716 int i, hbq_count;
1717 uint32_t hbqno;
1719 hbq_count = lpfc_sli_hbq_count();
1720 /* Return all memory used by all HBQs */
1721 spin_lock_irqsave(&phba->hbalock, flags);
1722 for (i = 0; i < hbq_count; ++i) {
1723 list_for_each_entry_safe(dmabuf, next_dmabuf,
1724 &phba->hbqs[i].hbq_buffer_list, list) {
1725 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1726 list_del(&hbq_buf->dbuf.list);
1727 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1729 phba->hbqs[i].buffer_count = 0;
1731 /* Return all HBQ buffer that are in-fly */
1732 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1733 list) {
1734 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1735 list_del(&hbq_buf->dbuf.list);
1736 if (hbq_buf->tag == -1) {
1737 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1738 (phba, hbq_buf);
1739 } else {
1740 hbqno = hbq_buf->tag >> 16;
1741 if (hbqno >= LPFC_MAX_HBQS)
1742 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1743 (phba, hbq_buf);
1744 else
1745 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1746 hbq_buf);
1750 /* Mark the HBQs not in use */
1751 phba->hbq_in_use = 0;
1752 spin_unlock_irqrestore(&phba->hbalock, flags);
1756 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1757 * @phba: Pointer to HBA context object.
1758 * @hbqno: HBQ number.
1759 * @hbq_buf: Pointer to HBQ buffer.
1761 * This function is called with the hbalock held to post a
1762 * hbq buffer to the firmware. If the function finds an empty
1763 * slot in the HBQ, it will post the buffer. The function will return
1764 * pointer to the hbq entry if it successfully post the buffer
1765 * else it will return NULL.
1767 static int
1768 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1769 struct hbq_dmabuf *hbq_buf)
1771 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1775 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @hbq_buf: Pointer to HBQ buffer.
1780 * This function is called with the hbalock held to post a hbq buffer to the
1781 * firmware. If the function finds an empty slot in the HBQ, it will post the
1782 * buffer and place it on the hbq_buffer_list. The function will return zero if
1783 * it successfully post the buffer else it will return an error.
1785 static int
1786 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1787 struct hbq_dmabuf *hbq_buf)
1789 struct lpfc_hbq_entry *hbqe;
1790 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1792 /* Get next HBQ entry slot to use */
1793 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1794 if (hbqe) {
1795 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1797 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1798 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1799 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1800 hbqe->bde.tus.f.bdeFlags = 0;
1801 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1802 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1803 /* Sync SLIM */
1804 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1805 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1806 /* flush */
1807 readl(phba->hbq_put + hbqno);
1808 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1809 return 0;
1810 } else
1811 return -ENOMEM;
1815 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1816 * @phba: Pointer to HBA context object.
1817 * @hbqno: HBQ number.
1818 * @hbq_buf: Pointer to HBQ buffer.
1820 * This function is called with the hbalock held to post an RQE to the SLI4
1821 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1822 * the hbq_buffer_list and return zero, otherwise it will return an error.
1824 static int
1825 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1826 struct hbq_dmabuf *hbq_buf)
1828 int rc;
1829 struct lpfc_rqe hrqe;
1830 struct lpfc_rqe drqe;
1832 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1833 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1834 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1835 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1836 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1837 &hrqe, &drqe);
1838 if (rc < 0)
1839 return rc;
1840 hbq_buf->tag = rc;
1841 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1842 return 0;
1845 /* HBQ for ELS and CT traffic. */
1846 static struct lpfc_hbq_init lpfc_els_hbq = {
1847 .rn = 1,
1848 .entry_count = 256,
1849 .mask_count = 0,
1850 .profile = 0,
1851 .ring_mask = (1 << LPFC_ELS_RING),
1852 .buffer_count = 0,
1853 .init_count = 40,
1854 .add_count = 40,
1857 /* HBQ for the extra ring if needed */
1858 static struct lpfc_hbq_init lpfc_extra_hbq = {
1859 .rn = 1,
1860 .entry_count = 200,
1861 .mask_count = 0,
1862 .profile = 0,
1863 .ring_mask = (1 << LPFC_EXTRA_RING),
1864 .buffer_count = 0,
1865 .init_count = 0,
1866 .add_count = 5,
1869 /* Array of HBQs */
1870 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1871 &lpfc_els_hbq,
1872 &lpfc_extra_hbq,
1876 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1877 * @phba: Pointer to HBA context object.
1878 * @hbqno: HBQ number.
1879 * @count: Number of HBQ buffers to be posted.
1881 * This function is called with no lock held to post more hbq buffers to the
1882 * given HBQ. The function returns the number of HBQ buffers successfully
1883 * posted.
1885 static int
1886 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1888 uint32_t i, posted = 0;
1889 unsigned long flags;
1890 struct hbq_dmabuf *hbq_buffer;
1891 LIST_HEAD(hbq_buf_list);
1892 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1893 return 0;
1895 if ((phba->hbqs[hbqno].buffer_count + count) >
1896 lpfc_hbq_defs[hbqno]->entry_count)
1897 count = lpfc_hbq_defs[hbqno]->entry_count -
1898 phba->hbqs[hbqno].buffer_count;
1899 if (!count)
1900 return 0;
1901 /* Allocate HBQ entries */
1902 for (i = 0; i < count; i++) {
1903 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1904 if (!hbq_buffer)
1905 break;
1906 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1908 /* Check whether HBQ is still in use */
1909 spin_lock_irqsave(&phba->hbalock, flags);
1910 if (!phba->hbq_in_use)
1911 goto err;
1912 while (!list_empty(&hbq_buf_list)) {
1913 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1914 dbuf.list);
1915 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1916 (hbqno << 16));
1917 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1918 phba->hbqs[hbqno].buffer_count++;
1919 posted++;
1920 } else
1921 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1923 spin_unlock_irqrestore(&phba->hbalock, flags);
1924 return posted;
1925 err:
1926 spin_unlock_irqrestore(&phba->hbalock, flags);
1927 while (!list_empty(&hbq_buf_list)) {
1928 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1929 dbuf.list);
1930 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1932 return 0;
1936 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1937 * @phba: Pointer to HBA context object.
1938 * @qno: HBQ number.
1940 * This function posts more buffers to the HBQ. This function
1941 * is called with no lock held. The function returns the number of HBQ entries
1942 * successfully allocated.
1945 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1947 if (phba->sli_rev == LPFC_SLI_REV4)
1948 return 0;
1949 else
1950 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1951 lpfc_hbq_defs[qno]->add_count);
1955 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1956 * @phba: Pointer to HBA context object.
1957 * @qno: HBQ queue number.
1959 * This function is called from SLI initialization code path with
1960 * no lock held to post initial HBQ buffers to firmware. The
1961 * function returns the number of HBQ entries successfully allocated.
1963 static int
1964 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1966 if (phba->sli_rev == LPFC_SLI_REV4)
1967 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1968 lpfc_hbq_defs[qno]->entry_count);
1969 else
1970 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1971 lpfc_hbq_defs[qno]->init_count);
1975 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1976 * @phba: Pointer to HBA context object.
1977 * @hbqno: HBQ number.
1979 * This function removes the first hbq buffer on an hbq list and returns a
1980 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1982 static struct hbq_dmabuf *
1983 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1985 struct lpfc_dmabuf *d_buf;
1987 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1988 if (!d_buf)
1989 return NULL;
1990 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1994 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1995 * @phba: Pointer to HBA context object.
1996 * @tag: Tag of the hbq buffer.
1998 * This function is called with hbalock held. This function searches
1999 * for the hbq buffer associated with the given tag in the hbq buffer
2000 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
2001 * it returns NULL.
2003 static struct hbq_dmabuf *
2004 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2006 struct lpfc_dmabuf *d_buf;
2007 struct hbq_dmabuf *hbq_buf;
2008 uint32_t hbqno;
2010 hbqno = tag >> 16;
2011 if (hbqno >= LPFC_MAX_HBQS)
2012 return NULL;
2014 spin_lock_irq(&phba->hbalock);
2015 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2016 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2017 if (hbq_buf->tag == tag) {
2018 spin_unlock_irq(&phba->hbalock);
2019 return hbq_buf;
2022 spin_unlock_irq(&phba->hbalock);
2023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2024 "1803 Bad hbq tag. Data: x%x x%x\n",
2025 tag, phba->hbqs[tag >> 16].buffer_count);
2026 return NULL;
2030 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2031 * @phba: Pointer to HBA context object.
2032 * @hbq_buffer: Pointer to HBQ buffer.
2034 * This function is called with hbalock. This function gives back
2035 * the hbq buffer to firmware. If the HBQ does not have space to
2036 * post the buffer, it will free the buffer.
2038 void
2039 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2041 uint32_t hbqno;
2043 if (hbq_buffer) {
2044 hbqno = hbq_buffer->tag >> 16;
2045 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2046 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2051 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2052 * @mbxCommand: mailbox command code.
2054 * This function is called by the mailbox event handler function to verify
2055 * that the completed mailbox command is a legitimate mailbox command. If the
2056 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2057 * and the mailbox event handler will take the HBA offline.
2059 static int
2060 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2062 uint8_t ret;
2064 switch (mbxCommand) {
2065 case MBX_LOAD_SM:
2066 case MBX_READ_NV:
2067 case MBX_WRITE_NV:
2068 case MBX_WRITE_VPARMS:
2069 case MBX_RUN_BIU_DIAG:
2070 case MBX_INIT_LINK:
2071 case MBX_DOWN_LINK:
2072 case MBX_CONFIG_LINK:
2073 case MBX_CONFIG_RING:
2074 case MBX_RESET_RING:
2075 case MBX_READ_CONFIG:
2076 case MBX_READ_RCONFIG:
2077 case MBX_READ_SPARM:
2078 case MBX_READ_STATUS:
2079 case MBX_READ_RPI:
2080 case MBX_READ_XRI:
2081 case MBX_READ_REV:
2082 case MBX_READ_LNK_STAT:
2083 case MBX_REG_LOGIN:
2084 case MBX_UNREG_LOGIN:
2085 case MBX_CLEAR_LA:
2086 case MBX_DUMP_MEMORY:
2087 case MBX_DUMP_CONTEXT:
2088 case MBX_RUN_DIAGS:
2089 case MBX_RESTART:
2090 case MBX_UPDATE_CFG:
2091 case MBX_DOWN_LOAD:
2092 case MBX_DEL_LD_ENTRY:
2093 case MBX_RUN_PROGRAM:
2094 case MBX_SET_MASK:
2095 case MBX_SET_VARIABLE:
2096 case MBX_UNREG_D_ID:
2097 case MBX_KILL_BOARD:
2098 case MBX_CONFIG_FARP:
2099 case MBX_BEACON:
2100 case MBX_LOAD_AREA:
2101 case MBX_RUN_BIU_DIAG64:
2102 case MBX_CONFIG_PORT:
2103 case MBX_READ_SPARM64:
2104 case MBX_READ_RPI64:
2105 case MBX_REG_LOGIN64:
2106 case MBX_READ_TOPOLOGY:
2107 case MBX_WRITE_WWN:
2108 case MBX_SET_DEBUG:
2109 case MBX_LOAD_EXP_ROM:
2110 case MBX_ASYNCEVT_ENABLE:
2111 case MBX_REG_VPI:
2112 case MBX_UNREG_VPI:
2113 case MBX_HEARTBEAT:
2114 case MBX_PORT_CAPABILITIES:
2115 case MBX_PORT_IOV_CONTROL:
2116 case MBX_SLI4_CONFIG:
2117 case MBX_SLI4_REQ_FTRS:
2118 case MBX_REG_FCFI:
2119 case MBX_UNREG_FCFI:
2120 case MBX_REG_VFI:
2121 case MBX_UNREG_VFI:
2122 case MBX_INIT_VPI:
2123 case MBX_INIT_VFI:
2124 case MBX_RESUME_RPI:
2125 case MBX_READ_EVENT_LOG_STATUS:
2126 case MBX_READ_EVENT_LOG:
2127 case MBX_SECURITY_MGMT:
2128 case MBX_AUTH_PORT:
2129 case MBX_ACCESS_VDATA:
2130 ret = mbxCommand;
2131 break;
2132 default:
2133 ret = MBX_SHUTDOWN;
2134 break;
2136 return ret;
2140 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2141 * @phba: Pointer to HBA context object.
2142 * @pmboxq: Pointer to mailbox command.
2144 * This is completion handler function for mailbox commands issued from
2145 * lpfc_sli_issue_mbox_wait function. This function is called by the
2146 * mailbox event handler function with no lock held. This function
2147 * will wake up thread waiting on the wait queue pointed by context1
2148 * of the mailbox.
2150 void
2151 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2153 wait_queue_head_t *pdone_q;
2154 unsigned long drvr_flag;
2157 * If pdone_q is empty, the driver thread gave up waiting and
2158 * continued running.
2160 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2161 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2162 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2163 if (pdone_q)
2164 wake_up_interruptible(pdone_q);
2165 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2166 return;
2171 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2172 * @phba: Pointer to HBA context object.
2173 * @pmb: Pointer to mailbox object.
2175 * This function is the default mailbox completion handler. It
2176 * frees the memory resources associated with the completed mailbox
2177 * command. If the completed command is a REG_LOGIN mailbox command,
2178 * this function will issue a UREG_LOGIN to re-claim the RPI.
2180 void
2181 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2183 struct lpfc_vport *vport = pmb->vport;
2184 struct lpfc_dmabuf *mp;
2185 struct lpfc_nodelist *ndlp;
2186 struct Scsi_Host *shost;
2187 uint16_t rpi, vpi;
2188 int rc;
2190 mp = (struct lpfc_dmabuf *) (pmb->context1);
2192 if (mp) {
2193 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2194 kfree(mp);
2198 * If a REG_LOGIN succeeded after node is destroyed or node
2199 * is in re-discovery driver need to cleanup the RPI.
2201 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2202 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2203 !pmb->u.mb.mbxStatus) {
2204 rpi = pmb->u.mb.un.varWords[0];
2205 vpi = pmb->u.mb.un.varRegLogin.vpi;
2206 lpfc_unreg_login(phba, vpi, rpi, pmb);
2207 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2208 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2209 if (rc != MBX_NOT_FINISHED)
2210 return;
2213 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2214 !(phba->pport->load_flag & FC_UNLOADING) &&
2215 !pmb->u.mb.mbxStatus) {
2216 shost = lpfc_shost_from_vport(vport);
2217 spin_lock_irq(shost->host_lock);
2218 vport->vpi_state |= LPFC_VPI_REGISTERED;
2219 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2220 spin_unlock_irq(shost->host_lock);
2223 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2224 ndlp = (struct lpfc_nodelist *)pmb->context2;
2225 lpfc_nlp_put(ndlp);
2226 pmb->context2 = NULL;
2229 /* Check security permission status on INIT_LINK mailbox command */
2230 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2231 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2232 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2233 "2860 SLI authentication is required "
2234 "for INIT_LINK but has not done yet\n");
2236 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2237 lpfc_sli4_mbox_cmd_free(phba, pmb);
2238 else
2239 mempool_free(pmb, phba->mbox_mem_pool);
2243 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2244 * @phba: Pointer to HBA context object.
2246 * This function is called with no lock held. This function processes all
2247 * the completed mailbox commands and gives it to upper layers. The interrupt
2248 * service routine processes mailbox completion interrupt and adds completed
2249 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2250 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2251 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2252 * function returns the mailbox commands to the upper layer by calling the
2253 * completion handler function of each mailbox.
2256 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2258 MAILBOX_t *pmbox;
2259 LPFC_MBOXQ_t *pmb;
2260 int rc;
2261 LIST_HEAD(cmplq);
2263 phba->sli.slistat.mbox_event++;
2265 /* Get all completed mailboxe buffers into the cmplq */
2266 spin_lock_irq(&phba->hbalock);
2267 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2268 spin_unlock_irq(&phba->hbalock);
2270 /* Get a Mailbox buffer to setup mailbox commands for callback */
2271 do {
2272 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2273 if (pmb == NULL)
2274 break;
2276 pmbox = &pmb->u.mb;
2278 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2279 if (pmb->vport) {
2280 lpfc_debugfs_disc_trc(pmb->vport,
2281 LPFC_DISC_TRC_MBOX_VPORT,
2282 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2283 (uint32_t)pmbox->mbxCommand,
2284 pmbox->un.varWords[0],
2285 pmbox->un.varWords[1]);
2287 else {
2288 lpfc_debugfs_disc_trc(phba->pport,
2289 LPFC_DISC_TRC_MBOX,
2290 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2291 (uint32_t)pmbox->mbxCommand,
2292 pmbox->un.varWords[0],
2293 pmbox->un.varWords[1]);
2298 * It is a fatal error if unknown mbox command completion.
2300 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2301 MBX_SHUTDOWN) {
2302 /* Unknown mailbox command compl */
2303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2304 "(%d):0323 Unknown Mailbox command "
2305 "x%x (x%x/x%x) Cmpl\n",
2306 pmb->vport ? pmb->vport->vpi : 0,
2307 pmbox->mbxCommand,
2308 lpfc_sli_config_mbox_subsys_get(phba,
2309 pmb),
2310 lpfc_sli_config_mbox_opcode_get(phba,
2311 pmb));
2312 phba->link_state = LPFC_HBA_ERROR;
2313 phba->work_hs = HS_FFER3;
2314 lpfc_handle_eratt(phba);
2315 continue;
2318 if (pmbox->mbxStatus) {
2319 phba->sli.slistat.mbox_stat_err++;
2320 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2321 /* Mbox cmd cmpl error - RETRYing */
2322 lpfc_printf_log(phba, KERN_INFO,
2323 LOG_MBOX | LOG_SLI,
2324 "(%d):0305 Mbox cmd cmpl "
2325 "error - RETRYing Data: x%x "
2326 "(x%x/x%x) x%x x%x x%x\n",
2327 pmb->vport ? pmb->vport->vpi : 0,
2328 pmbox->mbxCommand,
2329 lpfc_sli_config_mbox_subsys_get(phba,
2330 pmb),
2331 lpfc_sli_config_mbox_opcode_get(phba,
2332 pmb),
2333 pmbox->mbxStatus,
2334 pmbox->un.varWords[0],
2335 pmb->vport->port_state);
2336 pmbox->mbxStatus = 0;
2337 pmbox->mbxOwner = OWN_HOST;
2338 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2339 if (rc != MBX_NOT_FINISHED)
2340 continue;
2344 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2345 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2346 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2347 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2348 "x%x x%x x%x\n",
2349 pmb->vport ? pmb->vport->vpi : 0,
2350 pmbox->mbxCommand,
2351 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2352 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2353 pmb->mbox_cmpl,
2354 *((uint32_t *) pmbox),
2355 pmbox->un.varWords[0],
2356 pmbox->un.varWords[1],
2357 pmbox->un.varWords[2],
2358 pmbox->un.varWords[3],
2359 pmbox->un.varWords[4],
2360 pmbox->un.varWords[5],
2361 pmbox->un.varWords[6],
2362 pmbox->un.varWords[7],
2363 pmbox->un.varWords[8],
2364 pmbox->un.varWords[9],
2365 pmbox->un.varWords[10]);
2367 if (pmb->mbox_cmpl)
2368 pmb->mbox_cmpl(phba,pmb);
2369 } while (1);
2370 return 0;
2374 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2375 * @phba: Pointer to HBA context object.
2376 * @pring: Pointer to driver SLI ring object.
2377 * @tag: buffer tag.
2379 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2380 * is set in the tag the buffer is posted for a particular exchange,
2381 * the function will return the buffer without replacing the buffer.
2382 * If the buffer is for unsolicited ELS or CT traffic, this function
2383 * returns the buffer and also posts another buffer to the firmware.
2385 static struct lpfc_dmabuf *
2386 lpfc_sli_get_buff(struct lpfc_hba *phba,
2387 struct lpfc_sli_ring *pring,
2388 uint32_t tag)
2390 struct hbq_dmabuf *hbq_entry;
2392 if (tag & QUE_BUFTAG_BIT)
2393 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2394 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2395 if (!hbq_entry)
2396 return NULL;
2397 return &hbq_entry->dbuf;
2401 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2402 * @phba: Pointer to HBA context object.
2403 * @pring: Pointer to driver SLI ring object.
2404 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2405 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2406 * @fch_type: the type for the first frame of the sequence.
2408 * This function is called with no lock held. This function uses the r_ctl and
2409 * type of the received sequence to find the correct callback function to call
2410 * to process the sequence.
2412 static int
2413 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2414 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2415 uint32_t fch_type)
2417 int i;
2419 /* unSolicited Responses */
2420 if (pring->prt[0].profile) {
2421 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2422 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2423 saveq);
2424 return 1;
2426 /* We must search, based on rctl / type
2427 for the right routine */
2428 for (i = 0; i < pring->num_mask; i++) {
2429 if ((pring->prt[i].rctl == fch_r_ctl) &&
2430 (pring->prt[i].type == fch_type)) {
2431 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2432 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2433 (phba, pring, saveq);
2434 return 1;
2437 return 0;
2441 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2442 * @phba: Pointer to HBA context object.
2443 * @pring: Pointer to driver SLI ring object.
2444 * @saveq: Pointer to the unsolicited iocb.
2446 * This function is called with no lock held by the ring event handler
2447 * when there is an unsolicited iocb posted to the response ring by the
2448 * firmware. This function gets the buffer associated with the iocbs
2449 * and calls the event handler for the ring. This function handles both
2450 * qring buffers and hbq buffers.
2451 * When the function returns 1 the caller can free the iocb object otherwise
2452 * upper layer functions will free the iocb objects.
2454 static int
2455 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2456 struct lpfc_iocbq *saveq)
2458 IOCB_t * irsp;
2459 WORD5 * w5p;
2460 uint32_t Rctl, Type;
2461 uint32_t match;
2462 struct lpfc_iocbq *iocbq;
2463 struct lpfc_dmabuf *dmzbuf;
2465 match = 0;
2466 irsp = &(saveq->iocb);
2468 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2469 if (pring->lpfc_sli_rcv_async_status)
2470 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2471 else
2472 lpfc_printf_log(phba,
2473 KERN_WARNING,
2474 LOG_SLI,
2475 "0316 Ring %d handler: unexpected "
2476 "ASYNC_STATUS iocb received evt_code "
2477 "0x%x\n",
2478 pring->ringno,
2479 irsp->un.asyncstat.evt_code);
2480 return 1;
2483 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2484 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2485 if (irsp->ulpBdeCount > 0) {
2486 dmzbuf = lpfc_sli_get_buff(phba, pring,
2487 irsp->un.ulpWord[3]);
2488 lpfc_in_buf_free(phba, dmzbuf);
2491 if (irsp->ulpBdeCount > 1) {
2492 dmzbuf = lpfc_sli_get_buff(phba, pring,
2493 irsp->unsli3.sli3Words[3]);
2494 lpfc_in_buf_free(phba, dmzbuf);
2497 if (irsp->ulpBdeCount > 2) {
2498 dmzbuf = lpfc_sli_get_buff(phba, pring,
2499 irsp->unsli3.sli3Words[7]);
2500 lpfc_in_buf_free(phba, dmzbuf);
2503 return 1;
2506 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2507 if (irsp->ulpBdeCount != 0) {
2508 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2509 irsp->un.ulpWord[3]);
2510 if (!saveq->context2)
2511 lpfc_printf_log(phba,
2512 KERN_ERR,
2513 LOG_SLI,
2514 "0341 Ring %d Cannot find buffer for "
2515 "an unsolicited iocb. tag 0x%x\n",
2516 pring->ringno,
2517 irsp->un.ulpWord[3]);
2519 if (irsp->ulpBdeCount == 2) {
2520 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2521 irsp->unsli3.sli3Words[7]);
2522 if (!saveq->context3)
2523 lpfc_printf_log(phba,
2524 KERN_ERR,
2525 LOG_SLI,
2526 "0342 Ring %d Cannot find buffer for an"
2527 " unsolicited iocb. tag 0x%x\n",
2528 pring->ringno,
2529 irsp->unsli3.sli3Words[7]);
2531 list_for_each_entry(iocbq, &saveq->list, list) {
2532 irsp = &(iocbq->iocb);
2533 if (irsp->ulpBdeCount != 0) {
2534 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2535 irsp->un.ulpWord[3]);
2536 if (!iocbq->context2)
2537 lpfc_printf_log(phba,
2538 KERN_ERR,
2539 LOG_SLI,
2540 "0343 Ring %d Cannot find "
2541 "buffer for an unsolicited iocb"
2542 ". tag 0x%x\n", pring->ringno,
2543 irsp->un.ulpWord[3]);
2545 if (irsp->ulpBdeCount == 2) {
2546 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2547 irsp->unsli3.sli3Words[7]);
2548 if (!iocbq->context3)
2549 lpfc_printf_log(phba,
2550 KERN_ERR,
2551 LOG_SLI,
2552 "0344 Ring %d Cannot find "
2553 "buffer for an unsolicited "
2554 "iocb. tag 0x%x\n",
2555 pring->ringno,
2556 irsp->unsli3.sli3Words[7]);
2560 if (irsp->ulpBdeCount != 0 &&
2561 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2562 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2563 int found = 0;
2565 /* search continue save q for same XRI */
2566 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2567 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2568 saveq->iocb.unsli3.rcvsli3.ox_id) {
2569 list_add_tail(&saveq->list, &iocbq->list);
2570 found = 1;
2571 break;
2574 if (!found)
2575 list_add_tail(&saveq->clist,
2576 &pring->iocb_continue_saveq);
2577 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2578 list_del_init(&iocbq->clist);
2579 saveq = iocbq;
2580 irsp = &(saveq->iocb);
2581 } else
2582 return 0;
2584 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2585 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2586 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2587 Rctl = FC_RCTL_ELS_REQ;
2588 Type = FC_TYPE_ELS;
2589 } else {
2590 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2591 Rctl = w5p->hcsw.Rctl;
2592 Type = w5p->hcsw.Type;
2594 /* Firmware Workaround */
2595 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2596 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2597 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2598 Rctl = FC_RCTL_ELS_REQ;
2599 Type = FC_TYPE_ELS;
2600 w5p->hcsw.Rctl = Rctl;
2601 w5p->hcsw.Type = Type;
2605 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2606 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2607 "0313 Ring %d handler: unexpected Rctl x%x "
2608 "Type x%x received\n",
2609 pring->ringno, Rctl, Type);
2611 return 1;
2615 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2616 * @phba: Pointer to HBA context object.
2617 * @pring: Pointer to driver SLI ring object.
2618 * @prspiocb: Pointer to response iocb object.
2620 * This function looks up the iocb_lookup table to get the command iocb
2621 * corresponding to the given response iocb using the iotag of the
2622 * response iocb. This function is called with the hbalock held.
2623 * This function returns the command iocb object if it finds the command
2624 * iocb else returns NULL.
2626 static struct lpfc_iocbq *
2627 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2628 struct lpfc_sli_ring *pring,
2629 struct lpfc_iocbq *prspiocb)
2631 struct lpfc_iocbq *cmd_iocb = NULL;
2632 uint16_t iotag;
2634 iotag = prspiocb->iocb.ulpIoTag;
2636 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2637 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2638 list_del_init(&cmd_iocb->list);
2639 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2640 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2642 return cmd_iocb;
2645 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2646 "0317 iotag x%x is out off "
2647 "range: max iotag x%x wd0 x%x\n",
2648 iotag, phba->sli.last_iotag,
2649 *(((uint32_t *) &prspiocb->iocb) + 7));
2650 return NULL;
2654 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2655 * @phba: Pointer to HBA context object.
2656 * @pring: Pointer to driver SLI ring object.
2657 * @iotag: IOCB tag.
2659 * This function looks up the iocb_lookup table to get the command iocb
2660 * corresponding to the given iotag. This function is called with the
2661 * hbalock held.
2662 * This function returns the command iocb object if it finds the command
2663 * iocb else returns NULL.
2665 static struct lpfc_iocbq *
2666 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2667 struct lpfc_sli_ring *pring, uint16_t iotag)
2669 struct lpfc_iocbq *cmd_iocb;
2671 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2672 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2673 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2674 /* remove from txcmpl queue list */
2675 list_del_init(&cmd_iocb->list);
2676 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2677 return cmd_iocb;
2680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2681 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2682 iotag, phba->sli.last_iotag);
2683 return NULL;
2687 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2688 * @phba: Pointer to HBA context object.
2689 * @pring: Pointer to driver SLI ring object.
2690 * @saveq: Pointer to the response iocb to be processed.
2692 * This function is called by the ring event handler for non-fcp
2693 * rings when there is a new response iocb in the response ring.
2694 * The caller is not required to hold any locks. This function
2695 * gets the command iocb associated with the response iocb and
2696 * calls the completion handler for the command iocb. If there
2697 * is no completion handler, the function will free the resources
2698 * associated with command iocb. If the response iocb is for
2699 * an already aborted command iocb, the status of the completion
2700 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2701 * This function always returns 1.
2703 static int
2704 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2705 struct lpfc_iocbq *saveq)
2707 struct lpfc_iocbq *cmdiocbp;
2708 int rc = 1;
2709 unsigned long iflag;
2711 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2712 spin_lock_irqsave(&phba->hbalock, iflag);
2713 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2714 spin_unlock_irqrestore(&phba->hbalock, iflag);
2716 if (cmdiocbp) {
2717 if (cmdiocbp->iocb_cmpl) {
2719 * If an ELS command failed send an event to mgmt
2720 * application.
2722 if (saveq->iocb.ulpStatus &&
2723 (pring->ringno == LPFC_ELS_RING) &&
2724 (cmdiocbp->iocb.ulpCommand ==
2725 CMD_ELS_REQUEST64_CR))
2726 lpfc_send_els_failure_event(phba,
2727 cmdiocbp, saveq);
2730 * Post all ELS completions to the worker thread.
2731 * All other are passed to the completion callback.
2733 if (pring->ringno == LPFC_ELS_RING) {
2734 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2735 (cmdiocbp->iocb_flag &
2736 LPFC_DRIVER_ABORTED)) {
2737 spin_lock_irqsave(&phba->hbalock,
2738 iflag);
2739 cmdiocbp->iocb_flag &=
2740 ~LPFC_DRIVER_ABORTED;
2741 spin_unlock_irqrestore(&phba->hbalock,
2742 iflag);
2743 saveq->iocb.ulpStatus =
2744 IOSTAT_LOCAL_REJECT;
2745 saveq->iocb.un.ulpWord[4] =
2746 IOERR_SLI_ABORTED;
2748 /* Firmware could still be in progress
2749 * of DMAing payload, so don't free data
2750 * buffer till after a hbeat.
2752 spin_lock_irqsave(&phba->hbalock,
2753 iflag);
2754 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2755 spin_unlock_irqrestore(&phba->hbalock,
2756 iflag);
2758 if (phba->sli_rev == LPFC_SLI_REV4) {
2759 if (saveq->iocb_flag &
2760 LPFC_EXCHANGE_BUSY) {
2761 /* Set cmdiocb flag for the
2762 * exchange busy so sgl (xri)
2763 * will not be released until
2764 * the abort xri is received
2765 * from hba.
2767 spin_lock_irqsave(
2768 &phba->hbalock, iflag);
2769 cmdiocbp->iocb_flag |=
2770 LPFC_EXCHANGE_BUSY;
2771 spin_unlock_irqrestore(
2772 &phba->hbalock, iflag);
2774 if (cmdiocbp->iocb_flag &
2775 LPFC_DRIVER_ABORTED) {
2777 * Clear LPFC_DRIVER_ABORTED
2778 * bit in case it was driver
2779 * initiated abort.
2781 spin_lock_irqsave(
2782 &phba->hbalock, iflag);
2783 cmdiocbp->iocb_flag &=
2784 ~LPFC_DRIVER_ABORTED;
2785 spin_unlock_irqrestore(
2786 &phba->hbalock, iflag);
2787 cmdiocbp->iocb.ulpStatus =
2788 IOSTAT_LOCAL_REJECT;
2789 cmdiocbp->iocb.un.ulpWord[4] =
2790 IOERR_ABORT_REQUESTED;
2792 * For SLI4, irsiocb contains
2793 * NO_XRI in sli_xritag, it
2794 * shall not affect releasing
2795 * sgl (xri) process.
2797 saveq->iocb.ulpStatus =
2798 IOSTAT_LOCAL_REJECT;
2799 saveq->iocb.un.ulpWord[4] =
2800 IOERR_SLI_ABORTED;
2801 spin_lock_irqsave(
2802 &phba->hbalock, iflag);
2803 saveq->iocb_flag |=
2804 LPFC_DELAY_MEM_FREE;
2805 spin_unlock_irqrestore(
2806 &phba->hbalock, iflag);
2810 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2811 } else
2812 lpfc_sli_release_iocbq(phba, cmdiocbp);
2813 } else {
2815 * Unknown initiating command based on the response iotag.
2816 * This could be the case on the ELS ring because of
2817 * lpfc_els_abort().
2819 if (pring->ringno != LPFC_ELS_RING) {
2821 * Ring <ringno> handler: unexpected completion IoTag
2822 * <IoTag>
2824 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2825 "0322 Ring %d handler: "
2826 "unexpected completion IoTag x%x "
2827 "Data: x%x x%x x%x x%x\n",
2828 pring->ringno,
2829 saveq->iocb.ulpIoTag,
2830 saveq->iocb.ulpStatus,
2831 saveq->iocb.un.ulpWord[4],
2832 saveq->iocb.ulpCommand,
2833 saveq->iocb.ulpContext);
2837 return rc;
2841 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2842 * @phba: Pointer to HBA context object.
2843 * @pring: Pointer to driver SLI ring object.
2845 * This function is called from the iocb ring event handlers when
2846 * put pointer is ahead of the get pointer for a ring. This function signal
2847 * an error attention condition to the worker thread and the worker
2848 * thread will transition the HBA to offline state.
2850 static void
2851 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2853 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2855 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2856 * rsp ring <portRspMax>
2858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2859 "0312 Ring %d handler: portRspPut %d "
2860 "is bigger than rsp ring %d\n",
2861 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2862 pring->sli.sli3.numRiocb);
2864 phba->link_state = LPFC_HBA_ERROR;
2867 * All error attention handlers are posted to
2868 * worker thread
2870 phba->work_ha |= HA_ERATT;
2871 phba->work_hs = HS_FFER3;
2873 lpfc_worker_wake_up(phba);
2875 return;
2879 * lpfc_poll_eratt - Error attention polling timer timeout handler
2880 * @ptr: Pointer to address of HBA context object.
2882 * This function is invoked by the Error Attention polling timer when the
2883 * timer times out. It will check the SLI Error Attention register for
2884 * possible attention events. If so, it will post an Error Attention event
2885 * and wake up worker thread to process it. Otherwise, it will set up the
2886 * Error Attention polling timer for the next poll.
2888 void lpfc_poll_eratt(unsigned long ptr)
2890 struct lpfc_hba *phba;
2891 uint32_t eratt = 0, rem;
2892 uint64_t sli_intr, cnt;
2894 phba = (struct lpfc_hba *)ptr;
2896 /* Here we will also keep track of interrupts per sec of the hba */
2897 sli_intr = phba->sli.slistat.sli_intr;
2899 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2900 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2901 sli_intr);
2902 else
2903 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2905 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2906 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2907 phba->sli.slistat.sli_ips = cnt;
2909 phba->sli.slistat.sli_prev_intr = sli_intr;
2911 /* Check chip HA register for error event */
2912 eratt = lpfc_sli_check_eratt(phba);
2914 if (eratt)
2915 /* Tell the worker thread there is work to do */
2916 lpfc_worker_wake_up(phba);
2917 else
2918 /* Restart the timer for next eratt poll */
2919 mod_timer(&phba->eratt_poll,
2920 jiffies +
2921 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
2922 return;
2927 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2928 * @phba: Pointer to HBA context object.
2929 * @pring: Pointer to driver SLI ring object.
2930 * @mask: Host attention register mask for this ring.
2932 * This function is called from the interrupt context when there is a ring
2933 * event for the fcp ring. The caller does not hold any lock.
2934 * The function processes each response iocb in the response ring until it
2935 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2936 * LE bit set. The function will call the completion handler of the command iocb
2937 * if the response iocb indicates a completion for a command iocb or it is
2938 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2939 * function if this is an unsolicited iocb.
2940 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2941 * to check it explicitly.
2944 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2945 struct lpfc_sli_ring *pring, uint32_t mask)
2947 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2948 IOCB_t *irsp = NULL;
2949 IOCB_t *entry = NULL;
2950 struct lpfc_iocbq *cmdiocbq = NULL;
2951 struct lpfc_iocbq rspiocbq;
2952 uint32_t status;
2953 uint32_t portRspPut, portRspMax;
2954 int rc = 1;
2955 lpfc_iocb_type type;
2956 unsigned long iflag;
2957 uint32_t rsp_cmpl = 0;
2959 spin_lock_irqsave(&phba->hbalock, iflag);
2960 pring->stats.iocb_event++;
2963 * The next available response entry should never exceed the maximum
2964 * entries. If it does, treat it as an adapter hardware error.
2966 portRspMax = pring->sli.sli3.numRiocb;
2967 portRspPut = le32_to_cpu(pgp->rspPutInx);
2968 if (unlikely(portRspPut >= portRspMax)) {
2969 lpfc_sli_rsp_pointers_error(phba, pring);
2970 spin_unlock_irqrestore(&phba->hbalock, iflag);
2971 return 1;
2973 if (phba->fcp_ring_in_use) {
2974 spin_unlock_irqrestore(&phba->hbalock, iflag);
2975 return 1;
2976 } else
2977 phba->fcp_ring_in_use = 1;
2979 rmb();
2980 while (pring->sli.sli3.rspidx != portRspPut) {
2982 * Fetch an entry off the ring and copy it into a local data
2983 * structure. The copy involves a byte-swap since the
2984 * network byte order and pci byte orders are different.
2986 entry = lpfc_resp_iocb(phba, pring);
2987 phba->last_completion_time = jiffies;
2989 if (++pring->sli.sli3.rspidx >= portRspMax)
2990 pring->sli.sli3.rspidx = 0;
2992 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2993 (uint32_t *) &rspiocbq.iocb,
2994 phba->iocb_rsp_size);
2995 INIT_LIST_HEAD(&(rspiocbq.list));
2996 irsp = &rspiocbq.iocb;
2998 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2999 pring->stats.iocb_rsp++;
3000 rsp_cmpl++;
3002 if (unlikely(irsp->ulpStatus)) {
3004 * If resource errors reported from HBA, reduce
3005 * queuedepths of the SCSI device.
3007 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3008 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3009 IOERR_NO_RESOURCES)) {
3010 spin_unlock_irqrestore(&phba->hbalock, iflag);
3011 phba->lpfc_rampdown_queue_depth(phba);
3012 spin_lock_irqsave(&phba->hbalock, iflag);
3015 /* Rsp ring <ringno> error: IOCB */
3016 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3017 "0336 Rsp Ring %d error: IOCB Data: "
3018 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3019 pring->ringno,
3020 irsp->un.ulpWord[0],
3021 irsp->un.ulpWord[1],
3022 irsp->un.ulpWord[2],
3023 irsp->un.ulpWord[3],
3024 irsp->un.ulpWord[4],
3025 irsp->un.ulpWord[5],
3026 *(uint32_t *)&irsp->un1,
3027 *((uint32_t *)&irsp->un1 + 1));
3030 switch (type) {
3031 case LPFC_ABORT_IOCB:
3032 case LPFC_SOL_IOCB:
3034 * Idle exchange closed via ABTS from port. No iocb
3035 * resources need to be recovered.
3037 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3038 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3039 "0333 IOCB cmd 0x%x"
3040 " processed. Skipping"
3041 " completion\n",
3042 irsp->ulpCommand);
3043 break;
3046 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3047 &rspiocbq);
3048 if (unlikely(!cmdiocbq))
3049 break;
3050 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3051 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3052 if (cmdiocbq->iocb_cmpl) {
3053 spin_unlock_irqrestore(&phba->hbalock, iflag);
3054 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3055 &rspiocbq);
3056 spin_lock_irqsave(&phba->hbalock, iflag);
3058 break;
3059 case LPFC_UNSOL_IOCB:
3060 spin_unlock_irqrestore(&phba->hbalock, iflag);
3061 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3062 spin_lock_irqsave(&phba->hbalock, iflag);
3063 break;
3064 default:
3065 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3066 char adaptermsg[LPFC_MAX_ADPTMSG];
3067 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3068 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3069 MAX_MSG_DATA);
3070 dev_warn(&((phba->pcidev)->dev),
3071 "lpfc%d: %s\n",
3072 phba->brd_no, adaptermsg);
3073 } else {
3074 /* Unknown IOCB command */
3075 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3076 "0334 Unknown IOCB command "
3077 "Data: x%x, x%x x%x x%x x%x\n",
3078 type, irsp->ulpCommand,
3079 irsp->ulpStatus,
3080 irsp->ulpIoTag,
3081 irsp->ulpContext);
3083 break;
3087 * The response IOCB has been processed. Update the ring
3088 * pointer in SLIM. If the port response put pointer has not
3089 * been updated, sync the pgp->rspPutInx and fetch the new port
3090 * response put pointer.
3092 writel(pring->sli.sli3.rspidx,
3093 &phba->host_gp[pring->ringno].rspGetInx);
3095 if (pring->sli.sli3.rspidx == portRspPut)
3096 portRspPut = le32_to_cpu(pgp->rspPutInx);
3099 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3100 pring->stats.iocb_rsp_full++;
3101 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3102 writel(status, phba->CAregaddr);
3103 readl(phba->CAregaddr);
3105 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3106 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3107 pring->stats.iocb_cmd_empty++;
3109 /* Force update of the local copy of cmdGetInx */
3110 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3111 lpfc_sli_resume_iocb(phba, pring);
3113 if ((pring->lpfc_sli_cmd_available))
3114 (pring->lpfc_sli_cmd_available) (phba, pring);
3118 phba->fcp_ring_in_use = 0;
3119 spin_unlock_irqrestore(&phba->hbalock, iflag);
3120 return rc;
3124 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3125 * @phba: Pointer to HBA context object.
3126 * @pring: Pointer to driver SLI ring object.
3127 * @rspiocbp: Pointer to driver response IOCB object.
3129 * This function is called from the worker thread when there is a slow-path
3130 * response IOCB to process. This function chains all the response iocbs until
3131 * seeing the iocb with the LE bit set. The function will call
3132 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3133 * completion of a command iocb. The function will call the
3134 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3135 * The function frees the resources or calls the completion handler if this
3136 * iocb is an abort completion. The function returns NULL when the response
3137 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3138 * this function shall chain the iocb on to the iocb_continueq and return the
3139 * response iocb passed in.
3141 static struct lpfc_iocbq *
3142 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3143 struct lpfc_iocbq *rspiocbp)
3145 struct lpfc_iocbq *saveq;
3146 struct lpfc_iocbq *cmdiocbp;
3147 struct lpfc_iocbq *next_iocb;
3148 IOCB_t *irsp = NULL;
3149 uint32_t free_saveq;
3150 uint8_t iocb_cmd_type;
3151 lpfc_iocb_type type;
3152 unsigned long iflag;
3153 int rc;
3155 spin_lock_irqsave(&phba->hbalock, iflag);
3156 /* First add the response iocb to the countinueq list */
3157 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3158 pring->iocb_continueq_cnt++;
3160 /* Now, determine whether the list is completed for processing */
3161 irsp = &rspiocbp->iocb;
3162 if (irsp->ulpLe) {
3164 * By default, the driver expects to free all resources
3165 * associated with this iocb completion.
3167 free_saveq = 1;
3168 saveq = list_get_first(&pring->iocb_continueq,
3169 struct lpfc_iocbq, list);
3170 irsp = &(saveq->iocb);
3171 list_del_init(&pring->iocb_continueq);
3172 pring->iocb_continueq_cnt = 0;
3174 pring->stats.iocb_rsp++;
3177 * If resource errors reported from HBA, reduce
3178 * queuedepths of the SCSI device.
3180 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3181 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3182 IOERR_NO_RESOURCES)) {
3183 spin_unlock_irqrestore(&phba->hbalock, iflag);
3184 phba->lpfc_rampdown_queue_depth(phba);
3185 spin_lock_irqsave(&phba->hbalock, iflag);
3188 if (irsp->ulpStatus) {
3189 /* Rsp ring <ringno> error: IOCB */
3190 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3191 "0328 Rsp Ring %d error: "
3192 "IOCB Data: "
3193 "x%x x%x x%x x%x "
3194 "x%x x%x x%x x%x "
3195 "x%x x%x x%x x%x "
3196 "x%x x%x x%x x%x\n",
3197 pring->ringno,
3198 irsp->un.ulpWord[0],
3199 irsp->un.ulpWord[1],
3200 irsp->un.ulpWord[2],
3201 irsp->un.ulpWord[3],
3202 irsp->un.ulpWord[4],
3203 irsp->un.ulpWord[5],
3204 *(((uint32_t *) irsp) + 6),
3205 *(((uint32_t *) irsp) + 7),
3206 *(((uint32_t *) irsp) + 8),
3207 *(((uint32_t *) irsp) + 9),
3208 *(((uint32_t *) irsp) + 10),
3209 *(((uint32_t *) irsp) + 11),
3210 *(((uint32_t *) irsp) + 12),
3211 *(((uint32_t *) irsp) + 13),
3212 *(((uint32_t *) irsp) + 14),
3213 *(((uint32_t *) irsp) + 15));
3217 * Fetch the IOCB command type and call the correct completion
3218 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3219 * get freed back to the lpfc_iocb_list by the discovery
3220 * kernel thread.
3222 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3223 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3224 switch (type) {
3225 case LPFC_SOL_IOCB:
3226 spin_unlock_irqrestore(&phba->hbalock, iflag);
3227 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3228 spin_lock_irqsave(&phba->hbalock, iflag);
3229 break;
3231 case LPFC_UNSOL_IOCB:
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
3233 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3234 spin_lock_irqsave(&phba->hbalock, iflag);
3235 if (!rc)
3236 free_saveq = 0;
3237 break;
3239 case LPFC_ABORT_IOCB:
3240 cmdiocbp = NULL;
3241 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3242 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3243 saveq);
3244 if (cmdiocbp) {
3245 /* Call the specified completion routine */
3246 if (cmdiocbp->iocb_cmpl) {
3247 spin_unlock_irqrestore(&phba->hbalock,
3248 iflag);
3249 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3250 saveq);
3251 spin_lock_irqsave(&phba->hbalock,
3252 iflag);
3253 } else
3254 __lpfc_sli_release_iocbq(phba,
3255 cmdiocbp);
3257 break;
3259 case LPFC_UNKNOWN_IOCB:
3260 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3261 char adaptermsg[LPFC_MAX_ADPTMSG];
3262 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3263 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3264 MAX_MSG_DATA);
3265 dev_warn(&((phba->pcidev)->dev),
3266 "lpfc%d: %s\n",
3267 phba->brd_no, adaptermsg);
3268 } else {
3269 /* Unknown IOCB command */
3270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3271 "0335 Unknown IOCB "
3272 "command Data: x%x "
3273 "x%x x%x x%x\n",
3274 irsp->ulpCommand,
3275 irsp->ulpStatus,
3276 irsp->ulpIoTag,
3277 irsp->ulpContext);
3279 break;
3282 if (free_saveq) {
3283 list_for_each_entry_safe(rspiocbp, next_iocb,
3284 &saveq->list, list) {
3285 list_del_init(&rspiocbp->list);
3286 __lpfc_sli_release_iocbq(phba, rspiocbp);
3288 __lpfc_sli_release_iocbq(phba, saveq);
3290 rspiocbp = NULL;
3292 spin_unlock_irqrestore(&phba->hbalock, iflag);
3293 return rspiocbp;
3297 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3298 * @phba: Pointer to HBA context object.
3299 * @pring: Pointer to driver SLI ring object.
3300 * @mask: Host attention register mask for this ring.
3302 * This routine wraps the actual slow_ring event process routine from the
3303 * API jump table function pointer from the lpfc_hba struct.
3305 void
3306 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3307 struct lpfc_sli_ring *pring, uint32_t mask)
3309 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3313 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3314 * @phba: Pointer to HBA context object.
3315 * @pring: Pointer to driver SLI ring object.
3316 * @mask: Host attention register mask for this ring.
3318 * This function is called from the worker thread when there is a ring event
3319 * for non-fcp rings. The caller does not hold any lock. The function will
3320 * remove each response iocb in the response ring and calls the handle
3321 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3323 static void
3324 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3325 struct lpfc_sli_ring *pring, uint32_t mask)
3327 struct lpfc_pgp *pgp;
3328 IOCB_t *entry;
3329 IOCB_t *irsp = NULL;
3330 struct lpfc_iocbq *rspiocbp = NULL;
3331 uint32_t portRspPut, portRspMax;
3332 unsigned long iflag;
3333 uint32_t status;
3335 pgp = &phba->port_gp[pring->ringno];
3336 spin_lock_irqsave(&phba->hbalock, iflag);
3337 pring->stats.iocb_event++;
3340 * The next available response entry should never exceed the maximum
3341 * entries. If it does, treat it as an adapter hardware error.
3343 portRspMax = pring->sli.sli3.numRiocb;
3344 portRspPut = le32_to_cpu(pgp->rspPutInx);
3345 if (portRspPut >= portRspMax) {
3347 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3348 * rsp ring <portRspMax>
3350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3351 "0303 Ring %d handler: portRspPut %d "
3352 "is bigger than rsp ring %d\n",
3353 pring->ringno, portRspPut, portRspMax);
3355 phba->link_state = LPFC_HBA_ERROR;
3356 spin_unlock_irqrestore(&phba->hbalock, iflag);
3358 phba->work_hs = HS_FFER3;
3359 lpfc_handle_eratt(phba);
3361 return;
3364 rmb();
3365 while (pring->sli.sli3.rspidx != portRspPut) {
3367 * Build a completion list and call the appropriate handler.
3368 * The process is to get the next available response iocb, get
3369 * a free iocb from the list, copy the response data into the
3370 * free iocb, insert to the continuation list, and update the
3371 * next response index to slim. This process makes response
3372 * iocb's in the ring available to DMA as fast as possible but
3373 * pays a penalty for a copy operation. Since the iocb is
3374 * only 32 bytes, this penalty is considered small relative to
3375 * the PCI reads for register values and a slim write. When
3376 * the ulpLe field is set, the entire Command has been
3377 * received.
3379 entry = lpfc_resp_iocb(phba, pring);
3381 phba->last_completion_time = jiffies;
3382 rspiocbp = __lpfc_sli_get_iocbq(phba);
3383 if (rspiocbp == NULL) {
3384 printk(KERN_ERR "%s: out of buffers! Failing "
3385 "completion.\n", __func__);
3386 break;
3389 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3390 phba->iocb_rsp_size);
3391 irsp = &rspiocbp->iocb;
3393 if (++pring->sli.sli3.rspidx >= portRspMax)
3394 pring->sli.sli3.rspidx = 0;
3396 if (pring->ringno == LPFC_ELS_RING) {
3397 lpfc_debugfs_slow_ring_trc(phba,
3398 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3399 *(((uint32_t *) irsp) + 4),
3400 *(((uint32_t *) irsp) + 6),
3401 *(((uint32_t *) irsp) + 7));
3404 writel(pring->sli.sli3.rspidx,
3405 &phba->host_gp[pring->ringno].rspGetInx);
3407 spin_unlock_irqrestore(&phba->hbalock, iflag);
3408 /* Handle the response IOCB */
3409 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3410 spin_lock_irqsave(&phba->hbalock, iflag);
3413 * If the port response put pointer has not been updated, sync
3414 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3415 * response put pointer.
3417 if (pring->sli.sli3.rspidx == portRspPut) {
3418 portRspPut = le32_to_cpu(pgp->rspPutInx);
3420 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3422 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3423 /* At least one response entry has been freed */
3424 pring->stats.iocb_rsp_full++;
3425 /* SET RxRE_RSP in Chip Att register */
3426 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3427 writel(status, phba->CAregaddr);
3428 readl(phba->CAregaddr); /* flush */
3430 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3431 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3432 pring->stats.iocb_cmd_empty++;
3434 /* Force update of the local copy of cmdGetInx */
3435 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3436 lpfc_sli_resume_iocb(phba, pring);
3438 if ((pring->lpfc_sli_cmd_available))
3439 (pring->lpfc_sli_cmd_available) (phba, pring);
3443 spin_unlock_irqrestore(&phba->hbalock, iflag);
3444 return;
3448 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3449 * @phba: Pointer to HBA context object.
3450 * @pring: Pointer to driver SLI ring object.
3451 * @mask: Host attention register mask for this ring.
3453 * This function is called from the worker thread when there is a pending
3454 * ELS response iocb on the driver internal slow-path response iocb worker
3455 * queue. The caller does not hold any lock. The function will remove each
3456 * response iocb from the response worker queue and calls the handle
3457 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3459 static void
3460 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3461 struct lpfc_sli_ring *pring, uint32_t mask)
3463 struct lpfc_iocbq *irspiocbq;
3464 struct hbq_dmabuf *dmabuf;
3465 struct lpfc_cq_event *cq_event;
3466 unsigned long iflag;
3468 spin_lock_irqsave(&phba->hbalock, iflag);
3469 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3470 spin_unlock_irqrestore(&phba->hbalock, iflag);
3471 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3472 /* Get the response iocb from the head of work queue */
3473 spin_lock_irqsave(&phba->hbalock, iflag);
3474 list_remove_head(&phba->sli4_hba.sp_queue_event,
3475 cq_event, struct lpfc_cq_event, list);
3476 spin_unlock_irqrestore(&phba->hbalock, iflag);
3478 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3479 case CQE_CODE_COMPL_WQE:
3480 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3481 cq_event);
3482 /* Translate ELS WCQE to response IOCBQ */
3483 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3484 irspiocbq);
3485 if (irspiocbq)
3486 lpfc_sli_sp_handle_rspiocb(phba, pring,
3487 irspiocbq);
3488 break;
3489 case CQE_CODE_RECEIVE:
3490 case CQE_CODE_RECEIVE_V1:
3491 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3492 cq_event);
3493 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3494 break;
3495 default:
3496 break;
3502 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3503 * @phba: Pointer to HBA context object.
3504 * @pring: Pointer to driver SLI ring object.
3506 * This function aborts all iocbs in the given ring and frees all the iocb
3507 * objects in txq. This function issues an abort iocb for all the iocb commands
3508 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3509 * the return of this function. The caller is not required to hold any locks.
3511 void
3512 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3514 LIST_HEAD(completions);
3515 struct lpfc_iocbq *iocb, *next_iocb;
3517 if (pring->ringno == LPFC_ELS_RING) {
3518 lpfc_fabric_abort_hba(phba);
3521 /* Error everything on txq and txcmplq
3522 * First do the txq.
3524 spin_lock_irq(&phba->hbalock);
3525 list_splice_init(&pring->txq, &completions);
3527 /* Next issue ABTS for everything on the txcmplq */
3528 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3529 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3531 spin_unlock_irq(&phba->hbalock);
3533 /* Cancel all the IOCBs from the completions list */
3534 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3535 IOERR_SLI_ABORTED);
3539 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3540 * @phba: Pointer to HBA context object.
3542 * This function flushes all iocbs in the fcp ring and frees all the iocb
3543 * objects in txq and txcmplq. This function will not issue abort iocbs
3544 * for all the iocb commands in txcmplq, they will just be returned with
3545 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3546 * slot has been permanently disabled.
3548 void
3549 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3551 LIST_HEAD(txq);
3552 LIST_HEAD(txcmplq);
3553 struct lpfc_sli *psli = &phba->sli;
3554 struct lpfc_sli_ring *pring;
3556 /* Currently, only one fcp ring */
3557 pring = &psli->ring[psli->fcp_ring];
3559 spin_lock_irq(&phba->hbalock);
3560 /* Retrieve everything on txq */
3561 list_splice_init(&pring->txq, &txq);
3563 /* Retrieve everything on the txcmplq */
3564 list_splice_init(&pring->txcmplq, &txcmplq);
3566 /* Indicate the I/O queues are flushed */
3567 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3568 spin_unlock_irq(&phba->hbalock);
3570 /* Flush the txq */
3571 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3572 IOERR_SLI_DOWN);
3574 /* Flush the txcmpq */
3575 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3576 IOERR_SLI_DOWN);
3580 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3581 * @phba: Pointer to HBA context object.
3582 * @mask: Bit mask to be checked.
3584 * This function reads the host status register and compares
3585 * with the provided bit mask to check if HBA completed
3586 * the restart. This function will wait in a loop for the
3587 * HBA to complete restart. If the HBA does not restart within
3588 * 15 iterations, the function will reset the HBA again. The
3589 * function returns 1 when HBA fail to restart otherwise returns
3590 * zero.
3592 static int
3593 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3595 uint32_t status;
3596 int i = 0;
3597 int retval = 0;
3599 /* Read the HBA Host Status Register */
3600 if (lpfc_readl(phba->HSregaddr, &status))
3601 return 1;
3604 * Check status register every 100ms for 5 retries, then every
3605 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3606 * every 2.5 sec for 4.
3607 * Break our of the loop if errors occurred during init.
3609 while (((status & mask) != mask) &&
3610 !(status & HS_FFERM) &&
3611 i++ < 20) {
3613 if (i <= 5)
3614 msleep(10);
3615 else if (i <= 10)
3616 msleep(500);
3617 else
3618 msleep(2500);
3620 if (i == 15) {
3621 /* Do post */
3622 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3623 lpfc_sli_brdrestart(phba);
3625 /* Read the HBA Host Status Register */
3626 if (lpfc_readl(phba->HSregaddr, &status)) {
3627 retval = 1;
3628 break;
3632 /* Check to see if any errors occurred during init */
3633 if ((status & HS_FFERM) || (i >= 20)) {
3634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3635 "2751 Adapter failed to restart, "
3636 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3637 status,
3638 readl(phba->MBslimaddr + 0xa8),
3639 readl(phba->MBslimaddr + 0xac));
3640 phba->link_state = LPFC_HBA_ERROR;
3641 retval = 1;
3644 return retval;
3648 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3649 * @phba: Pointer to HBA context object.
3650 * @mask: Bit mask to be checked.
3652 * This function checks the host status register to check if HBA is
3653 * ready. This function will wait in a loop for the HBA to be ready
3654 * If the HBA is not ready , the function will will reset the HBA PCI
3655 * function again. The function returns 1 when HBA fail to be ready
3656 * otherwise returns zero.
3658 static int
3659 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3661 uint32_t status;
3662 int retval = 0;
3664 /* Read the HBA Host Status Register */
3665 status = lpfc_sli4_post_status_check(phba);
3667 if (status) {
3668 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3669 lpfc_sli_brdrestart(phba);
3670 status = lpfc_sli4_post_status_check(phba);
3673 /* Check to see if any errors occurred during init */
3674 if (status) {
3675 phba->link_state = LPFC_HBA_ERROR;
3676 retval = 1;
3677 } else
3678 phba->sli4_hba.intr_enable = 0;
3680 return retval;
3684 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3685 * @phba: Pointer to HBA context object.
3686 * @mask: Bit mask to be checked.
3688 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3689 * from the API jump table function pointer from the lpfc_hba struct.
3692 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3694 return phba->lpfc_sli_brdready(phba, mask);
3697 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3700 * lpfc_reset_barrier - Make HBA ready for HBA reset
3701 * @phba: Pointer to HBA context object.
3703 * This function is called before resetting an HBA. This function is called
3704 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3706 void lpfc_reset_barrier(struct lpfc_hba *phba)
3708 uint32_t __iomem *resp_buf;
3709 uint32_t __iomem *mbox_buf;
3710 volatile uint32_t mbox;
3711 uint32_t hc_copy, ha_copy, resp_data;
3712 int i;
3713 uint8_t hdrtype;
3715 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3716 if (hdrtype != 0x80 ||
3717 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3718 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3719 return;
3722 * Tell the other part of the chip to suspend temporarily all
3723 * its DMA activity.
3725 resp_buf = phba->MBslimaddr;
3727 /* Disable the error attention */
3728 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3729 return;
3730 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3731 readl(phba->HCregaddr); /* flush */
3732 phba->link_flag |= LS_IGNORE_ERATT;
3734 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3735 return;
3736 if (ha_copy & HA_ERATT) {
3737 /* Clear Chip error bit */
3738 writel(HA_ERATT, phba->HAregaddr);
3739 phba->pport->stopped = 1;
3742 mbox = 0;
3743 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3744 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3746 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3747 mbox_buf = phba->MBslimaddr;
3748 writel(mbox, mbox_buf);
3750 for (i = 0; i < 50; i++) {
3751 if (lpfc_readl((resp_buf + 1), &resp_data))
3752 return;
3753 if (resp_data != ~(BARRIER_TEST_PATTERN))
3754 mdelay(1);
3755 else
3756 break;
3758 resp_data = 0;
3759 if (lpfc_readl((resp_buf + 1), &resp_data))
3760 return;
3761 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3762 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3763 phba->pport->stopped)
3764 goto restore_hc;
3765 else
3766 goto clear_errat;
3769 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3770 resp_data = 0;
3771 for (i = 0; i < 500; i++) {
3772 if (lpfc_readl(resp_buf, &resp_data))
3773 return;
3774 if (resp_data != mbox)
3775 mdelay(1);
3776 else
3777 break;
3780 clear_errat:
3782 while (++i < 500) {
3783 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3784 return;
3785 if (!(ha_copy & HA_ERATT))
3786 mdelay(1);
3787 else
3788 break;
3791 if (readl(phba->HAregaddr) & HA_ERATT) {
3792 writel(HA_ERATT, phba->HAregaddr);
3793 phba->pport->stopped = 1;
3796 restore_hc:
3797 phba->link_flag &= ~LS_IGNORE_ERATT;
3798 writel(hc_copy, phba->HCregaddr);
3799 readl(phba->HCregaddr); /* flush */
3803 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3804 * @phba: Pointer to HBA context object.
3806 * This function issues a kill_board mailbox command and waits for
3807 * the error attention interrupt. This function is called for stopping
3808 * the firmware processing. The caller is not required to hold any
3809 * locks. This function calls lpfc_hba_down_post function to free
3810 * any pending commands after the kill. The function will return 1 when it
3811 * fails to kill the board else will return 0.
3814 lpfc_sli_brdkill(struct lpfc_hba *phba)
3816 struct lpfc_sli *psli;
3817 LPFC_MBOXQ_t *pmb;
3818 uint32_t status;
3819 uint32_t ha_copy;
3820 int retval;
3821 int i = 0;
3823 psli = &phba->sli;
3825 /* Kill HBA */
3826 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3827 "0329 Kill HBA Data: x%x x%x\n",
3828 phba->pport->port_state, psli->sli_flag);
3830 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3831 if (!pmb)
3832 return 1;
3834 /* Disable the error attention */
3835 spin_lock_irq(&phba->hbalock);
3836 if (lpfc_readl(phba->HCregaddr, &status)) {
3837 spin_unlock_irq(&phba->hbalock);
3838 mempool_free(pmb, phba->mbox_mem_pool);
3839 return 1;
3841 status &= ~HC_ERINT_ENA;
3842 writel(status, phba->HCregaddr);
3843 readl(phba->HCregaddr); /* flush */
3844 phba->link_flag |= LS_IGNORE_ERATT;
3845 spin_unlock_irq(&phba->hbalock);
3847 lpfc_kill_board(phba, pmb);
3848 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3849 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3851 if (retval != MBX_SUCCESS) {
3852 if (retval != MBX_BUSY)
3853 mempool_free(pmb, phba->mbox_mem_pool);
3854 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3855 "2752 KILL_BOARD command failed retval %d\n",
3856 retval);
3857 spin_lock_irq(&phba->hbalock);
3858 phba->link_flag &= ~LS_IGNORE_ERATT;
3859 spin_unlock_irq(&phba->hbalock);
3860 return 1;
3863 spin_lock_irq(&phba->hbalock);
3864 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3865 spin_unlock_irq(&phba->hbalock);
3867 mempool_free(pmb, phba->mbox_mem_pool);
3869 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3870 * attention every 100ms for 3 seconds. If we don't get ERATT after
3871 * 3 seconds we still set HBA_ERROR state because the status of the
3872 * board is now undefined.
3874 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3875 return 1;
3876 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3877 mdelay(100);
3878 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3879 return 1;
3882 del_timer_sync(&psli->mbox_tmo);
3883 if (ha_copy & HA_ERATT) {
3884 writel(HA_ERATT, phba->HAregaddr);
3885 phba->pport->stopped = 1;
3887 spin_lock_irq(&phba->hbalock);
3888 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3889 psli->mbox_active = NULL;
3890 phba->link_flag &= ~LS_IGNORE_ERATT;
3891 spin_unlock_irq(&phba->hbalock);
3893 lpfc_hba_down_post(phba);
3894 phba->link_state = LPFC_HBA_ERROR;
3896 return ha_copy & HA_ERATT ? 0 : 1;
3900 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3901 * @phba: Pointer to HBA context object.
3903 * This function resets the HBA by writing HC_INITFF to the control
3904 * register. After the HBA resets, this function resets all the iocb ring
3905 * indices. This function disables PCI layer parity checking during
3906 * the reset.
3907 * This function returns 0 always.
3908 * The caller is not required to hold any locks.
3911 lpfc_sli_brdreset(struct lpfc_hba *phba)
3913 struct lpfc_sli *psli;
3914 struct lpfc_sli_ring *pring;
3915 uint16_t cfg_value;
3916 int i;
3918 psli = &phba->sli;
3920 /* Reset HBA */
3921 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3922 "0325 Reset HBA Data: x%x x%x\n",
3923 phba->pport->port_state, psli->sli_flag);
3925 /* perform board reset */
3926 phba->fc_eventTag = 0;
3927 phba->link_events = 0;
3928 phba->pport->fc_myDID = 0;
3929 phba->pport->fc_prevDID = 0;
3931 /* Turn off parity checking and serr during the physical reset */
3932 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3933 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3934 (cfg_value &
3935 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3937 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3939 /* Now toggle INITFF bit in the Host Control Register */
3940 writel(HC_INITFF, phba->HCregaddr);
3941 mdelay(1);
3942 readl(phba->HCregaddr); /* flush */
3943 writel(0, phba->HCregaddr);
3944 readl(phba->HCregaddr); /* flush */
3946 /* Restore PCI cmd register */
3947 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3949 /* Initialize relevant SLI info */
3950 for (i = 0; i < psli->num_rings; i++) {
3951 pring = &psli->ring[i];
3952 pring->flag = 0;
3953 pring->sli.sli3.rspidx = 0;
3954 pring->sli.sli3.next_cmdidx = 0;
3955 pring->sli.sli3.local_getidx = 0;
3956 pring->sli.sli3.cmdidx = 0;
3957 pring->missbufcnt = 0;
3960 phba->link_state = LPFC_WARM_START;
3961 return 0;
3965 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3966 * @phba: Pointer to HBA context object.
3968 * This function resets a SLI4 HBA. This function disables PCI layer parity
3969 * checking during resets the device. The caller is not required to hold
3970 * any locks.
3972 * This function returns 0 always.
3975 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3977 struct lpfc_sli *psli = &phba->sli;
3978 uint16_t cfg_value;
3979 int rc;
3981 /* Reset HBA */
3982 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3983 "0295 Reset HBA Data: x%x x%x\n",
3984 phba->pport->port_state, psli->sli_flag);
3986 /* perform board reset */
3987 phba->fc_eventTag = 0;
3988 phba->link_events = 0;
3989 phba->pport->fc_myDID = 0;
3990 phba->pport->fc_prevDID = 0;
3992 spin_lock_irq(&phba->hbalock);
3993 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3994 phba->fcf.fcf_flag = 0;
3995 spin_unlock_irq(&phba->hbalock);
3997 /* Now physically reset the device */
3998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3999 "0389 Performing PCI function reset!\n");
4001 /* Turn off parity checking and serr during the physical reset */
4002 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4003 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4004 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4006 /* Perform FCoE PCI function reset before freeing queue memory */
4007 rc = lpfc_pci_function_reset(phba);
4008 lpfc_sli4_queue_destroy(phba);
4010 /* Restore PCI cmd register */
4011 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4013 return rc;
4017 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4018 * @phba: Pointer to HBA context object.
4020 * This function is called in the SLI initialization code path to
4021 * restart the HBA. The caller is not required to hold any lock.
4022 * This function writes MBX_RESTART mailbox command to the SLIM and
4023 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4024 * function to free any pending commands. The function enables
4025 * POST only during the first initialization. The function returns zero.
4026 * The function does not guarantee completion of MBX_RESTART mailbox
4027 * command before the return of this function.
4029 static int
4030 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4032 MAILBOX_t *mb;
4033 struct lpfc_sli *psli;
4034 volatile uint32_t word0;
4035 void __iomem *to_slim;
4036 uint32_t hba_aer_enabled;
4038 spin_lock_irq(&phba->hbalock);
4040 /* Take PCIe device Advanced Error Reporting (AER) state */
4041 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4043 psli = &phba->sli;
4045 /* Restart HBA */
4046 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4047 "0337 Restart HBA Data: x%x x%x\n",
4048 phba->pport->port_state, psli->sli_flag);
4050 word0 = 0;
4051 mb = (MAILBOX_t *) &word0;
4052 mb->mbxCommand = MBX_RESTART;
4053 mb->mbxHc = 1;
4055 lpfc_reset_barrier(phba);
4057 to_slim = phba->MBslimaddr;
4058 writel(*(uint32_t *) mb, to_slim);
4059 readl(to_slim); /* flush */
4061 /* Only skip post after fc_ffinit is completed */
4062 if (phba->pport->port_state)
4063 word0 = 1; /* This is really setting up word1 */
4064 else
4065 word0 = 0; /* This is really setting up word1 */
4066 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4067 writel(*(uint32_t *) mb, to_slim);
4068 readl(to_slim); /* flush */
4070 lpfc_sli_brdreset(phba);
4071 phba->pport->stopped = 0;
4072 phba->link_state = LPFC_INIT_START;
4073 phba->hba_flag = 0;
4074 spin_unlock_irq(&phba->hbalock);
4076 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4077 psli->stats_start = get_seconds();
4079 /* Give the INITFF and Post time to settle. */
4080 mdelay(100);
4082 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4083 if (hba_aer_enabled)
4084 pci_disable_pcie_error_reporting(phba->pcidev);
4086 lpfc_hba_down_post(phba);
4088 return 0;
4092 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4093 * @phba: Pointer to HBA context object.
4095 * This function is called in the SLI initialization code path to restart
4096 * a SLI4 HBA. The caller is not required to hold any lock.
4097 * At the end of the function, it calls lpfc_hba_down_post function to
4098 * free any pending commands.
4100 static int
4101 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4103 struct lpfc_sli *psli = &phba->sli;
4104 uint32_t hba_aer_enabled;
4105 int rc;
4107 /* Restart HBA */
4108 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4109 "0296 Restart HBA Data: x%x x%x\n",
4110 phba->pport->port_state, psli->sli_flag);
4112 /* Take PCIe device Advanced Error Reporting (AER) state */
4113 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4115 rc = lpfc_sli4_brdreset(phba);
4117 spin_lock_irq(&phba->hbalock);
4118 phba->pport->stopped = 0;
4119 phba->link_state = LPFC_INIT_START;
4120 phba->hba_flag = 0;
4121 spin_unlock_irq(&phba->hbalock);
4123 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4124 psli->stats_start = get_seconds();
4126 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4127 if (hba_aer_enabled)
4128 pci_disable_pcie_error_reporting(phba->pcidev);
4130 lpfc_hba_down_post(phba);
4132 return rc;
4136 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4137 * @phba: Pointer to HBA context object.
4139 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4140 * API jump table function pointer from the lpfc_hba struct.
4143 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4145 return phba->lpfc_sli_brdrestart(phba);
4149 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4150 * @phba: Pointer to HBA context object.
4152 * This function is called after a HBA restart to wait for successful
4153 * restart of the HBA. Successful restart of the HBA is indicated by
4154 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4155 * iteration, the function will restart the HBA again. The function returns
4156 * zero if HBA successfully restarted else returns negative error code.
4158 static int
4159 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4161 uint32_t status, i = 0;
4163 /* Read the HBA Host Status Register */
4164 if (lpfc_readl(phba->HSregaddr, &status))
4165 return -EIO;
4167 /* Check status register to see what current state is */
4168 i = 0;
4169 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4171 /* Check every 10ms for 10 retries, then every 100ms for 90
4172 * retries, then every 1 sec for 50 retires for a total of
4173 * ~60 seconds before reset the board again and check every
4174 * 1 sec for 50 retries. The up to 60 seconds before the
4175 * board ready is required by the Falcon FIPS zeroization
4176 * complete, and any reset the board in between shall cause
4177 * restart of zeroization, further delay the board ready.
4179 if (i++ >= 200) {
4180 /* Adapter failed to init, timeout, status reg
4181 <status> */
4182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4183 "0436 Adapter failed to init, "
4184 "timeout, status reg x%x, "
4185 "FW Data: A8 x%x AC x%x\n", status,
4186 readl(phba->MBslimaddr + 0xa8),
4187 readl(phba->MBslimaddr + 0xac));
4188 phba->link_state = LPFC_HBA_ERROR;
4189 return -ETIMEDOUT;
4192 /* Check to see if any errors occurred during init */
4193 if (status & HS_FFERM) {
4194 /* ERROR: During chipset initialization */
4195 /* Adapter failed to init, chipset, status reg
4196 <status> */
4197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4198 "0437 Adapter failed to init, "
4199 "chipset, status reg x%x, "
4200 "FW Data: A8 x%x AC x%x\n", status,
4201 readl(phba->MBslimaddr + 0xa8),
4202 readl(phba->MBslimaddr + 0xac));
4203 phba->link_state = LPFC_HBA_ERROR;
4204 return -EIO;
4207 if (i <= 10)
4208 msleep(10);
4209 else if (i <= 100)
4210 msleep(100);
4211 else
4212 msleep(1000);
4214 if (i == 150) {
4215 /* Do post */
4216 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4217 lpfc_sli_brdrestart(phba);
4219 /* Read the HBA Host Status Register */
4220 if (lpfc_readl(phba->HSregaddr, &status))
4221 return -EIO;
4224 /* Check to see if any errors occurred during init */
4225 if (status & HS_FFERM) {
4226 /* ERROR: During chipset initialization */
4227 /* Adapter failed to init, chipset, status reg <status> */
4228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4229 "0438 Adapter failed to init, chipset, "
4230 "status reg x%x, "
4231 "FW Data: A8 x%x AC x%x\n", status,
4232 readl(phba->MBslimaddr + 0xa8),
4233 readl(phba->MBslimaddr + 0xac));
4234 phba->link_state = LPFC_HBA_ERROR;
4235 return -EIO;
4238 /* Clear all interrupt enable conditions */
4239 writel(0, phba->HCregaddr);
4240 readl(phba->HCregaddr); /* flush */
4242 /* setup host attn register */
4243 writel(0xffffffff, phba->HAregaddr);
4244 readl(phba->HAregaddr); /* flush */
4245 return 0;
4249 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4251 * This function calculates and returns the number of HBQs required to be
4252 * configured.
4255 lpfc_sli_hbq_count(void)
4257 return ARRAY_SIZE(lpfc_hbq_defs);
4261 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4263 * This function adds the number of hbq entries in every HBQ to get
4264 * the total number of hbq entries required for the HBA and returns
4265 * the total count.
4267 static int
4268 lpfc_sli_hbq_entry_count(void)
4270 int hbq_count = lpfc_sli_hbq_count();
4271 int count = 0;
4272 int i;
4274 for (i = 0; i < hbq_count; ++i)
4275 count += lpfc_hbq_defs[i]->entry_count;
4276 return count;
4280 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4282 * This function calculates amount of memory required for all hbq entries
4283 * to be configured and returns the total memory required.
4286 lpfc_sli_hbq_size(void)
4288 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4292 * lpfc_sli_hbq_setup - configure and initialize HBQs
4293 * @phba: Pointer to HBA context object.
4295 * This function is called during the SLI initialization to configure
4296 * all the HBQs and post buffers to the HBQ. The caller is not
4297 * required to hold any locks. This function will return zero if successful
4298 * else it will return negative error code.
4300 static int
4301 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4303 int hbq_count = lpfc_sli_hbq_count();
4304 LPFC_MBOXQ_t *pmb;
4305 MAILBOX_t *pmbox;
4306 uint32_t hbqno;
4307 uint32_t hbq_entry_index;
4309 /* Get a Mailbox buffer to setup mailbox
4310 * commands for HBA initialization
4312 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4314 if (!pmb)
4315 return -ENOMEM;
4317 pmbox = &pmb->u.mb;
4319 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4320 phba->link_state = LPFC_INIT_MBX_CMDS;
4321 phba->hbq_in_use = 1;
4323 hbq_entry_index = 0;
4324 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4325 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4326 phba->hbqs[hbqno].hbqPutIdx = 0;
4327 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4328 phba->hbqs[hbqno].entry_count =
4329 lpfc_hbq_defs[hbqno]->entry_count;
4330 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4331 hbq_entry_index, pmb);
4332 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4334 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4335 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4336 mbxStatus <status>, ring <num> */
4338 lpfc_printf_log(phba, KERN_ERR,
4339 LOG_SLI | LOG_VPORT,
4340 "1805 Adapter failed to init. "
4341 "Data: x%x x%x x%x\n",
4342 pmbox->mbxCommand,
4343 pmbox->mbxStatus, hbqno);
4345 phba->link_state = LPFC_HBA_ERROR;
4346 mempool_free(pmb, phba->mbox_mem_pool);
4347 return -ENXIO;
4350 phba->hbq_count = hbq_count;
4352 mempool_free(pmb, phba->mbox_mem_pool);
4354 /* Initially populate or replenish the HBQs */
4355 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4356 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4357 return 0;
4361 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4362 * @phba: Pointer to HBA context object.
4364 * This function is called during the SLI initialization to configure
4365 * all the HBQs and post buffers to the HBQ. The caller is not
4366 * required to hold any locks. This function will return zero if successful
4367 * else it will return negative error code.
4369 static int
4370 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4372 phba->hbq_in_use = 1;
4373 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4374 phba->hbq_count = 1;
4375 /* Initially populate or replenish the HBQs */
4376 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4377 return 0;
4381 * lpfc_sli_config_port - Issue config port mailbox command
4382 * @phba: Pointer to HBA context object.
4383 * @sli_mode: sli mode - 2/3
4385 * This function is called by the sli intialization code path
4386 * to issue config_port mailbox command. This function restarts the
4387 * HBA firmware and issues a config_port mailbox command to configure
4388 * the SLI interface in the sli mode specified by sli_mode
4389 * variable. The caller is not required to hold any locks.
4390 * The function returns 0 if successful, else returns negative error
4391 * code.
4394 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4396 LPFC_MBOXQ_t *pmb;
4397 uint32_t resetcount = 0, rc = 0, done = 0;
4399 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4400 if (!pmb) {
4401 phba->link_state = LPFC_HBA_ERROR;
4402 return -ENOMEM;
4405 phba->sli_rev = sli_mode;
4406 while (resetcount < 2 && !done) {
4407 spin_lock_irq(&phba->hbalock);
4408 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4409 spin_unlock_irq(&phba->hbalock);
4410 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4411 lpfc_sli_brdrestart(phba);
4412 rc = lpfc_sli_chipset_init(phba);
4413 if (rc)
4414 break;
4416 spin_lock_irq(&phba->hbalock);
4417 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4418 spin_unlock_irq(&phba->hbalock);
4419 resetcount++;
4421 /* Call pre CONFIG_PORT mailbox command initialization. A
4422 * value of 0 means the call was successful. Any other
4423 * nonzero value is a failure, but if ERESTART is returned,
4424 * the driver may reset the HBA and try again.
4426 rc = lpfc_config_port_prep(phba);
4427 if (rc == -ERESTART) {
4428 phba->link_state = LPFC_LINK_UNKNOWN;
4429 continue;
4430 } else if (rc)
4431 break;
4433 phba->link_state = LPFC_INIT_MBX_CMDS;
4434 lpfc_config_port(phba, pmb);
4435 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4436 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4437 LPFC_SLI3_HBQ_ENABLED |
4438 LPFC_SLI3_CRP_ENABLED |
4439 LPFC_SLI3_BG_ENABLED |
4440 LPFC_SLI3_DSS_ENABLED);
4441 if (rc != MBX_SUCCESS) {
4442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4443 "0442 Adapter failed to init, mbxCmd x%x "
4444 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4445 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4446 spin_lock_irq(&phba->hbalock);
4447 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4448 spin_unlock_irq(&phba->hbalock);
4449 rc = -ENXIO;
4450 } else {
4451 /* Allow asynchronous mailbox command to go through */
4452 spin_lock_irq(&phba->hbalock);
4453 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4454 spin_unlock_irq(&phba->hbalock);
4455 done = 1;
4457 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4458 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4459 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4460 "3110 Port did not grant ASABT\n");
4463 if (!done) {
4464 rc = -EINVAL;
4465 goto do_prep_failed;
4467 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4468 if (!pmb->u.mb.un.varCfgPort.cMA) {
4469 rc = -ENXIO;
4470 goto do_prep_failed;
4472 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4473 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4474 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4475 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4476 phba->max_vpi : phba->max_vports;
4478 } else
4479 phba->max_vpi = 0;
4480 phba->fips_level = 0;
4481 phba->fips_spec_rev = 0;
4482 if (pmb->u.mb.un.varCfgPort.gdss) {
4483 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4484 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4485 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4487 "2850 Security Crypto Active. FIPS x%d "
4488 "(Spec Rev: x%d)",
4489 phba->fips_level, phba->fips_spec_rev);
4491 if (pmb->u.mb.un.varCfgPort.sec_err) {
4492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4493 "2856 Config Port Security Crypto "
4494 "Error: x%x ",
4495 pmb->u.mb.un.varCfgPort.sec_err);
4497 if (pmb->u.mb.un.varCfgPort.gerbm)
4498 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4499 if (pmb->u.mb.un.varCfgPort.gcrp)
4500 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4502 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4503 phba->port_gp = phba->mbox->us.s3_pgp.port;
4505 if (phba->cfg_enable_bg) {
4506 if (pmb->u.mb.un.varCfgPort.gbg)
4507 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4508 else
4509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4510 "0443 Adapter did not grant "
4511 "BlockGuard\n");
4513 } else {
4514 phba->hbq_get = NULL;
4515 phba->port_gp = phba->mbox->us.s2.port;
4516 phba->max_vpi = 0;
4518 do_prep_failed:
4519 mempool_free(pmb, phba->mbox_mem_pool);
4520 return rc;
4525 * lpfc_sli_hba_setup - SLI intialization function
4526 * @phba: Pointer to HBA context object.
4528 * This function is the main SLI intialization function. This function
4529 * is called by the HBA intialization code, HBA reset code and HBA
4530 * error attention handler code. Caller is not required to hold any
4531 * locks. This function issues config_port mailbox command to configure
4532 * the SLI, setup iocb rings and HBQ rings. In the end the function
4533 * calls the config_port_post function to issue init_link mailbox
4534 * command and to start the discovery. The function will return zero
4535 * if successful, else it will return negative error code.
4538 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4540 uint32_t rc;
4541 int mode = 3, i;
4542 int longs;
4544 switch (lpfc_sli_mode) {
4545 case 2:
4546 if (phba->cfg_enable_npiv) {
4547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4548 "1824 NPIV enabled: Override lpfc_sli_mode "
4549 "parameter (%d) to auto (0).\n",
4550 lpfc_sli_mode);
4551 break;
4553 mode = 2;
4554 break;
4555 case 0:
4556 case 3:
4557 break;
4558 default:
4559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4560 "1819 Unrecognized lpfc_sli_mode "
4561 "parameter: %d.\n", lpfc_sli_mode);
4563 break;
4566 rc = lpfc_sli_config_port(phba, mode);
4568 if (rc && lpfc_sli_mode == 3)
4569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4570 "1820 Unable to select SLI-3. "
4571 "Not supported by adapter.\n");
4572 if (rc && mode != 2)
4573 rc = lpfc_sli_config_port(phba, 2);
4574 if (rc)
4575 goto lpfc_sli_hba_setup_error;
4577 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4578 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4579 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4580 if (!rc) {
4581 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4582 "2709 This device supports "
4583 "Advanced Error Reporting (AER)\n");
4584 spin_lock_irq(&phba->hbalock);
4585 phba->hba_flag |= HBA_AER_ENABLED;
4586 spin_unlock_irq(&phba->hbalock);
4587 } else {
4588 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4589 "2708 This device does not support "
4590 "Advanced Error Reporting (AER): %d\n",
4591 rc);
4592 phba->cfg_aer_support = 0;
4596 if (phba->sli_rev == 3) {
4597 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4598 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4599 } else {
4600 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4601 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4602 phba->sli3_options = 0;
4605 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4606 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4607 phba->sli_rev, phba->max_vpi);
4608 rc = lpfc_sli_ring_map(phba);
4610 if (rc)
4611 goto lpfc_sli_hba_setup_error;
4613 /* Initialize VPIs. */
4614 if (phba->sli_rev == LPFC_SLI_REV3) {
4616 * The VPI bitmask and physical ID array are allocated
4617 * and initialized once only - at driver load. A port
4618 * reset doesn't need to reinitialize this memory.
4620 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4621 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4622 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4623 GFP_KERNEL);
4624 if (!phba->vpi_bmask) {
4625 rc = -ENOMEM;
4626 goto lpfc_sli_hba_setup_error;
4629 phba->vpi_ids = kzalloc(
4630 (phba->max_vpi+1) * sizeof(uint16_t),
4631 GFP_KERNEL);
4632 if (!phba->vpi_ids) {
4633 kfree(phba->vpi_bmask);
4634 rc = -ENOMEM;
4635 goto lpfc_sli_hba_setup_error;
4637 for (i = 0; i < phba->max_vpi; i++)
4638 phba->vpi_ids[i] = i;
4642 /* Init HBQs */
4643 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4644 rc = lpfc_sli_hbq_setup(phba);
4645 if (rc)
4646 goto lpfc_sli_hba_setup_error;
4648 spin_lock_irq(&phba->hbalock);
4649 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4650 spin_unlock_irq(&phba->hbalock);
4652 rc = lpfc_config_port_post(phba);
4653 if (rc)
4654 goto lpfc_sli_hba_setup_error;
4656 return rc;
4658 lpfc_sli_hba_setup_error:
4659 phba->link_state = LPFC_HBA_ERROR;
4660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4661 "0445 Firmware initialization failed\n");
4662 return rc;
4666 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4667 * @phba: Pointer to HBA context object.
4668 * @mboxq: mailbox pointer.
4669 * This function issue a dump mailbox command to read config region
4670 * 23 and parse the records in the region and populate driver
4671 * data structure.
4673 static int
4674 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4676 LPFC_MBOXQ_t *mboxq;
4677 struct lpfc_dmabuf *mp;
4678 struct lpfc_mqe *mqe;
4679 uint32_t data_length;
4680 int rc;
4682 /* Program the default value of vlan_id and fc_map */
4683 phba->valid_vlan = 0;
4684 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4685 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4686 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4688 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4689 if (!mboxq)
4690 return -ENOMEM;
4692 mqe = &mboxq->u.mqe;
4693 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4694 rc = -ENOMEM;
4695 goto out_free_mboxq;
4698 mp = (struct lpfc_dmabuf *) mboxq->context1;
4699 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4701 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4702 "(%d):2571 Mailbox cmd x%x Status x%x "
4703 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4704 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4705 "CQ: x%x x%x x%x x%x\n",
4706 mboxq->vport ? mboxq->vport->vpi : 0,
4707 bf_get(lpfc_mqe_command, mqe),
4708 bf_get(lpfc_mqe_status, mqe),
4709 mqe->un.mb_words[0], mqe->un.mb_words[1],
4710 mqe->un.mb_words[2], mqe->un.mb_words[3],
4711 mqe->un.mb_words[4], mqe->un.mb_words[5],
4712 mqe->un.mb_words[6], mqe->un.mb_words[7],
4713 mqe->un.mb_words[8], mqe->un.mb_words[9],
4714 mqe->un.mb_words[10], mqe->un.mb_words[11],
4715 mqe->un.mb_words[12], mqe->un.mb_words[13],
4716 mqe->un.mb_words[14], mqe->un.mb_words[15],
4717 mqe->un.mb_words[16], mqe->un.mb_words[50],
4718 mboxq->mcqe.word0,
4719 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4720 mboxq->mcqe.trailer);
4722 if (rc) {
4723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4724 kfree(mp);
4725 rc = -EIO;
4726 goto out_free_mboxq;
4728 data_length = mqe->un.mb_words[5];
4729 if (data_length > DMP_RGN23_SIZE) {
4730 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4731 kfree(mp);
4732 rc = -EIO;
4733 goto out_free_mboxq;
4736 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4737 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4738 kfree(mp);
4739 rc = 0;
4741 out_free_mboxq:
4742 mempool_free(mboxq, phba->mbox_mem_pool);
4743 return rc;
4747 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4748 * @phba: pointer to lpfc hba data structure.
4749 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4750 * @vpd: pointer to the memory to hold resulting port vpd data.
4751 * @vpd_size: On input, the number of bytes allocated to @vpd.
4752 * On output, the number of data bytes in @vpd.
4754 * This routine executes a READ_REV SLI4 mailbox command. In
4755 * addition, this routine gets the port vpd data.
4757 * Return codes
4758 * 0 - successful
4759 * -ENOMEM - could not allocated memory.
4761 static int
4762 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4763 uint8_t *vpd, uint32_t *vpd_size)
4765 int rc = 0;
4766 uint32_t dma_size;
4767 struct lpfc_dmabuf *dmabuf;
4768 struct lpfc_mqe *mqe;
4770 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4771 if (!dmabuf)
4772 return -ENOMEM;
4775 * Get a DMA buffer for the vpd data resulting from the READ_REV
4776 * mailbox command.
4778 dma_size = *vpd_size;
4779 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4780 dma_size,
4781 &dmabuf->phys,
4782 GFP_KERNEL);
4783 if (!dmabuf->virt) {
4784 kfree(dmabuf);
4785 return -ENOMEM;
4787 memset(dmabuf->virt, 0, dma_size);
4790 * The SLI4 implementation of READ_REV conflicts at word1,
4791 * bits 31:16 and SLI4 adds vpd functionality not present
4792 * in SLI3. This code corrects the conflicts.
4794 lpfc_read_rev(phba, mboxq);
4795 mqe = &mboxq->u.mqe;
4796 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4797 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4798 mqe->un.read_rev.word1 &= 0x0000FFFF;
4799 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4800 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4802 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4803 if (rc) {
4804 dma_free_coherent(&phba->pcidev->dev, dma_size,
4805 dmabuf->virt, dmabuf->phys);
4806 kfree(dmabuf);
4807 return -EIO;
4811 * The available vpd length cannot be bigger than the
4812 * DMA buffer passed to the port. Catch the less than
4813 * case and update the caller's size.
4815 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4816 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4818 memcpy(vpd, dmabuf->virt, *vpd_size);
4820 dma_free_coherent(&phba->pcidev->dev, dma_size,
4821 dmabuf->virt, dmabuf->phys);
4822 kfree(dmabuf);
4823 return 0;
4827 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4828 * @phba: pointer to lpfc hba data structure.
4830 * This routine retrieves SLI4 device physical port name this PCI function
4831 * is attached to.
4833 * Return codes
4834 * 0 - successful
4835 * otherwise - failed to retrieve physical port name
4837 static int
4838 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4840 LPFC_MBOXQ_t *mboxq;
4841 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4842 struct lpfc_controller_attribute *cntl_attr;
4843 struct lpfc_mbx_get_port_name *get_port_name;
4844 void *virtaddr = NULL;
4845 uint32_t alloclen, reqlen;
4846 uint32_t shdr_status, shdr_add_status;
4847 union lpfc_sli4_cfg_shdr *shdr;
4848 char cport_name = 0;
4849 int rc;
4851 /* We assume nothing at this point */
4852 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4853 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4855 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4856 if (!mboxq)
4857 return -ENOMEM;
4858 /* obtain link type and link number via READ_CONFIG */
4859 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4860 lpfc_sli4_read_config(phba);
4861 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4862 goto retrieve_ppname;
4864 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4865 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4866 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4867 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4868 LPFC_SLI4_MBX_NEMBED);
4869 if (alloclen < reqlen) {
4870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4871 "3084 Allocated DMA memory size (%d) is "
4872 "less than the requested DMA memory size "
4873 "(%d)\n", alloclen, reqlen);
4874 rc = -ENOMEM;
4875 goto out_free_mboxq;
4877 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4878 virtaddr = mboxq->sge_array->addr[0];
4879 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4880 shdr = &mbx_cntl_attr->cfg_shdr;
4881 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4882 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4883 if (shdr_status || shdr_add_status || rc) {
4884 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4885 "3085 Mailbox x%x (x%x/x%x) failed, "
4886 "rc:x%x, status:x%x, add_status:x%x\n",
4887 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4888 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4889 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4890 rc, shdr_status, shdr_add_status);
4891 rc = -ENXIO;
4892 goto out_free_mboxq;
4894 cntl_attr = &mbx_cntl_attr->cntl_attr;
4895 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4896 phba->sli4_hba.lnk_info.lnk_tp =
4897 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4898 phba->sli4_hba.lnk_info.lnk_no =
4899 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4900 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4901 "3086 lnk_type:%d, lnk_numb:%d\n",
4902 phba->sli4_hba.lnk_info.lnk_tp,
4903 phba->sli4_hba.lnk_info.lnk_no);
4905 retrieve_ppname:
4906 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4907 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4908 sizeof(struct lpfc_mbx_get_port_name) -
4909 sizeof(struct lpfc_sli4_cfg_mhdr),
4910 LPFC_SLI4_MBX_EMBED);
4911 get_port_name = &mboxq->u.mqe.un.get_port_name;
4912 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4913 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4914 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4915 phba->sli4_hba.lnk_info.lnk_tp);
4916 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4917 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4918 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4919 if (shdr_status || shdr_add_status || rc) {
4920 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4921 "3087 Mailbox x%x (x%x/x%x) failed: "
4922 "rc:x%x, status:x%x, add_status:x%x\n",
4923 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4924 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4925 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4926 rc, shdr_status, shdr_add_status);
4927 rc = -ENXIO;
4928 goto out_free_mboxq;
4930 switch (phba->sli4_hba.lnk_info.lnk_no) {
4931 case LPFC_LINK_NUMBER_0:
4932 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4933 &get_port_name->u.response);
4934 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4935 break;
4936 case LPFC_LINK_NUMBER_1:
4937 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4938 &get_port_name->u.response);
4939 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4940 break;
4941 case LPFC_LINK_NUMBER_2:
4942 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4943 &get_port_name->u.response);
4944 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4945 break;
4946 case LPFC_LINK_NUMBER_3:
4947 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4948 &get_port_name->u.response);
4949 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4950 break;
4951 default:
4952 break;
4955 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4956 phba->Port[0] = cport_name;
4957 phba->Port[1] = '\0';
4958 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4959 "3091 SLI get port name: %s\n", phba->Port);
4962 out_free_mboxq:
4963 if (rc != MBX_TIMEOUT) {
4964 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4965 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4966 else
4967 mempool_free(mboxq, phba->mbox_mem_pool);
4969 return rc;
4973 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4974 * @phba: pointer to lpfc hba data structure.
4976 * This routine is called to explicitly arm the SLI4 device's completion and
4977 * event queues
4979 static void
4980 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4982 int fcp_eqidx;
4984 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4985 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4986 fcp_eqidx = 0;
4987 if (phba->sli4_hba.fcp_cq) {
4988 do {
4989 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4990 LPFC_QUEUE_REARM);
4991 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
4993 if (phba->sli4_hba.hba_eq) {
4994 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
4995 fcp_eqidx++)
4996 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
4997 LPFC_QUEUE_REARM);
5002 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5003 * @phba: Pointer to HBA context object.
5004 * @type: The resource extent type.
5005 * @extnt_count: buffer to hold port available extent count.
5006 * @extnt_size: buffer to hold element count per extent.
5008 * This function calls the port and retrievs the number of available
5009 * extents and their size for a particular extent type.
5011 * Returns: 0 if successful. Nonzero otherwise.
5014 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5015 uint16_t *extnt_count, uint16_t *extnt_size)
5017 int rc = 0;
5018 uint32_t length;
5019 uint32_t mbox_tmo;
5020 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5021 LPFC_MBOXQ_t *mbox;
5023 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5024 if (!mbox)
5025 return -ENOMEM;
5027 /* Find out how many extents are available for this resource type */
5028 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5029 sizeof(struct lpfc_sli4_cfg_mhdr));
5030 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5031 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5032 length, LPFC_SLI4_MBX_EMBED);
5034 /* Send an extents count of 0 - the GET doesn't use it. */
5035 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5036 LPFC_SLI4_MBX_EMBED);
5037 if (unlikely(rc)) {
5038 rc = -EIO;
5039 goto err_exit;
5042 if (!phba->sli4_hba.intr_enable)
5043 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5044 else {
5045 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5046 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5048 if (unlikely(rc)) {
5049 rc = -EIO;
5050 goto err_exit;
5053 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5054 if (bf_get(lpfc_mbox_hdr_status,
5055 &rsrc_info->header.cfg_shdr.response)) {
5056 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5057 "2930 Failed to get resource extents "
5058 "Status 0x%x Add'l Status 0x%x\n",
5059 bf_get(lpfc_mbox_hdr_status,
5060 &rsrc_info->header.cfg_shdr.response),
5061 bf_get(lpfc_mbox_hdr_add_status,
5062 &rsrc_info->header.cfg_shdr.response));
5063 rc = -EIO;
5064 goto err_exit;
5067 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5068 &rsrc_info->u.rsp);
5069 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5070 &rsrc_info->u.rsp);
5072 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5073 "3162 Retrieved extents type-%d from port: count:%d, "
5074 "size:%d\n", type, *extnt_count, *extnt_size);
5076 err_exit:
5077 mempool_free(mbox, phba->mbox_mem_pool);
5078 return rc;
5082 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5083 * @phba: Pointer to HBA context object.
5084 * @type: The extent type to check.
5086 * This function reads the current available extents from the port and checks
5087 * if the extent count or extent size has changed since the last access.
5088 * Callers use this routine post port reset to understand if there is a
5089 * extent reprovisioning requirement.
5091 * Returns:
5092 * -Error: error indicates problem.
5093 * 1: Extent count or size has changed.
5094 * 0: No changes.
5096 static int
5097 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5099 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5100 uint16_t size_diff, rsrc_ext_size;
5101 int rc = 0;
5102 struct lpfc_rsrc_blks *rsrc_entry;
5103 struct list_head *rsrc_blk_list = NULL;
5105 size_diff = 0;
5106 curr_ext_cnt = 0;
5107 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5108 &rsrc_ext_cnt,
5109 &rsrc_ext_size);
5110 if (unlikely(rc))
5111 return -EIO;
5113 switch (type) {
5114 case LPFC_RSC_TYPE_FCOE_RPI:
5115 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5116 break;
5117 case LPFC_RSC_TYPE_FCOE_VPI:
5118 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5119 break;
5120 case LPFC_RSC_TYPE_FCOE_XRI:
5121 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5122 break;
5123 case LPFC_RSC_TYPE_FCOE_VFI:
5124 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5125 break;
5126 default:
5127 break;
5130 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5131 curr_ext_cnt++;
5132 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5133 size_diff++;
5136 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5137 rc = 1;
5139 return rc;
5143 * lpfc_sli4_cfg_post_extnts -
5144 * @phba: Pointer to HBA context object.
5145 * @extnt_cnt - number of available extents.
5146 * @type - the extent type (rpi, xri, vfi, vpi).
5147 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5148 * @mbox - pointer to the caller's allocated mailbox structure.
5150 * This function executes the extents allocation request. It also
5151 * takes care of the amount of memory needed to allocate or get the
5152 * allocated extents. It is the caller's responsibility to evaluate
5153 * the response.
5155 * Returns:
5156 * -Error: Error value describes the condition found.
5157 * 0: if successful
5159 static int
5160 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5161 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5163 int rc = 0;
5164 uint32_t req_len;
5165 uint32_t emb_len;
5166 uint32_t alloc_len, mbox_tmo;
5168 /* Calculate the total requested length of the dma memory */
5169 req_len = extnt_cnt * sizeof(uint16_t);
5172 * Calculate the size of an embedded mailbox. The uint32_t
5173 * accounts for extents-specific word.
5175 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5176 sizeof(uint32_t);
5179 * Presume the allocation and response will fit into an embedded
5180 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5182 *emb = LPFC_SLI4_MBX_EMBED;
5183 if (req_len > emb_len) {
5184 req_len = extnt_cnt * sizeof(uint16_t) +
5185 sizeof(union lpfc_sli4_cfg_shdr) +
5186 sizeof(uint32_t);
5187 *emb = LPFC_SLI4_MBX_NEMBED;
5190 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5191 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5192 req_len, *emb);
5193 if (alloc_len < req_len) {
5194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5195 "2982 Allocated DMA memory size (x%x) is "
5196 "less than the requested DMA memory "
5197 "size (x%x)\n", alloc_len, req_len);
5198 return -ENOMEM;
5200 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5201 if (unlikely(rc))
5202 return -EIO;
5204 if (!phba->sli4_hba.intr_enable)
5205 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5206 else {
5207 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5208 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5211 if (unlikely(rc))
5212 rc = -EIO;
5213 return rc;
5217 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5218 * @phba: Pointer to HBA context object.
5219 * @type: The resource extent type to allocate.
5221 * This function allocates the number of elements for the specified
5222 * resource type.
5224 static int
5225 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5227 bool emb = false;
5228 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5229 uint16_t rsrc_id, rsrc_start, j, k;
5230 uint16_t *ids;
5231 int i, rc;
5232 unsigned long longs;
5233 unsigned long *bmask;
5234 struct lpfc_rsrc_blks *rsrc_blks;
5235 LPFC_MBOXQ_t *mbox;
5236 uint32_t length;
5237 struct lpfc_id_range *id_array = NULL;
5238 void *virtaddr = NULL;
5239 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5240 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5241 struct list_head *ext_blk_list;
5243 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5244 &rsrc_cnt,
5245 &rsrc_size);
5246 if (unlikely(rc))
5247 return -EIO;
5249 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5250 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5251 "3009 No available Resource Extents "
5252 "for resource type 0x%x: Count: 0x%x, "
5253 "Size 0x%x\n", type, rsrc_cnt,
5254 rsrc_size);
5255 return -ENOMEM;
5258 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5259 "2903 Post resource extents type-0x%x: "
5260 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5262 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5263 if (!mbox)
5264 return -ENOMEM;
5266 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5267 if (unlikely(rc)) {
5268 rc = -EIO;
5269 goto err_exit;
5273 * Figure out where the response is located. Then get local pointers
5274 * to the response data. The port does not guarantee to respond to
5275 * all extents counts request so update the local variable with the
5276 * allocated count from the port.
5278 if (emb == LPFC_SLI4_MBX_EMBED) {
5279 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5280 id_array = &rsrc_ext->u.rsp.id[0];
5281 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5282 } else {
5283 virtaddr = mbox->sge_array->addr[0];
5284 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5285 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5286 id_array = &n_rsrc->id;
5289 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5290 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5293 * Based on the resource size and count, correct the base and max
5294 * resource values.
5296 length = sizeof(struct lpfc_rsrc_blks);
5297 switch (type) {
5298 case LPFC_RSC_TYPE_FCOE_RPI:
5299 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5300 sizeof(unsigned long),
5301 GFP_KERNEL);
5302 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5303 rc = -ENOMEM;
5304 goto err_exit;
5306 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5307 sizeof(uint16_t),
5308 GFP_KERNEL);
5309 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5310 kfree(phba->sli4_hba.rpi_bmask);
5311 rc = -ENOMEM;
5312 goto err_exit;
5316 * The next_rpi was initialized with the maximum available
5317 * count but the port may allocate a smaller number. Catch
5318 * that case and update the next_rpi.
5320 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5322 /* Initialize local ptrs for common extent processing later. */
5323 bmask = phba->sli4_hba.rpi_bmask;
5324 ids = phba->sli4_hba.rpi_ids;
5325 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5326 break;
5327 case LPFC_RSC_TYPE_FCOE_VPI:
5328 phba->vpi_bmask = kzalloc(longs *
5329 sizeof(unsigned long),
5330 GFP_KERNEL);
5331 if (unlikely(!phba->vpi_bmask)) {
5332 rc = -ENOMEM;
5333 goto err_exit;
5335 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5336 sizeof(uint16_t),
5337 GFP_KERNEL);
5338 if (unlikely(!phba->vpi_ids)) {
5339 kfree(phba->vpi_bmask);
5340 rc = -ENOMEM;
5341 goto err_exit;
5344 /* Initialize local ptrs for common extent processing later. */
5345 bmask = phba->vpi_bmask;
5346 ids = phba->vpi_ids;
5347 ext_blk_list = &phba->lpfc_vpi_blk_list;
5348 break;
5349 case LPFC_RSC_TYPE_FCOE_XRI:
5350 phba->sli4_hba.xri_bmask = kzalloc(longs *
5351 sizeof(unsigned long),
5352 GFP_KERNEL);
5353 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5354 rc = -ENOMEM;
5355 goto err_exit;
5357 phba->sli4_hba.max_cfg_param.xri_used = 0;
5358 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5359 sizeof(uint16_t),
5360 GFP_KERNEL);
5361 if (unlikely(!phba->sli4_hba.xri_ids)) {
5362 kfree(phba->sli4_hba.xri_bmask);
5363 rc = -ENOMEM;
5364 goto err_exit;
5367 /* Initialize local ptrs for common extent processing later. */
5368 bmask = phba->sli4_hba.xri_bmask;
5369 ids = phba->sli4_hba.xri_ids;
5370 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5371 break;
5372 case LPFC_RSC_TYPE_FCOE_VFI:
5373 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5374 sizeof(unsigned long),
5375 GFP_KERNEL);
5376 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5377 rc = -ENOMEM;
5378 goto err_exit;
5380 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5381 sizeof(uint16_t),
5382 GFP_KERNEL);
5383 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5384 kfree(phba->sli4_hba.vfi_bmask);
5385 rc = -ENOMEM;
5386 goto err_exit;
5389 /* Initialize local ptrs for common extent processing later. */
5390 bmask = phba->sli4_hba.vfi_bmask;
5391 ids = phba->sli4_hba.vfi_ids;
5392 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5393 break;
5394 default:
5395 /* Unsupported Opcode. Fail call. */
5396 id_array = NULL;
5397 bmask = NULL;
5398 ids = NULL;
5399 ext_blk_list = NULL;
5400 goto err_exit;
5404 * Complete initializing the extent configuration with the
5405 * allocated ids assigned to this function. The bitmask serves
5406 * as an index into the array and manages the available ids. The
5407 * array just stores the ids communicated to the port via the wqes.
5409 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5410 if ((i % 2) == 0)
5411 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5412 &id_array[k]);
5413 else
5414 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5415 &id_array[k]);
5417 rsrc_blks = kzalloc(length, GFP_KERNEL);
5418 if (unlikely(!rsrc_blks)) {
5419 rc = -ENOMEM;
5420 kfree(bmask);
5421 kfree(ids);
5422 goto err_exit;
5424 rsrc_blks->rsrc_start = rsrc_id;
5425 rsrc_blks->rsrc_size = rsrc_size;
5426 list_add_tail(&rsrc_blks->list, ext_blk_list);
5427 rsrc_start = rsrc_id;
5428 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5429 phba->sli4_hba.scsi_xri_start = rsrc_start +
5430 lpfc_sli4_get_els_iocb_cnt(phba);
5432 while (rsrc_id < (rsrc_start + rsrc_size)) {
5433 ids[j] = rsrc_id;
5434 rsrc_id++;
5435 j++;
5437 /* Entire word processed. Get next word.*/
5438 if ((i % 2) == 1)
5439 k++;
5441 err_exit:
5442 lpfc_sli4_mbox_cmd_free(phba, mbox);
5443 return rc;
5447 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5448 * @phba: Pointer to HBA context object.
5449 * @type: the extent's type.
5451 * This function deallocates all extents of a particular resource type.
5452 * SLI4 does not allow for deallocating a particular extent range. It
5453 * is the caller's responsibility to release all kernel memory resources.
5455 static int
5456 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5458 int rc;
5459 uint32_t length, mbox_tmo = 0;
5460 LPFC_MBOXQ_t *mbox;
5461 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5462 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5464 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5465 if (!mbox)
5466 return -ENOMEM;
5469 * This function sends an embedded mailbox because it only sends the
5470 * the resource type. All extents of this type are released by the
5471 * port.
5473 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5474 sizeof(struct lpfc_sli4_cfg_mhdr));
5475 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5476 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5477 length, LPFC_SLI4_MBX_EMBED);
5479 /* Send an extents count of 0 - the dealloc doesn't use it. */
5480 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5481 LPFC_SLI4_MBX_EMBED);
5482 if (unlikely(rc)) {
5483 rc = -EIO;
5484 goto out_free_mbox;
5486 if (!phba->sli4_hba.intr_enable)
5487 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5488 else {
5489 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5490 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5492 if (unlikely(rc)) {
5493 rc = -EIO;
5494 goto out_free_mbox;
5497 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5498 if (bf_get(lpfc_mbox_hdr_status,
5499 &dealloc_rsrc->header.cfg_shdr.response)) {
5500 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5501 "2919 Failed to release resource extents "
5502 "for type %d - Status 0x%x Add'l Status 0x%x. "
5503 "Resource memory not released.\n",
5504 type,
5505 bf_get(lpfc_mbox_hdr_status,
5506 &dealloc_rsrc->header.cfg_shdr.response),
5507 bf_get(lpfc_mbox_hdr_add_status,
5508 &dealloc_rsrc->header.cfg_shdr.response));
5509 rc = -EIO;
5510 goto out_free_mbox;
5513 /* Release kernel memory resources for the specific type. */
5514 switch (type) {
5515 case LPFC_RSC_TYPE_FCOE_VPI:
5516 kfree(phba->vpi_bmask);
5517 kfree(phba->vpi_ids);
5518 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5519 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5520 &phba->lpfc_vpi_blk_list, list) {
5521 list_del_init(&rsrc_blk->list);
5522 kfree(rsrc_blk);
5524 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5525 break;
5526 case LPFC_RSC_TYPE_FCOE_XRI:
5527 kfree(phba->sli4_hba.xri_bmask);
5528 kfree(phba->sli4_hba.xri_ids);
5529 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5530 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5531 list_del_init(&rsrc_blk->list);
5532 kfree(rsrc_blk);
5534 break;
5535 case LPFC_RSC_TYPE_FCOE_VFI:
5536 kfree(phba->sli4_hba.vfi_bmask);
5537 kfree(phba->sli4_hba.vfi_ids);
5538 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5539 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5540 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5541 list_del_init(&rsrc_blk->list);
5542 kfree(rsrc_blk);
5544 break;
5545 case LPFC_RSC_TYPE_FCOE_RPI:
5546 /* RPI bitmask and physical id array are cleaned up earlier. */
5547 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5548 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5549 list_del_init(&rsrc_blk->list);
5550 kfree(rsrc_blk);
5552 break;
5553 default:
5554 break;
5557 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5559 out_free_mbox:
5560 mempool_free(mbox, phba->mbox_mem_pool);
5561 return rc;
5565 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5566 * @phba: Pointer to HBA context object.
5568 * This function allocates all SLI4 resource identifiers.
5571 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5573 int i, rc, error = 0;
5574 uint16_t count, base;
5575 unsigned long longs;
5577 if (!phba->sli4_hba.rpi_hdrs_in_use)
5578 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5579 if (phba->sli4_hba.extents_in_use) {
5581 * The port supports resource extents. The XRI, VPI, VFI, RPI
5582 * resource extent count must be read and allocated before
5583 * provisioning the resource id arrays.
5585 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5586 LPFC_IDX_RSRC_RDY) {
5588 * Extent-based resources are set - the driver could
5589 * be in a port reset. Figure out if any corrective
5590 * actions need to be taken.
5592 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5593 LPFC_RSC_TYPE_FCOE_VFI);
5594 if (rc != 0)
5595 error++;
5596 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5597 LPFC_RSC_TYPE_FCOE_VPI);
5598 if (rc != 0)
5599 error++;
5600 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5601 LPFC_RSC_TYPE_FCOE_XRI);
5602 if (rc != 0)
5603 error++;
5604 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5605 LPFC_RSC_TYPE_FCOE_RPI);
5606 if (rc != 0)
5607 error++;
5610 * It's possible that the number of resources
5611 * provided to this port instance changed between
5612 * resets. Detect this condition and reallocate
5613 * resources. Otherwise, there is no action.
5615 if (error) {
5616 lpfc_printf_log(phba, KERN_INFO,
5617 LOG_MBOX | LOG_INIT,
5618 "2931 Detected extent resource "
5619 "change. Reallocating all "
5620 "extents.\n");
5621 rc = lpfc_sli4_dealloc_extent(phba,
5622 LPFC_RSC_TYPE_FCOE_VFI);
5623 rc = lpfc_sli4_dealloc_extent(phba,
5624 LPFC_RSC_TYPE_FCOE_VPI);
5625 rc = lpfc_sli4_dealloc_extent(phba,
5626 LPFC_RSC_TYPE_FCOE_XRI);
5627 rc = lpfc_sli4_dealloc_extent(phba,
5628 LPFC_RSC_TYPE_FCOE_RPI);
5629 } else
5630 return 0;
5633 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5634 if (unlikely(rc))
5635 goto err_exit;
5637 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5638 if (unlikely(rc))
5639 goto err_exit;
5641 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5642 if (unlikely(rc))
5643 goto err_exit;
5645 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5646 if (unlikely(rc))
5647 goto err_exit;
5648 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5649 LPFC_IDX_RSRC_RDY);
5650 return rc;
5651 } else {
5653 * The port does not support resource extents. The XRI, VPI,
5654 * VFI, RPI resource ids were determined from READ_CONFIG.
5655 * Just allocate the bitmasks and provision the resource id
5656 * arrays. If a port reset is active, the resources don't
5657 * need any action - just exit.
5659 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5660 LPFC_IDX_RSRC_RDY) {
5661 lpfc_sli4_dealloc_resource_identifiers(phba);
5662 lpfc_sli4_remove_rpis(phba);
5664 /* RPIs. */
5665 count = phba->sli4_hba.max_cfg_param.max_rpi;
5666 if (count <= 0) {
5667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5668 "3279 Invalid provisioning of "
5669 "rpi:%d\n", count);
5670 rc = -EINVAL;
5671 goto err_exit;
5673 base = phba->sli4_hba.max_cfg_param.rpi_base;
5674 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5675 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5676 sizeof(unsigned long),
5677 GFP_KERNEL);
5678 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5679 rc = -ENOMEM;
5680 goto err_exit;
5682 phba->sli4_hba.rpi_ids = kzalloc(count *
5683 sizeof(uint16_t),
5684 GFP_KERNEL);
5685 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5686 rc = -ENOMEM;
5687 goto free_rpi_bmask;
5690 for (i = 0; i < count; i++)
5691 phba->sli4_hba.rpi_ids[i] = base + i;
5693 /* VPIs. */
5694 count = phba->sli4_hba.max_cfg_param.max_vpi;
5695 if (count <= 0) {
5696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5697 "3280 Invalid provisioning of "
5698 "vpi:%d\n", count);
5699 rc = -EINVAL;
5700 goto free_rpi_ids;
5702 base = phba->sli4_hba.max_cfg_param.vpi_base;
5703 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5704 phba->vpi_bmask = kzalloc(longs *
5705 sizeof(unsigned long),
5706 GFP_KERNEL);
5707 if (unlikely(!phba->vpi_bmask)) {
5708 rc = -ENOMEM;
5709 goto free_rpi_ids;
5711 phba->vpi_ids = kzalloc(count *
5712 sizeof(uint16_t),
5713 GFP_KERNEL);
5714 if (unlikely(!phba->vpi_ids)) {
5715 rc = -ENOMEM;
5716 goto free_vpi_bmask;
5719 for (i = 0; i < count; i++)
5720 phba->vpi_ids[i] = base + i;
5722 /* XRIs. */
5723 count = phba->sli4_hba.max_cfg_param.max_xri;
5724 if (count <= 0) {
5725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5726 "3281 Invalid provisioning of "
5727 "xri:%d\n", count);
5728 rc = -EINVAL;
5729 goto free_vpi_ids;
5731 base = phba->sli4_hba.max_cfg_param.xri_base;
5732 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5733 phba->sli4_hba.xri_bmask = kzalloc(longs *
5734 sizeof(unsigned long),
5735 GFP_KERNEL);
5736 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5737 rc = -ENOMEM;
5738 goto free_vpi_ids;
5740 phba->sli4_hba.max_cfg_param.xri_used = 0;
5741 phba->sli4_hba.xri_ids = kzalloc(count *
5742 sizeof(uint16_t),
5743 GFP_KERNEL);
5744 if (unlikely(!phba->sli4_hba.xri_ids)) {
5745 rc = -ENOMEM;
5746 goto free_xri_bmask;
5749 for (i = 0; i < count; i++)
5750 phba->sli4_hba.xri_ids[i] = base + i;
5752 /* VFIs. */
5753 count = phba->sli4_hba.max_cfg_param.max_vfi;
5754 if (count <= 0) {
5755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5756 "3282 Invalid provisioning of "
5757 "vfi:%d\n", count);
5758 rc = -EINVAL;
5759 goto free_xri_ids;
5761 base = phba->sli4_hba.max_cfg_param.vfi_base;
5762 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5763 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5764 sizeof(unsigned long),
5765 GFP_KERNEL);
5766 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5767 rc = -ENOMEM;
5768 goto free_xri_ids;
5770 phba->sli4_hba.vfi_ids = kzalloc(count *
5771 sizeof(uint16_t),
5772 GFP_KERNEL);
5773 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5774 rc = -ENOMEM;
5775 goto free_vfi_bmask;
5778 for (i = 0; i < count; i++)
5779 phba->sli4_hba.vfi_ids[i] = base + i;
5782 * Mark all resources ready. An HBA reset doesn't need
5783 * to reset the initialization.
5785 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5786 LPFC_IDX_RSRC_RDY);
5787 return 0;
5790 free_vfi_bmask:
5791 kfree(phba->sli4_hba.vfi_bmask);
5792 free_xri_ids:
5793 kfree(phba->sli4_hba.xri_ids);
5794 free_xri_bmask:
5795 kfree(phba->sli4_hba.xri_bmask);
5796 free_vpi_ids:
5797 kfree(phba->vpi_ids);
5798 free_vpi_bmask:
5799 kfree(phba->vpi_bmask);
5800 free_rpi_ids:
5801 kfree(phba->sli4_hba.rpi_ids);
5802 free_rpi_bmask:
5803 kfree(phba->sli4_hba.rpi_bmask);
5804 err_exit:
5805 return rc;
5809 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5810 * @phba: Pointer to HBA context object.
5812 * This function allocates the number of elements for the specified
5813 * resource type.
5816 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5818 if (phba->sli4_hba.extents_in_use) {
5819 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5820 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5821 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5822 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5823 } else {
5824 kfree(phba->vpi_bmask);
5825 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5826 kfree(phba->vpi_ids);
5827 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5828 kfree(phba->sli4_hba.xri_bmask);
5829 kfree(phba->sli4_hba.xri_ids);
5830 kfree(phba->sli4_hba.vfi_bmask);
5831 kfree(phba->sli4_hba.vfi_ids);
5832 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5833 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5836 return 0;
5840 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5841 * @phba: Pointer to HBA context object.
5842 * @type: The resource extent type.
5843 * @extnt_count: buffer to hold port extent count response
5844 * @extnt_size: buffer to hold port extent size response.
5846 * This function calls the port to read the host allocated extents
5847 * for a particular type.
5850 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5851 uint16_t *extnt_cnt, uint16_t *extnt_size)
5853 bool emb;
5854 int rc = 0;
5855 uint16_t curr_blks = 0;
5856 uint32_t req_len, emb_len;
5857 uint32_t alloc_len, mbox_tmo;
5858 struct list_head *blk_list_head;
5859 struct lpfc_rsrc_blks *rsrc_blk;
5860 LPFC_MBOXQ_t *mbox;
5861 void *virtaddr = NULL;
5862 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5863 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5864 union lpfc_sli4_cfg_shdr *shdr;
5866 switch (type) {
5867 case LPFC_RSC_TYPE_FCOE_VPI:
5868 blk_list_head = &phba->lpfc_vpi_blk_list;
5869 break;
5870 case LPFC_RSC_TYPE_FCOE_XRI:
5871 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5872 break;
5873 case LPFC_RSC_TYPE_FCOE_VFI:
5874 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5875 break;
5876 case LPFC_RSC_TYPE_FCOE_RPI:
5877 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5878 break;
5879 default:
5880 return -EIO;
5883 /* Count the number of extents currently allocatd for this type. */
5884 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5885 if (curr_blks == 0) {
5887 * The GET_ALLOCATED mailbox does not return the size,
5888 * just the count. The size should be just the size
5889 * stored in the current allocated block and all sizes
5890 * for an extent type are the same so set the return
5891 * value now.
5893 *extnt_size = rsrc_blk->rsrc_size;
5895 curr_blks++;
5898 /* Calculate the total requested length of the dma memory. */
5899 req_len = curr_blks * sizeof(uint16_t);
5902 * Calculate the size of an embedded mailbox. The uint32_t
5903 * accounts for extents-specific word.
5905 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5906 sizeof(uint32_t);
5909 * Presume the allocation and response will fit into an embedded
5910 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5912 emb = LPFC_SLI4_MBX_EMBED;
5913 req_len = emb_len;
5914 if (req_len > emb_len) {
5915 req_len = curr_blks * sizeof(uint16_t) +
5916 sizeof(union lpfc_sli4_cfg_shdr) +
5917 sizeof(uint32_t);
5918 emb = LPFC_SLI4_MBX_NEMBED;
5921 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5922 if (!mbox)
5923 return -ENOMEM;
5924 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5926 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5927 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5928 req_len, emb);
5929 if (alloc_len < req_len) {
5930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5931 "2983 Allocated DMA memory size (x%x) is "
5932 "less than the requested DMA memory "
5933 "size (x%x)\n", alloc_len, req_len);
5934 rc = -ENOMEM;
5935 goto err_exit;
5937 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5938 if (unlikely(rc)) {
5939 rc = -EIO;
5940 goto err_exit;
5943 if (!phba->sli4_hba.intr_enable)
5944 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5945 else {
5946 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5947 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5950 if (unlikely(rc)) {
5951 rc = -EIO;
5952 goto err_exit;
5956 * Figure out where the response is located. Then get local pointers
5957 * to the response data. The port does not guarantee to respond to
5958 * all extents counts request so update the local variable with the
5959 * allocated count from the port.
5961 if (emb == LPFC_SLI4_MBX_EMBED) {
5962 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5963 shdr = &rsrc_ext->header.cfg_shdr;
5964 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5965 } else {
5966 virtaddr = mbox->sge_array->addr[0];
5967 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5968 shdr = &n_rsrc->cfg_shdr;
5969 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5972 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5973 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5974 "2984 Failed to read allocated resources "
5975 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5976 type,
5977 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5978 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5979 rc = -EIO;
5980 goto err_exit;
5982 err_exit:
5983 lpfc_sli4_mbox_cmd_free(phba, mbox);
5984 return rc;
5988 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5989 * @phba: pointer to lpfc hba data structure.
5991 * This routine walks the list of els buffers that have been allocated and
5992 * repost them to the port by using SGL block post. This is needed after a
5993 * pci_function_reset/warm_start or start. It attempts to construct blocks
5994 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5995 * SGL block post mailbox commands to post them to the port. For single els
5996 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5997 * mailbox command for posting.
5999 * Returns: 0 = success, non-zero failure.
6001 static int
6002 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6004 struct lpfc_sglq *sglq_entry = NULL;
6005 struct lpfc_sglq *sglq_entry_next = NULL;
6006 struct lpfc_sglq *sglq_entry_first = NULL;
6007 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
6008 int last_xritag = NO_XRI;
6009 LIST_HEAD(prep_sgl_list);
6010 LIST_HEAD(blck_sgl_list);
6011 LIST_HEAD(allc_sgl_list);
6012 LIST_HEAD(post_sgl_list);
6013 LIST_HEAD(free_sgl_list);
6015 spin_lock_irq(&phba->hbalock);
6016 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6017 spin_unlock_irq(&phba->hbalock);
6019 total_cnt = phba->sli4_hba.els_xri_cnt;
6020 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6021 &allc_sgl_list, list) {
6022 list_del_init(&sglq_entry->list);
6023 block_cnt++;
6024 if ((last_xritag != NO_XRI) &&
6025 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6026 /* a hole in xri block, form a sgl posting block */
6027 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6028 post_cnt = block_cnt - 1;
6029 /* prepare list for next posting block */
6030 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6031 block_cnt = 1;
6032 } else {
6033 /* prepare list for next posting block */
6034 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6035 /* enough sgls for non-embed sgl mbox command */
6036 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6037 list_splice_init(&prep_sgl_list,
6038 &blck_sgl_list);
6039 post_cnt = block_cnt;
6040 block_cnt = 0;
6043 num_posted++;
6045 /* keep track of last sgl's xritag */
6046 last_xritag = sglq_entry->sli4_xritag;
6048 /* end of repost sgl list condition for els buffers */
6049 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6050 if (post_cnt == 0) {
6051 list_splice_init(&prep_sgl_list,
6052 &blck_sgl_list);
6053 post_cnt = block_cnt;
6054 } else if (block_cnt == 1) {
6055 status = lpfc_sli4_post_sgl(phba,
6056 sglq_entry->phys, 0,
6057 sglq_entry->sli4_xritag);
6058 if (!status) {
6059 /* successful, put sgl to posted list */
6060 list_add_tail(&sglq_entry->list,
6061 &post_sgl_list);
6062 } else {
6063 /* Failure, put sgl to free list */
6064 lpfc_printf_log(phba, KERN_WARNING,
6065 LOG_SLI,
6066 "3159 Failed to post els "
6067 "sgl, xritag:x%x\n",
6068 sglq_entry->sli4_xritag);
6069 list_add_tail(&sglq_entry->list,
6070 &free_sgl_list);
6071 total_cnt--;
6076 /* continue until a nembed page worth of sgls */
6077 if (post_cnt == 0)
6078 continue;
6080 /* post the els buffer list sgls as a block */
6081 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6082 post_cnt);
6084 if (!status) {
6085 /* success, put sgl list to posted sgl list */
6086 list_splice_init(&blck_sgl_list, &post_sgl_list);
6087 } else {
6088 /* Failure, put sgl list to free sgl list */
6089 sglq_entry_first = list_first_entry(&blck_sgl_list,
6090 struct lpfc_sglq,
6091 list);
6092 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6093 "3160 Failed to post els sgl-list, "
6094 "xritag:x%x-x%x\n",
6095 sglq_entry_first->sli4_xritag,
6096 (sglq_entry_first->sli4_xritag +
6097 post_cnt - 1));
6098 list_splice_init(&blck_sgl_list, &free_sgl_list);
6099 total_cnt -= post_cnt;
6102 /* don't reset xirtag due to hole in xri block */
6103 if (block_cnt == 0)
6104 last_xritag = NO_XRI;
6106 /* reset els sgl post count for next round of posting */
6107 post_cnt = 0;
6109 /* update the number of XRIs posted for ELS */
6110 phba->sli4_hba.els_xri_cnt = total_cnt;
6112 /* free the els sgls failed to post */
6113 lpfc_free_sgl_list(phba, &free_sgl_list);
6115 /* push els sgls posted to the availble list */
6116 if (!list_empty(&post_sgl_list)) {
6117 spin_lock_irq(&phba->hbalock);
6118 list_splice_init(&post_sgl_list,
6119 &phba->sli4_hba.lpfc_sgl_list);
6120 spin_unlock_irq(&phba->hbalock);
6121 } else {
6122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6123 "3161 Failure to post els sgl to port.\n");
6124 return -EIO;
6126 return 0;
6130 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6131 * @phba: Pointer to HBA context object.
6133 * This function is the main SLI4 device intialization PCI function. This
6134 * function is called by the HBA intialization code, HBA reset code and
6135 * HBA error attention handler code. Caller is not required to hold any
6136 * locks.
6139 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6141 int rc;
6142 LPFC_MBOXQ_t *mboxq;
6143 struct lpfc_mqe *mqe;
6144 uint8_t *vpd;
6145 uint32_t vpd_size;
6146 uint32_t ftr_rsp = 0;
6147 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6148 struct lpfc_vport *vport = phba->pport;
6149 struct lpfc_dmabuf *mp;
6151 /* Perform a PCI function reset to start from clean */
6152 rc = lpfc_pci_function_reset(phba);
6153 if (unlikely(rc))
6154 return -ENODEV;
6156 /* Check the HBA Host Status Register for readyness */
6157 rc = lpfc_sli4_post_status_check(phba);
6158 if (unlikely(rc))
6159 return -ENODEV;
6160 else {
6161 spin_lock_irq(&phba->hbalock);
6162 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6163 spin_unlock_irq(&phba->hbalock);
6167 * Allocate a single mailbox container for initializing the
6168 * port.
6170 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6171 if (!mboxq)
6172 return -ENOMEM;
6174 /* Issue READ_REV to collect vpd and FW information. */
6175 vpd_size = SLI4_PAGE_SIZE;
6176 vpd = kzalloc(vpd_size, GFP_KERNEL);
6177 if (!vpd) {
6178 rc = -ENOMEM;
6179 goto out_free_mbox;
6182 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6183 if (unlikely(rc)) {
6184 kfree(vpd);
6185 goto out_free_mbox;
6188 mqe = &mboxq->u.mqe;
6189 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6190 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
6191 phba->hba_flag |= HBA_FCOE_MODE;
6192 else
6193 phba->hba_flag &= ~HBA_FCOE_MODE;
6195 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6196 LPFC_DCBX_CEE_MODE)
6197 phba->hba_flag |= HBA_FIP_SUPPORT;
6198 else
6199 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6201 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6203 if (phba->sli_rev != LPFC_SLI_REV4) {
6204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6205 "0376 READ_REV Error. SLI Level %d "
6206 "FCoE enabled %d\n",
6207 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6208 rc = -EIO;
6209 kfree(vpd);
6210 goto out_free_mbox;
6214 * Continue initialization with default values even if driver failed
6215 * to read FCoE param config regions, only read parameters if the
6216 * board is FCoE
6218 if (phba->hba_flag & HBA_FCOE_MODE &&
6219 lpfc_sli4_read_fcoe_params(phba))
6220 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6221 "2570 Failed to read FCoE parameters\n");
6224 * Retrieve sli4 device physical port name, failure of doing it
6225 * is considered as non-fatal.
6227 rc = lpfc_sli4_retrieve_pport_name(phba);
6228 if (!rc)
6229 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6230 "3080 Successful retrieving SLI4 device "
6231 "physical port name: %s.\n", phba->Port);
6234 * Evaluate the read rev and vpd data. Populate the driver
6235 * state with the results. If this routine fails, the failure
6236 * is not fatal as the driver will use generic values.
6238 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6239 if (unlikely(!rc)) {
6240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6241 "0377 Error %d parsing vpd. "
6242 "Using defaults.\n", rc);
6243 rc = 0;
6245 kfree(vpd);
6247 /* Save information as VPD data */
6248 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6249 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6250 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6251 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6252 &mqe->un.read_rev);
6253 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6254 &mqe->un.read_rev);
6255 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6256 &mqe->un.read_rev);
6257 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6258 &mqe->un.read_rev);
6259 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6260 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6261 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6262 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6263 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6264 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6265 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6266 "(%d):0380 READ_REV Status x%x "
6267 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6268 mboxq->vport ? mboxq->vport->vpi : 0,
6269 bf_get(lpfc_mqe_status, mqe),
6270 phba->vpd.rev.opFwName,
6271 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6272 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6274 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6275 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6276 if (phba->pport->cfg_lun_queue_depth > rc) {
6277 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6278 "3362 LUN queue depth changed from %d to %d\n",
6279 phba->pport->cfg_lun_queue_depth, rc);
6280 phba->pport->cfg_lun_queue_depth = rc;
6285 * Discover the port's supported feature set and match it against the
6286 * hosts requests.
6288 lpfc_request_features(phba, mboxq);
6289 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6290 if (unlikely(rc)) {
6291 rc = -EIO;
6292 goto out_free_mbox;
6296 * The port must support FCP initiator mode as this is the
6297 * only mode running in the host.
6299 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6300 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6301 "0378 No support for fcpi mode.\n");
6302 ftr_rsp++;
6304 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6305 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6306 else
6307 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6309 * If the port cannot support the host's requested features
6310 * then turn off the global config parameters to disable the
6311 * feature in the driver. This is not a fatal error.
6313 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6314 if (phba->cfg_enable_bg) {
6315 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6316 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6317 else
6318 ftr_rsp++;
6321 if (phba->max_vpi && phba->cfg_enable_npiv &&
6322 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6323 ftr_rsp++;
6325 if (ftr_rsp) {
6326 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6327 "0379 Feature Mismatch Data: x%08x %08x "
6328 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6329 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6330 phba->cfg_enable_npiv, phba->max_vpi);
6331 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6332 phba->cfg_enable_bg = 0;
6333 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6334 phba->cfg_enable_npiv = 0;
6337 /* These SLI3 features are assumed in SLI4 */
6338 spin_lock_irq(&phba->hbalock);
6339 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6340 spin_unlock_irq(&phba->hbalock);
6343 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6344 * calls depends on these resources to complete port setup.
6346 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6347 if (rc) {
6348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6349 "2920 Failed to alloc Resource IDs "
6350 "rc = x%x\n", rc);
6351 goto out_free_mbox;
6354 /* Read the port's service parameters. */
6355 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6356 if (rc) {
6357 phba->link_state = LPFC_HBA_ERROR;
6358 rc = -ENOMEM;
6359 goto out_free_mbox;
6362 mboxq->vport = vport;
6363 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6364 mp = (struct lpfc_dmabuf *) mboxq->context1;
6365 if (rc == MBX_SUCCESS) {
6366 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6367 rc = 0;
6371 * This memory was allocated by the lpfc_read_sparam routine. Release
6372 * it to the mbuf pool.
6374 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6375 kfree(mp);
6376 mboxq->context1 = NULL;
6377 if (unlikely(rc)) {
6378 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6379 "0382 READ_SPARAM command failed "
6380 "status %d, mbxStatus x%x\n",
6381 rc, bf_get(lpfc_mqe_status, mqe));
6382 phba->link_state = LPFC_HBA_ERROR;
6383 rc = -EIO;
6384 goto out_free_mbox;
6387 lpfc_update_vport_wwn(vport);
6389 /* Update the fc_host data structures with new wwn. */
6390 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6391 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6393 /* update host els and scsi xri-sgl sizes and mappings */
6394 rc = lpfc_sli4_xri_sgl_update(phba);
6395 if (unlikely(rc)) {
6396 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6397 "1400 Failed to update xri-sgl size and "
6398 "mapping: %d\n", rc);
6399 goto out_free_mbox;
6402 /* register the els sgl pool to the port */
6403 rc = lpfc_sli4_repost_els_sgl_list(phba);
6404 if (unlikely(rc)) {
6405 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6406 "0582 Error %d during els sgl post "
6407 "operation\n", rc);
6408 rc = -ENODEV;
6409 goto out_free_mbox;
6412 /* register the allocated scsi sgl pool to the port */
6413 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6414 if (unlikely(rc)) {
6415 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6416 "0383 Error %d during scsi sgl post "
6417 "operation\n", rc);
6418 /* Some Scsi buffers were moved to the abort scsi list */
6419 /* A pci function reset will repost them */
6420 rc = -ENODEV;
6421 goto out_free_mbox;
6424 /* Post the rpi header region to the device. */
6425 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6426 if (unlikely(rc)) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6428 "0393 Error %d during rpi post operation\n",
6429 rc);
6430 rc = -ENODEV;
6431 goto out_free_mbox;
6433 lpfc_sli4_node_prep(phba);
6435 /* Create all the SLI4 queues */
6436 rc = lpfc_sli4_queue_create(phba);
6437 if (rc) {
6438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6439 "3089 Failed to allocate queues\n");
6440 rc = -ENODEV;
6441 goto out_stop_timers;
6443 /* Set up all the queues to the device */
6444 rc = lpfc_sli4_queue_setup(phba);
6445 if (unlikely(rc)) {
6446 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6447 "0381 Error %d during queue setup.\n ", rc);
6448 goto out_destroy_queue;
6451 /* Arm the CQs and then EQs on device */
6452 lpfc_sli4_arm_cqeq_intr(phba);
6454 /* Indicate device interrupt mode */
6455 phba->sli4_hba.intr_enable = 1;
6457 /* Allow asynchronous mailbox command to go through */
6458 spin_lock_irq(&phba->hbalock);
6459 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6460 spin_unlock_irq(&phba->hbalock);
6462 /* Post receive buffers to the device */
6463 lpfc_sli4_rb_setup(phba);
6465 /* Reset HBA FCF states after HBA reset */
6466 phba->fcf.fcf_flag = 0;
6467 phba->fcf.current_rec.flag = 0;
6469 /* Start the ELS watchdog timer */
6470 mod_timer(&vport->els_tmofunc,
6471 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
6473 /* Start heart beat timer */
6474 mod_timer(&phba->hb_tmofunc,
6475 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
6476 phba->hb_outstanding = 0;
6477 phba->last_completion_time = jiffies;
6479 /* Start error attention (ERATT) polling timer */
6480 mod_timer(&phba->eratt_poll,
6481 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
6483 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6484 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6485 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6486 if (!rc) {
6487 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6488 "2829 This device supports "
6489 "Advanced Error Reporting (AER)\n");
6490 spin_lock_irq(&phba->hbalock);
6491 phba->hba_flag |= HBA_AER_ENABLED;
6492 spin_unlock_irq(&phba->hbalock);
6493 } else {
6494 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6495 "2830 This device does not support "
6496 "Advanced Error Reporting (AER)\n");
6497 phba->cfg_aer_support = 0;
6499 rc = 0;
6502 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6504 * The FC Port needs to register FCFI (index 0)
6506 lpfc_reg_fcfi(phba, mboxq);
6507 mboxq->vport = phba->pport;
6508 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6509 if (rc != MBX_SUCCESS)
6510 goto out_unset_queue;
6511 rc = 0;
6512 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6513 &mboxq->u.mqe.un.reg_fcfi);
6515 /* Check if the port is configured to be disabled */
6516 lpfc_sli_read_link_ste(phba);
6520 * The port is ready, set the host's link state to LINK_DOWN
6521 * in preparation for link interrupts.
6523 spin_lock_irq(&phba->hbalock);
6524 phba->link_state = LPFC_LINK_DOWN;
6525 spin_unlock_irq(&phba->hbalock);
6526 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6527 (phba->hba_flag & LINK_DISABLED)) {
6528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6529 "3103 Adapter Link is disabled.\n");
6530 lpfc_down_link(phba, mboxq);
6531 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6532 if (rc != MBX_SUCCESS) {
6533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6534 "3104 Adapter failed to issue "
6535 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6536 goto out_unset_queue;
6538 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6539 /* don't perform init_link on SLI4 FC port loopback test */
6540 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6541 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6542 if (rc)
6543 goto out_unset_queue;
6546 mempool_free(mboxq, phba->mbox_mem_pool);
6547 return rc;
6548 out_unset_queue:
6549 /* Unset all the queues set up in this routine when error out */
6550 lpfc_sli4_queue_unset(phba);
6551 out_destroy_queue:
6552 lpfc_sli4_queue_destroy(phba);
6553 out_stop_timers:
6554 lpfc_stop_hba_timers(phba);
6555 out_free_mbox:
6556 mempool_free(mboxq, phba->mbox_mem_pool);
6557 return rc;
6561 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6562 * @ptr: context object - pointer to hba structure.
6564 * This is the callback function for mailbox timer. The mailbox
6565 * timer is armed when a new mailbox command is issued and the timer
6566 * is deleted when the mailbox complete. The function is called by
6567 * the kernel timer code when a mailbox does not complete within
6568 * expected time. This function wakes up the worker thread to
6569 * process the mailbox timeout and returns. All the processing is
6570 * done by the worker thread function lpfc_mbox_timeout_handler.
6572 void
6573 lpfc_mbox_timeout(unsigned long ptr)
6575 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6576 unsigned long iflag;
6577 uint32_t tmo_posted;
6579 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6580 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6581 if (!tmo_posted)
6582 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6583 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6585 if (!tmo_posted)
6586 lpfc_worker_wake_up(phba);
6587 return;
6592 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6593 * @phba: Pointer to HBA context object.
6595 * This function is called from worker thread when a mailbox command times out.
6596 * The caller is not required to hold any locks. This function will reset the
6597 * HBA and recover all the pending commands.
6599 void
6600 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6602 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6603 MAILBOX_t *mb = &pmbox->u.mb;
6604 struct lpfc_sli *psli = &phba->sli;
6605 struct lpfc_sli_ring *pring;
6607 /* Check the pmbox pointer first. There is a race condition
6608 * between the mbox timeout handler getting executed in the
6609 * worklist and the mailbox actually completing. When this
6610 * race condition occurs, the mbox_active will be NULL.
6612 spin_lock_irq(&phba->hbalock);
6613 if (pmbox == NULL) {
6614 lpfc_printf_log(phba, KERN_WARNING,
6615 LOG_MBOX | LOG_SLI,
6616 "0353 Active Mailbox cleared - mailbox timeout "
6617 "exiting\n");
6618 spin_unlock_irq(&phba->hbalock);
6619 return;
6622 /* Mbox cmd <mbxCommand> timeout */
6623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6624 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6625 mb->mbxCommand,
6626 phba->pport->port_state,
6627 phba->sli.sli_flag,
6628 phba->sli.mbox_active);
6629 spin_unlock_irq(&phba->hbalock);
6631 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6632 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6633 * it to fail all outstanding SCSI IO.
6635 spin_lock_irq(&phba->pport->work_port_lock);
6636 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6637 spin_unlock_irq(&phba->pport->work_port_lock);
6638 spin_lock_irq(&phba->hbalock);
6639 phba->link_state = LPFC_LINK_UNKNOWN;
6640 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6641 spin_unlock_irq(&phba->hbalock);
6643 pring = &psli->ring[psli->fcp_ring];
6644 lpfc_sli_abort_iocb_ring(phba, pring);
6646 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6647 "0345 Resetting board due to mailbox timeout\n");
6649 /* Reset the HBA device */
6650 lpfc_reset_hba(phba);
6654 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6655 * @phba: Pointer to HBA context object.
6656 * @pmbox: Pointer to mailbox object.
6657 * @flag: Flag indicating how the mailbox need to be processed.
6659 * This function is called by discovery code and HBA management code
6660 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6661 * function gets the hbalock to protect the data structures.
6662 * The mailbox command can be submitted in polling mode, in which case
6663 * this function will wait in a polling loop for the completion of the
6664 * mailbox.
6665 * If the mailbox is submitted in no_wait mode (not polling) the
6666 * function will submit the command and returns immediately without waiting
6667 * for the mailbox completion. The no_wait is supported only when HBA
6668 * is in SLI2/SLI3 mode - interrupts are enabled.
6669 * The SLI interface allows only one mailbox pending at a time. If the
6670 * mailbox is issued in polling mode and there is already a mailbox
6671 * pending, then the function will return an error. If the mailbox is issued
6672 * in NO_WAIT mode and there is a mailbox pending already, the function
6673 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6674 * The sli layer owns the mailbox object until the completion of mailbox
6675 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6676 * return codes the caller owns the mailbox command after the return of
6677 * the function.
6679 static int
6680 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6681 uint32_t flag)
6683 MAILBOX_t *mbx;
6684 struct lpfc_sli *psli = &phba->sli;
6685 uint32_t status, evtctr;
6686 uint32_t ha_copy, hc_copy;
6687 int i;
6688 unsigned long timeout;
6689 unsigned long drvr_flag = 0;
6690 uint32_t word0, ldata;
6691 void __iomem *to_slim;
6692 int processing_queue = 0;
6694 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6695 if (!pmbox) {
6696 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6697 /* processing mbox queue from intr_handler */
6698 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6700 return MBX_SUCCESS;
6702 processing_queue = 1;
6703 pmbox = lpfc_mbox_get(phba);
6704 if (!pmbox) {
6705 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6706 return MBX_SUCCESS;
6710 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6711 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6712 if(!pmbox->vport) {
6713 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6714 lpfc_printf_log(phba, KERN_ERR,
6715 LOG_MBOX | LOG_VPORT,
6716 "1806 Mbox x%x failed. No vport\n",
6717 pmbox->u.mb.mbxCommand);
6718 dump_stack();
6719 goto out_not_finished;
6723 /* If the PCI channel is in offline state, do not post mbox. */
6724 if (unlikely(pci_channel_offline(phba->pcidev))) {
6725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6726 goto out_not_finished;
6729 /* If HBA has a deferred error attention, fail the iocb. */
6730 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6732 goto out_not_finished;
6735 psli = &phba->sli;
6737 mbx = &pmbox->u.mb;
6738 status = MBX_SUCCESS;
6740 if (phba->link_state == LPFC_HBA_ERROR) {
6741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6743 /* Mbox command <mbxCommand> cannot issue */
6744 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6745 "(%d):0311 Mailbox command x%x cannot "
6746 "issue Data: x%x x%x\n",
6747 pmbox->vport ? pmbox->vport->vpi : 0,
6748 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6749 goto out_not_finished;
6752 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6753 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6754 !(hc_copy & HC_MBINT_ENA)) {
6755 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6757 "(%d):2528 Mailbox command x%x cannot "
6758 "issue Data: x%x x%x\n",
6759 pmbox->vport ? pmbox->vport->vpi : 0,
6760 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6761 goto out_not_finished;
6765 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6766 /* Polling for a mbox command when another one is already active
6767 * is not allowed in SLI. Also, the driver must have established
6768 * SLI2 mode to queue and process multiple mbox commands.
6771 if (flag & MBX_POLL) {
6772 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6774 /* Mbox command <mbxCommand> cannot issue */
6775 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6776 "(%d):2529 Mailbox command x%x "
6777 "cannot issue Data: x%x x%x\n",
6778 pmbox->vport ? pmbox->vport->vpi : 0,
6779 pmbox->u.mb.mbxCommand,
6780 psli->sli_flag, flag);
6781 goto out_not_finished;
6784 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6785 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6786 /* Mbox command <mbxCommand> cannot issue */
6787 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6788 "(%d):2530 Mailbox command x%x "
6789 "cannot issue Data: x%x x%x\n",
6790 pmbox->vport ? pmbox->vport->vpi : 0,
6791 pmbox->u.mb.mbxCommand,
6792 psli->sli_flag, flag);
6793 goto out_not_finished;
6796 /* Another mailbox command is still being processed, queue this
6797 * command to be processed later.
6799 lpfc_mbox_put(phba, pmbox);
6801 /* Mbox cmd issue - BUSY */
6802 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6803 "(%d):0308 Mbox cmd issue - BUSY Data: "
6804 "x%x x%x x%x x%x\n",
6805 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6806 mbx->mbxCommand, phba->pport->port_state,
6807 psli->sli_flag, flag);
6809 psli->slistat.mbox_busy++;
6810 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6812 if (pmbox->vport) {
6813 lpfc_debugfs_disc_trc(pmbox->vport,
6814 LPFC_DISC_TRC_MBOX_VPORT,
6815 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6816 (uint32_t)mbx->mbxCommand,
6817 mbx->un.varWords[0], mbx->un.varWords[1]);
6819 else {
6820 lpfc_debugfs_disc_trc(phba->pport,
6821 LPFC_DISC_TRC_MBOX,
6822 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6823 (uint32_t)mbx->mbxCommand,
6824 mbx->un.varWords[0], mbx->un.varWords[1]);
6827 return MBX_BUSY;
6830 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6832 /* If we are not polling, we MUST be in SLI2 mode */
6833 if (flag != MBX_POLL) {
6834 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6835 (mbx->mbxCommand != MBX_KILL_BOARD)) {
6836 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6838 /* Mbox command <mbxCommand> cannot issue */
6839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6840 "(%d):2531 Mailbox command x%x "
6841 "cannot issue Data: x%x x%x\n",
6842 pmbox->vport ? pmbox->vport->vpi : 0,
6843 pmbox->u.mb.mbxCommand,
6844 psli->sli_flag, flag);
6845 goto out_not_finished;
6847 /* timeout active mbox command */
6848 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6849 1000);
6850 mod_timer(&psli->mbox_tmo, jiffies + timeout);
6853 /* Mailbox cmd <cmd> issue */
6854 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6855 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6856 "x%x\n",
6857 pmbox->vport ? pmbox->vport->vpi : 0,
6858 mbx->mbxCommand, phba->pport->port_state,
6859 psli->sli_flag, flag);
6861 if (mbx->mbxCommand != MBX_HEARTBEAT) {
6862 if (pmbox->vport) {
6863 lpfc_debugfs_disc_trc(pmbox->vport,
6864 LPFC_DISC_TRC_MBOX_VPORT,
6865 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6866 (uint32_t)mbx->mbxCommand,
6867 mbx->un.varWords[0], mbx->un.varWords[1]);
6869 else {
6870 lpfc_debugfs_disc_trc(phba->pport,
6871 LPFC_DISC_TRC_MBOX,
6872 "MBOX Send: cmd:x%x mb:x%x x%x",
6873 (uint32_t)mbx->mbxCommand,
6874 mbx->un.varWords[0], mbx->un.varWords[1]);
6878 psli->slistat.mbox_cmd++;
6879 evtctr = psli->slistat.mbox_event;
6881 /* next set own bit for the adapter and copy over command word */
6882 mbx->mbxOwner = OWN_CHIP;
6884 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6885 /* Populate mbox extension offset word. */
6886 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6887 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
6888 = (uint8_t *)phba->mbox_ext
6889 - (uint8_t *)phba->mbox;
6892 /* Copy the mailbox extension data */
6893 if (pmbox->in_ext_byte_len && pmbox->context2) {
6894 lpfc_sli_pcimem_bcopy(pmbox->context2,
6895 (uint8_t *)phba->mbox_ext,
6896 pmbox->in_ext_byte_len);
6898 /* Copy command data to host SLIM area */
6899 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
6900 } else {
6901 /* Populate mbox extension offset word. */
6902 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6903 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
6904 = MAILBOX_HBA_EXT_OFFSET;
6906 /* Copy the mailbox extension data */
6907 if (pmbox->in_ext_byte_len && pmbox->context2) {
6908 lpfc_memcpy_to_slim(phba->MBslimaddr +
6909 MAILBOX_HBA_EXT_OFFSET,
6910 pmbox->context2, pmbox->in_ext_byte_len);
6913 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
6914 /* copy command data into host mbox for cmpl */
6915 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
6918 /* First copy mbox command data to HBA SLIM, skip past first
6919 word */
6920 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6921 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
6922 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6924 /* Next copy over first word, with mbxOwner set */
6925 ldata = *((uint32_t *)mbx);
6926 to_slim = phba->MBslimaddr;
6927 writel(ldata, to_slim);
6928 readl(to_slim); /* flush */
6930 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
6931 /* switch over to host mailbox */
6932 psli->sli_flag |= LPFC_SLI_ACTIVE;
6936 wmb();
6938 switch (flag) {
6939 case MBX_NOWAIT:
6940 /* Set up reference to mailbox command */
6941 psli->mbox_active = pmbox;
6942 /* Interrupt board to do it */
6943 writel(CA_MBATT, phba->CAregaddr);
6944 readl(phba->CAregaddr); /* flush */
6945 /* Don't wait for it to finish, just return */
6946 break;
6948 case MBX_POLL:
6949 /* Set up null reference to mailbox command */
6950 psli->mbox_active = NULL;
6951 /* Interrupt board to do it */
6952 writel(CA_MBATT, phba->CAregaddr);
6953 readl(phba->CAregaddr); /* flush */
6955 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6956 /* First read mbox status word */
6957 word0 = *((uint32_t *)phba->mbox);
6958 word0 = le32_to_cpu(word0);
6959 } else {
6960 /* First read mbox status word */
6961 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6962 spin_unlock_irqrestore(&phba->hbalock,
6963 drvr_flag);
6964 goto out_not_finished;
6968 /* Read the HBA Host Attention Register */
6969 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6970 spin_unlock_irqrestore(&phba->hbalock,
6971 drvr_flag);
6972 goto out_not_finished;
6974 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6975 1000) + jiffies;
6976 i = 0;
6977 /* Wait for command to complete */
6978 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6979 (!(ha_copy & HA_MBATT) &&
6980 (phba->link_state > LPFC_WARM_START))) {
6981 if (time_after(jiffies, timeout)) {
6982 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6983 spin_unlock_irqrestore(&phba->hbalock,
6984 drvr_flag);
6985 goto out_not_finished;
6988 /* Check if we took a mbox interrupt while we were
6989 polling */
6990 if (((word0 & OWN_CHIP) != OWN_CHIP)
6991 && (evtctr != psli->slistat.mbox_event))
6992 break;
6994 if (i++ > 10) {
6995 spin_unlock_irqrestore(&phba->hbalock,
6996 drvr_flag);
6997 msleep(1);
6998 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7001 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7002 /* First copy command data */
7003 word0 = *((uint32_t *)phba->mbox);
7004 word0 = le32_to_cpu(word0);
7005 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7006 MAILBOX_t *slimmb;
7007 uint32_t slimword0;
7008 /* Check real SLIM for any errors */
7009 slimword0 = readl(phba->MBslimaddr);
7010 slimmb = (MAILBOX_t *) & slimword0;
7011 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7012 && slimmb->mbxStatus) {
7013 psli->sli_flag &=
7014 ~LPFC_SLI_ACTIVE;
7015 word0 = slimword0;
7018 } else {
7019 /* First copy command data */
7020 word0 = readl(phba->MBslimaddr);
7022 /* Read the HBA Host Attention Register */
7023 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7024 spin_unlock_irqrestore(&phba->hbalock,
7025 drvr_flag);
7026 goto out_not_finished;
7030 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7031 /* copy results back to user */
7032 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7033 /* Copy the mailbox extension data */
7034 if (pmbox->out_ext_byte_len && pmbox->context2) {
7035 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7036 pmbox->context2,
7037 pmbox->out_ext_byte_len);
7039 } else {
7040 /* First copy command data */
7041 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7042 MAILBOX_CMD_SIZE);
7043 /* Copy the mailbox extension data */
7044 if (pmbox->out_ext_byte_len && pmbox->context2) {
7045 lpfc_memcpy_from_slim(pmbox->context2,
7046 phba->MBslimaddr +
7047 MAILBOX_HBA_EXT_OFFSET,
7048 pmbox->out_ext_byte_len);
7052 writel(HA_MBATT, phba->HAregaddr);
7053 readl(phba->HAregaddr); /* flush */
7055 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7056 status = mbx->mbxStatus;
7059 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7060 return status;
7062 out_not_finished:
7063 if (processing_queue) {
7064 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7065 lpfc_mbox_cmpl_put(phba, pmbox);
7067 return MBX_NOT_FINISHED;
7071 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7072 * @phba: Pointer to HBA context object.
7074 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7075 * the driver internal pending mailbox queue. It will then try to wait out the
7076 * possible outstanding mailbox command before return.
7078 * Returns:
7079 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7080 * the outstanding mailbox command timed out.
7082 static int
7083 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7085 struct lpfc_sli *psli = &phba->sli;
7086 int rc = 0;
7087 unsigned long timeout = 0;
7089 /* Mark the asynchronous mailbox command posting as blocked */
7090 spin_lock_irq(&phba->hbalock);
7091 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7092 /* Determine how long we might wait for the active mailbox
7093 * command to be gracefully completed by firmware.
7095 if (phba->sli.mbox_active)
7096 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7097 phba->sli.mbox_active) *
7098 1000) + jiffies;
7099 spin_unlock_irq(&phba->hbalock);
7101 /* Wait for the outstnading mailbox command to complete */
7102 while (phba->sli.mbox_active) {
7103 /* Check active mailbox complete status every 2ms */
7104 msleep(2);
7105 if (time_after(jiffies, timeout)) {
7106 /* Timeout, marked the outstanding cmd not complete */
7107 rc = 1;
7108 break;
7112 /* Can not cleanly block async mailbox command, fails it */
7113 if (rc) {
7114 spin_lock_irq(&phba->hbalock);
7115 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7116 spin_unlock_irq(&phba->hbalock);
7118 return rc;
7122 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7123 * @phba: Pointer to HBA context object.
7125 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7126 * commands from the driver internal pending mailbox queue. It makes sure
7127 * that there is no outstanding mailbox command before resuming posting
7128 * asynchronous mailbox commands. If, for any reason, there is outstanding
7129 * mailbox command, it will try to wait it out before resuming asynchronous
7130 * mailbox command posting.
7132 static void
7133 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7135 struct lpfc_sli *psli = &phba->sli;
7137 spin_lock_irq(&phba->hbalock);
7138 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7139 /* Asynchronous mailbox posting is not blocked, do nothing */
7140 spin_unlock_irq(&phba->hbalock);
7141 return;
7144 /* Outstanding synchronous mailbox command is guaranteed to be done,
7145 * successful or timeout, after timing-out the outstanding mailbox
7146 * command shall always be removed, so just unblock posting async
7147 * mailbox command and resume
7149 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7150 spin_unlock_irq(&phba->hbalock);
7152 /* wake up worker thread to post asynchronlous mailbox command */
7153 lpfc_worker_wake_up(phba);
7157 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7158 * @phba: Pointer to HBA context object.
7159 * @mboxq: Pointer to mailbox object.
7161 * The function waits for the bootstrap mailbox register ready bit from
7162 * port for twice the regular mailbox command timeout value.
7164 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7165 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7167 static int
7168 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7170 uint32_t db_ready;
7171 unsigned long timeout;
7172 struct lpfc_register bmbx_reg;
7174 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7175 * 1000) + jiffies;
7177 do {
7178 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7179 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7180 if (!db_ready)
7181 msleep(2);
7183 if (time_after(jiffies, timeout))
7184 return MBXERR_ERROR;
7185 } while (!db_ready);
7187 return 0;
7191 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7192 * @phba: Pointer to HBA context object.
7193 * @mboxq: Pointer to mailbox object.
7195 * The function posts a mailbox to the port. The mailbox is expected
7196 * to be comletely filled in and ready for the port to operate on it.
7197 * This routine executes a synchronous completion operation on the
7198 * mailbox by polling for its completion.
7200 * The caller must not be holding any locks when calling this routine.
7202 * Returns:
7203 * MBX_SUCCESS - mailbox posted successfully
7204 * Any of the MBX error values.
7206 static int
7207 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7209 int rc = MBX_SUCCESS;
7210 unsigned long iflag;
7211 uint32_t mcqe_status;
7212 uint32_t mbx_cmnd;
7213 struct lpfc_sli *psli = &phba->sli;
7214 struct lpfc_mqe *mb = &mboxq->u.mqe;
7215 struct lpfc_bmbx_create *mbox_rgn;
7216 struct dma_address *dma_address;
7219 * Only one mailbox can be active to the bootstrap mailbox region
7220 * at a time and there is no queueing provided.
7222 spin_lock_irqsave(&phba->hbalock, iflag);
7223 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7224 spin_unlock_irqrestore(&phba->hbalock, iflag);
7225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7226 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7227 "cannot issue Data: x%x x%x\n",
7228 mboxq->vport ? mboxq->vport->vpi : 0,
7229 mboxq->u.mb.mbxCommand,
7230 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7231 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7232 psli->sli_flag, MBX_POLL);
7233 return MBXERR_ERROR;
7235 /* The server grabs the token and owns it until release */
7236 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7237 phba->sli.mbox_active = mboxq;
7238 spin_unlock_irqrestore(&phba->hbalock, iflag);
7240 /* wait for bootstrap mbox register for readyness */
7241 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7242 if (rc)
7243 goto exit;
7246 * Initialize the bootstrap memory region to avoid stale data areas
7247 * in the mailbox post. Then copy the caller's mailbox contents to
7248 * the bmbx mailbox region.
7250 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7251 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7252 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7253 sizeof(struct lpfc_mqe));
7255 /* Post the high mailbox dma address to the port and wait for ready. */
7256 dma_address = &phba->sli4_hba.bmbx.dma_address;
7257 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7259 /* wait for bootstrap mbox register for hi-address write done */
7260 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7261 if (rc)
7262 goto exit;
7264 /* Post the low mailbox dma address to the port. */
7265 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7267 /* wait for bootstrap mbox register for low address write done */
7268 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7269 if (rc)
7270 goto exit;
7273 * Read the CQ to ensure the mailbox has completed.
7274 * If so, update the mailbox status so that the upper layers
7275 * can complete the request normally.
7277 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7278 sizeof(struct lpfc_mqe));
7279 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7280 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7281 sizeof(struct lpfc_mcqe));
7282 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7284 * When the CQE status indicates a failure and the mailbox status
7285 * indicates success then copy the CQE status into the mailbox status
7286 * (and prefix it with x4000).
7288 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7289 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7290 bf_set(lpfc_mqe_status, mb,
7291 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7292 rc = MBXERR_ERROR;
7293 } else
7294 lpfc_sli4_swap_str(phba, mboxq);
7296 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7297 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7298 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7299 " x%x x%x CQ: x%x x%x x%x x%x\n",
7300 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7301 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7302 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7303 bf_get(lpfc_mqe_status, mb),
7304 mb->un.mb_words[0], mb->un.mb_words[1],
7305 mb->un.mb_words[2], mb->un.mb_words[3],
7306 mb->un.mb_words[4], mb->un.mb_words[5],
7307 mb->un.mb_words[6], mb->un.mb_words[7],
7308 mb->un.mb_words[8], mb->un.mb_words[9],
7309 mb->un.mb_words[10], mb->un.mb_words[11],
7310 mb->un.mb_words[12], mboxq->mcqe.word0,
7311 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7312 mboxq->mcqe.trailer);
7313 exit:
7314 /* We are holding the token, no needed for lock when release */
7315 spin_lock_irqsave(&phba->hbalock, iflag);
7316 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7317 phba->sli.mbox_active = NULL;
7318 spin_unlock_irqrestore(&phba->hbalock, iflag);
7319 return rc;
7323 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7324 * @phba: Pointer to HBA context object.
7325 * @pmbox: Pointer to mailbox object.
7326 * @flag: Flag indicating how the mailbox need to be processed.
7328 * This function is called by discovery code and HBA management code to submit
7329 * a mailbox command to firmware with SLI-4 interface spec.
7331 * Return codes the caller owns the mailbox command after the return of the
7332 * function.
7334 static int
7335 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7336 uint32_t flag)
7338 struct lpfc_sli *psli = &phba->sli;
7339 unsigned long iflags;
7340 int rc;
7342 /* dump from issue mailbox command if setup */
7343 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7345 rc = lpfc_mbox_dev_check(phba);
7346 if (unlikely(rc)) {
7347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7348 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7349 "cannot issue Data: x%x x%x\n",
7350 mboxq->vport ? mboxq->vport->vpi : 0,
7351 mboxq->u.mb.mbxCommand,
7352 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7353 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7354 psli->sli_flag, flag);
7355 goto out_not_finished;
7358 /* Detect polling mode and jump to a handler */
7359 if (!phba->sli4_hba.intr_enable) {
7360 if (flag == MBX_POLL)
7361 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7362 else
7363 rc = -EIO;
7364 if (rc != MBX_SUCCESS)
7365 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7366 "(%d):2541 Mailbox command x%x "
7367 "(x%x/x%x) failure: "
7368 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7369 "Data: x%x x%x\n,",
7370 mboxq->vport ? mboxq->vport->vpi : 0,
7371 mboxq->u.mb.mbxCommand,
7372 lpfc_sli_config_mbox_subsys_get(phba,
7373 mboxq),
7374 lpfc_sli_config_mbox_opcode_get(phba,
7375 mboxq),
7376 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7377 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7378 bf_get(lpfc_mcqe_ext_status,
7379 &mboxq->mcqe),
7380 psli->sli_flag, flag);
7381 return rc;
7382 } else if (flag == MBX_POLL) {
7383 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7384 "(%d):2542 Try to issue mailbox command "
7385 "x%x (x%x/x%x) synchronously ahead of async"
7386 "mailbox command queue: x%x x%x\n",
7387 mboxq->vport ? mboxq->vport->vpi : 0,
7388 mboxq->u.mb.mbxCommand,
7389 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7390 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7391 psli->sli_flag, flag);
7392 /* Try to block the asynchronous mailbox posting */
7393 rc = lpfc_sli4_async_mbox_block(phba);
7394 if (!rc) {
7395 /* Successfully blocked, now issue sync mbox cmd */
7396 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7397 if (rc != MBX_SUCCESS)
7398 lpfc_printf_log(phba, KERN_WARNING,
7399 LOG_MBOX | LOG_SLI,
7400 "(%d):2597 Sync Mailbox command "
7401 "x%x (x%x/x%x) failure: "
7402 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7403 "Data: x%x x%x\n,",
7404 mboxq->vport ? mboxq->vport->vpi : 0,
7405 mboxq->u.mb.mbxCommand,
7406 lpfc_sli_config_mbox_subsys_get(phba,
7407 mboxq),
7408 lpfc_sli_config_mbox_opcode_get(phba,
7409 mboxq),
7410 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7411 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7412 bf_get(lpfc_mcqe_ext_status,
7413 &mboxq->mcqe),
7414 psli->sli_flag, flag);
7415 /* Unblock the async mailbox posting afterward */
7416 lpfc_sli4_async_mbox_unblock(phba);
7418 return rc;
7421 /* Now, interrupt mode asynchrous mailbox command */
7422 rc = lpfc_mbox_cmd_check(phba, mboxq);
7423 if (rc) {
7424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7425 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7426 "cannot issue Data: x%x x%x\n",
7427 mboxq->vport ? mboxq->vport->vpi : 0,
7428 mboxq->u.mb.mbxCommand,
7429 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7430 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7431 psli->sli_flag, flag);
7432 goto out_not_finished;
7435 /* Put the mailbox command to the driver internal FIFO */
7436 psli->slistat.mbox_busy++;
7437 spin_lock_irqsave(&phba->hbalock, iflags);
7438 lpfc_mbox_put(phba, mboxq);
7439 spin_unlock_irqrestore(&phba->hbalock, iflags);
7440 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7441 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7442 "x%x (x%x/x%x) x%x x%x x%x\n",
7443 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7444 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7445 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7446 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7447 phba->pport->port_state,
7448 psli->sli_flag, MBX_NOWAIT);
7449 /* Wake up worker thread to transport mailbox command from head */
7450 lpfc_worker_wake_up(phba);
7452 return MBX_BUSY;
7454 out_not_finished:
7455 return MBX_NOT_FINISHED;
7459 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7460 * @phba: Pointer to HBA context object.
7462 * This function is called by worker thread to send a mailbox command to
7463 * SLI4 HBA firmware.
7467 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7469 struct lpfc_sli *psli = &phba->sli;
7470 LPFC_MBOXQ_t *mboxq;
7471 int rc = MBX_SUCCESS;
7472 unsigned long iflags;
7473 struct lpfc_mqe *mqe;
7474 uint32_t mbx_cmnd;
7476 /* Check interrupt mode before post async mailbox command */
7477 if (unlikely(!phba->sli4_hba.intr_enable))
7478 return MBX_NOT_FINISHED;
7480 /* Check for mailbox command service token */
7481 spin_lock_irqsave(&phba->hbalock, iflags);
7482 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7483 spin_unlock_irqrestore(&phba->hbalock, iflags);
7484 return MBX_NOT_FINISHED;
7486 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7487 spin_unlock_irqrestore(&phba->hbalock, iflags);
7488 return MBX_NOT_FINISHED;
7490 if (unlikely(phba->sli.mbox_active)) {
7491 spin_unlock_irqrestore(&phba->hbalock, iflags);
7492 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7493 "0384 There is pending active mailbox cmd\n");
7494 return MBX_NOT_FINISHED;
7496 /* Take the mailbox command service token */
7497 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7499 /* Get the next mailbox command from head of queue */
7500 mboxq = lpfc_mbox_get(phba);
7502 /* If no more mailbox command waiting for post, we're done */
7503 if (!mboxq) {
7504 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7505 spin_unlock_irqrestore(&phba->hbalock, iflags);
7506 return MBX_SUCCESS;
7508 phba->sli.mbox_active = mboxq;
7509 spin_unlock_irqrestore(&phba->hbalock, iflags);
7511 /* Check device readiness for posting mailbox command */
7512 rc = lpfc_mbox_dev_check(phba);
7513 if (unlikely(rc))
7514 /* Driver clean routine will clean up pending mailbox */
7515 goto out_not_finished;
7517 /* Prepare the mbox command to be posted */
7518 mqe = &mboxq->u.mqe;
7519 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7521 /* Start timer for the mbox_tmo and log some mailbox post messages */
7522 mod_timer(&psli->mbox_tmo, (jiffies +
7523 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
7525 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7526 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7527 "x%x x%x\n",
7528 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7529 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7530 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7531 phba->pport->port_state, psli->sli_flag);
7533 if (mbx_cmnd != MBX_HEARTBEAT) {
7534 if (mboxq->vport) {
7535 lpfc_debugfs_disc_trc(mboxq->vport,
7536 LPFC_DISC_TRC_MBOX_VPORT,
7537 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7538 mbx_cmnd, mqe->un.mb_words[0],
7539 mqe->un.mb_words[1]);
7540 } else {
7541 lpfc_debugfs_disc_trc(phba->pport,
7542 LPFC_DISC_TRC_MBOX,
7543 "MBOX Send: cmd:x%x mb:x%x x%x",
7544 mbx_cmnd, mqe->un.mb_words[0],
7545 mqe->un.mb_words[1]);
7548 psli->slistat.mbox_cmd++;
7550 /* Post the mailbox command to the port */
7551 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7552 if (rc != MBX_SUCCESS) {
7553 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7554 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7555 "cannot issue Data: x%x x%x\n",
7556 mboxq->vport ? mboxq->vport->vpi : 0,
7557 mboxq->u.mb.mbxCommand,
7558 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7559 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7560 psli->sli_flag, MBX_NOWAIT);
7561 goto out_not_finished;
7564 return rc;
7566 out_not_finished:
7567 spin_lock_irqsave(&phba->hbalock, iflags);
7568 if (phba->sli.mbox_active) {
7569 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7570 __lpfc_mbox_cmpl_put(phba, mboxq);
7571 /* Release the token */
7572 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7573 phba->sli.mbox_active = NULL;
7575 spin_unlock_irqrestore(&phba->hbalock, iflags);
7577 return MBX_NOT_FINISHED;
7581 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7582 * @phba: Pointer to HBA context object.
7583 * @pmbox: Pointer to mailbox object.
7584 * @flag: Flag indicating how the mailbox need to be processed.
7586 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7587 * the API jump table function pointer from the lpfc_hba struct.
7589 * Return codes the caller owns the mailbox command after the return of the
7590 * function.
7593 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7595 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7599 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7600 * @phba: The hba struct for which this call is being executed.
7601 * @dev_grp: The HBA PCI-Device group number.
7603 * This routine sets up the mbox interface API function jump table in @phba
7604 * struct.
7605 * Returns: 0 - success, -ENODEV - failure.
7608 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7611 switch (dev_grp) {
7612 case LPFC_PCI_DEV_LP:
7613 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7614 phba->lpfc_sli_handle_slow_ring_event =
7615 lpfc_sli_handle_slow_ring_event_s3;
7616 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7617 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7618 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7619 break;
7620 case LPFC_PCI_DEV_OC:
7621 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7622 phba->lpfc_sli_handle_slow_ring_event =
7623 lpfc_sli_handle_slow_ring_event_s4;
7624 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7625 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7626 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7627 break;
7628 default:
7629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7630 "1420 Invalid HBA PCI-device group: 0x%x\n",
7631 dev_grp);
7632 return -ENODEV;
7633 break;
7635 return 0;
7639 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7640 * @phba: Pointer to HBA context object.
7641 * @pring: Pointer to driver SLI ring object.
7642 * @piocb: Pointer to address of newly added command iocb.
7644 * This function is called with hbalock held to add a command
7645 * iocb to the txq when SLI layer cannot submit the command iocb
7646 * to the ring.
7648 void
7649 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7650 struct lpfc_iocbq *piocb)
7652 /* Insert the caller's iocb in the txq tail for later processing. */
7653 list_add_tail(&piocb->list, &pring->txq);
7657 * lpfc_sli_next_iocb - Get the next iocb in the txq
7658 * @phba: Pointer to HBA context object.
7659 * @pring: Pointer to driver SLI ring object.
7660 * @piocb: Pointer to address of newly added command iocb.
7662 * This function is called with hbalock held before a new
7663 * iocb is submitted to the firmware. This function checks
7664 * txq to flush the iocbs in txq to Firmware before
7665 * submitting new iocbs to the Firmware.
7666 * If there are iocbs in the txq which need to be submitted
7667 * to firmware, lpfc_sli_next_iocb returns the first element
7668 * of the txq after dequeuing it from txq.
7669 * If there is no iocb in the txq then the function will return
7670 * *piocb and *piocb is set to NULL. Caller needs to check
7671 * *piocb to find if there are more commands in the txq.
7673 static struct lpfc_iocbq *
7674 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7675 struct lpfc_iocbq **piocb)
7677 struct lpfc_iocbq * nextiocb;
7679 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7680 if (!nextiocb) {
7681 nextiocb = *piocb;
7682 *piocb = NULL;
7685 return nextiocb;
7689 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7690 * @phba: Pointer to HBA context object.
7691 * @ring_number: SLI ring number to issue iocb on.
7692 * @piocb: Pointer to command iocb.
7693 * @flag: Flag indicating if this command can be put into txq.
7695 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7696 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7697 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7698 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7699 * this function allows only iocbs for posting buffers. This function finds
7700 * next available slot in the command ring and posts the command to the
7701 * available slot and writes the port attention register to request HBA start
7702 * processing new iocb. If there is no slot available in the ring and
7703 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7704 * the function returns IOCB_BUSY.
7706 * This function is called with hbalock held. The function will return success
7707 * after it successfully submit the iocb to firmware or after adding to the
7708 * txq.
7710 static int
7711 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7712 struct lpfc_iocbq *piocb, uint32_t flag)
7714 struct lpfc_iocbq *nextiocb;
7715 IOCB_t *iocb;
7716 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7718 if (piocb->iocb_cmpl && (!piocb->vport) &&
7719 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7720 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7721 lpfc_printf_log(phba, KERN_ERR,
7722 LOG_SLI | LOG_VPORT,
7723 "1807 IOCB x%x failed. No vport\n",
7724 piocb->iocb.ulpCommand);
7725 dump_stack();
7726 return IOCB_ERROR;
7730 /* If the PCI channel is in offline state, do not post iocbs. */
7731 if (unlikely(pci_channel_offline(phba->pcidev)))
7732 return IOCB_ERROR;
7734 /* If HBA has a deferred error attention, fail the iocb. */
7735 if (unlikely(phba->hba_flag & DEFER_ERATT))
7736 return IOCB_ERROR;
7739 * We should never get an IOCB if we are in a < LINK_DOWN state
7741 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7742 return IOCB_ERROR;
7745 * Check to see if we are blocking IOCB processing because of a
7746 * outstanding event.
7748 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7749 goto iocb_busy;
7751 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7753 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7754 * can be issued if the link is not up.
7756 switch (piocb->iocb.ulpCommand) {
7757 case CMD_GEN_REQUEST64_CR:
7758 case CMD_GEN_REQUEST64_CX:
7759 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7760 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7761 FC_RCTL_DD_UNSOL_CMD) ||
7762 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7763 MENLO_TRANSPORT_TYPE))
7765 goto iocb_busy;
7766 break;
7767 case CMD_QUE_RING_BUF_CN:
7768 case CMD_QUE_RING_BUF64_CN:
7770 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7771 * completion, iocb_cmpl MUST be 0.
7773 if (piocb->iocb_cmpl)
7774 piocb->iocb_cmpl = NULL;
7775 /*FALLTHROUGH*/
7776 case CMD_CREATE_XRI_CR:
7777 case CMD_CLOSE_XRI_CN:
7778 case CMD_CLOSE_XRI_CX:
7779 break;
7780 default:
7781 goto iocb_busy;
7785 * For FCP commands, we must be in a state where we can process link
7786 * attention events.
7788 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7789 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7790 goto iocb_busy;
7793 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7794 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7795 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7797 if (iocb)
7798 lpfc_sli_update_ring(phba, pring);
7799 else
7800 lpfc_sli_update_full_ring(phba, pring);
7802 if (!piocb)
7803 return IOCB_SUCCESS;
7805 goto out_busy;
7807 iocb_busy:
7808 pring->stats.iocb_cmd_delay++;
7810 out_busy:
7812 if (!(flag & SLI_IOCB_RET_IOCB)) {
7813 __lpfc_sli_ringtx_put(phba, pring, piocb);
7814 return IOCB_SUCCESS;
7817 return IOCB_BUSY;
7821 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7822 * @phba: Pointer to HBA context object.
7823 * @piocb: Pointer to command iocb.
7824 * @sglq: Pointer to the scatter gather queue object.
7826 * This routine converts the bpl or bde that is in the IOCB
7827 * to a sgl list for the sli4 hardware. The physical address
7828 * of the bpl/bde is converted back to a virtual address.
7829 * If the IOCB contains a BPL then the list of BDE's is
7830 * converted to sli4_sge's. If the IOCB contains a single
7831 * BDE then it is converted to a single sli_sge.
7832 * The IOCB is still in cpu endianess so the contents of
7833 * the bpl can be used without byte swapping.
7835 * Returns valid XRI = Success, NO_XRI = Failure.
7837 static uint16_t
7838 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7839 struct lpfc_sglq *sglq)
7841 uint16_t xritag = NO_XRI;
7842 struct ulp_bde64 *bpl = NULL;
7843 struct ulp_bde64 bde;
7844 struct sli4_sge *sgl = NULL;
7845 struct lpfc_dmabuf *dmabuf;
7846 IOCB_t *icmd;
7847 int numBdes = 0;
7848 int i = 0;
7849 uint32_t offset = 0; /* accumulated offset in the sg request list */
7850 int inbound = 0; /* number of sg reply entries inbound from firmware */
7852 if (!piocbq || !sglq)
7853 return xritag;
7855 sgl = (struct sli4_sge *)sglq->sgl;
7856 icmd = &piocbq->iocb;
7857 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7858 return sglq->sli4_xritag;
7859 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7860 numBdes = icmd->un.genreq64.bdl.bdeSize /
7861 sizeof(struct ulp_bde64);
7862 /* The addrHigh and addrLow fields within the IOCB
7863 * have not been byteswapped yet so there is no
7864 * need to swap them back.
7866 if (piocbq->context3)
7867 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7868 else
7869 return xritag;
7871 bpl = (struct ulp_bde64 *)dmabuf->virt;
7872 if (!bpl)
7873 return xritag;
7875 for (i = 0; i < numBdes; i++) {
7876 /* Should already be byte swapped. */
7877 sgl->addr_hi = bpl->addrHigh;
7878 sgl->addr_lo = bpl->addrLow;
7880 sgl->word2 = le32_to_cpu(sgl->word2);
7881 if ((i+1) == numBdes)
7882 bf_set(lpfc_sli4_sge_last, sgl, 1);
7883 else
7884 bf_set(lpfc_sli4_sge_last, sgl, 0);
7885 /* swap the size field back to the cpu so we
7886 * can assign it to the sgl.
7888 bde.tus.w = le32_to_cpu(bpl->tus.w);
7889 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7890 /* The offsets in the sgl need to be accumulated
7891 * separately for the request and reply lists.
7892 * The request is always first, the reply follows.
7894 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7895 /* add up the reply sg entries */
7896 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7897 inbound++;
7898 /* first inbound? reset the offset */
7899 if (inbound == 1)
7900 offset = 0;
7901 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7902 bf_set(lpfc_sli4_sge_type, sgl,
7903 LPFC_SGE_TYPE_DATA);
7904 offset += bde.tus.f.bdeSize;
7906 sgl->word2 = cpu_to_le32(sgl->word2);
7907 bpl++;
7908 sgl++;
7910 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7911 /* The addrHigh and addrLow fields of the BDE have not
7912 * been byteswapped yet so they need to be swapped
7913 * before putting them in the sgl.
7915 sgl->addr_hi =
7916 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7917 sgl->addr_lo =
7918 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7919 sgl->word2 = le32_to_cpu(sgl->word2);
7920 bf_set(lpfc_sli4_sge_last, sgl, 1);
7921 sgl->word2 = cpu_to_le32(sgl->word2);
7922 sgl->sge_len =
7923 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7925 return sglq->sli4_xritag;
7929 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7930 * @phba: Pointer to HBA context object.
7932 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7933 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7934 * held.
7936 * Return: index into SLI4 fast-path FCP queue index.
7938 static inline uint32_t
7939 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7941 struct lpfc_vector_map_info *cpup;
7942 int chann, cpu;
7944 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU) {
7945 cpu = smp_processor_id();
7946 if (cpu < phba->sli4_hba.num_present_cpu) {
7947 cpup = phba->sli4_hba.cpu_map;
7948 cpup += cpu;
7949 return cpup->channel_id;
7951 chann = cpu;
7953 chann = atomic_add_return(1, &phba->fcp_qidx);
7954 chann = (chann % phba->cfg_fcp_io_channel);
7955 return chann;
7959 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7960 * @phba: Pointer to HBA context object.
7961 * @piocb: Pointer to command iocb.
7962 * @wqe: Pointer to the work queue entry.
7964 * This routine converts the iocb command to its Work Queue Entry
7965 * equivalent. The wqe pointer should not have any fields set when
7966 * this routine is called because it will memcpy over them.
7967 * This routine does not set the CQ_ID or the WQEC bits in the
7968 * wqe.
7970 * Returns: 0 = Success, IOCB_ERROR = Failure.
7972 static int
7973 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7974 union lpfc_wqe *wqe)
7976 uint32_t xmit_len = 0, total_len = 0;
7977 uint8_t ct = 0;
7978 uint32_t fip;
7979 uint32_t abort_tag;
7980 uint8_t command_type = ELS_COMMAND_NON_FIP;
7981 uint8_t cmnd;
7982 uint16_t xritag;
7983 uint16_t abrt_iotag;
7984 struct lpfc_iocbq *abrtiocbq;
7985 struct ulp_bde64 *bpl = NULL;
7986 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7987 int numBdes, i;
7988 struct ulp_bde64 bde;
7989 struct lpfc_nodelist *ndlp;
7990 uint32_t *pcmd;
7991 uint32_t if_type;
7993 fip = phba->hba_flag & HBA_FIP_SUPPORT;
7994 /* The fcp commands will set command type */
7995 if (iocbq->iocb_flag & LPFC_IO_FCP)
7996 command_type = FCP_COMMAND;
7997 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7998 command_type = ELS_COMMAND_FIP;
7999 else
8000 command_type = ELS_COMMAND_NON_FIP;
8002 /* Some of the fields are in the right position already */
8003 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8004 abort_tag = (uint32_t) iocbq->iotag;
8005 xritag = iocbq->sli4_xritag;
8006 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8007 /* words0-2 bpl convert bde */
8008 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8009 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8010 sizeof(struct ulp_bde64);
8011 bpl = (struct ulp_bde64 *)
8012 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8013 if (!bpl)
8014 return IOCB_ERROR;
8016 /* Should already be byte swapped. */
8017 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8018 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8019 /* swap the size field back to the cpu so we
8020 * can assign it to the sgl.
8022 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
8023 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8024 total_len = 0;
8025 for (i = 0; i < numBdes; i++) {
8026 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8027 total_len += bde.tus.f.bdeSize;
8029 } else
8030 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8032 iocbq->iocb.ulpIoTag = iocbq->iotag;
8033 cmnd = iocbq->iocb.ulpCommand;
8035 switch (iocbq->iocb.ulpCommand) {
8036 case CMD_ELS_REQUEST64_CR:
8037 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8038 ndlp = iocbq->context_un.ndlp;
8039 else
8040 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8041 if (!iocbq->iocb.ulpLe) {
8042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8043 "2007 Only Limited Edition cmd Format"
8044 " supported 0x%x\n",
8045 iocbq->iocb.ulpCommand);
8046 return IOCB_ERROR;
8049 wqe->els_req.payload_len = xmit_len;
8050 /* Els_reguest64 has a TMO */
8051 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8052 iocbq->iocb.ulpTimeout);
8053 /* Need a VF for word 4 set the vf bit*/
8054 bf_set(els_req64_vf, &wqe->els_req, 0);
8055 /* And a VFID for word 12 */
8056 bf_set(els_req64_vfid, &wqe->els_req, 0);
8057 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8058 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8059 iocbq->iocb.ulpContext);
8060 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8061 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8062 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8063 if (command_type == ELS_COMMAND_FIP)
8064 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8065 >> LPFC_FIP_ELS_ID_SHIFT);
8066 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8067 iocbq->context2)->virt);
8068 if_type = bf_get(lpfc_sli_intf_if_type,
8069 &phba->sli4_hba.sli_intf);
8070 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8071 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8072 *pcmd == ELS_CMD_SCR ||
8073 *pcmd == ELS_CMD_FDISC ||
8074 *pcmd == ELS_CMD_LOGO ||
8075 *pcmd == ELS_CMD_PLOGI)) {
8076 bf_set(els_req64_sp, &wqe->els_req, 1);
8077 bf_set(els_req64_sid, &wqe->els_req,
8078 iocbq->vport->fc_myDID);
8079 if ((*pcmd == ELS_CMD_FLOGI) &&
8080 !(phba->fc_topology ==
8081 LPFC_TOPOLOGY_LOOP))
8082 bf_set(els_req64_sid, &wqe->els_req, 0);
8083 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8084 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8085 phba->vpi_ids[iocbq->vport->vpi]);
8086 } else if (pcmd && iocbq->context1) {
8087 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8088 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8089 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8092 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8093 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8094 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8095 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8096 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8097 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8098 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8099 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8100 break;
8101 case CMD_XMIT_SEQUENCE64_CX:
8102 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8103 iocbq->iocb.un.ulpWord[3]);
8104 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8105 iocbq->iocb.unsli3.rcvsli3.ox_id);
8106 /* The entire sequence is transmitted for this IOCB */
8107 xmit_len = total_len;
8108 cmnd = CMD_XMIT_SEQUENCE64_CR;
8109 if (phba->link_flag & LS_LOOPBACK_MODE)
8110 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8111 case CMD_XMIT_SEQUENCE64_CR:
8112 /* word3 iocb=io_tag32 wqe=reserved */
8113 wqe->xmit_sequence.rsvd3 = 0;
8114 /* word4 relative_offset memcpy */
8115 /* word5 r_ctl/df_ctl memcpy */
8116 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8117 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8118 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8119 LPFC_WQE_IOD_WRITE);
8120 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8121 LPFC_WQE_LENLOC_WORD12);
8122 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8123 wqe->xmit_sequence.xmit_len = xmit_len;
8124 command_type = OTHER_COMMAND;
8125 break;
8126 case CMD_XMIT_BCAST64_CN:
8127 /* word3 iocb=iotag32 wqe=seq_payload_len */
8128 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8129 /* word4 iocb=rsvd wqe=rsvd */
8130 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8131 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8132 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8133 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8134 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8135 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8136 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8137 LPFC_WQE_LENLOC_WORD3);
8138 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8139 break;
8140 case CMD_FCP_IWRITE64_CR:
8141 command_type = FCP_COMMAND_DATA_OUT;
8142 /* word3 iocb=iotag wqe=payload_offset_len */
8143 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8144 wqe->fcp_iwrite.payload_offset_len =
8145 xmit_len + sizeof(struct fcp_rsp);
8146 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8147 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8148 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8149 iocbq->iocb.ulpFCP2Rcvy);
8150 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8151 /* Always open the exchange */
8152 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
8153 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8154 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8155 LPFC_WQE_LENLOC_WORD4);
8156 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8157 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8158 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8159 break;
8160 case CMD_FCP_IREAD64_CR:
8161 /* word3 iocb=iotag wqe=payload_offset_len */
8162 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8163 wqe->fcp_iread.payload_offset_len =
8164 xmit_len + sizeof(struct fcp_rsp);
8165 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8166 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8167 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8168 iocbq->iocb.ulpFCP2Rcvy);
8169 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8170 /* Always open the exchange */
8171 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
8172 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8173 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8174 LPFC_WQE_LENLOC_WORD4);
8175 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8176 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8177 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8178 break;
8179 case CMD_FCP_ICMND64_CR:
8180 /* word3 iocb=IO_TAG wqe=reserved */
8181 wqe->fcp_icmd.rsrvd3 = 0;
8182 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8183 /* Always open the exchange */
8184 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8185 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8186 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8187 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8188 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8189 LPFC_WQE_LENLOC_NONE);
8190 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
8191 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8192 iocbq->iocb.ulpFCP2Rcvy);
8193 break;
8194 case CMD_GEN_REQUEST64_CR:
8195 /* For this command calculate the xmit length of the
8196 * request bde.
8198 xmit_len = 0;
8199 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8200 sizeof(struct ulp_bde64);
8201 for (i = 0; i < numBdes; i++) {
8202 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8203 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8204 break;
8205 xmit_len += bde.tus.f.bdeSize;
8207 /* word3 iocb=IO_TAG wqe=request_payload_len */
8208 wqe->gen_req.request_payload_len = xmit_len;
8209 /* word4 iocb=parameter wqe=relative_offset memcpy */
8210 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8211 /* word6 context tag copied in memcpy */
8212 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8213 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8214 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8215 "2015 Invalid CT %x command 0x%x\n",
8216 ct, iocbq->iocb.ulpCommand);
8217 return IOCB_ERROR;
8219 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8220 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8221 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8222 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8223 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8224 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8225 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8226 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8227 command_type = OTHER_COMMAND;
8228 break;
8229 case CMD_XMIT_ELS_RSP64_CX:
8230 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8231 /* words0-2 BDE memcpy */
8232 /* word3 iocb=iotag32 wqe=response_payload_len */
8233 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8234 /* word4 */
8235 wqe->xmit_els_rsp.word4 = 0;
8236 /* word5 iocb=rsvd wge=did */
8237 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8238 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8240 if_type = bf_get(lpfc_sli_intf_if_type,
8241 &phba->sli4_hba.sli_intf);
8242 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8243 if (iocbq->vport->fc_flag & FC_PT2PT) {
8244 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8245 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8246 iocbq->vport->fc_myDID);
8247 if (iocbq->vport->fc_myDID == Fabric_DID) {
8248 bf_set(wqe_els_did,
8249 &wqe->xmit_els_rsp.wqe_dest, 0);
8253 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8254 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8255 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8256 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8257 iocbq->iocb.unsli3.rcvsli3.ox_id);
8258 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8259 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8260 phba->vpi_ids[iocbq->vport->vpi]);
8261 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8262 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8263 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8264 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8265 LPFC_WQE_LENLOC_WORD3);
8266 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8267 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8268 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8269 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8270 iocbq->context2)->virt);
8271 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8272 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8273 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8274 iocbq->vport->fc_myDID);
8275 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8276 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8277 phba->vpi_ids[phba->pport->vpi]);
8279 command_type = OTHER_COMMAND;
8280 break;
8281 case CMD_CLOSE_XRI_CN:
8282 case CMD_ABORT_XRI_CN:
8283 case CMD_ABORT_XRI_CX:
8284 /* words 0-2 memcpy should be 0 rserved */
8285 /* port will send abts */
8286 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8287 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8288 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8289 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8290 } else
8291 fip = 0;
8293 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8295 * The link is down, or the command was ELS_FIP
8296 * so the fw does not need to send abts
8297 * on the wire.
8299 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8300 else
8301 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8302 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8303 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8304 wqe->abort_cmd.rsrvd5 = 0;
8305 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8306 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8307 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8309 * The abort handler will send us CMD_ABORT_XRI_CN or
8310 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8312 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8313 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8314 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8315 LPFC_WQE_LENLOC_NONE);
8316 cmnd = CMD_ABORT_XRI_CX;
8317 command_type = OTHER_COMMAND;
8318 xritag = 0;
8319 break;
8320 case CMD_XMIT_BLS_RSP64_CX:
8321 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8322 /* As BLS ABTS RSP WQE is very different from other WQEs,
8323 * we re-construct this WQE here based on information in
8324 * iocbq from scratch.
8326 memset(wqe, 0, sizeof(union lpfc_wqe));
8327 /* OX_ID is invariable to who sent ABTS to CT exchange */
8328 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8329 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8330 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8331 LPFC_ABTS_UNSOL_INT) {
8332 /* ABTS sent by initiator to CT exchange, the
8333 * RX_ID field will be filled with the newly
8334 * allocated responder XRI.
8336 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8337 iocbq->sli4_xritag);
8338 } else {
8339 /* ABTS sent by responder to CT exchange, the
8340 * RX_ID field will be filled with the responder
8341 * RX_ID from ABTS.
8343 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8344 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8346 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8347 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8349 /* Use CT=VPI */
8350 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8351 ndlp->nlp_DID);
8352 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8353 iocbq->iocb.ulpContext);
8354 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8355 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8356 phba->vpi_ids[phba->pport->vpi]);
8357 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8358 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8359 LPFC_WQE_LENLOC_NONE);
8360 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8361 command_type = OTHER_COMMAND;
8362 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8363 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8364 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8365 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8366 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8367 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8368 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8371 break;
8372 case CMD_XRI_ABORTED_CX:
8373 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8374 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8375 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8376 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8377 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8378 default:
8379 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8380 "2014 Invalid command 0x%x\n",
8381 iocbq->iocb.ulpCommand);
8382 return IOCB_ERROR;
8383 break;
8386 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8387 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8388 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8389 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8390 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8391 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8392 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8393 LPFC_IO_DIF_INSERT);
8394 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8395 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8396 wqe->generic.wqe_com.abort_tag = abort_tag;
8397 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8398 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8399 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8400 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8401 return 0;
8405 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8406 * @phba: Pointer to HBA context object.
8407 * @ring_number: SLI ring number to issue iocb on.
8408 * @piocb: Pointer to command iocb.
8409 * @flag: Flag indicating if this command can be put into txq.
8411 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8412 * an iocb command to an HBA with SLI-4 interface spec.
8414 * This function is called with hbalock held. The function will return success
8415 * after it successfully submit the iocb to firmware or after adding to the
8416 * txq.
8418 static int
8419 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8420 struct lpfc_iocbq *piocb, uint32_t flag)
8422 struct lpfc_sglq *sglq;
8423 union lpfc_wqe wqe;
8424 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8426 if (piocb->sli4_xritag == NO_XRI) {
8427 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8428 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8429 sglq = NULL;
8430 else {
8431 if (!list_empty(&pring->txq)) {
8432 if (!(flag & SLI_IOCB_RET_IOCB)) {
8433 __lpfc_sli_ringtx_put(phba,
8434 pring, piocb);
8435 return IOCB_SUCCESS;
8436 } else {
8437 return IOCB_BUSY;
8439 } else {
8440 sglq = __lpfc_sli_get_sglq(phba, piocb);
8441 if (!sglq) {
8442 if (!(flag & SLI_IOCB_RET_IOCB)) {
8443 __lpfc_sli_ringtx_put(phba,
8444 pring,
8445 piocb);
8446 return IOCB_SUCCESS;
8447 } else
8448 return IOCB_BUSY;
8452 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8453 /* These IO's already have an XRI and a mapped sgl. */
8454 sglq = NULL;
8455 } else {
8457 * This is a continuation of a commandi,(CX) so this
8458 * sglq is on the active list
8460 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
8461 if (!sglq)
8462 return IOCB_ERROR;
8465 if (sglq) {
8466 piocb->sli4_lxritag = sglq->sli4_lxritag;
8467 piocb->sli4_xritag = sglq->sli4_xritag;
8468 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8469 return IOCB_ERROR;
8472 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8473 return IOCB_ERROR;
8475 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8476 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8477 if (unlikely(!phba->sli4_hba.fcp_wq))
8478 return IOCB_ERROR;
8479 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8480 &wqe))
8481 return IOCB_ERROR;
8482 } else {
8483 if (unlikely(!phba->sli4_hba.els_wq))
8484 return IOCB_ERROR;
8485 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8486 return IOCB_ERROR;
8488 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8490 return 0;
8494 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8496 * This routine wraps the actual lockless version for issusing IOCB function
8497 * pointer from the lpfc_hba struct.
8499 * Return codes:
8500 * IOCB_ERROR - Error
8501 * IOCB_SUCCESS - Success
8502 * IOCB_BUSY - Busy
8505 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8506 struct lpfc_iocbq *piocb, uint32_t flag)
8508 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8512 * lpfc_sli_api_table_setup - Set up sli api function jump table
8513 * @phba: The hba struct for which this call is being executed.
8514 * @dev_grp: The HBA PCI-Device group number.
8516 * This routine sets up the SLI interface API function jump table in @phba
8517 * struct.
8518 * Returns: 0 - success, -ENODEV - failure.
8521 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8524 switch (dev_grp) {
8525 case LPFC_PCI_DEV_LP:
8526 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8527 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8528 break;
8529 case LPFC_PCI_DEV_OC:
8530 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8531 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8532 break;
8533 default:
8534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8535 "1419 Invalid HBA PCI-device group: 0x%x\n",
8536 dev_grp);
8537 return -ENODEV;
8538 break;
8540 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8541 return 0;
8545 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8546 * @phba: Pointer to HBA context object.
8547 * @pring: Pointer to driver SLI ring object.
8548 * @piocb: Pointer to command iocb.
8549 * @flag: Flag indicating if this command can be put into txq.
8551 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8552 * function. This function gets the hbalock and calls
8553 * __lpfc_sli_issue_iocb function and will return the error returned
8554 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8555 * functions which do not hold hbalock.
8558 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8559 struct lpfc_iocbq *piocb, uint32_t flag)
8561 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
8562 struct lpfc_sli_ring *pring;
8563 struct lpfc_queue *fpeq;
8564 struct lpfc_eqe *eqe;
8565 unsigned long iflags;
8566 int rc, idx;
8568 if (phba->sli_rev == LPFC_SLI_REV4) {
8569 if (piocb->iocb_flag & LPFC_IO_FCP) {
8570 if (unlikely(!phba->sli4_hba.fcp_wq))
8571 return IOCB_ERROR;
8572 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8573 piocb->fcp_wqidx = idx;
8574 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8576 pring = &phba->sli.ring[ring_number];
8577 spin_lock_irqsave(&pring->ring_lock, iflags);
8578 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8579 flag);
8580 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8582 if (lpfc_fcp_look_ahead) {
8583 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8585 if (atomic_dec_and_test(&fcp_eq_hdl->
8586 fcp_eq_in_use)) {
8588 /* Get associated EQ with this index */
8589 fpeq = phba->sli4_hba.hba_eq[idx];
8591 /* Turn off interrupts from this EQ */
8592 lpfc_sli4_eq_clr_intr(fpeq);
8595 * Process all the events on FCP EQ
8597 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8598 lpfc_sli4_hba_handle_eqe(phba,
8599 eqe, idx);
8600 fpeq->EQ_processed++;
8603 /* Always clear and re-arm the EQ */
8604 lpfc_sli4_eq_release(fpeq,
8605 LPFC_QUEUE_REARM);
8607 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8609 } else {
8610 pring = &phba->sli.ring[ring_number];
8611 spin_lock_irqsave(&pring->ring_lock, iflags);
8612 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8613 flag);
8614 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8617 } else {
8618 /* For now, SLI2/3 will still use hbalock */
8619 spin_lock_irqsave(&phba->hbalock, iflags);
8620 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8621 spin_unlock_irqrestore(&phba->hbalock, iflags);
8623 return rc;
8627 * lpfc_extra_ring_setup - Extra ring setup function
8628 * @phba: Pointer to HBA context object.
8630 * This function is called while driver attaches with the
8631 * HBA to setup the extra ring. The extra ring is used
8632 * only when driver needs to support target mode functionality
8633 * or IP over FC functionalities.
8635 * This function is called with no lock held.
8637 static int
8638 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8640 struct lpfc_sli *psli;
8641 struct lpfc_sli_ring *pring;
8643 psli = &phba->sli;
8645 /* Adjust cmd/rsp ring iocb entries more evenly */
8647 /* Take some away from the FCP ring */
8648 pring = &psli->ring[psli->fcp_ring];
8649 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8650 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8651 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8652 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8654 /* and give them to the extra ring */
8655 pring = &psli->ring[psli->extra_ring];
8657 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8658 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8659 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8660 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8662 /* Setup default profile for this ring */
8663 pring->iotag_max = 4096;
8664 pring->num_mask = 1;
8665 pring->prt[0].profile = 0; /* Mask 0 */
8666 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8667 pring->prt[0].type = phba->cfg_multi_ring_type;
8668 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8669 return 0;
8672 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8673 * @phba: Pointer to HBA context object.
8674 * @iocbq: Pointer to iocb object.
8676 * The async_event handler calls this routine when it receives
8677 * an ASYNC_STATUS_CN event from the port. The port generates
8678 * this event when an Abort Sequence request to an rport fails
8679 * twice in succession. The abort could be originated by the
8680 * driver or by the port. The ABTS could have been for an ELS
8681 * or FCP IO. The port only generates this event when an ABTS
8682 * fails to complete after one retry.
8684 static void
8685 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8686 struct lpfc_iocbq *iocbq)
8688 struct lpfc_nodelist *ndlp = NULL;
8689 uint16_t rpi = 0, vpi = 0;
8690 struct lpfc_vport *vport = NULL;
8692 /* The rpi in the ulpContext is vport-sensitive. */
8693 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8694 rpi = iocbq->iocb.ulpContext;
8696 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8697 "3092 Port generated ABTS async event "
8698 "on vpi %d rpi %d status 0x%x\n",
8699 vpi, rpi, iocbq->iocb.ulpStatus);
8701 vport = lpfc_find_vport_by_vpid(phba, vpi);
8702 if (!vport)
8703 goto err_exit;
8704 ndlp = lpfc_findnode_rpi(vport, rpi);
8705 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8706 goto err_exit;
8708 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8709 lpfc_sli_abts_recover_port(vport, ndlp);
8710 return;
8712 err_exit:
8713 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8714 "3095 Event Context not found, no "
8715 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8716 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8717 vpi, rpi);
8720 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8721 * @phba: pointer to HBA context object.
8722 * @ndlp: nodelist pointer for the impacted rport.
8723 * @axri: pointer to the wcqe containing the failed exchange.
8725 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8726 * port. The port generates this event when an abort exchange request to an
8727 * rport fails twice in succession with no reply. The abort could be originated
8728 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8730 void
8731 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8732 struct lpfc_nodelist *ndlp,
8733 struct sli4_wcqe_xri_aborted *axri)
8735 struct lpfc_vport *vport;
8736 uint32_t ext_status = 0;
8738 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
8739 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8740 "3115 Node Context not found, driver "
8741 "ignoring abts err event\n");
8742 return;
8745 vport = ndlp->vport;
8746 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8747 "3116 Port generated FCP XRI ABORT event on "
8748 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8749 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
8750 bf_get(lpfc_wcqe_xa_xri, axri),
8751 bf_get(lpfc_wcqe_xa_status, axri),
8752 axri->parameter);
8755 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8756 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8757 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8759 ext_status = axri->parameter & IOERR_PARAM_MASK;
8760 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8761 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
8762 lpfc_sli_abts_recover_port(vport, ndlp);
8766 * lpfc_sli_async_event_handler - ASYNC iocb handler function
8767 * @phba: Pointer to HBA context object.
8768 * @pring: Pointer to driver SLI ring object.
8769 * @iocbq: Pointer to iocb object.
8771 * This function is called by the slow ring event handler
8772 * function when there is an ASYNC event iocb in the ring.
8773 * This function is called with no lock held.
8774 * Currently this function handles only temperature related
8775 * ASYNC events. The function decodes the temperature sensor
8776 * event message and posts events for the management applications.
8778 static void
8779 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8780 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8782 IOCB_t *icmd;
8783 uint16_t evt_code;
8784 struct temp_event temp_event_data;
8785 struct Scsi_Host *shost;
8786 uint32_t *iocb_w;
8788 icmd = &iocbq->iocb;
8789 evt_code = icmd->un.asyncstat.evt_code;
8791 switch (evt_code) {
8792 case ASYNC_TEMP_WARN:
8793 case ASYNC_TEMP_SAFE:
8794 temp_event_data.data = (uint32_t) icmd->ulpContext;
8795 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8796 if (evt_code == ASYNC_TEMP_WARN) {
8797 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8798 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8799 "0347 Adapter is very hot, please take "
8800 "corrective action. temperature : %d Celsius\n",
8801 (uint32_t) icmd->ulpContext);
8802 } else {
8803 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8804 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8805 "0340 Adapter temperature is OK now. "
8806 "temperature : %d Celsius\n",
8807 (uint32_t) icmd->ulpContext);
8810 /* Send temperature change event to applications */
8811 shost = lpfc_shost_from_vport(phba->pport);
8812 fc_host_post_vendor_event(shost, fc_get_event_number(),
8813 sizeof(temp_event_data), (char *) &temp_event_data,
8814 LPFC_NL_VENDOR_ID);
8815 break;
8816 case ASYNC_STATUS_CN:
8817 lpfc_sli_abts_err_handler(phba, iocbq);
8818 break;
8819 default:
8820 iocb_w = (uint32_t *) icmd;
8821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8822 "0346 Ring %d handler: unexpected ASYNC_STATUS"
8823 " evt_code 0x%x\n"
8824 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8825 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8826 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8827 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
8828 pring->ringno, icmd->un.asyncstat.evt_code,
8829 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8830 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8831 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8832 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8834 break;
8840 * lpfc_sli_setup - SLI ring setup function
8841 * @phba: Pointer to HBA context object.
8843 * lpfc_sli_setup sets up rings of the SLI interface with
8844 * number of iocbs per ring and iotags. This function is
8845 * called while driver attach to the HBA and before the
8846 * interrupts are enabled. So there is no need for locking.
8848 * This function always returns 0.
8851 lpfc_sli_setup(struct lpfc_hba *phba)
8853 int i, totiocbsize = 0;
8854 struct lpfc_sli *psli = &phba->sli;
8855 struct lpfc_sli_ring *pring;
8857 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8858 if (phba->sli_rev == LPFC_SLI_REV4)
8859 psli->num_rings += phba->cfg_fcp_io_channel;
8860 psli->sli_flag = 0;
8861 psli->fcp_ring = LPFC_FCP_RING;
8862 psli->next_ring = LPFC_FCP_NEXT_RING;
8863 psli->extra_ring = LPFC_EXTRA_RING;
8865 psli->iocbq_lookup = NULL;
8866 psli->iocbq_lookup_len = 0;
8867 psli->last_iotag = 0;
8869 for (i = 0; i < psli->num_rings; i++) {
8870 pring = &psli->ring[i];
8871 switch (i) {
8872 case LPFC_FCP_RING: /* ring 0 - FCP */
8873 /* numCiocb and numRiocb are used in config_port */
8874 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8875 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8876 pring->sli.sli3.numCiocb +=
8877 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8878 pring->sli.sli3.numRiocb +=
8879 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8880 pring->sli.sli3.numCiocb +=
8881 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8882 pring->sli.sli3.numRiocb +=
8883 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8884 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8885 SLI3_IOCB_CMD_SIZE :
8886 SLI2_IOCB_CMD_SIZE;
8887 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8888 SLI3_IOCB_RSP_SIZE :
8889 SLI2_IOCB_RSP_SIZE;
8890 pring->iotag_ctr = 0;
8891 pring->iotag_max =
8892 (phba->cfg_hba_queue_depth * 2);
8893 pring->fast_iotag = pring->iotag_max;
8894 pring->num_mask = 0;
8895 break;
8896 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
8897 /* numCiocb and numRiocb are used in config_port */
8898 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8899 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8900 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8901 SLI3_IOCB_CMD_SIZE :
8902 SLI2_IOCB_CMD_SIZE;
8903 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8904 SLI3_IOCB_RSP_SIZE :
8905 SLI2_IOCB_RSP_SIZE;
8906 pring->iotag_max = phba->cfg_hba_queue_depth;
8907 pring->num_mask = 0;
8908 break;
8909 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8910 /* numCiocb and numRiocb are used in config_port */
8911 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8912 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8913 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8914 SLI3_IOCB_CMD_SIZE :
8915 SLI2_IOCB_CMD_SIZE;
8916 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8917 SLI3_IOCB_RSP_SIZE :
8918 SLI2_IOCB_RSP_SIZE;
8919 pring->fast_iotag = 0;
8920 pring->iotag_ctr = 0;
8921 pring->iotag_max = 4096;
8922 pring->lpfc_sli_rcv_async_status =
8923 lpfc_sli_async_event_handler;
8924 pring->num_mask = LPFC_MAX_RING_MASK;
8925 pring->prt[0].profile = 0; /* Mask 0 */
8926 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8927 pring->prt[0].type = FC_TYPE_ELS;
8928 pring->prt[0].lpfc_sli_rcv_unsol_event =
8929 lpfc_els_unsol_event;
8930 pring->prt[1].profile = 0; /* Mask 1 */
8931 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8932 pring->prt[1].type = FC_TYPE_ELS;
8933 pring->prt[1].lpfc_sli_rcv_unsol_event =
8934 lpfc_els_unsol_event;
8935 pring->prt[2].profile = 0; /* Mask 2 */
8936 /* NameServer Inquiry */
8937 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
8938 /* NameServer */
8939 pring->prt[2].type = FC_TYPE_CT;
8940 pring->prt[2].lpfc_sli_rcv_unsol_event =
8941 lpfc_ct_unsol_event;
8942 pring->prt[3].profile = 0; /* Mask 3 */
8943 /* NameServer response */
8944 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
8945 /* NameServer */
8946 pring->prt[3].type = FC_TYPE_CT;
8947 pring->prt[3].lpfc_sli_rcv_unsol_event =
8948 lpfc_ct_unsol_event;
8949 break;
8951 totiocbsize += (pring->sli.sli3.numCiocb *
8952 pring->sli.sli3.sizeCiocb) +
8953 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
8955 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
8956 /* Too many cmd / rsp ring entries in SLI2 SLIM */
8957 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8958 "SLI2 SLIM Data: x%x x%lx\n",
8959 phba->brd_no, totiocbsize,
8960 (unsigned long) MAX_SLIM_IOCB_SIZE);
8962 if (phba->cfg_multi_ring_support == 2)
8963 lpfc_extra_ring_setup(phba);
8965 return 0;
8969 * lpfc_sli_queue_setup - Queue initialization function
8970 * @phba: Pointer to HBA context object.
8972 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8973 * ring. This function also initializes ring indices of each ring.
8974 * This function is called during the initialization of the SLI
8975 * interface of an HBA.
8976 * This function is called with no lock held and always returns
8977 * 1.
8980 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8982 struct lpfc_sli *psli;
8983 struct lpfc_sli_ring *pring;
8984 int i;
8986 psli = &phba->sli;
8987 spin_lock_irq(&phba->hbalock);
8988 INIT_LIST_HEAD(&psli->mboxq);
8989 INIT_LIST_HEAD(&psli->mboxq_cmpl);
8990 /* Initialize list headers for txq and txcmplq as double linked lists */
8991 for (i = 0; i < psli->num_rings; i++) {
8992 pring = &psli->ring[i];
8993 pring->ringno = i;
8994 pring->sli.sli3.next_cmdidx = 0;
8995 pring->sli.sli3.local_getidx = 0;
8996 pring->sli.sli3.cmdidx = 0;
8997 INIT_LIST_HEAD(&pring->txq);
8998 INIT_LIST_HEAD(&pring->txcmplq);
8999 INIT_LIST_HEAD(&pring->iocb_continueq);
9000 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9001 INIT_LIST_HEAD(&pring->postbufq);
9002 spin_lock_init(&pring->ring_lock);
9004 spin_unlock_irq(&phba->hbalock);
9005 return 1;
9009 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9010 * @phba: Pointer to HBA context object.
9012 * This routine flushes the mailbox command subsystem. It will unconditionally
9013 * flush all the mailbox commands in the three possible stages in the mailbox
9014 * command sub-system: pending mailbox command queue; the outstanding mailbox
9015 * command; and completed mailbox command queue. It is caller's responsibility
9016 * to make sure that the driver is in the proper state to flush the mailbox
9017 * command sub-system. Namely, the posting of mailbox commands into the
9018 * pending mailbox command queue from the various clients must be stopped;
9019 * either the HBA is in a state that it will never works on the outstanding
9020 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9021 * mailbox command has been completed.
9023 static void
9024 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9026 LIST_HEAD(completions);
9027 struct lpfc_sli *psli = &phba->sli;
9028 LPFC_MBOXQ_t *pmb;
9029 unsigned long iflag;
9031 /* Flush all the mailbox commands in the mbox system */
9032 spin_lock_irqsave(&phba->hbalock, iflag);
9033 /* The pending mailbox command queue */
9034 list_splice_init(&phba->sli.mboxq, &completions);
9035 /* The outstanding active mailbox command */
9036 if (psli->mbox_active) {
9037 list_add_tail(&psli->mbox_active->list, &completions);
9038 psli->mbox_active = NULL;
9039 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9041 /* The completed mailbox command queue */
9042 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9043 spin_unlock_irqrestore(&phba->hbalock, iflag);
9045 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9046 while (!list_empty(&completions)) {
9047 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9048 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9049 if (pmb->mbox_cmpl)
9050 pmb->mbox_cmpl(phba, pmb);
9055 * lpfc_sli_host_down - Vport cleanup function
9056 * @vport: Pointer to virtual port object.
9058 * lpfc_sli_host_down is called to clean up the resources
9059 * associated with a vport before destroying virtual
9060 * port data structures.
9061 * This function does following operations:
9062 * - Free discovery resources associated with this virtual
9063 * port.
9064 * - Free iocbs associated with this virtual port in
9065 * the txq.
9066 * - Send abort for all iocb commands associated with this
9067 * vport in txcmplq.
9069 * This function is called with no lock held and always returns 1.
9072 lpfc_sli_host_down(struct lpfc_vport *vport)
9074 LIST_HEAD(completions);
9075 struct lpfc_hba *phba = vport->phba;
9076 struct lpfc_sli *psli = &phba->sli;
9077 struct lpfc_sli_ring *pring;
9078 struct lpfc_iocbq *iocb, *next_iocb;
9079 int i;
9080 unsigned long flags = 0;
9081 uint16_t prev_pring_flag;
9083 lpfc_cleanup_discovery_resources(vport);
9085 spin_lock_irqsave(&phba->hbalock, flags);
9086 for (i = 0; i < psli->num_rings; i++) {
9087 pring = &psli->ring[i];
9088 prev_pring_flag = pring->flag;
9089 /* Only slow rings */
9090 if (pring->ringno == LPFC_ELS_RING) {
9091 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9092 /* Set the lpfc data pending flag */
9093 set_bit(LPFC_DATA_READY, &phba->data_flags);
9096 * Error everything on the txq since these iocbs have not been
9097 * given to the FW yet.
9099 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9100 if (iocb->vport != vport)
9101 continue;
9102 list_move_tail(&iocb->list, &completions);
9105 /* Next issue ABTS for everything on the txcmplq */
9106 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9107 list) {
9108 if (iocb->vport != vport)
9109 continue;
9110 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9113 pring->flag = prev_pring_flag;
9116 spin_unlock_irqrestore(&phba->hbalock, flags);
9118 /* Cancel all the IOCBs from the completions list */
9119 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9120 IOERR_SLI_DOWN);
9121 return 1;
9125 * lpfc_sli_hba_down - Resource cleanup function for the HBA
9126 * @phba: Pointer to HBA context object.
9128 * This function cleans up all iocb, buffers, mailbox commands
9129 * while shutting down the HBA. This function is called with no
9130 * lock held and always returns 1.
9131 * This function does the following to cleanup driver resources:
9132 * - Free discovery resources for each virtual port
9133 * - Cleanup any pending fabric iocbs
9134 * - Iterate through the iocb txq and free each entry
9135 * in the list.
9136 * - Free up any buffer posted to the HBA
9137 * - Free mailbox commands in the mailbox queue.
9140 lpfc_sli_hba_down(struct lpfc_hba *phba)
9142 LIST_HEAD(completions);
9143 struct lpfc_sli *psli = &phba->sli;
9144 struct lpfc_sli_ring *pring;
9145 struct lpfc_dmabuf *buf_ptr;
9146 unsigned long flags = 0;
9147 int i;
9149 /* Shutdown the mailbox command sub-system */
9150 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9152 lpfc_hba_down_prep(phba);
9154 lpfc_fabric_abort_hba(phba);
9156 spin_lock_irqsave(&phba->hbalock, flags);
9157 for (i = 0; i < psli->num_rings; i++) {
9158 pring = &psli->ring[i];
9159 /* Only slow rings */
9160 if (pring->ringno == LPFC_ELS_RING) {
9161 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9162 /* Set the lpfc data pending flag */
9163 set_bit(LPFC_DATA_READY, &phba->data_flags);
9167 * Error everything on the txq since these iocbs have not been
9168 * given to the FW yet.
9170 list_splice_init(&pring->txq, &completions);
9172 spin_unlock_irqrestore(&phba->hbalock, flags);
9174 /* Cancel all the IOCBs from the completions list */
9175 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9176 IOERR_SLI_DOWN);
9178 spin_lock_irqsave(&phba->hbalock, flags);
9179 list_splice_init(&phba->elsbuf, &completions);
9180 phba->elsbuf_cnt = 0;
9181 phba->elsbuf_prev_cnt = 0;
9182 spin_unlock_irqrestore(&phba->hbalock, flags);
9184 while (!list_empty(&completions)) {
9185 list_remove_head(&completions, buf_ptr,
9186 struct lpfc_dmabuf, list);
9187 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9188 kfree(buf_ptr);
9191 /* Return any active mbox cmds */
9192 del_timer_sync(&psli->mbox_tmo);
9194 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9195 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9196 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9198 return 1;
9202 * lpfc_sli_pcimem_bcopy - SLI memory copy function
9203 * @srcp: Source memory pointer.
9204 * @destp: Destination memory pointer.
9205 * @cnt: Number of words required to be copied.
9207 * This function is used for copying data between driver memory
9208 * and the SLI memory. This function also changes the endianness
9209 * of each word if native endianness is different from SLI
9210 * endianness. This function can be called with or without
9211 * lock.
9213 void
9214 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9216 uint32_t *src = srcp;
9217 uint32_t *dest = destp;
9218 uint32_t ldata;
9219 int i;
9221 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9222 ldata = *src;
9223 ldata = le32_to_cpu(ldata);
9224 *dest = ldata;
9225 src++;
9226 dest++;
9232 * lpfc_sli_bemem_bcopy - SLI memory copy function
9233 * @srcp: Source memory pointer.
9234 * @destp: Destination memory pointer.
9235 * @cnt: Number of words required to be copied.
9237 * This function is used for copying data between a data structure
9238 * with big endian representation to local endianness.
9239 * This function can be called with or without lock.
9241 void
9242 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9244 uint32_t *src = srcp;
9245 uint32_t *dest = destp;
9246 uint32_t ldata;
9247 int i;
9249 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9250 ldata = *src;
9251 ldata = be32_to_cpu(ldata);
9252 *dest = ldata;
9253 src++;
9254 dest++;
9259 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9260 * @phba: Pointer to HBA context object.
9261 * @pring: Pointer to driver SLI ring object.
9262 * @mp: Pointer to driver buffer object.
9264 * This function is called with no lock held.
9265 * It always return zero after adding the buffer to the postbufq
9266 * buffer list.
9269 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9270 struct lpfc_dmabuf *mp)
9272 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9273 later */
9274 spin_lock_irq(&phba->hbalock);
9275 list_add_tail(&mp->list, &pring->postbufq);
9276 pring->postbufq_cnt++;
9277 spin_unlock_irq(&phba->hbalock);
9278 return 0;
9282 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9283 * @phba: Pointer to HBA context object.
9285 * When HBQ is enabled, buffers are searched based on tags. This function
9286 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9287 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9288 * does not conflict with tags of buffer posted for unsolicited events.
9289 * The function returns the allocated tag. The function is called with
9290 * no locks held.
9292 uint32_t
9293 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9295 spin_lock_irq(&phba->hbalock);
9296 phba->buffer_tag_count++;
9298 * Always set the QUE_BUFTAG_BIT to distiguish between
9299 * a tag assigned by HBQ.
9301 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9302 spin_unlock_irq(&phba->hbalock);
9303 return phba->buffer_tag_count;
9307 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9308 * @phba: Pointer to HBA context object.
9309 * @pring: Pointer to driver SLI ring object.
9310 * @tag: Buffer tag.
9312 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9313 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9314 * iocb is posted to the response ring with the tag of the buffer.
9315 * This function searches the pring->postbufq list using the tag
9316 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9317 * iocb. If the buffer is found then lpfc_dmabuf object of the
9318 * buffer is returned to the caller else NULL is returned.
9319 * This function is called with no lock held.
9321 struct lpfc_dmabuf *
9322 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9323 uint32_t tag)
9325 struct lpfc_dmabuf *mp, *next_mp;
9326 struct list_head *slp = &pring->postbufq;
9328 /* Search postbufq, from the beginning, looking for a match on tag */
9329 spin_lock_irq(&phba->hbalock);
9330 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9331 if (mp->buffer_tag == tag) {
9332 list_del_init(&mp->list);
9333 pring->postbufq_cnt--;
9334 spin_unlock_irq(&phba->hbalock);
9335 return mp;
9339 spin_unlock_irq(&phba->hbalock);
9340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9341 "0402 Cannot find virtual addr for buffer tag on "
9342 "ring %d Data x%lx x%p x%p x%x\n",
9343 pring->ringno, (unsigned long) tag,
9344 slp->next, slp->prev, pring->postbufq_cnt);
9346 return NULL;
9350 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9351 * @phba: Pointer to HBA context object.
9352 * @pring: Pointer to driver SLI ring object.
9353 * @phys: DMA address of the buffer.
9355 * This function searches the buffer list using the dma_address
9356 * of unsolicited event to find the driver's lpfc_dmabuf object
9357 * corresponding to the dma_address. The function returns the
9358 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9359 * This function is called by the ct and els unsolicited event
9360 * handlers to get the buffer associated with the unsolicited
9361 * event.
9363 * This function is called with no lock held.
9365 struct lpfc_dmabuf *
9366 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9367 dma_addr_t phys)
9369 struct lpfc_dmabuf *mp, *next_mp;
9370 struct list_head *slp = &pring->postbufq;
9372 /* Search postbufq, from the beginning, looking for a match on phys */
9373 spin_lock_irq(&phba->hbalock);
9374 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9375 if (mp->phys == phys) {
9376 list_del_init(&mp->list);
9377 pring->postbufq_cnt--;
9378 spin_unlock_irq(&phba->hbalock);
9379 return mp;
9383 spin_unlock_irq(&phba->hbalock);
9384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9385 "0410 Cannot find virtual addr for mapped buf on "
9386 "ring %d Data x%llx x%p x%p x%x\n",
9387 pring->ringno, (unsigned long long)phys,
9388 slp->next, slp->prev, pring->postbufq_cnt);
9389 return NULL;
9393 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9394 * @phba: Pointer to HBA context object.
9395 * @cmdiocb: Pointer to driver command iocb object.
9396 * @rspiocb: Pointer to driver response iocb object.
9398 * This function is the completion handler for the abort iocbs for
9399 * ELS commands. This function is called from the ELS ring event
9400 * handler with no lock held. This function frees memory resources
9401 * associated with the abort iocb.
9403 static void
9404 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9405 struct lpfc_iocbq *rspiocb)
9407 IOCB_t *irsp = &rspiocb->iocb;
9408 uint16_t abort_iotag, abort_context;
9409 struct lpfc_iocbq *abort_iocb = NULL;
9411 if (irsp->ulpStatus) {
9414 * Assume that the port already completed and returned, or
9415 * will return the iocb. Just Log the message.
9417 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9418 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9420 spin_lock_irq(&phba->hbalock);
9421 if (phba->sli_rev < LPFC_SLI_REV4) {
9422 if (abort_iotag != 0 &&
9423 abort_iotag <= phba->sli.last_iotag)
9424 abort_iocb =
9425 phba->sli.iocbq_lookup[abort_iotag];
9426 } else
9427 /* For sli4 the abort_tag is the XRI,
9428 * so the abort routine puts the iotag of the iocb
9429 * being aborted in the context field of the abort
9430 * IOCB.
9432 abort_iocb = phba->sli.iocbq_lookup[abort_context];
9434 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9435 "0327 Cannot abort els iocb %p "
9436 "with tag %x context %x, abort status %x, "
9437 "abort code %x\n",
9438 abort_iocb, abort_iotag, abort_context,
9439 irsp->ulpStatus, irsp->un.ulpWord[4]);
9441 spin_unlock_irq(&phba->hbalock);
9443 lpfc_sli_release_iocbq(phba, cmdiocb);
9444 return;
9448 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9449 * @phba: Pointer to HBA context object.
9450 * @cmdiocb: Pointer to driver command iocb object.
9451 * @rspiocb: Pointer to driver response iocb object.
9453 * The function is called from SLI ring event handler with no
9454 * lock held. This function is the completion handler for ELS commands
9455 * which are aborted. The function frees memory resources used for
9456 * the aborted ELS commands.
9458 static void
9459 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9460 struct lpfc_iocbq *rspiocb)
9462 IOCB_t *irsp = &rspiocb->iocb;
9464 /* ELS cmd tag <ulpIoTag> completes */
9465 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9466 "0139 Ignoring ELS cmd tag x%x completion Data: "
9467 "x%x x%x x%x\n",
9468 irsp->ulpIoTag, irsp->ulpStatus,
9469 irsp->un.ulpWord[4], irsp->ulpTimeout);
9470 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9471 lpfc_ct_free_iocb(phba, cmdiocb);
9472 else
9473 lpfc_els_free_iocb(phba, cmdiocb);
9474 return;
9478 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9479 * @phba: Pointer to HBA context object.
9480 * @pring: Pointer to driver SLI ring object.
9481 * @cmdiocb: Pointer to driver command iocb object.
9483 * This function issues an abort iocb for the provided command iocb down to
9484 * the port. Other than the case the outstanding command iocb is an abort
9485 * request, this function issues abort out unconditionally. This function is
9486 * called with hbalock held. The function returns 0 when it fails due to
9487 * memory allocation failure or when the command iocb is an abort request.
9489 static int
9490 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9491 struct lpfc_iocbq *cmdiocb)
9493 struct lpfc_vport *vport = cmdiocb->vport;
9494 struct lpfc_iocbq *abtsiocbp;
9495 IOCB_t *icmd = NULL;
9496 IOCB_t *iabt = NULL;
9497 int retval;
9498 unsigned long iflags;
9501 * There are certain command types we don't want to abort. And we
9502 * don't want to abort commands that are already in the process of
9503 * being aborted.
9505 icmd = &cmdiocb->iocb;
9506 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9507 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9508 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9509 return 0;
9511 /* issue ABTS for this IOCB based on iotag */
9512 abtsiocbp = __lpfc_sli_get_iocbq(phba);
9513 if (abtsiocbp == NULL)
9514 return 0;
9516 /* This signals the response to set the correct status
9517 * before calling the completion handler
9519 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9521 iabt = &abtsiocbp->iocb;
9522 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9523 iabt->un.acxri.abortContextTag = icmd->ulpContext;
9524 if (phba->sli_rev == LPFC_SLI_REV4) {
9525 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9526 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9528 else
9529 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9530 iabt->ulpLe = 1;
9531 iabt->ulpClass = icmd->ulpClass;
9533 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9534 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9535 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9536 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9538 if (phba->link_state >= LPFC_LINK_UP)
9539 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9540 else
9541 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9543 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9545 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9546 "0339 Abort xri x%x, original iotag x%x, "
9547 "abort cmd iotag x%x\n",
9548 iabt->un.acxri.abortIoTag,
9549 iabt->un.acxri.abortContextTag,
9550 abtsiocbp->iotag);
9552 if (phba->sli_rev == LPFC_SLI_REV4) {
9553 /* Note: both hbalock and ring_lock need to be set here */
9554 spin_lock_irqsave(&pring->ring_lock, iflags);
9555 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9556 abtsiocbp, 0);
9557 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9558 } else {
9559 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9560 abtsiocbp, 0);
9563 if (retval)
9564 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9567 * Caller to this routine should check for IOCB_ERROR
9568 * and handle it properly. This routine no longer removes
9569 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9571 return retval;
9575 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9576 * @phba: Pointer to HBA context object.
9577 * @pring: Pointer to driver SLI ring object.
9578 * @cmdiocb: Pointer to driver command iocb object.
9580 * This function issues an abort iocb for the provided command iocb. In case
9581 * of unloading, the abort iocb will not be issued to commands on the ELS
9582 * ring. Instead, the callback function shall be changed to those commands
9583 * so that nothing happens when them finishes. This function is called with
9584 * hbalock held. The function returns 0 when the command iocb is an abort
9585 * request.
9588 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9589 struct lpfc_iocbq *cmdiocb)
9591 struct lpfc_vport *vport = cmdiocb->vport;
9592 int retval = IOCB_ERROR;
9593 IOCB_t *icmd = NULL;
9596 * There are certain command types we don't want to abort. And we
9597 * don't want to abort commands that are already in the process of
9598 * being aborted.
9600 icmd = &cmdiocb->iocb;
9601 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9602 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9603 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9604 return 0;
9607 * If we're unloading, don't abort iocb on the ELS ring, but change
9608 * the callback so that nothing happens when it finishes.
9610 if ((vport->load_flag & FC_UNLOADING) &&
9611 (pring->ringno == LPFC_ELS_RING)) {
9612 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9613 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9614 else
9615 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9616 goto abort_iotag_exit;
9619 /* Now, we try to issue the abort to the cmdiocb out */
9620 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9622 abort_iotag_exit:
9624 * Caller to this routine should check for IOCB_ERROR
9625 * and handle it properly. This routine no longer removes
9626 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9628 return retval;
9632 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9633 * @phba: Pointer to HBA context object.
9634 * @pring: Pointer to driver SLI ring object.
9636 * This function aborts all iocbs in the given ring and frees all the iocb
9637 * objects in txq. This function issues abort iocbs unconditionally for all
9638 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9639 * to complete before the return of this function. The caller is not required
9640 * to hold any locks.
9642 static void
9643 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9645 LIST_HEAD(completions);
9646 struct lpfc_iocbq *iocb, *next_iocb;
9648 if (pring->ringno == LPFC_ELS_RING)
9649 lpfc_fabric_abort_hba(phba);
9651 spin_lock_irq(&phba->hbalock);
9653 /* Take off all the iocbs on txq for cancelling */
9654 list_splice_init(&pring->txq, &completions);
9655 pring->txq_cnt = 0;
9657 /* Next issue ABTS for everything on the txcmplq */
9658 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9659 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9661 spin_unlock_irq(&phba->hbalock);
9663 /* Cancel all the IOCBs from the completions list */
9664 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9665 IOERR_SLI_ABORTED);
9669 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9670 * @phba: pointer to lpfc HBA data structure.
9672 * This routine will abort all pending and outstanding iocbs to an HBA.
9674 void
9675 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9677 struct lpfc_sli *psli = &phba->sli;
9678 struct lpfc_sli_ring *pring;
9679 int i;
9681 for (i = 0; i < psli->num_rings; i++) {
9682 pring = &psli->ring[i];
9683 lpfc_sli_iocb_ring_abort(phba, pring);
9688 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9689 * @iocbq: Pointer to driver iocb object.
9690 * @vport: Pointer to driver virtual port object.
9691 * @tgt_id: SCSI ID of the target.
9692 * @lun_id: LUN ID of the scsi device.
9693 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9695 * This function acts as an iocb filter for functions which abort or count
9696 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9697 * 0 if the filtering criteria is met for the given iocb and will return
9698 * 1 if the filtering criteria is not met.
9699 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9700 * given iocb is for the SCSI device specified by vport, tgt_id and
9701 * lun_id parameter.
9702 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9703 * given iocb is for the SCSI target specified by vport and tgt_id
9704 * parameters.
9705 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9706 * given iocb is for the SCSI host associated with the given vport.
9707 * This function is called with no locks held.
9709 static int
9710 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9711 uint16_t tgt_id, uint64_t lun_id,
9712 lpfc_ctx_cmd ctx_cmd)
9714 struct lpfc_scsi_buf *lpfc_cmd;
9715 int rc = 1;
9717 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9718 return rc;
9720 if (iocbq->vport != vport)
9721 return rc;
9723 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9725 if (lpfc_cmd->pCmd == NULL)
9726 return rc;
9728 switch (ctx_cmd) {
9729 case LPFC_CTX_LUN:
9730 if ((lpfc_cmd->rdata->pnode) &&
9731 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9732 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9733 rc = 0;
9734 break;
9735 case LPFC_CTX_TGT:
9736 if ((lpfc_cmd->rdata->pnode) &&
9737 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9738 rc = 0;
9739 break;
9740 case LPFC_CTX_HOST:
9741 rc = 0;
9742 break;
9743 default:
9744 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9745 __func__, ctx_cmd);
9746 break;
9749 return rc;
9753 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
9754 * @vport: Pointer to virtual port.
9755 * @tgt_id: SCSI ID of the target.
9756 * @lun_id: LUN ID of the scsi device.
9757 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9759 * This function returns number of FCP commands pending for the vport.
9760 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9761 * commands pending on the vport associated with SCSI device specified
9762 * by tgt_id and lun_id parameters.
9763 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9764 * commands pending on the vport associated with SCSI target specified
9765 * by tgt_id parameter.
9766 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9767 * commands pending on the vport.
9768 * This function returns the number of iocbs which satisfy the filter.
9769 * This function is called without any lock held.
9772 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9773 lpfc_ctx_cmd ctx_cmd)
9775 struct lpfc_hba *phba = vport->phba;
9776 struct lpfc_iocbq *iocbq;
9777 int sum, i;
9779 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9780 iocbq = phba->sli.iocbq_lookup[i];
9782 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9783 ctx_cmd) == 0)
9784 sum++;
9787 return sum;
9791 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
9792 * @phba: Pointer to HBA context object
9793 * @cmdiocb: Pointer to command iocb object.
9794 * @rspiocb: Pointer to response iocb object.
9796 * This function is called when an aborted FCP iocb completes. This
9797 * function is called by the ring event handler with no lock held.
9798 * This function frees the iocb.
9800 void
9801 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9802 struct lpfc_iocbq *rspiocb)
9804 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9805 "3096 ABORT_XRI_CN completing on rpi x%x "
9806 "original iotag x%x, abort cmd iotag x%x "
9807 "status 0x%x, reason 0x%x\n",
9808 cmdiocb->iocb.un.acxri.abortContextTag,
9809 cmdiocb->iocb.un.acxri.abortIoTag,
9810 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9811 rspiocb->iocb.un.ulpWord[4]);
9812 lpfc_sli_release_iocbq(phba, cmdiocb);
9813 return;
9817 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
9818 * @vport: Pointer to virtual port.
9819 * @pring: Pointer to driver SLI ring object.
9820 * @tgt_id: SCSI ID of the target.
9821 * @lun_id: LUN ID of the scsi device.
9822 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9824 * This function sends an abort command for every SCSI command
9825 * associated with the given virtual port pending on the ring
9826 * filtered by lpfc_sli_validate_fcp_iocb function.
9827 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9828 * FCP iocbs associated with lun specified by tgt_id and lun_id
9829 * parameters
9830 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9831 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9832 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9833 * FCP iocbs associated with virtual port.
9834 * This function returns number of iocbs it failed to abort.
9835 * This function is called with no locks held.
9838 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9839 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
9841 struct lpfc_hba *phba = vport->phba;
9842 struct lpfc_iocbq *iocbq;
9843 struct lpfc_iocbq *abtsiocb;
9844 IOCB_t *cmd = NULL;
9845 int errcnt = 0, ret_val = 0;
9846 int i;
9848 for (i = 1; i <= phba->sli.last_iotag; i++) {
9849 iocbq = phba->sli.iocbq_lookup[i];
9851 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
9852 abort_cmd) != 0)
9853 continue;
9856 * If the iocbq is already being aborted, don't take a second
9857 * action, but do count it.
9859 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
9860 continue;
9862 /* issue ABTS for this IOCB based on iotag */
9863 abtsiocb = lpfc_sli_get_iocbq(phba);
9864 if (abtsiocb == NULL) {
9865 errcnt++;
9866 continue;
9869 /* indicate the IO is being aborted by the driver. */
9870 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
9872 cmd = &iocbq->iocb;
9873 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9874 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
9875 if (phba->sli_rev == LPFC_SLI_REV4)
9876 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9877 else
9878 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
9879 abtsiocb->iocb.ulpLe = 1;
9880 abtsiocb->iocb.ulpClass = cmd->ulpClass;
9881 abtsiocb->vport = vport;
9883 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9884 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
9885 if (iocbq->iocb_flag & LPFC_IO_FCP)
9886 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9888 if (lpfc_is_link_up(phba))
9889 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9890 else
9891 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9893 /* Setup callback routine and issue the command. */
9894 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
9895 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9896 abtsiocb, 0);
9897 if (ret_val == IOCB_ERROR) {
9898 lpfc_sli_release_iocbq(phba, abtsiocb);
9899 errcnt++;
9900 continue;
9904 return errcnt;
9908 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
9909 * @phba: Pointer to HBA context object.
9910 * @cmdiocbq: Pointer to command iocb.
9911 * @rspiocbq: Pointer to response iocb.
9913 * This function is the completion handler for iocbs issued using
9914 * lpfc_sli_issue_iocb_wait function. This function is called by the
9915 * ring event handler function without any lock held. This function
9916 * can be called from both worker thread context and interrupt
9917 * context. This function also can be called from other thread which
9918 * cleans up the SLI layer objects.
9919 * This function copy the contents of the response iocb to the
9920 * response iocb memory object provided by the caller of
9921 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9922 * sleeps for the iocb completion.
9924 static void
9925 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9926 struct lpfc_iocbq *cmdiocbq,
9927 struct lpfc_iocbq *rspiocbq)
9929 wait_queue_head_t *pdone_q;
9930 unsigned long iflags;
9931 struct lpfc_scsi_buf *lpfc_cmd;
9933 spin_lock_irqsave(&phba->hbalock, iflags);
9934 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
9937 * A time out has occurred for the iocb. If a time out
9938 * completion handler has been supplied, call it. Otherwise,
9939 * just free the iocbq.
9942 spin_unlock_irqrestore(&phba->hbalock, iflags);
9943 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
9944 cmdiocbq->wait_iocb_cmpl = NULL;
9945 if (cmdiocbq->iocb_cmpl)
9946 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
9947 else
9948 lpfc_sli_release_iocbq(phba, cmdiocbq);
9949 return;
9952 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9953 if (cmdiocbq->context2 && rspiocbq)
9954 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9955 &rspiocbq->iocb, sizeof(IOCB_t));
9957 /* Set the exchange busy flag for task management commands */
9958 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9959 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9960 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9961 cur_iocbq);
9962 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9965 pdone_q = cmdiocbq->context_un.wait_queue;
9966 if (pdone_q)
9967 wake_up(pdone_q);
9968 spin_unlock_irqrestore(&phba->hbalock, iflags);
9969 return;
9973 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9974 * @phba: Pointer to HBA context object..
9975 * @piocbq: Pointer to command iocb.
9976 * @flag: Flag to test.
9978 * This routine grabs the hbalock and then test the iocb_flag to
9979 * see if the passed in flag is set.
9980 * Returns:
9981 * 1 if flag is set.
9982 * 0 if flag is not set.
9984 static int
9985 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9986 struct lpfc_iocbq *piocbq, uint32_t flag)
9988 unsigned long iflags;
9989 int ret;
9991 spin_lock_irqsave(&phba->hbalock, iflags);
9992 ret = piocbq->iocb_flag & flag;
9993 spin_unlock_irqrestore(&phba->hbalock, iflags);
9994 return ret;
9999 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
10000 * @phba: Pointer to HBA context object..
10001 * @pring: Pointer to sli ring.
10002 * @piocb: Pointer to command iocb.
10003 * @prspiocbq: Pointer to response iocb.
10004 * @timeout: Timeout in number of seconds.
10006 * This function issues the iocb to firmware and waits for the
10007 * iocb to complete. The iocb_cmpl field of the shall be used
10008 * to handle iocbs which time out. If the field is NULL, the
10009 * function shall free the iocbq structure. If more clean up is
10010 * needed, the caller is expected to provide a completion function
10011 * that will provide the needed clean up. If the iocb command is
10012 * not completed within timeout seconds, the function will either
10013 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10014 * completion function set in the iocb_cmpl field and then return
10015 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10016 * resources if this function returns IOCB_TIMEDOUT.
10017 * The function waits for the iocb completion using an
10018 * non-interruptible wait.
10019 * This function will sleep while waiting for iocb completion.
10020 * So, this function should not be called from any context which
10021 * does not allow sleeping. Due to the same reason, this function
10022 * cannot be called with interrupt disabled.
10023 * This function assumes that the iocb completions occur while
10024 * this function sleep. So, this function cannot be called from
10025 * the thread which process iocb completion for this ring.
10026 * This function clears the iocb_flag of the iocb object before
10027 * issuing the iocb and the iocb completion handler sets this
10028 * flag and wakes this thread when the iocb completes.
10029 * The contents of the response iocb will be copied to prspiocbq
10030 * by the completion handler when the command completes.
10031 * This function returns IOCB_SUCCESS when success.
10032 * This function is called with no lock held.
10035 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
10036 uint32_t ring_number,
10037 struct lpfc_iocbq *piocb,
10038 struct lpfc_iocbq *prspiocbq,
10039 uint32_t timeout)
10041 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10042 long timeleft, timeout_req = 0;
10043 int retval = IOCB_SUCCESS;
10044 uint32_t creg_val;
10045 struct lpfc_iocbq *iocb;
10046 int txq_cnt = 0;
10047 int txcmplq_cnt = 0;
10048 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10049 unsigned long iflags;
10050 bool iocb_completed = true;
10053 * If the caller has provided a response iocbq buffer, then context2
10054 * is NULL or its an error.
10056 if (prspiocbq) {
10057 if (piocb->context2)
10058 return IOCB_ERROR;
10059 piocb->context2 = prspiocbq;
10062 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
10063 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10064 piocb->context_un.wait_queue = &done_q;
10065 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
10067 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10068 if (lpfc_readl(phba->HCregaddr, &creg_val))
10069 return IOCB_ERROR;
10070 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10071 writel(creg_val, phba->HCregaddr);
10072 readl(phba->HCregaddr); /* flush */
10075 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10076 SLI_IOCB_RET_IOCB);
10077 if (retval == IOCB_SUCCESS) {
10078 timeout_req = msecs_to_jiffies(timeout * 1000);
10079 timeleft = wait_event_timeout(done_q,
10080 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
10081 timeout_req);
10082 spin_lock_irqsave(&phba->hbalock, iflags);
10083 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10086 * IOCB timed out. Inform the wake iocb wait
10087 * completion function and set local status
10090 iocb_completed = false;
10091 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10093 spin_unlock_irqrestore(&phba->hbalock, iflags);
10094 if (iocb_completed) {
10095 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10096 "0331 IOCB wake signaled\n");
10097 } else if (timeleft == 0) {
10098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10099 "0338 IOCB wait timeout error - no "
10100 "wake response Data x%x\n", timeout);
10101 retval = IOCB_TIMEDOUT;
10102 } else {
10103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10104 "0330 IOCB wake NOT set, "
10105 "Data x%x x%lx\n",
10106 timeout, (timeleft / jiffies));
10107 retval = IOCB_TIMEDOUT;
10109 } else if (retval == IOCB_BUSY) {
10110 if (phba->cfg_log_verbose & LOG_SLI) {
10111 list_for_each_entry(iocb, &pring->txq, list) {
10112 txq_cnt++;
10114 list_for_each_entry(iocb, &pring->txcmplq, list) {
10115 txcmplq_cnt++;
10117 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10118 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10119 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10121 return retval;
10122 } else {
10123 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10124 "0332 IOCB wait issue failed, Data x%x\n",
10125 retval);
10126 retval = IOCB_ERROR;
10129 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10130 if (lpfc_readl(phba->HCregaddr, &creg_val))
10131 return IOCB_ERROR;
10132 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10133 writel(creg_val, phba->HCregaddr);
10134 readl(phba->HCregaddr); /* flush */
10137 if (prspiocbq)
10138 piocb->context2 = NULL;
10140 piocb->context_un.wait_queue = NULL;
10141 piocb->iocb_cmpl = NULL;
10142 return retval;
10146 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10147 * @phba: Pointer to HBA context object.
10148 * @pmboxq: Pointer to driver mailbox object.
10149 * @timeout: Timeout in number of seconds.
10151 * This function issues the mailbox to firmware and waits for the
10152 * mailbox command to complete. If the mailbox command is not
10153 * completed within timeout seconds, it returns MBX_TIMEOUT.
10154 * The function waits for the mailbox completion using an
10155 * interruptible wait. If the thread is woken up due to a
10156 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10157 * should not free the mailbox resources, if this function returns
10158 * MBX_TIMEOUT.
10159 * This function will sleep while waiting for mailbox completion.
10160 * So, this function should not be called from any context which
10161 * does not allow sleeping. Due to the same reason, this function
10162 * cannot be called with interrupt disabled.
10163 * This function assumes that the mailbox completion occurs while
10164 * this function sleep. So, this function cannot be called from
10165 * the worker thread which processes mailbox completion.
10166 * This function is called in the context of HBA management
10167 * applications.
10168 * This function returns MBX_SUCCESS when successful.
10169 * This function is called with no lock held.
10172 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10173 uint32_t timeout)
10175 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10176 MAILBOX_t *mb = NULL;
10177 int retval;
10178 unsigned long flag;
10180 /* The caller might set context1 for extended buffer */
10181 if (pmboxq->context1)
10182 mb = (MAILBOX_t *)pmboxq->context1;
10184 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10185 /* setup wake call as IOCB callback */
10186 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10187 /* setup context field to pass wait_queue pointer to wake function */
10188 pmboxq->context1 = &done_q;
10190 /* now issue the command */
10191 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10192 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10193 wait_event_interruptible_timeout(done_q,
10194 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10195 msecs_to_jiffies(timeout * 1000));
10197 spin_lock_irqsave(&phba->hbalock, flag);
10198 /* restore the possible extended buffer for free resource */
10199 pmboxq->context1 = (uint8_t *)mb;
10201 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10202 * else do not free the resources.
10204 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10205 retval = MBX_SUCCESS;
10206 } else {
10207 retval = MBX_TIMEOUT;
10208 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10210 spin_unlock_irqrestore(&phba->hbalock, flag);
10211 } else {
10212 /* restore the possible extended buffer for free resource */
10213 pmboxq->context1 = (uint8_t *)mb;
10216 return retval;
10220 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10221 * @phba: Pointer to HBA context.
10223 * This function is called to shutdown the driver's mailbox sub-system.
10224 * It first marks the mailbox sub-system is in a block state to prevent
10225 * the asynchronous mailbox command from issued off the pending mailbox
10226 * command queue. If the mailbox command sub-system shutdown is due to
10227 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10228 * the mailbox sub-system flush routine to forcefully bring down the
10229 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10230 * as with offline or HBA function reset), this routine will wait for the
10231 * outstanding mailbox command to complete before invoking the mailbox
10232 * sub-system flush routine to gracefully bring down mailbox sub-system.
10234 void
10235 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10237 struct lpfc_sli *psli = &phba->sli;
10238 unsigned long timeout;
10240 if (mbx_action == LPFC_MBX_NO_WAIT) {
10241 /* delay 100ms for port state */
10242 msleep(100);
10243 lpfc_sli_mbox_sys_flush(phba);
10244 return;
10246 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10248 spin_lock_irq(&phba->hbalock);
10249 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10251 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10252 /* Determine how long we might wait for the active mailbox
10253 * command to be gracefully completed by firmware.
10255 if (phba->sli.mbox_active)
10256 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10257 phba->sli.mbox_active) *
10258 1000) + jiffies;
10259 spin_unlock_irq(&phba->hbalock);
10261 while (phba->sli.mbox_active) {
10262 /* Check active mailbox complete status every 2ms */
10263 msleep(2);
10264 if (time_after(jiffies, timeout))
10265 /* Timeout, let the mailbox flush routine to
10266 * forcefully release active mailbox command
10268 break;
10270 } else
10271 spin_unlock_irq(&phba->hbalock);
10273 lpfc_sli_mbox_sys_flush(phba);
10277 * lpfc_sli_eratt_read - read sli-3 error attention events
10278 * @phba: Pointer to HBA context.
10280 * This function is called to read the SLI3 device error attention registers
10281 * for possible error attention events. The caller must hold the hostlock
10282 * with spin_lock_irq().
10284 * This function returns 1 when there is Error Attention in the Host Attention
10285 * Register and returns 0 otherwise.
10287 static int
10288 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10290 uint32_t ha_copy;
10292 /* Read chip Host Attention (HA) register */
10293 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10294 goto unplug_err;
10296 if (ha_copy & HA_ERATT) {
10297 /* Read host status register to retrieve error event */
10298 if (lpfc_sli_read_hs(phba))
10299 goto unplug_err;
10301 /* Check if there is a deferred error condition is active */
10302 if ((HS_FFER1 & phba->work_hs) &&
10303 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10304 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10305 phba->hba_flag |= DEFER_ERATT;
10306 /* Clear all interrupt enable conditions */
10307 writel(0, phba->HCregaddr);
10308 readl(phba->HCregaddr);
10311 /* Set the driver HA work bitmap */
10312 phba->work_ha |= HA_ERATT;
10313 /* Indicate polling handles this ERATT */
10314 phba->hba_flag |= HBA_ERATT_HANDLED;
10315 return 1;
10317 return 0;
10319 unplug_err:
10320 /* Set the driver HS work bitmap */
10321 phba->work_hs |= UNPLUG_ERR;
10322 /* Set the driver HA work bitmap */
10323 phba->work_ha |= HA_ERATT;
10324 /* Indicate polling handles this ERATT */
10325 phba->hba_flag |= HBA_ERATT_HANDLED;
10326 return 1;
10330 * lpfc_sli4_eratt_read - read sli-4 error attention events
10331 * @phba: Pointer to HBA context.
10333 * This function is called to read the SLI4 device error attention registers
10334 * for possible error attention events. The caller must hold the hostlock
10335 * with spin_lock_irq().
10337 * This function returns 1 when there is Error Attention in the Host Attention
10338 * Register and returns 0 otherwise.
10340 static int
10341 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10343 uint32_t uerr_sta_hi, uerr_sta_lo;
10344 uint32_t if_type, portsmphr;
10345 struct lpfc_register portstat_reg;
10348 * For now, use the SLI4 device internal unrecoverable error
10349 * registers for error attention. This can be changed later.
10351 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10352 switch (if_type) {
10353 case LPFC_SLI_INTF_IF_TYPE_0:
10354 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10355 &uerr_sta_lo) ||
10356 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10357 &uerr_sta_hi)) {
10358 phba->work_hs |= UNPLUG_ERR;
10359 phba->work_ha |= HA_ERATT;
10360 phba->hba_flag |= HBA_ERATT_HANDLED;
10361 return 1;
10363 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10364 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10366 "1423 HBA Unrecoverable error: "
10367 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10368 "ue_mask_lo_reg=0x%x, "
10369 "ue_mask_hi_reg=0x%x\n",
10370 uerr_sta_lo, uerr_sta_hi,
10371 phba->sli4_hba.ue_mask_lo,
10372 phba->sli4_hba.ue_mask_hi);
10373 phba->work_status[0] = uerr_sta_lo;
10374 phba->work_status[1] = uerr_sta_hi;
10375 phba->work_ha |= HA_ERATT;
10376 phba->hba_flag |= HBA_ERATT_HANDLED;
10377 return 1;
10379 break;
10380 case LPFC_SLI_INTF_IF_TYPE_2:
10381 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10382 &portstat_reg.word0) ||
10383 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10384 &portsmphr)){
10385 phba->work_hs |= UNPLUG_ERR;
10386 phba->work_ha |= HA_ERATT;
10387 phba->hba_flag |= HBA_ERATT_HANDLED;
10388 return 1;
10390 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10391 phba->work_status[0] =
10392 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10393 phba->work_status[1] =
10394 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10396 "2885 Port Status Event: "
10397 "port status reg 0x%x, "
10398 "port smphr reg 0x%x, "
10399 "error 1=0x%x, error 2=0x%x\n",
10400 portstat_reg.word0,
10401 portsmphr,
10402 phba->work_status[0],
10403 phba->work_status[1]);
10404 phba->work_ha |= HA_ERATT;
10405 phba->hba_flag |= HBA_ERATT_HANDLED;
10406 return 1;
10408 break;
10409 case LPFC_SLI_INTF_IF_TYPE_1:
10410 default:
10411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10412 "2886 HBA Error Attention on unsupported "
10413 "if type %d.", if_type);
10414 return 1;
10417 return 0;
10421 * lpfc_sli_check_eratt - check error attention events
10422 * @phba: Pointer to HBA context.
10424 * This function is called from timer soft interrupt context to check HBA's
10425 * error attention register bit for error attention events.
10427 * This function returns 1 when there is Error Attention in the Host Attention
10428 * Register and returns 0 otherwise.
10431 lpfc_sli_check_eratt(struct lpfc_hba *phba)
10433 uint32_t ha_copy;
10435 /* If somebody is waiting to handle an eratt, don't process it
10436 * here. The brdkill function will do this.
10438 if (phba->link_flag & LS_IGNORE_ERATT)
10439 return 0;
10441 /* Check if interrupt handler handles this ERATT */
10442 spin_lock_irq(&phba->hbalock);
10443 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10444 /* Interrupt handler has handled ERATT */
10445 spin_unlock_irq(&phba->hbalock);
10446 return 0;
10450 * If there is deferred error attention, do not check for error
10451 * attention
10453 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10454 spin_unlock_irq(&phba->hbalock);
10455 return 0;
10458 /* If PCI channel is offline, don't process it */
10459 if (unlikely(pci_channel_offline(phba->pcidev))) {
10460 spin_unlock_irq(&phba->hbalock);
10461 return 0;
10464 switch (phba->sli_rev) {
10465 case LPFC_SLI_REV2:
10466 case LPFC_SLI_REV3:
10467 /* Read chip Host Attention (HA) register */
10468 ha_copy = lpfc_sli_eratt_read(phba);
10469 break;
10470 case LPFC_SLI_REV4:
10471 /* Read device Uncoverable Error (UERR) registers */
10472 ha_copy = lpfc_sli4_eratt_read(phba);
10473 break;
10474 default:
10475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10476 "0299 Invalid SLI revision (%d)\n",
10477 phba->sli_rev);
10478 ha_copy = 0;
10479 break;
10481 spin_unlock_irq(&phba->hbalock);
10483 return ha_copy;
10487 * lpfc_intr_state_check - Check device state for interrupt handling
10488 * @phba: Pointer to HBA context.
10490 * This inline routine checks whether a device or its PCI slot is in a state
10491 * that the interrupt should be handled.
10493 * This function returns 0 if the device or the PCI slot is in a state that
10494 * interrupt should be handled, otherwise -EIO.
10496 static inline int
10497 lpfc_intr_state_check(struct lpfc_hba *phba)
10499 /* If the pci channel is offline, ignore all the interrupts */
10500 if (unlikely(pci_channel_offline(phba->pcidev)))
10501 return -EIO;
10503 /* Update device level interrupt statistics */
10504 phba->sli.slistat.sli_intr++;
10506 /* Ignore all interrupts during initialization. */
10507 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10508 return -EIO;
10510 return 0;
10514 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
10515 * @irq: Interrupt number.
10516 * @dev_id: The device context pointer.
10518 * This function is directly called from the PCI layer as an interrupt
10519 * service routine when device with SLI-3 interface spec is enabled with
10520 * MSI-X multi-message interrupt mode and there are slow-path events in
10521 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10522 * interrupt mode, this function is called as part of the device-level
10523 * interrupt handler. When the PCI slot is in error recovery or the HBA
10524 * is undergoing initialization, the interrupt handler will not process
10525 * the interrupt. The link attention and ELS ring attention events are
10526 * handled by the worker thread. The interrupt handler signals the worker
10527 * thread and returns for these events. This function is called without
10528 * any lock held. It gets the hbalock to access and update SLI data
10529 * structures.
10531 * This function returns IRQ_HANDLED when interrupt is handled else it
10532 * returns IRQ_NONE.
10534 irqreturn_t
10535 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
10537 struct lpfc_hba *phba;
10538 uint32_t ha_copy, hc_copy;
10539 uint32_t work_ha_copy;
10540 unsigned long status;
10541 unsigned long iflag;
10542 uint32_t control;
10544 MAILBOX_t *mbox, *pmbox;
10545 struct lpfc_vport *vport;
10546 struct lpfc_nodelist *ndlp;
10547 struct lpfc_dmabuf *mp;
10548 LPFC_MBOXQ_t *pmb;
10549 int rc;
10552 * Get the driver's phba structure from the dev_id and
10553 * assume the HBA is not interrupting.
10555 phba = (struct lpfc_hba *)dev_id;
10557 if (unlikely(!phba))
10558 return IRQ_NONE;
10561 * Stuff needs to be attented to when this function is invoked as an
10562 * individual interrupt handler in MSI-X multi-message interrupt mode
10564 if (phba->intr_type == MSIX) {
10565 /* Check device state for handling interrupt */
10566 if (lpfc_intr_state_check(phba))
10567 return IRQ_NONE;
10568 /* Need to read HA REG for slow-path events */
10569 spin_lock_irqsave(&phba->hbalock, iflag);
10570 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10571 goto unplug_error;
10572 /* If somebody is waiting to handle an eratt don't process it
10573 * here. The brdkill function will do this.
10575 if (phba->link_flag & LS_IGNORE_ERATT)
10576 ha_copy &= ~HA_ERATT;
10577 /* Check the need for handling ERATT in interrupt handler */
10578 if (ha_copy & HA_ERATT) {
10579 if (phba->hba_flag & HBA_ERATT_HANDLED)
10580 /* ERATT polling has handled ERATT */
10581 ha_copy &= ~HA_ERATT;
10582 else
10583 /* Indicate interrupt handler handles ERATT */
10584 phba->hba_flag |= HBA_ERATT_HANDLED;
10588 * If there is deferred error attention, do not check for any
10589 * interrupt.
10591 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10592 spin_unlock_irqrestore(&phba->hbalock, iflag);
10593 return IRQ_NONE;
10596 /* Clear up only attention source related to slow-path */
10597 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10598 goto unplug_error;
10600 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10601 HC_LAINT_ENA | HC_ERINT_ENA),
10602 phba->HCregaddr);
10603 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10604 phba->HAregaddr);
10605 writel(hc_copy, phba->HCregaddr);
10606 readl(phba->HAregaddr); /* flush */
10607 spin_unlock_irqrestore(&phba->hbalock, iflag);
10608 } else
10609 ha_copy = phba->ha_copy;
10611 work_ha_copy = ha_copy & phba->work_ha_mask;
10613 if (work_ha_copy) {
10614 if (work_ha_copy & HA_LATT) {
10615 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10617 * Turn off Link Attention interrupts
10618 * until CLEAR_LA done
10620 spin_lock_irqsave(&phba->hbalock, iflag);
10621 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
10622 if (lpfc_readl(phba->HCregaddr, &control))
10623 goto unplug_error;
10624 control &= ~HC_LAINT_ENA;
10625 writel(control, phba->HCregaddr);
10626 readl(phba->HCregaddr); /* flush */
10627 spin_unlock_irqrestore(&phba->hbalock, iflag);
10629 else
10630 work_ha_copy &= ~HA_LATT;
10633 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
10635 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10636 * the only slow ring.
10638 status = (work_ha_copy &
10639 (HA_RXMASK << (4*LPFC_ELS_RING)));
10640 status >>= (4*LPFC_ELS_RING);
10641 if (status & HA_RXMASK) {
10642 spin_lock_irqsave(&phba->hbalock, iflag);
10643 if (lpfc_readl(phba->HCregaddr, &control))
10644 goto unplug_error;
10646 lpfc_debugfs_slow_ring_trc(phba,
10647 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10648 control, status,
10649 (uint32_t)phba->sli.slistat.sli_intr);
10651 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
10652 lpfc_debugfs_slow_ring_trc(phba,
10653 "ISR Disable ring:"
10654 "pwork:x%x hawork:x%x wait:x%x",
10655 phba->work_ha, work_ha_copy,
10656 (uint32_t)((unsigned long)
10657 &phba->work_waitq));
10659 control &=
10660 ~(HC_R0INT_ENA << LPFC_ELS_RING);
10661 writel(control, phba->HCregaddr);
10662 readl(phba->HCregaddr); /* flush */
10664 else {
10665 lpfc_debugfs_slow_ring_trc(phba,
10666 "ISR slow ring: pwork:"
10667 "x%x hawork:x%x wait:x%x",
10668 phba->work_ha, work_ha_copy,
10669 (uint32_t)((unsigned long)
10670 &phba->work_waitq));
10672 spin_unlock_irqrestore(&phba->hbalock, iflag);
10675 spin_lock_irqsave(&phba->hbalock, iflag);
10676 if (work_ha_copy & HA_ERATT) {
10677 if (lpfc_sli_read_hs(phba))
10678 goto unplug_error;
10680 * Check if there is a deferred error condition
10681 * is active
10683 if ((HS_FFER1 & phba->work_hs) &&
10684 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10685 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10686 phba->work_hs)) {
10687 phba->hba_flag |= DEFER_ERATT;
10688 /* Clear all interrupt enable conditions */
10689 writel(0, phba->HCregaddr);
10690 readl(phba->HCregaddr);
10694 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
10695 pmb = phba->sli.mbox_active;
10696 pmbox = &pmb->u.mb;
10697 mbox = phba->mbox;
10698 vport = pmb->vport;
10700 /* First check out the status word */
10701 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10702 if (pmbox->mbxOwner != OWN_HOST) {
10703 spin_unlock_irqrestore(&phba->hbalock, iflag);
10705 * Stray Mailbox Interrupt, mbxCommand <cmd>
10706 * mbxStatus <status>
10708 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10709 LOG_SLI,
10710 "(%d):0304 Stray Mailbox "
10711 "Interrupt mbxCommand x%x "
10712 "mbxStatus x%x\n",
10713 (vport ? vport->vpi : 0),
10714 pmbox->mbxCommand,
10715 pmbox->mbxStatus);
10716 /* clear mailbox attention bit */
10717 work_ha_copy &= ~HA_MBATT;
10718 } else {
10719 phba->sli.mbox_active = NULL;
10720 spin_unlock_irqrestore(&phba->hbalock, iflag);
10721 phba->last_completion_time = jiffies;
10722 del_timer(&phba->sli.mbox_tmo);
10723 if (pmb->mbox_cmpl) {
10724 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10725 MAILBOX_CMD_SIZE);
10726 if (pmb->out_ext_byte_len &&
10727 pmb->context2)
10728 lpfc_sli_pcimem_bcopy(
10729 phba->mbox_ext,
10730 pmb->context2,
10731 pmb->out_ext_byte_len);
10733 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10734 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10736 lpfc_debugfs_disc_trc(vport,
10737 LPFC_DISC_TRC_MBOX_VPORT,
10738 "MBOX dflt rpi: : "
10739 "status:x%x rpi:x%x",
10740 (uint32_t)pmbox->mbxStatus,
10741 pmbox->un.varWords[0], 0);
10743 if (!pmbox->mbxStatus) {
10744 mp = (struct lpfc_dmabuf *)
10745 (pmb->context1);
10746 ndlp = (struct lpfc_nodelist *)
10747 pmb->context2;
10749 /* Reg_LOGIN of dflt RPI was
10750 * successful. new lets get
10751 * rid of the RPI using the
10752 * same mbox buffer.
10754 lpfc_unreg_login(phba,
10755 vport->vpi,
10756 pmbox->un.varWords[0],
10757 pmb);
10758 pmb->mbox_cmpl =
10759 lpfc_mbx_cmpl_dflt_rpi;
10760 pmb->context1 = mp;
10761 pmb->context2 = ndlp;
10762 pmb->vport = vport;
10763 rc = lpfc_sli_issue_mbox(phba,
10764 pmb,
10765 MBX_NOWAIT);
10766 if (rc != MBX_BUSY)
10767 lpfc_printf_log(phba,
10768 KERN_ERR,
10769 LOG_MBOX | LOG_SLI,
10770 "0350 rc should have"
10771 "been MBX_BUSY\n");
10772 if (rc != MBX_NOT_FINISHED)
10773 goto send_current_mbox;
10776 spin_lock_irqsave(
10777 &phba->pport->work_port_lock,
10778 iflag);
10779 phba->pport->work_port_events &=
10780 ~WORKER_MBOX_TMO;
10781 spin_unlock_irqrestore(
10782 &phba->pport->work_port_lock,
10783 iflag);
10784 lpfc_mbox_cmpl_put(phba, pmb);
10786 } else
10787 spin_unlock_irqrestore(&phba->hbalock, iflag);
10789 if ((work_ha_copy & HA_MBATT) &&
10790 (phba->sli.mbox_active == NULL)) {
10791 send_current_mbox:
10792 /* Process next mailbox command if there is one */
10793 do {
10794 rc = lpfc_sli_issue_mbox(phba, NULL,
10795 MBX_NOWAIT);
10796 } while (rc == MBX_NOT_FINISHED);
10797 if (rc != MBX_SUCCESS)
10798 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10799 LOG_SLI, "0349 rc should be "
10800 "MBX_SUCCESS\n");
10803 spin_lock_irqsave(&phba->hbalock, iflag);
10804 phba->work_ha |= work_ha_copy;
10805 spin_unlock_irqrestore(&phba->hbalock, iflag);
10806 lpfc_worker_wake_up(phba);
10808 return IRQ_HANDLED;
10809 unplug_error:
10810 spin_unlock_irqrestore(&phba->hbalock, iflag);
10811 return IRQ_HANDLED;
10813 } /* lpfc_sli_sp_intr_handler */
10816 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
10817 * @irq: Interrupt number.
10818 * @dev_id: The device context pointer.
10820 * This function is directly called from the PCI layer as an interrupt
10821 * service routine when device with SLI-3 interface spec is enabled with
10822 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10823 * ring event in the HBA. However, when the device is enabled with either
10824 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10825 * device-level interrupt handler. When the PCI slot is in error recovery
10826 * or the HBA is undergoing initialization, the interrupt handler will not
10827 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10828 * the intrrupt context. This function is called without any lock held.
10829 * It gets the hbalock to access and update SLI data structures.
10831 * This function returns IRQ_HANDLED when interrupt is handled else it
10832 * returns IRQ_NONE.
10834 irqreturn_t
10835 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
10837 struct lpfc_hba *phba;
10838 uint32_t ha_copy;
10839 unsigned long status;
10840 unsigned long iflag;
10842 /* Get the driver's phba structure from the dev_id and
10843 * assume the HBA is not interrupting.
10845 phba = (struct lpfc_hba *) dev_id;
10847 if (unlikely(!phba))
10848 return IRQ_NONE;
10851 * Stuff needs to be attented to when this function is invoked as an
10852 * individual interrupt handler in MSI-X multi-message interrupt mode
10854 if (phba->intr_type == MSIX) {
10855 /* Check device state for handling interrupt */
10856 if (lpfc_intr_state_check(phba))
10857 return IRQ_NONE;
10858 /* Need to read HA REG for FCP ring and other ring events */
10859 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10860 return IRQ_HANDLED;
10861 /* Clear up only attention source related to fast-path */
10862 spin_lock_irqsave(&phba->hbalock, iflag);
10864 * If there is deferred error attention, do not check for
10865 * any interrupt.
10867 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10868 spin_unlock_irqrestore(&phba->hbalock, iflag);
10869 return IRQ_NONE;
10871 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10872 phba->HAregaddr);
10873 readl(phba->HAregaddr); /* flush */
10874 spin_unlock_irqrestore(&phba->hbalock, iflag);
10875 } else
10876 ha_copy = phba->ha_copy;
10879 * Process all events on FCP ring. Take the optimized path for FCP IO.
10881 ha_copy &= ~(phba->work_ha_mask);
10883 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10884 status >>= (4*LPFC_FCP_RING);
10885 if (status & HA_RXMASK)
10886 lpfc_sli_handle_fast_ring_event(phba,
10887 &phba->sli.ring[LPFC_FCP_RING],
10888 status);
10890 if (phba->cfg_multi_ring_support == 2) {
10892 * Process all events on extra ring. Take the optimized path
10893 * for extra ring IO.
10895 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10896 status >>= (4*LPFC_EXTRA_RING);
10897 if (status & HA_RXMASK) {
10898 lpfc_sli_handle_fast_ring_event(phba,
10899 &phba->sli.ring[LPFC_EXTRA_RING],
10900 status);
10903 return IRQ_HANDLED;
10904 } /* lpfc_sli_fp_intr_handler */
10907 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
10908 * @irq: Interrupt number.
10909 * @dev_id: The device context pointer.
10911 * This function is the HBA device-level interrupt handler to device with
10912 * SLI-3 interface spec, called from the PCI layer when either MSI or
10913 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10914 * requires driver attention. This function invokes the slow-path interrupt
10915 * attention handling function and fast-path interrupt attention handling
10916 * function in turn to process the relevant HBA attention events. This
10917 * function is called without any lock held. It gets the hbalock to access
10918 * and update SLI data structures.
10920 * This function returns IRQ_HANDLED when interrupt is handled, else it
10921 * returns IRQ_NONE.
10923 irqreturn_t
10924 lpfc_sli_intr_handler(int irq, void *dev_id)
10926 struct lpfc_hba *phba;
10927 irqreturn_t sp_irq_rc, fp_irq_rc;
10928 unsigned long status1, status2;
10929 uint32_t hc_copy;
10932 * Get the driver's phba structure from the dev_id and
10933 * assume the HBA is not interrupting.
10935 phba = (struct lpfc_hba *) dev_id;
10937 if (unlikely(!phba))
10938 return IRQ_NONE;
10940 /* Check device state for handling interrupt */
10941 if (lpfc_intr_state_check(phba))
10942 return IRQ_NONE;
10944 spin_lock(&phba->hbalock);
10945 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10946 spin_unlock(&phba->hbalock);
10947 return IRQ_HANDLED;
10950 if (unlikely(!phba->ha_copy)) {
10951 spin_unlock(&phba->hbalock);
10952 return IRQ_NONE;
10953 } else if (phba->ha_copy & HA_ERATT) {
10954 if (phba->hba_flag & HBA_ERATT_HANDLED)
10955 /* ERATT polling has handled ERATT */
10956 phba->ha_copy &= ~HA_ERATT;
10957 else
10958 /* Indicate interrupt handler handles ERATT */
10959 phba->hba_flag |= HBA_ERATT_HANDLED;
10963 * If there is deferred error attention, do not check for any interrupt.
10965 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10966 spin_unlock(&phba->hbalock);
10967 return IRQ_NONE;
10970 /* Clear attention sources except link and error attentions */
10971 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10972 spin_unlock(&phba->hbalock);
10973 return IRQ_HANDLED;
10975 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10976 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10977 phba->HCregaddr);
10978 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
10979 writel(hc_copy, phba->HCregaddr);
10980 readl(phba->HAregaddr); /* flush */
10981 spin_unlock(&phba->hbalock);
10984 * Invokes slow-path host attention interrupt handling as appropriate.
10987 /* status of events with mailbox and link attention */
10988 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10990 /* status of events with ELS ring */
10991 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10992 status2 >>= (4*LPFC_ELS_RING);
10994 if (status1 || (status2 & HA_RXMASK))
10995 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
10996 else
10997 sp_irq_rc = IRQ_NONE;
11000 * Invoke fast-path host attention interrupt handling as appropriate.
11003 /* status of events with FCP ring */
11004 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11005 status1 >>= (4*LPFC_FCP_RING);
11007 /* status of events with extra ring */
11008 if (phba->cfg_multi_ring_support == 2) {
11009 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11010 status2 >>= (4*LPFC_EXTRA_RING);
11011 } else
11012 status2 = 0;
11014 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
11015 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
11016 else
11017 fp_irq_rc = IRQ_NONE;
11019 /* Return device-level interrupt handling status */
11020 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
11021 } /* lpfc_sli_intr_handler */
11024 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11025 * @phba: pointer to lpfc hba data structure.
11027 * This routine is invoked by the worker thread to process all the pending
11028 * SLI4 FCP abort XRI events.
11030 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11032 struct lpfc_cq_event *cq_event;
11034 /* First, declare the fcp xri abort event has been handled */
11035 spin_lock_irq(&phba->hbalock);
11036 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11037 spin_unlock_irq(&phba->hbalock);
11038 /* Now, handle all the fcp xri abort events */
11039 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11040 /* Get the first event from the head of the event queue */
11041 spin_lock_irq(&phba->hbalock);
11042 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11043 cq_event, struct lpfc_cq_event, list);
11044 spin_unlock_irq(&phba->hbalock);
11045 /* Notify aborted XRI for FCP work queue */
11046 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11047 /* Free the event processed back to the free pool */
11048 lpfc_sli4_cq_event_release(phba, cq_event);
11053 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11054 * @phba: pointer to lpfc hba data structure.
11056 * This routine is invoked by the worker thread to process all the pending
11057 * SLI4 els abort xri events.
11059 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11061 struct lpfc_cq_event *cq_event;
11063 /* First, declare the els xri abort event has been handled */
11064 spin_lock_irq(&phba->hbalock);
11065 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11066 spin_unlock_irq(&phba->hbalock);
11067 /* Now, handle all the els xri abort events */
11068 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11069 /* Get the first event from the head of the event queue */
11070 spin_lock_irq(&phba->hbalock);
11071 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11072 cq_event, struct lpfc_cq_event, list);
11073 spin_unlock_irq(&phba->hbalock);
11074 /* Notify aborted XRI for ELS work queue */
11075 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11076 /* Free the event processed back to the free pool */
11077 lpfc_sli4_cq_event_release(phba, cq_event);
11082 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11083 * @phba: pointer to lpfc hba data structure
11084 * @pIocbIn: pointer to the rspiocbq
11085 * @pIocbOut: pointer to the cmdiocbq
11086 * @wcqe: pointer to the complete wcqe
11088 * This routine transfers the fields of a command iocbq to a response iocbq
11089 * by copying all the IOCB fields from command iocbq and transferring the
11090 * completion status information from the complete wcqe.
11092 static void
11093 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11094 struct lpfc_iocbq *pIocbIn,
11095 struct lpfc_iocbq *pIocbOut,
11096 struct lpfc_wcqe_complete *wcqe)
11098 unsigned long iflags;
11099 uint32_t status;
11100 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11102 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11103 sizeof(struct lpfc_iocbq) - offset);
11104 /* Map WCQE parameters into irspiocb parameters */
11105 status = bf_get(lpfc_wcqe_c_status, wcqe);
11106 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
11107 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11108 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11109 pIocbIn->iocb.un.fcpi.fcpi_parm =
11110 pIocbOut->iocb.un.fcpi.fcpi_parm -
11111 wcqe->total_data_placed;
11112 else
11113 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11114 else {
11115 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11116 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
11119 /* Convert BG errors for completion status */
11120 if (status == CQE_STATUS_DI_ERROR) {
11121 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11123 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11124 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11125 else
11126 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11128 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11129 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11130 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11131 BGS_GUARD_ERR_MASK;
11132 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11133 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11134 BGS_APPTAG_ERR_MASK;
11135 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11136 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11137 BGS_REFTAG_ERR_MASK;
11139 /* Check to see if there was any good data before the error */
11140 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11141 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11142 BGS_HI_WATER_MARK_PRESENT_MASK;
11143 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11144 wcqe->total_data_placed;
11148 * Set ALL the error bits to indicate we don't know what
11149 * type of error it is.
11151 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11152 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11153 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11154 BGS_GUARD_ERR_MASK);
11157 /* Pick up HBA exchange busy condition */
11158 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11159 spin_lock_irqsave(&phba->hbalock, iflags);
11160 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11161 spin_unlock_irqrestore(&phba->hbalock, iflags);
11166 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11167 * @phba: Pointer to HBA context object.
11168 * @wcqe: Pointer to work-queue completion queue entry.
11170 * This routine handles an ELS work-queue completion event and construct
11171 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11172 * discovery engine to handle.
11174 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11176 static struct lpfc_iocbq *
11177 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11178 struct lpfc_iocbq *irspiocbq)
11180 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11181 struct lpfc_iocbq *cmdiocbq;
11182 struct lpfc_wcqe_complete *wcqe;
11183 unsigned long iflags;
11185 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11186 spin_lock_irqsave(&pring->ring_lock, iflags);
11187 pring->stats.iocb_event++;
11188 /* Look up the ELS command IOCB and create pseudo response IOCB */
11189 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11190 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11191 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11193 if (unlikely(!cmdiocbq)) {
11194 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11195 "0386 ELS complete with no corresponding "
11196 "cmdiocb: iotag (%d)\n",
11197 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11198 lpfc_sli_release_iocbq(phba, irspiocbq);
11199 return NULL;
11202 /* Fake the irspiocbq and copy necessary response information */
11203 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11205 return irspiocbq;
11209 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11210 * @phba: Pointer to HBA context object.
11211 * @cqe: Pointer to mailbox completion queue entry.
11213 * This routine process a mailbox completion queue entry with asynchrous
11214 * event.
11216 * Return: true if work posted to worker thread, otherwise false.
11218 static bool
11219 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11221 struct lpfc_cq_event *cq_event;
11222 unsigned long iflags;
11224 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11225 "0392 Async Event: word0:x%x, word1:x%x, "
11226 "word2:x%x, word3:x%x\n", mcqe->word0,
11227 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11229 /* Allocate a new internal CQ_EVENT entry */
11230 cq_event = lpfc_sli4_cq_event_alloc(phba);
11231 if (!cq_event) {
11232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11233 "0394 Failed to allocate CQ_EVENT entry\n");
11234 return false;
11237 /* Move the CQE into an asynchronous event entry */
11238 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11239 spin_lock_irqsave(&phba->hbalock, iflags);
11240 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11241 /* Set the async event flag */
11242 phba->hba_flag |= ASYNC_EVENT;
11243 spin_unlock_irqrestore(&phba->hbalock, iflags);
11245 return true;
11249 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11250 * @phba: Pointer to HBA context object.
11251 * @cqe: Pointer to mailbox completion queue entry.
11253 * This routine process a mailbox completion queue entry with mailbox
11254 * completion event.
11256 * Return: true if work posted to worker thread, otherwise false.
11258 static bool
11259 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11261 uint32_t mcqe_status;
11262 MAILBOX_t *mbox, *pmbox;
11263 struct lpfc_mqe *mqe;
11264 struct lpfc_vport *vport;
11265 struct lpfc_nodelist *ndlp;
11266 struct lpfc_dmabuf *mp;
11267 unsigned long iflags;
11268 LPFC_MBOXQ_t *pmb;
11269 bool workposted = false;
11270 int rc;
11272 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11273 if (!bf_get(lpfc_trailer_completed, mcqe))
11274 goto out_no_mqe_complete;
11276 /* Get the reference to the active mbox command */
11277 spin_lock_irqsave(&phba->hbalock, iflags);
11278 pmb = phba->sli.mbox_active;
11279 if (unlikely(!pmb)) {
11280 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11281 "1832 No pending MBOX command to handle\n");
11282 spin_unlock_irqrestore(&phba->hbalock, iflags);
11283 goto out_no_mqe_complete;
11285 spin_unlock_irqrestore(&phba->hbalock, iflags);
11286 mqe = &pmb->u.mqe;
11287 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11288 mbox = phba->mbox;
11289 vport = pmb->vport;
11291 /* Reset heartbeat timer */
11292 phba->last_completion_time = jiffies;
11293 del_timer(&phba->sli.mbox_tmo);
11295 /* Move mbox data to caller's mailbox region, do endian swapping */
11296 if (pmb->mbox_cmpl && mbox)
11297 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11300 * For mcqe errors, conditionally move a modified error code to
11301 * the mbox so that the error will not be missed.
11303 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11304 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11305 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11306 bf_set(lpfc_mqe_status, mqe,
11307 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11309 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11310 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11311 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11312 "MBOX dflt rpi: status:x%x rpi:x%x",
11313 mcqe_status,
11314 pmbox->un.varWords[0], 0);
11315 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11316 mp = (struct lpfc_dmabuf *)(pmb->context1);
11317 ndlp = (struct lpfc_nodelist *)pmb->context2;
11318 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11319 * RID of the PPI using the same mbox buffer.
11321 lpfc_unreg_login(phba, vport->vpi,
11322 pmbox->un.varWords[0], pmb);
11323 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11324 pmb->context1 = mp;
11325 pmb->context2 = ndlp;
11326 pmb->vport = vport;
11327 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11328 if (rc != MBX_BUSY)
11329 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11330 LOG_SLI, "0385 rc should "
11331 "have been MBX_BUSY\n");
11332 if (rc != MBX_NOT_FINISHED)
11333 goto send_current_mbox;
11336 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11337 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11338 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11340 /* There is mailbox completion work to do */
11341 spin_lock_irqsave(&phba->hbalock, iflags);
11342 __lpfc_mbox_cmpl_put(phba, pmb);
11343 phba->work_ha |= HA_MBATT;
11344 spin_unlock_irqrestore(&phba->hbalock, iflags);
11345 workposted = true;
11347 send_current_mbox:
11348 spin_lock_irqsave(&phba->hbalock, iflags);
11349 /* Release the mailbox command posting token */
11350 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11351 /* Setting active mailbox pointer need to be in sync to flag clear */
11352 phba->sli.mbox_active = NULL;
11353 spin_unlock_irqrestore(&phba->hbalock, iflags);
11354 /* Wake up worker thread to post the next pending mailbox command */
11355 lpfc_worker_wake_up(phba);
11356 out_no_mqe_complete:
11357 if (bf_get(lpfc_trailer_consumed, mcqe))
11358 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11359 return workposted;
11363 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11364 * @phba: Pointer to HBA context object.
11365 * @cqe: Pointer to mailbox completion queue entry.
11367 * This routine process a mailbox completion queue entry, it invokes the
11368 * proper mailbox complete handling or asynchrous event handling routine
11369 * according to the MCQE's async bit.
11371 * Return: true if work posted to worker thread, otherwise false.
11373 static bool
11374 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11376 struct lpfc_mcqe mcqe;
11377 bool workposted;
11379 /* Copy the mailbox MCQE and convert endian order as needed */
11380 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11382 /* Invoke the proper event handling routine */
11383 if (!bf_get(lpfc_trailer_async, &mcqe))
11384 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11385 else
11386 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11387 return workposted;
11391 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11392 * @phba: Pointer to HBA context object.
11393 * @cq: Pointer to associated CQ
11394 * @wcqe: Pointer to work-queue completion queue entry.
11396 * This routine handles an ELS work-queue completion event.
11398 * Return: true if work posted to worker thread, otherwise false.
11400 static bool
11401 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11402 struct lpfc_wcqe_complete *wcqe)
11404 struct lpfc_iocbq *irspiocbq;
11405 unsigned long iflags;
11406 struct lpfc_sli_ring *pring = cq->pring;
11407 int txq_cnt = 0;
11408 int txcmplq_cnt = 0;
11409 int fcp_txcmplq_cnt = 0;
11411 /* Get an irspiocbq for later ELS response processing use */
11412 irspiocbq = lpfc_sli_get_iocbq(phba);
11413 if (!irspiocbq) {
11414 if (!list_empty(&pring->txq))
11415 txq_cnt++;
11416 if (!list_empty(&pring->txcmplq))
11417 txcmplq_cnt++;
11418 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11419 fcp_txcmplq_cnt++;
11420 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11421 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11422 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11423 txq_cnt, phba->iocb_cnt,
11424 fcp_txcmplq_cnt,
11425 txcmplq_cnt);
11426 return false;
11429 /* Save off the slow-path queue event for work thread to process */
11430 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
11431 spin_lock_irqsave(&phba->hbalock, iflags);
11432 list_add_tail(&irspiocbq->cq_event.list,
11433 &phba->sli4_hba.sp_queue_event);
11434 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11435 spin_unlock_irqrestore(&phba->hbalock, iflags);
11437 return true;
11441 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11442 * @phba: Pointer to HBA context object.
11443 * @wcqe: Pointer to work-queue completion queue entry.
11445 * This routine handles slow-path WQ entry comsumed event by invoking the
11446 * proper WQ release routine to the slow-path WQ.
11448 static void
11449 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11450 struct lpfc_wcqe_release *wcqe)
11452 /* sanity check on queue memory */
11453 if (unlikely(!phba->sli4_hba.els_wq))
11454 return;
11455 /* Check for the slow-path ELS work queue */
11456 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11457 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11458 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11459 else
11460 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11461 "2579 Slow-path wqe consume event carries "
11462 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11463 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11464 phba->sli4_hba.els_wq->queue_id);
11468 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11469 * @phba: Pointer to HBA context object.
11470 * @cq: Pointer to a WQ completion queue.
11471 * @wcqe: Pointer to work-queue completion queue entry.
11473 * This routine handles an XRI abort event.
11475 * Return: true if work posted to worker thread, otherwise false.
11477 static bool
11478 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11479 struct lpfc_queue *cq,
11480 struct sli4_wcqe_xri_aborted *wcqe)
11482 bool workposted = false;
11483 struct lpfc_cq_event *cq_event;
11484 unsigned long iflags;
11486 /* Allocate a new internal CQ_EVENT entry */
11487 cq_event = lpfc_sli4_cq_event_alloc(phba);
11488 if (!cq_event) {
11489 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11490 "0602 Failed to allocate CQ_EVENT entry\n");
11491 return false;
11494 /* Move the CQE into the proper xri abort event list */
11495 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11496 switch (cq->subtype) {
11497 case LPFC_FCP:
11498 spin_lock_irqsave(&phba->hbalock, iflags);
11499 list_add_tail(&cq_event->list,
11500 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11501 /* Set the fcp xri abort event flag */
11502 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11503 spin_unlock_irqrestore(&phba->hbalock, iflags);
11504 workposted = true;
11505 break;
11506 case LPFC_ELS:
11507 spin_lock_irqsave(&phba->hbalock, iflags);
11508 list_add_tail(&cq_event->list,
11509 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11510 /* Set the els xri abort event flag */
11511 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11512 spin_unlock_irqrestore(&phba->hbalock, iflags);
11513 workposted = true;
11514 break;
11515 default:
11516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11517 "0603 Invalid work queue CQE subtype (x%x)\n",
11518 cq->subtype);
11519 workposted = false;
11520 break;
11522 return workposted;
11526 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11527 * @phba: Pointer to HBA context object.
11528 * @rcqe: Pointer to receive-queue completion queue entry.
11530 * This routine process a receive-queue completion queue entry.
11532 * Return: true if work posted to worker thread, otherwise false.
11534 static bool
11535 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11537 bool workposted = false;
11538 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11539 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11540 struct hbq_dmabuf *dma_buf;
11541 uint32_t status, rq_id;
11542 unsigned long iflags;
11544 /* sanity check on queue memory */
11545 if (unlikely(!hrq) || unlikely(!drq))
11546 return workposted;
11548 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11549 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11550 else
11551 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11552 if (rq_id != hrq->queue_id)
11553 goto out;
11555 status = bf_get(lpfc_rcqe_status, rcqe);
11556 switch (status) {
11557 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11559 "2537 Receive Frame Truncated!!\n");
11560 hrq->RQ_buf_trunc++;
11561 case FC_STATUS_RQ_SUCCESS:
11562 lpfc_sli4_rq_release(hrq, drq);
11563 spin_lock_irqsave(&phba->hbalock, iflags);
11564 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11565 if (!dma_buf) {
11566 hrq->RQ_no_buf_found++;
11567 spin_unlock_irqrestore(&phba->hbalock, iflags);
11568 goto out;
11570 hrq->RQ_rcv_buf++;
11571 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11572 /* save off the frame for the word thread to process */
11573 list_add_tail(&dma_buf->cq_event.list,
11574 &phba->sli4_hba.sp_queue_event);
11575 /* Frame received */
11576 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11577 spin_unlock_irqrestore(&phba->hbalock, iflags);
11578 workposted = true;
11579 break;
11580 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11581 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11582 hrq->RQ_no_posted_buf++;
11583 /* Post more buffers if possible */
11584 spin_lock_irqsave(&phba->hbalock, iflags);
11585 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11586 spin_unlock_irqrestore(&phba->hbalock, iflags);
11587 workposted = true;
11588 break;
11590 out:
11591 return workposted;
11595 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11596 * @phba: Pointer to HBA context object.
11597 * @cq: Pointer to the completion queue.
11598 * @wcqe: Pointer to a completion queue entry.
11600 * This routine process a slow-path work-queue or receive queue completion queue
11601 * entry.
11603 * Return: true if work posted to worker thread, otherwise false.
11605 static bool
11606 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11607 struct lpfc_cqe *cqe)
11609 struct lpfc_cqe cqevt;
11610 bool workposted = false;
11612 /* Copy the work queue CQE and convert endian order if needed */
11613 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
11615 /* Check and process for different type of WCQE and dispatch */
11616 switch (bf_get(lpfc_cqe_code, &cqevt)) {
11617 case CQE_CODE_COMPL_WQE:
11618 /* Process the WQ/RQ complete event */
11619 phba->last_completion_time = jiffies;
11620 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
11621 (struct lpfc_wcqe_complete *)&cqevt);
11622 break;
11623 case CQE_CODE_RELEASE_WQE:
11624 /* Process the WQ release event */
11625 lpfc_sli4_sp_handle_rel_wcqe(phba,
11626 (struct lpfc_wcqe_release *)&cqevt);
11627 break;
11628 case CQE_CODE_XRI_ABORTED:
11629 /* Process the WQ XRI abort event */
11630 phba->last_completion_time = jiffies;
11631 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11632 (struct sli4_wcqe_xri_aborted *)&cqevt);
11633 break;
11634 case CQE_CODE_RECEIVE:
11635 case CQE_CODE_RECEIVE_V1:
11636 /* Process the RQ event */
11637 phba->last_completion_time = jiffies;
11638 workposted = lpfc_sli4_sp_handle_rcqe(phba,
11639 (struct lpfc_rcqe *)&cqevt);
11640 break;
11641 default:
11642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11643 "0388 Not a valid WCQE code: x%x\n",
11644 bf_get(lpfc_cqe_code, &cqevt));
11645 break;
11647 return workposted;
11651 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11652 * @phba: Pointer to HBA context object.
11653 * @eqe: Pointer to fast-path event queue entry.
11655 * This routine process a event queue entry from the slow-path event queue.
11656 * It will check the MajorCode and MinorCode to determine this is for a
11657 * completion event on a completion queue, if not, an error shall be logged
11658 * and just return. Otherwise, it will get to the corresponding completion
11659 * queue and process all the entries on that completion queue, rearm the
11660 * completion queue, and then return.
11663 static void
11664 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11665 struct lpfc_queue *speq)
11667 struct lpfc_queue *cq = NULL, *childq;
11668 struct lpfc_cqe *cqe;
11669 bool workposted = false;
11670 int ecount = 0;
11671 uint16_t cqid;
11673 /* Get the reference to the corresponding CQ */
11674 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11676 list_for_each_entry(childq, &speq->child_list, list) {
11677 if (childq->queue_id == cqid) {
11678 cq = childq;
11679 break;
11682 if (unlikely(!cq)) {
11683 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11685 "0365 Slow-path CQ identifier "
11686 "(%d) does not exist\n", cqid);
11687 return;
11690 /* Process all the entries to the CQ */
11691 switch (cq->type) {
11692 case LPFC_MCQ:
11693 while ((cqe = lpfc_sli4_cq_get(cq))) {
11694 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
11695 if (!(++ecount % cq->entry_repost))
11696 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11697 cq->CQ_mbox++;
11699 break;
11700 case LPFC_WCQ:
11701 while ((cqe = lpfc_sli4_cq_get(cq))) {
11702 if (cq->subtype == LPFC_FCP)
11703 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11704 cqe);
11705 else
11706 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11707 cqe);
11708 if (!(++ecount % cq->entry_repost))
11709 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11712 /* Track the max number of CQEs processed in 1 EQ */
11713 if (ecount > cq->CQ_max_cqe)
11714 cq->CQ_max_cqe = ecount;
11715 break;
11716 default:
11717 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11718 "0370 Invalid completion queue type (%d)\n",
11719 cq->type);
11720 return;
11723 /* Catch the no cq entry condition, log an error */
11724 if (unlikely(ecount == 0))
11725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11726 "0371 No entry from the CQ: identifier "
11727 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11729 /* In any case, flash and re-arm the RCQ */
11730 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11732 /* wake up worker thread if there are works to be done */
11733 if (workposted)
11734 lpfc_worker_wake_up(phba);
11738 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11739 * @phba: Pointer to HBA context object.
11740 * @cq: Pointer to associated CQ
11741 * @wcqe: Pointer to work-queue completion queue entry.
11743 * This routine process a fast-path work queue completion entry from fast-path
11744 * event queue for FCP command response completion.
11746 static void
11747 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11748 struct lpfc_wcqe_complete *wcqe)
11750 struct lpfc_sli_ring *pring = cq->pring;
11751 struct lpfc_iocbq *cmdiocbq;
11752 struct lpfc_iocbq irspiocbq;
11753 unsigned long iflags;
11755 /* Check for response status */
11756 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11757 /* If resource errors reported from HBA, reduce queue
11758 * depth of the SCSI device.
11760 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11761 IOSTAT_LOCAL_REJECT)) &&
11762 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11763 IOERR_NO_RESOURCES))
11764 phba->lpfc_rampdown_queue_depth(phba);
11766 /* Log the error status */
11767 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11768 "0373 FCP complete error: status=x%x, "
11769 "hw_status=x%x, total_data_specified=%d, "
11770 "parameter=x%x, word3=x%x\n",
11771 bf_get(lpfc_wcqe_c_status, wcqe),
11772 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11773 wcqe->total_data_placed, wcqe->parameter,
11774 wcqe->word3);
11777 /* Look up the FCP command IOCB and create pseudo response IOCB */
11778 spin_lock_irqsave(&pring->ring_lock, iflags);
11779 pring->stats.iocb_event++;
11780 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11781 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11782 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11783 if (unlikely(!cmdiocbq)) {
11784 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11785 "0374 FCP complete with no corresponding "
11786 "cmdiocb: iotag (%d)\n",
11787 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11788 return;
11790 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11791 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11792 "0375 FCP cmdiocb not callback function "
11793 "iotag: (%d)\n",
11794 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11795 return;
11798 /* Fake the irspiocb and copy necessary response information */
11799 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
11801 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11802 spin_lock_irqsave(&phba->hbalock, iflags);
11803 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11804 spin_unlock_irqrestore(&phba->hbalock, iflags);
11807 /* Pass the cmd_iocb and the rsp state to the upper layer */
11808 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11812 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11813 * @phba: Pointer to HBA context object.
11814 * @cq: Pointer to completion queue.
11815 * @wcqe: Pointer to work-queue completion queue entry.
11817 * This routine handles an fast-path WQ entry comsumed event by invoking the
11818 * proper WQ release routine to the slow-path WQ.
11820 static void
11821 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11822 struct lpfc_wcqe_release *wcqe)
11824 struct lpfc_queue *childwq;
11825 bool wqid_matched = false;
11826 uint16_t fcp_wqid;
11828 /* Check for fast-path FCP work queue release */
11829 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11830 list_for_each_entry(childwq, &cq->child_list, list) {
11831 if (childwq->queue_id == fcp_wqid) {
11832 lpfc_sli4_wq_release(childwq,
11833 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11834 wqid_matched = true;
11835 break;
11838 /* Report warning log message if no match found */
11839 if (wqid_matched != true)
11840 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11841 "2580 Fast-path wqe consume event carries "
11842 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11846 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11847 * @cq: Pointer to the completion queue.
11848 * @eqe: Pointer to fast-path completion queue entry.
11850 * This routine process a fast-path work queue completion entry from fast-path
11851 * event queue for FCP command response completion.
11853 static int
11854 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11855 struct lpfc_cqe *cqe)
11857 struct lpfc_wcqe_release wcqe;
11858 bool workposted = false;
11860 /* Copy the work queue CQE and convert endian order if needed */
11861 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11863 /* Check and process for different type of WCQE and dispatch */
11864 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11865 case CQE_CODE_COMPL_WQE:
11866 cq->CQ_wq++;
11867 /* Process the WQ complete event */
11868 phba->last_completion_time = jiffies;
11869 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
11870 (struct lpfc_wcqe_complete *)&wcqe);
11871 break;
11872 case CQE_CODE_RELEASE_WQE:
11873 cq->CQ_release_wqe++;
11874 /* Process the WQ release event */
11875 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11876 (struct lpfc_wcqe_release *)&wcqe);
11877 break;
11878 case CQE_CODE_XRI_ABORTED:
11879 cq->CQ_xri_aborted++;
11880 /* Process the WQ XRI abort event */
11881 phba->last_completion_time = jiffies;
11882 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11883 (struct sli4_wcqe_xri_aborted *)&wcqe);
11884 break;
11885 default:
11886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11887 "0144 Not a valid WCQE code: x%x\n",
11888 bf_get(lpfc_wcqe_c_code, &wcqe));
11889 break;
11891 return workposted;
11895 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
11896 * @phba: Pointer to HBA context object.
11897 * @eqe: Pointer to fast-path event queue entry.
11899 * This routine process a event queue entry from the fast-path event queue.
11900 * It will check the MajorCode and MinorCode to determine this is for a
11901 * completion event on a completion queue, if not, an error shall be logged
11902 * and just return. Otherwise, it will get to the corresponding completion
11903 * queue and process all the entries on the completion queue, rearm the
11904 * completion queue, and then return.
11906 static void
11907 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11908 uint32_t qidx)
11910 struct lpfc_queue *cq;
11911 struct lpfc_cqe *cqe;
11912 bool workposted = false;
11913 uint16_t cqid;
11914 int ecount = 0;
11916 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
11917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11918 "0366 Not a valid completion "
11919 "event: majorcode=x%x, minorcode=x%x\n",
11920 bf_get_le32(lpfc_eqe_major_code, eqe),
11921 bf_get_le32(lpfc_eqe_minor_code, eqe));
11922 return;
11925 /* Get the reference to the corresponding CQ */
11926 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11928 /* Check if this is a Slow path event */
11929 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11930 lpfc_sli4_sp_handle_eqe(phba, eqe,
11931 phba->sli4_hba.hba_eq[qidx]);
11932 return;
11935 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11936 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11937 "3146 Fast-path completion queues "
11938 "does not exist\n");
11939 return;
11941 cq = phba->sli4_hba.fcp_cq[qidx];
11942 if (unlikely(!cq)) {
11943 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11945 "0367 Fast-path completion queue "
11946 "(%d) does not exist\n", qidx);
11947 return;
11950 if (unlikely(cqid != cq->queue_id)) {
11951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11952 "0368 Miss-matched fast-path completion "
11953 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11954 cqid, cq->queue_id);
11955 return;
11958 /* Process all the entries to the CQ */
11959 while ((cqe = lpfc_sli4_cq_get(cq))) {
11960 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
11961 if (!(++ecount % cq->entry_repost))
11962 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11965 /* Track the max number of CQEs processed in 1 EQ */
11966 if (ecount > cq->CQ_max_cqe)
11967 cq->CQ_max_cqe = ecount;
11969 /* Catch the no cq entry condition */
11970 if (unlikely(ecount == 0))
11971 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11972 "0369 No entry from fast-path completion "
11973 "queue fcpcqid=%d\n", cq->queue_id);
11975 /* In any case, flash and re-arm the CQ */
11976 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11978 /* wake up worker thread if there are works to be done */
11979 if (workposted)
11980 lpfc_worker_wake_up(phba);
11983 static void
11984 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11986 struct lpfc_eqe *eqe;
11988 /* walk all the EQ entries and drop on the floor */
11989 while ((eqe = lpfc_sli4_eq_get(eq)))
11992 /* Clear and re-arm the EQ */
11993 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11997 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
11998 * @irq: Interrupt number.
11999 * @dev_id: The device context pointer.
12001 * This function is directly called from the PCI layer as an interrupt
12002 * service routine when device with SLI-4 interface spec is enabled with
12003 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12004 * ring event in the HBA. However, when the device is enabled with either
12005 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12006 * device-level interrupt handler. When the PCI slot is in error recovery
12007 * or the HBA is undergoing initialization, the interrupt handler will not
12008 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12009 * the intrrupt context. This function is called without any lock held.
12010 * It gets the hbalock to access and update SLI data structures. Note that,
12011 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12012 * equal to that of FCP CQ index.
12014 * The link attention and ELS ring attention events are handled
12015 * by the worker thread. The interrupt handler signals the worker thread
12016 * and returns for these events. This function is called without any lock
12017 * held. It gets the hbalock to access and update SLI data structures.
12019 * This function returns IRQ_HANDLED when interrupt is handled else it
12020 * returns IRQ_NONE.
12022 irqreturn_t
12023 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
12025 struct lpfc_hba *phba;
12026 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12027 struct lpfc_queue *fpeq;
12028 struct lpfc_eqe *eqe;
12029 unsigned long iflag;
12030 int ecount = 0;
12031 int fcp_eqidx;
12033 /* Get the driver's phba structure from the dev_id */
12034 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12035 phba = fcp_eq_hdl->phba;
12036 fcp_eqidx = fcp_eq_hdl->idx;
12038 if (unlikely(!phba))
12039 return IRQ_NONE;
12040 if (unlikely(!phba->sli4_hba.hba_eq))
12041 return IRQ_NONE;
12043 /* Get to the EQ struct associated with this vector */
12044 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
12045 if (unlikely(!fpeq))
12046 return IRQ_NONE;
12048 if (lpfc_fcp_look_ahead) {
12049 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12050 lpfc_sli4_eq_clr_intr(fpeq);
12051 else {
12052 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12053 return IRQ_NONE;
12057 /* Check device state for handling interrupt */
12058 if (unlikely(lpfc_intr_state_check(phba))) {
12059 fpeq->EQ_badstate++;
12060 /* Check again for link_state with lock held */
12061 spin_lock_irqsave(&phba->hbalock, iflag);
12062 if (phba->link_state < LPFC_LINK_DOWN)
12063 /* Flush, clear interrupt, and rearm the EQ */
12064 lpfc_sli4_eq_flush(phba, fpeq);
12065 spin_unlock_irqrestore(&phba->hbalock, iflag);
12066 if (lpfc_fcp_look_ahead)
12067 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12068 return IRQ_NONE;
12072 * Process all the event on FCP fast-path EQ
12074 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
12075 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
12076 if (!(++ecount % fpeq->entry_repost))
12077 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
12078 fpeq->EQ_processed++;
12081 /* Track the max number of EQEs processed in 1 intr */
12082 if (ecount > fpeq->EQ_max_eqe)
12083 fpeq->EQ_max_eqe = ecount;
12085 /* Always clear and re-arm the fast-path EQ */
12086 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12088 if (unlikely(ecount == 0)) {
12089 fpeq->EQ_no_entry++;
12091 if (lpfc_fcp_look_ahead) {
12092 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12093 return IRQ_NONE;
12096 if (phba->intr_type == MSIX)
12097 /* MSI-X treated interrupt served as no EQ share INT */
12098 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12099 "0358 MSI-X interrupt with no EQE\n");
12100 else
12101 /* Non MSI-X treated on interrupt as EQ share INT */
12102 return IRQ_NONE;
12105 if (lpfc_fcp_look_ahead)
12106 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12107 return IRQ_HANDLED;
12108 } /* lpfc_sli4_fp_intr_handler */
12111 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12112 * @irq: Interrupt number.
12113 * @dev_id: The device context pointer.
12115 * This function is the device-level interrupt handler to device with SLI-4
12116 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12117 * interrupt mode is enabled and there is an event in the HBA which requires
12118 * driver attention. This function invokes the slow-path interrupt attention
12119 * handling function and fast-path interrupt attention handling function in
12120 * turn to process the relevant HBA attention events. This function is called
12121 * without any lock held. It gets the hbalock to access and update SLI data
12122 * structures.
12124 * This function returns IRQ_HANDLED when interrupt is handled, else it
12125 * returns IRQ_NONE.
12127 irqreturn_t
12128 lpfc_sli4_intr_handler(int irq, void *dev_id)
12130 struct lpfc_hba *phba;
12131 irqreturn_t hba_irq_rc;
12132 bool hba_handled = false;
12133 int fcp_eqidx;
12135 /* Get the driver's phba structure from the dev_id */
12136 phba = (struct lpfc_hba *)dev_id;
12138 if (unlikely(!phba))
12139 return IRQ_NONE;
12142 * Invoke fast-path host attention interrupt handling as appropriate.
12144 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12145 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12146 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12147 if (hba_irq_rc == IRQ_HANDLED)
12148 hba_handled |= true;
12151 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12152 } /* lpfc_sli4_intr_handler */
12155 * lpfc_sli4_queue_free - free a queue structure and associated memory
12156 * @queue: The queue structure to free.
12158 * This function frees a queue structure and the DMAable memory used for
12159 * the host resident queue. This function must be called after destroying the
12160 * queue on the HBA.
12162 void
12163 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12165 struct lpfc_dmabuf *dmabuf;
12167 if (!queue)
12168 return;
12170 while (!list_empty(&queue->page_list)) {
12171 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12172 list);
12173 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12174 dmabuf->virt, dmabuf->phys);
12175 kfree(dmabuf);
12177 kfree(queue);
12178 return;
12182 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12183 * @phba: The HBA that this queue is being created on.
12184 * @entry_size: The size of each queue entry for this queue.
12185 * @entry count: The number of entries that this queue will handle.
12187 * This function allocates a queue structure and the DMAable memory used for
12188 * the host resident queue. This function must be called before creating the
12189 * queue on the HBA.
12191 struct lpfc_queue *
12192 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12193 uint32_t entry_count)
12195 struct lpfc_queue *queue;
12196 struct lpfc_dmabuf *dmabuf;
12197 int x, total_qe_count;
12198 void *dma_pointer;
12199 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12201 if (!phba->sli4_hba.pc_sli4_params.supported)
12202 hw_page_size = SLI4_PAGE_SIZE;
12204 queue = kzalloc(sizeof(struct lpfc_queue) +
12205 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12206 if (!queue)
12207 return NULL;
12208 queue->page_count = (ALIGN(entry_size * entry_count,
12209 hw_page_size))/hw_page_size;
12210 INIT_LIST_HEAD(&queue->list);
12211 INIT_LIST_HEAD(&queue->page_list);
12212 INIT_LIST_HEAD(&queue->child_list);
12213 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12214 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12215 if (!dmabuf)
12216 goto out_fail;
12217 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12218 hw_page_size, &dmabuf->phys,
12219 GFP_KERNEL);
12220 if (!dmabuf->virt) {
12221 kfree(dmabuf);
12222 goto out_fail;
12224 memset(dmabuf->virt, 0, hw_page_size);
12225 dmabuf->buffer_tag = x;
12226 list_add_tail(&dmabuf->list, &queue->page_list);
12227 /* initialize queue's entry array */
12228 dma_pointer = dmabuf->virt;
12229 for (; total_qe_count < entry_count &&
12230 dma_pointer < (hw_page_size + dmabuf->virt);
12231 total_qe_count++, dma_pointer += entry_size) {
12232 queue->qe[total_qe_count].address = dma_pointer;
12235 queue->entry_size = entry_size;
12236 queue->entry_count = entry_count;
12239 * entry_repost is calculated based on the number of entries in the
12240 * queue. This works out except for RQs. If buffers are NOT initially
12241 * posted for every RQE, entry_repost should be adjusted accordingly.
12243 queue->entry_repost = (entry_count >> 3);
12244 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12245 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
12246 queue->phba = phba;
12248 return queue;
12249 out_fail:
12250 lpfc_sli4_queue_free(queue);
12251 return NULL;
12255 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12256 * @phba: HBA structure that indicates port to create a queue on.
12257 * @pci_barset: PCI BAR set flag.
12259 * This function shall perform iomap of the specified PCI BAR address to host
12260 * memory address if not already done so and return it. The returned host
12261 * memory address can be NULL.
12263 static void __iomem *
12264 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12266 struct pci_dev *pdev;
12268 if (!phba->pcidev)
12269 return NULL;
12270 else
12271 pdev = phba->pcidev;
12273 switch (pci_barset) {
12274 case WQ_PCI_BAR_0_AND_1:
12275 return phba->pci_bar0_memmap_p;
12276 case WQ_PCI_BAR_2_AND_3:
12277 return phba->pci_bar2_memmap_p;
12278 case WQ_PCI_BAR_4_AND_5:
12279 return phba->pci_bar4_memmap_p;
12280 default:
12281 break;
12283 return NULL;
12287 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12288 * @phba: HBA structure that indicates port to create a queue on.
12289 * @startq: The starting FCP EQ to modify
12291 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12293 * The @phba struct is used to send mailbox command to HBA. The @startq
12294 * is used to get the starting FCP EQ to change.
12295 * This function is asynchronous and will wait for the mailbox
12296 * command to finish before continuing.
12298 * On success this function will return a zero. If unable to allocate enough
12299 * memory this function will return -ENOMEM. If the queue create mailbox command
12300 * fails this function will return -ENXIO.
12302 uint32_t
12303 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12305 struct lpfc_mbx_modify_eq_delay *eq_delay;
12306 LPFC_MBOXQ_t *mbox;
12307 struct lpfc_queue *eq;
12308 int cnt, rc, length, status = 0;
12309 uint32_t shdr_status, shdr_add_status;
12310 uint32_t result;
12311 int fcp_eqidx;
12312 union lpfc_sli4_cfg_shdr *shdr;
12313 uint16_t dmult;
12315 if (startq >= phba->cfg_fcp_io_channel)
12316 return 0;
12318 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12319 if (!mbox)
12320 return -ENOMEM;
12321 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12322 sizeof(struct lpfc_sli4_cfg_mhdr));
12323 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12324 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12325 length, LPFC_SLI4_MBX_EMBED);
12326 eq_delay = &mbox->u.mqe.un.eq_delay;
12328 /* Calculate delay multiper from maximum interrupt per second */
12329 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12330 if (result > LPFC_DMULT_CONST)
12331 dmult = 0;
12332 else
12333 dmult = LPFC_DMULT_CONST/result - 1;
12335 cnt = 0;
12336 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
12337 fcp_eqidx++) {
12338 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
12339 if (!eq)
12340 continue;
12341 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12342 eq_delay->u.request.eq[cnt].phase = 0;
12343 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12344 cnt++;
12345 if (cnt >= LPFC_MAX_EQ_DELAY)
12346 break;
12348 eq_delay->u.request.num_eq = cnt;
12350 mbox->vport = phba->pport;
12351 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12352 mbox->context1 = NULL;
12353 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12354 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12355 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12356 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12357 if (shdr_status || shdr_add_status || rc) {
12358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12359 "2512 MODIFY_EQ_DELAY mailbox failed with "
12360 "status x%x add_status x%x, mbx status x%x\n",
12361 shdr_status, shdr_add_status, rc);
12362 status = -ENXIO;
12364 mempool_free(mbox, phba->mbox_mem_pool);
12365 return status;
12369 * lpfc_eq_create - Create an Event Queue on the HBA
12370 * @phba: HBA structure that indicates port to create a queue on.
12371 * @eq: The queue structure to use to create the event queue.
12372 * @imax: The maximum interrupt per second limit.
12374 * This function creates an event queue, as detailed in @eq, on a port,
12375 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12377 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12378 * is used to get the entry count and entry size that are necessary to
12379 * determine the number of pages to allocate and use for this queue. This
12380 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12381 * event queue. This function is asynchronous and will wait for the mailbox
12382 * command to finish before continuing.
12384 * On success this function will return a zero. If unable to allocate enough
12385 * memory this function will return -ENOMEM. If the queue create mailbox command
12386 * fails this function will return -ENXIO.
12388 uint32_t
12389 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
12391 struct lpfc_mbx_eq_create *eq_create;
12392 LPFC_MBOXQ_t *mbox;
12393 int rc, length, status = 0;
12394 struct lpfc_dmabuf *dmabuf;
12395 uint32_t shdr_status, shdr_add_status;
12396 union lpfc_sli4_cfg_shdr *shdr;
12397 uint16_t dmult;
12398 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12400 /* sanity check on queue memory */
12401 if (!eq)
12402 return -ENODEV;
12403 if (!phba->sli4_hba.pc_sli4_params.supported)
12404 hw_page_size = SLI4_PAGE_SIZE;
12406 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12407 if (!mbox)
12408 return -ENOMEM;
12409 length = (sizeof(struct lpfc_mbx_eq_create) -
12410 sizeof(struct lpfc_sli4_cfg_mhdr));
12411 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12412 LPFC_MBOX_OPCODE_EQ_CREATE,
12413 length, LPFC_SLI4_MBX_EMBED);
12414 eq_create = &mbox->u.mqe.un.eq_create;
12415 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12416 eq->page_count);
12417 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12418 LPFC_EQE_SIZE);
12419 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12420 /* Calculate delay multiper from maximum interrupt per second */
12421 if (imax > LPFC_DMULT_CONST)
12422 dmult = 0;
12423 else
12424 dmult = LPFC_DMULT_CONST/imax - 1;
12425 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12426 dmult);
12427 switch (eq->entry_count) {
12428 default:
12429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12430 "0360 Unsupported EQ count. (%d)\n",
12431 eq->entry_count);
12432 if (eq->entry_count < 256)
12433 return -EINVAL;
12434 /* otherwise default to smallest count (drop through) */
12435 case 256:
12436 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12437 LPFC_EQ_CNT_256);
12438 break;
12439 case 512:
12440 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12441 LPFC_EQ_CNT_512);
12442 break;
12443 case 1024:
12444 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12445 LPFC_EQ_CNT_1024);
12446 break;
12447 case 2048:
12448 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12449 LPFC_EQ_CNT_2048);
12450 break;
12451 case 4096:
12452 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12453 LPFC_EQ_CNT_4096);
12454 break;
12456 list_for_each_entry(dmabuf, &eq->page_list, list) {
12457 memset(dmabuf->virt, 0, hw_page_size);
12458 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12459 putPaddrLow(dmabuf->phys);
12460 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12461 putPaddrHigh(dmabuf->phys);
12463 mbox->vport = phba->pport;
12464 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12465 mbox->context1 = NULL;
12466 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12467 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12468 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12469 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12470 if (shdr_status || shdr_add_status || rc) {
12471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12472 "2500 EQ_CREATE mailbox failed with "
12473 "status x%x add_status x%x, mbx status x%x\n",
12474 shdr_status, shdr_add_status, rc);
12475 status = -ENXIO;
12477 eq->type = LPFC_EQ;
12478 eq->subtype = LPFC_NONE;
12479 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12480 if (eq->queue_id == 0xFFFF)
12481 status = -ENXIO;
12482 eq->host_index = 0;
12483 eq->hba_index = 0;
12485 mempool_free(mbox, phba->mbox_mem_pool);
12486 return status;
12490 * lpfc_cq_create - Create a Completion Queue on the HBA
12491 * @phba: HBA structure that indicates port to create a queue on.
12492 * @cq: The queue structure to use to create the completion queue.
12493 * @eq: The event queue to bind this completion queue to.
12495 * This function creates a completion queue, as detailed in @wq, on a port,
12496 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12498 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12499 * is used to get the entry count and entry size that are necessary to
12500 * determine the number of pages to allocate and use for this queue. The @eq
12501 * is used to indicate which event queue to bind this completion queue to. This
12502 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12503 * completion queue. This function is asynchronous and will wait for the mailbox
12504 * command to finish before continuing.
12506 * On success this function will return a zero. If unable to allocate enough
12507 * memory this function will return -ENOMEM. If the queue create mailbox command
12508 * fails this function will return -ENXIO.
12510 uint32_t
12511 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12512 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12514 struct lpfc_mbx_cq_create *cq_create;
12515 struct lpfc_dmabuf *dmabuf;
12516 LPFC_MBOXQ_t *mbox;
12517 int rc, length, status = 0;
12518 uint32_t shdr_status, shdr_add_status;
12519 union lpfc_sli4_cfg_shdr *shdr;
12520 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12522 /* sanity check on queue memory */
12523 if (!cq || !eq)
12524 return -ENODEV;
12525 if (!phba->sli4_hba.pc_sli4_params.supported)
12526 hw_page_size = SLI4_PAGE_SIZE;
12528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12529 if (!mbox)
12530 return -ENOMEM;
12531 length = (sizeof(struct lpfc_mbx_cq_create) -
12532 sizeof(struct lpfc_sli4_cfg_mhdr));
12533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12534 LPFC_MBOX_OPCODE_CQ_CREATE,
12535 length, LPFC_SLI4_MBX_EMBED);
12536 cq_create = &mbox->u.mqe.un.cq_create;
12537 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
12538 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12539 cq->page_count);
12540 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12541 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
12542 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12543 phba->sli4_hba.pc_sli4_params.cqv);
12544 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
12545 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12546 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
12547 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12548 eq->queue_id);
12549 } else {
12550 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12551 eq->queue_id);
12553 switch (cq->entry_count) {
12554 default:
12555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12556 "0361 Unsupported CQ count. (%d)\n",
12557 cq->entry_count);
12558 if (cq->entry_count < 256) {
12559 status = -EINVAL;
12560 goto out;
12562 /* otherwise default to smallest count (drop through) */
12563 case 256:
12564 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12565 LPFC_CQ_CNT_256);
12566 break;
12567 case 512:
12568 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12569 LPFC_CQ_CNT_512);
12570 break;
12571 case 1024:
12572 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12573 LPFC_CQ_CNT_1024);
12574 break;
12576 list_for_each_entry(dmabuf, &cq->page_list, list) {
12577 memset(dmabuf->virt, 0, hw_page_size);
12578 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12579 putPaddrLow(dmabuf->phys);
12580 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12581 putPaddrHigh(dmabuf->phys);
12583 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12585 /* The IOCTL status is embedded in the mailbox subheader. */
12586 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12587 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12588 if (shdr_status || shdr_add_status || rc) {
12589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12590 "2501 CQ_CREATE mailbox failed with "
12591 "status x%x add_status x%x, mbx status x%x\n",
12592 shdr_status, shdr_add_status, rc);
12593 status = -ENXIO;
12594 goto out;
12596 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12597 if (cq->queue_id == 0xFFFF) {
12598 status = -ENXIO;
12599 goto out;
12601 /* link the cq onto the parent eq child list */
12602 list_add_tail(&cq->list, &eq->child_list);
12603 /* Set up completion queue's type and subtype */
12604 cq->type = type;
12605 cq->subtype = subtype;
12606 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12607 cq->assoc_qid = eq->queue_id;
12608 cq->host_index = 0;
12609 cq->hba_index = 0;
12611 out:
12612 mempool_free(mbox, phba->mbox_mem_pool);
12613 return status;
12617 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12618 * @phba: HBA structure that indicates port to create a queue on.
12619 * @mq: The queue structure to use to create the mailbox queue.
12620 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12621 * @cq: The completion queue to associate with this cq.
12623 * This function provides failback (fb) functionality when the
12624 * mq_create_ext fails on older FW generations. It's purpose is identical
12625 * to mq_create_ext otherwise.
12627 * This routine cannot fail as all attributes were previously accessed and
12628 * initialized in mq_create_ext.
12630 static void
12631 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12632 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12634 struct lpfc_mbx_mq_create *mq_create;
12635 struct lpfc_dmabuf *dmabuf;
12636 int length;
12638 length = (sizeof(struct lpfc_mbx_mq_create) -
12639 sizeof(struct lpfc_sli4_cfg_mhdr));
12640 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12641 LPFC_MBOX_OPCODE_MQ_CREATE,
12642 length, LPFC_SLI4_MBX_EMBED);
12643 mq_create = &mbox->u.mqe.un.mq_create;
12644 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12645 mq->page_count);
12646 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12647 cq->queue_id);
12648 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12649 switch (mq->entry_count) {
12650 case 16:
12651 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12652 LPFC_MQ_RING_SIZE_16);
12653 break;
12654 case 32:
12655 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12656 LPFC_MQ_RING_SIZE_32);
12657 break;
12658 case 64:
12659 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12660 LPFC_MQ_RING_SIZE_64);
12661 break;
12662 case 128:
12663 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12664 LPFC_MQ_RING_SIZE_128);
12665 break;
12667 list_for_each_entry(dmabuf, &mq->page_list, list) {
12668 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12669 putPaddrLow(dmabuf->phys);
12670 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12671 putPaddrHigh(dmabuf->phys);
12676 * lpfc_mq_create - Create a mailbox Queue on the HBA
12677 * @phba: HBA structure that indicates port to create a queue on.
12678 * @mq: The queue structure to use to create the mailbox queue.
12679 * @cq: The completion queue to associate with this cq.
12680 * @subtype: The queue's subtype.
12682 * This function creates a mailbox queue, as detailed in @mq, on a port,
12683 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12685 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12686 * is used to get the entry count and entry size that are necessary to
12687 * determine the number of pages to allocate and use for this queue. This
12688 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12689 * mailbox queue. This function is asynchronous and will wait for the mailbox
12690 * command to finish before continuing.
12692 * On success this function will return a zero. If unable to allocate enough
12693 * memory this function will return -ENOMEM. If the queue create mailbox command
12694 * fails this function will return -ENXIO.
12696 int32_t
12697 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12698 struct lpfc_queue *cq, uint32_t subtype)
12700 struct lpfc_mbx_mq_create *mq_create;
12701 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12702 struct lpfc_dmabuf *dmabuf;
12703 LPFC_MBOXQ_t *mbox;
12704 int rc, length, status = 0;
12705 uint32_t shdr_status, shdr_add_status;
12706 union lpfc_sli4_cfg_shdr *shdr;
12707 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12709 /* sanity check on queue memory */
12710 if (!mq || !cq)
12711 return -ENODEV;
12712 if (!phba->sli4_hba.pc_sli4_params.supported)
12713 hw_page_size = SLI4_PAGE_SIZE;
12715 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12716 if (!mbox)
12717 return -ENOMEM;
12718 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12719 sizeof(struct lpfc_sli4_cfg_mhdr));
12720 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12721 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12722 length, LPFC_SLI4_MBX_EMBED);
12724 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
12725 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
12726 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12727 &mq_create_ext->u.request, mq->page_count);
12728 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12729 &mq_create_ext->u.request, 1);
12730 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
12731 &mq_create_ext->u.request, 1);
12732 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12733 &mq_create_ext->u.request, 1);
12734 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12735 &mq_create_ext->u.request, 1);
12736 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12737 &mq_create_ext->u.request, 1);
12738 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
12739 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12740 phba->sli4_hba.pc_sli4_params.mqv);
12741 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12742 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12743 cq->queue_id);
12744 else
12745 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12746 cq->queue_id);
12747 switch (mq->entry_count) {
12748 default:
12749 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12750 "0362 Unsupported MQ count. (%d)\n",
12751 mq->entry_count);
12752 if (mq->entry_count < 16) {
12753 status = -EINVAL;
12754 goto out;
12756 /* otherwise default to smallest count (drop through) */
12757 case 16:
12758 bf_set(lpfc_mq_context_ring_size,
12759 &mq_create_ext->u.request.context,
12760 LPFC_MQ_RING_SIZE_16);
12761 break;
12762 case 32:
12763 bf_set(lpfc_mq_context_ring_size,
12764 &mq_create_ext->u.request.context,
12765 LPFC_MQ_RING_SIZE_32);
12766 break;
12767 case 64:
12768 bf_set(lpfc_mq_context_ring_size,
12769 &mq_create_ext->u.request.context,
12770 LPFC_MQ_RING_SIZE_64);
12771 break;
12772 case 128:
12773 bf_set(lpfc_mq_context_ring_size,
12774 &mq_create_ext->u.request.context,
12775 LPFC_MQ_RING_SIZE_128);
12776 break;
12778 list_for_each_entry(dmabuf, &mq->page_list, list) {
12779 memset(dmabuf->virt, 0, hw_page_size);
12780 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12781 putPaddrLow(dmabuf->phys);
12782 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
12783 putPaddrHigh(dmabuf->phys);
12785 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12786 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12787 &mq_create_ext->u.response);
12788 if (rc != MBX_SUCCESS) {
12789 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12790 "2795 MQ_CREATE_EXT failed with "
12791 "status x%x. Failback to MQ_CREATE.\n",
12792 rc);
12793 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12794 mq_create = &mbox->u.mqe.un.mq_create;
12795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12796 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12797 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12798 &mq_create->u.response);
12801 /* The IOCTL status is embedded in the mailbox subheader. */
12802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12804 if (shdr_status || shdr_add_status || rc) {
12805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12806 "2502 MQ_CREATE mailbox failed with "
12807 "status x%x add_status x%x, mbx status x%x\n",
12808 shdr_status, shdr_add_status, rc);
12809 status = -ENXIO;
12810 goto out;
12812 if (mq->queue_id == 0xFFFF) {
12813 status = -ENXIO;
12814 goto out;
12816 mq->type = LPFC_MQ;
12817 mq->assoc_qid = cq->queue_id;
12818 mq->subtype = subtype;
12819 mq->host_index = 0;
12820 mq->hba_index = 0;
12822 /* link the mq onto the parent cq child list */
12823 list_add_tail(&mq->list, &cq->child_list);
12824 out:
12825 mempool_free(mbox, phba->mbox_mem_pool);
12826 return status;
12830 * lpfc_wq_create - Create a Work Queue on the HBA
12831 * @phba: HBA structure that indicates port to create a queue on.
12832 * @wq: The queue structure to use to create the work queue.
12833 * @cq: The completion queue to bind this work queue to.
12834 * @subtype: The subtype of the work queue indicating its functionality.
12836 * This function creates a work queue, as detailed in @wq, on a port, described
12837 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12839 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12840 * is used to get the entry count and entry size that are necessary to
12841 * determine the number of pages to allocate and use for this queue. The @cq
12842 * is used to indicate which completion queue to bind this work queue to. This
12843 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12844 * work queue. This function is asynchronous and will wait for the mailbox
12845 * command to finish before continuing.
12847 * On success this function will return a zero. If unable to allocate enough
12848 * memory this function will return -ENOMEM. If the queue create mailbox command
12849 * fails this function will return -ENXIO.
12851 uint32_t
12852 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12853 struct lpfc_queue *cq, uint32_t subtype)
12855 struct lpfc_mbx_wq_create *wq_create;
12856 struct lpfc_dmabuf *dmabuf;
12857 LPFC_MBOXQ_t *mbox;
12858 int rc, length, status = 0;
12859 uint32_t shdr_status, shdr_add_status;
12860 union lpfc_sli4_cfg_shdr *shdr;
12861 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12862 struct dma_address *page;
12863 void __iomem *bar_memmap_p;
12864 uint32_t db_offset;
12865 uint16_t pci_barset;
12867 /* sanity check on queue memory */
12868 if (!wq || !cq)
12869 return -ENODEV;
12870 if (!phba->sli4_hba.pc_sli4_params.supported)
12871 hw_page_size = SLI4_PAGE_SIZE;
12873 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12874 if (!mbox)
12875 return -ENOMEM;
12876 length = (sizeof(struct lpfc_mbx_wq_create) -
12877 sizeof(struct lpfc_sli4_cfg_mhdr));
12878 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12879 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12880 length, LPFC_SLI4_MBX_EMBED);
12881 wq_create = &mbox->u.mqe.un.wq_create;
12882 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
12883 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12884 wq->page_count);
12885 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12886 cq->queue_id);
12888 /* wqv is the earliest version supported, NOT the latest */
12889 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12890 phba->sli4_hba.pc_sli4_params.wqv);
12892 switch (phba->sli4_hba.pc_sli4_params.wqv) {
12893 case LPFC_Q_CREATE_VERSION_0:
12894 switch (wq->entry_size) {
12895 default:
12896 case 64:
12897 /* Nothing to do, version 0 ONLY supports 64 byte */
12898 page = wq_create->u.request.page;
12899 break;
12900 case 128:
12901 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
12902 LPFC_WQ_SZ128_SUPPORT)) {
12903 status = -ERANGE;
12904 goto out;
12906 /* If we get here the HBA MUST also support V1 and
12907 * we MUST use it
12909 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12910 LPFC_Q_CREATE_VERSION_1);
12912 bf_set(lpfc_mbx_wq_create_wqe_count,
12913 &wq_create->u.request_1, wq->entry_count);
12914 bf_set(lpfc_mbx_wq_create_wqe_size,
12915 &wq_create->u.request_1,
12916 LPFC_WQ_WQE_SIZE_128);
12917 bf_set(lpfc_mbx_wq_create_page_size,
12918 &wq_create->u.request_1,
12919 (PAGE_SIZE/SLI4_PAGE_SIZE));
12920 page = wq_create->u.request_1.page;
12921 break;
12923 break;
12924 case LPFC_Q_CREATE_VERSION_1:
12925 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12926 wq->entry_count);
12927 switch (wq->entry_size) {
12928 default:
12929 case 64:
12930 bf_set(lpfc_mbx_wq_create_wqe_size,
12931 &wq_create->u.request_1,
12932 LPFC_WQ_WQE_SIZE_64);
12933 break;
12934 case 128:
12935 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
12936 LPFC_WQ_SZ128_SUPPORT)) {
12937 status = -ERANGE;
12938 goto out;
12940 bf_set(lpfc_mbx_wq_create_wqe_size,
12941 &wq_create->u.request_1,
12942 LPFC_WQ_WQE_SIZE_128);
12943 break;
12945 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12946 (PAGE_SIZE/SLI4_PAGE_SIZE));
12947 page = wq_create->u.request_1.page;
12948 break;
12949 default:
12950 status = -ERANGE;
12951 goto out;
12954 list_for_each_entry(dmabuf, &wq->page_list, list) {
12955 memset(dmabuf->virt, 0, hw_page_size);
12956 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12957 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
12960 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
12961 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
12963 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12964 /* The IOCTL status is embedded in the mailbox subheader. */
12965 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12966 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12967 if (shdr_status || shdr_add_status || rc) {
12968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12969 "2503 WQ_CREATE mailbox failed with "
12970 "status x%x add_status x%x, mbx status x%x\n",
12971 shdr_status, shdr_add_status, rc);
12972 status = -ENXIO;
12973 goto out;
12975 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12976 if (wq->queue_id == 0xFFFF) {
12977 status = -ENXIO;
12978 goto out;
12980 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
12981 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
12982 &wq_create->u.response);
12983 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
12984 (wq->db_format != LPFC_DB_RING_FORMAT)) {
12985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12986 "3265 WQ[%d] doorbell format not "
12987 "supported: x%x\n", wq->queue_id,
12988 wq->db_format);
12989 status = -EINVAL;
12990 goto out;
12992 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
12993 &wq_create->u.response);
12994 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
12995 if (!bar_memmap_p) {
12996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12997 "3263 WQ[%d] failed to memmap pci "
12998 "barset:x%x\n", wq->queue_id,
12999 pci_barset);
13000 status = -ENOMEM;
13001 goto out;
13003 db_offset = wq_create->u.response.doorbell_offset;
13004 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13005 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13007 "3252 WQ[%d] doorbell offset not "
13008 "supported: x%x\n", wq->queue_id,
13009 db_offset);
13010 status = -EINVAL;
13011 goto out;
13013 wq->db_regaddr = bar_memmap_p + db_offset;
13014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13015 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13016 "format:x%x\n", wq->queue_id, pci_barset,
13017 db_offset, wq->db_format);
13018 } else {
13019 wq->db_format = LPFC_DB_LIST_FORMAT;
13020 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13022 wq->type = LPFC_WQ;
13023 wq->assoc_qid = cq->queue_id;
13024 wq->subtype = subtype;
13025 wq->host_index = 0;
13026 wq->hba_index = 0;
13027 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
13029 /* link the wq onto the parent cq child list */
13030 list_add_tail(&wq->list, &cq->child_list);
13031 out:
13032 mempool_free(mbox, phba->mbox_mem_pool);
13033 return status;
13037 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13038 * @phba: HBA structure that indicates port to create a queue on.
13039 * @rq: The queue structure to use for the receive queue.
13040 * @qno: The associated HBQ number
13043 * For SLI4 we need to adjust the RQ repost value based on
13044 * the number of buffers that are initially posted to the RQ.
13046 void
13047 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13049 uint32_t cnt;
13051 /* sanity check on queue memory */
13052 if (!rq)
13053 return;
13054 cnt = lpfc_hbq_defs[qno]->entry_count;
13056 /* Recalc repost for RQs based on buffers initially posted */
13057 cnt = (cnt >> 3);
13058 if (cnt < LPFC_QUEUE_MIN_REPOST)
13059 cnt = LPFC_QUEUE_MIN_REPOST;
13061 rq->entry_repost = cnt;
13065 * lpfc_rq_create - Create a Receive Queue on the HBA
13066 * @phba: HBA structure that indicates port to create a queue on.
13067 * @hrq: The queue structure to use to create the header receive queue.
13068 * @drq: The queue structure to use to create the data receive queue.
13069 * @cq: The completion queue to bind this work queue to.
13071 * This function creates a receive buffer queue pair , as detailed in @hrq and
13072 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13073 * to the HBA.
13075 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13076 * struct is used to get the entry count that is necessary to determine the
13077 * number of pages to use for this queue. The @cq is used to indicate which
13078 * completion queue to bind received buffers that are posted to these queues to.
13079 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13080 * receive queue pair. This function is asynchronous and will wait for the
13081 * mailbox command to finish before continuing.
13083 * On success this function will return a zero. If unable to allocate enough
13084 * memory this function will return -ENOMEM. If the queue create mailbox command
13085 * fails this function will return -ENXIO.
13087 uint32_t
13088 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13089 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13091 struct lpfc_mbx_rq_create *rq_create;
13092 struct lpfc_dmabuf *dmabuf;
13093 LPFC_MBOXQ_t *mbox;
13094 int rc, length, status = 0;
13095 uint32_t shdr_status, shdr_add_status;
13096 union lpfc_sli4_cfg_shdr *shdr;
13097 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13098 void __iomem *bar_memmap_p;
13099 uint32_t db_offset;
13100 uint16_t pci_barset;
13102 /* sanity check on queue memory */
13103 if (!hrq || !drq || !cq)
13104 return -ENODEV;
13105 if (!phba->sli4_hba.pc_sli4_params.supported)
13106 hw_page_size = SLI4_PAGE_SIZE;
13108 if (hrq->entry_count != drq->entry_count)
13109 return -EINVAL;
13110 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13111 if (!mbox)
13112 return -ENOMEM;
13113 length = (sizeof(struct lpfc_mbx_rq_create) -
13114 sizeof(struct lpfc_sli4_cfg_mhdr));
13115 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13116 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13117 length, LPFC_SLI4_MBX_EMBED);
13118 rq_create = &mbox->u.mqe.un.rq_create;
13119 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13120 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13121 phba->sli4_hba.pc_sli4_params.rqv);
13122 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13123 bf_set(lpfc_rq_context_rqe_count_1,
13124 &rq_create->u.request.context,
13125 hrq->entry_count);
13126 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
13127 bf_set(lpfc_rq_context_rqe_size,
13128 &rq_create->u.request.context,
13129 LPFC_RQE_SIZE_8);
13130 bf_set(lpfc_rq_context_page_size,
13131 &rq_create->u.request.context,
13132 (PAGE_SIZE/SLI4_PAGE_SIZE));
13133 } else {
13134 switch (hrq->entry_count) {
13135 default:
13136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13137 "2535 Unsupported RQ count. (%d)\n",
13138 hrq->entry_count);
13139 if (hrq->entry_count < 512) {
13140 status = -EINVAL;
13141 goto out;
13143 /* otherwise default to smallest count (drop through) */
13144 case 512:
13145 bf_set(lpfc_rq_context_rqe_count,
13146 &rq_create->u.request.context,
13147 LPFC_RQ_RING_SIZE_512);
13148 break;
13149 case 1024:
13150 bf_set(lpfc_rq_context_rqe_count,
13151 &rq_create->u.request.context,
13152 LPFC_RQ_RING_SIZE_1024);
13153 break;
13154 case 2048:
13155 bf_set(lpfc_rq_context_rqe_count,
13156 &rq_create->u.request.context,
13157 LPFC_RQ_RING_SIZE_2048);
13158 break;
13159 case 4096:
13160 bf_set(lpfc_rq_context_rqe_count,
13161 &rq_create->u.request.context,
13162 LPFC_RQ_RING_SIZE_4096);
13163 break;
13165 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13166 LPFC_HDR_BUF_SIZE);
13168 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13169 cq->queue_id);
13170 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13171 hrq->page_count);
13172 list_for_each_entry(dmabuf, &hrq->page_list, list) {
13173 memset(dmabuf->virt, 0, hw_page_size);
13174 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13175 putPaddrLow(dmabuf->phys);
13176 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13177 putPaddrHigh(dmabuf->phys);
13179 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13180 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13182 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13183 /* The IOCTL status is embedded in the mailbox subheader. */
13184 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13185 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13186 if (shdr_status || shdr_add_status || rc) {
13187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13188 "2504 RQ_CREATE mailbox failed with "
13189 "status x%x add_status x%x, mbx status x%x\n",
13190 shdr_status, shdr_add_status, rc);
13191 status = -ENXIO;
13192 goto out;
13194 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13195 if (hrq->queue_id == 0xFFFF) {
13196 status = -ENXIO;
13197 goto out;
13200 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13201 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13202 &rq_create->u.response);
13203 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13204 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13206 "3262 RQ [%d] doorbell format not "
13207 "supported: x%x\n", hrq->queue_id,
13208 hrq->db_format);
13209 status = -EINVAL;
13210 goto out;
13213 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13214 &rq_create->u.response);
13215 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13216 if (!bar_memmap_p) {
13217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13218 "3269 RQ[%d] failed to memmap pci "
13219 "barset:x%x\n", hrq->queue_id,
13220 pci_barset);
13221 status = -ENOMEM;
13222 goto out;
13225 db_offset = rq_create->u.response.doorbell_offset;
13226 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13227 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13229 "3270 RQ[%d] doorbell offset not "
13230 "supported: x%x\n", hrq->queue_id,
13231 db_offset);
13232 status = -EINVAL;
13233 goto out;
13235 hrq->db_regaddr = bar_memmap_p + db_offset;
13236 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13237 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13238 "format:x%x\n", hrq->queue_id, pci_barset,
13239 db_offset, hrq->db_format);
13240 } else {
13241 hrq->db_format = LPFC_DB_RING_FORMAT;
13242 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13244 hrq->type = LPFC_HRQ;
13245 hrq->assoc_qid = cq->queue_id;
13246 hrq->subtype = subtype;
13247 hrq->host_index = 0;
13248 hrq->hba_index = 0;
13250 /* now create the data queue */
13251 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13252 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13253 length, LPFC_SLI4_MBX_EMBED);
13254 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13255 phba->sli4_hba.pc_sli4_params.rqv);
13256 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13257 bf_set(lpfc_rq_context_rqe_count_1,
13258 &rq_create->u.request.context, hrq->entry_count);
13259 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
13260 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13261 LPFC_RQE_SIZE_8);
13262 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13263 (PAGE_SIZE/SLI4_PAGE_SIZE));
13264 } else {
13265 switch (drq->entry_count) {
13266 default:
13267 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13268 "2536 Unsupported RQ count. (%d)\n",
13269 drq->entry_count);
13270 if (drq->entry_count < 512) {
13271 status = -EINVAL;
13272 goto out;
13274 /* otherwise default to smallest count (drop through) */
13275 case 512:
13276 bf_set(lpfc_rq_context_rqe_count,
13277 &rq_create->u.request.context,
13278 LPFC_RQ_RING_SIZE_512);
13279 break;
13280 case 1024:
13281 bf_set(lpfc_rq_context_rqe_count,
13282 &rq_create->u.request.context,
13283 LPFC_RQ_RING_SIZE_1024);
13284 break;
13285 case 2048:
13286 bf_set(lpfc_rq_context_rqe_count,
13287 &rq_create->u.request.context,
13288 LPFC_RQ_RING_SIZE_2048);
13289 break;
13290 case 4096:
13291 bf_set(lpfc_rq_context_rqe_count,
13292 &rq_create->u.request.context,
13293 LPFC_RQ_RING_SIZE_4096);
13294 break;
13296 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13297 LPFC_DATA_BUF_SIZE);
13299 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13300 cq->queue_id);
13301 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13302 drq->page_count);
13303 list_for_each_entry(dmabuf, &drq->page_list, list) {
13304 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13305 putPaddrLow(dmabuf->phys);
13306 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13307 putPaddrHigh(dmabuf->phys);
13309 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13310 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13311 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13312 /* The IOCTL status is embedded in the mailbox subheader. */
13313 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13314 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13315 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13316 if (shdr_status || shdr_add_status || rc) {
13317 status = -ENXIO;
13318 goto out;
13320 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13321 if (drq->queue_id == 0xFFFF) {
13322 status = -ENXIO;
13323 goto out;
13325 drq->type = LPFC_DRQ;
13326 drq->assoc_qid = cq->queue_id;
13327 drq->subtype = subtype;
13328 drq->host_index = 0;
13329 drq->hba_index = 0;
13331 /* link the header and data RQs onto the parent cq child list */
13332 list_add_tail(&hrq->list, &cq->child_list);
13333 list_add_tail(&drq->list, &cq->child_list);
13335 out:
13336 mempool_free(mbox, phba->mbox_mem_pool);
13337 return status;
13341 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13342 * @eq: The queue structure associated with the queue to destroy.
13344 * This function destroys a queue, as detailed in @eq by sending an mailbox
13345 * command, specific to the type of queue, to the HBA.
13347 * The @eq struct is used to get the queue ID of the queue to destroy.
13349 * On success this function will return a zero. If the queue destroy mailbox
13350 * command fails this function will return -ENXIO.
13352 uint32_t
13353 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13355 LPFC_MBOXQ_t *mbox;
13356 int rc, length, status = 0;
13357 uint32_t shdr_status, shdr_add_status;
13358 union lpfc_sli4_cfg_shdr *shdr;
13360 /* sanity check on queue memory */
13361 if (!eq)
13362 return -ENODEV;
13363 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13364 if (!mbox)
13365 return -ENOMEM;
13366 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13367 sizeof(struct lpfc_sli4_cfg_mhdr));
13368 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13369 LPFC_MBOX_OPCODE_EQ_DESTROY,
13370 length, LPFC_SLI4_MBX_EMBED);
13371 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13372 eq->queue_id);
13373 mbox->vport = eq->phba->pport;
13374 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13376 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13377 /* The IOCTL status is embedded in the mailbox subheader. */
13378 shdr = (union lpfc_sli4_cfg_shdr *)
13379 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13380 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13381 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13382 if (shdr_status || shdr_add_status || rc) {
13383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13384 "2505 EQ_DESTROY mailbox failed with "
13385 "status x%x add_status x%x, mbx status x%x\n",
13386 shdr_status, shdr_add_status, rc);
13387 status = -ENXIO;
13390 /* Remove eq from any list */
13391 list_del_init(&eq->list);
13392 mempool_free(mbox, eq->phba->mbox_mem_pool);
13393 return status;
13397 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13398 * @cq: The queue structure associated with the queue to destroy.
13400 * This function destroys a queue, as detailed in @cq by sending an mailbox
13401 * command, specific to the type of queue, to the HBA.
13403 * The @cq struct is used to get the queue ID of the queue to destroy.
13405 * On success this function will return a zero. If the queue destroy mailbox
13406 * command fails this function will return -ENXIO.
13408 uint32_t
13409 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13411 LPFC_MBOXQ_t *mbox;
13412 int rc, length, status = 0;
13413 uint32_t shdr_status, shdr_add_status;
13414 union lpfc_sli4_cfg_shdr *shdr;
13416 /* sanity check on queue memory */
13417 if (!cq)
13418 return -ENODEV;
13419 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13420 if (!mbox)
13421 return -ENOMEM;
13422 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13423 sizeof(struct lpfc_sli4_cfg_mhdr));
13424 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13425 LPFC_MBOX_OPCODE_CQ_DESTROY,
13426 length, LPFC_SLI4_MBX_EMBED);
13427 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13428 cq->queue_id);
13429 mbox->vport = cq->phba->pport;
13430 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13431 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13432 /* The IOCTL status is embedded in the mailbox subheader. */
13433 shdr = (union lpfc_sli4_cfg_shdr *)
13434 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13435 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13436 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13437 if (shdr_status || shdr_add_status || rc) {
13438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13439 "2506 CQ_DESTROY mailbox failed with "
13440 "status x%x add_status x%x, mbx status x%x\n",
13441 shdr_status, shdr_add_status, rc);
13442 status = -ENXIO;
13444 /* Remove cq from any list */
13445 list_del_init(&cq->list);
13446 mempool_free(mbox, cq->phba->mbox_mem_pool);
13447 return status;
13451 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13452 * @qm: The queue structure associated with the queue to destroy.
13454 * This function destroys a queue, as detailed in @mq by sending an mailbox
13455 * command, specific to the type of queue, to the HBA.
13457 * The @mq struct is used to get the queue ID of the queue to destroy.
13459 * On success this function will return a zero. If the queue destroy mailbox
13460 * command fails this function will return -ENXIO.
13462 uint32_t
13463 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13465 LPFC_MBOXQ_t *mbox;
13466 int rc, length, status = 0;
13467 uint32_t shdr_status, shdr_add_status;
13468 union lpfc_sli4_cfg_shdr *shdr;
13470 /* sanity check on queue memory */
13471 if (!mq)
13472 return -ENODEV;
13473 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13474 if (!mbox)
13475 return -ENOMEM;
13476 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13477 sizeof(struct lpfc_sli4_cfg_mhdr));
13478 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13479 LPFC_MBOX_OPCODE_MQ_DESTROY,
13480 length, LPFC_SLI4_MBX_EMBED);
13481 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13482 mq->queue_id);
13483 mbox->vport = mq->phba->pport;
13484 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13485 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13486 /* The IOCTL status is embedded in the mailbox subheader. */
13487 shdr = (union lpfc_sli4_cfg_shdr *)
13488 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13489 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13491 if (shdr_status || shdr_add_status || rc) {
13492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13493 "2507 MQ_DESTROY mailbox failed with "
13494 "status x%x add_status x%x, mbx status x%x\n",
13495 shdr_status, shdr_add_status, rc);
13496 status = -ENXIO;
13498 /* Remove mq from any list */
13499 list_del_init(&mq->list);
13500 mempool_free(mbox, mq->phba->mbox_mem_pool);
13501 return status;
13505 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13506 * @wq: The queue structure associated with the queue to destroy.
13508 * This function destroys a queue, as detailed in @wq by sending an mailbox
13509 * command, specific to the type of queue, to the HBA.
13511 * The @wq struct is used to get the queue ID of the queue to destroy.
13513 * On success this function will return a zero. If the queue destroy mailbox
13514 * command fails this function will return -ENXIO.
13516 uint32_t
13517 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13519 LPFC_MBOXQ_t *mbox;
13520 int rc, length, status = 0;
13521 uint32_t shdr_status, shdr_add_status;
13522 union lpfc_sli4_cfg_shdr *shdr;
13524 /* sanity check on queue memory */
13525 if (!wq)
13526 return -ENODEV;
13527 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13528 if (!mbox)
13529 return -ENOMEM;
13530 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13531 sizeof(struct lpfc_sli4_cfg_mhdr));
13532 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13533 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13534 length, LPFC_SLI4_MBX_EMBED);
13535 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13536 wq->queue_id);
13537 mbox->vport = wq->phba->pport;
13538 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13539 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13540 shdr = (union lpfc_sli4_cfg_shdr *)
13541 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13542 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13543 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13544 if (shdr_status || shdr_add_status || rc) {
13545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13546 "2508 WQ_DESTROY mailbox failed with "
13547 "status x%x add_status x%x, mbx status x%x\n",
13548 shdr_status, shdr_add_status, rc);
13549 status = -ENXIO;
13551 /* Remove wq from any list */
13552 list_del_init(&wq->list);
13553 mempool_free(mbox, wq->phba->mbox_mem_pool);
13554 return status;
13558 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13559 * @rq: The queue structure associated with the queue to destroy.
13561 * This function destroys a queue, as detailed in @rq by sending an mailbox
13562 * command, specific to the type of queue, to the HBA.
13564 * The @rq struct is used to get the queue ID of the queue to destroy.
13566 * On success this function will return a zero. If the queue destroy mailbox
13567 * command fails this function will return -ENXIO.
13569 uint32_t
13570 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13571 struct lpfc_queue *drq)
13573 LPFC_MBOXQ_t *mbox;
13574 int rc, length, status = 0;
13575 uint32_t shdr_status, shdr_add_status;
13576 union lpfc_sli4_cfg_shdr *shdr;
13578 /* sanity check on queue memory */
13579 if (!hrq || !drq)
13580 return -ENODEV;
13581 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13582 if (!mbox)
13583 return -ENOMEM;
13584 length = (sizeof(struct lpfc_mbx_rq_destroy) -
13585 sizeof(struct lpfc_sli4_cfg_mhdr));
13586 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13587 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13588 length, LPFC_SLI4_MBX_EMBED);
13589 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13590 hrq->queue_id);
13591 mbox->vport = hrq->phba->pport;
13592 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13593 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13594 /* The IOCTL status is embedded in the mailbox subheader. */
13595 shdr = (union lpfc_sli4_cfg_shdr *)
13596 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13597 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13598 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13599 if (shdr_status || shdr_add_status || rc) {
13600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13601 "2509 RQ_DESTROY mailbox failed with "
13602 "status x%x add_status x%x, mbx status x%x\n",
13603 shdr_status, shdr_add_status, rc);
13604 if (rc != MBX_TIMEOUT)
13605 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13606 return -ENXIO;
13608 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13609 drq->queue_id);
13610 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13611 shdr = (union lpfc_sli4_cfg_shdr *)
13612 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13615 if (shdr_status || shdr_add_status || rc) {
13616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13617 "2510 RQ_DESTROY mailbox failed with "
13618 "status x%x add_status x%x, mbx status x%x\n",
13619 shdr_status, shdr_add_status, rc);
13620 status = -ENXIO;
13622 list_del_init(&hrq->list);
13623 list_del_init(&drq->list);
13624 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13625 return status;
13629 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13630 * @phba: The virtual port for which this call being executed.
13631 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13632 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13633 * @xritag: the xritag that ties this io to the SGL pages.
13635 * This routine will post the sgl pages for the IO that has the xritag
13636 * that is in the iocbq structure. The xritag is assigned during iocbq
13637 * creation and persists for as long as the driver is loaded.
13638 * if the caller has fewer than 256 scatter gather segments to map then
13639 * pdma_phys_addr1 should be 0.
13640 * If the caller needs to map more than 256 scatter gather segment then
13641 * pdma_phys_addr1 should be a valid physical address.
13642 * physical address for SGLs must be 64 byte aligned.
13643 * If you are going to map 2 SGL's then the first one must have 256 entries
13644 * the second sgl can have between 1 and 256 entries.
13646 * Return codes:
13647 * 0 - Success
13648 * -ENXIO, -ENOMEM - Failure
13651 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13652 dma_addr_t pdma_phys_addr0,
13653 dma_addr_t pdma_phys_addr1,
13654 uint16_t xritag)
13656 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13657 LPFC_MBOXQ_t *mbox;
13658 int rc;
13659 uint32_t shdr_status, shdr_add_status;
13660 uint32_t mbox_tmo;
13661 union lpfc_sli4_cfg_shdr *shdr;
13663 if (xritag == NO_XRI) {
13664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13665 "0364 Invalid param:\n");
13666 return -EINVAL;
13669 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13670 if (!mbox)
13671 return -ENOMEM;
13673 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13674 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13675 sizeof(struct lpfc_mbx_post_sgl_pages) -
13676 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
13678 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13679 &mbox->u.mqe.un.post_sgl_pages;
13680 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13681 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13683 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13684 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13685 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13686 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13688 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13689 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13690 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13691 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13692 if (!phba->sli4_hba.intr_enable)
13693 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13694 else {
13695 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13696 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13698 /* The IOCTL status is embedded in the mailbox subheader. */
13699 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13700 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13701 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13702 if (rc != MBX_TIMEOUT)
13703 mempool_free(mbox, phba->mbox_mem_pool);
13704 if (shdr_status || shdr_add_status || rc) {
13705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13706 "2511 POST_SGL mailbox failed with "
13707 "status x%x add_status x%x, mbx status x%x\n",
13708 shdr_status, shdr_add_status, rc);
13709 rc = -ENXIO;
13711 return 0;
13715 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
13716 * @phba: pointer to lpfc hba data structure.
13718 * This routine is invoked to post rpi header templates to the
13719 * HBA consistent with the SLI-4 interface spec. This routine
13720 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13721 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13723 * Returns
13724 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13725 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13727 uint16_t
13728 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13730 unsigned long xri;
13733 * Fetch the next logical xri. Because this index is logical,
13734 * the driver starts at 0 each time.
13736 spin_lock_irq(&phba->hbalock);
13737 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13738 phba->sli4_hba.max_cfg_param.max_xri, 0);
13739 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13740 spin_unlock_irq(&phba->hbalock);
13741 return NO_XRI;
13742 } else {
13743 set_bit(xri, phba->sli4_hba.xri_bmask);
13744 phba->sli4_hba.max_cfg_param.xri_used++;
13746 spin_unlock_irq(&phba->hbalock);
13747 return xri;
13751 * lpfc_sli4_free_xri - Release an xri for reuse.
13752 * @phba: pointer to lpfc hba data structure.
13754 * This routine is invoked to release an xri to the pool of
13755 * available rpis maintained by the driver.
13757 void
13758 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13760 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13761 phba->sli4_hba.max_cfg_param.xri_used--;
13766 * lpfc_sli4_free_xri - Release an xri for reuse.
13767 * @phba: pointer to lpfc hba data structure.
13769 * This routine is invoked to release an xri to the pool of
13770 * available rpis maintained by the driver.
13772 void
13773 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13775 spin_lock_irq(&phba->hbalock);
13776 __lpfc_sli4_free_xri(phba, xri);
13777 spin_unlock_irq(&phba->hbalock);
13781 * lpfc_sli4_next_xritag - Get an xritag for the io
13782 * @phba: Pointer to HBA context object.
13784 * This function gets an xritag for the iocb. If there is no unused xritag
13785 * it will return 0xffff.
13786 * The function returns the allocated xritag if successful, else returns zero.
13787 * Zero is not a valid xritag.
13788 * The caller is not required to hold any lock.
13790 uint16_t
13791 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13793 uint16_t xri_index;
13795 xri_index = lpfc_sli4_alloc_xri(phba);
13796 if (xri_index == NO_XRI)
13797 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13798 "2004 Failed to allocate XRI.last XRITAG is %d"
13799 " Max XRI is %d, Used XRI is %d\n",
13800 xri_index,
13801 phba->sli4_hba.max_cfg_param.max_xri,
13802 phba->sli4_hba.max_cfg_param.xri_used);
13803 return xri_index;
13807 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
13808 * @phba: pointer to lpfc hba data structure.
13809 * @post_sgl_list: pointer to els sgl entry list.
13810 * @count: number of els sgl entries on the list.
13812 * This routine is invoked to post a block of driver's sgl pages to the
13813 * HBA using non-embedded mailbox command. No Lock is held. This routine
13814 * is only called when the driver is loading and after all IO has been
13815 * stopped.
13817 static int
13818 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13819 struct list_head *post_sgl_list,
13820 int post_cnt)
13822 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
13823 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13824 struct sgl_page_pairs *sgl_pg_pairs;
13825 void *viraddr;
13826 LPFC_MBOXQ_t *mbox;
13827 uint32_t reqlen, alloclen, pg_pairs;
13828 uint32_t mbox_tmo;
13829 uint16_t xritag_start = 0;
13830 int rc = 0;
13831 uint32_t shdr_status, shdr_add_status;
13832 union lpfc_sli4_cfg_shdr *shdr;
13834 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
13835 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13836 if (reqlen > SLI4_PAGE_SIZE) {
13837 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13838 "2559 Block sgl registration required DMA "
13839 "size (%d) great than a page\n", reqlen);
13840 return -ENOMEM;
13842 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13843 if (!mbox)
13844 return -ENOMEM;
13846 /* Allocate DMA memory and set up the non-embedded mailbox command */
13847 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13848 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13849 LPFC_SLI4_MBX_NEMBED);
13851 if (alloclen < reqlen) {
13852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13853 "0285 Allocated DMA memory size (%d) is "
13854 "less than the requested DMA memory "
13855 "size (%d)\n", alloclen, reqlen);
13856 lpfc_sli4_mbox_cmd_free(phba, mbox);
13857 return -ENOMEM;
13859 /* Set up the SGL pages in the non-embedded DMA pages */
13860 viraddr = mbox->sge_array->addr[0];
13861 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13862 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13864 pg_pairs = 0;
13865 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
13866 /* Set up the sge entry */
13867 sgl_pg_pairs->sgl_pg0_addr_lo =
13868 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13869 sgl_pg_pairs->sgl_pg0_addr_hi =
13870 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13871 sgl_pg_pairs->sgl_pg1_addr_lo =
13872 cpu_to_le32(putPaddrLow(0));
13873 sgl_pg_pairs->sgl_pg1_addr_hi =
13874 cpu_to_le32(putPaddrHigh(0));
13876 /* Keep the first xritag on the list */
13877 if (pg_pairs == 0)
13878 xritag_start = sglq_entry->sli4_xritag;
13879 sgl_pg_pairs++;
13880 pg_pairs++;
13883 /* Complete initialization and perform endian conversion. */
13884 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13885 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
13886 sgl->word0 = cpu_to_le32(sgl->word0);
13887 if (!phba->sli4_hba.intr_enable)
13888 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13889 else {
13890 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13891 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13893 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13894 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13895 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13896 if (rc != MBX_TIMEOUT)
13897 lpfc_sli4_mbox_cmd_free(phba, mbox);
13898 if (shdr_status || shdr_add_status || rc) {
13899 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13900 "2513 POST_SGL_BLOCK mailbox command failed "
13901 "status x%x add_status x%x mbx status x%x\n",
13902 shdr_status, shdr_add_status, rc);
13903 rc = -ENXIO;
13905 return rc;
13909 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13910 * @phba: pointer to lpfc hba data structure.
13911 * @sblist: pointer to scsi buffer list.
13912 * @count: number of scsi buffers on the list.
13914 * This routine is invoked to post a block of @count scsi sgl pages from a
13915 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13916 * No Lock is held.
13920 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13921 struct list_head *sblist,
13922 int count)
13924 struct lpfc_scsi_buf *psb;
13925 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13926 struct sgl_page_pairs *sgl_pg_pairs;
13927 void *viraddr;
13928 LPFC_MBOXQ_t *mbox;
13929 uint32_t reqlen, alloclen, pg_pairs;
13930 uint32_t mbox_tmo;
13931 uint16_t xritag_start = 0;
13932 int rc = 0;
13933 uint32_t shdr_status, shdr_add_status;
13934 dma_addr_t pdma_phys_bpl1;
13935 union lpfc_sli4_cfg_shdr *shdr;
13937 /* Calculate the requested length of the dma memory */
13938 reqlen = count * sizeof(struct sgl_page_pairs) +
13939 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13940 if (reqlen > SLI4_PAGE_SIZE) {
13941 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13942 "0217 Block sgl registration required DMA "
13943 "size (%d) great than a page\n", reqlen);
13944 return -ENOMEM;
13946 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13947 if (!mbox) {
13948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13949 "0283 Failed to allocate mbox cmd memory\n");
13950 return -ENOMEM;
13953 /* Allocate DMA memory and set up the non-embedded mailbox command */
13954 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13955 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13956 LPFC_SLI4_MBX_NEMBED);
13958 if (alloclen < reqlen) {
13959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13960 "2561 Allocated DMA memory size (%d) is "
13961 "less than the requested DMA memory "
13962 "size (%d)\n", alloclen, reqlen);
13963 lpfc_sli4_mbox_cmd_free(phba, mbox);
13964 return -ENOMEM;
13967 /* Get the first SGE entry from the non-embedded DMA memory */
13968 viraddr = mbox->sge_array->addr[0];
13970 /* Set up the SGL pages in the non-embedded DMA pages */
13971 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13972 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13974 pg_pairs = 0;
13975 list_for_each_entry(psb, sblist, list) {
13976 /* Set up the sge entry */
13977 sgl_pg_pairs->sgl_pg0_addr_lo =
13978 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13979 sgl_pg_pairs->sgl_pg0_addr_hi =
13980 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13981 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13982 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13983 else
13984 pdma_phys_bpl1 = 0;
13985 sgl_pg_pairs->sgl_pg1_addr_lo =
13986 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13987 sgl_pg_pairs->sgl_pg1_addr_hi =
13988 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13989 /* Keep the first xritag on the list */
13990 if (pg_pairs == 0)
13991 xritag_start = psb->cur_iocbq.sli4_xritag;
13992 sgl_pg_pairs++;
13993 pg_pairs++;
13995 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13996 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13997 /* Perform endian conversion if necessary */
13998 sgl->word0 = cpu_to_le32(sgl->word0);
14000 if (!phba->sli4_hba.intr_enable)
14001 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14002 else {
14003 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14004 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14006 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14007 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14008 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14009 if (rc != MBX_TIMEOUT)
14010 lpfc_sli4_mbox_cmd_free(phba, mbox);
14011 if (shdr_status || shdr_add_status || rc) {
14012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14013 "2564 POST_SGL_BLOCK mailbox command failed "
14014 "status x%x add_status x%x mbx status x%x\n",
14015 shdr_status, shdr_add_status, rc);
14016 rc = -ENXIO;
14018 return rc;
14022 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14023 * @phba: pointer to lpfc_hba struct that the frame was received on
14024 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14026 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14027 * valid type of frame that the LPFC driver will handle. This function will
14028 * return a zero if the frame is a valid frame or a non zero value when the
14029 * frame does not pass the check.
14031 static int
14032 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14034 /* make rctl_names static to save stack space */
14035 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
14036 char *type_names[] = FC_TYPE_NAMES_INIT;
14037 struct fc_vft_header *fc_vft_hdr;
14038 uint32_t *header = (uint32_t *) fc_hdr;
14040 switch (fc_hdr->fh_r_ctl) {
14041 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14042 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14043 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14044 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14045 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14046 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14047 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14048 case FC_RCTL_DD_CMD_STATUS: /* command status */
14049 case FC_RCTL_ELS_REQ: /* extended link services request */
14050 case FC_RCTL_ELS_REP: /* extended link services reply */
14051 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14052 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14053 case FC_RCTL_BA_NOP: /* basic link service NOP */
14054 case FC_RCTL_BA_ABTS: /* basic link service abort */
14055 case FC_RCTL_BA_RMC: /* remove connection */
14056 case FC_RCTL_BA_ACC: /* basic accept */
14057 case FC_RCTL_BA_RJT: /* basic reject */
14058 case FC_RCTL_BA_PRMT:
14059 case FC_RCTL_ACK_1: /* acknowledge_1 */
14060 case FC_RCTL_ACK_0: /* acknowledge_0 */
14061 case FC_RCTL_P_RJT: /* port reject */
14062 case FC_RCTL_F_RJT: /* fabric reject */
14063 case FC_RCTL_P_BSY: /* port busy */
14064 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14065 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14066 case FC_RCTL_LCR: /* link credit reset */
14067 case FC_RCTL_END: /* end */
14068 break;
14069 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14070 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14071 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14072 return lpfc_fc_frame_check(phba, fc_hdr);
14073 default:
14074 goto drop;
14076 switch (fc_hdr->fh_type) {
14077 case FC_TYPE_BLS:
14078 case FC_TYPE_ELS:
14079 case FC_TYPE_FCP:
14080 case FC_TYPE_CT:
14081 break;
14082 case FC_TYPE_IP:
14083 case FC_TYPE_ILS:
14084 default:
14085 goto drop;
14088 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14089 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14090 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14091 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14092 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
14093 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14094 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
14095 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14096 be32_to_cpu(header[6]));
14097 return 0;
14098 drop:
14099 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14100 "2539 Dropped frame rctl:%s type:%s\n",
14101 rctl_names[fc_hdr->fh_r_ctl],
14102 type_names[fc_hdr->fh_type]);
14103 return 1;
14107 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14108 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14110 * This function processes the FC header to retrieve the VFI from the VF
14111 * header, if one exists. This function will return the VFI if one exists
14112 * or 0 if no VSAN Header exists.
14114 static uint32_t
14115 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14117 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14119 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14120 return 0;
14121 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14125 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14126 * @phba: Pointer to the HBA structure to search for the vport on
14127 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14128 * @fcfi: The FC Fabric ID that the frame came from
14130 * This function searches the @phba for a vport that matches the content of the
14131 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14132 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14133 * returns the matching vport pointer or NULL if unable to match frame to a
14134 * vport.
14136 static struct lpfc_vport *
14137 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14138 uint16_t fcfi)
14140 struct lpfc_vport **vports;
14141 struct lpfc_vport *vport = NULL;
14142 int i;
14143 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14144 fc_hdr->fh_d_id[1] << 8 |
14145 fc_hdr->fh_d_id[2]);
14147 if (did == Fabric_DID)
14148 return phba->pport;
14149 if ((phba->pport->fc_flag & FC_PT2PT) &&
14150 !(phba->link_state == LPFC_HBA_READY))
14151 return phba->pport;
14153 vports = lpfc_create_vport_work_array(phba);
14154 if (vports != NULL)
14155 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14156 if (phba->fcf.fcfi == fcfi &&
14157 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14158 vports[i]->fc_myDID == did) {
14159 vport = vports[i];
14160 break;
14163 lpfc_destroy_vport_work_array(phba, vports);
14164 return vport;
14168 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14169 * @vport: The vport to work on.
14171 * This function updates the receive sequence time stamp for this vport. The
14172 * receive sequence time stamp indicates the time that the last frame of the
14173 * the sequence that has been idle for the longest amount of time was received.
14174 * the driver uses this time stamp to indicate if any received sequences have
14175 * timed out.
14177 void
14178 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14180 struct lpfc_dmabuf *h_buf;
14181 struct hbq_dmabuf *dmabuf = NULL;
14183 /* get the oldest sequence on the rcv list */
14184 h_buf = list_get_first(&vport->rcv_buffer_list,
14185 struct lpfc_dmabuf, list);
14186 if (!h_buf)
14187 return;
14188 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14189 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14193 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14194 * @vport: The vport that the received sequences were sent to.
14196 * This function cleans up all outstanding received sequences. This is called
14197 * by the driver when a link event or user action invalidates all the received
14198 * sequences.
14200 void
14201 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14203 struct lpfc_dmabuf *h_buf, *hnext;
14204 struct lpfc_dmabuf *d_buf, *dnext;
14205 struct hbq_dmabuf *dmabuf = NULL;
14207 /* start with the oldest sequence on the rcv list */
14208 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14209 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14210 list_del_init(&dmabuf->hbuf.list);
14211 list_for_each_entry_safe(d_buf, dnext,
14212 &dmabuf->dbuf.list, list) {
14213 list_del_init(&d_buf->list);
14214 lpfc_in_buf_free(vport->phba, d_buf);
14216 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14221 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14222 * @vport: The vport that the received sequences were sent to.
14224 * This function determines whether any received sequences have timed out by
14225 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14226 * indicates that there is at least one timed out sequence this routine will
14227 * go through the received sequences one at a time from most inactive to most
14228 * active to determine which ones need to be cleaned up. Once it has determined
14229 * that a sequence needs to be cleaned up it will simply free up the resources
14230 * without sending an abort.
14232 void
14233 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14235 struct lpfc_dmabuf *h_buf, *hnext;
14236 struct lpfc_dmabuf *d_buf, *dnext;
14237 struct hbq_dmabuf *dmabuf = NULL;
14238 unsigned long timeout;
14239 int abort_count = 0;
14241 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14242 vport->rcv_buffer_time_stamp);
14243 if (list_empty(&vport->rcv_buffer_list) ||
14244 time_before(jiffies, timeout))
14245 return;
14246 /* start with the oldest sequence on the rcv list */
14247 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14248 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14249 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14250 dmabuf->time_stamp);
14251 if (time_before(jiffies, timeout))
14252 break;
14253 abort_count++;
14254 list_del_init(&dmabuf->hbuf.list);
14255 list_for_each_entry_safe(d_buf, dnext,
14256 &dmabuf->dbuf.list, list) {
14257 list_del_init(&d_buf->list);
14258 lpfc_in_buf_free(vport->phba, d_buf);
14260 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14262 if (abort_count)
14263 lpfc_update_rcv_time_stamp(vport);
14267 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14268 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14270 * This function searches through the existing incomplete sequences that have
14271 * been sent to this @vport. If the frame matches one of the incomplete
14272 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14273 * make up that sequence. If no sequence is found that matches this frame then
14274 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14275 * This function returns a pointer to the first dmabuf in the sequence list that
14276 * the frame was linked to.
14278 static struct hbq_dmabuf *
14279 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14281 struct fc_frame_header *new_hdr;
14282 struct fc_frame_header *temp_hdr;
14283 struct lpfc_dmabuf *d_buf;
14284 struct lpfc_dmabuf *h_buf;
14285 struct hbq_dmabuf *seq_dmabuf = NULL;
14286 struct hbq_dmabuf *temp_dmabuf = NULL;
14288 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14289 dmabuf->time_stamp = jiffies;
14290 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14291 /* Use the hdr_buf to find the sequence that this frame belongs to */
14292 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14293 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14294 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14295 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14296 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14297 continue;
14298 /* found a pending sequence that matches this frame */
14299 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14300 break;
14302 if (!seq_dmabuf) {
14304 * This indicates first frame received for this sequence.
14305 * Queue the buffer on the vport's rcv_buffer_list.
14307 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14308 lpfc_update_rcv_time_stamp(vport);
14309 return dmabuf;
14311 temp_hdr = seq_dmabuf->hbuf.virt;
14312 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14313 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14314 list_del_init(&seq_dmabuf->hbuf.list);
14315 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14316 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14317 lpfc_update_rcv_time_stamp(vport);
14318 return dmabuf;
14320 /* move this sequence to the tail to indicate a young sequence */
14321 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14322 seq_dmabuf->time_stamp = jiffies;
14323 lpfc_update_rcv_time_stamp(vport);
14324 if (list_empty(&seq_dmabuf->dbuf.list)) {
14325 temp_hdr = dmabuf->hbuf.virt;
14326 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14327 return seq_dmabuf;
14329 /* find the correct place in the sequence to insert this frame */
14330 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14331 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14332 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14334 * If the frame's sequence count is greater than the frame on
14335 * the list then insert the frame right after this frame
14337 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14338 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14339 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14340 return seq_dmabuf;
14343 return NULL;
14347 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14348 * @vport: pointer to a vitural port
14349 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14351 * This function tries to abort from the partially assembed sequence, described
14352 * by the information from basic abbort @dmabuf. It checks to see whether such
14353 * partially assembled sequence held by the driver. If so, it shall free up all
14354 * the frames from the partially assembled sequence.
14356 * Return
14357 * true -- if there is matching partially assembled sequence present and all
14358 * the frames freed with the sequence;
14359 * false -- if there is no matching partially assembled sequence present so
14360 * nothing got aborted in the lower layer driver
14362 static bool
14363 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14364 struct hbq_dmabuf *dmabuf)
14366 struct fc_frame_header *new_hdr;
14367 struct fc_frame_header *temp_hdr;
14368 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14369 struct hbq_dmabuf *seq_dmabuf = NULL;
14371 /* Use the hdr_buf to find the sequence that matches this frame */
14372 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14373 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14374 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14375 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14376 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14377 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14378 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14379 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14380 continue;
14381 /* found a pending sequence that matches this frame */
14382 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14383 break;
14386 /* Free up all the frames from the partially assembled sequence */
14387 if (seq_dmabuf) {
14388 list_for_each_entry_safe(d_buf, n_buf,
14389 &seq_dmabuf->dbuf.list, list) {
14390 list_del_init(&d_buf->list);
14391 lpfc_in_buf_free(vport->phba, d_buf);
14393 return true;
14395 return false;
14399 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14400 * @vport: pointer to a vitural port
14401 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14403 * This function tries to abort from the assembed sequence from upper level
14404 * protocol, described by the information from basic abbort @dmabuf. It
14405 * checks to see whether such pending context exists at upper level protocol.
14406 * If so, it shall clean up the pending context.
14408 * Return
14409 * true -- if there is matching pending context of the sequence cleaned
14410 * at ulp;
14411 * false -- if there is no matching pending context of the sequence present
14412 * at ulp.
14414 static bool
14415 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14417 struct lpfc_hba *phba = vport->phba;
14418 int handled;
14420 /* Accepting abort at ulp with SLI4 only */
14421 if (phba->sli_rev < LPFC_SLI_REV4)
14422 return false;
14424 /* Register all caring upper level protocols to attend abort */
14425 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14426 if (handled)
14427 return true;
14429 return false;
14433 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
14434 * @phba: Pointer to HBA context object.
14435 * @cmd_iocbq: pointer to the command iocbq structure.
14436 * @rsp_iocbq: pointer to the response iocbq structure.
14438 * This function handles the sequence abort response iocb command complete
14439 * event. It properly releases the memory allocated to the sequence abort
14440 * accept iocb.
14442 static void
14443 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
14444 struct lpfc_iocbq *cmd_iocbq,
14445 struct lpfc_iocbq *rsp_iocbq)
14447 struct lpfc_nodelist *ndlp;
14449 if (cmd_iocbq) {
14450 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14451 lpfc_nlp_put(ndlp);
14452 lpfc_nlp_not_used(ndlp);
14453 lpfc_sli_release_iocbq(phba, cmd_iocbq);
14456 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14457 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14459 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14460 rsp_iocbq->iocb.ulpStatus,
14461 rsp_iocbq->iocb.un.ulpWord[4]);
14465 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14466 * @phba: Pointer to HBA context object.
14467 * @xri: xri id in transaction.
14469 * This function validates the xri maps to the known range of XRIs allocated an
14470 * used by the driver.
14472 uint16_t
14473 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14474 uint16_t xri)
14476 int i;
14478 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14479 if (xri == phba->sli4_hba.xri_ids[i])
14480 return i;
14482 return NO_XRI;
14486 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
14487 * @phba: Pointer to HBA context object.
14488 * @fc_hdr: pointer to a FC frame header.
14490 * This function sends a basic response to a previous unsol sequence abort
14491 * event after aborting the sequence handling.
14493 static void
14494 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14495 struct fc_frame_header *fc_hdr, bool aborted)
14497 struct lpfc_hba *phba = vport->phba;
14498 struct lpfc_iocbq *ctiocb = NULL;
14499 struct lpfc_nodelist *ndlp;
14500 uint16_t oxid, rxid, xri, lxri;
14501 uint32_t sid, fctl;
14502 IOCB_t *icmd;
14503 int rc;
14505 if (!lpfc_is_link_up(phba))
14506 return;
14508 sid = sli4_sid_from_fc_hdr(fc_hdr);
14509 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
14510 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
14512 ndlp = lpfc_findnode_did(vport, sid);
14513 if (!ndlp) {
14514 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14515 if (!ndlp) {
14516 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14517 "1268 Failed to allocate ndlp for "
14518 "oxid:x%x SID:x%x\n", oxid, sid);
14519 return;
14521 lpfc_nlp_init(vport, ndlp, sid);
14522 /* Put ndlp onto pport node list */
14523 lpfc_enqueue_node(vport, ndlp);
14524 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14525 /* re-setup ndlp without removing from node list */
14526 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14527 if (!ndlp) {
14528 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14529 "3275 Failed to active ndlp found "
14530 "for oxid:x%x SID:x%x\n", oxid, sid);
14531 return;
14535 /* Allocate buffer for rsp iocb */
14536 ctiocb = lpfc_sli_get_iocbq(phba);
14537 if (!ctiocb)
14538 return;
14540 /* Extract the F_CTL field from FC_HDR */
14541 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14543 icmd = &ctiocb->iocb;
14544 icmd->un.xseq64.bdl.bdeSize = 0;
14545 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
14546 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14547 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14548 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14550 /* Fill in the rest of iocb fields */
14551 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14552 icmd->ulpBdeCount = 0;
14553 icmd->ulpLe = 1;
14554 icmd->ulpClass = CLASS3;
14555 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
14556 ctiocb->context1 = lpfc_nlp_get(ndlp);
14558 ctiocb->iocb_cmpl = NULL;
14559 ctiocb->vport = phba->pport;
14560 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
14561 ctiocb->sli4_lxritag = NO_XRI;
14562 ctiocb->sli4_xritag = NO_XRI;
14564 if (fctl & FC_FC_EX_CTX)
14565 /* Exchange responder sent the abort so we
14566 * own the oxid.
14568 xri = oxid;
14569 else
14570 xri = rxid;
14571 lxri = lpfc_sli4_xri_inrange(phba, xri);
14572 if (lxri != NO_XRI)
14573 lpfc_set_rrq_active(phba, ndlp, lxri,
14574 (xri == oxid) ? rxid : oxid, 0);
14575 /* For BA_ABTS from exchange responder, if the logical xri with
14576 * the oxid maps to the FCP XRI range, the port no longer has
14577 * that exchange context, send a BLS_RJT. Override the IOCB for
14578 * a BA_RJT.
14580 if ((fctl & FC_FC_EX_CTX) &&
14581 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14582 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14583 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14584 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14585 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14588 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14589 * the driver no longer has that exchange, send a BLS_RJT. Override
14590 * the IOCB for a BA_RJT.
14592 if (aborted == false) {
14593 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14594 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14595 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14596 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14599 if (fctl & FC_FC_EX_CTX) {
14600 /* ABTS sent by responder to CT exchange, construction
14601 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14602 * field and RX_ID from ABTS for RX_ID field.
14604 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14605 } else {
14606 /* ABTS sent by initiator to CT exchange, construction
14607 * of BA_ACC will need to allocate a new XRI as for the
14608 * XRI_TAG field.
14610 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14612 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
14613 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
14615 /* Xmit CT abts response on exchange <xid> */
14616 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14617 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14618 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14620 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14621 if (rc == IOCB_ERROR) {
14622 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14623 "2925 Failed to issue CT ABTS RSP x%x on "
14624 "xri x%x, Data x%x\n",
14625 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14626 phba->link_state);
14627 lpfc_nlp_put(ndlp);
14628 ctiocb->context1 = NULL;
14629 lpfc_sli_release_iocbq(phba, ctiocb);
14634 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14635 * @vport: Pointer to the vport on which this sequence was received
14636 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14638 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14639 * receive sequence is only partially assembed by the driver, it shall abort
14640 * the partially assembled frames for the sequence. Otherwise, if the
14641 * unsolicited receive sequence has been completely assembled and passed to
14642 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14643 * unsolicited sequence has been aborted. After that, it will issue a basic
14644 * accept to accept the abort.
14646 void
14647 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14648 struct hbq_dmabuf *dmabuf)
14650 struct lpfc_hba *phba = vport->phba;
14651 struct fc_frame_header fc_hdr;
14652 uint32_t fctl;
14653 bool aborted;
14655 /* Make a copy of fc_hdr before the dmabuf being released */
14656 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
14657 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
14659 if (fctl & FC_FC_EX_CTX) {
14660 /* ABTS by responder to exchange, no cleanup needed */
14661 aborted = true;
14662 } else {
14663 /* ABTS by initiator to exchange, need to do cleanup */
14664 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14665 if (aborted == false)
14666 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
14668 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14670 /* Respond with BA_ACC or BA_RJT accordingly */
14671 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
14675 * lpfc_seq_complete - Indicates if a sequence is complete
14676 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14678 * This function checks the sequence, starting with the frame described by
14679 * @dmabuf, to see if all the frames associated with this sequence are present.
14680 * the frames associated with this sequence are linked to the @dmabuf using the
14681 * dbuf list. This function looks for two major things. 1) That the first frame
14682 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14683 * set. 3) That there are no holes in the sequence count. The function will
14684 * return 1 when the sequence is complete, otherwise it will return 0.
14686 static int
14687 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14689 struct fc_frame_header *hdr;
14690 struct lpfc_dmabuf *d_buf;
14691 struct hbq_dmabuf *seq_dmabuf;
14692 uint32_t fctl;
14693 int seq_count = 0;
14695 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14696 /* make sure first fame of sequence has a sequence count of zero */
14697 if (hdr->fh_seq_cnt != seq_count)
14698 return 0;
14699 fctl = (hdr->fh_f_ctl[0] << 16 |
14700 hdr->fh_f_ctl[1] << 8 |
14701 hdr->fh_f_ctl[2]);
14702 /* If last frame of sequence we can return success. */
14703 if (fctl & FC_FC_END_SEQ)
14704 return 1;
14705 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14706 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14707 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14708 /* If there is a hole in the sequence count then fail. */
14709 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
14710 return 0;
14711 fctl = (hdr->fh_f_ctl[0] << 16 |
14712 hdr->fh_f_ctl[1] << 8 |
14713 hdr->fh_f_ctl[2]);
14714 /* If last frame of sequence we can return success. */
14715 if (fctl & FC_FC_END_SEQ)
14716 return 1;
14718 return 0;
14722 * lpfc_prep_seq - Prep sequence for ULP processing
14723 * @vport: Pointer to the vport on which this sequence was received
14724 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14726 * This function takes a sequence, described by a list of frames, and creates
14727 * a list of iocbq structures to describe the sequence. This iocbq list will be
14728 * used to issue to the generic unsolicited sequence handler. This routine
14729 * returns a pointer to the first iocbq in the list. If the function is unable
14730 * to allocate an iocbq then it throw out the received frames that were not
14731 * able to be described and return a pointer to the first iocbq. If unable to
14732 * allocate any iocbqs (including the first) this function will return NULL.
14734 static struct lpfc_iocbq *
14735 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14737 struct hbq_dmabuf *hbq_buf;
14738 struct lpfc_dmabuf *d_buf, *n_buf;
14739 struct lpfc_iocbq *first_iocbq, *iocbq;
14740 struct fc_frame_header *fc_hdr;
14741 uint32_t sid;
14742 uint32_t len, tot_len;
14743 struct ulp_bde64 *pbde;
14745 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14746 /* remove from receive buffer list */
14747 list_del_init(&seq_dmabuf->hbuf.list);
14748 lpfc_update_rcv_time_stamp(vport);
14749 /* get the Remote Port's SID */
14750 sid = sli4_sid_from_fc_hdr(fc_hdr);
14751 tot_len = 0;
14752 /* Get an iocbq struct to fill in. */
14753 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14754 if (first_iocbq) {
14755 /* Initialize the first IOCB. */
14756 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
14757 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14759 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14760 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14761 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14762 first_iocbq->iocb.un.rcvels.parmRo =
14763 sli4_did_from_fc_hdr(fc_hdr);
14764 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14765 } else
14766 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
14767 first_iocbq->iocb.ulpContext = NO_XRI;
14768 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14769 be16_to_cpu(fc_hdr->fh_ox_id);
14770 /* iocbq is prepped for internal consumption. Physical vpi. */
14771 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14772 vport->phba->vpi_ids[vport->vpi];
14773 /* put the first buffer into the first IOCBq */
14774 tot_len = bf_get(lpfc_rcqe_length,
14775 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14777 first_iocbq->context2 = &seq_dmabuf->dbuf;
14778 first_iocbq->context3 = NULL;
14779 first_iocbq->iocb.ulpBdeCount = 1;
14780 if (tot_len > LPFC_DATA_BUF_SIZE)
14781 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14782 LPFC_DATA_BUF_SIZE;
14783 else
14784 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
14786 first_iocbq->iocb.un.rcvels.remoteID = sid;
14788 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14790 iocbq = first_iocbq;
14792 * Each IOCBq can have two Buffers assigned, so go through the list
14793 * of buffers for this sequence and save two buffers in each IOCBq
14795 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14796 if (!iocbq) {
14797 lpfc_in_buf_free(vport->phba, d_buf);
14798 continue;
14800 if (!iocbq->context3) {
14801 iocbq->context3 = d_buf;
14802 iocbq->iocb.ulpBdeCount++;
14803 /* We need to get the size out of the right CQE */
14804 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14805 len = bf_get(lpfc_rcqe_length,
14806 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14807 pbde = (struct ulp_bde64 *)
14808 &iocbq->iocb.unsli3.sli3Words[4];
14809 if (len > LPFC_DATA_BUF_SIZE)
14810 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14811 else
14812 pbde->tus.f.bdeSize = len;
14814 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14815 tot_len += len;
14816 } else {
14817 iocbq = lpfc_sli_get_iocbq(vport->phba);
14818 if (!iocbq) {
14819 if (first_iocbq) {
14820 first_iocbq->iocb.ulpStatus =
14821 IOSTAT_FCP_RSP_ERROR;
14822 first_iocbq->iocb.un.ulpWord[4] =
14823 IOERR_NO_RESOURCES;
14825 lpfc_in_buf_free(vport->phba, d_buf);
14826 continue;
14828 /* We need to get the size out of the right CQE */
14829 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14830 len = bf_get(lpfc_rcqe_length,
14831 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14832 iocbq->context2 = d_buf;
14833 iocbq->context3 = NULL;
14834 iocbq->iocb.ulpBdeCount = 1;
14835 if (len > LPFC_DATA_BUF_SIZE)
14836 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14837 LPFC_DATA_BUF_SIZE;
14838 else
14839 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
14841 tot_len += len;
14842 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14844 iocbq->iocb.un.rcvels.remoteID = sid;
14845 list_add_tail(&iocbq->list, &first_iocbq->list);
14848 return first_iocbq;
14851 static void
14852 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14853 struct hbq_dmabuf *seq_dmabuf)
14855 struct fc_frame_header *fc_hdr;
14856 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14857 struct lpfc_hba *phba = vport->phba;
14859 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14860 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14861 if (!iocbq) {
14862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14863 "2707 Ring %d handler: Failed to allocate "
14864 "iocb Rctl x%x Type x%x received\n",
14865 LPFC_ELS_RING,
14866 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14867 return;
14869 if (!lpfc_complete_unsol_iocb(phba,
14870 &phba->sli.ring[LPFC_ELS_RING],
14871 iocbq, fc_hdr->fh_r_ctl,
14872 fc_hdr->fh_type))
14873 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14874 "2540 Ring %d handler: unexpected Rctl "
14875 "x%x Type x%x received\n",
14876 LPFC_ELS_RING,
14877 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14879 /* Free iocb created in lpfc_prep_seq */
14880 list_for_each_entry_safe(curr_iocb, next_iocb,
14881 &iocbq->list, list) {
14882 list_del_init(&curr_iocb->list);
14883 lpfc_sli_release_iocbq(phba, curr_iocb);
14885 lpfc_sli_release_iocbq(phba, iocbq);
14889 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14890 * @phba: Pointer to HBA context object.
14892 * This function is called with no lock held. This function processes all
14893 * the received buffers and gives it to upper layers when a received buffer
14894 * indicates that it is the final frame in the sequence. The interrupt
14895 * service routine processes received buffers at interrupt contexts and adds
14896 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14897 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14898 * appropriate receive function when the final frame in a sequence is received.
14900 void
14901 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14902 struct hbq_dmabuf *dmabuf)
14904 struct hbq_dmabuf *seq_dmabuf;
14905 struct fc_frame_header *fc_hdr;
14906 struct lpfc_vport *vport;
14907 uint32_t fcfi;
14908 uint32_t did;
14910 /* Process each received buffer */
14911 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14912 /* check to see if this a valid type of frame */
14913 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14914 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14915 return;
14917 if ((bf_get(lpfc_cqe_code,
14918 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14919 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14920 &dmabuf->cq_event.cqe.rcqe_cmpl);
14921 else
14922 fcfi = bf_get(lpfc_rcqe_fcf_id,
14923 &dmabuf->cq_event.cqe.rcqe_cmpl);
14925 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
14926 if (!vport) {
14927 /* throw out the frame */
14928 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14929 return;
14932 /* d_id this frame is directed to */
14933 did = sli4_did_from_fc_hdr(fc_hdr);
14935 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14936 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14937 (did != Fabric_DID)) {
14939 * Throw out the frame if we are not pt2pt.
14940 * The pt2pt protocol allows for discovery frames
14941 * to be received without a registered VPI.
14943 if (!(vport->fc_flag & FC_PT2PT) ||
14944 (phba->link_state == LPFC_HBA_READY)) {
14945 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14946 return;
14950 /* Handle the basic abort sequence (BA_ABTS) event */
14951 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14952 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14953 return;
14956 /* Link this frame */
14957 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14958 if (!seq_dmabuf) {
14959 /* unable to add frame to vport - throw it out */
14960 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14961 return;
14963 /* If not last frame in sequence continue processing frames. */
14964 if (!lpfc_seq_complete(seq_dmabuf))
14965 return;
14967 /* Send the complete sequence to the upper layer protocol */
14968 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
14972 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14973 * @phba: pointer to lpfc hba data structure.
14975 * This routine is invoked to post rpi header templates to the
14976 * HBA consistent with the SLI-4 interface spec. This routine
14977 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14978 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14980 * This routine does not require any locks. It's usage is expected
14981 * to be driver load or reset recovery when the driver is
14982 * sequential.
14984 * Return codes
14985 * 0 - successful
14986 * -EIO - The mailbox failed to complete successfully.
14987 * When this error occurs, the driver is not guaranteed
14988 * to have any rpi regions posted to the device and
14989 * must either attempt to repost the regions or take a
14990 * fatal error.
14993 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14995 struct lpfc_rpi_hdr *rpi_page;
14996 uint32_t rc = 0;
14997 uint16_t lrpi = 0;
14999 /* SLI4 ports that support extents do not require RPI headers. */
15000 if (!phba->sli4_hba.rpi_hdrs_in_use)
15001 goto exit;
15002 if (phba->sli4_hba.extents_in_use)
15003 return -EIO;
15005 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
15007 * Assign the rpi headers a physical rpi only if the driver
15008 * has not initialized those resources. A port reset only
15009 * needs the headers posted.
15011 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15012 LPFC_RPI_RSRC_RDY)
15013 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15015 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15016 if (rc != MBX_SUCCESS) {
15017 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15018 "2008 Error %d posting all rpi "
15019 "headers\n", rc);
15020 rc = -EIO;
15021 break;
15025 exit:
15026 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15027 LPFC_RPI_RSRC_RDY);
15028 return rc;
15032 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15033 * @phba: pointer to lpfc hba data structure.
15034 * @rpi_page: pointer to the rpi memory region.
15036 * This routine is invoked to post a single rpi header to the
15037 * HBA consistent with the SLI-4 interface spec. This memory region
15038 * maps up to 64 rpi context regions.
15040 * Return codes
15041 * 0 - successful
15042 * -ENOMEM - No available memory
15043 * -EIO - The mailbox failed to complete successfully.
15046 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15048 LPFC_MBOXQ_t *mboxq;
15049 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15050 uint32_t rc = 0;
15051 uint32_t shdr_status, shdr_add_status;
15052 union lpfc_sli4_cfg_shdr *shdr;
15054 /* SLI4 ports that support extents do not require RPI headers. */
15055 if (!phba->sli4_hba.rpi_hdrs_in_use)
15056 return rc;
15057 if (phba->sli4_hba.extents_in_use)
15058 return -EIO;
15060 /* The port is notified of the header region via a mailbox command. */
15061 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15062 if (!mboxq) {
15063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15064 "2001 Unable to allocate memory for issuing "
15065 "SLI_CONFIG_SPECIAL mailbox command\n");
15066 return -ENOMEM;
15069 /* Post all rpi memory regions to the port. */
15070 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
15071 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15072 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15073 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
15074 sizeof(struct lpfc_sli4_cfg_mhdr),
15075 LPFC_SLI4_MBX_EMBED);
15078 /* Post the physical rpi to the port for this rpi header. */
15079 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15080 rpi_page->start_rpi);
15081 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15082 hdr_tmpl, rpi_page->page_count);
15084 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15085 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
15086 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15087 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15088 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15089 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15090 if (rc != MBX_TIMEOUT)
15091 mempool_free(mboxq, phba->mbox_mem_pool);
15092 if (shdr_status || shdr_add_status || rc) {
15093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15094 "2514 POST_RPI_HDR mailbox failed with "
15095 "status x%x add_status x%x, mbx status x%x\n",
15096 shdr_status, shdr_add_status, rc);
15097 rc = -ENXIO;
15099 return rc;
15103 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15104 * @phba: pointer to lpfc hba data structure.
15106 * This routine is invoked to post rpi header templates to the
15107 * HBA consistent with the SLI-4 interface spec. This routine
15108 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15109 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15111 * Returns
15112 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15113 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15116 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15118 unsigned long rpi;
15119 uint16_t max_rpi, rpi_limit;
15120 uint16_t rpi_remaining, lrpi = 0;
15121 struct lpfc_rpi_hdr *rpi_hdr;
15123 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15124 rpi_limit = phba->sli4_hba.next_rpi;
15127 * Fetch the next logical rpi. Because this index is logical,
15128 * the driver starts at 0 each time.
15130 spin_lock_irq(&phba->hbalock);
15131 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15132 if (rpi >= rpi_limit)
15133 rpi = LPFC_RPI_ALLOC_ERROR;
15134 else {
15135 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15136 phba->sli4_hba.max_cfg_param.rpi_used++;
15137 phba->sli4_hba.rpi_count++;
15141 * Don't try to allocate more rpi header regions if the device limit
15142 * has been exhausted.
15144 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15145 (phba->sli4_hba.rpi_count >= max_rpi)) {
15146 spin_unlock_irq(&phba->hbalock);
15147 return rpi;
15151 * RPI header postings are not required for SLI4 ports capable of
15152 * extents.
15154 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15155 spin_unlock_irq(&phba->hbalock);
15156 return rpi;
15160 * If the driver is running low on rpi resources, allocate another
15161 * page now. Note that the next_rpi value is used because
15162 * it represents how many are actually in use whereas max_rpi notes
15163 * how many are supported max by the device.
15165 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
15166 spin_unlock_irq(&phba->hbalock);
15167 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15168 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15169 if (!rpi_hdr) {
15170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15171 "2002 Error Could not grow rpi "
15172 "count\n");
15173 } else {
15174 lrpi = rpi_hdr->start_rpi;
15175 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15176 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15180 return rpi;
15184 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15185 * @phba: pointer to lpfc hba data structure.
15187 * This routine is invoked to release an rpi to the pool of
15188 * available rpis maintained by the driver.
15190 void
15191 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15193 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15194 phba->sli4_hba.rpi_count--;
15195 phba->sli4_hba.max_cfg_param.rpi_used--;
15200 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15201 * @phba: pointer to lpfc hba data structure.
15203 * This routine is invoked to release an rpi to the pool of
15204 * available rpis maintained by the driver.
15206 void
15207 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15209 spin_lock_irq(&phba->hbalock);
15210 __lpfc_sli4_free_rpi(phba, rpi);
15211 spin_unlock_irq(&phba->hbalock);
15215 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15216 * @phba: pointer to lpfc hba data structure.
15218 * This routine is invoked to remove the memory region that
15219 * provided rpi via a bitmask.
15221 void
15222 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15224 kfree(phba->sli4_hba.rpi_bmask);
15225 kfree(phba->sli4_hba.rpi_ids);
15226 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
15230 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15231 * @phba: pointer to lpfc hba data structure.
15233 * This routine is invoked to remove the memory region that
15234 * provided rpi via a bitmask.
15237 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15238 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
15240 LPFC_MBOXQ_t *mboxq;
15241 struct lpfc_hba *phba = ndlp->phba;
15242 int rc;
15244 /* The port is notified of the header region via a mailbox command. */
15245 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15246 if (!mboxq)
15247 return -ENOMEM;
15249 /* Post all rpi memory regions to the port. */
15250 lpfc_resume_rpi(mboxq, ndlp);
15251 if (cmpl) {
15252 mboxq->mbox_cmpl = cmpl;
15253 mboxq->context1 = arg;
15254 mboxq->context2 = ndlp;
15255 } else
15256 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15257 mboxq->vport = ndlp->vport;
15258 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15259 if (rc == MBX_NOT_FINISHED) {
15260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15261 "2010 Resume RPI Mailbox failed "
15262 "status %d, mbxStatus x%x\n", rc,
15263 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15264 mempool_free(mboxq, phba->mbox_mem_pool);
15265 return -EIO;
15267 return 0;
15271 * lpfc_sli4_init_vpi - Initialize a vpi with the port
15272 * @vport: Pointer to the vport for which the vpi is being initialized
15274 * This routine is invoked to activate a vpi with the port.
15276 * Returns:
15277 * 0 success
15278 * -Evalue otherwise
15281 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
15283 LPFC_MBOXQ_t *mboxq;
15284 int rc = 0;
15285 int retval = MBX_SUCCESS;
15286 uint32_t mbox_tmo;
15287 struct lpfc_hba *phba = vport->phba;
15288 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15289 if (!mboxq)
15290 return -ENOMEM;
15291 lpfc_init_vpi(phba, mboxq, vport->vpi);
15292 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
15293 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
15294 if (rc != MBX_SUCCESS) {
15295 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
15296 "2022 INIT VPI Mailbox failed "
15297 "status %d, mbxStatus x%x\n", rc,
15298 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15299 retval = -EIO;
15301 if (rc != MBX_TIMEOUT)
15302 mempool_free(mboxq, vport->phba->mbox_mem_pool);
15304 return retval;
15308 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15309 * @phba: pointer to lpfc hba data structure.
15310 * @mboxq: Pointer to mailbox object.
15312 * This routine is invoked to manually add a single FCF record. The caller
15313 * must pass a completely initialized FCF_Record. This routine takes
15314 * care of the nonembedded mailbox operations.
15316 static void
15317 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15319 void *virt_addr;
15320 union lpfc_sli4_cfg_shdr *shdr;
15321 uint32_t shdr_status, shdr_add_status;
15323 virt_addr = mboxq->sge_array->addr[0];
15324 /* The IOCTL status is embedded in the mailbox subheader. */
15325 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15326 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15327 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15329 if ((shdr_status || shdr_add_status) &&
15330 (shdr_status != STATUS_FCF_IN_USE))
15331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15332 "2558 ADD_FCF_RECORD mailbox failed with "
15333 "status x%x add_status x%x\n",
15334 shdr_status, shdr_add_status);
15336 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15340 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15341 * @phba: pointer to lpfc hba data structure.
15342 * @fcf_record: pointer to the initialized fcf record to add.
15344 * This routine is invoked to manually add a single FCF record. The caller
15345 * must pass a completely initialized FCF_Record. This routine takes
15346 * care of the nonembedded mailbox operations.
15349 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15351 int rc = 0;
15352 LPFC_MBOXQ_t *mboxq;
15353 uint8_t *bytep;
15354 void *virt_addr;
15355 dma_addr_t phys_addr;
15356 struct lpfc_mbx_sge sge;
15357 uint32_t alloc_len, req_len;
15358 uint32_t fcfindex;
15360 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15361 if (!mboxq) {
15362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15363 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15364 return -ENOMEM;
15367 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15368 sizeof(uint32_t);
15370 /* Allocate DMA memory and set up the non-embedded mailbox command */
15371 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15372 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15373 req_len, LPFC_SLI4_MBX_NEMBED);
15374 if (alloc_len < req_len) {
15375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15376 "2523 Allocated DMA memory size (x%x) is "
15377 "less than the requested DMA memory "
15378 "size (x%x)\n", alloc_len, req_len);
15379 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15380 return -ENOMEM;
15384 * Get the first SGE entry from the non-embedded DMA memory. This
15385 * routine only uses a single SGE.
15387 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15388 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
15389 virt_addr = mboxq->sge_array->addr[0];
15391 * Configure the FCF record for FCFI 0. This is the driver's
15392 * hardcoded default and gets used in nonFIP mode.
15394 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15395 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15396 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15399 * Copy the fcf_index and the FCF Record Data. The data starts after
15400 * the FCoE header plus word10. The data copy needs to be endian
15401 * correct.
15403 bytep += sizeof(uint32_t);
15404 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15405 mboxq->vport = phba->pport;
15406 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15407 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15408 if (rc == MBX_NOT_FINISHED) {
15409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15410 "2515 ADD_FCF_RECORD mailbox failed with "
15411 "status 0x%x\n", rc);
15412 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15413 rc = -EIO;
15414 } else
15415 rc = 0;
15417 return rc;
15421 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15422 * @phba: pointer to lpfc hba data structure.
15423 * @fcf_record: pointer to the fcf record to write the default data.
15424 * @fcf_index: FCF table entry index.
15426 * This routine is invoked to build the driver's default FCF record. The
15427 * values used are hardcoded. This routine handles memory initialization.
15430 void
15431 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15432 struct fcf_record *fcf_record,
15433 uint16_t fcf_index)
15435 memset(fcf_record, 0, sizeof(struct fcf_record));
15436 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15437 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15438 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15439 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15440 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15441 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15442 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15443 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15444 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15445 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15446 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15447 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15448 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
15449 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
15450 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15451 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15452 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15453 /* Set the VLAN bit map */
15454 if (phba->valid_vlan) {
15455 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15456 = 1 << (phba->vlan_id % 8);
15461 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
15462 * @phba: pointer to lpfc hba data structure.
15463 * @fcf_index: FCF table entry offset.
15465 * This routine is invoked to scan the entire FCF table by reading FCF
15466 * record and processing it one at a time starting from the @fcf_index
15467 * for initial FCF discovery or fast FCF failover rediscovery.
15469 * Return 0 if the mailbox command is submitted successfully, none 0
15470 * otherwise.
15473 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15475 int rc = 0, error;
15476 LPFC_MBOXQ_t *mboxq;
15478 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
15479 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
15480 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15481 if (!mboxq) {
15482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15483 "2000 Failed to allocate mbox for "
15484 "READ_FCF cmd\n");
15485 error = -ENOMEM;
15486 goto fail_fcf_scan;
15488 /* Construct the read FCF record mailbox command */
15489 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15490 if (rc) {
15491 error = -EINVAL;
15492 goto fail_fcf_scan;
15494 /* Issue the mailbox command asynchronously */
15495 mboxq->vport = phba->pport;
15496 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
15498 spin_lock_irq(&phba->hbalock);
15499 phba->hba_flag |= FCF_TS_INPROG;
15500 spin_unlock_irq(&phba->hbalock);
15502 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15503 if (rc == MBX_NOT_FINISHED)
15504 error = -EIO;
15505 else {
15506 /* Reset eligible FCF count for new scan */
15507 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
15508 phba->fcf.eligible_fcf_cnt = 0;
15509 error = 0;
15511 fail_fcf_scan:
15512 if (error) {
15513 if (mboxq)
15514 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15515 /* FCF scan failed, clear FCF_TS_INPROG flag */
15516 spin_lock_irq(&phba->hbalock);
15517 phba->hba_flag &= ~FCF_TS_INPROG;
15518 spin_unlock_irq(&phba->hbalock);
15520 return error;
15524 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
15525 * @phba: pointer to lpfc hba data structure.
15526 * @fcf_index: FCF table entry offset.
15528 * This routine is invoked to read an FCF record indicated by @fcf_index
15529 * and to use it for FLOGI roundrobin FCF failover.
15531 * Return 0 if the mailbox command is submitted successfully, none 0
15532 * otherwise.
15535 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15537 int rc = 0, error;
15538 LPFC_MBOXQ_t *mboxq;
15540 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15541 if (!mboxq) {
15542 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15543 "2763 Failed to allocate mbox for "
15544 "READ_FCF cmd\n");
15545 error = -ENOMEM;
15546 goto fail_fcf_read;
15548 /* Construct the read FCF record mailbox command */
15549 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15550 if (rc) {
15551 error = -EINVAL;
15552 goto fail_fcf_read;
15554 /* Issue the mailbox command asynchronously */
15555 mboxq->vport = phba->pport;
15556 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15557 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15558 if (rc == MBX_NOT_FINISHED)
15559 error = -EIO;
15560 else
15561 error = 0;
15563 fail_fcf_read:
15564 if (error && mboxq)
15565 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15566 return error;
15570 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15571 * @phba: pointer to lpfc hba data structure.
15572 * @fcf_index: FCF table entry offset.
15574 * This routine is invoked to read an FCF record indicated by @fcf_index to
15575 * determine whether it's eligible for FLOGI roundrobin failover list.
15577 * Return 0 if the mailbox command is submitted successfully, none 0
15578 * otherwise.
15581 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15583 int rc = 0, error;
15584 LPFC_MBOXQ_t *mboxq;
15586 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15587 if (!mboxq) {
15588 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15589 "2758 Failed to allocate mbox for "
15590 "READ_FCF cmd\n");
15591 error = -ENOMEM;
15592 goto fail_fcf_read;
15594 /* Construct the read FCF record mailbox command */
15595 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15596 if (rc) {
15597 error = -EINVAL;
15598 goto fail_fcf_read;
15600 /* Issue the mailbox command asynchronously */
15601 mboxq->vport = phba->pport;
15602 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15603 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15604 if (rc == MBX_NOT_FINISHED)
15605 error = -EIO;
15606 else
15607 error = 0;
15609 fail_fcf_read:
15610 if (error && mboxq)
15611 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15612 return error;
15616 * lpfc_check_next_fcf_pri
15617 * phba pointer to the lpfc_hba struct for this port.
15618 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15619 * routine when the rr_bmask is empty. The FCF indecies are put into the
15620 * rr_bmask based on their priority level. Starting from the highest priority
15621 * to the lowest. The most likely FCF candidate will be in the highest
15622 * priority group. When this routine is called it searches the fcf_pri list for
15623 * next lowest priority group and repopulates the rr_bmask with only those
15624 * fcf_indexes.
15625 * returns:
15626 * 1=success 0=failure
15629 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15631 uint16_t next_fcf_pri;
15632 uint16_t last_index;
15633 struct lpfc_fcf_pri *fcf_pri;
15634 int rc;
15635 int ret = 0;
15637 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15638 LPFC_SLI4_FCF_TBL_INDX_MAX);
15639 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15640 "3060 Last IDX %d\n", last_index);
15642 /* Verify the priority list has 2 or more entries */
15643 spin_lock_irq(&phba->hbalock);
15644 if (list_empty(&phba->fcf.fcf_pri_list) ||
15645 list_is_singular(&phba->fcf.fcf_pri_list)) {
15646 spin_unlock_irq(&phba->hbalock);
15647 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15648 "3061 Last IDX %d\n", last_index);
15649 return 0; /* Empty rr list */
15651 spin_unlock_irq(&phba->hbalock);
15653 next_fcf_pri = 0;
15655 * Clear the rr_bmask and set all of the bits that are at this
15656 * priority.
15658 memset(phba->fcf.fcf_rr_bmask, 0,
15659 sizeof(*phba->fcf.fcf_rr_bmask));
15660 spin_lock_irq(&phba->hbalock);
15661 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15662 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15663 continue;
15665 * the 1st priority that has not FLOGI failed
15666 * will be the highest.
15668 if (!next_fcf_pri)
15669 next_fcf_pri = fcf_pri->fcf_rec.priority;
15670 spin_unlock_irq(&phba->hbalock);
15671 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15672 rc = lpfc_sli4_fcf_rr_index_set(phba,
15673 fcf_pri->fcf_rec.fcf_index);
15674 if (rc)
15675 return 0;
15677 spin_lock_irq(&phba->hbalock);
15680 * if next_fcf_pri was not set above and the list is not empty then
15681 * we have failed flogis on all of them. So reset flogi failed
15682 * and start at the beginning.
15684 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15685 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15686 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15688 * the 1st priority that has not FLOGI failed
15689 * will be the highest.
15691 if (!next_fcf_pri)
15692 next_fcf_pri = fcf_pri->fcf_rec.priority;
15693 spin_unlock_irq(&phba->hbalock);
15694 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15695 rc = lpfc_sli4_fcf_rr_index_set(phba,
15696 fcf_pri->fcf_rec.fcf_index);
15697 if (rc)
15698 return 0;
15700 spin_lock_irq(&phba->hbalock);
15702 } else
15703 ret = 1;
15704 spin_unlock_irq(&phba->hbalock);
15706 return ret;
15709 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15710 * @phba: pointer to lpfc hba data structure.
15712 * This routine is to get the next eligible FCF record index in a round
15713 * robin fashion. If the next eligible FCF record index equals to the
15714 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
15715 * shall be returned, otherwise, the next eligible FCF record's index
15716 * shall be returned.
15718 uint16_t
15719 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15721 uint16_t next_fcf_index;
15723 initial_priority:
15724 /* Search start from next bit of currently registered FCF index */
15725 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15727 next_priority:
15728 /* Determine the next fcf index to check */
15729 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
15730 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15731 LPFC_SLI4_FCF_TBL_INDX_MAX,
15732 next_fcf_index);
15734 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
15735 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15737 * If we have wrapped then we need to clear the bits that
15738 * have been tested so that we can detect when we should
15739 * change the priority level.
15741 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15742 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
15746 /* Check roundrobin failover list empty condition */
15747 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15748 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15750 * If next fcf index is not found check if there are lower
15751 * Priority level fcf's in the fcf_priority list.
15752 * Set up the rr_bmask with all of the avaiable fcf bits
15753 * at that level and continue the selection process.
15755 if (lpfc_check_next_fcf_pri_level(phba))
15756 goto initial_priority;
15757 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15758 "2844 No roundrobin failover FCF available\n");
15759 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15760 return LPFC_FCOE_FCF_NEXT_NONE;
15761 else {
15762 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15763 "3063 Only FCF available idx %d, flag %x\n",
15764 next_fcf_index,
15765 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15766 return next_fcf_index;
15770 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15771 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15772 LPFC_FCF_FLOGI_FAILED)
15773 goto next_priority;
15775 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15776 "2845 Get next roundrobin failover FCF (x%x)\n",
15777 next_fcf_index);
15779 return next_fcf_index;
15783 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15784 * @phba: pointer to lpfc hba data structure.
15786 * This routine sets the FCF record index in to the eligible bmask for
15787 * roundrobin failover search. It checks to make sure that the index
15788 * does not go beyond the range of the driver allocated bmask dimension
15789 * before setting the bit.
15791 * Returns 0 if the index bit successfully set, otherwise, it returns
15792 * -EINVAL.
15795 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15797 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15798 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15799 "2610 FCF (x%x) reached driver's book "
15800 "keeping dimension:x%x\n",
15801 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15802 return -EINVAL;
15804 /* Set the eligible FCF record index bmask */
15805 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15807 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15808 "2790 Set FCF (x%x) to roundrobin FCF failover "
15809 "bmask\n", fcf_index);
15811 return 0;
15815 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
15816 * @phba: pointer to lpfc hba data structure.
15818 * This routine clears the FCF record index from the eligible bmask for
15819 * roundrobin failover search. It checks to make sure that the index
15820 * does not go beyond the range of the driver allocated bmask dimension
15821 * before clearing the bit.
15823 void
15824 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15826 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
15827 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15828 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15829 "2762 FCF (x%x) reached driver's book "
15830 "keeping dimension:x%x\n",
15831 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15832 return;
15834 /* Clear the eligible FCF record index bmask */
15835 spin_lock_irq(&phba->hbalock);
15836 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
15837 list) {
15838 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15839 list_del_init(&fcf_pri->list);
15840 break;
15843 spin_unlock_irq(&phba->hbalock);
15844 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15846 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15847 "2791 Clear FCF (x%x) from roundrobin failover "
15848 "bmask\n", fcf_index);
15852 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15853 * @phba: pointer to lpfc hba data structure.
15855 * This routine is the completion routine for the rediscover FCF table mailbox
15856 * command. If the mailbox command returned failure, it will try to stop the
15857 * FCF rediscover wait timer.
15859 void
15860 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15862 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15863 uint32_t shdr_status, shdr_add_status;
15865 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15867 shdr_status = bf_get(lpfc_mbox_hdr_status,
15868 &redisc_fcf->header.cfg_shdr.response);
15869 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15870 &redisc_fcf->header.cfg_shdr.response);
15871 if (shdr_status || shdr_add_status) {
15872 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15873 "2746 Requesting for FCF rediscovery failed "
15874 "status x%x add_status x%x\n",
15875 shdr_status, shdr_add_status);
15876 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
15877 spin_lock_irq(&phba->hbalock);
15878 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
15879 spin_unlock_irq(&phba->hbalock);
15881 * CVL event triggered FCF rediscover request failed,
15882 * last resort to re-try current registered FCF entry.
15884 lpfc_retry_pport_discovery(phba);
15885 } else {
15886 spin_lock_irq(&phba->hbalock);
15887 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
15888 spin_unlock_irq(&phba->hbalock);
15890 * DEAD FCF event triggered FCF rediscover request
15891 * failed, last resort to fail over as a link down
15892 * to FCF registration.
15894 lpfc_sli4_fcf_dead_failthrough(phba);
15896 } else {
15897 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15898 "2775 Start FCF rediscover quiescent timer\n");
15900 * Start FCF rediscovery wait timer for pending FCF
15901 * before rescan FCF record table.
15903 lpfc_fcf_redisc_wait_start_timer(phba);
15906 mempool_free(mbox, phba->mbox_mem_pool);
15910 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
15911 * @phba: pointer to lpfc hba data structure.
15913 * This routine is invoked to request for rediscovery of the entire FCF table
15914 * by the port.
15917 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15919 LPFC_MBOXQ_t *mbox;
15920 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15921 int rc, length;
15923 /* Cancel retry delay timers to all vports before FCF rediscover */
15924 lpfc_cancel_all_vport_retry_delay_timer(phba);
15926 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15927 if (!mbox) {
15928 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15929 "2745 Failed to allocate mbox for "
15930 "requesting FCF rediscover.\n");
15931 return -ENOMEM;
15934 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15935 sizeof(struct lpfc_sli4_cfg_mhdr));
15936 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15937 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15938 length, LPFC_SLI4_MBX_EMBED);
15940 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15941 /* Set count to 0 for invalidating the entire FCF database */
15942 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15944 /* Issue the mailbox command asynchronously */
15945 mbox->vport = phba->pport;
15946 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15947 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15949 if (rc == MBX_NOT_FINISHED) {
15950 mempool_free(mbox, phba->mbox_mem_pool);
15951 return -EIO;
15953 return 0;
15957 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15958 * @phba: pointer to lpfc hba data structure.
15960 * This function is the failover routine as a last resort to the FCF DEAD
15961 * event when driver failed to perform fast FCF failover.
15963 void
15964 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15966 uint32_t link_state;
15969 * Last resort as FCF DEAD event failover will treat this as
15970 * a link down, but save the link state because we don't want
15971 * it to be changed to Link Down unless it is already down.
15973 link_state = phba->link_state;
15974 lpfc_linkdown(phba);
15975 phba->link_state = link_state;
15977 /* Unregister FCF if no devices connected to it */
15978 lpfc_unregister_unused_fcf(phba);
15982 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
15983 * @phba: pointer to lpfc hba data structure.
15984 * @rgn23_data: pointer to configure region 23 data.
15986 * This function gets SLI3 port configure region 23 data through memory dump
15987 * mailbox command. When it successfully retrieves data, the size of the data
15988 * will be returned, otherwise, 0 will be returned.
15990 static uint32_t
15991 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15993 LPFC_MBOXQ_t *pmb = NULL;
15994 MAILBOX_t *mb;
15995 uint32_t offset = 0;
15996 int rc;
15998 if (!rgn23_data)
15999 return 0;
16001 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16002 if (!pmb) {
16003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16004 "2600 failed to allocate mailbox memory\n");
16005 return 0;
16007 mb = &pmb->u.mb;
16009 do {
16010 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16011 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16013 if (rc != MBX_SUCCESS) {
16014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16015 "2601 failed to read config "
16016 "region 23, rc 0x%x Status 0x%x\n",
16017 rc, mb->mbxStatus);
16018 mb->un.varDmp.word_cnt = 0;
16021 * dump mem may return a zero when finished or we got a
16022 * mailbox error, either way we are done.
16024 if (mb->un.varDmp.word_cnt == 0)
16025 break;
16026 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16027 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16029 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
16030 rgn23_data + offset,
16031 mb->un.varDmp.word_cnt);
16032 offset += mb->un.varDmp.word_cnt;
16033 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16035 mempool_free(pmb, phba->mbox_mem_pool);
16036 return offset;
16040 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16041 * @phba: pointer to lpfc hba data structure.
16042 * @rgn23_data: pointer to configure region 23 data.
16044 * This function gets SLI4 port configure region 23 data through memory dump
16045 * mailbox command. When it successfully retrieves data, the size of the data
16046 * will be returned, otherwise, 0 will be returned.
16048 static uint32_t
16049 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16051 LPFC_MBOXQ_t *mboxq = NULL;
16052 struct lpfc_dmabuf *mp = NULL;
16053 struct lpfc_mqe *mqe;
16054 uint32_t data_length = 0;
16055 int rc;
16057 if (!rgn23_data)
16058 return 0;
16060 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16061 if (!mboxq) {
16062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16063 "3105 failed to allocate mailbox memory\n");
16064 return 0;
16067 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16068 goto out;
16069 mqe = &mboxq->u.mqe;
16070 mp = (struct lpfc_dmabuf *) mboxq->context1;
16071 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16072 if (rc)
16073 goto out;
16074 data_length = mqe->un.mb_words[5];
16075 if (data_length == 0)
16076 goto out;
16077 if (data_length > DMP_RGN23_SIZE) {
16078 data_length = 0;
16079 goto out;
16081 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16082 out:
16083 mempool_free(mboxq, phba->mbox_mem_pool);
16084 if (mp) {
16085 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16086 kfree(mp);
16088 return data_length;
16092 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16093 * @phba: pointer to lpfc hba data structure.
16095 * This function read region 23 and parse TLV for port status to
16096 * decide if the user disaled the port. If the TLV indicates the
16097 * port is disabled, the hba_flag is set accordingly.
16099 void
16100 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16102 uint8_t *rgn23_data = NULL;
16103 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16104 uint32_t offset = 0;
16106 /* Get adapter Region 23 data */
16107 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16108 if (!rgn23_data)
16109 goto out;
16111 if (phba->sli_rev < LPFC_SLI_REV4)
16112 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16113 else {
16114 if_type = bf_get(lpfc_sli_intf_if_type,
16115 &phba->sli4_hba.sli_intf);
16116 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16117 goto out;
16118 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16121 if (!data_size)
16122 goto out;
16124 /* Check the region signature first */
16125 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16127 "2619 Config region 23 has bad signature\n");
16128 goto out;
16130 offset += 4;
16132 /* Check the data structure version */
16133 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16135 "2620 Config region 23 has bad version\n");
16136 goto out;
16138 offset += 4;
16140 /* Parse TLV entries in the region */
16141 while (offset < data_size) {
16142 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16143 break;
16145 * If the TLV is not driver specific TLV or driver id is
16146 * not linux driver id, skip the record.
16148 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16149 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16150 (rgn23_data[offset + 3] != 0)) {
16151 offset += rgn23_data[offset + 1] * 4 + 4;
16152 continue;
16155 /* Driver found a driver specific TLV in the config region */
16156 sub_tlv_len = rgn23_data[offset + 1] * 4;
16157 offset += 4;
16158 tlv_offset = 0;
16161 * Search for configured port state sub-TLV.
16163 while ((offset < data_size) &&
16164 (tlv_offset < sub_tlv_len)) {
16165 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16166 offset += 4;
16167 tlv_offset += 4;
16168 break;
16170 if (rgn23_data[offset] != PORT_STE_TYPE) {
16171 offset += rgn23_data[offset + 1] * 4 + 4;
16172 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16173 continue;
16176 /* This HBA contains PORT_STE configured */
16177 if (!rgn23_data[offset + 2])
16178 phba->hba_flag |= LINK_DISABLED;
16180 goto out;
16184 out:
16185 kfree(rgn23_data);
16186 return;
16190 * lpfc_wr_object - write an object to the firmware
16191 * @phba: HBA structure that indicates port to create a queue on.
16192 * @dmabuf_list: list of dmabufs to write to the port.
16193 * @size: the total byte value of the objects to write to the port.
16194 * @offset: the current offset to be used to start the transfer.
16196 * This routine will create a wr_object mailbox command to send to the port.
16197 * the mailbox command will be constructed using the dma buffers described in
16198 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16199 * BDEs that the imbedded mailbox can support. The @offset variable will be
16200 * used to indicate the starting offset of the transfer and will also return
16201 * the offset after the write object mailbox has completed. @size is used to
16202 * determine the end of the object and whether the eof bit should be set.
16204 * Return 0 is successful and offset will contain the the new offset to use
16205 * for the next write.
16206 * Return negative value for error cases.
16209 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16210 uint32_t size, uint32_t *offset)
16212 struct lpfc_mbx_wr_object *wr_object;
16213 LPFC_MBOXQ_t *mbox;
16214 int rc = 0, i = 0;
16215 uint32_t shdr_status, shdr_add_status;
16216 uint32_t mbox_tmo;
16217 union lpfc_sli4_cfg_shdr *shdr;
16218 struct lpfc_dmabuf *dmabuf;
16219 uint32_t written = 0;
16221 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16222 if (!mbox)
16223 return -ENOMEM;
16225 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16226 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16227 sizeof(struct lpfc_mbx_wr_object) -
16228 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16230 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16231 wr_object->u.request.write_offset = *offset;
16232 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16233 wr_object->u.request.object_name[0] =
16234 cpu_to_le32(wr_object->u.request.object_name[0]);
16235 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16236 list_for_each_entry(dmabuf, dmabuf_list, list) {
16237 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16238 break;
16239 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16240 wr_object->u.request.bde[i].addrHigh =
16241 putPaddrHigh(dmabuf->phys);
16242 if (written + SLI4_PAGE_SIZE >= size) {
16243 wr_object->u.request.bde[i].tus.f.bdeSize =
16244 (size - written);
16245 written += (size - written);
16246 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16247 } else {
16248 wr_object->u.request.bde[i].tus.f.bdeSize =
16249 SLI4_PAGE_SIZE;
16250 written += SLI4_PAGE_SIZE;
16252 i++;
16254 wr_object->u.request.bde_count = i;
16255 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16256 if (!phba->sli4_hba.intr_enable)
16257 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16258 else {
16259 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16260 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16262 /* The IOCTL status is embedded in the mailbox subheader. */
16263 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16264 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16265 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16266 if (rc != MBX_TIMEOUT)
16267 mempool_free(mbox, phba->mbox_mem_pool);
16268 if (shdr_status || shdr_add_status || rc) {
16269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16270 "3025 Write Object mailbox failed with "
16271 "status x%x add_status x%x, mbx status x%x\n",
16272 shdr_status, shdr_add_status, rc);
16273 rc = -ENXIO;
16274 } else
16275 *offset += wr_object->u.response.actual_write_length;
16276 return rc;
16280 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16281 * @vport: pointer to vport data structure.
16283 * This function iterate through the mailboxq and clean up all REG_LOGIN
16284 * and REG_VPI mailbox commands associated with the vport. This function
16285 * is called when driver want to restart discovery of the vport due to
16286 * a Clear Virtual Link event.
16288 void
16289 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16291 struct lpfc_hba *phba = vport->phba;
16292 LPFC_MBOXQ_t *mb, *nextmb;
16293 struct lpfc_dmabuf *mp;
16294 struct lpfc_nodelist *ndlp;
16295 struct lpfc_nodelist *act_mbx_ndlp = NULL;
16296 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
16297 LIST_HEAD(mbox_cmd_list);
16298 uint8_t restart_loop;
16300 /* Clean up internally queued mailbox commands with the vport */
16301 spin_lock_irq(&phba->hbalock);
16302 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16303 if (mb->vport != vport)
16304 continue;
16306 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16307 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16308 continue;
16310 list_del(&mb->list);
16311 list_add_tail(&mb->list, &mbox_cmd_list);
16313 /* Clean up active mailbox command with the vport */
16314 mb = phba->sli.mbox_active;
16315 if (mb && (mb->vport == vport)) {
16316 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16317 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16318 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16319 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16320 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16321 /* Put reference count for delayed processing */
16322 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16323 /* Unregister the RPI when mailbox complete */
16324 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16327 /* Cleanup any mailbox completions which are not yet processed */
16328 do {
16329 restart_loop = 0;
16330 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16332 * If this mailox is already processed or it is
16333 * for another vport ignore it.
16335 if ((mb->vport != vport) ||
16336 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16337 continue;
16339 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16340 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16341 continue;
16343 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16344 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16345 ndlp = (struct lpfc_nodelist *)mb->context2;
16346 /* Unregister the RPI when mailbox complete */
16347 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16348 restart_loop = 1;
16349 spin_unlock_irq(&phba->hbalock);
16350 spin_lock(shost->host_lock);
16351 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16352 spin_unlock(shost->host_lock);
16353 spin_lock_irq(&phba->hbalock);
16354 break;
16357 } while (restart_loop);
16359 spin_unlock_irq(&phba->hbalock);
16361 /* Release the cleaned-up mailbox commands */
16362 while (!list_empty(&mbox_cmd_list)) {
16363 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
16364 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16365 mp = (struct lpfc_dmabuf *) (mb->context1);
16366 if (mp) {
16367 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16368 kfree(mp);
16370 ndlp = (struct lpfc_nodelist *) mb->context2;
16371 mb->context2 = NULL;
16372 if (ndlp) {
16373 spin_lock(shost->host_lock);
16374 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16375 spin_unlock(shost->host_lock);
16376 lpfc_nlp_put(ndlp);
16379 mempool_free(mb, phba->mbox_mem_pool);
16382 /* Release the ndlp with the cleaned-up active mailbox command */
16383 if (act_mbx_ndlp) {
16384 spin_lock(shost->host_lock);
16385 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16386 spin_unlock(shost->host_lock);
16387 lpfc_nlp_put(act_mbx_ndlp);
16392 * lpfc_drain_txq - Drain the txq
16393 * @phba: Pointer to HBA context object.
16395 * This function attempt to submit IOCBs on the txq
16396 * to the adapter. For SLI4 adapters, the txq contains
16397 * ELS IOCBs that have been deferred because the there
16398 * are no SGLs. This congestion can occur with large
16399 * vport counts during node discovery.
16402 uint32_t
16403 lpfc_drain_txq(struct lpfc_hba *phba)
16405 LIST_HEAD(completions);
16406 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16407 struct lpfc_iocbq *piocbq = 0;
16408 unsigned long iflags = 0;
16409 char *fail_msg = NULL;
16410 struct lpfc_sglq *sglq;
16411 union lpfc_wqe wqe;
16412 int txq_cnt = 0;
16414 spin_lock_irqsave(&pring->ring_lock, iflags);
16415 list_for_each_entry(piocbq, &pring->txq, list) {
16416 txq_cnt++;
16419 if (txq_cnt > pring->txq_max)
16420 pring->txq_max = txq_cnt;
16422 spin_unlock_irqrestore(&pring->ring_lock, iflags);
16424 while (!list_empty(&pring->txq)) {
16425 spin_lock_irqsave(&pring->ring_lock, iflags);
16427 piocbq = lpfc_sli_ringtx_get(phba, pring);
16428 if (!piocbq) {
16429 spin_unlock_irqrestore(&pring->ring_lock, iflags);
16430 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16431 "2823 txq empty and txq_cnt is %d\n ",
16432 txq_cnt);
16433 break;
16435 sglq = __lpfc_sli_get_sglq(phba, piocbq);
16436 if (!sglq) {
16437 __lpfc_sli_ringtx_put(phba, pring, piocbq);
16438 spin_unlock_irqrestore(&pring->ring_lock, iflags);
16439 break;
16441 txq_cnt--;
16443 /* The xri and iocb resources secured,
16444 * attempt to issue request
16446 piocbq->sli4_lxritag = sglq->sli4_lxritag;
16447 piocbq->sli4_xritag = sglq->sli4_xritag;
16448 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16449 fail_msg = "to convert bpl to sgl";
16450 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16451 fail_msg = "to convert iocb to wqe";
16452 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16453 fail_msg = " - Wq is full";
16454 else
16455 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16457 if (fail_msg) {
16458 /* Failed means we can't issue and need to cancel */
16459 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16460 "2822 IOCB failed %s iotag 0x%x "
16461 "xri 0x%x\n",
16462 fail_msg,
16463 piocbq->iotag, piocbq->sli4_xritag);
16464 list_add_tail(&piocbq->list, &completions);
16466 spin_unlock_irqrestore(&pring->ring_lock, iflags);
16469 /* Cancel all the IOCBs that cannot be issued */
16470 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16471 IOERR_SLI_ABORTED);
16473 return txq_cnt;