scsi: lpfc: NVME Target: bind to nvmet_fc api
[linux-2.6/btrfs-unstable.git] / drivers / scsi / lpfc / lpfc_sli.c
blob84565f810392b76d9c0f39b27c605de77a68f302
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2016 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>
27 #include <linux/lockdep.h>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/fc/fc_fs.h>
35 #include <linux/aer.h>
37 #include <linux/nvme-fc-driver.h>
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_sli.h"
42 #include "lpfc_sli4.h"
43 #include "lpfc_nl.h"
44 #include "lpfc_disc.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_nvmet.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_logmsg.h"
51 #include "lpfc_compat.h"
52 #include "lpfc_debugfs.h"
53 #include "lpfc_vport.h"
54 #include "lpfc_version.h"
56 /* There are only four IOCB completion types. */
57 typedef enum _lpfc_iocb_type {
58 LPFC_UNKNOWN_IOCB,
59 LPFC_UNSOL_IOCB,
60 LPFC_SOL_IOCB,
61 LPFC_ABORT_IOCB
62 } lpfc_iocb_type;
65 /* Provide function prototypes local to this module. */
66 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
67 uint32_t);
68 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
69 uint8_t *, uint32_t *);
70 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
71 struct lpfc_iocbq *);
72 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
73 struct hbq_dmabuf *);
74 static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
75 struct lpfc_cqe *);
76 static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
77 int);
78 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
79 uint32_t);
80 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
81 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
82 static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
83 struct lpfc_sli_ring *pring,
84 struct lpfc_iocbq *cmdiocb);
86 static IOCB_t *
87 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
89 return &iocbq->iocb;
92 /**
93 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
94 * @q: The Work Queue to operate on.
95 * @wqe: The work Queue Entry to put on the Work queue.
97 * This routine will copy the contents of @wqe to the next available entry on
98 * the @q. This function will then ring the Work Queue Doorbell to signal the
99 * HBA to start processing the Work Queue Entry. This function returns 0 if
100 * successful. If no entries are available on @q then this function will return
101 * -ENOMEM.
102 * The caller is expected to hold the hbalock when calling this routine.
104 static uint32_t
105 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
107 union lpfc_wqe *temp_wqe;
108 struct lpfc_register doorbell;
109 uint32_t host_index;
110 uint32_t idx;
112 /* sanity check on queue memory */
113 if (unlikely(!q))
114 return -ENOMEM;
115 temp_wqe = q->qe[q->host_index].wqe;
117 /* If the host has not yet processed the next entry then we are done */
118 idx = ((q->host_index + 1) % q->entry_count);
119 if (idx == q->hba_index) {
120 q->WQ_overflow++;
121 return -ENOMEM;
123 q->WQ_posted++;
124 /* set consumption flag every once in a while */
125 if (!((q->host_index + 1) % q->entry_repost))
126 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
127 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
128 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
129 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
130 /* ensure WQE bcopy flushed before doorbell write */
131 wmb();
133 /* Update the host index before invoking device */
134 host_index = q->host_index;
136 q->host_index = idx;
138 /* Ring Doorbell */
139 doorbell.word0 = 0;
140 if (q->db_format == LPFC_DB_LIST_FORMAT) {
141 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
142 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
143 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
144 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
145 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
146 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
147 } else {
148 return -EINVAL;
150 writel(doorbell.word0, q->db_regaddr);
152 return 0;
156 * lpfc_sli4_wq_release - Updates internal hba index for WQ
157 * @q: The Work Queue to operate on.
158 * @index: The index to advance the hba index to.
160 * This routine will update the HBA index of a queue to reflect consumption of
161 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
162 * an entry the host calls this function to update the queue's internal
163 * pointers. This routine returns the number of entries that were consumed by
164 * the HBA.
166 static uint32_t
167 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
169 uint32_t released = 0;
171 /* sanity check on queue memory */
172 if (unlikely(!q))
173 return 0;
175 if (q->hba_index == index)
176 return 0;
177 do {
178 q->hba_index = ((q->hba_index + 1) % q->entry_count);
179 released++;
180 } while (q->hba_index != index);
181 return released;
185 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
186 * @q: The Mailbox Queue to operate on.
187 * @wqe: The Mailbox Queue Entry to put on the Work queue.
189 * This routine will copy the contents of @mqe to the next available entry on
190 * the @q. This function will then ring the Work Queue Doorbell to signal the
191 * HBA to start processing the Work Queue Entry. This function returns 0 if
192 * successful. If no entries are available on @q then this function will return
193 * -ENOMEM.
194 * The caller is expected to hold the hbalock when calling this routine.
196 static uint32_t
197 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
199 struct lpfc_mqe *temp_mqe;
200 struct lpfc_register doorbell;
202 /* sanity check on queue memory */
203 if (unlikely(!q))
204 return -ENOMEM;
205 temp_mqe = q->qe[q->host_index].mqe;
207 /* If the host has not yet processed the next entry then we are done */
208 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
209 return -ENOMEM;
210 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
211 /* Save off the mailbox pointer for completion */
212 q->phba->mbox = (MAILBOX_t *)temp_mqe;
214 /* Update the host index before invoking device */
215 q->host_index = ((q->host_index + 1) % q->entry_count);
217 /* Ring Doorbell */
218 doorbell.word0 = 0;
219 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
220 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
221 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
222 return 0;
226 * lpfc_sli4_mq_release - Updates internal hba index for MQ
227 * @q: The Mailbox Queue to operate on.
229 * This routine will update the HBA index of a queue to reflect consumption of
230 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
231 * an entry the host calls this function to update the queue's internal
232 * pointers. This routine returns the number of entries that were consumed by
233 * the HBA.
235 static uint32_t
236 lpfc_sli4_mq_release(struct lpfc_queue *q)
238 /* sanity check on queue memory */
239 if (unlikely(!q))
240 return 0;
242 /* Clear the mailbox pointer for completion */
243 q->phba->mbox = NULL;
244 q->hba_index = ((q->hba_index + 1) % q->entry_count);
245 return 1;
249 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
250 * @q: The Event Queue to get the first valid EQE from
252 * This routine will get the first valid Event Queue Entry from @q, update
253 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
254 * the Queue (no more work to do), or the Queue is full of EQEs that have been
255 * processed, but not popped back to the HBA then this routine will return NULL.
257 static struct lpfc_eqe *
258 lpfc_sli4_eq_get(struct lpfc_queue *q)
260 struct lpfc_eqe *eqe;
261 uint32_t idx;
263 /* sanity check on queue memory */
264 if (unlikely(!q))
265 return NULL;
266 eqe = q->qe[q->hba_index].eqe;
268 /* If the next EQE is not valid then we are done */
269 if (!bf_get_le32(lpfc_eqe_valid, eqe))
270 return NULL;
271 /* If the host has not yet processed the next entry then we are done */
272 idx = ((q->hba_index + 1) % q->entry_count);
273 if (idx == q->host_index)
274 return NULL;
276 q->hba_index = idx;
279 * insert barrier for instruction interlock : data from the hardware
280 * must have the valid bit checked before it can be copied and acted
281 * upon. Speculative instructions were allowing a bcopy at the start
282 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
283 * after our return, to copy data before the valid bit check above
284 * was done. As such, some of the copied data was stale. The barrier
285 * ensures the check is before any data is copied.
287 mb();
288 return eqe;
292 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
293 * @q: The Event Queue to disable interrupts
296 static inline void
297 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
299 struct lpfc_register doorbell;
301 doorbell.word0 = 0;
302 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
303 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
304 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
305 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
306 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
307 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
311 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
312 * @q: The Event Queue that the host has completed processing for.
313 * @arm: Indicates whether the host wants to arms this CQ.
315 * This routine will mark all Event Queue Entries on @q, from the last
316 * known completed entry to the last entry that was processed, as completed
317 * by clearing the valid bit for each completion queue entry. Then it will
318 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
319 * The internal host index in the @q will be updated by this routine to indicate
320 * that the host has finished processing the entries. The @arm parameter
321 * indicates that the queue should be rearmed when ringing the doorbell.
323 * This function will return the number of EQEs that were popped.
325 uint32_t
326 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
328 uint32_t released = 0;
329 struct lpfc_eqe *temp_eqe;
330 struct lpfc_register doorbell;
332 /* sanity check on queue memory */
333 if (unlikely(!q))
334 return 0;
336 /* while there are valid entries */
337 while (q->hba_index != q->host_index) {
338 temp_eqe = q->qe[q->host_index].eqe;
339 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
340 released++;
341 q->host_index = ((q->host_index + 1) % q->entry_count);
343 if (unlikely(released == 0 && !arm))
344 return 0;
346 /* ring doorbell for number popped */
347 doorbell.word0 = 0;
348 if (arm) {
349 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
350 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
352 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
353 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
354 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
355 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
356 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
357 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
358 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
359 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
360 readl(q->phba->sli4_hba.EQCQDBregaddr);
361 return released;
365 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
366 * @q: The Completion Queue to get the first valid CQE from
368 * This routine will get the first valid Completion Queue Entry from @q, update
369 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
370 * the Queue (no more work to do), or the Queue is full of CQEs that have been
371 * processed, but not popped back to the HBA then this routine will return NULL.
373 static struct lpfc_cqe *
374 lpfc_sli4_cq_get(struct lpfc_queue *q)
376 struct lpfc_cqe *cqe;
377 uint32_t idx;
379 /* sanity check on queue memory */
380 if (unlikely(!q))
381 return NULL;
383 /* If the next CQE is not valid then we are done */
384 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
385 return NULL;
386 /* If the host has not yet processed the next entry then we are done */
387 idx = ((q->hba_index + 1) % q->entry_count);
388 if (idx == q->host_index)
389 return NULL;
391 cqe = q->qe[q->hba_index].cqe;
392 q->hba_index = idx;
395 * insert barrier for instruction interlock : data from the hardware
396 * must have the valid bit checked before it can be copied and acted
397 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
398 * instructions allowing action on content before valid bit checked,
399 * add barrier here as well. May not be needed as "content" is a
400 * single 32-bit entity here (vs multi word structure for cq's).
402 mb();
403 return cqe;
407 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
408 * @q: The Completion Queue that the host has completed processing for.
409 * @arm: Indicates whether the host wants to arms this CQ.
411 * This routine will mark all Completion queue entries on @q, from the last
412 * known completed entry to the last entry that was processed, as completed
413 * by clearing the valid bit for each completion queue entry. Then it will
414 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
415 * The internal host index in the @q will be updated by this routine to indicate
416 * that the host has finished processing the entries. The @arm parameter
417 * indicates that the queue should be rearmed when ringing the doorbell.
419 * This function will return the number of CQEs that were released.
421 uint32_t
422 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
424 uint32_t released = 0;
425 struct lpfc_cqe *temp_qe;
426 struct lpfc_register doorbell;
428 /* sanity check on queue memory */
429 if (unlikely(!q))
430 return 0;
431 /* while there are valid entries */
432 while (q->hba_index != q->host_index) {
433 temp_qe = q->qe[q->host_index].cqe;
434 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
435 released++;
436 q->host_index = ((q->host_index + 1) % q->entry_count);
438 if (unlikely(released == 0 && !arm))
439 return 0;
441 /* ring doorbell for number popped */
442 doorbell.word0 = 0;
443 if (arm)
444 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
445 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
446 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
447 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
448 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
449 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
450 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
451 return released;
455 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
456 * @q: The Header Receive Queue to operate on.
457 * @wqe: The Receive Queue Entry to put on the Receive queue.
459 * This routine will copy the contents of @wqe to the next available entry on
460 * the @q. This function will then ring the Receive Queue Doorbell to signal the
461 * HBA to start processing the Receive Queue Entry. This function returns the
462 * index that the rqe was copied to if successful. If no entries are available
463 * on @q then this function will return -ENOMEM.
464 * The caller is expected to hold the hbalock when calling this routine.
467 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
468 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
470 struct lpfc_rqe *temp_hrqe;
471 struct lpfc_rqe *temp_drqe;
472 struct lpfc_register doorbell;
473 int put_index;
475 /* sanity check on queue memory */
476 if (unlikely(!hq) || unlikely(!dq))
477 return -ENOMEM;
478 put_index = hq->host_index;
479 temp_hrqe = hq->qe[hq->host_index].rqe;
480 temp_drqe = dq->qe[dq->host_index].rqe;
482 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
483 return -EINVAL;
484 if (hq->host_index != dq->host_index)
485 return -EINVAL;
486 /* If the host has not yet processed the next entry then we are done */
487 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
488 return -EBUSY;
489 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
490 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
492 /* Update the host index to point to the next slot */
493 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
494 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
496 /* Ring The Header Receive Queue Doorbell */
497 if (!(hq->host_index % hq->entry_repost)) {
498 doorbell.word0 = 0;
499 if (hq->db_format == LPFC_DB_RING_FORMAT) {
500 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
501 hq->entry_repost);
502 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
503 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
504 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
505 hq->entry_repost);
506 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
507 hq->host_index);
508 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
509 } else {
510 return -EINVAL;
512 writel(doorbell.word0, hq->db_regaddr);
514 return put_index;
518 * lpfc_sli4_rq_release - Updates internal hba index for RQ
519 * @q: The Header Receive Queue to operate on.
521 * This routine will update the HBA index of a queue to reflect consumption of
522 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
523 * consumed an entry the host calls this function to update the queue's
524 * internal pointers. This routine returns the number of entries that were
525 * consumed by the HBA.
527 static uint32_t
528 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
530 /* sanity check on queue memory */
531 if (unlikely(!hq) || unlikely(!dq))
532 return 0;
534 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
535 return 0;
536 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
537 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
538 return 1;
542 * lpfc_cmd_iocb - Get next command iocb entry in the ring
543 * @phba: Pointer to HBA context object.
544 * @pring: Pointer to driver SLI ring object.
546 * This function returns pointer to next command iocb entry
547 * in the command ring. The caller must hold hbalock to prevent
548 * other threads consume the next command iocb.
549 * SLI-2/SLI-3 provide different sized iocbs.
551 static inline IOCB_t *
552 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
554 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
555 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
559 * lpfc_resp_iocb - Get next response iocb entry in the ring
560 * @phba: Pointer to HBA context object.
561 * @pring: Pointer to driver SLI ring object.
563 * This function returns pointer to next response iocb entry
564 * in the response ring. The caller must hold hbalock to make sure
565 * that no other thread consume the next response iocb.
566 * SLI-2/SLI-3 provide different sized iocbs.
568 static inline IOCB_t *
569 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
571 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
572 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
576 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
577 * @phba: Pointer to HBA context object.
579 * This function is called with hbalock held. This function
580 * allocates a new driver iocb object from the iocb pool. If the
581 * allocation is successful, it returns pointer to the newly
582 * allocated iocb object else it returns NULL.
584 struct lpfc_iocbq *
585 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
587 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
588 struct lpfc_iocbq * iocbq = NULL;
590 lockdep_assert_held(&phba->hbalock);
592 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
593 if (iocbq)
594 phba->iocb_cnt++;
595 if (phba->iocb_cnt > phba->iocb_max)
596 phba->iocb_max = phba->iocb_cnt;
597 return iocbq;
601 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
602 * @phba: Pointer to HBA context object.
603 * @xritag: XRI value.
605 * This function clears the sglq pointer from the array of acive
606 * sglq's. The xritag that is passed in is used to index into the
607 * array. Before the xritag can be used it needs to be adjusted
608 * by subtracting the xribase.
610 * Returns sglq ponter = success, NULL = Failure.
612 struct lpfc_sglq *
613 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
615 struct lpfc_sglq *sglq;
617 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
618 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
619 return sglq;
623 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
624 * @phba: Pointer to HBA context object.
625 * @xritag: XRI value.
627 * This function returns the sglq pointer from the array of acive
628 * sglq's. The xritag that is passed in is used to index into the
629 * array. Before the xritag can be used it needs to be adjusted
630 * by subtracting the xribase.
632 * Returns sglq ponter = success, NULL = Failure.
634 struct lpfc_sglq *
635 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
637 struct lpfc_sglq *sglq;
639 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
640 return sglq;
644 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
645 * @phba: Pointer to HBA context object.
646 * @xritag: xri used in this exchange.
647 * @rrq: The RRQ to be cleared.
650 void
651 lpfc_clr_rrq_active(struct lpfc_hba *phba,
652 uint16_t xritag,
653 struct lpfc_node_rrq *rrq)
655 struct lpfc_nodelist *ndlp = NULL;
657 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
658 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
660 /* The target DID could have been swapped (cable swap)
661 * we should use the ndlp from the findnode if it is
662 * available.
664 if ((!ndlp) && rrq->ndlp)
665 ndlp = rrq->ndlp;
667 if (!ndlp)
668 goto out;
670 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
671 rrq->send_rrq = 0;
672 rrq->xritag = 0;
673 rrq->rrq_stop_time = 0;
675 out:
676 mempool_free(rrq, phba->rrq_pool);
680 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
681 * @phba: Pointer to HBA context object.
683 * This function is called with hbalock held. This function
684 * Checks if stop_time (ratov from setting rrq active) has
685 * been reached, if it has and the send_rrq flag is set then
686 * it will call lpfc_send_rrq. If the send_rrq flag is not set
687 * then it will just call the routine to clear the rrq and
688 * free the rrq resource.
689 * The timer is set to the next rrq that is going to expire before
690 * leaving the routine.
693 void
694 lpfc_handle_rrq_active(struct lpfc_hba *phba)
696 struct lpfc_node_rrq *rrq;
697 struct lpfc_node_rrq *nextrrq;
698 unsigned long next_time;
699 unsigned long iflags;
700 LIST_HEAD(send_rrq);
702 spin_lock_irqsave(&phba->hbalock, iflags);
703 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
704 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
705 list_for_each_entry_safe(rrq, nextrrq,
706 &phba->active_rrq_list, list) {
707 if (time_after(jiffies, rrq->rrq_stop_time))
708 list_move(&rrq->list, &send_rrq);
709 else if (time_before(rrq->rrq_stop_time, next_time))
710 next_time = rrq->rrq_stop_time;
712 spin_unlock_irqrestore(&phba->hbalock, iflags);
713 if ((!list_empty(&phba->active_rrq_list)) &&
714 (!(phba->pport->load_flag & FC_UNLOADING)))
715 mod_timer(&phba->rrq_tmr, next_time);
716 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
717 list_del(&rrq->list);
718 if (!rrq->send_rrq)
719 /* this call will free the rrq */
720 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
721 else if (lpfc_send_rrq(phba, rrq)) {
722 /* if we send the rrq then the completion handler
723 * will clear the bit in the xribitmap.
725 lpfc_clr_rrq_active(phba, rrq->xritag,
726 rrq);
732 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
733 * @vport: Pointer to vport context object.
734 * @xri: The xri used in the exchange.
735 * @did: The targets DID for this exchange.
737 * returns NULL = rrq not found in the phba->active_rrq_list.
738 * rrq = rrq for this xri and target.
740 struct lpfc_node_rrq *
741 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
743 struct lpfc_hba *phba = vport->phba;
744 struct lpfc_node_rrq *rrq;
745 struct lpfc_node_rrq *nextrrq;
746 unsigned long iflags;
748 if (phba->sli_rev != LPFC_SLI_REV4)
749 return NULL;
750 spin_lock_irqsave(&phba->hbalock, iflags);
751 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
752 if (rrq->vport == vport && rrq->xritag == xri &&
753 rrq->nlp_DID == did){
754 list_del(&rrq->list);
755 spin_unlock_irqrestore(&phba->hbalock, iflags);
756 return rrq;
759 spin_unlock_irqrestore(&phba->hbalock, iflags);
760 return NULL;
764 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
765 * @vport: Pointer to vport context object.
766 * @ndlp: Pointer to the lpfc_node_list structure.
767 * If ndlp is NULL Remove all active RRQs for this vport from the
768 * phba->active_rrq_list and clear the rrq.
769 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
771 void
772 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
775 struct lpfc_hba *phba = vport->phba;
776 struct lpfc_node_rrq *rrq;
777 struct lpfc_node_rrq *nextrrq;
778 unsigned long iflags;
779 LIST_HEAD(rrq_list);
781 if (phba->sli_rev != LPFC_SLI_REV4)
782 return;
783 if (!ndlp) {
784 lpfc_sli4_vport_delete_els_xri_aborted(vport);
785 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
787 spin_lock_irqsave(&phba->hbalock, iflags);
788 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
789 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
790 list_move(&rrq->list, &rrq_list);
791 spin_unlock_irqrestore(&phba->hbalock, iflags);
793 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
794 list_del(&rrq->list);
795 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
800 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
801 * @phba: Pointer to HBA context object.
802 * @ndlp: Targets nodelist pointer for this exchange.
803 * @xritag the xri in the bitmap to test.
805 * This function is called with hbalock held. This function
806 * returns 0 = rrq not active for this xri
807 * 1 = rrq is valid for this xri.
810 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
811 uint16_t xritag)
813 lockdep_assert_held(&phba->hbalock);
814 if (!ndlp)
815 return 0;
816 if (!ndlp->active_rrqs_xri_bitmap)
817 return 0;
818 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
819 return 1;
820 else
821 return 0;
825 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
826 * @phba: Pointer to HBA context object.
827 * @ndlp: nodelist pointer for this target.
828 * @xritag: xri used in this exchange.
829 * @rxid: Remote Exchange ID.
830 * @send_rrq: Flag used to determine if we should send rrq els cmd.
832 * This function takes the hbalock.
833 * The active bit is always set in the active rrq xri_bitmap even
834 * if there is no slot avaiable for the other rrq information.
836 * returns 0 rrq actived for this xri
837 * < 0 No memory or invalid ndlp.
840 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
841 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
843 unsigned long iflags;
844 struct lpfc_node_rrq *rrq;
845 int empty;
847 if (!ndlp)
848 return -EINVAL;
850 if (!phba->cfg_enable_rrq)
851 return -EINVAL;
853 spin_lock_irqsave(&phba->hbalock, iflags);
854 if (phba->pport->load_flag & FC_UNLOADING) {
855 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
856 goto out;
860 * set the active bit even if there is no mem available.
862 if (NLP_CHK_FREE_REQ(ndlp))
863 goto out;
865 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
866 goto out;
868 if (!ndlp->active_rrqs_xri_bitmap)
869 goto out;
871 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
872 goto out;
874 spin_unlock_irqrestore(&phba->hbalock, iflags);
875 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
876 if (!rrq) {
877 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
878 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
879 " DID:0x%x Send:%d\n",
880 xritag, rxid, ndlp->nlp_DID, send_rrq);
881 return -EINVAL;
883 if (phba->cfg_enable_rrq == 1)
884 rrq->send_rrq = send_rrq;
885 else
886 rrq->send_rrq = 0;
887 rrq->xritag = xritag;
888 rrq->rrq_stop_time = jiffies +
889 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
890 rrq->ndlp = ndlp;
891 rrq->nlp_DID = ndlp->nlp_DID;
892 rrq->vport = ndlp->vport;
893 rrq->rxid = rxid;
894 spin_lock_irqsave(&phba->hbalock, iflags);
895 empty = list_empty(&phba->active_rrq_list);
896 list_add_tail(&rrq->list, &phba->active_rrq_list);
897 phba->hba_flag |= HBA_RRQ_ACTIVE;
898 if (empty)
899 lpfc_worker_wake_up(phba);
900 spin_unlock_irqrestore(&phba->hbalock, iflags);
901 return 0;
902 out:
903 spin_unlock_irqrestore(&phba->hbalock, iflags);
904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
905 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
906 " DID:0x%x Send:%d\n",
907 xritag, rxid, ndlp->nlp_DID, send_rrq);
908 return -EINVAL;
912 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
913 * @phba: Pointer to HBA context object.
914 * @piocb: Pointer to the iocbq.
916 * This function is called with the ring lock held. This function
917 * gets a new driver sglq object from the sglq list. If the
918 * list is not empty then it is successful, it returns pointer to the newly
919 * allocated sglq object else it returns NULL.
921 static struct lpfc_sglq *
922 __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
924 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
925 struct lpfc_sglq *sglq = NULL;
926 struct lpfc_sglq *start_sglq = NULL;
927 struct lpfc_scsi_buf *lpfc_cmd;
928 struct lpfc_nodelist *ndlp;
929 int found = 0;
931 lockdep_assert_held(&phba->hbalock);
933 if (piocbq->iocb_flag & LPFC_IO_FCP) {
934 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
935 ndlp = lpfc_cmd->rdata->pnode;
936 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
937 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
938 ndlp = piocbq->context_un.ndlp;
939 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
940 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
941 ndlp = NULL;
942 else
943 ndlp = piocbq->context_un.ndlp;
944 } else {
945 ndlp = piocbq->context1;
948 spin_lock(&phba->sli4_hba.sgl_list_lock);
949 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
950 start_sglq = sglq;
951 while (!found) {
952 if (!sglq)
953 return NULL;
954 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
955 test_bit(sglq->sli4_lxritag,
956 ndlp->active_rrqs_xri_bitmap)) {
957 /* This xri has an rrq outstanding for this DID.
958 * put it back in the list and get another xri.
960 list_add_tail(&sglq->list, lpfc_els_sgl_list);
961 sglq = NULL;
962 list_remove_head(lpfc_els_sgl_list, sglq,
963 struct lpfc_sglq, list);
964 if (sglq == start_sglq) {
965 sglq = NULL;
966 break;
967 } else
968 continue;
970 sglq->ndlp = ndlp;
971 found = 1;
972 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
973 sglq->state = SGL_ALLOCATED;
975 spin_unlock(&phba->sli4_hba.sgl_list_lock);
976 return sglq;
980 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
981 * @phba: Pointer to HBA context object.
982 * @piocb: Pointer to the iocbq.
984 * This function is called with the sgl_list lock held. This function
985 * gets a new driver sglq object from the sglq list. If the
986 * list is not empty then it is successful, it returns pointer to the newly
987 * allocated sglq object else it returns NULL.
989 struct lpfc_sglq *
990 __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
992 struct list_head *lpfc_nvmet_sgl_list;
993 struct lpfc_sglq *sglq = NULL;
995 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
997 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
999 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1000 if (!sglq)
1001 return NULL;
1002 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1003 sglq->state = SGL_ALLOCATED;
1004 return sglq;
1008 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1009 * @phba: Pointer to HBA context object.
1011 * This function is called with no lock held. This function
1012 * allocates a new driver iocb object from the iocb pool. If the
1013 * allocation is successful, it returns pointer to the newly
1014 * allocated iocb object else it returns NULL.
1016 struct lpfc_iocbq *
1017 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1019 struct lpfc_iocbq * iocbq = NULL;
1020 unsigned long iflags;
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 iocbq = __lpfc_sli_get_iocbq(phba);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025 return iocbq;
1029 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1030 * @phba: Pointer to HBA context object.
1031 * @iocbq: Pointer to driver iocb object.
1033 * This function is called with hbalock held to release driver
1034 * iocb object to the iocb pool. The iotag in the iocb object
1035 * does not change for each use of the iocb object. This function
1036 * clears all other fields of the iocb object when it is freed.
1037 * The sqlq structure that holds the xritag and phys and virtual
1038 * mappings for the scatter gather list is retrieved from the
1039 * active array of sglq. The get of the sglq pointer also clears
1040 * the entry in the array. If the status of the IO indiactes that
1041 * this IO was aborted then the sglq entry it put on the
1042 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1043 * IO has good status or fails for any other reason then the sglq
1044 * entry is added to the free list (lpfc_els_sgl_list).
1046 static void
1047 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1049 struct lpfc_sglq *sglq;
1050 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1051 unsigned long iflag = 0;
1052 struct lpfc_sli_ring *pring;
1054 lockdep_assert_held(&phba->hbalock);
1056 if (iocbq->sli4_xritag == NO_XRI)
1057 sglq = NULL;
1058 else
1059 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1062 if (sglq) {
1063 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1064 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1065 iflag);
1066 sglq->state = SGL_FREED;
1067 sglq->ndlp = NULL;
1068 list_add_tail(&sglq->list,
1069 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1070 spin_unlock_irqrestore(
1071 &phba->sli4_hba.sgl_list_lock, iflag);
1072 goto out;
1075 pring = phba->sli4_hba.els_wq->pring;
1076 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1077 (sglq->state != SGL_XRI_ABORTED)) {
1078 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1079 iflag);
1080 list_add(&sglq->list,
1081 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1082 spin_unlock_irqrestore(
1083 &phba->sli4_hba.sgl_list_lock, iflag);
1084 } else {
1085 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1086 iflag);
1087 sglq->state = SGL_FREED;
1088 sglq->ndlp = NULL;
1089 list_add_tail(&sglq->list,
1090 &phba->sli4_hba.lpfc_els_sgl_list);
1091 spin_unlock_irqrestore(
1092 &phba->sli4_hba.sgl_list_lock, iflag);
1094 /* Check if TXQ queue needs to be serviced */
1095 if (!list_empty(&pring->txq))
1096 lpfc_worker_wake_up(phba);
1100 out:
1102 * Clean all volatile data fields, preserve iotag and node struct.
1104 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1105 iocbq->sli4_lxritag = NO_XRI;
1106 iocbq->sli4_xritag = NO_XRI;
1107 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1108 LPFC_IO_NVME_LS);
1109 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1114 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1115 * @phba: Pointer to HBA context object.
1116 * @iocbq: Pointer to driver iocb object.
1118 * This function is called with hbalock held to release driver
1119 * iocb object to the iocb pool. The iotag in the iocb object
1120 * does not change for each use of the iocb object. This function
1121 * clears all other fields of the iocb object when it is freed.
1123 static void
1124 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1126 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1128 lockdep_assert_held(&phba->hbalock);
1131 * Clean all volatile data fields, preserve iotag and node struct.
1133 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1134 iocbq->sli4_xritag = NO_XRI;
1135 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1139 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1140 * @phba: Pointer to HBA context object.
1141 * @iocbq: Pointer to driver iocb object.
1143 * This function is called with hbalock held to release driver
1144 * iocb object to the iocb pool. The iotag in the iocb object
1145 * does not change for each use of the iocb object. This function
1146 * clears all other fields of the iocb object when it is freed.
1148 static void
1149 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1151 lockdep_assert_held(&phba->hbalock);
1153 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1154 phba->iocb_cnt--;
1158 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1159 * @phba: Pointer to HBA context object.
1160 * @iocbq: Pointer to driver iocb object.
1162 * This function is called with no lock held to release the iocb to
1163 * iocb pool.
1165 void
1166 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1168 unsigned long iflags;
1171 * Clean all volatile data fields, preserve iotag and node struct.
1173 spin_lock_irqsave(&phba->hbalock, iflags);
1174 __lpfc_sli_release_iocbq(phba, iocbq);
1175 spin_unlock_irqrestore(&phba->hbalock, iflags);
1179 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1180 * @phba: Pointer to HBA context object.
1181 * @iocblist: List of IOCBs.
1182 * @ulpstatus: ULP status in IOCB command field.
1183 * @ulpWord4: ULP word-4 in IOCB command field.
1185 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1186 * on the list by invoking the complete callback function associated with the
1187 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1188 * fields.
1190 void
1191 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1192 uint32_t ulpstatus, uint32_t ulpWord4)
1194 struct lpfc_iocbq *piocb;
1196 while (!list_empty(iocblist)) {
1197 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1198 if (!piocb->iocb_cmpl)
1199 lpfc_sli_release_iocbq(phba, piocb);
1200 else {
1201 piocb->iocb.ulpStatus = ulpstatus;
1202 piocb->iocb.un.ulpWord[4] = ulpWord4;
1203 (piocb->iocb_cmpl) (phba, piocb, piocb);
1206 return;
1210 * lpfc_sli_iocb_cmd_type - Get the iocb type
1211 * @iocb_cmnd: iocb command code.
1213 * This function is called by ring event handler function to get the iocb type.
1214 * This function translates the iocb command to an iocb command type used to
1215 * decide the final disposition of each completed IOCB.
1216 * The function returns
1217 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1218 * LPFC_SOL_IOCB if it is a solicited iocb completion
1219 * LPFC_ABORT_IOCB if it is an abort iocb
1220 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1222 * The caller is not required to hold any lock.
1224 static lpfc_iocb_type
1225 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1227 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1229 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1230 return 0;
1232 switch (iocb_cmnd) {
1233 case CMD_XMIT_SEQUENCE_CR:
1234 case CMD_XMIT_SEQUENCE_CX:
1235 case CMD_XMIT_BCAST_CN:
1236 case CMD_XMIT_BCAST_CX:
1237 case CMD_ELS_REQUEST_CR:
1238 case CMD_ELS_REQUEST_CX:
1239 case CMD_CREATE_XRI_CR:
1240 case CMD_CREATE_XRI_CX:
1241 case CMD_GET_RPI_CN:
1242 case CMD_XMIT_ELS_RSP_CX:
1243 case CMD_GET_RPI_CR:
1244 case CMD_FCP_IWRITE_CR:
1245 case CMD_FCP_IWRITE_CX:
1246 case CMD_FCP_IREAD_CR:
1247 case CMD_FCP_IREAD_CX:
1248 case CMD_FCP_ICMND_CR:
1249 case CMD_FCP_ICMND_CX:
1250 case CMD_FCP_TSEND_CX:
1251 case CMD_FCP_TRSP_CX:
1252 case CMD_FCP_TRECEIVE_CX:
1253 case CMD_FCP_AUTO_TRSP_CX:
1254 case CMD_ADAPTER_MSG:
1255 case CMD_ADAPTER_DUMP:
1256 case CMD_XMIT_SEQUENCE64_CR:
1257 case CMD_XMIT_SEQUENCE64_CX:
1258 case CMD_XMIT_BCAST64_CN:
1259 case CMD_XMIT_BCAST64_CX:
1260 case CMD_ELS_REQUEST64_CR:
1261 case CMD_ELS_REQUEST64_CX:
1262 case CMD_FCP_IWRITE64_CR:
1263 case CMD_FCP_IWRITE64_CX:
1264 case CMD_FCP_IREAD64_CR:
1265 case CMD_FCP_IREAD64_CX:
1266 case CMD_FCP_ICMND64_CR:
1267 case CMD_FCP_ICMND64_CX:
1268 case CMD_FCP_TSEND64_CX:
1269 case CMD_FCP_TRSP64_CX:
1270 case CMD_FCP_TRECEIVE64_CX:
1271 case CMD_GEN_REQUEST64_CR:
1272 case CMD_GEN_REQUEST64_CX:
1273 case CMD_XMIT_ELS_RSP64_CX:
1274 case DSSCMD_IWRITE64_CR:
1275 case DSSCMD_IWRITE64_CX:
1276 case DSSCMD_IREAD64_CR:
1277 case DSSCMD_IREAD64_CX:
1278 type = LPFC_SOL_IOCB;
1279 break;
1280 case CMD_ABORT_XRI_CN:
1281 case CMD_ABORT_XRI_CX:
1282 case CMD_CLOSE_XRI_CN:
1283 case CMD_CLOSE_XRI_CX:
1284 case CMD_XRI_ABORTED_CX:
1285 case CMD_ABORT_MXRI64_CN:
1286 case CMD_XMIT_BLS_RSP64_CX:
1287 type = LPFC_ABORT_IOCB;
1288 break;
1289 case CMD_RCV_SEQUENCE_CX:
1290 case CMD_RCV_ELS_REQ_CX:
1291 case CMD_RCV_SEQUENCE64_CX:
1292 case CMD_RCV_ELS_REQ64_CX:
1293 case CMD_ASYNC_STATUS:
1294 case CMD_IOCB_RCV_SEQ64_CX:
1295 case CMD_IOCB_RCV_ELS64_CX:
1296 case CMD_IOCB_RCV_CONT64_CX:
1297 case CMD_IOCB_RET_XRI64_CX:
1298 type = LPFC_UNSOL_IOCB;
1299 break;
1300 case CMD_IOCB_XMIT_MSEQ64_CR:
1301 case CMD_IOCB_XMIT_MSEQ64_CX:
1302 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1303 case CMD_IOCB_RCV_ELS_LIST64_CX:
1304 case CMD_IOCB_CLOSE_EXTENDED_CN:
1305 case CMD_IOCB_ABORT_EXTENDED_CN:
1306 case CMD_IOCB_RET_HBQE64_CN:
1307 case CMD_IOCB_FCP_IBIDIR64_CR:
1308 case CMD_IOCB_FCP_IBIDIR64_CX:
1309 case CMD_IOCB_FCP_ITASKMGT64_CX:
1310 case CMD_IOCB_LOGENTRY_CN:
1311 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1312 printk("%s - Unhandled SLI-3 Command x%x\n",
1313 __func__, iocb_cmnd);
1314 type = LPFC_UNKNOWN_IOCB;
1315 break;
1316 default:
1317 type = LPFC_UNKNOWN_IOCB;
1318 break;
1321 return type;
1325 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1326 * @phba: Pointer to HBA context object.
1328 * This function is called from SLI initialization code
1329 * to configure every ring of the HBA's SLI interface. The
1330 * caller is not required to hold any lock. This function issues
1331 * a config_ring mailbox command for each ring.
1332 * This function returns zero if successful else returns a negative
1333 * error code.
1335 static int
1336 lpfc_sli_ring_map(struct lpfc_hba *phba)
1338 struct lpfc_sli *psli = &phba->sli;
1339 LPFC_MBOXQ_t *pmb;
1340 MAILBOX_t *pmbox;
1341 int i, rc, ret = 0;
1343 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1344 if (!pmb)
1345 return -ENOMEM;
1346 pmbox = &pmb->u.mb;
1347 phba->link_state = LPFC_INIT_MBX_CMDS;
1348 for (i = 0; i < psli->num_rings; i++) {
1349 lpfc_config_ring(phba, i, pmb);
1350 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1351 if (rc != MBX_SUCCESS) {
1352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1353 "0446 Adapter failed to init (%d), "
1354 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1355 "ring %d\n",
1356 rc, pmbox->mbxCommand,
1357 pmbox->mbxStatus, i);
1358 phba->link_state = LPFC_HBA_ERROR;
1359 ret = -ENXIO;
1360 break;
1363 mempool_free(pmb, phba->mbox_mem_pool);
1364 return ret;
1368 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1369 * @phba: Pointer to HBA context object.
1370 * @pring: Pointer to driver SLI ring object.
1371 * @piocb: Pointer to the driver iocb object.
1373 * This function is called with hbalock held. The function adds the
1374 * new iocb to txcmplq of the given ring. This function always returns
1375 * 0. If this function is called for ELS ring, this function checks if
1376 * there is a vport associated with the ELS command. This function also
1377 * starts els_tmofunc timer if this is an ELS command.
1379 static int
1380 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1381 struct lpfc_iocbq *piocb)
1383 lockdep_assert_held(&phba->hbalock);
1385 BUG_ON(!piocb);
1387 list_add_tail(&piocb->list, &pring->txcmplq);
1388 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1390 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1391 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1392 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1393 BUG_ON(!piocb->vport);
1394 if (!(piocb->vport->load_flag & FC_UNLOADING))
1395 mod_timer(&piocb->vport->els_tmofunc,
1396 jiffies +
1397 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1400 return 0;
1404 * lpfc_sli_ringtx_get - Get first element of the txq
1405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1408 * This function is called with hbalock held to get next
1409 * iocb in txq of the given ring. If there is any iocb in
1410 * the txq, the function returns first iocb in the list after
1411 * removing the iocb from the list, else it returns NULL.
1413 struct lpfc_iocbq *
1414 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1416 struct lpfc_iocbq *cmd_iocb;
1418 lockdep_assert_held(&phba->hbalock);
1420 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1421 return cmd_iocb;
1425 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1426 * @phba: Pointer to HBA context object.
1427 * @pring: Pointer to driver SLI ring object.
1429 * This function is called with hbalock held and the caller must post the
1430 * iocb without releasing the lock. If the caller releases the lock,
1431 * iocb slot returned by the function is not guaranteed to be available.
1432 * The function returns pointer to the next available iocb slot if there
1433 * is available slot in the ring, else it returns NULL.
1434 * If the get index of the ring is ahead of the put index, the function
1435 * will post an error attention event to the worker thread to take the
1436 * HBA to offline state.
1438 static IOCB_t *
1439 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1441 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1442 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1444 lockdep_assert_held(&phba->hbalock);
1446 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1447 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1448 pring->sli.sli3.next_cmdidx = 0;
1450 if (unlikely(pring->sli.sli3.local_getidx ==
1451 pring->sli.sli3.next_cmdidx)) {
1453 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1455 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1456 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1457 "0315 Ring %d issue: portCmdGet %d "
1458 "is bigger than cmd ring %d\n",
1459 pring->ringno,
1460 pring->sli.sli3.local_getidx,
1461 max_cmd_idx);
1463 phba->link_state = LPFC_HBA_ERROR;
1465 * All error attention handlers are posted to
1466 * worker thread
1468 phba->work_ha |= HA_ERATT;
1469 phba->work_hs = HS_FFER3;
1471 lpfc_worker_wake_up(phba);
1473 return NULL;
1476 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1477 return NULL;
1480 return lpfc_cmd_iocb(phba, pring);
1484 * lpfc_sli_next_iotag - Get an iotag for the iocb
1485 * @phba: Pointer to HBA context object.
1486 * @iocbq: Pointer to driver iocb object.
1488 * This function gets an iotag for the iocb. If there is no unused iotag and
1489 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1490 * array and assigns a new iotag.
1491 * The function returns the allocated iotag if successful, else returns zero.
1492 * Zero is not a valid iotag.
1493 * The caller is not required to hold any lock.
1495 uint16_t
1496 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1498 struct lpfc_iocbq **new_arr;
1499 struct lpfc_iocbq **old_arr;
1500 size_t new_len;
1501 struct lpfc_sli *psli = &phba->sli;
1502 uint16_t iotag;
1504 spin_lock_irq(&phba->hbalock);
1505 iotag = psli->last_iotag;
1506 if(++iotag < psli->iocbq_lookup_len) {
1507 psli->last_iotag = iotag;
1508 psli->iocbq_lookup[iotag] = iocbq;
1509 spin_unlock_irq(&phba->hbalock);
1510 iocbq->iotag = iotag;
1511 return iotag;
1512 } else if (psli->iocbq_lookup_len < (0xffff
1513 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1514 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1515 spin_unlock_irq(&phba->hbalock);
1516 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1517 GFP_KERNEL);
1518 if (new_arr) {
1519 spin_lock_irq(&phba->hbalock);
1520 old_arr = psli->iocbq_lookup;
1521 if (new_len <= psli->iocbq_lookup_len) {
1522 /* highly unprobable case */
1523 kfree(new_arr);
1524 iotag = psli->last_iotag;
1525 if(++iotag < psli->iocbq_lookup_len) {
1526 psli->last_iotag = iotag;
1527 psli->iocbq_lookup[iotag] = iocbq;
1528 spin_unlock_irq(&phba->hbalock);
1529 iocbq->iotag = iotag;
1530 return iotag;
1532 spin_unlock_irq(&phba->hbalock);
1533 return 0;
1535 if (psli->iocbq_lookup)
1536 memcpy(new_arr, old_arr,
1537 ((psli->last_iotag + 1) *
1538 sizeof (struct lpfc_iocbq *)));
1539 psli->iocbq_lookup = new_arr;
1540 psli->iocbq_lookup_len = new_len;
1541 psli->last_iotag = iotag;
1542 psli->iocbq_lookup[iotag] = iocbq;
1543 spin_unlock_irq(&phba->hbalock);
1544 iocbq->iotag = iotag;
1545 kfree(old_arr);
1546 return iotag;
1548 } else
1549 spin_unlock_irq(&phba->hbalock);
1551 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1552 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1553 psli->last_iotag);
1555 return 0;
1559 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1560 * @phba: Pointer to HBA context object.
1561 * @pring: Pointer to driver SLI ring object.
1562 * @iocb: Pointer to iocb slot in the ring.
1563 * @nextiocb: Pointer to driver iocb object which need to be
1564 * posted to firmware.
1566 * This function is called with hbalock held to post a new iocb to
1567 * the firmware. This function copies the new iocb to ring iocb slot and
1568 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1569 * a completion call back for this iocb else the function will free the
1570 * iocb object.
1572 static void
1573 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1574 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1576 lockdep_assert_held(&phba->hbalock);
1578 * Set up an iotag
1580 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1583 if (pring->ringno == LPFC_ELS_RING) {
1584 lpfc_debugfs_slow_ring_trc(phba,
1585 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1586 *(((uint32_t *) &nextiocb->iocb) + 4),
1587 *(((uint32_t *) &nextiocb->iocb) + 6),
1588 *(((uint32_t *) &nextiocb->iocb) + 7));
1592 * Issue iocb command to adapter
1594 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1595 wmb();
1596 pring->stats.iocb_cmd++;
1599 * If there is no completion routine to call, we can release the
1600 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1601 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1603 if (nextiocb->iocb_cmpl)
1604 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1605 else
1606 __lpfc_sli_release_iocbq(phba, nextiocb);
1609 * Let the HBA know what IOCB slot will be the next one the
1610 * driver will put a command into.
1612 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1613 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1617 * lpfc_sli_update_full_ring - Update the chip attention register
1618 * @phba: Pointer to HBA context object.
1619 * @pring: Pointer to driver SLI ring object.
1621 * The caller is not required to hold any lock for calling this function.
1622 * This function updates the chip attention bits for the ring to inform firmware
1623 * that there are pending work to be done for this ring and requests an
1624 * interrupt when there is space available in the ring. This function is
1625 * called when the driver is unable to post more iocbs to the ring due
1626 * to unavailability of space in the ring.
1628 static void
1629 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1631 int ringno = pring->ringno;
1633 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1635 wmb();
1638 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1639 * The HBA will tell us when an IOCB entry is available.
1641 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1642 readl(phba->CAregaddr); /* flush */
1644 pring->stats.iocb_cmd_full++;
1648 * lpfc_sli_update_ring - Update chip attention register
1649 * @phba: Pointer to HBA context object.
1650 * @pring: Pointer to driver SLI ring object.
1652 * This function updates the chip attention register bit for the
1653 * given ring to inform HBA that there is more work to be done
1654 * in this ring. The caller is not required to hold any lock.
1656 static void
1657 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1659 int ringno = pring->ringno;
1662 * Tell the HBA that there is work to do in this ring.
1664 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1665 wmb();
1666 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1667 readl(phba->CAregaddr); /* flush */
1672 * lpfc_sli_resume_iocb - Process iocbs in the txq
1673 * @phba: Pointer to HBA context object.
1674 * @pring: Pointer to driver SLI ring object.
1676 * This function is called with hbalock held to post pending iocbs
1677 * in the txq to the firmware. This function is called when driver
1678 * detects space available in the ring.
1680 static void
1681 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1683 IOCB_t *iocb;
1684 struct lpfc_iocbq *nextiocb;
1686 lockdep_assert_held(&phba->hbalock);
1689 * Check to see if:
1690 * (a) there is anything on the txq to send
1691 * (b) link is up
1692 * (c) link attention events can be processed (fcp ring only)
1693 * (d) IOCB processing is not blocked by the outstanding mbox command.
1696 if (lpfc_is_link_up(phba) &&
1697 (!list_empty(&pring->txq)) &&
1698 (pring->ringno != LPFC_FCP_RING ||
1699 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1701 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1702 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1703 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1705 if (iocb)
1706 lpfc_sli_update_ring(phba, pring);
1707 else
1708 lpfc_sli_update_full_ring(phba, pring);
1711 return;
1715 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1716 * @phba: Pointer to HBA context object.
1717 * @hbqno: HBQ number.
1719 * This function is called with hbalock held to get the next
1720 * available slot for the given HBQ. If there is free slot
1721 * available for the HBQ it will return pointer to the next available
1722 * HBQ entry else it will return NULL.
1724 static struct lpfc_hbq_entry *
1725 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1727 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1729 lockdep_assert_held(&phba->hbalock);
1731 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1732 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1733 hbqp->next_hbqPutIdx = 0;
1735 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1736 uint32_t raw_index = phba->hbq_get[hbqno];
1737 uint32_t getidx = le32_to_cpu(raw_index);
1739 hbqp->local_hbqGetIdx = getidx;
1741 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1742 lpfc_printf_log(phba, KERN_ERR,
1743 LOG_SLI | LOG_VPORT,
1744 "1802 HBQ %d: local_hbqGetIdx "
1745 "%u is > than hbqp->entry_count %u\n",
1746 hbqno, hbqp->local_hbqGetIdx,
1747 hbqp->entry_count);
1749 phba->link_state = LPFC_HBA_ERROR;
1750 return NULL;
1753 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1754 return NULL;
1757 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1758 hbqp->hbqPutIdx;
1762 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1763 * @phba: Pointer to HBA context object.
1765 * This function is called with no lock held to free all the
1766 * hbq buffers while uninitializing the SLI interface. It also
1767 * frees the HBQ buffers returned by the firmware but not yet
1768 * processed by the upper layers.
1770 void
1771 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1773 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1774 struct hbq_dmabuf *hbq_buf;
1775 unsigned long flags;
1776 int i, hbq_count;
1778 hbq_count = lpfc_sli_hbq_count();
1779 /* Return all memory used by all HBQs */
1780 spin_lock_irqsave(&phba->hbalock, flags);
1781 for (i = 0; i < hbq_count; ++i) {
1782 list_for_each_entry_safe(dmabuf, next_dmabuf,
1783 &phba->hbqs[i].hbq_buffer_list, list) {
1784 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1785 list_del(&hbq_buf->dbuf.list);
1786 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1788 phba->hbqs[i].buffer_count = 0;
1791 /* Mark the HBQs not in use */
1792 phba->hbq_in_use = 0;
1793 spin_unlock_irqrestore(&phba->hbalock, flags);
1797 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1798 * @phba: Pointer to HBA context object.
1799 * @hbqno: HBQ number.
1800 * @hbq_buf: Pointer to HBQ buffer.
1802 * This function is called with the hbalock held to post a
1803 * hbq buffer to the firmware. If the function finds an empty
1804 * slot in the HBQ, it will post the buffer. The function will return
1805 * pointer to the hbq entry if it successfully post the buffer
1806 * else it will return NULL.
1808 static int
1809 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1810 struct hbq_dmabuf *hbq_buf)
1812 lockdep_assert_held(&phba->hbalock);
1813 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1817 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1818 * @phba: Pointer to HBA context object.
1819 * @hbqno: HBQ number.
1820 * @hbq_buf: Pointer to HBQ buffer.
1822 * This function is called with the hbalock held to post a hbq buffer to the
1823 * firmware. If the function finds an empty slot in the HBQ, it will post the
1824 * buffer and place it on the hbq_buffer_list. The function will return zero if
1825 * it successfully post the buffer else it will return an error.
1827 static int
1828 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1829 struct hbq_dmabuf *hbq_buf)
1831 struct lpfc_hbq_entry *hbqe;
1832 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1834 lockdep_assert_held(&phba->hbalock);
1835 /* Get next HBQ entry slot to use */
1836 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1837 if (hbqe) {
1838 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1840 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1841 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1842 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
1843 hbqe->bde.tus.f.bdeFlags = 0;
1844 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1845 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1846 /* Sync SLIM */
1847 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1848 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1849 /* flush */
1850 readl(phba->hbq_put + hbqno);
1851 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1852 return 0;
1853 } else
1854 return -ENOMEM;
1858 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1859 * @phba: Pointer to HBA context object.
1860 * @hbqno: HBQ number.
1861 * @hbq_buf: Pointer to HBQ buffer.
1863 * This function is called with the hbalock held to post an RQE to the SLI4
1864 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1865 * the hbq_buffer_list and return zero, otherwise it will return an error.
1867 static int
1868 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1869 struct hbq_dmabuf *hbq_buf)
1871 int rc;
1872 struct lpfc_rqe hrqe;
1873 struct lpfc_rqe drqe;
1874 struct lpfc_queue *hrq;
1875 struct lpfc_queue *drq;
1877 if (hbqno != LPFC_ELS_HBQ)
1878 return 1;
1879 hrq = phba->sli4_hba.hdr_rq;
1880 drq = phba->sli4_hba.dat_rq;
1882 lockdep_assert_held(&phba->hbalock);
1883 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1884 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1885 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1886 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1887 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
1888 if (rc < 0)
1889 return rc;
1890 hbq_buf->tag = (rc | (hbqno << 16));
1891 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1892 return 0;
1895 /* HBQ for ELS and CT traffic. */
1896 static struct lpfc_hbq_init lpfc_els_hbq = {
1897 .rn = 1,
1898 .entry_count = 256,
1899 .mask_count = 0,
1900 .profile = 0,
1901 .ring_mask = (1 << LPFC_ELS_RING),
1902 .buffer_count = 0,
1903 .init_count = 40,
1904 .add_count = 40,
1907 /* Array of HBQs */
1908 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1909 &lpfc_els_hbq,
1913 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1914 * @phba: Pointer to HBA context object.
1915 * @hbqno: HBQ number.
1916 * @count: Number of HBQ buffers to be posted.
1918 * This function is called with no lock held to post more hbq buffers to the
1919 * given HBQ. The function returns the number of HBQ buffers successfully
1920 * posted.
1922 static int
1923 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1925 uint32_t i, posted = 0;
1926 unsigned long flags;
1927 struct hbq_dmabuf *hbq_buffer;
1928 LIST_HEAD(hbq_buf_list);
1929 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1930 return 0;
1932 if ((phba->hbqs[hbqno].buffer_count + count) >
1933 lpfc_hbq_defs[hbqno]->entry_count)
1934 count = lpfc_hbq_defs[hbqno]->entry_count -
1935 phba->hbqs[hbqno].buffer_count;
1936 if (!count)
1937 return 0;
1938 /* Allocate HBQ entries */
1939 for (i = 0; i < count; i++) {
1940 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1941 if (!hbq_buffer)
1942 break;
1943 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1945 /* Check whether HBQ is still in use */
1946 spin_lock_irqsave(&phba->hbalock, flags);
1947 if (!phba->hbq_in_use)
1948 goto err;
1949 while (!list_empty(&hbq_buf_list)) {
1950 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1951 dbuf.list);
1952 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1953 (hbqno << 16));
1954 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1955 phba->hbqs[hbqno].buffer_count++;
1956 posted++;
1957 } else
1958 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1960 spin_unlock_irqrestore(&phba->hbalock, flags);
1961 return posted;
1962 err:
1963 spin_unlock_irqrestore(&phba->hbalock, flags);
1964 while (!list_empty(&hbq_buf_list)) {
1965 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1966 dbuf.list);
1967 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1969 return 0;
1973 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1974 * @phba: Pointer to HBA context object.
1975 * @qno: HBQ number.
1977 * This function posts more buffers to the HBQ. This function
1978 * is called with no lock held. The function returns the number of HBQ entries
1979 * successfully allocated.
1982 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1984 if (phba->sli_rev == LPFC_SLI_REV4)
1985 return 0;
1986 else
1987 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1988 lpfc_hbq_defs[qno]->add_count);
1992 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1993 * @phba: Pointer to HBA context object.
1994 * @qno: HBQ queue number.
1996 * This function is called from SLI initialization code path with
1997 * no lock held to post initial HBQ buffers to firmware. The
1998 * function returns the number of HBQ entries successfully allocated.
2000 static int
2001 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2003 if (phba->sli_rev == LPFC_SLI_REV4)
2004 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2005 lpfc_hbq_defs[qno]->entry_count);
2006 else
2007 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2008 lpfc_hbq_defs[qno]->init_count);
2012 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2013 * @phba: Pointer to HBA context object.
2014 * @hbqno: HBQ number.
2016 * This function removes the first hbq buffer on an hbq list and returns a
2017 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2019 static struct hbq_dmabuf *
2020 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2022 struct lpfc_dmabuf *d_buf;
2024 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2025 if (!d_buf)
2026 return NULL;
2027 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2031 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2032 * @phba: Pointer to HBA context object.
2033 * @hbqno: HBQ number.
2035 * This function removes the first RQ buffer on an RQ buffer list and returns a
2036 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2038 static struct rqb_dmabuf *
2039 lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2041 struct lpfc_dmabuf *h_buf;
2042 struct lpfc_rqb *rqbp;
2044 rqbp = hrq->rqbp;
2045 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2046 struct lpfc_dmabuf, list);
2047 if (!h_buf)
2048 return NULL;
2049 rqbp->buffer_count--;
2050 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2054 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2055 * @phba: Pointer to HBA context object.
2056 * @tag: Tag of the hbq buffer.
2058 * This function searches for the hbq buffer associated with the given tag in
2059 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2060 * otherwise it returns NULL.
2062 static struct hbq_dmabuf *
2063 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2065 struct lpfc_dmabuf *d_buf;
2066 struct hbq_dmabuf *hbq_buf;
2067 uint32_t hbqno;
2069 hbqno = tag >> 16;
2070 if (hbqno >= LPFC_MAX_HBQS)
2071 return NULL;
2073 spin_lock_irq(&phba->hbalock);
2074 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2075 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2076 if (hbq_buf->tag == tag) {
2077 spin_unlock_irq(&phba->hbalock);
2078 return hbq_buf;
2081 spin_unlock_irq(&phba->hbalock);
2082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2083 "1803 Bad hbq tag. Data: x%x x%x\n",
2084 tag, phba->hbqs[tag >> 16].buffer_count);
2085 return NULL;
2089 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2090 * @phba: Pointer to HBA context object.
2091 * @hbq_buffer: Pointer to HBQ buffer.
2093 * This function is called with hbalock. This function gives back
2094 * the hbq buffer to firmware. If the HBQ does not have space to
2095 * post the buffer, it will free the buffer.
2097 void
2098 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2100 uint32_t hbqno;
2102 if (hbq_buffer) {
2103 hbqno = hbq_buffer->tag >> 16;
2104 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2105 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2110 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2111 * @mbxCommand: mailbox command code.
2113 * This function is called by the mailbox event handler function to verify
2114 * that the completed mailbox command is a legitimate mailbox command. If the
2115 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2116 * and the mailbox event handler will take the HBA offline.
2118 static int
2119 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2121 uint8_t ret;
2123 switch (mbxCommand) {
2124 case MBX_LOAD_SM:
2125 case MBX_READ_NV:
2126 case MBX_WRITE_NV:
2127 case MBX_WRITE_VPARMS:
2128 case MBX_RUN_BIU_DIAG:
2129 case MBX_INIT_LINK:
2130 case MBX_DOWN_LINK:
2131 case MBX_CONFIG_LINK:
2132 case MBX_CONFIG_RING:
2133 case MBX_RESET_RING:
2134 case MBX_READ_CONFIG:
2135 case MBX_READ_RCONFIG:
2136 case MBX_READ_SPARM:
2137 case MBX_READ_STATUS:
2138 case MBX_READ_RPI:
2139 case MBX_READ_XRI:
2140 case MBX_READ_REV:
2141 case MBX_READ_LNK_STAT:
2142 case MBX_REG_LOGIN:
2143 case MBX_UNREG_LOGIN:
2144 case MBX_CLEAR_LA:
2145 case MBX_DUMP_MEMORY:
2146 case MBX_DUMP_CONTEXT:
2147 case MBX_RUN_DIAGS:
2148 case MBX_RESTART:
2149 case MBX_UPDATE_CFG:
2150 case MBX_DOWN_LOAD:
2151 case MBX_DEL_LD_ENTRY:
2152 case MBX_RUN_PROGRAM:
2153 case MBX_SET_MASK:
2154 case MBX_SET_VARIABLE:
2155 case MBX_UNREG_D_ID:
2156 case MBX_KILL_BOARD:
2157 case MBX_CONFIG_FARP:
2158 case MBX_BEACON:
2159 case MBX_LOAD_AREA:
2160 case MBX_RUN_BIU_DIAG64:
2161 case MBX_CONFIG_PORT:
2162 case MBX_READ_SPARM64:
2163 case MBX_READ_RPI64:
2164 case MBX_REG_LOGIN64:
2165 case MBX_READ_TOPOLOGY:
2166 case MBX_WRITE_WWN:
2167 case MBX_SET_DEBUG:
2168 case MBX_LOAD_EXP_ROM:
2169 case MBX_ASYNCEVT_ENABLE:
2170 case MBX_REG_VPI:
2171 case MBX_UNREG_VPI:
2172 case MBX_HEARTBEAT:
2173 case MBX_PORT_CAPABILITIES:
2174 case MBX_PORT_IOV_CONTROL:
2175 case MBX_SLI4_CONFIG:
2176 case MBX_SLI4_REQ_FTRS:
2177 case MBX_REG_FCFI:
2178 case MBX_UNREG_FCFI:
2179 case MBX_REG_VFI:
2180 case MBX_UNREG_VFI:
2181 case MBX_INIT_VPI:
2182 case MBX_INIT_VFI:
2183 case MBX_RESUME_RPI:
2184 case MBX_READ_EVENT_LOG_STATUS:
2185 case MBX_READ_EVENT_LOG:
2186 case MBX_SECURITY_MGMT:
2187 case MBX_AUTH_PORT:
2188 case MBX_ACCESS_VDATA:
2189 ret = mbxCommand;
2190 break;
2191 default:
2192 ret = MBX_SHUTDOWN;
2193 break;
2195 return ret;
2199 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2200 * @phba: Pointer to HBA context object.
2201 * @pmboxq: Pointer to mailbox command.
2203 * This is completion handler function for mailbox commands issued from
2204 * lpfc_sli_issue_mbox_wait function. This function is called by the
2205 * mailbox event handler function with no lock held. This function
2206 * will wake up thread waiting on the wait queue pointed by context1
2207 * of the mailbox.
2209 void
2210 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2212 wait_queue_head_t *pdone_q;
2213 unsigned long drvr_flag;
2216 * If pdone_q is empty, the driver thread gave up waiting and
2217 * continued running.
2219 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2220 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2221 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2222 if (pdone_q)
2223 wake_up_interruptible(pdone_q);
2224 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2225 return;
2230 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2231 * @phba: Pointer to HBA context object.
2232 * @pmb: Pointer to mailbox object.
2234 * This function is the default mailbox completion handler. It
2235 * frees the memory resources associated with the completed mailbox
2236 * command. If the completed command is a REG_LOGIN mailbox command,
2237 * this function will issue a UREG_LOGIN to re-claim the RPI.
2239 void
2240 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2242 struct lpfc_vport *vport = pmb->vport;
2243 struct lpfc_dmabuf *mp;
2244 struct lpfc_nodelist *ndlp;
2245 struct Scsi_Host *shost;
2246 uint16_t rpi, vpi;
2247 int rc;
2249 mp = (struct lpfc_dmabuf *) (pmb->context1);
2251 if (mp) {
2252 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2253 kfree(mp);
2257 * If a REG_LOGIN succeeded after node is destroyed or node
2258 * is in re-discovery driver need to cleanup the RPI.
2260 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2261 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2262 !pmb->u.mb.mbxStatus) {
2263 rpi = pmb->u.mb.un.varWords[0];
2264 vpi = pmb->u.mb.un.varRegLogin.vpi;
2265 lpfc_unreg_login(phba, vpi, rpi, pmb);
2266 pmb->vport = vport;
2267 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2268 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2269 if (rc != MBX_NOT_FINISHED)
2270 return;
2273 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2274 !(phba->pport->load_flag & FC_UNLOADING) &&
2275 !pmb->u.mb.mbxStatus) {
2276 shost = lpfc_shost_from_vport(vport);
2277 spin_lock_irq(shost->host_lock);
2278 vport->vpi_state |= LPFC_VPI_REGISTERED;
2279 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2280 spin_unlock_irq(shost->host_lock);
2283 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2284 ndlp = (struct lpfc_nodelist *)pmb->context2;
2285 lpfc_nlp_put(ndlp);
2286 pmb->context2 = NULL;
2289 /* Check security permission status on INIT_LINK mailbox command */
2290 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2291 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2293 "2860 SLI authentication is required "
2294 "for INIT_LINK but has not done yet\n");
2296 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2297 lpfc_sli4_mbox_cmd_free(phba, pmb);
2298 else
2299 mempool_free(pmb, phba->mbox_mem_pool);
2302 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2303 * @phba: Pointer to HBA context object.
2304 * @pmb: Pointer to mailbox object.
2306 * This function is the unreg rpi mailbox completion handler. It
2307 * frees the memory resources associated with the completed mailbox
2308 * command. An additional refrenece is put on the ndlp to prevent
2309 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2310 * the unreg mailbox command completes, this routine puts the
2311 * reference back.
2314 void
2315 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2317 struct lpfc_vport *vport = pmb->vport;
2318 struct lpfc_nodelist *ndlp;
2320 ndlp = pmb->context1;
2321 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2322 if (phba->sli_rev == LPFC_SLI_REV4 &&
2323 (bf_get(lpfc_sli_intf_if_type,
2324 &phba->sli4_hba.sli_intf) ==
2325 LPFC_SLI_INTF_IF_TYPE_2)) {
2326 if (ndlp) {
2327 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2328 "0010 UNREG_LOGIN vpi:%x "
2329 "rpi:%x DID:%x map:%x %p\n",
2330 vport->vpi, ndlp->nlp_rpi,
2331 ndlp->nlp_DID,
2332 ndlp->nlp_usg_map, ndlp);
2333 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2334 lpfc_nlp_put(ndlp);
2339 mempool_free(pmb, phba->mbox_mem_pool);
2343 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2344 * @phba: Pointer to HBA context object.
2346 * This function is called with no lock held. This function processes all
2347 * the completed mailbox commands and gives it to upper layers. The interrupt
2348 * service routine processes mailbox completion interrupt and adds completed
2349 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2350 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2351 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2352 * function returns the mailbox commands to the upper layer by calling the
2353 * completion handler function of each mailbox.
2356 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2358 MAILBOX_t *pmbox;
2359 LPFC_MBOXQ_t *pmb;
2360 int rc;
2361 LIST_HEAD(cmplq);
2363 phba->sli.slistat.mbox_event++;
2365 /* Get all completed mailboxe buffers into the cmplq */
2366 spin_lock_irq(&phba->hbalock);
2367 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2368 spin_unlock_irq(&phba->hbalock);
2370 /* Get a Mailbox buffer to setup mailbox commands for callback */
2371 do {
2372 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2373 if (pmb == NULL)
2374 break;
2376 pmbox = &pmb->u.mb;
2378 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2379 if (pmb->vport) {
2380 lpfc_debugfs_disc_trc(pmb->vport,
2381 LPFC_DISC_TRC_MBOX_VPORT,
2382 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2383 (uint32_t)pmbox->mbxCommand,
2384 pmbox->un.varWords[0],
2385 pmbox->un.varWords[1]);
2387 else {
2388 lpfc_debugfs_disc_trc(phba->pport,
2389 LPFC_DISC_TRC_MBOX,
2390 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2391 (uint32_t)pmbox->mbxCommand,
2392 pmbox->un.varWords[0],
2393 pmbox->un.varWords[1]);
2398 * It is a fatal error if unknown mbox command completion.
2400 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2401 MBX_SHUTDOWN) {
2402 /* Unknown mailbox command compl */
2403 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2404 "(%d):0323 Unknown Mailbox command "
2405 "x%x (x%x/x%x) Cmpl\n",
2406 pmb->vport ? pmb->vport->vpi : 0,
2407 pmbox->mbxCommand,
2408 lpfc_sli_config_mbox_subsys_get(phba,
2409 pmb),
2410 lpfc_sli_config_mbox_opcode_get(phba,
2411 pmb));
2412 phba->link_state = LPFC_HBA_ERROR;
2413 phba->work_hs = HS_FFER3;
2414 lpfc_handle_eratt(phba);
2415 continue;
2418 if (pmbox->mbxStatus) {
2419 phba->sli.slistat.mbox_stat_err++;
2420 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2421 /* Mbox cmd cmpl error - RETRYing */
2422 lpfc_printf_log(phba, KERN_INFO,
2423 LOG_MBOX | LOG_SLI,
2424 "(%d):0305 Mbox cmd cmpl "
2425 "error - RETRYing Data: x%x "
2426 "(x%x/x%x) x%x x%x x%x\n",
2427 pmb->vport ? pmb->vport->vpi : 0,
2428 pmbox->mbxCommand,
2429 lpfc_sli_config_mbox_subsys_get(phba,
2430 pmb),
2431 lpfc_sli_config_mbox_opcode_get(phba,
2432 pmb),
2433 pmbox->mbxStatus,
2434 pmbox->un.varWords[0],
2435 pmb->vport->port_state);
2436 pmbox->mbxStatus = 0;
2437 pmbox->mbxOwner = OWN_HOST;
2438 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2439 if (rc != MBX_NOT_FINISHED)
2440 continue;
2444 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2445 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2446 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2447 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2448 "x%x x%x x%x\n",
2449 pmb->vport ? pmb->vport->vpi : 0,
2450 pmbox->mbxCommand,
2451 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2452 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2453 pmb->mbox_cmpl,
2454 *((uint32_t *) pmbox),
2455 pmbox->un.varWords[0],
2456 pmbox->un.varWords[1],
2457 pmbox->un.varWords[2],
2458 pmbox->un.varWords[3],
2459 pmbox->un.varWords[4],
2460 pmbox->un.varWords[5],
2461 pmbox->un.varWords[6],
2462 pmbox->un.varWords[7],
2463 pmbox->un.varWords[8],
2464 pmbox->un.varWords[9],
2465 pmbox->un.varWords[10]);
2467 if (pmb->mbox_cmpl)
2468 pmb->mbox_cmpl(phba,pmb);
2469 } while (1);
2470 return 0;
2474 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2475 * @phba: Pointer to HBA context object.
2476 * @pring: Pointer to driver SLI ring object.
2477 * @tag: buffer tag.
2479 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2480 * is set in the tag the buffer is posted for a particular exchange,
2481 * the function will return the buffer without replacing the buffer.
2482 * If the buffer is for unsolicited ELS or CT traffic, this function
2483 * returns the buffer and also posts another buffer to the firmware.
2485 static struct lpfc_dmabuf *
2486 lpfc_sli_get_buff(struct lpfc_hba *phba,
2487 struct lpfc_sli_ring *pring,
2488 uint32_t tag)
2490 struct hbq_dmabuf *hbq_entry;
2492 if (tag & QUE_BUFTAG_BIT)
2493 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2494 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2495 if (!hbq_entry)
2496 return NULL;
2497 return &hbq_entry->dbuf;
2501 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2502 * @phba: Pointer to HBA context object.
2503 * @pring: Pointer to driver SLI ring object.
2504 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2505 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2506 * @fch_type: the type for the first frame of the sequence.
2508 * This function is called with no lock held. This function uses the r_ctl and
2509 * type of the received sequence to find the correct callback function to call
2510 * to process the sequence.
2512 static int
2513 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2514 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2515 uint32_t fch_type)
2517 int i;
2519 switch (fch_type) {
2520 case FC_TYPE_NVME:
2521 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2522 return 1;
2523 default:
2524 break;
2527 /* unSolicited Responses */
2528 if (pring->prt[0].profile) {
2529 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2530 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2531 saveq);
2532 return 1;
2534 /* We must search, based on rctl / type
2535 for the right routine */
2536 for (i = 0; i < pring->num_mask; i++) {
2537 if ((pring->prt[i].rctl == fch_r_ctl) &&
2538 (pring->prt[i].type == fch_type)) {
2539 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2540 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2541 (phba, pring, saveq);
2542 return 1;
2545 return 0;
2549 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @saveq: Pointer to the unsolicited iocb.
2554 * This function is called with no lock held by the ring event handler
2555 * when there is an unsolicited iocb posted to the response ring by the
2556 * firmware. This function gets the buffer associated with the iocbs
2557 * and calls the event handler for the ring. This function handles both
2558 * qring buffers and hbq buffers.
2559 * When the function returns 1 the caller can free the iocb object otherwise
2560 * upper layer functions will free the iocb objects.
2562 static int
2563 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2564 struct lpfc_iocbq *saveq)
2566 IOCB_t * irsp;
2567 WORD5 * w5p;
2568 uint32_t Rctl, Type;
2569 struct lpfc_iocbq *iocbq;
2570 struct lpfc_dmabuf *dmzbuf;
2572 irsp = &(saveq->iocb);
2574 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2575 if (pring->lpfc_sli_rcv_async_status)
2576 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2577 else
2578 lpfc_printf_log(phba,
2579 KERN_WARNING,
2580 LOG_SLI,
2581 "0316 Ring %d handler: unexpected "
2582 "ASYNC_STATUS iocb received evt_code "
2583 "0x%x\n",
2584 pring->ringno,
2585 irsp->un.asyncstat.evt_code);
2586 return 1;
2589 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2590 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2591 if (irsp->ulpBdeCount > 0) {
2592 dmzbuf = lpfc_sli_get_buff(phba, pring,
2593 irsp->un.ulpWord[3]);
2594 lpfc_in_buf_free(phba, dmzbuf);
2597 if (irsp->ulpBdeCount > 1) {
2598 dmzbuf = lpfc_sli_get_buff(phba, pring,
2599 irsp->unsli3.sli3Words[3]);
2600 lpfc_in_buf_free(phba, dmzbuf);
2603 if (irsp->ulpBdeCount > 2) {
2604 dmzbuf = lpfc_sli_get_buff(phba, pring,
2605 irsp->unsli3.sli3Words[7]);
2606 lpfc_in_buf_free(phba, dmzbuf);
2609 return 1;
2612 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2613 if (irsp->ulpBdeCount != 0) {
2614 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2615 irsp->un.ulpWord[3]);
2616 if (!saveq->context2)
2617 lpfc_printf_log(phba,
2618 KERN_ERR,
2619 LOG_SLI,
2620 "0341 Ring %d Cannot find buffer for "
2621 "an unsolicited iocb. tag 0x%x\n",
2622 pring->ringno,
2623 irsp->un.ulpWord[3]);
2625 if (irsp->ulpBdeCount == 2) {
2626 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2627 irsp->unsli3.sli3Words[7]);
2628 if (!saveq->context3)
2629 lpfc_printf_log(phba,
2630 KERN_ERR,
2631 LOG_SLI,
2632 "0342 Ring %d Cannot find buffer for an"
2633 " unsolicited iocb. tag 0x%x\n",
2634 pring->ringno,
2635 irsp->unsli3.sli3Words[7]);
2637 list_for_each_entry(iocbq, &saveq->list, list) {
2638 irsp = &(iocbq->iocb);
2639 if (irsp->ulpBdeCount != 0) {
2640 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2641 irsp->un.ulpWord[3]);
2642 if (!iocbq->context2)
2643 lpfc_printf_log(phba,
2644 KERN_ERR,
2645 LOG_SLI,
2646 "0343 Ring %d Cannot find "
2647 "buffer for an unsolicited iocb"
2648 ". tag 0x%x\n", pring->ringno,
2649 irsp->un.ulpWord[3]);
2651 if (irsp->ulpBdeCount == 2) {
2652 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2653 irsp->unsli3.sli3Words[7]);
2654 if (!iocbq->context3)
2655 lpfc_printf_log(phba,
2656 KERN_ERR,
2657 LOG_SLI,
2658 "0344 Ring %d Cannot find "
2659 "buffer for an unsolicited "
2660 "iocb. tag 0x%x\n",
2661 pring->ringno,
2662 irsp->unsli3.sli3Words[7]);
2666 if (irsp->ulpBdeCount != 0 &&
2667 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2668 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2669 int found = 0;
2671 /* search continue save q for same XRI */
2672 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2673 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2674 saveq->iocb.unsli3.rcvsli3.ox_id) {
2675 list_add_tail(&saveq->list, &iocbq->list);
2676 found = 1;
2677 break;
2680 if (!found)
2681 list_add_tail(&saveq->clist,
2682 &pring->iocb_continue_saveq);
2683 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2684 list_del_init(&iocbq->clist);
2685 saveq = iocbq;
2686 irsp = &(saveq->iocb);
2687 } else
2688 return 0;
2690 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2691 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2692 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2693 Rctl = FC_RCTL_ELS_REQ;
2694 Type = FC_TYPE_ELS;
2695 } else {
2696 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2697 Rctl = w5p->hcsw.Rctl;
2698 Type = w5p->hcsw.Type;
2700 /* Firmware Workaround */
2701 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2702 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2703 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2704 Rctl = FC_RCTL_ELS_REQ;
2705 Type = FC_TYPE_ELS;
2706 w5p->hcsw.Rctl = Rctl;
2707 w5p->hcsw.Type = Type;
2711 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2712 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2713 "0313 Ring %d handler: unexpected Rctl x%x "
2714 "Type x%x received\n",
2715 pring->ringno, Rctl, Type);
2717 return 1;
2721 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2722 * @phba: Pointer to HBA context object.
2723 * @pring: Pointer to driver SLI ring object.
2724 * @prspiocb: Pointer to response iocb object.
2726 * This function looks up the iocb_lookup table to get the command iocb
2727 * corresponding to the given response iocb using the iotag of the
2728 * response iocb. This function is called with the hbalock held.
2729 * This function returns the command iocb object if it finds the command
2730 * iocb else returns NULL.
2732 static struct lpfc_iocbq *
2733 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2734 struct lpfc_sli_ring *pring,
2735 struct lpfc_iocbq *prspiocb)
2737 struct lpfc_iocbq *cmd_iocb = NULL;
2738 uint16_t iotag;
2739 lockdep_assert_held(&phba->hbalock);
2741 iotag = prspiocb->iocb.ulpIoTag;
2743 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2744 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2745 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2746 /* remove from txcmpl queue list */
2747 list_del_init(&cmd_iocb->list);
2748 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2749 return cmd_iocb;
2753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2754 "0317 iotag x%x is out of "
2755 "range: max iotag x%x wd0 x%x\n",
2756 iotag, phba->sli.last_iotag,
2757 *(((uint32_t *) &prspiocb->iocb) + 7));
2758 return NULL;
2762 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2763 * @phba: Pointer to HBA context object.
2764 * @pring: Pointer to driver SLI ring object.
2765 * @iotag: IOCB tag.
2767 * This function looks up the iocb_lookup table to get the command iocb
2768 * corresponding to the given iotag. This function is called with the
2769 * hbalock held.
2770 * This function returns the command iocb object if it finds the command
2771 * iocb else returns NULL.
2773 static struct lpfc_iocbq *
2774 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2775 struct lpfc_sli_ring *pring, uint16_t iotag)
2777 struct lpfc_iocbq *cmd_iocb = NULL;
2779 lockdep_assert_held(&phba->hbalock);
2780 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2781 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2782 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2783 /* remove from txcmpl queue list */
2784 list_del_init(&cmd_iocb->list);
2785 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2786 return cmd_iocb;
2790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2791 "0372 iotag x%x lookup error: max iotag (x%x) "
2792 "iocb_flag x%x\n",
2793 iotag, phba->sli.last_iotag,
2794 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
2795 return NULL;
2799 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2800 * @phba: Pointer to HBA context object.
2801 * @pring: Pointer to driver SLI ring object.
2802 * @saveq: Pointer to the response iocb to be processed.
2804 * This function is called by the ring event handler for non-fcp
2805 * rings when there is a new response iocb in the response ring.
2806 * The caller is not required to hold any locks. This function
2807 * gets the command iocb associated with the response iocb and
2808 * calls the completion handler for the command iocb. If there
2809 * is no completion handler, the function will free the resources
2810 * associated with command iocb. If the response iocb is for
2811 * an already aborted command iocb, the status of the completion
2812 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2813 * This function always returns 1.
2815 static int
2816 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2817 struct lpfc_iocbq *saveq)
2819 struct lpfc_iocbq *cmdiocbp;
2820 int rc = 1;
2821 unsigned long iflag;
2823 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2824 spin_lock_irqsave(&phba->hbalock, iflag);
2825 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2826 spin_unlock_irqrestore(&phba->hbalock, iflag);
2828 if (cmdiocbp) {
2829 if (cmdiocbp->iocb_cmpl) {
2831 * If an ELS command failed send an event to mgmt
2832 * application.
2834 if (saveq->iocb.ulpStatus &&
2835 (pring->ringno == LPFC_ELS_RING) &&
2836 (cmdiocbp->iocb.ulpCommand ==
2837 CMD_ELS_REQUEST64_CR))
2838 lpfc_send_els_failure_event(phba,
2839 cmdiocbp, saveq);
2842 * Post all ELS completions to the worker thread.
2843 * All other are passed to the completion callback.
2845 if (pring->ringno == LPFC_ELS_RING) {
2846 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2847 (cmdiocbp->iocb_flag &
2848 LPFC_DRIVER_ABORTED)) {
2849 spin_lock_irqsave(&phba->hbalock,
2850 iflag);
2851 cmdiocbp->iocb_flag &=
2852 ~LPFC_DRIVER_ABORTED;
2853 spin_unlock_irqrestore(&phba->hbalock,
2854 iflag);
2855 saveq->iocb.ulpStatus =
2856 IOSTAT_LOCAL_REJECT;
2857 saveq->iocb.un.ulpWord[4] =
2858 IOERR_SLI_ABORTED;
2860 /* Firmware could still be in progress
2861 * of DMAing payload, so don't free data
2862 * buffer till after a hbeat.
2864 spin_lock_irqsave(&phba->hbalock,
2865 iflag);
2866 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2867 spin_unlock_irqrestore(&phba->hbalock,
2868 iflag);
2870 if (phba->sli_rev == LPFC_SLI_REV4) {
2871 if (saveq->iocb_flag &
2872 LPFC_EXCHANGE_BUSY) {
2873 /* Set cmdiocb flag for the
2874 * exchange busy so sgl (xri)
2875 * will not be released until
2876 * the abort xri is received
2877 * from hba.
2879 spin_lock_irqsave(
2880 &phba->hbalock, iflag);
2881 cmdiocbp->iocb_flag |=
2882 LPFC_EXCHANGE_BUSY;
2883 spin_unlock_irqrestore(
2884 &phba->hbalock, iflag);
2886 if (cmdiocbp->iocb_flag &
2887 LPFC_DRIVER_ABORTED) {
2889 * Clear LPFC_DRIVER_ABORTED
2890 * bit in case it was driver
2891 * initiated abort.
2893 spin_lock_irqsave(
2894 &phba->hbalock, iflag);
2895 cmdiocbp->iocb_flag &=
2896 ~LPFC_DRIVER_ABORTED;
2897 spin_unlock_irqrestore(
2898 &phba->hbalock, iflag);
2899 cmdiocbp->iocb.ulpStatus =
2900 IOSTAT_LOCAL_REJECT;
2901 cmdiocbp->iocb.un.ulpWord[4] =
2902 IOERR_ABORT_REQUESTED;
2904 * For SLI4, irsiocb contains
2905 * NO_XRI in sli_xritag, it
2906 * shall not affect releasing
2907 * sgl (xri) process.
2909 saveq->iocb.ulpStatus =
2910 IOSTAT_LOCAL_REJECT;
2911 saveq->iocb.un.ulpWord[4] =
2912 IOERR_SLI_ABORTED;
2913 spin_lock_irqsave(
2914 &phba->hbalock, iflag);
2915 saveq->iocb_flag |=
2916 LPFC_DELAY_MEM_FREE;
2917 spin_unlock_irqrestore(
2918 &phba->hbalock, iflag);
2922 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2923 } else
2924 lpfc_sli_release_iocbq(phba, cmdiocbp);
2925 } else {
2927 * Unknown initiating command based on the response iotag.
2928 * This could be the case on the ELS ring because of
2929 * lpfc_els_abort().
2931 if (pring->ringno != LPFC_ELS_RING) {
2933 * Ring <ringno> handler: unexpected completion IoTag
2934 * <IoTag>
2936 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2937 "0322 Ring %d handler: "
2938 "unexpected completion IoTag x%x "
2939 "Data: x%x x%x x%x x%x\n",
2940 pring->ringno,
2941 saveq->iocb.ulpIoTag,
2942 saveq->iocb.ulpStatus,
2943 saveq->iocb.un.ulpWord[4],
2944 saveq->iocb.ulpCommand,
2945 saveq->iocb.ulpContext);
2949 return rc;
2953 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2954 * @phba: Pointer to HBA context object.
2955 * @pring: Pointer to driver SLI ring object.
2957 * This function is called from the iocb ring event handlers when
2958 * put pointer is ahead of the get pointer for a ring. This function signal
2959 * an error attention condition to the worker thread and the worker
2960 * thread will transition the HBA to offline state.
2962 static void
2963 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2965 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2967 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2968 * rsp ring <portRspMax>
2970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2971 "0312 Ring %d handler: portRspPut %d "
2972 "is bigger than rsp ring %d\n",
2973 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2974 pring->sli.sli3.numRiocb);
2976 phba->link_state = LPFC_HBA_ERROR;
2979 * All error attention handlers are posted to
2980 * worker thread
2982 phba->work_ha |= HA_ERATT;
2983 phba->work_hs = HS_FFER3;
2985 lpfc_worker_wake_up(phba);
2987 return;
2991 * lpfc_poll_eratt - Error attention polling timer timeout handler
2992 * @ptr: Pointer to address of HBA context object.
2994 * This function is invoked by the Error Attention polling timer when the
2995 * timer times out. It will check the SLI Error Attention register for
2996 * possible attention events. If so, it will post an Error Attention event
2997 * and wake up worker thread to process it. Otherwise, it will set up the
2998 * Error Attention polling timer for the next poll.
3000 void lpfc_poll_eratt(unsigned long ptr)
3002 struct lpfc_hba *phba;
3003 uint32_t eratt = 0;
3004 uint64_t sli_intr, cnt;
3006 phba = (struct lpfc_hba *)ptr;
3008 /* Here we will also keep track of interrupts per sec of the hba */
3009 sli_intr = phba->sli.slistat.sli_intr;
3011 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3012 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3013 sli_intr);
3014 else
3015 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3017 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3018 do_div(cnt, phba->eratt_poll_interval);
3019 phba->sli.slistat.sli_ips = cnt;
3021 phba->sli.slistat.sli_prev_intr = sli_intr;
3023 /* Check chip HA register for error event */
3024 eratt = lpfc_sli_check_eratt(phba);
3026 if (eratt)
3027 /* Tell the worker thread there is work to do */
3028 lpfc_worker_wake_up(phba);
3029 else
3030 /* Restart the timer for next eratt poll */
3031 mod_timer(&phba->eratt_poll,
3032 jiffies +
3033 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3034 return;
3039 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3040 * @phba: Pointer to HBA context object.
3041 * @pring: Pointer to driver SLI ring object.
3042 * @mask: Host attention register mask for this ring.
3044 * This function is called from the interrupt context when there is a ring
3045 * event for the fcp ring. The caller does not hold any lock.
3046 * The function processes each response iocb in the response ring until it
3047 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3048 * LE bit set. The function will call the completion handler of the command iocb
3049 * if the response iocb indicates a completion for a command iocb or it is
3050 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3051 * function if this is an unsolicited iocb.
3052 * This routine presumes LPFC_FCP_RING handling and doesn't bother
3053 * to check it explicitly.
3056 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3057 struct lpfc_sli_ring *pring, uint32_t mask)
3059 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3060 IOCB_t *irsp = NULL;
3061 IOCB_t *entry = NULL;
3062 struct lpfc_iocbq *cmdiocbq = NULL;
3063 struct lpfc_iocbq rspiocbq;
3064 uint32_t status;
3065 uint32_t portRspPut, portRspMax;
3066 int rc = 1;
3067 lpfc_iocb_type type;
3068 unsigned long iflag;
3069 uint32_t rsp_cmpl = 0;
3071 spin_lock_irqsave(&phba->hbalock, iflag);
3072 pring->stats.iocb_event++;
3075 * The next available response entry should never exceed the maximum
3076 * entries. If it does, treat it as an adapter hardware error.
3078 portRspMax = pring->sli.sli3.numRiocb;
3079 portRspPut = le32_to_cpu(pgp->rspPutInx);
3080 if (unlikely(portRspPut >= portRspMax)) {
3081 lpfc_sli_rsp_pointers_error(phba, pring);
3082 spin_unlock_irqrestore(&phba->hbalock, iflag);
3083 return 1;
3085 if (phba->fcp_ring_in_use) {
3086 spin_unlock_irqrestore(&phba->hbalock, iflag);
3087 return 1;
3088 } else
3089 phba->fcp_ring_in_use = 1;
3091 rmb();
3092 while (pring->sli.sli3.rspidx != portRspPut) {
3094 * Fetch an entry off the ring and copy it into a local data
3095 * structure. The copy involves a byte-swap since the
3096 * network byte order and pci byte orders are different.
3098 entry = lpfc_resp_iocb(phba, pring);
3099 phba->last_completion_time = jiffies;
3101 if (++pring->sli.sli3.rspidx >= portRspMax)
3102 pring->sli.sli3.rspidx = 0;
3104 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3105 (uint32_t *) &rspiocbq.iocb,
3106 phba->iocb_rsp_size);
3107 INIT_LIST_HEAD(&(rspiocbq.list));
3108 irsp = &rspiocbq.iocb;
3110 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3111 pring->stats.iocb_rsp++;
3112 rsp_cmpl++;
3114 if (unlikely(irsp->ulpStatus)) {
3116 * If resource errors reported from HBA, reduce
3117 * queuedepths of the SCSI device.
3119 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3120 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3121 IOERR_NO_RESOURCES)) {
3122 spin_unlock_irqrestore(&phba->hbalock, iflag);
3123 phba->lpfc_rampdown_queue_depth(phba);
3124 spin_lock_irqsave(&phba->hbalock, iflag);
3127 /* Rsp ring <ringno> error: IOCB */
3128 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3129 "0336 Rsp Ring %d error: IOCB Data: "
3130 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3131 pring->ringno,
3132 irsp->un.ulpWord[0],
3133 irsp->un.ulpWord[1],
3134 irsp->un.ulpWord[2],
3135 irsp->un.ulpWord[3],
3136 irsp->un.ulpWord[4],
3137 irsp->un.ulpWord[5],
3138 *(uint32_t *)&irsp->un1,
3139 *((uint32_t *)&irsp->un1 + 1));
3142 switch (type) {
3143 case LPFC_ABORT_IOCB:
3144 case LPFC_SOL_IOCB:
3146 * Idle exchange closed via ABTS from port. No iocb
3147 * resources need to be recovered.
3149 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3150 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3151 "0333 IOCB cmd 0x%x"
3152 " processed. Skipping"
3153 " completion\n",
3154 irsp->ulpCommand);
3155 break;
3158 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3159 &rspiocbq);
3160 if (unlikely(!cmdiocbq))
3161 break;
3162 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3163 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3164 if (cmdiocbq->iocb_cmpl) {
3165 spin_unlock_irqrestore(&phba->hbalock, iflag);
3166 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3167 &rspiocbq);
3168 spin_lock_irqsave(&phba->hbalock, iflag);
3170 break;
3171 case LPFC_UNSOL_IOCB:
3172 spin_unlock_irqrestore(&phba->hbalock, iflag);
3173 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3174 spin_lock_irqsave(&phba->hbalock, iflag);
3175 break;
3176 default:
3177 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3178 char adaptermsg[LPFC_MAX_ADPTMSG];
3179 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3180 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3181 MAX_MSG_DATA);
3182 dev_warn(&((phba->pcidev)->dev),
3183 "lpfc%d: %s\n",
3184 phba->brd_no, adaptermsg);
3185 } else {
3186 /* Unknown IOCB command */
3187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3188 "0334 Unknown IOCB command "
3189 "Data: x%x, x%x x%x x%x x%x\n",
3190 type, irsp->ulpCommand,
3191 irsp->ulpStatus,
3192 irsp->ulpIoTag,
3193 irsp->ulpContext);
3195 break;
3199 * The response IOCB has been processed. Update the ring
3200 * pointer in SLIM. If the port response put pointer has not
3201 * been updated, sync the pgp->rspPutInx and fetch the new port
3202 * response put pointer.
3204 writel(pring->sli.sli3.rspidx,
3205 &phba->host_gp[pring->ringno].rspGetInx);
3207 if (pring->sli.sli3.rspidx == portRspPut)
3208 portRspPut = le32_to_cpu(pgp->rspPutInx);
3211 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3212 pring->stats.iocb_rsp_full++;
3213 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3214 writel(status, phba->CAregaddr);
3215 readl(phba->CAregaddr);
3217 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3218 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3219 pring->stats.iocb_cmd_empty++;
3221 /* Force update of the local copy of cmdGetInx */
3222 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3223 lpfc_sli_resume_iocb(phba, pring);
3225 if ((pring->lpfc_sli_cmd_available))
3226 (pring->lpfc_sli_cmd_available) (phba, pring);
3230 phba->fcp_ring_in_use = 0;
3231 spin_unlock_irqrestore(&phba->hbalock, iflag);
3232 return rc;
3236 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3237 * @phba: Pointer to HBA context object.
3238 * @pring: Pointer to driver SLI ring object.
3239 * @rspiocbp: Pointer to driver response IOCB object.
3241 * This function is called from the worker thread when there is a slow-path
3242 * response IOCB to process. This function chains all the response iocbs until
3243 * seeing the iocb with the LE bit set. The function will call
3244 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3245 * completion of a command iocb. The function will call the
3246 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3247 * The function frees the resources or calls the completion handler if this
3248 * iocb is an abort completion. The function returns NULL when the response
3249 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3250 * this function shall chain the iocb on to the iocb_continueq and return the
3251 * response iocb passed in.
3253 static struct lpfc_iocbq *
3254 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3255 struct lpfc_iocbq *rspiocbp)
3257 struct lpfc_iocbq *saveq;
3258 struct lpfc_iocbq *cmdiocbp;
3259 struct lpfc_iocbq *next_iocb;
3260 IOCB_t *irsp = NULL;
3261 uint32_t free_saveq;
3262 uint8_t iocb_cmd_type;
3263 lpfc_iocb_type type;
3264 unsigned long iflag;
3265 int rc;
3267 spin_lock_irqsave(&phba->hbalock, iflag);
3268 /* First add the response iocb to the countinueq list */
3269 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3270 pring->iocb_continueq_cnt++;
3272 /* Now, determine whether the list is completed for processing */
3273 irsp = &rspiocbp->iocb;
3274 if (irsp->ulpLe) {
3276 * By default, the driver expects to free all resources
3277 * associated with this iocb completion.
3279 free_saveq = 1;
3280 saveq = list_get_first(&pring->iocb_continueq,
3281 struct lpfc_iocbq, list);
3282 irsp = &(saveq->iocb);
3283 list_del_init(&pring->iocb_continueq);
3284 pring->iocb_continueq_cnt = 0;
3286 pring->stats.iocb_rsp++;
3289 * If resource errors reported from HBA, reduce
3290 * queuedepths of the SCSI device.
3292 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3293 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3294 IOERR_NO_RESOURCES)) {
3295 spin_unlock_irqrestore(&phba->hbalock, iflag);
3296 phba->lpfc_rampdown_queue_depth(phba);
3297 spin_lock_irqsave(&phba->hbalock, iflag);
3300 if (irsp->ulpStatus) {
3301 /* Rsp ring <ringno> error: IOCB */
3302 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3303 "0328 Rsp Ring %d error: "
3304 "IOCB Data: "
3305 "x%x x%x x%x x%x "
3306 "x%x x%x x%x x%x "
3307 "x%x x%x x%x x%x "
3308 "x%x x%x x%x x%x\n",
3309 pring->ringno,
3310 irsp->un.ulpWord[0],
3311 irsp->un.ulpWord[1],
3312 irsp->un.ulpWord[2],
3313 irsp->un.ulpWord[3],
3314 irsp->un.ulpWord[4],
3315 irsp->un.ulpWord[5],
3316 *(((uint32_t *) irsp) + 6),
3317 *(((uint32_t *) irsp) + 7),
3318 *(((uint32_t *) irsp) + 8),
3319 *(((uint32_t *) irsp) + 9),
3320 *(((uint32_t *) irsp) + 10),
3321 *(((uint32_t *) irsp) + 11),
3322 *(((uint32_t *) irsp) + 12),
3323 *(((uint32_t *) irsp) + 13),
3324 *(((uint32_t *) irsp) + 14),
3325 *(((uint32_t *) irsp) + 15));
3329 * Fetch the IOCB command type and call the correct completion
3330 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3331 * get freed back to the lpfc_iocb_list by the discovery
3332 * kernel thread.
3334 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3335 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3336 switch (type) {
3337 case LPFC_SOL_IOCB:
3338 spin_unlock_irqrestore(&phba->hbalock, iflag);
3339 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3340 spin_lock_irqsave(&phba->hbalock, iflag);
3341 break;
3343 case LPFC_UNSOL_IOCB:
3344 spin_unlock_irqrestore(&phba->hbalock, iflag);
3345 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3346 spin_lock_irqsave(&phba->hbalock, iflag);
3347 if (!rc)
3348 free_saveq = 0;
3349 break;
3351 case LPFC_ABORT_IOCB:
3352 cmdiocbp = NULL;
3353 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3354 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3355 saveq);
3356 if (cmdiocbp) {
3357 /* Call the specified completion routine */
3358 if (cmdiocbp->iocb_cmpl) {
3359 spin_unlock_irqrestore(&phba->hbalock,
3360 iflag);
3361 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3362 saveq);
3363 spin_lock_irqsave(&phba->hbalock,
3364 iflag);
3365 } else
3366 __lpfc_sli_release_iocbq(phba,
3367 cmdiocbp);
3369 break;
3371 case LPFC_UNKNOWN_IOCB:
3372 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3373 char adaptermsg[LPFC_MAX_ADPTMSG];
3374 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3375 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3376 MAX_MSG_DATA);
3377 dev_warn(&((phba->pcidev)->dev),
3378 "lpfc%d: %s\n",
3379 phba->brd_no, adaptermsg);
3380 } else {
3381 /* Unknown IOCB command */
3382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3383 "0335 Unknown IOCB "
3384 "command Data: x%x "
3385 "x%x x%x x%x\n",
3386 irsp->ulpCommand,
3387 irsp->ulpStatus,
3388 irsp->ulpIoTag,
3389 irsp->ulpContext);
3391 break;
3394 if (free_saveq) {
3395 list_for_each_entry_safe(rspiocbp, next_iocb,
3396 &saveq->list, list) {
3397 list_del_init(&rspiocbp->list);
3398 __lpfc_sli_release_iocbq(phba, rspiocbp);
3400 __lpfc_sli_release_iocbq(phba, saveq);
3402 rspiocbp = NULL;
3404 spin_unlock_irqrestore(&phba->hbalock, iflag);
3405 return rspiocbp;
3409 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3410 * @phba: Pointer to HBA context object.
3411 * @pring: Pointer to driver SLI ring object.
3412 * @mask: Host attention register mask for this ring.
3414 * This routine wraps the actual slow_ring event process routine from the
3415 * API jump table function pointer from the lpfc_hba struct.
3417 void
3418 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3419 struct lpfc_sli_ring *pring, uint32_t mask)
3421 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3425 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3426 * @phba: Pointer to HBA context object.
3427 * @pring: Pointer to driver SLI ring object.
3428 * @mask: Host attention register mask for this ring.
3430 * This function is called from the worker thread when there is a ring event
3431 * for non-fcp rings. The caller does not hold any lock. The function will
3432 * remove each response iocb in the response ring and calls the handle
3433 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3435 static void
3436 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3437 struct lpfc_sli_ring *pring, uint32_t mask)
3439 struct lpfc_pgp *pgp;
3440 IOCB_t *entry;
3441 IOCB_t *irsp = NULL;
3442 struct lpfc_iocbq *rspiocbp = NULL;
3443 uint32_t portRspPut, portRspMax;
3444 unsigned long iflag;
3445 uint32_t status;
3447 pgp = &phba->port_gp[pring->ringno];
3448 spin_lock_irqsave(&phba->hbalock, iflag);
3449 pring->stats.iocb_event++;
3452 * The next available response entry should never exceed the maximum
3453 * entries. If it does, treat it as an adapter hardware error.
3455 portRspMax = pring->sli.sli3.numRiocb;
3456 portRspPut = le32_to_cpu(pgp->rspPutInx);
3457 if (portRspPut >= portRspMax) {
3459 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3460 * rsp ring <portRspMax>
3462 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3463 "0303 Ring %d handler: portRspPut %d "
3464 "is bigger than rsp ring %d\n",
3465 pring->ringno, portRspPut, portRspMax);
3467 phba->link_state = LPFC_HBA_ERROR;
3468 spin_unlock_irqrestore(&phba->hbalock, iflag);
3470 phba->work_hs = HS_FFER3;
3471 lpfc_handle_eratt(phba);
3473 return;
3476 rmb();
3477 while (pring->sli.sli3.rspidx != portRspPut) {
3479 * Build a completion list and call the appropriate handler.
3480 * The process is to get the next available response iocb, get
3481 * a free iocb from the list, copy the response data into the
3482 * free iocb, insert to the continuation list, and update the
3483 * next response index to slim. This process makes response
3484 * iocb's in the ring available to DMA as fast as possible but
3485 * pays a penalty for a copy operation. Since the iocb is
3486 * only 32 bytes, this penalty is considered small relative to
3487 * the PCI reads for register values and a slim write. When
3488 * the ulpLe field is set, the entire Command has been
3489 * received.
3491 entry = lpfc_resp_iocb(phba, pring);
3493 phba->last_completion_time = jiffies;
3494 rspiocbp = __lpfc_sli_get_iocbq(phba);
3495 if (rspiocbp == NULL) {
3496 printk(KERN_ERR "%s: out of buffers! Failing "
3497 "completion.\n", __func__);
3498 break;
3501 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3502 phba->iocb_rsp_size);
3503 irsp = &rspiocbp->iocb;
3505 if (++pring->sli.sli3.rspidx >= portRspMax)
3506 pring->sli.sli3.rspidx = 0;
3508 if (pring->ringno == LPFC_ELS_RING) {
3509 lpfc_debugfs_slow_ring_trc(phba,
3510 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3511 *(((uint32_t *) irsp) + 4),
3512 *(((uint32_t *) irsp) + 6),
3513 *(((uint32_t *) irsp) + 7));
3516 writel(pring->sli.sli3.rspidx,
3517 &phba->host_gp[pring->ringno].rspGetInx);
3519 spin_unlock_irqrestore(&phba->hbalock, iflag);
3520 /* Handle the response IOCB */
3521 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3522 spin_lock_irqsave(&phba->hbalock, iflag);
3525 * If the port response put pointer has not been updated, sync
3526 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3527 * response put pointer.
3529 if (pring->sli.sli3.rspidx == portRspPut) {
3530 portRspPut = le32_to_cpu(pgp->rspPutInx);
3532 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3534 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3535 /* At least one response entry has been freed */
3536 pring->stats.iocb_rsp_full++;
3537 /* SET RxRE_RSP in Chip Att register */
3538 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3539 writel(status, phba->CAregaddr);
3540 readl(phba->CAregaddr); /* flush */
3542 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3543 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3544 pring->stats.iocb_cmd_empty++;
3546 /* Force update of the local copy of cmdGetInx */
3547 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3548 lpfc_sli_resume_iocb(phba, pring);
3550 if ((pring->lpfc_sli_cmd_available))
3551 (pring->lpfc_sli_cmd_available) (phba, pring);
3555 spin_unlock_irqrestore(&phba->hbalock, iflag);
3556 return;
3560 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3561 * @phba: Pointer to HBA context object.
3562 * @pring: Pointer to driver SLI ring object.
3563 * @mask: Host attention register mask for this ring.
3565 * This function is called from the worker thread when there is a pending
3566 * ELS response iocb on the driver internal slow-path response iocb worker
3567 * queue. The caller does not hold any lock. The function will remove each
3568 * response iocb from the response worker queue and calls the handle
3569 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3571 static void
3572 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3573 struct lpfc_sli_ring *pring, uint32_t mask)
3575 struct lpfc_iocbq *irspiocbq;
3576 struct hbq_dmabuf *dmabuf;
3577 struct lpfc_cq_event *cq_event;
3578 unsigned long iflag;
3580 spin_lock_irqsave(&phba->hbalock, iflag);
3581 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3582 spin_unlock_irqrestore(&phba->hbalock, iflag);
3583 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3584 /* Get the response iocb from the head of work queue */
3585 spin_lock_irqsave(&phba->hbalock, iflag);
3586 list_remove_head(&phba->sli4_hba.sp_queue_event,
3587 cq_event, struct lpfc_cq_event, list);
3588 spin_unlock_irqrestore(&phba->hbalock, iflag);
3590 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3591 case CQE_CODE_COMPL_WQE:
3592 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3593 cq_event);
3594 /* Translate ELS WCQE to response IOCBQ */
3595 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3596 irspiocbq);
3597 if (irspiocbq)
3598 lpfc_sli_sp_handle_rspiocb(phba, pring,
3599 irspiocbq);
3600 break;
3601 case CQE_CODE_RECEIVE:
3602 case CQE_CODE_RECEIVE_V1:
3603 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3604 cq_event);
3605 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3606 break;
3607 default:
3608 break;
3614 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3615 * @phba: Pointer to HBA context object.
3616 * @pring: Pointer to driver SLI ring object.
3618 * This function aborts all iocbs in the given ring and frees all the iocb
3619 * objects in txq. This function issues an abort iocb for all the iocb commands
3620 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3621 * the return of this function. The caller is not required to hold any locks.
3623 void
3624 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3626 LIST_HEAD(completions);
3627 struct lpfc_iocbq *iocb, *next_iocb;
3629 if (pring->ringno == LPFC_ELS_RING) {
3630 lpfc_fabric_abort_hba(phba);
3633 /* Error everything on txq and txcmplq
3634 * First do the txq.
3636 if (phba->sli_rev >= LPFC_SLI_REV4) {
3637 spin_lock_irq(&pring->ring_lock);
3638 list_splice_init(&pring->txq, &completions);
3639 pring->txq_cnt = 0;
3640 spin_unlock_irq(&pring->ring_lock);
3642 spin_lock_irq(&phba->hbalock);
3643 /* Next issue ABTS for everything on the txcmplq */
3644 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3645 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3646 spin_unlock_irq(&phba->hbalock);
3647 } else {
3648 spin_lock_irq(&phba->hbalock);
3649 list_splice_init(&pring->txq, &completions);
3650 pring->txq_cnt = 0;
3652 /* Next issue ABTS for everything on the txcmplq */
3653 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3654 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3655 spin_unlock_irq(&phba->hbalock);
3658 /* Cancel all the IOCBs from the completions list */
3659 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3660 IOERR_SLI_ABORTED);
3664 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3665 * @phba: Pointer to HBA context object.
3666 * @pring: Pointer to driver SLI ring object.
3668 * This function aborts all iocbs in the given ring and frees all the iocb
3669 * objects in txq. This function issues an abort iocb for all the iocb commands
3670 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3671 * the return of this function. The caller is not required to hold any locks.
3673 void
3674 lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3676 LIST_HEAD(completions);
3677 struct lpfc_iocbq *iocb, *next_iocb;
3679 if (pring->ringno == LPFC_ELS_RING)
3680 lpfc_fabric_abort_hba(phba);
3682 spin_lock_irq(&phba->hbalock);
3683 /* Next issue ABTS for everything on the txcmplq */
3684 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3685 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3686 spin_unlock_irq(&phba->hbalock);
3691 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3692 * @phba: Pointer to HBA context object.
3693 * @pring: Pointer to driver SLI ring object.
3695 * This function aborts all iocbs in FCP rings and frees all the iocb
3696 * objects in txq. This function issues an abort iocb for all the iocb commands
3697 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3698 * the return of this function. The caller is not required to hold any locks.
3700 void
3701 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3703 struct lpfc_sli *psli = &phba->sli;
3704 struct lpfc_sli_ring *pring;
3705 uint32_t i;
3707 /* Look on all the FCP Rings for the iotag */
3708 if (phba->sli_rev >= LPFC_SLI_REV4) {
3709 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3710 pring = phba->sli4_hba.fcp_wq[i]->pring;
3711 lpfc_sli_abort_iocb_ring(phba, pring);
3713 } else {
3714 pring = &psli->sli3_ring[LPFC_FCP_RING];
3715 lpfc_sli_abort_iocb_ring(phba, pring);
3720 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3721 * @phba: Pointer to HBA context object.
3723 * This function aborts all wqes in NVME rings. This function issues an
3724 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3725 * the txcmplq is not guaranteed to complete before the return of this
3726 * function. The caller is not required to hold any locks.
3728 void
3729 lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3731 struct lpfc_sli_ring *pring;
3732 uint32_t i;
3734 if (phba->sli_rev < LPFC_SLI_REV4)
3735 return;
3737 /* Abort all IO on each NVME ring. */
3738 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3739 pring = phba->sli4_hba.nvme_wq[i]->pring;
3740 lpfc_sli_abort_wqe_ring(phba, pring);
3746 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3747 * @phba: Pointer to HBA context object.
3749 * This function flushes all iocbs in the fcp ring and frees all the iocb
3750 * objects in txq and txcmplq. This function will not issue abort iocbs
3751 * for all the iocb commands in txcmplq, they will just be returned with
3752 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3753 * slot has been permanently disabled.
3755 void
3756 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3758 LIST_HEAD(txq);
3759 LIST_HEAD(txcmplq);
3760 struct lpfc_sli *psli = &phba->sli;
3761 struct lpfc_sli_ring *pring;
3762 uint32_t i;
3764 spin_lock_irq(&phba->hbalock);
3765 /* Indicate the I/O queues are flushed */
3766 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3767 spin_unlock_irq(&phba->hbalock);
3769 /* Look on all the FCP Rings for the iotag */
3770 if (phba->sli_rev >= LPFC_SLI_REV4) {
3771 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3772 pring = phba->sli4_hba.fcp_wq[i]->pring;
3774 spin_lock_irq(&pring->ring_lock);
3775 /* Retrieve everything on txq */
3776 list_splice_init(&pring->txq, &txq);
3777 /* Retrieve everything on the txcmplq */
3778 list_splice_init(&pring->txcmplq, &txcmplq);
3779 pring->txq_cnt = 0;
3780 pring->txcmplq_cnt = 0;
3781 spin_unlock_irq(&pring->ring_lock);
3783 /* Flush the txq */
3784 lpfc_sli_cancel_iocbs(phba, &txq,
3785 IOSTAT_LOCAL_REJECT,
3786 IOERR_SLI_DOWN);
3787 /* Flush the txcmpq */
3788 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3789 IOSTAT_LOCAL_REJECT,
3790 IOERR_SLI_DOWN);
3792 } else {
3793 pring = &psli->sli3_ring[LPFC_FCP_RING];
3795 spin_lock_irq(&phba->hbalock);
3796 /* Retrieve everything on txq */
3797 list_splice_init(&pring->txq, &txq);
3798 /* Retrieve everything on the txcmplq */
3799 list_splice_init(&pring->txcmplq, &txcmplq);
3800 pring->txq_cnt = 0;
3801 pring->txcmplq_cnt = 0;
3802 spin_unlock_irq(&phba->hbalock);
3804 /* Flush the txq */
3805 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3806 IOERR_SLI_DOWN);
3807 /* Flush the txcmpq */
3808 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3809 IOERR_SLI_DOWN);
3814 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3815 * @phba: Pointer to HBA context object.
3817 * This function flushes all wqes in the nvme rings and frees all resources
3818 * in the txcmplq. This function does not issue abort wqes for the IO
3819 * commands in txcmplq, they will just be returned with
3820 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3821 * slot has been permanently disabled.
3823 void
3824 lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3826 LIST_HEAD(txcmplq);
3827 struct lpfc_sli_ring *pring;
3828 uint32_t i;
3830 if (phba->sli_rev < LPFC_SLI_REV4)
3831 return;
3833 /* Hint to other driver operations that a flush is in progress. */
3834 spin_lock_irq(&phba->hbalock);
3835 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3836 spin_unlock_irq(&phba->hbalock);
3838 /* Cycle through all NVME rings and complete each IO with
3839 * a local driver reason code. This is a flush so no
3840 * abort exchange to FW.
3842 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3843 pring = phba->sli4_hba.nvme_wq[i]->pring;
3845 /* Retrieve everything on the txcmplq */
3846 spin_lock_irq(&pring->ring_lock);
3847 list_splice_init(&pring->txcmplq, &txcmplq);
3848 pring->txcmplq_cnt = 0;
3849 spin_unlock_irq(&pring->ring_lock);
3851 /* Flush the txcmpq &&&PAE */
3852 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3853 IOSTAT_LOCAL_REJECT,
3854 IOERR_SLI_DOWN);
3859 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3860 * @phba: Pointer to HBA context object.
3861 * @mask: Bit mask to be checked.
3863 * This function reads the host status register and compares
3864 * with the provided bit mask to check if HBA completed
3865 * the restart. This function will wait in a loop for the
3866 * HBA to complete restart. If the HBA does not restart within
3867 * 15 iterations, the function will reset the HBA again. The
3868 * function returns 1 when HBA fail to restart otherwise returns
3869 * zero.
3871 static int
3872 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3874 uint32_t status;
3875 int i = 0;
3876 int retval = 0;
3878 /* Read the HBA Host Status Register */
3879 if (lpfc_readl(phba->HSregaddr, &status))
3880 return 1;
3883 * Check status register every 100ms for 5 retries, then every
3884 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3885 * every 2.5 sec for 4.
3886 * Break our of the loop if errors occurred during init.
3888 while (((status & mask) != mask) &&
3889 !(status & HS_FFERM) &&
3890 i++ < 20) {
3892 if (i <= 5)
3893 msleep(10);
3894 else if (i <= 10)
3895 msleep(500);
3896 else
3897 msleep(2500);
3899 if (i == 15) {
3900 /* Do post */
3901 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3902 lpfc_sli_brdrestart(phba);
3904 /* Read the HBA Host Status Register */
3905 if (lpfc_readl(phba->HSregaddr, &status)) {
3906 retval = 1;
3907 break;
3911 /* Check to see if any errors occurred during init */
3912 if ((status & HS_FFERM) || (i >= 20)) {
3913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3914 "2751 Adapter failed to restart, "
3915 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3916 status,
3917 readl(phba->MBslimaddr + 0xa8),
3918 readl(phba->MBslimaddr + 0xac));
3919 phba->link_state = LPFC_HBA_ERROR;
3920 retval = 1;
3923 return retval;
3927 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3928 * @phba: Pointer to HBA context object.
3929 * @mask: Bit mask to be checked.
3931 * This function checks the host status register to check if HBA is
3932 * ready. This function will wait in a loop for the HBA to be ready
3933 * If the HBA is not ready , the function will will reset the HBA PCI
3934 * function again. The function returns 1 when HBA fail to be ready
3935 * otherwise returns zero.
3937 static int
3938 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3940 uint32_t status;
3941 int retval = 0;
3943 /* Read the HBA Host Status Register */
3944 status = lpfc_sli4_post_status_check(phba);
3946 if (status) {
3947 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3948 lpfc_sli_brdrestart(phba);
3949 status = lpfc_sli4_post_status_check(phba);
3952 /* Check to see if any errors occurred during init */
3953 if (status) {
3954 phba->link_state = LPFC_HBA_ERROR;
3955 retval = 1;
3956 } else
3957 phba->sli4_hba.intr_enable = 0;
3959 return retval;
3963 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3964 * @phba: Pointer to HBA context object.
3965 * @mask: Bit mask to be checked.
3967 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3968 * from the API jump table function pointer from the lpfc_hba struct.
3971 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3973 return phba->lpfc_sli_brdready(phba, mask);
3976 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3979 * lpfc_reset_barrier - Make HBA ready for HBA reset
3980 * @phba: Pointer to HBA context object.
3982 * This function is called before resetting an HBA. This function is called
3983 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3985 void lpfc_reset_barrier(struct lpfc_hba *phba)
3987 uint32_t __iomem *resp_buf;
3988 uint32_t __iomem *mbox_buf;
3989 volatile uint32_t mbox;
3990 uint32_t hc_copy, ha_copy, resp_data;
3991 int i;
3992 uint8_t hdrtype;
3994 lockdep_assert_held(&phba->hbalock);
3996 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3997 if (hdrtype != 0x80 ||
3998 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3999 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4000 return;
4003 * Tell the other part of the chip to suspend temporarily all
4004 * its DMA activity.
4006 resp_buf = phba->MBslimaddr;
4008 /* Disable the error attention */
4009 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4010 return;
4011 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4012 readl(phba->HCregaddr); /* flush */
4013 phba->link_flag |= LS_IGNORE_ERATT;
4015 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4016 return;
4017 if (ha_copy & HA_ERATT) {
4018 /* Clear Chip error bit */
4019 writel(HA_ERATT, phba->HAregaddr);
4020 phba->pport->stopped = 1;
4023 mbox = 0;
4024 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4025 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4027 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4028 mbox_buf = phba->MBslimaddr;
4029 writel(mbox, mbox_buf);
4031 for (i = 0; i < 50; i++) {
4032 if (lpfc_readl((resp_buf + 1), &resp_data))
4033 return;
4034 if (resp_data != ~(BARRIER_TEST_PATTERN))
4035 mdelay(1);
4036 else
4037 break;
4039 resp_data = 0;
4040 if (lpfc_readl((resp_buf + 1), &resp_data))
4041 return;
4042 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
4043 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4044 phba->pport->stopped)
4045 goto restore_hc;
4046 else
4047 goto clear_errat;
4050 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4051 resp_data = 0;
4052 for (i = 0; i < 500; i++) {
4053 if (lpfc_readl(resp_buf, &resp_data))
4054 return;
4055 if (resp_data != mbox)
4056 mdelay(1);
4057 else
4058 break;
4061 clear_errat:
4063 while (++i < 500) {
4064 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4065 return;
4066 if (!(ha_copy & HA_ERATT))
4067 mdelay(1);
4068 else
4069 break;
4072 if (readl(phba->HAregaddr) & HA_ERATT) {
4073 writel(HA_ERATT, phba->HAregaddr);
4074 phba->pport->stopped = 1;
4077 restore_hc:
4078 phba->link_flag &= ~LS_IGNORE_ERATT;
4079 writel(hc_copy, phba->HCregaddr);
4080 readl(phba->HCregaddr); /* flush */
4084 * lpfc_sli_brdkill - Issue a kill_board mailbox command
4085 * @phba: Pointer to HBA context object.
4087 * This function issues a kill_board mailbox command and waits for
4088 * the error attention interrupt. This function is called for stopping
4089 * the firmware processing. The caller is not required to hold any
4090 * locks. This function calls lpfc_hba_down_post function to free
4091 * any pending commands after the kill. The function will return 1 when it
4092 * fails to kill the board else will return 0.
4095 lpfc_sli_brdkill(struct lpfc_hba *phba)
4097 struct lpfc_sli *psli;
4098 LPFC_MBOXQ_t *pmb;
4099 uint32_t status;
4100 uint32_t ha_copy;
4101 int retval;
4102 int i = 0;
4104 psli = &phba->sli;
4106 /* Kill HBA */
4107 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4108 "0329 Kill HBA Data: x%x x%x\n",
4109 phba->pport->port_state, psli->sli_flag);
4111 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4112 if (!pmb)
4113 return 1;
4115 /* Disable the error attention */
4116 spin_lock_irq(&phba->hbalock);
4117 if (lpfc_readl(phba->HCregaddr, &status)) {
4118 spin_unlock_irq(&phba->hbalock);
4119 mempool_free(pmb, phba->mbox_mem_pool);
4120 return 1;
4122 status &= ~HC_ERINT_ENA;
4123 writel(status, phba->HCregaddr);
4124 readl(phba->HCregaddr); /* flush */
4125 phba->link_flag |= LS_IGNORE_ERATT;
4126 spin_unlock_irq(&phba->hbalock);
4128 lpfc_kill_board(phba, pmb);
4129 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4130 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4132 if (retval != MBX_SUCCESS) {
4133 if (retval != MBX_BUSY)
4134 mempool_free(pmb, phba->mbox_mem_pool);
4135 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4136 "2752 KILL_BOARD command failed retval %d\n",
4137 retval);
4138 spin_lock_irq(&phba->hbalock);
4139 phba->link_flag &= ~LS_IGNORE_ERATT;
4140 spin_unlock_irq(&phba->hbalock);
4141 return 1;
4144 spin_lock_irq(&phba->hbalock);
4145 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4146 spin_unlock_irq(&phba->hbalock);
4148 mempool_free(pmb, phba->mbox_mem_pool);
4150 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4151 * attention every 100ms for 3 seconds. If we don't get ERATT after
4152 * 3 seconds we still set HBA_ERROR state because the status of the
4153 * board is now undefined.
4155 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4156 return 1;
4157 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4158 mdelay(100);
4159 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4160 return 1;
4163 del_timer_sync(&psli->mbox_tmo);
4164 if (ha_copy & HA_ERATT) {
4165 writel(HA_ERATT, phba->HAregaddr);
4166 phba->pport->stopped = 1;
4168 spin_lock_irq(&phba->hbalock);
4169 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4170 psli->mbox_active = NULL;
4171 phba->link_flag &= ~LS_IGNORE_ERATT;
4172 spin_unlock_irq(&phba->hbalock);
4174 lpfc_hba_down_post(phba);
4175 phba->link_state = LPFC_HBA_ERROR;
4177 return ha_copy & HA_ERATT ? 0 : 1;
4181 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4182 * @phba: Pointer to HBA context object.
4184 * This function resets the HBA by writing HC_INITFF to the control
4185 * register. After the HBA resets, this function resets all the iocb ring
4186 * indices. This function disables PCI layer parity checking during
4187 * the reset.
4188 * This function returns 0 always.
4189 * The caller is not required to hold any locks.
4192 lpfc_sli_brdreset(struct lpfc_hba *phba)
4194 struct lpfc_sli *psli;
4195 struct lpfc_sli_ring *pring;
4196 uint16_t cfg_value;
4197 int i;
4199 psli = &phba->sli;
4201 /* Reset HBA */
4202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4203 "0325 Reset HBA Data: x%x x%x\n",
4204 phba->pport->port_state, psli->sli_flag);
4206 /* perform board reset */
4207 phba->fc_eventTag = 0;
4208 phba->link_events = 0;
4209 phba->pport->fc_myDID = 0;
4210 phba->pport->fc_prevDID = 0;
4212 /* Turn off parity checking and serr during the physical reset */
4213 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4214 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4215 (cfg_value &
4216 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4218 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4220 /* Now toggle INITFF bit in the Host Control Register */
4221 writel(HC_INITFF, phba->HCregaddr);
4222 mdelay(1);
4223 readl(phba->HCregaddr); /* flush */
4224 writel(0, phba->HCregaddr);
4225 readl(phba->HCregaddr); /* flush */
4227 /* Restore PCI cmd register */
4228 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4230 /* Initialize relevant SLI info */
4231 for (i = 0; i < psli->num_rings; i++) {
4232 pring = &psli->sli3_ring[i];
4233 pring->flag = 0;
4234 pring->sli.sli3.rspidx = 0;
4235 pring->sli.sli3.next_cmdidx = 0;
4236 pring->sli.sli3.local_getidx = 0;
4237 pring->sli.sli3.cmdidx = 0;
4238 pring->missbufcnt = 0;
4241 phba->link_state = LPFC_WARM_START;
4242 return 0;
4246 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4247 * @phba: Pointer to HBA context object.
4249 * This function resets a SLI4 HBA. This function disables PCI layer parity
4250 * checking during resets the device. The caller is not required to hold
4251 * any locks.
4253 * This function returns 0 always.
4256 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4258 struct lpfc_sli *psli = &phba->sli;
4259 uint16_t cfg_value;
4260 int rc = 0;
4262 /* Reset HBA */
4263 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4264 "0295 Reset HBA Data: x%x x%x x%x\n",
4265 phba->pport->port_state, psli->sli_flag,
4266 phba->hba_flag);
4268 /* perform board reset */
4269 phba->fc_eventTag = 0;
4270 phba->link_events = 0;
4271 phba->pport->fc_myDID = 0;
4272 phba->pport->fc_prevDID = 0;
4274 spin_lock_irq(&phba->hbalock);
4275 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4276 phba->fcf.fcf_flag = 0;
4277 spin_unlock_irq(&phba->hbalock);
4279 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4280 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4281 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4282 return rc;
4285 /* Now physically reset the device */
4286 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4287 "0389 Performing PCI function reset!\n");
4289 /* Turn off parity checking and serr during the physical reset */
4290 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4291 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4292 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4294 /* Perform FCoE PCI function reset before freeing queue memory */
4295 rc = lpfc_pci_function_reset(phba);
4296 lpfc_sli4_queue_destroy(phba);
4298 /* Restore PCI cmd register */
4299 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4301 return rc;
4305 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4306 * @phba: Pointer to HBA context object.
4308 * This function is called in the SLI initialization code path to
4309 * restart the HBA. The caller is not required to hold any lock.
4310 * This function writes MBX_RESTART mailbox command to the SLIM and
4311 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4312 * function to free any pending commands. The function enables
4313 * POST only during the first initialization. The function returns zero.
4314 * The function does not guarantee completion of MBX_RESTART mailbox
4315 * command before the return of this function.
4317 static int
4318 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4320 MAILBOX_t *mb;
4321 struct lpfc_sli *psli;
4322 volatile uint32_t word0;
4323 void __iomem *to_slim;
4324 uint32_t hba_aer_enabled;
4326 spin_lock_irq(&phba->hbalock);
4328 /* Take PCIe device Advanced Error Reporting (AER) state */
4329 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4331 psli = &phba->sli;
4333 /* Restart HBA */
4334 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4335 "0337 Restart HBA Data: x%x x%x\n",
4336 phba->pport->port_state, psli->sli_flag);
4338 word0 = 0;
4339 mb = (MAILBOX_t *) &word0;
4340 mb->mbxCommand = MBX_RESTART;
4341 mb->mbxHc = 1;
4343 lpfc_reset_barrier(phba);
4345 to_slim = phba->MBslimaddr;
4346 writel(*(uint32_t *) mb, to_slim);
4347 readl(to_slim); /* flush */
4349 /* Only skip post after fc_ffinit is completed */
4350 if (phba->pport->port_state)
4351 word0 = 1; /* This is really setting up word1 */
4352 else
4353 word0 = 0; /* This is really setting up word1 */
4354 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4355 writel(*(uint32_t *) mb, to_slim);
4356 readl(to_slim); /* flush */
4358 lpfc_sli_brdreset(phba);
4359 phba->pport->stopped = 0;
4360 phba->link_state = LPFC_INIT_START;
4361 phba->hba_flag = 0;
4362 spin_unlock_irq(&phba->hbalock);
4364 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4365 psli->stats_start = get_seconds();
4367 /* Give the INITFF and Post time to settle. */
4368 mdelay(100);
4370 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4371 if (hba_aer_enabled)
4372 pci_disable_pcie_error_reporting(phba->pcidev);
4374 lpfc_hba_down_post(phba);
4376 return 0;
4380 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4381 * @phba: Pointer to HBA context object.
4383 * This function is called in the SLI initialization code path to restart
4384 * a SLI4 HBA. The caller is not required to hold any lock.
4385 * At the end of the function, it calls lpfc_hba_down_post function to
4386 * free any pending commands.
4388 static int
4389 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4391 struct lpfc_sli *psli = &phba->sli;
4392 uint32_t hba_aer_enabled;
4393 int rc;
4395 /* Restart HBA */
4396 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4397 "0296 Restart HBA Data: x%x x%x\n",
4398 phba->pport->port_state, psli->sli_flag);
4400 /* Take PCIe device Advanced Error Reporting (AER) state */
4401 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4403 rc = lpfc_sli4_brdreset(phba);
4405 spin_lock_irq(&phba->hbalock);
4406 phba->pport->stopped = 0;
4407 phba->link_state = LPFC_INIT_START;
4408 phba->hba_flag = 0;
4409 spin_unlock_irq(&phba->hbalock);
4411 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4412 psli->stats_start = get_seconds();
4414 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4415 if (hba_aer_enabled)
4416 pci_disable_pcie_error_reporting(phba->pcidev);
4418 lpfc_hba_down_post(phba);
4420 return rc;
4424 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4425 * @phba: Pointer to HBA context object.
4427 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4428 * API jump table function pointer from the lpfc_hba struct.
4431 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4433 return phba->lpfc_sli_brdrestart(phba);
4437 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4438 * @phba: Pointer to HBA context object.
4440 * This function is called after a HBA restart to wait for successful
4441 * restart of the HBA. Successful restart of the HBA is indicated by
4442 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4443 * iteration, the function will restart the HBA again. The function returns
4444 * zero if HBA successfully restarted else returns negative error code.
4446 static int
4447 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4449 uint32_t status, i = 0;
4451 /* Read the HBA Host Status Register */
4452 if (lpfc_readl(phba->HSregaddr, &status))
4453 return -EIO;
4455 /* Check status register to see what current state is */
4456 i = 0;
4457 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4459 /* Check every 10ms for 10 retries, then every 100ms for 90
4460 * retries, then every 1 sec for 50 retires for a total of
4461 * ~60 seconds before reset the board again and check every
4462 * 1 sec for 50 retries. The up to 60 seconds before the
4463 * board ready is required by the Falcon FIPS zeroization
4464 * complete, and any reset the board in between shall cause
4465 * restart of zeroization, further delay the board ready.
4467 if (i++ >= 200) {
4468 /* Adapter failed to init, timeout, status reg
4469 <status> */
4470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4471 "0436 Adapter failed to init, "
4472 "timeout, status reg x%x, "
4473 "FW Data: A8 x%x AC x%x\n", status,
4474 readl(phba->MBslimaddr + 0xa8),
4475 readl(phba->MBslimaddr + 0xac));
4476 phba->link_state = LPFC_HBA_ERROR;
4477 return -ETIMEDOUT;
4480 /* Check to see if any errors occurred during init */
4481 if (status & HS_FFERM) {
4482 /* ERROR: During chipset initialization */
4483 /* Adapter failed to init, chipset, status reg
4484 <status> */
4485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4486 "0437 Adapter failed to init, "
4487 "chipset, status reg x%x, "
4488 "FW Data: A8 x%x AC x%x\n", status,
4489 readl(phba->MBslimaddr + 0xa8),
4490 readl(phba->MBslimaddr + 0xac));
4491 phba->link_state = LPFC_HBA_ERROR;
4492 return -EIO;
4495 if (i <= 10)
4496 msleep(10);
4497 else if (i <= 100)
4498 msleep(100);
4499 else
4500 msleep(1000);
4502 if (i == 150) {
4503 /* Do post */
4504 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4505 lpfc_sli_brdrestart(phba);
4507 /* Read the HBA Host Status Register */
4508 if (lpfc_readl(phba->HSregaddr, &status))
4509 return -EIO;
4512 /* Check to see if any errors occurred during init */
4513 if (status & HS_FFERM) {
4514 /* ERROR: During chipset initialization */
4515 /* Adapter failed to init, chipset, status reg <status> */
4516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4517 "0438 Adapter failed to init, chipset, "
4518 "status reg x%x, "
4519 "FW Data: A8 x%x AC x%x\n", status,
4520 readl(phba->MBslimaddr + 0xa8),
4521 readl(phba->MBslimaddr + 0xac));
4522 phba->link_state = LPFC_HBA_ERROR;
4523 return -EIO;
4526 /* Clear all interrupt enable conditions */
4527 writel(0, phba->HCregaddr);
4528 readl(phba->HCregaddr); /* flush */
4530 /* setup host attn register */
4531 writel(0xffffffff, phba->HAregaddr);
4532 readl(phba->HAregaddr); /* flush */
4533 return 0;
4537 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4539 * This function calculates and returns the number of HBQs required to be
4540 * configured.
4543 lpfc_sli_hbq_count(void)
4545 return ARRAY_SIZE(lpfc_hbq_defs);
4549 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4551 * This function adds the number of hbq entries in every HBQ to get
4552 * the total number of hbq entries required for the HBA and returns
4553 * the total count.
4555 static int
4556 lpfc_sli_hbq_entry_count(void)
4558 int hbq_count = lpfc_sli_hbq_count();
4559 int count = 0;
4560 int i;
4562 for (i = 0; i < hbq_count; ++i)
4563 count += lpfc_hbq_defs[i]->entry_count;
4564 return count;
4568 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4570 * This function calculates amount of memory required for all hbq entries
4571 * to be configured and returns the total memory required.
4574 lpfc_sli_hbq_size(void)
4576 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4580 * lpfc_sli_hbq_setup - configure and initialize HBQs
4581 * @phba: Pointer to HBA context object.
4583 * This function is called during the SLI initialization to configure
4584 * all the HBQs and post buffers to the HBQ. The caller is not
4585 * required to hold any locks. This function will return zero if successful
4586 * else it will return negative error code.
4588 static int
4589 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4591 int hbq_count = lpfc_sli_hbq_count();
4592 LPFC_MBOXQ_t *pmb;
4593 MAILBOX_t *pmbox;
4594 uint32_t hbqno;
4595 uint32_t hbq_entry_index;
4597 /* Get a Mailbox buffer to setup mailbox
4598 * commands for HBA initialization
4600 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4602 if (!pmb)
4603 return -ENOMEM;
4605 pmbox = &pmb->u.mb;
4607 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4608 phba->link_state = LPFC_INIT_MBX_CMDS;
4609 phba->hbq_in_use = 1;
4611 hbq_entry_index = 0;
4612 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4613 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4614 phba->hbqs[hbqno].hbqPutIdx = 0;
4615 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4616 phba->hbqs[hbqno].entry_count =
4617 lpfc_hbq_defs[hbqno]->entry_count;
4618 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4619 hbq_entry_index, pmb);
4620 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4622 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4623 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4624 mbxStatus <status>, ring <num> */
4626 lpfc_printf_log(phba, KERN_ERR,
4627 LOG_SLI | LOG_VPORT,
4628 "1805 Adapter failed to init. "
4629 "Data: x%x x%x x%x\n",
4630 pmbox->mbxCommand,
4631 pmbox->mbxStatus, hbqno);
4633 phba->link_state = LPFC_HBA_ERROR;
4634 mempool_free(pmb, phba->mbox_mem_pool);
4635 return -ENXIO;
4638 phba->hbq_count = hbq_count;
4640 mempool_free(pmb, phba->mbox_mem_pool);
4642 /* Initially populate or replenish the HBQs */
4643 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4644 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4645 return 0;
4649 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4650 * @phba: Pointer to HBA context object.
4652 * This function is called during the SLI initialization to configure
4653 * all the HBQs and post buffers to the HBQ. The caller is not
4654 * required to hold any locks. This function will return zero if successful
4655 * else it will return negative error code.
4657 static int
4658 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4660 phba->hbq_in_use = 1;
4661 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4662 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4663 phba->hbq_count = 1;
4664 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4665 /* Initially populate or replenish the HBQs */
4666 return 0;
4670 * lpfc_sli_config_port - Issue config port mailbox command
4671 * @phba: Pointer to HBA context object.
4672 * @sli_mode: sli mode - 2/3
4674 * This function is called by the sli intialization code path
4675 * to issue config_port mailbox command. This function restarts the
4676 * HBA firmware and issues a config_port mailbox command to configure
4677 * the SLI interface in the sli mode specified by sli_mode
4678 * variable. The caller is not required to hold any locks.
4679 * The function returns 0 if successful, else returns negative error
4680 * code.
4683 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4685 LPFC_MBOXQ_t *pmb;
4686 uint32_t resetcount = 0, rc = 0, done = 0;
4688 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4689 if (!pmb) {
4690 phba->link_state = LPFC_HBA_ERROR;
4691 return -ENOMEM;
4694 phba->sli_rev = sli_mode;
4695 while (resetcount < 2 && !done) {
4696 spin_lock_irq(&phba->hbalock);
4697 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4698 spin_unlock_irq(&phba->hbalock);
4699 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4700 lpfc_sli_brdrestart(phba);
4701 rc = lpfc_sli_chipset_init(phba);
4702 if (rc)
4703 break;
4705 spin_lock_irq(&phba->hbalock);
4706 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4707 spin_unlock_irq(&phba->hbalock);
4708 resetcount++;
4710 /* Call pre CONFIG_PORT mailbox command initialization. A
4711 * value of 0 means the call was successful. Any other
4712 * nonzero value is a failure, but if ERESTART is returned,
4713 * the driver may reset the HBA and try again.
4715 rc = lpfc_config_port_prep(phba);
4716 if (rc == -ERESTART) {
4717 phba->link_state = LPFC_LINK_UNKNOWN;
4718 continue;
4719 } else if (rc)
4720 break;
4722 phba->link_state = LPFC_INIT_MBX_CMDS;
4723 lpfc_config_port(phba, pmb);
4724 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4725 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4726 LPFC_SLI3_HBQ_ENABLED |
4727 LPFC_SLI3_CRP_ENABLED |
4728 LPFC_SLI3_BG_ENABLED |
4729 LPFC_SLI3_DSS_ENABLED);
4730 if (rc != MBX_SUCCESS) {
4731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4732 "0442 Adapter failed to init, mbxCmd x%x "
4733 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4734 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4735 spin_lock_irq(&phba->hbalock);
4736 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4737 spin_unlock_irq(&phba->hbalock);
4738 rc = -ENXIO;
4739 } else {
4740 /* Allow asynchronous mailbox command to go through */
4741 spin_lock_irq(&phba->hbalock);
4742 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4743 spin_unlock_irq(&phba->hbalock);
4744 done = 1;
4746 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4747 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4748 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4749 "3110 Port did not grant ASABT\n");
4752 if (!done) {
4753 rc = -EINVAL;
4754 goto do_prep_failed;
4756 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4757 if (!pmb->u.mb.un.varCfgPort.cMA) {
4758 rc = -ENXIO;
4759 goto do_prep_failed;
4761 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4762 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4763 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4764 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4765 phba->max_vpi : phba->max_vports;
4767 } else
4768 phba->max_vpi = 0;
4769 phba->fips_level = 0;
4770 phba->fips_spec_rev = 0;
4771 if (pmb->u.mb.un.varCfgPort.gdss) {
4772 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4773 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4774 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4775 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4776 "2850 Security Crypto Active. FIPS x%d "
4777 "(Spec Rev: x%d)",
4778 phba->fips_level, phba->fips_spec_rev);
4780 if (pmb->u.mb.un.varCfgPort.sec_err) {
4781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4782 "2856 Config Port Security Crypto "
4783 "Error: x%x ",
4784 pmb->u.mb.un.varCfgPort.sec_err);
4786 if (pmb->u.mb.un.varCfgPort.gerbm)
4787 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4788 if (pmb->u.mb.un.varCfgPort.gcrp)
4789 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4791 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4792 phba->port_gp = phba->mbox->us.s3_pgp.port;
4794 if (phba->cfg_enable_bg) {
4795 if (pmb->u.mb.un.varCfgPort.gbg)
4796 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4797 else
4798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4799 "0443 Adapter did not grant "
4800 "BlockGuard\n");
4802 } else {
4803 phba->hbq_get = NULL;
4804 phba->port_gp = phba->mbox->us.s2.port;
4805 phba->max_vpi = 0;
4807 do_prep_failed:
4808 mempool_free(pmb, phba->mbox_mem_pool);
4809 return rc;
4814 * lpfc_sli_hba_setup - SLI intialization function
4815 * @phba: Pointer to HBA context object.
4817 * This function is the main SLI intialization function. This function
4818 * is called by the HBA intialization code, HBA reset code and HBA
4819 * error attention handler code. Caller is not required to hold any
4820 * locks. This function issues config_port mailbox command to configure
4821 * the SLI, setup iocb rings and HBQ rings. In the end the function
4822 * calls the config_port_post function to issue init_link mailbox
4823 * command and to start the discovery. The function will return zero
4824 * if successful, else it will return negative error code.
4827 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4829 uint32_t rc;
4830 int mode = 3, i;
4831 int longs;
4833 switch (phba->cfg_sli_mode) {
4834 case 2:
4835 if (phba->cfg_enable_npiv) {
4836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4837 "1824 NPIV enabled: Override sli_mode "
4838 "parameter (%d) to auto (0).\n",
4839 phba->cfg_sli_mode);
4840 break;
4842 mode = 2;
4843 break;
4844 case 0:
4845 case 3:
4846 break;
4847 default:
4848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4849 "1819 Unrecognized sli_mode parameter: %d.\n",
4850 phba->cfg_sli_mode);
4852 break;
4854 phba->fcp_embed_io = 0; /* SLI4 FC support only */
4856 rc = lpfc_sli_config_port(phba, mode);
4858 if (rc && phba->cfg_sli_mode == 3)
4859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4860 "1820 Unable to select SLI-3. "
4861 "Not supported by adapter.\n");
4862 if (rc && mode != 2)
4863 rc = lpfc_sli_config_port(phba, 2);
4864 else if (rc && mode == 2)
4865 rc = lpfc_sli_config_port(phba, 3);
4866 if (rc)
4867 goto lpfc_sli_hba_setup_error;
4869 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4870 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4871 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4872 if (!rc) {
4873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4874 "2709 This device supports "
4875 "Advanced Error Reporting (AER)\n");
4876 spin_lock_irq(&phba->hbalock);
4877 phba->hba_flag |= HBA_AER_ENABLED;
4878 spin_unlock_irq(&phba->hbalock);
4879 } else {
4880 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4881 "2708 This device does not support "
4882 "Advanced Error Reporting (AER): %d\n",
4883 rc);
4884 phba->cfg_aer_support = 0;
4888 if (phba->sli_rev == 3) {
4889 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4890 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4891 } else {
4892 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4893 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4894 phba->sli3_options = 0;
4897 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4898 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4899 phba->sli_rev, phba->max_vpi);
4900 rc = lpfc_sli_ring_map(phba);
4902 if (rc)
4903 goto lpfc_sli_hba_setup_error;
4905 /* Initialize VPIs. */
4906 if (phba->sli_rev == LPFC_SLI_REV3) {
4908 * The VPI bitmask and physical ID array are allocated
4909 * and initialized once only - at driver load. A port
4910 * reset doesn't need to reinitialize this memory.
4912 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4913 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4914 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4915 GFP_KERNEL);
4916 if (!phba->vpi_bmask) {
4917 rc = -ENOMEM;
4918 goto lpfc_sli_hba_setup_error;
4921 phba->vpi_ids = kzalloc(
4922 (phba->max_vpi+1) * sizeof(uint16_t),
4923 GFP_KERNEL);
4924 if (!phba->vpi_ids) {
4925 kfree(phba->vpi_bmask);
4926 rc = -ENOMEM;
4927 goto lpfc_sli_hba_setup_error;
4929 for (i = 0; i < phba->max_vpi; i++)
4930 phba->vpi_ids[i] = i;
4934 /* Init HBQs */
4935 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4936 rc = lpfc_sli_hbq_setup(phba);
4937 if (rc)
4938 goto lpfc_sli_hba_setup_error;
4940 spin_lock_irq(&phba->hbalock);
4941 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4942 spin_unlock_irq(&phba->hbalock);
4944 rc = lpfc_config_port_post(phba);
4945 if (rc)
4946 goto lpfc_sli_hba_setup_error;
4948 return rc;
4950 lpfc_sli_hba_setup_error:
4951 phba->link_state = LPFC_HBA_ERROR;
4952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4953 "0445 Firmware initialization failed\n");
4954 return rc;
4958 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4959 * @phba: Pointer to HBA context object.
4960 * @mboxq: mailbox pointer.
4961 * This function issue a dump mailbox command to read config region
4962 * 23 and parse the records in the region and populate driver
4963 * data structure.
4965 static int
4966 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4968 LPFC_MBOXQ_t *mboxq;
4969 struct lpfc_dmabuf *mp;
4970 struct lpfc_mqe *mqe;
4971 uint32_t data_length;
4972 int rc;
4974 /* Program the default value of vlan_id and fc_map */
4975 phba->valid_vlan = 0;
4976 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4977 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4978 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4980 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4981 if (!mboxq)
4982 return -ENOMEM;
4984 mqe = &mboxq->u.mqe;
4985 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4986 rc = -ENOMEM;
4987 goto out_free_mboxq;
4990 mp = (struct lpfc_dmabuf *) mboxq->context1;
4991 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4993 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4994 "(%d):2571 Mailbox cmd x%x Status x%x "
4995 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4996 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4997 "CQ: x%x x%x x%x x%x\n",
4998 mboxq->vport ? mboxq->vport->vpi : 0,
4999 bf_get(lpfc_mqe_command, mqe),
5000 bf_get(lpfc_mqe_status, mqe),
5001 mqe->un.mb_words[0], mqe->un.mb_words[1],
5002 mqe->un.mb_words[2], mqe->un.mb_words[3],
5003 mqe->un.mb_words[4], mqe->un.mb_words[5],
5004 mqe->un.mb_words[6], mqe->un.mb_words[7],
5005 mqe->un.mb_words[8], mqe->un.mb_words[9],
5006 mqe->un.mb_words[10], mqe->un.mb_words[11],
5007 mqe->un.mb_words[12], mqe->un.mb_words[13],
5008 mqe->un.mb_words[14], mqe->un.mb_words[15],
5009 mqe->un.mb_words[16], mqe->un.mb_words[50],
5010 mboxq->mcqe.word0,
5011 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5012 mboxq->mcqe.trailer);
5014 if (rc) {
5015 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5016 kfree(mp);
5017 rc = -EIO;
5018 goto out_free_mboxq;
5020 data_length = mqe->un.mb_words[5];
5021 if (data_length > DMP_RGN23_SIZE) {
5022 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5023 kfree(mp);
5024 rc = -EIO;
5025 goto out_free_mboxq;
5028 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5029 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5030 kfree(mp);
5031 rc = 0;
5033 out_free_mboxq:
5034 mempool_free(mboxq, phba->mbox_mem_pool);
5035 return rc;
5039 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5040 * @phba: pointer to lpfc hba data structure.
5041 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5042 * @vpd: pointer to the memory to hold resulting port vpd data.
5043 * @vpd_size: On input, the number of bytes allocated to @vpd.
5044 * On output, the number of data bytes in @vpd.
5046 * This routine executes a READ_REV SLI4 mailbox command. In
5047 * addition, this routine gets the port vpd data.
5049 * Return codes
5050 * 0 - successful
5051 * -ENOMEM - could not allocated memory.
5053 static int
5054 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5055 uint8_t *vpd, uint32_t *vpd_size)
5057 int rc = 0;
5058 uint32_t dma_size;
5059 struct lpfc_dmabuf *dmabuf;
5060 struct lpfc_mqe *mqe;
5062 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5063 if (!dmabuf)
5064 return -ENOMEM;
5067 * Get a DMA buffer for the vpd data resulting from the READ_REV
5068 * mailbox command.
5070 dma_size = *vpd_size;
5071 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5072 &dmabuf->phys, GFP_KERNEL);
5073 if (!dmabuf->virt) {
5074 kfree(dmabuf);
5075 return -ENOMEM;
5079 * The SLI4 implementation of READ_REV conflicts at word1,
5080 * bits 31:16 and SLI4 adds vpd functionality not present
5081 * in SLI3. This code corrects the conflicts.
5083 lpfc_read_rev(phba, mboxq);
5084 mqe = &mboxq->u.mqe;
5085 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5086 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5087 mqe->un.read_rev.word1 &= 0x0000FFFF;
5088 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5089 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5091 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5092 if (rc) {
5093 dma_free_coherent(&phba->pcidev->dev, dma_size,
5094 dmabuf->virt, dmabuf->phys);
5095 kfree(dmabuf);
5096 return -EIO;
5100 * The available vpd length cannot be bigger than the
5101 * DMA buffer passed to the port. Catch the less than
5102 * case and update the caller's size.
5104 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5105 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5107 memcpy(vpd, dmabuf->virt, *vpd_size);
5109 dma_free_coherent(&phba->pcidev->dev, dma_size,
5110 dmabuf->virt, dmabuf->phys);
5111 kfree(dmabuf);
5112 return 0;
5116 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5117 * @phba: pointer to lpfc hba data structure.
5119 * This routine retrieves SLI4 device physical port name this PCI function
5120 * is attached to.
5122 * Return codes
5123 * 0 - successful
5124 * otherwise - failed to retrieve physical port name
5126 static int
5127 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5129 LPFC_MBOXQ_t *mboxq;
5130 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5131 struct lpfc_controller_attribute *cntl_attr;
5132 struct lpfc_mbx_get_port_name *get_port_name;
5133 void *virtaddr = NULL;
5134 uint32_t alloclen, reqlen;
5135 uint32_t shdr_status, shdr_add_status;
5136 union lpfc_sli4_cfg_shdr *shdr;
5137 char cport_name = 0;
5138 int rc;
5140 /* We assume nothing at this point */
5141 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5142 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5144 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5145 if (!mboxq)
5146 return -ENOMEM;
5147 /* obtain link type and link number via READ_CONFIG */
5148 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5149 lpfc_sli4_read_config(phba);
5150 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5151 goto retrieve_ppname;
5153 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5154 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5155 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5156 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5157 LPFC_SLI4_MBX_NEMBED);
5158 if (alloclen < reqlen) {
5159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5160 "3084 Allocated DMA memory size (%d) is "
5161 "less than the requested DMA memory size "
5162 "(%d)\n", alloclen, reqlen);
5163 rc = -ENOMEM;
5164 goto out_free_mboxq;
5166 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5167 virtaddr = mboxq->sge_array->addr[0];
5168 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5169 shdr = &mbx_cntl_attr->cfg_shdr;
5170 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5171 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5172 if (shdr_status || shdr_add_status || rc) {
5173 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5174 "3085 Mailbox x%x (x%x/x%x) failed, "
5175 "rc:x%x, status:x%x, add_status:x%x\n",
5176 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5177 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5178 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5179 rc, shdr_status, shdr_add_status);
5180 rc = -ENXIO;
5181 goto out_free_mboxq;
5183 cntl_attr = &mbx_cntl_attr->cntl_attr;
5184 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5185 phba->sli4_hba.lnk_info.lnk_tp =
5186 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5187 phba->sli4_hba.lnk_info.lnk_no =
5188 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5189 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5190 "3086 lnk_type:%d, lnk_numb:%d\n",
5191 phba->sli4_hba.lnk_info.lnk_tp,
5192 phba->sli4_hba.lnk_info.lnk_no);
5194 retrieve_ppname:
5195 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5196 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5197 sizeof(struct lpfc_mbx_get_port_name) -
5198 sizeof(struct lpfc_sli4_cfg_mhdr),
5199 LPFC_SLI4_MBX_EMBED);
5200 get_port_name = &mboxq->u.mqe.un.get_port_name;
5201 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5202 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5203 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5204 phba->sli4_hba.lnk_info.lnk_tp);
5205 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5206 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5207 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5208 if (shdr_status || shdr_add_status || rc) {
5209 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5210 "3087 Mailbox x%x (x%x/x%x) failed: "
5211 "rc:x%x, status:x%x, add_status:x%x\n",
5212 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5213 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5214 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5215 rc, shdr_status, shdr_add_status);
5216 rc = -ENXIO;
5217 goto out_free_mboxq;
5219 switch (phba->sli4_hba.lnk_info.lnk_no) {
5220 case LPFC_LINK_NUMBER_0:
5221 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5222 &get_port_name->u.response);
5223 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5224 break;
5225 case LPFC_LINK_NUMBER_1:
5226 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5227 &get_port_name->u.response);
5228 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5229 break;
5230 case LPFC_LINK_NUMBER_2:
5231 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5232 &get_port_name->u.response);
5233 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5234 break;
5235 case LPFC_LINK_NUMBER_3:
5236 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5237 &get_port_name->u.response);
5238 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5239 break;
5240 default:
5241 break;
5244 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5245 phba->Port[0] = cport_name;
5246 phba->Port[1] = '\0';
5247 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5248 "3091 SLI get port name: %s\n", phba->Port);
5251 out_free_mboxq:
5252 if (rc != MBX_TIMEOUT) {
5253 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5254 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5255 else
5256 mempool_free(mboxq, phba->mbox_mem_pool);
5258 return rc;
5262 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5263 * @phba: pointer to lpfc hba data structure.
5265 * This routine is called to explicitly arm the SLI4 device's completion and
5266 * event queues
5268 static void
5269 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5271 int qidx;
5273 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5274 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5275 if (phba->sli4_hba.nvmels_cq)
5276 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5277 LPFC_QUEUE_REARM);
5279 if (phba->sli4_hba.fcp_cq)
5280 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5281 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5282 LPFC_QUEUE_REARM);
5284 if (phba->sli4_hba.nvme_cq)
5285 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5286 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5287 LPFC_QUEUE_REARM);
5289 if (phba->cfg_fof)
5290 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5292 if (phba->sli4_hba.hba_eq)
5293 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5294 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5295 LPFC_QUEUE_REARM);
5297 if (phba->nvmet_support) {
5298 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5299 lpfc_sli4_cq_release(
5300 phba->sli4_hba.nvmet_cqset[qidx],
5301 LPFC_QUEUE_REARM);
5305 if (phba->cfg_fof)
5306 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5310 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5311 * @phba: Pointer to HBA context object.
5312 * @type: The resource extent type.
5313 * @extnt_count: buffer to hold port available extent count.
5314 * @extnt_size: buffer to hold element count per extent.
5316 * This function calls the port and retrievs the number of available
5317 * extents and their size for a particular extent type.
5319 * Returns: 0 if successful. Nonzero otherwise.
5322 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5323 uint16_t *extnt_count, uint16_t *extnt_size)
5325 int rc = 0;
5326 uint32_t length;
5327 uint32_t mbox_tmo;
5328 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5329 LPFC_MBOXQ_t *mbox;
5331 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5332 if (!mbox)
5333 return -ENOMEM;
5335 /* Find out how many extents are available for this resource type */
5336 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5337 sizeof(struct lpfc_sli4_cfg_mhdr));
5338 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5339 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5340 length, LPFC_SLI4_MBX_EMBED);
5342 /* Send an extents count of 0 - the GET doesn't use it. */
5343 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5344 LPFC_SLI4_MBX_EMBED);
5345 if (unlikely(rc)) {
5346 rc = -EIO;
5347 goto err_exit;
5350 if (!phba->sli4_hba.intr_enable)
5351 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5352 else {
5353 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5354 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5356 if (unlikely(rc)) {
5357 rc = -EIO;
5358 goto err_exit;
5361 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5362 if (bf_get(lpfc_mbox_hdr_status,
5363 &rsrc_info->header.cfg_shdr.response)) {
5364 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5365 "2930 Failed to get resource extents "
5366 "Status 0x%x Add'l Status 0x%x\n",
5367 bf_get(lpfc_mbox_hdr_status,
5368 &rsrc_info->header.cfg_shdr.response),
5369 bf_get(lpfc_mbox_hdr_add_status,
5370 &rsrc_info->header.cfg_shdr.response));
5371 rc = -EIO;
5372 goto err_exit;
5375 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5376 &rsrc_info->u.rsp);
5377 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5378 &rsrc_info->u.rsp);
5380 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5381 "3162 Retrieved extents type-%d from port: count:%d, "
5382 "size:%d\n", type, *extnt_count, *extnt_size);
5384 err_exit:
5385 mempool_free(mbox, phba->mbox_mem_pool);
5386 return rc;
5390 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5391 * @phba: Pointer to HBA context object.
5392 * @type: The extent type to check.
5394 * This function reads the current available extents from the port and checks
5395 * if the extent count or extent size has changed since the last access.
5396 * Callers use this routine post port reset to understand if there is a
5397 * extent reprovisioning requirement.
5399 * Returns:
5400 * -Error: error indicates problem.
5401 * 1: Extent count or size has changed.
5402 * 0: No changes.
5404 static int
5405 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5407 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5408 uint16_t size_diff, rsrc_ext_size;
5409 int rc = 0;
5410 struct lpfc_rsrc_blks *rsrc_entry;
5411 struct list_head *rsrc_blk_list = NULL;
5413 size_diff = 0;
5414 curr_ext_cnt = 0;
5415 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5416 &rsrc_ext_cnt,
5417 &rsrc_ext_size);
5418 if (unlikely(rc))
5419 return -EIO;
5421 switch (type) {
5422 case LPFC_RSC_TYPE_FCOE_RPI:
5423 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5424 break;
5425 case LPFC_RSC_TYPE_FCOE_VPI:
5426 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5427 break;
5428 case LPFC_RSC_TYPE_FCOE_XRI:
5429 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5430 break;
5431 case LPFC_RSC_TYPE_FCOE_VFI:
5432 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5433 break;
5434 default:
5435 break;
5438 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5439 curr_ext_cnt++;
5440 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5441 size_diff++;
5444 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5445 rc = 1;
5447 return rc;
5451 * lpfc_sli4_cfg_post_extnts -
5452 * @phba: Pointer to HBA context object.
5453 * @extnt_cnt - number of available extents.
5454 * @type - the extent type (rpi, xri, vfi, vpi).
5455 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5456 * @mbox - pointer to the caller's allocated mailbox structure.
5458 * This function executes the extents allocation request. It also
5459 * takes care of the amount of memory needed to allocate or get the
5460 * allocated extents. It is the caller's responsibility to evaluate
5461 * the response.
5463 * Returns:
5464 * -Error: Error value describes the condition found.
5465 * 0: if successful
5467 static int
5468 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5469 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5471 int rc = 0;
5472 uint32_t req_len;
5473 uint32_t emb_len;
5474 uint32_t alloc_len, mbox_tmo;
5476 /* Calculate the total requested length of the dma memory */
5477 req_len = extnt_cnt * sizeof(uint16_t);
5480 * Calculate the size of an embedded mailbox. The uint32_t
5481 * accounts for extents-specific word.
5483 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5484 sizeof(uint32_t);
5487 * Presume the allocation and response will fit into an embedded
5488 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5490 *emb = LPFC_SLI4_MBX_EMBED;
5491 if (req_len > emb_len) {
5492 req_len = extnt_cnt * sizeof(uint16_t) +
5493 sizeof(union lpfc_sli4_cfg_shdr) +
5494 sizeof(uint32_t);
5495 *emb = LPFC_SLI4_MBX_NEMBED;
5498 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5499 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5500 req_len, *emb);
5501 if (alloc_len < req_len) {
5502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5503 "2982 Allocated DMA memory size (x%x) is "
5504 "less than the requested DMA memory "
5505 "size (x%x)\n", alloc_len, req_len);
5506 return -ENOMEM;
5508 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5509 if (unlikely(rc))
5510 return -EIO;
5512 if (!phba->sli4_hba.intr_enable)
5513 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5514 else {
5515 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5516 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5519 if (unlikely(rc))
5520 rc = -EIO;
5521 return rc;
5525 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5526 * @phba: Pointer to HBA context object.
5527 * @type: The resource extent type to allocate.
5529 * This function allocates the number of elements for the specified
5530 * resource type.
5532 static int
5533 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5535 bool emb = false;
5536 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5537 uint16_t rsrc_id, rsrc_start, j, k;
5538 uint16_t *ids;
5539 int i, rc;
5540 unsigned long longs;
5541 unsigned long *bmask;
5542 struct lpfc_rsrc_blks *rsrc_blks;
5543 LPFC_MBOXQ_t *mbox;
5544 uint32_t length;
5545 struct lpfc_id_range *id_array = NULL;
5546 void *virtaddr = NULL;
5547 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5548 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5549 struct list_head *ext_blk_list;
5551 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5552 &rsrc_cnt,
5553 &rsrc_size);
5554 if (unlikely(rc))
5555 return -EIO;
5557 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5558 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5559 "3009 No available Resource Extents "
5560 "for resource type 0x%x: Count: 0x%x, "
5561 "Size 0x%x\n", type, rsrc_cnt,
5562 rsrc_size);
5563 return -ENOMEM;
5566 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5567 "2903 Post resource extents type-0x%x: "
5568 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5570 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5571 if (!mbox)
5572 return -ENOMEM;
5574 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5575 if (unlikely(rc)) {
5576 rc = -EIO;
5577 goto err_exit;
5581 * Figure out where the response is located. Then get local pointers
5582 * to the response data. The port does not guarantee to respond to
5583 * all extents counts request so update the local variable with the
5584 * allocated count from the port.
5586 if (emb == LPFC_SLI4_MBX_EMBED) {
5587 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5588 id_array = &rsrc_ext->u.rsp.id[0];
5589 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5590 } else {
5591 virtaddr = mbox->sge_array->addr[0];
5592 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5593 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5594 id_array = &n_rsrc->id;
5597 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5598 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5601 * Based on the resource size and count, correct the base and max
5602 * resource values.
5604 length = sizeof(struct lpfc_rsrc_blks);
5605 switch (type) {
5606 case LPFC_RSC_TYPE_FCOE_RPI:
5607 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5608 sizeof(unsigned long),
5609 GFP_KERNEL);
5610 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5611 rc = -ENOMEM;
5612 goto err_exit;
5614 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5615 sizeof(uint16_t),
5616 GFP_KERNEL);
5617 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5618 kfree(phba->sli4_hba.rpi_bmask);
5619 rc = -ENOMEM;
5620 goto err_exit;
5624 * The next_rpi was initialized with the maximum available
5625 * count but the port may allocate a smaller number. Catch
5626 * that case and update the next_rpi.
5628 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5630 /* Initialize local ptrs for common extent processing later. */
5631 bmask = phba->sli4_hba.rpi_bmask;
5632 ids = phba->sli4_hba.rpi_ids;
5633 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5634 break;
5635 case LPFC_RSC_TYPE_FCOE_VPI:
5636 phba->vpi_bmask = kzalloc(longs *
5637 sizeof(unsigned long),
5638 GFP_KERNEL);
5639 if (unlikely(!phba->vpi_bmask)) {
5640 rc = -ENOMEM;
5641 goto err_exit;
5643 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5644 sizeof(uint16_t),
5645 GFP_KERNEL);
5646 if (unlikely(!phba->vpi_ids)) {
5647 kfree(phba->vpi_bmask);
5648 rc = -ENOMEM;
5649 goto err_exit;
5652 /* Initialize local ptrs for common extent processing later. */
5653 bmask = phba->vpi_bmask;
5654 ids = phba->vpi_ids;
5655 ext_blk_list = &phba->lpfc_vpi_blk_list;
5656 break;
5657 case LPFC_RSC_TYPE_FCOE_XRI:
5658 phba->sli4_hba.xri_bmask = kzalloc(longs *
5659 sizeof(unsigned long),
5660 GFP_KERNEL);
5661 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5662 rc = -ENOMEM;
5663 goto err_exit;
5665 phba->sli4_hba.max_cfg_param.xri_used = 0;
5666 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5667 sizeof(uint16_t),
5668 GFP_KERNEL);
5669 if (unlikely(!phba->sli4_hba.xri_ids)) {
5670 kfree(phba->sli4_hba.xri_bmask);
5671 rc = -ENOMEM;
5672 goto err_exit;
5675 /* Initialize local ptrs for common extent processing later. */
5676 bmask = phba->sli4_hba.xri_bmask;
5677 ids = phba->sli4_hba.xri_ids;
5678 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_VFI:
5681 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5682 sizeof(unsigned long),
5683 GFP_KERNEL);
5684 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5685 rc = -ENOMEM;
5686 goto err_exit;
5688 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5689 sizeof(uint16_t),
5690 GFP_KERNEL);
5691 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5692 kfree(phba->sli4_hba.vfi_bmask);
5693 rc = -ENOMEM;
5694 goto err_exit;
5697 /* Initialize local ptrs for common extent processing later. */
5698 bmask = phba->sli4_hba.vfi_bmask;
5699 ids = phba->sli4_hba.vfi_ids;
5700 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5701 break;
5702 default:
5703 /* Unsupported Opcode. Fail call. */
5704 id_array = NULL;
5705 bmask = NULL;
5706 ids = NULL;
5707 ext_blk_list = NULL;
5708 goto err_exit;
5712 * Complete initializing the extent configuration with the
5713 * allocated ids assigned to this function. The bitmask serves
5714 * as an index into the array and manages the available ids. The
5715 * array just stores the ids communicated to the port via the wqes.
5717 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5718 if ((i % 2) == 0)
5719 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5720 &id_array[k]);
5721 else
5722 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5723 &id_array[k]);
5725 rsrc_blks = kzalloc(length, GFP_KERNEL);
5726 if (unlikely(!rsrc_blks)) {
5727 rc = -ENOMEM;
5728 kfree(bmask);
5729 kfree(ids);
5730 goto err_exit;
5732 rsrc_blks->rsrc_start = rsrc_id;
5733 rsrc_blks->rsrc_size = rsrc_size;
5734 list_add_tail(&rsrc_blks->list, ext_blk_list);
5735 rsrc_start = rsrc_id;
5736 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5737 phba->sli4_hba.scsi_xri_start = rsrc_start +
5738 lpfc_sli4_get_iocb_cnt(phba);
5739 phba->sli4_hba.nvme_xri_start =
5740 phba->sli4_hba.scsi_xri_start +
5741 phba->sli4_hba.scsi_xri_max;
5744 while (rsrc_id < (rsrc_start + rsrc_size)) {
5745 ids[j] = rsrc_id;
5746 rsrc_id++;
5747 j++;
5749 /* Entire word processed. Get next word.*/
5750 if ((i % 2) == 1)
5751 k++;
5753 err_exit:
5754 lpfc_sli4_mbox_cmd_free(phba, mbox);
5755 return rc;
5761 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5762 * @phba: Pointer to HBA context object.
5763 * @type: the extent's type.
5765 * This function deallocates all extents of a particular resource type.
5766 * SLI4 does not allow for deallocating a particular extent range. It
5767 * is the caller's responsibility to release all kernel memory resources.
5769 static int
5770 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5772 int rc;
5773 uint32_t length, mbox_tmo = 0;
5774 LPFC_MBOXQ_t *mbox;
5775 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5776 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5778 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5779 if (!mbox)
5780 return -ENOMEM;
5783 * This function sends an embedded mailbox because it only sends the
5784 * the resource type. All extents of this type are released by the
5785 * port.
5787 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5788 sizeof(struct lpfc_sli4_cfg_mhdr));
5789 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5790 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5791 length, LPFC_SLI4_MBX_EMBED);
5793 /* Send an extents count of 0 - the dealloc doesn't use it. */
5794 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5795 LPFC_SLI4_MBX_EMBED);
5796 if (unlikely(rc)) {
5797 rc = -EIO;
5798 goto out_free_mbox;
5800 if (!phba->sli4_hba.intr_enable)
5801 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5802 else {
5803 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5804 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5806 if (unlikely(rc)) {
5807 rc = -EIO;
5808 goto out_free_mbox;
5811 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5812 if (bf_get(lpfc_mbox_hdr_status,
5813 &dealloc_rsrc->header.cfg_shdr.response)) {
5814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5815 "2919 Failed to release resource extents "
5816 "for type %d - Status 0x%x Add'l Status 0x%x. "
5817 "Resource memory not released.\n",
5818 type,
5819 bf_get(lpfc_mbox_hdr_status,
5820 &dealloc_rsrc->header.cfg_shdr.response),
5821 bf_get(lpfc_mbox_hdr_add_status,
5822 &dealloc_rsrc->header.cfg_shdr.response));
5823 rc = -EIO;
5824 goto out_free_mbox;
5827 /* Release kernel memory resources for the specific type. */
5828 switch (type) {
5829 case LPFC_RSC_TYPE_FCOE_VPI:
5830 kfree(phba->vpi_bmask);
5831 kfree(phba->vpi_ids);
5832 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5833 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5834 &phba->lpfc_vpi_blk_list, list) {
5835 list_del_init(&rsrc_blk->list);
5836 kfree(rsrc_blk);
5838 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5839 break;
5840 case LPFC_RSC_TYPE_FCOE_XRI:
5841 kfree(phba->sli4_hba.xri_bmask);
5842 kfree(phba->sli4_hba.xri_ids);
5843 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5844 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5845 list_del_init(&rsrc_blk->list);
5846 kfree(rsrc_blk);
5848 break;
5849 case LPFC_RSC_TYPE_FCOE_VFI:
5850 kfree(phba->sli4_hba.vfi_bmask);
5851 kfree(phba->sli4_hba.vfi_ids);
5852 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5853 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5854 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5855 list_del_init(&rsrc_blk->list);
5856 kfree(rsrc_blk);
5858 break;
5859 case LPFC_RSC_TYPE_FCOE_RPI:
5860 /* RPI bitmask and physical id array are cleaned up earlier. */
5861 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5862 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5863 list_del_init(&rsrc_blk->list);
5864 kfree(rsrc_blk);
5866 break;
5867 default:
5868 break;
5871 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5873 out_free_mbox:
5874 mempool_free(mbox, phba->mbox_mem_pool);
5875 return rc;
5878 static void
5879 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5880 uint32_t feature)
5882 uint32_t len;
5884 len = sizeof(struct lpfc_mbx_set_feature) -
5885 sizeof(struct lpfc_sli4_cfg_mhdr);
5886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5887 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5888 LPFC_SLI4_MBX_EMBED);
5890 switch (feature) {
5891 case LPFC_SET_UE_RECOVERY:
5892 bf_set(lpfc_mbx_set_feature_UER,
5893 &mbox->u.mqe.un.set_feature, 1);
5894 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5895 mbox->u.mqe.un.set_feature.param_len = 8;
5896 break;
5897 case LPFC_SET_MDS_DIAGS:
5898 bf_set(lpfc_mbx_set_feature_mds,
5899 &mbox->u.mqe.un.set_feature, 1);
5900 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5901 &mbox->u.mqe.un.set_feature, 0);
5902 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5903 mbox->u.mqe.un.set_feature.param_len = 8;
5904 break;
5907 return;
5911 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5912 * @phba: Pointer to HBA context object.
5914 * This function allocates all SLI4 resource identifiers.
5917 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5919 int i, rc, error = 0;
5920 uint16_t count, base;
5921 unsigned long longs;
5923 if (!phba->sli4_hba.rpi_hdrs_in_use)
5924 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5925 if (phba->sli4_hba.extents_in_use) {
5927 * The port supports resource extents. The XRI, VPI, VFI, RPI
5928 * resource extent count must be read and allocated before
5929 * provisioning the resource id arrays.
5931 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5932 LPFC_IDX_RSRC_RDY) {
5934 * Extent-based resources are set - the driver could
5935 * be in a port reset. Figure out if any corrective
5936 * actions need to be taken.
5938 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5939 LPFC_RSC_TYPE_FCOE_VFI);
5940 if (rc != 0)
5941 error++;
5942 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5943 LPFC_RSC_TYPE_FCOE_VPI);
5944 if (rc != 0)
5945 error++;
5946 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5947 LPFC_RSC_TYPE_FCOE_XRI);
5948 if (rc != 0)
5949 error++;
5950 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5951 LPFC_RSC_TYPE_FCOE_RPI);
5952 if (rc != 0)
5953 error++;
5956 * It's possible that the number of resources
5957 * provided to this port instance changed between
5958 * resets. Detect this condition and reallocate
5959 * resources. Otherwise, there is no action.
5961 if (error) {
5962 lpfc_printf_log(phba, KERN_INFO,
5963 LOG_MBOX | LOG_INIT,
5964 "2931 Detected extent resource "
5965 "change. Reallocating all "
5966 "extents.\n");
5967 rc = lpfc_sli4_dealloc_extent(phba,
5968 LPFC_RSC_TYPE_FCOE_VFI);
5969 rc = lpfc_sli4_dealloc_extent(phba,
5970 LPFC_RSC_TYPE_FCOE_VPI);
5971 rc = lpfc_sli4_dealloc_extent(phba,
5972 LPFC_RSC_TYPE_FCOE_XRI);
5973 rc = lpfc_sli4_dealloc_extent(phba,
5974 LPFC_RSC_TYPE_FCOE_RPI);
5975 } else
5976 return 0;
5979 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5980 if (unlikely(rc))
5981 goto err_exit;
5983 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5984 if (unlikely(rc))
5985 goto err_exit;
5987 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5988 if (unlikely(rc))
5989 goto err_exit;
5991 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5992 if (unlikely(rc))
5993 goto err_exit;
5994 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5995 LPFC_IDX_RSRC_RDY);
5996 return rc;
5997 } else {
5999 * The port does not support resource extents. The XRI, VPI,
6000 * VFI, RPI resource ids were determined from READ_CONFIG.
6001 * Just allocate the bitmasks and provision the resource id
6002 * arrays. If a port reset is active, the resources don't
6003 * need any action - just exit.
6005 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6006 LPFC_IDX_RSRC_RDY) {
6007 lpfc_sli4_dealloc_resource_identifiers(phba);
6008 lpfc_sli4_remove_rpis(phba);
6010 /* RPIs. */
6011 count = phba->sli4_hba.max_cfg_param.max_rpi;
6012 if (count <= 0) {
6013 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6014 "3279 Invalid provisioning of "
6015 "rpi:%d\n", count);
6016 rc = -EINVAL;
6017 goto err_exit;
6019 base = phba->sli4_hba.max_cfg_param.rpi_base;
6020 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6021 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6022 sizeof(unsigned long),
6023 GFP_KERNEL);
6024 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6025 rc = -ENOMEM;
6026 goto err_exit;
6028 phba->sli4_hba.rpi_ids = kzalloc(count *
6029 sizeof(uint16_t),
6030 GFP_KERNEL);
6031 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6032 rc = -ENOMEM;
6033 goto free_rpi_bmask;
6036 for (i = 0; i < count; i++)
6037 phba->sli4_hba.rpi_ids[i] = base + i;
6039 /* VPIs. */
6040 count = phba->sli4_hba.max_cfg_param.max_vpi;
6041 if (count <= 0) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6043 "3280 Invalid provisioning of "
6044 "vpi:%d\n", count);
6045 rc = -EINVAL;
6046 goto free_rpi_ids;
6048 base = phba->sli4_hba.max_cfg_param.vpi_base;
6049 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6050 phba->vpi_bmask = kzalloc(longs *
6051 sizeof(unsigned long),
6052 GFP_KERNEL);
6053 if (unlikely(!phba->vpi_bmask)) {
6054 rc = -ENOMEM;
6055 goto free_rpi_ids;
6057 phba->vpi_ids = kzalloc(count *
6058 sizeof(uint16_t),
6059 GFP_KERNEL);
6060 if (unlikely(!phba->vpi_ids)) {
6061 rc = -ENOMEM;
6062 goto free_vpi_bmask;
6065 for (i = 0; i < count; i++)
6066 phba->vpi_ids[i] = base + i;
6068 /* XRIs. */
6069 count = phba->sli4_hba.max_cfg_param.max_xri;
6070 if (count <= 0) {
6071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6072 "3281 Invalid provisioning of "
6073 "xri:%d\n", count);
6074 rc = -EINVAL;
6075 goto free_vpi_ids;
6077 base = phba->sli4_hba.max_cfg_param.xri_base;
6078 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6079 phba->sli4_hba.xri_bmask = kzalloc(longs *
6080 sizeof(unsigned long),
6081 GFP_KERNEL);
6082 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6083 rc = -ENOMEM;
6084 goto free_vpi_ids;
6086 phba->sli4_hba.max_cfg_param.xri_used = 0;
6087 phba->sli4_hba.xri_ids = kzalloc(count *
6088 sizeof(uint16_t),
6089 GFP_KERNEL);
6090 if (unlikely(!phba->sli4_hba.xri_ids)) {
6091 rc = -ENOMEM;
6092 goto free_xri_bmask;
6095 for (i = 0; i < count; i++)
6096 phba->sli4_hba.xri_ids[i] = base + i;
6098 /* VFIs. */
6099 count = phba->sli4_hba.max_cfg_param.max_vfi;
6100 if (count <= 0) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6102 "3282 Invalid provisioning of "
6103 "vfi:%d\n", count);
6104 rc = -EINVAL;
6105 goto free_xri_ids;
6107 base = phba->sli4_hba.max_cfg_param.vfi_base;
6108 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6109 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6110 sizeof(unsigned long),
6111 GFP_KERNEL);
6112 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6113 rc = -ENOMEM;
6114 goto free_xri_ids;
6116 phba->sli4_hba.vfi_ids = kzalloc(count *
6117 sizeof(uint16_t),
6118 GFP_KERNEL);
6119 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6120 rc = -ENOMEM;
6121 goto free_vfi_bmask;
6124 for (i = 0; i < count; i++)
6125 phba->sli4_hba.vfi_ids[i] = base + i;
6128 * Mark all resources ready. An HBA reset doesn't need
6129 * to reset the initialization.
6131 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6132 LPFC_IDX_RSRC_RDY);
6133 return 0;
6136 free_vfi_bmask:
6137 kfree(phba->sli4_hba.vfi_bmask);
6138 phba->sli4_hba.vfi_bmask = NULL;
6139 free_xri_ids:
6140 kfree(phba->sli4_hba.xri_ids);
6141 phba->sli4_hba.xri_ids = NULL;
6142 free_xri_bmask:
6143 kfree(phba->sli4_hba.xri_bmask);
6144 phba->sli4_hba.xri_bmask = NULL;
6145 free_vpi_ids:
6146 kfree(phba->vpi_ids);
6147 phba->vpi_ids = NULL;
6148 free_vpi_bmask:
6149 kfree(phba->vpi_bmask);
6150 phba->vpi_bmask = NULL;
6151 free_rpi_ids:
6152 kfree(phba->sli4_hba.rpi_ids);
6153 phba->sli4_hba.rpi_ids = NULL;
6154 free_rpi_bmask:
6155 kfree(phba->sli4_hba.rpi_bmask);
6156 phba->sli4_hba.rpi_bmask = NULL;
6157 err_exit:
6158 return rc;
6162 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6163 * @phba: Pointer to HBA context object.
6165 * This function allocates the number of elements for the specified
6166 * resource type.
6169 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6171 if (phba->sli4_hba.extents_in_use) {
6172 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6173 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6174 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6175 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6176 } else {
6177 kfree(phba->vpi_bmask);
6178 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6179 kfree(phba->vpi_ids);
6180 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6181 kfree(phba->sli4_hba.xri_bmask);
6182 kfree(phba->sli4_hba.xri_ids);
6183 kfree(phba->sli4_hba.vfi_bmask);
6184 kfree(phba->sli4_hba.vfi_ids);
6185 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6186 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6189 return 0;
6193 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6194 * @phba: Pointer to HBA context object.
6195 * @type: The resource extent type.
6196 * @extnt_count: buffer to hold port extent count response
6197 * @extnt_size: buffer to hold port extent size response.
6199 * This function calls the port to read the host allocated extents
6200 * for a particular type.
6203 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6204 uint16_t *extnt_cnt, uint16_t *extnt_size)
6206 bool emb;
6207 int rc = 0;
6208 uint16_t curr_blks = 0;
6209 uint32_t req_len, emb_len;
6210 uint32_t alloc_len, mbox_tmo;
6211 struct list_head *blk_list_head;
6212 struct lpfc_rsrc_blks *rsrc_blk;
6213 LPFC_MBOXQ_t *mbox;
6214 void *virtaddr = NULL;
6215 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6216 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6217 union lpfc_sli4_cfg_shdr *shdr;
6219 switch (type) {
6220 case LPFC_RSC_TYPE_FCOE_VPI:
6221 blk_list_head = &phba->lpfc_vpi_blk_list;
6222 break;
6223 case LPFC_RSC_TYPE_FCOE_XRI:
6224 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6225 break;
6226 case LPFC_RSC_TYPE_FCOE_VFI:
6227 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6228 break;
6229 case LPFC_RSC_TYPE_FCOE_RPI:
6230 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6231 break;
6232 default:
6233 return -EIO;
6236 /* Count the number of extents currently allocatd for this type. */
6237 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6238 if (curr_blks == 0) {
6240 * The GET_ALLOCATED mailbox does not return the size,
6241 * just the count. The size should be just the size
6242 * stored in the current allocated block and all sizes
6243 * for an extent type are the same so set the return
6244 * value now.
6246 *extnt_size = rsrc_blk->rsrc_size;
6248 curr_blks++;
6252 * Calculate the size of an embedded mailbox. The uint32_t
6253 * accounts for extents-specific word.
6255 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6256 sizeof(uint32_t);
6259 * Presume the allocation and response will fit into an embedded
6260 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6262 emb = LPFC_SLI4_MBX_EMBED;
6263 req_len = emb_len;
6264 if (req_len > emb_len) {
6265 req_len = curr_blks * sizeof(uint16_t) +
6266 sizeof(union lpfc_sli4_cfg_shdr) +
6267 sizeof(uint32_t);
6268 emb = LPFC_SLI4_MBX_NEMBED;
6271 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6272 if (!mbox)
6273 return -ENOMEM;
6274 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6276 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6277 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6278 req_len, emb);
6279 if (alloc_len < req_len) {
6280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6281 "2983 Allocated DMA memory size (x%x) is "
6282 "less than the requested DMA memory "
6283 "size (x%x)\n", alloc_len, req_len);
6284 rc = -ENOMEM;
6285 goto err_exit;
6287 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6288 if (unlikely(rc)) {
6289 rc = -EIO;
6290 goto err_exit;
6293 if (!phba->sli4_hba.intr_enable)
6294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6295 else {
6296 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6297 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6300 if (unlikely(rc)) {
6301 rc = -EIO;
6302 goto err_exit;
6306 * Figure out where the response is located. Then get local pointers
6307 * to the response data. The port does not guarantee to respond to
6308 * all extents counts request so update the local variable with the
6309 * allocated count from the port.
6311 if (emb == LPFC_SLI4_MBX_EMBED) {
6312 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6313 shdr = &rsrc_ext->header.cfg_shdr;
6314 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6315 } else {
6316 virtaddr = mbox->sge_array->addr[0];
6317 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6318 shdr = &n_rsrc->cfg_shdr;
6319 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6322 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6324 "2984 Failed to read allocated resources "
6325 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6326 type,
6327 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6328 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6329 rc = -EIO;
6330 goto err_exit;
6332 err_exit:
6333 lpfc_sli4_mbox_cmd_free(phba, mbox);
6334 return rc;
6338 * lpfc_sli4_repost_sgl_list - Repsot the buffers sgl pages as block
6339 * @phba: pointer to lpfc hba data structure.
6340 * @pring: Pointer to driver SLI ring object.
6341 * @sgl_list: linked link of sgl buffers to post
6342 * @cnt: number of linked list buffers
6344 * This routine walks the list of buffers that have been allocated and
6345 * repost them to the port by using SGL block post. This is needed after a
6346 * pci_function_reset/warm_start or start. It attempts to construct blocks
6347 * of buffer sgls which contains contiguous xris and uses the non-embedded
6348 * SGL block post mailbox commands to post them to the port. For single
6349 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6350 * mailbox command for posting.
6352 * Returns: 0 = success, non-zero failure.
6354 static int
6355 lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6356 struct list_head *sgl_list, int cnt)
6358 struct lpfc_sglq *sglq_entry = NULL;
6359 struct lpfc_sglq *sglq_entry_next = NULL;
6360 struct lpfc_sglq *sglq_entry_first = NULL;
6361 int status, total_cnt;
6362 int post_cnt = 0, num_posted = 0, block_cnt = 0;
6363 int last_xritag = NO_XRI;
6364 LIST_HEAD(prep_sgl_list);
6365 LIST_HEAD(blck_sgl_list);
6366 LIST_HEAD(allc_sgl_list);
6367 LIST_HEAD(post_sgl_list);
6368 LIST_HEAD(free_sgl_list);
6370 spin_lock_irq(&phba->hbalock);
6371 spin_lock(&phba->sli4_hba.sgl_list_lock);
6372 list_splice_init(sgl_list, &allc_sgl_list);
6373 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6374 spin_unlock_irq(&phba->hbalock);
6376 total_cnt = cnt;
6377 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6378 &allc_sgl_list, list) {
6379 list_del_init(&sglq_entry->list);
6380 block_cnt++;
6381 if ((last_xritag != NO_XRI) &&
6382 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6383 /* a hole in xri block, form a sgl posting block */
6384 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6385 post_cnt = block_cnt - 1;
6386 /* prepare list for next posting block */
6387 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6388 block_cnt = 1;
6389 } else {
6390 /* prepare list for next posting block */
6391 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6392 /* enough sgls for non-embed sgl mbox command */
6393 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6394 list_splice_init(&prep_sgl_list,
6395 &blck_sgl_list);
6396 post_cnt = block_cnt;
6397 block_cnt = 0;
6400 num_posted++;
6402 /* keep track of last sgl's xritag */
6403 last_xritag = sglq_entry->sli4_xritag;
6405 /* end of repost sgl list condition for buffers */
6406 if (num_posted == total_cnt) {
6407 if (post_cnt == 0) {
6408 list_splice_init(&prep_sgl_list,
6409 &blck_sgl_list);
6410 post_cnt = block_cnt;
6411 } else if (block_cnt == 1) {
6412 status = lpfc_sli4_post_sgl(phba,
6413 sglq_entry->phys, 0,
6414 sglq_entry->sli4_xritag);
6415 if (!status) {
6416 /* successful, put sgl to posted list */
6417 list_add_tail(&sglq_entry->list,
6418 &post_sgl_list);
6419 } else {
6420 /* Failure, put sgl to free list */
6421 lpfc_printf_log(phba, KERN_WARNING,
6422 LOG_SLI,
6423 "3159 Failed to post "
6424 "sgl, xritag:x%x\n",
6425 sglq_entry->sli4_xritag);
6426 list_add_tail(&sglq_entry->list,
6427 &free_sgl_list);
6428 total_cnt--;
6433 /* continue until a nembed page worth of sgls */
6434 if (post_cnt == 0)
6435 continue;
6437 /* post the buffer list sgls as a block */
6438 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6439 post_cnt);
6441 if (!status) {
6442 /* success, put sgl list to posted sgl list */
6443 list_splice_init(&blck_sgl_list, &post_sgl_list);
6444 } else {
6445 /* Failure, put sgl list to free sgl list */
6446 sglq_entry_first = list_first_entry(&blck_sgl_list,
6447 struct lpfc_sglq,
6448 list);
6449 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6450 "3160 Failed to post sgl-list, "
6451 "xritag:x%x-x%x\n",
6452 sglq_entry_first->sli4_xritag,
6453 (sglq_entry_first->sli4_xritag +
6454 post_cnt - 1));
6455 list_splice_init(&blck_sgl_list, &free_sgl_list);
6456 total_cnt -= post_cnt;
6459 /* don't reset xirtag due to hole in xri block */
6460 if (block_cnt == 0)
6461 last_xritag = NO_XRI;
6463 /* reset sgl post count for next round of posting */
6464 post_cnt = 0;
6467 /* free the sgls failed to post */
6468 lpfc_free_sgl_list(phba, &free_sgl_list);
6470 /* push sgls posted to the available list */
6471 if (!list_empty(&post_sgl_list)) {
6472 spin_lock_irq(&phba->hbalock);
6473 spin_lock(&phba->sli4_hba.sgl_list_lock);
6474 list_splice_init(&post_sgl_list, sgl_list);
6475 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6476 spin_unlock_irq(&phba->hbalock);
6477 } else {
6478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6479 "3161 Failure to post sgl to port.\n");
6480 return -EIO;
6483 /* return the number of XRIs actually posted */
6484 return total_cnt;
6487 void
6488 lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6490 uint32_t len;
6492 len = sizeof(struct lpfc_mbx_set_host_data) -
6493 sizeof(struct lpfc_sli4_cfg_mhdr);
6494 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6495 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6496 LPFC_SLI4_MBX_EMBED);
6498 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6499 mbox->u.mqe.un.set_host_data.param_len =
6500 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
6501 snprintf(mbox->u.mqe.un.set_host_data.data,
6502 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6503 "Linux %s v"LPFC_DRIVER_VERSION,
6504 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6508 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6509 * @phba: Pointer to HBA context object.
6511 * This function is the main SLI4 device intialization PCI function. This
6512 * function is called by the HBA intialization code, HBA reset code and
6513 * HBA error attention handler code. Caller is not required to hold any
6514 * locks.
6517 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6519 int rc, i;
6520 LPFC_MBOXQ_t *mboxq;
6521 struct lpfc_mqe *mqe;
6522 uint8_t *vpd;
6523 uint32_t vpd_size;
6524 uint32_t ftr_rsp = 0;
6525 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6526 struct lpfc_vport *vport = phba->pport;
6527 struct lpfc_dmabuf *mp;
6528 struct lpfc_rqb *rqbp;
6530 /* Perform a PCI function reset to start from clean */
6531 rc = lpfc_pci_function_reset(phba);
6532 if (unlikely(rc))
6533 return -ENODEV;
6535 /* Check the HBA Host Status Register for readyness */
6536 rc = lpfc_sli4_post_status_check(phba);
6537 if (unlikely(rc))
6538 return -ENODEV;
6539 else {
6540 spin_lock_irq(&phba->hbalock);
6541 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6542 spin_unlock_irq(&phba->hbalock);
6546 * Allocate a single mailbox container for initializing the
6547 * port.
6549 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6550 if (!mboxq)
6551 return -ENOMEM;
6553 /* Issue READ_REV to collect vpd and FW information. */
6554 vpd_size = SLI4_PAGE_SIZE;
6555 vpd = kzalloc(vpd_size, GFP_KERNEL);
6556 if (!vpd) {
6557 rc = -ENOMEM;
6558 goto out_free_mbox;
6561 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6562 if (unlikely(rc)) {
6563 kfree(vpd);
6564 goto out_free_mbox;
6567 mqe = &mboxq->u.mqe;
6568 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6569 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6570 phba->hba_flag |= HBA_FCOE_MODE;
6571 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6572 } else {
6573 phba->hba_flag &= ~HBA_FCOE_MODE;
6576 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6577 LPFC_DCBX_CEE_MODE)
6578 phba->hba_flag |= HBA_FIP_SUPPORT;
6579 else
6580 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6582 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6584 if (phba->sli_rev != LPFC_SLI_REV4) {
6585 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6586 "0376 READ_REV Error. SLI Level %d "
6587 "FCoE enabled %d\n",
6588 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6589 rc = -EIO;
6590 kfree(vpd);
6591 goto out_free_mbox;
6595 * Continue initialization with default values even if driver failed
6596 * to read FCoE param config regions, only read parameters if the
6597 * board is FCoE
6599 if (phba->hba_flag & HBA_FCOE_MODE &&
6600 lpfc_sli4_read_fcoe_params(phba))
6601 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6602 "2570 Failed to read FCoE parameters\n");
6605 * Retrieve sli4 device physical port name, failure of doing it
6606 * is considered as non-fatal.
6608 rc = lpfc_sli4_retrieve_pport_name(phba);
6609 if (!rc)
6610 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6611 "3080 Successful retrieving SLI4 device "
6612 "physical port name: %s.\n", phba->Port);
6615 * Evaluate the read rev and vpd data. Populate the driver
6616 * state with the results. If this routine fails, the failure
6617 * is not fatal as the driver will use generic values.
6619 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6620 if (unlikely(!rc)) {
6621 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6622 "0377 Error %d parsing vpd. "
6623 "Using defaults.\n", rc);
6624 rc = 0;
6626 kfree(vpd);
6628 /* Save information as VPD data */
6629 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6630 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6631 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6632 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6633 &mqe->un.read_rev);
6634 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6635 &mqe->un.read_rev);
6636 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6637 &mqe->un.read_rev);
6638 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6639 &mqe->un.read_rev);
6640 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6641 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6642 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6643 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6644 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6645 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6646 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6647 "(%d):0380 READ_REV Status x%x "
6648 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6649 mboxq->vport ? mboxq->vport->vpi : 0,
6650 bf_get(lpfc_mqe_status, mqe),
6651 phba->vpd.rev.opFwName,
6652 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6653 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6655 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6656 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6657 if (phba->pport->cfg_lun_queue_depth > rc) {
6658 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6659 "3362 LUN queue depth changed from %d to %d\n",
6660 phba->pport->cfg_lun_queue_depth, rc);
6661 phba->pport->cfg_lun_queue_depth = rc;
6664 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6665 LPFC_SLI_INTF_IF_TYPE_0) {
6666 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6667 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6668 if (rc == MBX_SUCCESS) {
6669 phba->hba_flag |= HBA_RECOVERABLE_UE;
6670 /* Set 1Sec interval to detect UE */
6671 phba->eratt_poll_interval = 1;
6672 phba->sli4_hba.ue_to_sr = bf_get(
6673 lpfc_mbx_set_feature_UESR,
6674 &mboxq->u.mqe.un.set_feature);
6675 phba->sli4_hba.ue_to_rp = bf_get(
6676 lpfc_mbx_set_feature_UERP,
6677 &mboxq->u.mqe.un.set_feature);
6681 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6682 /* Enable MDS Diagnostics only if the SLI Port supports it */
6683 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6684 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6685 if (rc != MBX_SUCCESS)
6686 phba->mds_diags_support = 0;
6690 * Discover the port's supported feature set and match it against the
6691 * hosts requests.
6693 lpfc_request_features(phba, mboxq);
6694 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6695 if (unlikely(rc)) {
6696 rc = -EIO;
6697 goto out_free_mbox;
6701 * The port must support FCP initiator mode as this is the
6702 * only mode running in the host.
6704 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6705 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6706 "0378 No support for fcpi mode.\n");
6707 ftr_rsp++;
6709 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6710 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6711 else
6712 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6714 * If the port cannot support the host's requested features
6715 * then turn off the global config parameters to disable the
6716 * feature in the driver. This is not a fatal error.
6718 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6719 if (phba->cfg_enable_bg) {
6720 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6721 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6722 else
6723 ftr_rsp++;
6726 if (phba->max_vpi && phba->cfg_enable_npiv &&
6727 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6728 ftr_rsp++;
6730 if (ftr_rsp) {
6731 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6732 "0379 Feature Mismatch Data: x%08x %08x "
6733 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6734 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6735 phba->cfg_enable_npiv, phba->max_vpi);
6736 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6737 phba->cfg_enable_bg = 0;
6738 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6739 phba->cfg_enable_npiv = 0;
6742 /* These SLI3 features are assumed in SLI4 */
6743 spin_lock_irq(&phba->hbalock);
6744 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6745 spin_unlock_irq(&phba->hbalock);
6748 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6749 * calls depends on these resources to complete port setup.
6751 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6752 if (rc) {
6753 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6754 "2920 Failed to alloc Resource IDs "
6755 "rc = x%x\n", rc);
6756 goto out_free_mbox;
6759 lpfc_set_host_data(phba, mboxq);
6761 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6762 if (rc) {
6763 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6764 "2134 Failed to set host os driver version %x",
6765 rc);
6768 /* Read the port's service parameters. */
6769 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6770 if (rc) {
6771 phba->link_state = LPFC_HBA_ERROR;
6772 rc = -ENOMEM;
6773 goto out_free_mbox;
6776 mboxq->vport = vport;
6777 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6778 mp = (struct lpfc_dmabuf *) mboxq->context1;
6779 if (rc == MBX_SUCCESS) {
6780 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6781 rc = 0;
6785 * This memory was allocated by the lpfc_read_sparam routine. Release
6786 * it to the mbuf pool.
6788 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6789 kfree(mp);
6790 mboxq->context1 = NULL;
6791 if (unlikely(rc)) {
6792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6793 "0382 READ_SPARAM command failed "
6794 "status %d, mbxStatus x%x\n",
6795 rc, bf_get(lpfc_mqe_status, mqe));
6796 phba->link_state = LPFC_HBA_ERROR;
6797 rc = -EIO;
6798 goto out_free_mbox;
6801 lpfc_update_vport_wwn(vport);
6803 /* Update the fc_host data structures with new wwn. */
6804 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6805 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6807 /* Create all the SLI4 queues */
6808 rc = lpfc_sli4_queue_create(phba);
6809 if (rc) {
6810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6811 "3089 Failed to allocate queues\n");
6812 rc = -ENODEV;
6813 goto out_free_mbox;
6815 /* Set up all the queues to the device */
6816 rc = lpfc_sli4_queue_setup(phba);
6817 if (unlikely(rc)) {
6818 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6819 "0381 Error %d during queue setup.\n ", rc);
6820 goto out_stop_timers;
6822 /* Initialize the driver internal SLI layer lists. */
6823 lpfc_sli4_setup(phba);
6824 lpfc_sli4_queue_init(phba);
6826 /* update host els xri-sgl sizes and mappings */
6827 rc = lpfc_sli4_els_sgl_update(phba);
6828 if (unlikely(rc)) {
6829 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6830 "1400 Failed to update xri-sgl size and "
6831 "mapping: %d\n", rc);
6832 goto out_destroy_queue;
6835 /* register the els sgl pool to the port */
6836 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6837 phba->sli4_hba.els_xri_cnt);
6838 if (unlikely(rc < 0)) {
6839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6840 "0582 Error %d during els sgl post "
6841 "operation\n", rc);
6842 rc = -ENODEV;
6843 goto out_destroy_queue;
6845 phba->sli4_hba.els_xri_cnt = rc;
6847 if (phba->nvmet_support) {
6848 /* update host nvmet xri-sgl sizes and mappings */
6849 rc = lpfc_sli4_nvmet_sgl_update(phba);
6850 if (unlikely(rc)) {
6851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6852 "6308 Failed to update nvmet-sgl size "
6853 "and mapping: %d\n", rc);
6854 goto out_destroy_queue;
6857 /* register the nvmet sgl pool to the port */
6858 rc = lpfc_sli4_repost_sgl_list(
6859 phba,
6860 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6861 phba->sli4_hba.nvmet_xri_cnt);
6862 if (unlikely(rc < 0)) {
6863 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6864 "3117 Error %d during nvmet "
6865 "sgl post\n", rc);
6866 rc = -ENODEV;
6867 goto out_destroy_queue;
6869 phba->sli4_hba.nvmet_xri_cnt = rc;
6870 lpfc_nvmet_create_targetport(phba);
6871 } else {
6872 /* update host scsi xri-sgl sizes and mappings */
6873 rc = lpfc_sli4_scsi_sgl_update(phba);
6874 if (unlikely(rc)) {
6875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6876 "6309 Failed to update scsi-sgl size "
6877 "and mapping: %d\n", rc);
6878 goto out_destroy_queue;
6881 /* update host nvme xri-sgl sizes and mappings */
6882 rc = lpfc_sli4_nvme_sgl_update(phba);
6883 if (unlikely(rc)) {
6884 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6885 "6082 Failed to update nvme-sgl size "
6886 "and mapping: %d\n", rc);
6887 goto out_destroy_queue;
6891 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6893 /* Post initial buffers to all RQs created */
6894 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6895 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6896 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6897 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6898 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6899 rqbp->entry_count = 256;
6900 rqbp->buffer_count = 0;
6902 /* Divide by 4 and round down to multiple of 16 */
6903 rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6904 phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6905 phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6907 lpfc_post_rq_buffer(
6908 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6909 phba->sli4_hba.nvmet_mrq_data[i],
6910 phba->cfg_nvmet_mrq_post);
6914 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6915 /* register the allocated scsi sgl pool to the port */
6916 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6917 if (unlikely(rc)) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "0383 Error %d during scsi sgl post "
6920 "operation\n", rc);
6921 /* Some Scsi buffers were moved to abort scsi list */
6922 /* A pci function reset will repost them */
6923 rc = -ENODEV;
6924 goto out_destroy_queue;
6928 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6929 (phba->nvmet_support == 0)) {
6931 /* register the allocated nvme sgl pool to the port */
6932 rc = lpfc_repost_nvme_sgl_list(phba);
6933 if (unlikely(rc)) {
6934 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6935 "6116 Error %d during nvme sgl post "
6936 "operation\n", rc);
6937 /* Some NVME buffers were moved to abort nvme list */
6938 /* A pci function reset will repost them */
6939 rc = -ENODEV;
6940 goto out_destroy_queue;
6944 /* Post the rpi header region to the device. */
6945 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6946 if (unlikely(rc)) {
6947 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6948 "0393 Error %d during rpi post operation\n",
6949 rc);
6950 rc = -ENODEV;
6951 goto out_destroy_queue;
6953 lpfc_sli4_node_prep(phba);
6955 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6956 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
6958 * The FC Port needs to register FCFI (index 0)
6960 lpfc_reg_fcfi(phba, mboxq);
6961 mboxq->vport = phba->pport;
6962 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6963 if (rc != MBX_SUCCESS)
6964 goto out_unset_queue;
6965 rc = 0;
6966 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6967 &mboxq->u.mqe.un.reg_fcfi);
6968 } else {
6969 /* We are a NVME Target mode with MRQ > 1 */
6971 /* First register the FCFI */
6972 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6973 mboxq->vport = phba->pport;
6974 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6975 if (rc != MBX_SUCCESS)
6976 goto out_unset_queue;
6977 rc = 0;
6978 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6979 &mboxq->u.mqe.un.reg_fcfi_mrq);
6981 /* Next register the MRQs */
6982 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6983 mboxq->vport = phba->pport;
6984 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6985 if (rc != MBX_SUCCESS)
6986 goto out_unset_queue;
6987 rc = 0;
6989 /* Check if the port is configured to be disabled */
6990 lpfc_sli_read_link_ste(phba);
6993 /* Arm the CQs and then EQs on device */
6994 lpfc_sli4_arm_cqeq_intr(phba);
6996 /* Indicate device interrupt mode */
6997 phba->sli4_hba.intr_enable = 1;
6999 /* Allow asynchronous mailbox command to go through */
7000 spin_lock_irq(&phba->hbalock);
7001 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7002 spin_unlock_irq(&phba->hbalock);
7004 /* Post receive buffers to the device */
7005 lpfc_sli4_rb_setup(phba);
7007 /* Reset HBA FCF states after HBA reset */
7008 phba->fcf.fcf_flag = 0;
7009 phba->fcf.current_rec.flag = 0;
7011 /* Start the ELS watchdog timer */
7012 mod_timer(&vport->els_tmofunc,
7013 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7015 /* Start heart beat timer */
7016 mod_timer(&phba->hb_tmofunc,
7017 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7018 phba->hb_outstanding = 0;
7019 phba->last_completion_time = jiffies;
7021 /* Start error attention (ERATT) polling timer */
7022 mod_timer(&phba->eratt_poll,
7023 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7025 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7026 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7027 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7028 if (!rc) {
7029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7030 "2829 This device supports "
7031 "Advanced Error Reporting (AER)\n");
7032 spin_lock_irq(&phba->hbalock);
7033 phba->hba_flag |= HBA_AER_ENABLED;
7034 spin_unlock_irq(&phba->hbalock);
7035 } else {
7036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7037 "2830 This device does not support "
7038 "Advanced Error Reporting (AER)\n");
7039 phba->cfg_aer_support = 0;
7041 rc = 0;
7045 * The port is ready, set the host's link state to LINK_DOWN
7046 * in preparation for link interrupts.
7048 spin_lock_irq(&phba->hbalock);
7049 phba->link_state = LPFC_LINK_DOWN;
7050 spin_unlock_irq(&phba->hbalock);
7051 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7052 (phba->hba_flag & LINK_DISABLED)) {
7053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7054 "3103 Adapter Link is disabled.\n");
7055 lpfc_down_link(phba, mboxq);
7056 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7057 if (rc != MBX_SUCCESS) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7059 "3104 Adapter failed to issue "
7060 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7061 goto out_unset_queue;
7063 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7064 /* don't perform init_link on SLI4 FC port loopback test */
7065 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7066 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7067 if (rc)
7068 goto out_unset_queue;
7071 mempool_free(mboxq, phba->mbox_mem_pool);
7072 return rc;
7073 out_unset_queue:
7074 /* Unset all the queues set up in this routine when error out */
7075 lpfc_sli4_queue_unset(phba);
7076 out_destroy_queue:
7077 lpfc_sli4_queue_destroy(phba);
7078 out_stop_timers:
7079 lpfc_stop_hba_timers(phba);
7080 out_free_mbox:
7081 mempool_free(mboxq, phba->mbox_mem_pool);
7082 return rc;
7086 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7087 * @ptr: context object - pointer to hba structure.
7089 * This is the callback function for mailbox timer. The mailbox
7090 * timer is armed when a new mailbox command is issued and the timer
7091 * is deleted when the mailbox complete. The function is called by
7092 * the kernel timer code when a mailbox does not complete within
7093 * expected time. This function wakes up the worker thread to
7094 * process the mailbox timeout and returns. All the processing is
7095 * done by the worker thread function lpfc_mbox_timeout_handler.
7097 void
7098 lpfc_mbox_timeout(unsigned long ptr)
7100 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
7101 unsigned long iflag;
7102 uint32_t tmo_posted;
7104 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7105 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7106 if (!tmo_posted)
7107 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7108 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7110 if (!tmo_posted)
7111 lpfc_worker_wake_up(phba);
7112 return;
7116 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7117 * are pending
7118 * @phba: Pointer to HBA context object.
7120 * This function checks if any mailbox completions are present on the mailbox
7121 * completion queue.
7123 static bool
7124 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7127 uint32_t idx;
7128 struct lpfc_queue *mcq;
7129 struct lpfc_mcqe *mcqe;
7130 bool pending_completions = false;
7132 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7133 return false;
7135 /* Check for completions on mailbox completion queue */
7137 mcq = phba->sli4_hba.mbx_cq;
7138 idx = mcq->hba_index;
7139 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7140 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7141 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7142 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7143 pending_completions = true;
7144 break;
7146 idx = (idx + 1) % mcq->entry_count;
7147 if (mcq->hba_index == idx)
7148 break;
7150 return pending_completions;
7155 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7156 * that were missed.
7157 * @phba: Pointer to HBA context object.
7159 * For sli4, it is possible to miss an interrupt. As such mbox completions
7160 * maybe missed causing erroneous mailbox timeouts to occur. This function
7161 * checks to see if mbox completions are on the mailbox completion queue
7162 * and will process all the completions associated with the eq for the
7163 * mailbox completion queue.
7165 bool
7166 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7169 uint32_t eqidx;
7170 struct lpfc_queue *fpeq = NULL;
7171 struct lpfc_eqe *eqe;
7172 bool mbox_pending;
7174 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7175 return false;
7177 /* Find the eq associated with the mcq */
7179 if (phba->sli4_hba.hba_eq)
7180 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7181 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7182 phba->sli4_hba.mbx_cq->assoc_qid) {
7183 fpeq = phba->sli4_hba.hba_eq[eqidx];
7184 break;
7186 if (!fpeq)
7187 return false;
7189 /* Turn off interrupts from this EQ */
7191 lpfc_sli4_eq_clr_intr(fpeq);
7193 /* Check to see if a mbox completion is pending */
7195 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7198 * If a mbox completion is pending, process all the events on EQ
7199 * associated with the mbox completion queue (this could include
7200 * mailbox commands, async events, els commands, receive queue data
7201 * and fcp commands)
7204 if (mbox_pending)
7205 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7206 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7207 fpeq->EQ_processed++;
7210 /* Always clear and re-arm the EQ */
7212 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7214 return mbox_pending;
7219 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7220 * @phba: Pointer to HBA context object.
7222 * This function is called from worker thread when a mailbox command times out.
7223 * The caller is not required to hold any locks. This function will reset the
7224 * HBA and recover all the pending commands.
7226 void
7227 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7229 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7230 MAILBOX_t *mb = NULL;
7232 struct lpfc_sli *psli = &phba->sli;
7234 /* If the mailbox completed, process the completion and return */
7235 if (lpfc_sli4_process_missed_mbox_completions(phba))
7236 return;
7238 if (pmbox != NULL)
7239 mb = &pmbox->u.mb;
7240 /* Check the pmbox pointer first. There is a race condition
7241 * between the mbox timeout handler getting executed in the
7242 * worklist and the mailbox actually completing. When this
7243 * race condition occurs, the mbox_active will be NULL.
7245 spin_lock_irq(&phba->hbalock);
7246 if (pmbox == NULL) {
7247 lpfc_printf_log(phba, KERN_WARNING,
7248 LOG_MBOX | LOG_SLI,
7249 "0353 Active Mailbox cleared - mailbox timeout "
7250 "exiting\n");
7251 spin_unlock_irq(&phba->hbalock);
7252 return;
7255 /* Mbox cmd <mbxCommand> timeout */
7256 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7257 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7258 mb->mbxCommand,
7259 phba->pport->port_state,
7260 phba->sli.sli_flag,
7261 phba->sli.mbox_active);
7262 spin_unlock_irq(&phba->hbalock);
7264 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7265 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7266 * it to fail all outstanding SCSI IO.
7268 spin_lock_irq(&phba->pport->work_port_lock);
7269 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7270 spin_unlock_irq(&phba->pport->work_port_lock);
7271 spin_lock_irq(&phba->hbalock);
7272 phba->link_state = LPFC_LINK_UNKNOWN;
7273 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7274 spin_unlock_irq(&phba->hbalock);
7276 lpfc_sli_abort_fcp_rings(phba);
7278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7279 "0345 Resetting board due to mailbox timeout\n");
7281 /* Reset the HBA device */
7282 lpfc_reset_hba(phba);
7286 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7287 * @phba: Pointer to HBA context object.
7288 * @pmbox: Pointer to mailbox object.
7289 * @flag: Flag indicating how the mailbox need to be processed.
7291 * This function is called by discovery code and HBA management code
7292 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7293 * function gets the hbalock to protect the data structures.
7294 * The mailbox command can be submitted in polling mode, in which case
7295 * this function will wait in a polling loop for the completion of the
7296 * mailbox.
7297 * If the mailbox is submitted in no_wait mode (not polling) the
7298 * function will submit the command and returns immediately without waiting
7299 * for the mailbox completion. The no_wait is supported only when HBA
7300 * is in SLI2/SLI3 mode - interrupts are enabled.
7301 * The SLI interface allows only one mailbox pending at a time. If the
7302 * mailbox is issued in polling mode and there is already a mailbox
7303 * pending, then the function will return an error. If the mailbox is issued
7304 * in NO_WAIT mode and there is a mailbox pending already, the function
7305 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7306 * The sli layer owns the mailbox object until the completion of mailbox
7307 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7308 * return codes the caller owns the mailbox command after the return of
7309 * the function.
7311 static int
7312 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7313 uint32_t flag)
7315 MAILBOX_t *mbx;
7316 struct lpfc_sli *psli = &phba->sli;
7317 uint32_t status, evtctr;
7318 uint32_t ha_copy, hc_copy;
7319 int i;
7320 unsigned long timeout;
7321 unsigned long drvr_flag = 0;
7322 uint32_t word0, ldata;
7323 void __iomem *to_slim;
7324 int processing_queue = 0;
7326 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7327 if (!pmbox) {
7328 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7329 /* processing mbox queue from intr_handler */
7330 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7331 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7332 return MBX_SUCCESS;
7334 processing_queue = 1;
7335 pmbox = lpfc_mbox_get(phba);
7336 if (!pmbox) {
7337 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7338 return MBX_SUCCESS;
7342 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7343 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7344 if(!pmbox->vport) {
7345 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7346 lpfc_printf_log(phba, KERN_ERR,
7347 LOG_MBOX | LOG_VPORT,
7348 "1806 Mbox x%x failed. No vport\n",
7349 pmbox->u.mb.mbxCommand);
7350 dump_stack();
7351 goto out_not_finished;
7355 /* If the PCI channel is in offline state, do not post mbox. */
7356 if (unlikely(pci_channel_offline(phba->pcidev))) {
7357 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7358 goto out_not_finished;
7361 /* If HBA has a deferred error attention, fail the iocb. */
7362 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7363 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7364 goto out_not_finished;
7367 psli = &phba->sli;
7369 mbx = &pmbox->u.mb;
7370 status = MBX_SUCCESS;
7372 if (phba->link_state == LPFC_HBA_ERROR) {
7373 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7375 /* Mbox command <mbxCommand> cannot issue */
7376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7377 "(%d):0311 Mailbox command x%x cannot "
7378 "issue Data: x%x x%x\n",
7379 pmbox->vport ? pmbox->vport->vpi : 0,
7380 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7381 goto out_not_finished;
7384 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7385 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7386 !(hc_copy & HC_MBINT_ENA)) {
7387 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7388 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7389 "(%d):2528 Mailbox command x%x cannot "
7390 "issue Data: x%x x%x\n",
7391 pmbox->vport ? pmbox->vport->vpi : 0,
7392 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7393 goto out_not_finished;
7397 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7398 /* Polling for a mbox command when another one is already active
7399 * is not allowed in SLI. Also, the driver must have established
7400 * SLI2 mode to queue and process multiple mbox commands.
7403 if (flag & MBX_POLL) {
7404 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7406 /* Mbox command <mbxCommand> cannot issue */
7407 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7408 "(%d):2529 Mailbox command x%x "
7409 "cannot issue Data: x%x x%x\n",
7410 pmbox->vport ? pmbox->vport->vpi : 0,
7411 pmbox->u.mb.mbxCommand,
7412 psli->sli_flag, flag);
7413 goto out_not_finished;
7416 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7417 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7418 /* Mbox command <mbxCommand> cannot issue */
7419 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7420 "(%d):2530 Mailbox command x%x "
7421 "cannot issue Data: x%x x%x\n",
7422 pmbox->vport ? pmbox->vport->vpi : 0,
7423 pmbox->u.mb.mbxCommand,
7424 psli->sli_flag, flag);
7425 goto out_not_finished;
7428 /* Another mailbox command is still being processed, queue this
7429 * command to be processed later.
7431 lpfc_mbox_put(phba, pmbox);
7433 /* Mbox cmd issue - BUSY */
7434 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7435 "(%d):0308 Mbox cmd issue - BUSY Data: "
7436 "x%x x%x x%x x%x\n",
7437 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7438 mbx->mbxCommand, phba->pport->port_state,
7439 psli->sli_flag, flag);
7441 psli->slistat.mbox_busy++;
7442 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7444 if (pmbox->vport) {
7445 lpfc_debugfs_disc_trc(pmbox->vport,
7446 LPFC_DISC_TRC_MBOX_VPORT,
7447 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
7448 (uint32_t)mbx->mbxCommand,
7449 mbx->un.varWords[0], mbx->un.varWords[1]);
7451 else {
7452 lpfc_debugfs_disc_trc(phba->pport,
7453 LPFC_DISC_TRC_MBOX,
7454 "MBOX Bsy: cmd:x%x mb:x%x x%x",
7455 (uint32_t)mbx->mbxCommand,
7456 mbx->un.varWords[0], mbx->un.varWords[1]);
7459 return MBX_BUSY;
7462 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7464 /* If we are not polling, we MUST be in SLI2 mode */
7465 if (flag != MBX_POLL) {
7466 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7467 (mbx->mbxCommand != MBX_KILL_BOARD)) {
7468 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7469 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7470 /* Mbox command <mbxCommand> cannot issue */
7471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7472 "(%d):2531 Mailbox command x%x "
7473 "cannot issue Data: x%x x%x\n",
7474 pmbox->vport ? pmbox->vport->vpi : 0,
7475 pmbox->u.mb.mbxCommand,
7476 psli->sli_flag, flag);
7477 goto out_not_finished;
7479 /* timeout active mbox command */
7480 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7481 1000);
7482 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7485 /* Mailbox cmd <cmd> issue */
7486 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7487 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7488 "x%x\n",
7489 pmbox->vport ? pmbox->vport->vpi : 0,
7490 mbx->mbxCommand, phba->pport->port_state,
7491 psli->sli_flag, flag);
7493 if (mbx->mbxCommand != MBX_HEARTBEAT) {
7494 if (pmbox->vport) {
7495 lpfc_debugfs_disc_trc(pmbox->vport,
7496 LPFC_DISC_TRC_MBOX_VPORT,
7497 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7498 (uint32_t)mbx->mbxCommand,
7499 mbx->un.varWords[0], mbx->un.varWords[1]);
7501 else {
7502 lpfc_debugfs_disc_trc(phba->pport,
7503 LPFC_DISC_TRC_MBOX,
7504 "MBOX Send: cmd:x%x mb:x%x x%x",
7505 (uint32_t)mbx->mbxCommand,
7506 mbx->un.varWords[0], mbx->un.varWords[1]);
7510 psli->slistat.mbox_cmd++;
7511 evtctr = psli->slistat.mbox_event;
7513 /* next set own bit for the adapter and copy over command word */
7514 mbx->mbxOwner = OWN_CHIP;
7516 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7517 /* Populate mbox extension offset word. */
7518 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7519 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7520 = (uint8_t *)phba->mbox_ext
7521 - (uint8_t *)phba->mbox;
7524 /* Copy the mailbox extension data */
7525 if (pmbox->in_ext_byte_len && pmbox->context2) {
7526 lpfc_sli_pcimem_bcopy(pmbox->context2,
7527 (uint8_t *)phba->mbox_ext,
7528 pmbox->in_ext_byte_len);
7530 /* Copy command data to host SLIM area */
7531 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7532 } else {
7533 /* Populate mbox extension offset word. */
7534 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7535 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7536 = MAILBOX_HBA_EXT_OFFSET;
7538 /* Copy the mailbox extension data */
7539 if (pmbox->in_ext_byte_len && pmbox->context2)
7540 lpfc_memcpy_to_slim(phba->MBslimaddr +
7541 MAILBOX_HBA_EXT_OFFSET,
7542 pmbox->context2, pmbox->in_ext_byte_len);
7544 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7545 /* copy command data into host mbox for cmpl */
7546 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7547 MAILBOX_CMD_SIZE);
7549 /* First copy mbox command data to HBA SLIM, skip past first
7550 word */
7551 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7552 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7553 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7555 /* Next copy over first word, with mbxOwner set */
7556 ldata = *((uint32_t *)mbx);
7557 to_slim = phba->MBslimaddr;
7558 writel(ldata, to_slim);
7559 readl(to_slim); /* flush */
7561 if (mbx->mbxCommand == MBX_CONFIG_PORT)
7562 /* switch over to host mailbox */
7563 psli->sli_flag |= LPFC_SLI_ACTIVE;
7566 wmb();
7568 switch (flag) {
7569 case MBX_NOWAIT:
7570 /* Set up reference to mailbox command */
7571 psli->mbox_active = pmbox;
7572 /* Interrupt board to do it */
7573 writel(CA_MBATT, phba->CAregaddr);
7574 readl(phba->CAregaddr); /* flush */
7575 /* Don't wait for it to finish, just return */
7576 break;
7578 case MBX_POLL:
7579 /* Set up null reference to mailbox command */
7580 psli->mbox_active = NULL;
7581 /* Interrupt board to do it */
7582 writel(CA_MBATT, phba->CAregaddr);
7583 readl(phba->CAregaddr); /* flush */
7585 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7586 /* First read mbox status word */
7587 word0 = *((uint32_t *)phba->mbox);
7588 word0 = le32_to_cpu(word0);
7589 } else {
7590 /* First read mbox status word */
7591 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7592 spin_unlock_irqrestore(&phba->hbalock,
7593 drvr_flag);
7594 goto out_not_finished;
7598 /* Read the HBA Host Attention Register */
7599 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7600 spin_unlock_irqrestore(&phba->hbalock,
7601 drvr_flag);
7602 goto out_not_finished;
7604 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7605 1000) + jiffies;
7606 i = 0;
7607 /* Wait for command to complete */
7608 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7609 (!(ha_copy & HA_MBATT) &&
7610 (phba->link_state > LPFC_WARM_START))) {
7611 if (time_after(jiffies, timeout)) {
7612 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7613 spin_unlock_irqrestore(&phba->hbalock,
7614 drvr_flag);
7615 goto out_not_finished;
7618 /* Check if we took a mbox interrupt while we were
7619 polling */
7620 if (((word0 & OWN_CHIP) != OWN_CHIP)
7621 && (evtctr != psli->slistat.mbox_event))
7622 break;
7624 if (i++ > 10) {
7625 spin_unlock_irqrestore(&phba->hbalock,
7626 drvr_flag);
7627 msleep(1);
7628 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7631 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7632 /* First copy command data */
7633 word0 = *((uint32_t *)phba->mbox);
7634 word0 = le32_to_cpu(word0);
7635 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7636 MAILBOX_t *slimmb;
7637 uint32_t slimword0;
7638 /* Check real SLIM for any errors */
7639 slimword0 = readl(phba->MBslimaddr);
7640 slimmb = (MAILBOX_t *) & slimword0;
7641 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7642 && slimmb->mbxStatus) {
7643 psli->sli_flag &=
7644 ~LPFC_SLI_ACTIVE;
7645 word0 = slimword0;
7648 } else {
7649 /* First copy command data */
7650 word0 = readl(phba->MBslimaddr);
7652 /* Read the HBA Host Attention Register */
7653 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7654 spin_unlock_irqrestore(&phba->hbalock,
7655 drvr_flag);
7656 goto out_not_finished;
7660 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7661 /* copy results back to user */
7662 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7663 MAILBOX_CMD_SIZE);
7664 /* Copy the mailbox extension data */
7665 if (pmbox->out_ext_byte_len && pmbox->context2) {
7666 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7667 pmbox->context2,
7668 pmbox->out_ext_byte_len);
7670 } else {
7671 /* First copy command data */
7672 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7673 MAILBOX_CMD_SIZE);
7674 /* Copy the mailbox extension data */
7675 if (pmbox->out_ext_byte_len && pmbox->context2) {
7676 lpfc_memcpy_from_slim(pmbox->context2,
7677 phba->MBslimaddr +
7678 MAILBOX_HBA_EXT_OFFSET,
7679 pmbox->out_ext_byte_len);
7683 writel(HA_MBATT, phba->HAregaddr);
7684 readl(phba->HAregaddr); /* flush */
7686 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7687 status = mbx->mbxStatus;
7690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7691 return status;
7693 out_not_finished:
7694 if (processing_queue) {
7695 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7696 lpfc_mbox_cmpl_put(phba, pmbox);
7698 return MBX_NOT_FINISHED;
7702 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7703 * @phba: Pointer to HBA context object.
7705 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7706 * the driver internal pending mailbox queue. It will then try to wait out the
7707 * possible outstanding mailbox command before return.
7709 * Returns:
7710 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7711 * the outstanding mailbox command timed out.
7713 static int
7714 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7716 struct lpfc_sli *psli = &phba->sli;
7717 int rc = 0;
7718 unsigned long timeout = 0;
7720 /* Mark the asynchronous mailbox command posting as blocked */
7721 spin_lock_irq(&phba->hbalock);
7722 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7723 /* Determine how long we might wait for the active mailbox
7724 * command to be gracefully completed by firmware.
7726 if (phba->sli.mbox_active)
7727 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7728 phba->sli.mbox_active) *
7729 1000) + jiffies;
7730 spin_unlock_irq(&phba->hbalock);
7732 /* Make sure the mailbox is really active */
7733 if (timeout)
7734 lpfc_sli4_process_missed_mbox_completions(phba);
7736 /* Wait for the outstnading mailbox command to complete */
7737 while (phba->sli.mbox_active) {
7738 /* Check active mailbox complete status every 2ms */
7739 msleep(2);
7740 if (time_after(jiffies, timeout)) {
7741 /* Timeout, marked the outstanding cmd not complete */
7742 rc = 1;
7743 break;
7747 /* Can not cleanly block async mailbox command, fails it */
7748 if (rc) {
7749 spin_lock_irq(&phba->hbalock);
7750 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7751 spin_unlock_irq(&phba->hbalock);
7753 return rc;
7757 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7758 * @phba: Pointer to HBA context object.
7760 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7761 * commands from the driver internal pending mailbox queue. It makes sure
7762 * that there is no outstanding mailbox command before resuming posting
7763 * asynchronous mailbox commands. If, for any reason, there is outstanding
7764 * mailbox command, it will try to wait it out before resuming asynchronous
7765 * mailbox command posting.
7767 static void
7768 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7770 struct lpfc_sli *psli = &phba->sli;
7772 spin_lock_irq(&phba->hbalock);
7773 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7774 /* Asynchronous mailbox posting is not blocked, do nothing */
7775 spin_unlock_irq(&phba->hbalock);
7776 return;
7779 /* Outstanding synchronous mailbox command is guaranteed to be done,
7780 * successful or timeout, after timing-out the outstanding mailbox
7781 * command shall always be removed, so just unblock posting async
7782 * mailbox command and resume
7784 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7785 spin_unlock_irq(&phba->hbalock);
7787 /* wake up worker thread to post asynchronlous mailbox command */
7788 lpfc_worker_wake_up(phba);
7792 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7793 * @phba: Pointer to HBA context object.
7794 * @mboxq: Pointer to mailbox object.
7796 * The function waits for the bootstrap mailbox register ready bit from
7797 * port for twice the regular mailbox command timeout value.
7799 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7800 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7802 static int
7803 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7805 uint32_t db_ready;
7806 unsigned long timeout;
7807 struct lpfc_register bmbx_reg;
7809 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7810 * 1000) + jiffies;
7812 do {
7813 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7814 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7815 if (!db_ready)
7816 msleep(2);
7818 if (time_after(jiffies, timeout))
7819 return MBXERR_ERROR;
7820 } while (!db_ready);
7822 return 0;
7826 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7827 * @phba: Pointer to HBA context object.
7828 * @mboxq: Pointer to mailbox object.
7830 * The function posts a mailbox to the port. The mailbox is expected
7831 * to be comletely filled in and ready for the port to operate on it.
7832 * This routine executes a synchronous completion operation on the
7833 * mailbox by polling for its completion.
7835 * The caller must not be holding any locks when calling this routine.
7837 * Returns:
7838 * MBX_SUCCESS - mailbox posted successfully
7839 * Any of the MBX error values.
7841 static int
7842 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7844 int rc = MBX_SUCCESS;
7845 unsigned long iflag;
7846 uint32_t mcqe_status;
7847 uint32_t mbx_cmnd;
7848 struct lpfc_sli *psli = &phba->sli;
7849 struct lpfc_mqe *mb = &mboxq->u.mqe;
7850 struct lpfc_bmbx_create *mbox_rgn;
7851 struct dma_address *dma_address;
7854 * Only one mailbox can be active to the bootstrap mailbox region
7855 * at a time and there is no queueing provided.
7857 spin_lock_irqsave(&phba->hbalock, iflag);
7858 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7859 spin_unlock_irqrestore(&phba->hbalock, iflag);
7860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7861 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7862 "cannot issue Data: x%x x%x\n",
7863 mboxq->vport ? mboxq->vport->vpi : 0,
7864 mboxq->u.mb.mbxCommand,
7865 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7866 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7867 psli->sli_flag, MBX_POLL);
7868 return MBXERR_ERROR;
7870 /* The server grabs the token and owns it until release */
7871 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7872 phba->sli.mbox_active = mboxq;
7873 spin_unlock_irqrestore(&phba->hbalock, iflag);
7875 /* wait for bootstrap mbox register for readyness */
7876 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7877 if (rc)
7878 goto exit;
7881 * Initialize the bootstrap memory region to avoid stale data areas
7882 * in the mailbox post. Then copy the caller's mailbox contents to
7883 * the bmbx mailbox region.
7885 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7886 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7887 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7888 sizeof(struct lpfc_mqe));
7890 /* Post the high mailbox dma address to the port and wait for ready. */
7891 dma_address = &phba->sli4_hba.bmbx.dma_address;
7892 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7894 /* wait for bootstrap mbox register for hi-address write done */
7895 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7896 if (rc)
7897 goto exit;
7899 /* Post the low mailbox dma address to the port. */
7900 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7902 /* wait for bootstrap mbox register for low address write done */
7903 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7904 if (rc)
7905 goto exit;
7908 * Read the CQ to ensure the mailbox has completed.
7909 * If so, update the mailbox status so that the upper layers
7910 * can complete the request normally.
7912 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7913 sizeof(struct lpfc_mqe));
7914 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7915 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7916 sizeof(struct lpfc_mcqe));
7917 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7919 * When the CQE status indicates a failure and the mailbox status
7920 * indicates success then copy the CQE status into the mailbox status
7921 * (and prefix it with x4000).
7923 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7924 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7925 bf_set(lpfc_mqe_status, mb,
7926 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7927 rc = MBXERR_ERROR;
7928 } else
7929 lpfc_sli4_swap_str(phba, mboxq);
7931 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7932 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7933 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7934 " x%x x%x CQ: x%x x%x x%x x%x\n",
7935 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7936 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7937 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7938 bf_get(lpfc_mqe_status, mb),
7939 mb->un.mb_words[0], mb->un.mb_words[1],
7940 mb->un.mb_words[2], mb->un.mb_words[3],
7941 mb->un.mb_words[4], mb->un.mb_words[5],
7942 mb->un.mb_words[6], mb->un.mb_words[7],
7943 mb->un.mb_words[8], mb->un.mb_words[9],
7944 mb->un.mb_words[10], mb->un.mb_words[11],
7945 mb->un.mb_words[12], mboxq->mcqe.word0,
7946 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7947 mboxq->mcqe.trailer);
7948 exit:
7949 /* We are holding the token, no needed for lock when release */
7950 spin_lock_irqsave(&phba->hbalock, iflag);
7951 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7952 phba->sli.mbox_active = NULL;
7953 spin_unlock_irqrestore(&phba->hbalock, iflag);
7954 return rc;
7958 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7959 * @phba: Pointer to HBA context object.
7960 * @pmbox: Pointer to mailbox object.
7961 * @flag: Flag indicating how the mailbox need to be processed.
7963 * This function is called by discovery code and HBA management code to submit
7964 * a mailbox command to firmware with SLI-4 interface spec.
7966 * Return codes the caller owns the mailbox command after the return of the
7967 * function.
7969 static int
7970 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7971 uint32_t flag)
7973 struct lpfc_sli *psli = &phba->sli;
7974 unsigned long iflags;
7975 int rc;
7977 /* dump from issue mailbox command if setup */
7978 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7980 rc = lpfc_mbox_dev_check(phba);
7981 if (unlikely(rc)) {
7982 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7983 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7984 "cannot issue Data: x%x x%x\n",
7985 mboxq->vport ? mboxq->vport->vpi : 0,
7986 mboxq->u.mb.mbxCommand,
7987 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7988 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7989 psli->sli_flag, flag);
7990 goto out_not_finished;
7993 /* Detect polling mode and jump to a handler */
7994 if (!phba->sli4_hba.intr_enable) {
7995 if (flag == MBX_POLL)
7996 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7997 else
7998 rc = -EIO;
7999 if (rc != MBX_SUCCESS)
8000 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8001 "(%d):2541 Mailbox command x%x "
8002 "(x%x/x%x) failure: "
8003 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8004 "Data: x%x x%x\n,",
8005 mboxq->vport ? mboxq->vport->vpi : 0,
8006 mboxq->u.mb.mbxCommand,
8007 lpfc_sli_config_mbox_subsys_get(phba,
8008 mboxq),
8009 lpfc_sli_config_mbox_opcode_get(phba,
8010 mboxq),
8011 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8012 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8013 bf_get(lpfc_mcqe_ext_status,
8014 &mboxq->mcqe),
8015 psli->sli_flag, flag);
8016 return rc;
8017 } else if (flag == MBX_POLL) {
8018 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8019 "(%d):2542 Try to issue mailbox command "
8020 "x%x (x%x/x%x) synchronously ahead of async"
8021 "mailbox command queue: x%x x%x\n",
8022 mboxq->vport ? mboxq->vport->vpi : 0,
8023 mboxq->u.mb.mbxCommand,
8024 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8025 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8026 psli->sli_flag, flag);
8027 /* Try to block the asynchronous mailbox posting */
8028 rc = lpfc_sli4_async_mbox_block(phba);
8029 if (!rc) {
8030 /* Successfully blocked, now issue sync mbox cmd */
8031 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8032 if (rc != MBX_SUCCESS)
8033 lpfc_printf_log(phba, KERN_WARNING,
8034 LOG_MBOX | LOG_SLI,
8035 "(%d):2597 Sync Mailbox command "
8036 "x%x (x%x/x%x) failure: "
8037 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8038 "Data: x%x x%x\n,",
8039 mboxq->vport ? mboxq->vport->vpi : 0,
8040 mboxq->u.mb.mbxCommand,
8041 lpfc_sli_config_mbox_subsys_get(phba,
8042 mboxq),
8043 lpfc_sli_config_mbox_opcode_get(phba,
8044 mboxq),
8045 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8046 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8047 bf_get(lpfc_mcqe_ext_status,
8048 &mboxq->mcqe),
8049 psli->sli_flag, flag);
8050 /* Unblock the async mailbox posting afterward */
8051 lpfc_sli4_async_mbox_unblock(phba);
8053 return rc;
8056 /* Now, interrupt mode asynchrous mailbox command */
8057 rc = lpfc_mbox_cmd_check(phba, mboxq);
8058 if (rc) {
8059 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8060 "(%d):2543 Mailbox command x%x (x%x/x%x) "
8061 "cannot issue Data: x%x x%x\n",
8062 mboxq->vport ? mboxq->vport->vpi : 0,
8063 mboxq->u.mb.mbxCommand,
8064 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8065 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8066 psli->sli_flag, flag);
8067 goto out_not_finished;
8070 /* Put the mailbox command to the driver internal FIFO */
8071 psli->slistat.mbox_busy++;
8072 spin_lock_irqsave(&phba->hbalock, iflags);
8073 lpfc_mbox_put(phba, mboxq);
8074 spin_unlock_irqrestore(&phba->hbalock, iflags);
8075 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8076 "(%d):0354 Mbox cmd issue - Enqueue Data: "
8077 "x%x (x%x/x%x) x%x x%x x%x\n",
8078 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8079 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8080 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8081 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8082 phba->pport->port_state,
8083 psli->sli_flag, MBX_NOWAIT);
8084 /* Wake up worker thread to transport mailbox command from head */
8085 lpfc_worker_wake_up(phba);
8087 return MBX_BUSY;
8089 out_not_finished:
8090 return MBX_NOT_FINISHED;
8094 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8095 * @phba: Pointer to HBA context object.
8097 * This function is called by worker thread to send a mailbox command to
8098 * SLI4 HBA firmware.
8102 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8104 struct lpfc_sli *psli = &phba->sli;
8105 LPFC_MBOXQ_t *mboxq;
8106 int rc = MBX_SUCCESS;
8107 unsigned long iflags;
8108 struct lpfc_mqe *mqe;
8109 uint32_t mbx_cmnd;
8111 /* Check interrupt mode before post async mailbox command */
8112 if (unlikely(!phba->sli4_hba.intr_enable))
8113 return MBX_NOT_FINISHED;
8115 /* Check for mailbox command service token */
8116 spin_lock_irqsave(&phba->hbalock, iflags);
8117 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8118 spin_unlock_irqrestore(&phba->hbalock, iflags);
8119 return MBX_NOT_FINISHED;
8121 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8122 spin_unlock_irqrestore(&phba->hbalock, iflags);
8123 return MBX_NOT_FINISHED;
8125 if (unlikely(phba->sli.mbox_active)) {
8126 spin_unlock_irqrestore(&phba->hbalock, iflags);
8127 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8128 "0384 There is pending active mailbox cmd\n");
8129 return MBX_NOT_FINISHED;
8131 /* Take the mailbox command service token */
8132 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8134 /* Get the next mailbox command from head of queue */
8135 mboxq = lpfc_mbox_get(phba);
8137 /* If no more mailbox command waiting for post, we're done */
8138 if (!mboxq) {
8139 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8140 spin_unlock_irqrestore(&phba->hbalock, iflags);
8141 return MBX_SUCCESS;
8143 phba->sli.mbox_active = mboxq;
8144 spin_unlock_irqrestore(&phba->hbalock, iflags);
8146 /* Check device readiness for posting mailbox command */
8147 rc = lpfc_mbox_dev_check(phba);
8148 if (unlikely(rc))
8149 /* Driver clean routine will clean up pending mailbox */
8150 goto out_not_finished;
8152 /* Prepare the mbox command to be posted */
8153 mqe = &mboxq->u.mqe;
8154 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8156 /* Start timer for the mbox_tmo and log some mailbox post messages */
8157 mod_timer(&psli->mbox_tmo, (jiffies +
8158 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8160 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8161 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8162 "x%x x%x\n",
8163 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8164 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8165 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8166 phba->pport->port_state, psli->sli_flag);
8168 if (mbx_cmnd != MBX_HEARTBEAT) {
8169 if (mboxq->vport) {
8170 lpfc_debugfs_disc_trc(mboxq->vport,
8171 LPFC_DISC_TRC_MBOX_VPORT,
8172 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8173 mbx_cmnd, mqe->un.mb_words[0],
8174 mqe->un.mb_words[1]);
8175 } else {
8176 lpfc_debugfs_disc_trc(phba->pport,
8177 LPFC_DISC_TRC_MBOX,
8178 "MBOX Send: cmd:x%x mb:x%x x%x",
8179 mbx_cmnd, mqe->un.mb_words[0],
8180 mqe->un.mb_words[1]);
8183 psli->slistat.mbox_cmd++;
8185 /* Post the mailbox command to the port */
8186 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8187 if (rc != MBX_SUCCESS) {
8188 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8189 "(%d):2533 Mailbox command x%x (x%x/x%x) "
8190 "cannot issue Data: x%x x%x\n",
8191 mboxq->vport ? mboxq->vport->vpi : 0,
8192 mboxq->u.mb.mbxCommand,
8193 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8194 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8195 psli->sli_flag, MBX_NOWAIT);
8196 goto out_not_finished;
8199 return rc;
8201 out_not_finished:
8202 spin_lock_irqsave(&phba->hbalock, iflags);
8203 if (phba->sli.mbox_active) {
8204 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8205 __lpfc_mbox_cmpl_put(phba, mboxq);
8206 /* Release the token */
8207 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8208 phba->sli.mbox_active = NULL;
8210 spin_unlock_irqrestore(&phba->hbalock, iflags);
8212 return MBX_NOT_FINISHED;
8216 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8217 * @phba: Pointer to HBA context object.
8218 * @pmbox: Pointer to mailbox object.
8219 * @flag: Flag indicating how the mailbox need to be processed.
8221 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8222 * the API jump table function pointer from the lpfc_hba struct.
8224 * Return codes the caller owns the mailbox command after the return of the
8225 * function.
8228 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8230 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8234 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8235 * @phba: The hba struct for which this call is being executed.
8236 * @dev_grp: The HBA PCI-Device group number.
8238 * This routine sets up the mbox interface API function jump table in @phba
8239 * struct.
8240 * Returns: 0 - success, -ENODEV - failure.
8243 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8246 switch (dev_grp) {
8247 case LPFC_PCI_DEV_LP:
8248 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8249 phba->lpfc_sli_handle_slow_ring_event =
8250 lpfc_sli_handle_slow_ring_event_s3;
8251 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8252 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8253 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8254 break;
8255 case LPFC_PCI_DEV_OC:
8256 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8257 phba->lpfc_sli_handle_slow_ring_event =
8258 lpfc_sli_handle_slow_ring_event_s4;
8259 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8260 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8261 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8262 break;
8263 default:
8264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8265 "1420 Invalid HBA PCI-device group: 0x%x\n",
8266 dev_grp);
8267 return -ENODEV;
8268 break;
8270 return 0;
8274 * __lpfc_sli_ringtx_put - Add an iocb to the txq
8275 * @phba: Pointer to HBA context object.
8276 * @pring: Pointer to driver SLI ring object.
8277 * @piocb: Pointer to address of newly added command iocb.
8279 * This function is called with hbalock held to add a command
8280 * iocb to the txq when SLI layer cannot submit the command iocb
8281 * to the ring.
8283 void
8284 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8285 struct lpfc_iocbq *piocb)
8287 lockdep_assert_held(&phba->hbalock);
8288 /* Insert the caller's iocb in the txq tail for later processing. */
8289 list_add_tail(&piocb->list, &pring->txq);
8293 * lpfc_sli_next_iocb - Get the next iocb in the txq
8294 * @phba: Pointer to HBA context object.
8295 * @pring: Pointer to driver SLI ring object.
8296 * @piocb: Pointer to address of newly added command iocb.
8298 * This function is called with hbalock held before a new
8299 * iocb is submitted to the firmware. This function checks
8300 * txq to flush the iocbs in txq to Firmware before
8301 * submitting new iocbs to the Firmware.
8302 * If there are iocbs in the txq which need to be submitted
8303 * to firmware, lpfc_sli_next_iocb returns the first element
8304 * of the txq after dequeuing it from txq.
8305 * If there is no iocb in the txq then the function will return
8306 * *piocb and *piocb is set to NULL. Caller needs to check
8307 * *piocb to find if there are more commands in the txq.
8309 static struct lpfc_iocbq *
8310 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8311 struct lpfc_iocbq **piocb)
8313 struct lpfc_iocbq * nextiocb;
8315 lockdep_assert_held(&phba->hbalock);
8317 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8318 if (!nextiocb) {
8319 nextiocb = *piocb;
8320 *piocb = NULL;
8323 return nextiocb;
8327 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8328 * @phba: Pointer to HBA context object.
8329 * @ring_number: SLI ring number to issue iocb on.
8330 * @piocb: Pointer to command iocb.
8331 * @flag: Flag indicating if this command can be put into txq.
8333 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8334 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8335 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8336 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8337 * this function allows only iocbs for posting buffers. This function finds
8338 * next available slot in the command ring and posts the command to the
8339 * available slot and writes the port attention register to request HBA start
8340 * processing new iocb. If there is no slot available in the ring and
8341 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8342 * the function returns IOCB_BUSY.
8344 * This function is called with hbalock held. The function will return success
8345 * after it successfully submit the iocb to firmware or after adding to the
8346 * txq.
8348 static int
8349 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8350 struct lpfc_iocbq *piocb, uint32_t flag)
8352 struct lpfc_iocbq *nextiocb;
8353 IOCB_t *iocb;
8354 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8356 lockdep_assert_held(&phba->hbalock);
8358 if (piocb->iocb_cmpl && (!piocb->vport) &&
8359 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8360 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8361 lpfc_printf_log(phba, KERN_ERR,
8362 LOG_SLI | LOG_VPORT,
8363 "1807 IOCB x%x failed. No vport\n",
8364 piocb->iocb.ulpCommand);
8365 dump_stack();
8366 return IOCB_ERROR;
8370 /* If the PCI channel is in offline state, do not post iocbs. */
8371 if (unlikely(pci_channel_offline(phba->pcidev)))
8372 return IOCB_ERROR;
8374 /* If HBA has a deferred error attention, fail the iocb. */
8375 if (unlikely(phba->hba_flag & DEFER_ERATT))
8376 return IOCB_ERROR;
8379 * We should never get an IOCB if we are in a < LINK_DOWN state
8381 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8382 return IOCB_ERROR;
8385 * Check to see if we are blocking IOCB processing because of a
8386 * outstanding event.
8388 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8389 goto iocb_busy;
8391 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8393 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8394 * can be issued if the link is not up.
8396 switch (piocb->iocb.ulpCommand) {
8397 case CMD_GEN_REQUEST64_CR:
8398 case CMD_GEN_REQUEST64_CX:
8399 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8400 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8401 FC_RCTL_DD_UNSOL_CMD) ||
8402 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8403 MENLO_TRANSPORT_TYPE))
8405 goto iocb_busy;
8406 break;
8407 case CMD_QUE_RING_BUF_CN:
8408 case CMD_QUE_RING_BUF64_CN:
8410 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8411 * completion, iocb_cmpl MUST be 0.
8413 if (piocb->iocb_cmpl)
8414 piocb->iocb_cmpl = NULL;
8415 /*FALLTHROUGH*/
8416 case CMD_CREATE_XRI_CR:
8417 case CMD_CLOSE_XRI_CN:
8418 case CMD_CLOSE_XRI_CX:
8419 break;
8420 default:
8421 goto iocb_busy;
8425 * For FCP commands, we must be in a state where we can process link
8426 * attention events.
8428 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
8429 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8430 goto iocb_busy;
8433 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8434 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8435 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8437 if (iocb)
8438 lpfc_sli_update_ring(phba, pring);
8439 else
8440 lpfc_sli_update_full_ring(phba, pring);
8442 if (!piocb)
8443 return IOCB_SUCCESS;
8445 goto out_busy;
8447 iocb_busy:
8448 pring->stats.iocb_cmd_delay++;
8450 out_busy:
8452 if (!(flag & SLI_IOCB_RET_IOCB)) {
8453 __lpfc_sli_ringtx_put(phba, pring, piocb);
8454 return IOCB_SUCCESS;
8457 return IOCB_BUSY;
8461 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8462 * @phba: Pointer to HBA context object.
8463 * @piocb: Pointer to command iocb.
8464 * @sglq: Pointer to the scatter gather queue object.
8466 * This routine converts the bpl or bde that is in the IOCB
8467 * to a sgl list for the sli4 hardware. The physical address
8468 * of the bpl/bde is converted back to a virtual address.
8469 * If the IOCB contains a BPL then the list of BDE's is
8470 * converted to sli4_sge's. If the IOCB contains a single
8471 * BDE then it is converted to a single sli_sge.
8472 * The IOCB is still in cpu endianess so the contents of
8473 * the bpl can be used without byte swapping.
8475 * Returns valid XRI = Success, NO_XRI = Failure.
8477 static uint16_t
8478 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8479 struct lpfc_sglq *sglq)
8481 uint16_t xritag = NO_XRI;
8482 struct ulp_bde64 *bpl = NULL;
8483 struct ulp_bde64 bde;
8484 struct sli4_sge *sgl = NULL;
8485 struct lpfc_dmabuf *dmabuf;
8486 IOCB_t *icmd;
8487 int numBdes = 0;
8488 int i = 0;
8489 uint32_t offset = 0; /* accumulated offset in the sg request list */
8490 int inbound = 0; /* number of sg reply entries inbound from firmware */
8492 if (!piocbq || !sglq)
8493 return xritag;
8495 sgl = (struct sli4_sge *)sglq->sgl;
8496 icmd = &piocbq->iocb;
8497 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8498 return sglq->sli4_xritag;
8499 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8500 numBdes = icmd->un.genreq64.bdl.bdeSize /
8501 sizeof(struct ulp_bde64);
8502 /* The addrHigh and addrLow fields within the IOCB
8503 * have not been byteswapped yet so there is no
8504 * need to swap them back.
8506 if (piocbq->context3)
8507 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8508 else
8509 return xritag;
8511 bpl = (struct ulp_bde64 *)dmabuf->virt;
8512 if (!bpl)
8513 return xritag;
8515 for (i = 0; i < numBdes; i++) {
8516 /* Should already be byte swapped. */
8517 sgl->addr_hi = bpl->addrHigh;
8518 sgl->addr_lo = bpl->addrLow;
8520 sgl->word2 = le32_to_cpu(sgl->word2);
8521 if ((i+1) == numBdes)
8522 bf_set(lpfc_sli4_sge_last, sgl, 1);
8523 else
8524 bf_set(lpfc_sli4_sge_last, sgl, 0);
8525 /* swap the size field back to the cpu so we
8526 * can assign it to the sgl.
8528 bde.tus.w = le32_to_cpu(bpl->tus.w);
8529 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8530 /* The offsets in the sgl need to be accumulated
8531 * separately for the request and reply lists.
8532 * The request is always first, the reply follows.
8534 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8535 /* add up the reply sg entries */
8536 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8537 inbound++;
8538 /* first inbound? reset the offset */
8539 if (inbound == 1)
8540 offset = 0;
8541 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8542 bf_set(lpfc_sli4_sge_type, sgl,
8543 LPFC_SGE_TYPE_DATA);
8544 offset += bde.tus.f.bdeSize;
8546 sgl->word2 = cpu_to_le32(sgl->word2);
8547 bpl++;
8548 sgl++;
8550 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8551 /* The addrHigh and addrLow fields of the BDE have not
8552 * been byteswapped yet so they need to be swapped
8553 * before putting them in the sgl.
8555 sgl->addr_hi =
8556 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8557 sgl->addr_lo =
8558 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8559 sgl->word2 = le32_to_cpu(sgl->word2);
8560 bf_set(lpfc_sli4_sge_last, sgl, 1);
8561 sgl->word2 = cpu_to_le32(sgl->word2);
8562 sgl->sge_len =
8563 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8565 return sglq->sli4_xritag;
8569 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8570 * @phba: Pointer to HBA context object.
8571 * @piocb: Pointer to command iocb.
8572 * @wqe: Pointer to the work queue entry.
8574 * This routine converts the iocb command to its Work Queue Entry
8575 * equivalent. The wqe pointer should not have any fields set when
8576 * this routine is called because it will memcpy over them.
8577 * This routine does not set the CQ_ID or the WQEC bits in the
8578 * wqe.
8580 * Returns: 0 = Success, IOCB_ERROR = Failure.
8582 static int
8583 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8584 union lpfc_wqe *wqe)
8586 uint32_t xmit_len = 0, total_len = 0;
8587 uint8_t ct = 0;
8588 uint32_t fip;
8589 uint32_t abort_tag;
8590 uint8_t command_type = ELS_COMMAND_NON_FIP;
8591 uint8_t cmnd;
8592 uint16_t xritag;
8593 uint16_t abrt_iotag;
8594 struct lpfc_iocbq *abrtiocbq;
8595 struct ulp_bde64 *bpl = NULL;
8596 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8597 int numBdes, i;
8598 struct ulp_bde64 bde;
8599 struct lpfc_nodelist *ndlp;
8600 uint32_t *pcmd;
8601 uint32_t if_type;
8603 fip = phba->hba_flag & HBA_FIP_SUPPORT;
8604 /* The fcp commands will set command type */
8605 if (iocbq->iocb_flag & LPFC_IO_FCP)
8606 command_type = FCP_COMMAND;
8607 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8608 command_type = ELS_COMMAND_FIP;
8609 else
8610 command_type = ELS_COMMAND_NON_FIP;
8612 if (phba->fcp_embed_io)
8613 memset(wqe, 0, sizeof(union lpfc_wqe128));
8614 /* Some of the fields are in the right position already */
8615 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8616 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8617 wqe->generic.wqe_com.word10 = 0;
8619 abort_tag = (uint32_t) iocbq->iotag;
8620 xritag = iocbq->sli4_xritag;
8621 /* words0-2 bpl convert bde */
8622 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8623 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8624 sizeof(struct ulp_bde64);
8625 bpl = (struct ulp_bde64 *)
8626 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8627 if (!bpl)
8628 return IOCB_ERROR;
8630 /* Should already be byte swapped. */
8631 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8632 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8633 /* swap the size field back to the cpu so we
8634 * can assign it to the sgl.
8636 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
8637 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8638 total_len = 0;
8639 for (i = 0; i < numBdes; i++) {
8640 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8641 total_len += bde.tus.f.bdeSize;
8643 } else
8644 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8646 iocbq->iocb.ulpIoTag = iocbq->iotag;
8647 cmnd = iocbq->iocb.ulpCommand;
8649 switch (iocbq->iocb.ulpCommand) {
8650 case CMD_ELS_REQUEST64_CR:
8651 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8652 ndlp = iocbq->context_un.ndlp;
8653 else
8654 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8655 if (!iocbq->iocb.ulpLe) {
8656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8657 "2007 Only Limited Edition cmd Format"
8658 " supported 0x%x\n",
8659 iocbq->iocb.ulpCommand);
8660 return IOCB_ERROR;
8663 wqe->els_req.payload_len = xmit_len;
8664 /* Els_reguest64 has a TMO */
8665 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8666 iocbq->iocb.ulpTimeout);
8667 /* Need a VF for word 4 set the vf bit*/
8668 bf_set(els_req64_vf, &wqe->els_req, 0);
8669 /* And a VFID for word 12 */
8670 bf_set(els_req64_vfid, &wqe->els_req, 0);
8671 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8672 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8673 iocbq->iocb.ulpContext);
8674 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8675 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8676 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8677 if (command_type == ELS_COMMAND_FIP)
8678 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8679 >> LPFC_FIP_ELS_ID_SHIFT);
8680 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8681 iocbq->context2)->virt);
8682 if_type = bf_get(lpfc_sli_intf_if_type,
8683 &phba->sli4_hba.sli_intf);
8684 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8685 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8686 *pcmd == ELS_CMD_SCR ||
8687 *pcmd == ELS_CMD_FDISC ||
8688 *pcmd == ELS_CMD_LOGO ||
8689 *pcmd == ELS_CMD_PLOGI)) {
8690 bf_set(els_req64_sp, &wqe->els_req, 1);
8691 bf_set(els_req64_sid, &wqe->els_req,
8692 iocbq->vport->fc_myDID);
8693 if ((*pcmd == ELS_CMD_FLOGI) &&
8694 !(phba->fc_topology ==
8695 LPFC_TOPOLOGY_LOOP))
8696 bf_set(els_req64_sid, &wqe->els_req, 0);
8697 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8698 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8699 phba->vpi_ids[iocbq->vport->vpi]);
8700 } else if (pcmd && iocbq->context1) {
8701 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8702 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8703 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8706 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8707 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8708 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8709 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8710 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8711 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8712 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8713 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8714 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8715 break;
8716 case CMD_XMIT_SEQUENCE64_CX:
8717 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8718 iocbq->iocb.un.ulpWord[3]);
8719 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8720 iocbq->iocb.unsli3.rcvsli3.ox_id);
8721 /* The entire sequence is transmitted for this IOCB */
8722 xmit_len = total_len;
8723 cmnd = CMD_XMIT_SEQUENCE64_CR;
8724 if (phba->link_flag & LS_LOOPBACK_MODE)
8725 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8726 case CMD_XMIT_SEQUENCE64_CR:
8727 /* word3 iocb=io_tag32 wqe=reserved */
8728 wqe->xmit_sequence.rsvd3 = 0;
8729 /* word4 relative_offset memcpy */
8730 /* word5 r_ctl/df_ctl memcpy */
8731 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8732 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8733 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8734 LPFC_WQE_IOD_WRITE);
8735 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8736 LPFC_WQE_LENLOC_WORD12);
8737 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8738 wqe->xmit_sequence.xmit_len = xmit_len;
8739 command_type = OTHER_COMMAND;
8740 break;
8741 case CMD_XMIT_BCAST64_CN:
8742 /* word3 iocb=iotag32 wqe=seq_payload_len */
8743 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8744 /* word4 iocb=rsvd wqe=rsvd */
8745 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8746 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8747 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8748 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8749 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8750 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8751 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8752 LPFC_WQE_LENLOC_WORD3);
8753 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8754 break;
8755 case CMD_FCP_IWRITE64_CR:
8756 command_type = FCP_COMMAND_DATA_OUT;
8757 /* word3 iocb=iotag wqe=payload_offset_len */
8758 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8759 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8760 xmit_len + sizeof(struct fcp_rsp));
8761 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8763 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8764 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8765 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8766 iocbq->iocb.ulpFCP2Rcvy);
8767 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8768 /* Always open the exchange */
8769 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8770 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8771 LPFC_WQE_LENLOC_WORD4);
8772 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8773 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8774 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8775 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8776 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8777 if (iocbq->priority) {
8778 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8779 (iocbq->priority << 1));
8780 } else {
8781 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8782 (phba->cfg_XLanePriority << 1));
8785 /* Note, word 10 is already initialized to 0 */
8787 if (phba->fcp_embed_io) {
8788 struct lpfc_scsi_buf *lpfc_cmd;
8789 struct sli4_sge *sgl;
8790 union lpfc_wqe128 *wqe128;
8791 struct fcp_cmnd *fcp_cmnd;
8792 uint32_t *ptr;
8794 /* 128 byte wqe support here */
8795 wqe128 = (union lpfc_wqe128 *)wqe;
8797 lpfc_cmd = iocbq->context1;
8798 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8799 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8801 /* Word 0-2 - FCP_CMND */
8802 wqe128->generic.bde.tus.f.bdeFlags =
8803 BUFF_TYPE_BDE_IMMED;
8804 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8805 wqe128->generic.bde.addrHigh = 0;
8806 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8808 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8810 /* Word 22-29 FCP CMND Payload */
8811 ptr = &wqe128->words[22];
8812 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8814 break;
8815 case CMD_FCP_IREAD64_CR:
8816 /* word3 iocb=iotag wqe=payload_offset_len */
8817 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8818 bf_set(payload_offset_len, &wqe->fcp_iread,
8819 xmit_len + sizeof(struct fcp_rsp));
8820 bf_set(cmd_buff_len, &wqe->fcp_iread,
8822 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8823 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8824 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8825 iocbq->iocb.ulpFCP2Rcvy);
8826 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8827 /* Always open the exchange */
8828 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8829 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8830 LPFC_WQE_LENLOC_WORD4);
8831 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8832 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8833 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8834 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8835 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8836 if (iocbq->priority) {
8837 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8838 (iocbq->priority << 1));
8839 } else {
8840 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8841 (phba->cfg_XLanePriority << 1));
8844 /* Note, word 10 is already initialized to 0 */
8846 if (phba->fcp_embed_io) {
8847 struct lpfc_scsi_buf *lpfc_cmd;
8848 struct sli4_sge *sgl;
8849 union lpfc_wqe128 *wqe128;
8850 struct fcp_cmnd *fcp_cmnd;
8851 uint32_t *ptr;
8853 /* 128 byte wqe support here */
8854 wqe128 = (union lpfc_wqe128 *)wqe;
8856 lpfc_cmd = iocbq->context1;
8857 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8858 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8860 /* Word 0-2 - FCP_CMND */
8861 wqe128->generic.bde.tus.f.bdeFlags =
8862 BUFF_TYPE_BDE_IMMED;
8863 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8864 wqe128->generic.bde.addrHigh = 0;
8865 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8867 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8869 /* Word 22-29 FCP CMND Payload */
8870 ptr = &wqe128->words[22];
8871 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8873 break;
8874 case CMD_FCP_ICMND64_CR:
8875 /* word3 iocb=iotag wqe=payload_offset_len */
8876 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8877 bf_set(payload_offset_len, &wqe->fcp_icmd,
8878 xmit_len + sizeof(struct fcp_rsp));
8879 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8881 /* word3 iocb=IO_TAG wqe=reserved */
8882 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8883 /* Always open the exchange */
8884 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8885 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8886 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8887 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8888 LPFC_WQE_LENLOC_NONE);
8889 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8890 iocbq->iocb.ulpFCP2Rcvy);
8891 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8892 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8893 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8894 if (iocbq->priority) {
8895 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8896 (iocbq->priority << 1));
8897 } else {
8898 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8899 (phba->cfg_XLanePriority << 1));
8902 /* Note, word 10 is already initialized to 0 */
8904 if (phba->fcp_embed_io) {
8905 struct lpfc_scsi_buf *lpfc_cmd;
8906 struct sli4_sge *sgl;
8907 union lpfc_wqe128 *wqe128;
8908 struct fcp_cmnd *fcp_cmnd;
8909 uint32_t *ptr;
8911 /* 128 byte wqe support here */
8912 wqe128 = (union lpfc_wqe128 *)wqe;
8914 lpfc_cmd = iocbq->context1;
8915 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8916 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8918 /* Word 0-2 - FCP_CMND */
8919 wqe128->generic.bde.tus.f.bdeFlags =
8920 BUFF_TYPE_BDE_IMMED;
8921 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8922 wqe128->generic.bde.addrHigh = 0;
8923 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8925 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8927 /* Word 22-29 FCP CMND Payload */
8928 ptr = &wqe128->words[22];
8929 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8931 break;
8932 case CMD_GEN_REQUEST64_CR:
8933 /* For this command calculate the xmit length of the
8934 * request bde.
8936 xmit_len = 0;
8937 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8938 sizeof(struct ulp_bde64);
8939 for (i = 0; i < numBdes; i++) {
8940 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8941 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8942 break;
8943 xmit_len += bde.tus.f.bdeSize;
8945 /* word3 iocb=IO_TAG wqe=request_payload_len */
8946 wqe->gen_req.request_payload_len = xmit_len;
8947 /* word4 iocb=parameter wqe=relative_offset memcpy */
8948 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8949 /* word6 context tag copied in memcpy */
8950 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8951 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953 "2015 Invalid CT %x command 0x%x\n",
8954 ct, iocbq->iocb.ulpCommand);
8955 return IOCB_ERROR;
8957 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8958 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8959 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8960 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8961 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8962 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8963 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8964 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8965 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8966 command_type = OTHER_COMMAND;
8967 break;
8968 case CMD_XMIT_ELS_RSP64_CX:
8969 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8970 /* words0-2 BDE memcpy */
8971 /* word3 iocb=iotag32 wqe=response_payload_len */
8972 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8973 /* word4 */
8974 wqe->xmit_els_rsp.word4 = 0;
8975 /* word5 iocb=rsvd wge=did */
8976 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8977 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8979 if_type = bf_get(lpfc_sli_intf_if_type,
8980 &phba->sli4_hba.sli_intf);
8981 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8982 if (iocbq->vport->fc_flag & FC_PT2PT) {
8983 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8984 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8985 iocbq->vport->fc_myDID);
8986 if (iocbq->vport->fc_myDID == Fabric_DID) {
8987 bf_set(wqe_els_did,
8988 &wqe->xmit_els_rsp.wqe_dest, 0);
8992 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8993 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8994 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8995 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8996 iocbq->iocb.unsli3.rcvsli3.ox_id);
8997 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8998 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8999 phba->vpi_ids[iocbq->vport->vpi]);
9000 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9001 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9002 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9003 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9004 LPFC_WQE_LENLOC_WORD3);
9005 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9006 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9007 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9008 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9009 iocbq->context2)->virt);
9010 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9011 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9012 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9013 iocbq->vport->fc_myDID);
9014 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9015 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9016 phba->vpi_ids[phba->pport->vpi]);
9018 command_type = OTHER_COMMAND;
9019 break;
9020 case CMD_CLOSE_XRI_CN:
9021 case CMD_ABORT_XRI_CN:
9022 case CMD_ABORT_XRI_CX:
9023 /* words 0-2 memcpy should be 0 rserved */
9024 /* port will send abts */
9025 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9026 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9027 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9028 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9029 } else
9030 fip = 0;
9032 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9034 * The link is down, or the command was ELS_FIP
9035 * so the fw does not need to send abts
9036 * on the wire.
9038 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9039 else
9040 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9041 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9042 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9043 wqe->abort_cmd.rsrvd5 = 0;
9044 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9045 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9046 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9048 * The abort handler will send us CMD_ABORT_XRI_CN or
9049 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9051 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9052 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9053 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9054 LPFC_WQE_LENLOC_NONE);
9055 cmnd = CMD_ABORT_XRI_CX;
9056 command_type = OTHER_COMMAND;
9057 xritag = 0;
9058 break;
9059 case CMD_XMIT_BLS_RSP64_CX:
9060 ndlp = (struct lpfc_nodelist *)iocbq->context1;
9061 /* As BLS ABTS RSP WQE is very different from other WQEs,
9062 * we re-construct this WQE here based on information in
9063 * iocbq from scratch.
9065 memset(wqe, 0, sizeof(union lpfc_wqe));
9066 /* OX_ID is invariable to who sent ABTS to CT exchange */
9067 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9068 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9069 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9070 LPFC_ABTS_UNSOL_INT) {
9071 /* ABTS sent by initiator to CT exchange, the
9072 * RX_ID field will be filled with the newly
9073 * allocated responder XRI.
9075 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9076 iocbq->sli4_xritag);
9077 } else {
9078 /* ABTS sent by responder to CT exchange, the
9079 * RX_ID field will be filled with the responder
9080 * RX_ID from ABTS.
9082 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9083 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9085 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9086 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9088 /* Use CT=VPI */
9089 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9090 ndlp->nlp_DID);
9091 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9092 iocbq->iocb.ulpContext);
9093 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9094 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9095 phba->vpi_ids[phba->pport->vpi]);
9096 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9097 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9098 LPFC_WQE_LENLOC_NONE);
9099 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9100 command_type = OTHER_COMMAND;
9101 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9102 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9103 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9104 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9105 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9106 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9107 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9110 break;
9111 case CMD_XRI_ABORTED_CX:
9112 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9113 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9114 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9115 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9116 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9117 default:
9118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9119 "2014 Invalid command 0x%x\n",
9120 iocbq->iocb.ulpCommand);
9121 return IOCB_ERROR;
9122 break;
9125 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9126 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9127 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9128 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9129 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9130 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9131 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9132 LPFC_IO_DIF_INSERT);
9133 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9134 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9135 wqe->generic.wqe_com.abort_tag = abort_tag;
9136 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9137 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9138 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9139 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9140 return 0;
9144 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9145 * @phba: Pointer to HBA context object.
9146 * @ring_number: SLI ring number to issue iocb on.
9147 * @piocb: Pointer to command iocb.
9148 * @flag: Flag indicating if this command can be put into txq.
9150 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9151 * an iocb command to an HBA with SLI-4 interface spec.
9153 * This function is called with hbalock held. The function will return success
9154 * after it successfully submit the iocb to firmware or after adding to the
9155 * txq.
9157 static int
9158 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9159 struct lpfc_iocbq *piocb, uint32_t flag)
9161 struct lpfc_sglq *sglq;
9162 union lpfc_wqe *wqe;
9163 union lpfc_wqe128 wqe128;
9164 struct lpfc_queue *wq;
9165 struct lpfc_sli_ring *pring;
9167 /* Get the WQ */
9168 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9169 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9170 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9171 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9172 else
9173 wq = phba->sli4_hba.oas_wq;
9174 } else {
9175 wq = phba->sli4_hba.els_wq;
9178 /* Get corresponding ring */
9179 pring = wq->pring;
9182 * The WQE can be either 64 or 128 bytes,
9183 * so allocate space on the stack assuming the largest.
9185 wqe = (union lpfc_wqe *)&wqe128;
9187 lockdep_assert_held(&phba->hbalock);
9189 if (piocb->sli4_xritag == NO_XRI) {
9190 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9191 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9192 sglq = NULL;
9193 else {
9194 if (!list_empty(&pring->txq)) {
9195 if (!(flag & SLI_IOCB_RET_IOCB)) {
9196 __lpfc_sli_ringtx_put(phba,
9197 pring, piocb);
9198 return IOCB_SUCCESS;
9199 } else {
9200 return IOCB_BUSY;
9202 } else {
9203 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9204 if (!sglq) {
9205 if (!(flag & SLI_IOCB_RET_IOCB)) {
9206 __lpfc_sli_ringtx_put(phba,
9207 pring,
9208 piocb);
9209 return IOCB_SUCCESS;
9210 } else
9211 return IOCB_BUSY;
9215 } else if (piocb->iocb_flag & LPFC_IO_FCP)
9216 /* These IO's already have an XRI and a mapped sgl. */
9217 sglq = NULL;
9218 else {
9220 * This is a continuation of a commandi,(CX) so this
9221 * sglq is on the active list
9223 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9224 if (!sglq)
9225 return IOCB_ERROR;
9228 if (sglq) {
9229 piocb->sli4_lxritag = sglq->sli4_lxritag;
9230 piocb->sli4_xritag = sglq->sli4_xritag;
9231 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9232 return IOCB_ERROR;
9235 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
9236 return IOCB_ERROR;
9238 if (lpfc_sli4_wq_put(wq, wqe))
9239 return IOCB_ERROR;
9240 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9242 return 0;
9246 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9248 * This routine wraps the actual lockless version for issusing IOCB function
9249 * pointer from the lpfc_hba struct.
9251 * Return codes:
9252 * IOCB_ERROR - Error
9253 * IOCB_SUCCESS - Success
9254 * IOCB_BUSY - Busy
9257 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9258 struct lpfc_iocbq *piocb, uint32_t flag)
9260 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9264 * lpfc_sli_api_table_setup - Set up sli api function jump table
9265 * @phba: The hba struct for which this call is being executed.
9266 * @dev_grp: The HBA PCI-Device group number.
9268 * This routine sets up the SLI interface API function jump table in @phba
9269 * struct.
9270 * Returns: 0 - success, -ENODEV - failure.
9273 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9276 switch (dev_grp) {
9277 case LPFC_PCI_DEV_LP:
9278 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9279 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9280 break;
9281 case LPFC_PCI_DEV_OC:
9282 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9283 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9284 break;
9285 default:
9286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9287 "1419 Invalid HBA PCI-device group: 0x%x\n",
9288 dev_grp);
9289 return -ENODEV;
9290 break;
9292 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9293 return 0;
9297 * lpfc_sli4_calc_ring - Calculates which ring to use
9298 * @phba: Pointer to HBA context object.
9299 * @piocb: Pointer to command iocb.
9301 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9302 * hba_wqidx, thus we need to calculate the corresponding ring.
9303 * Since ABORTS must go on the same WQ of the command they are
9304 * aborting, we use command's hba_wqidx.
9306 struct lpfc_sli_ring *
9307 lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9309 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9310 if (!(phba->cfg_fof) ||
9311 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9312 if (unlikely(!phba->sli4_hba.fcp_wq))
9313 return NULL;
9315 * for abort iocb hba_wqidx should already
9316 * be setup based on what work queue we used.
9318 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9319 piocb->hba_wqidx =
9320 lpfc_sli4_scmd_to_wqidx_distr(phba,
9321 piocb->context1);
9322 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9323 } else {
9324 if (unlikely(!phba->sli4_hba.oas_wq))
9325 return NULL;
9326 piocb->hba_wqidx = 0;
9327 return phba->sli4_hba.oas_wq->pring;
9329 } else {
9330 if (unlikely(!phba->sli4_hba.els_wq))
9331 return NULL;
9332 piocb->hba_wqidx = 0;
9333 return phba->sli4_hba.els_wq->pring;
9338 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9339 * @phba: Pointer to HBA context object.
9340 * @pring: Pointer to driver SLI ring object.
9341 * @piocb: Pointer to command iocb.
9342 * @flag: Flag indicating if this command can be put into txq.
9344 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9345 * function. This function gets the hbalock and calls
9346 * __lpfc_sli_issue_iocb function and will return the error returned
9347 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9348 * functions which do not hold hbalock.
9351 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9352 struct lpfc_iocbq *piocb, uint32_t flag)
9354 struct lpfc_hba_eq_hdl *hba_eq_hdl;
9355 struct lpfc_sli_ring *pring;
9356 struct lpfc_queue *fpeq;
9357 struct lpfc_eqe *eqe;
9358 unsigned long iflags;
9359 int rc, idx;
9361 if (phba->sli_rev == LPFC_SLI_REV4) {
9362 pring = lpfc_sli4_calc_ring(phba, piocb);
9363 if (unlikely(pring == NULL))
9364 return IOCB_ERROR;
9366 spin_lock_irqsave(&pring->ring_lock, iflags);
9367 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9368 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9370 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9371 idx = piocb->hba_wqidx;
9372 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9374 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
9376 /* Get associated EQ with this index */
9377 fpeq = phba->sli4_hba.hba_eq[idx];
9379 /* Turn off interrupts from this EQ */
9380 lpfc_sli4_eq_clr_intr(fpeq);
9383 * Process all the events on FCP EQ
9385 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9386 lpfc_sli4_hba_handle_eqe(phba,
9387 eqe, idx);
9388 fpeq->EQ_processed++;
9391 /* Always clear and re-arm the EQ */
9392 lpfc_sli4_eq_release(fpeq,
9393 LPFC_QUEUE_REARM);
9395 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
9397 } else {
9398 /* For now, SLI2/3 will still use hbalock */
9399 spin_lock_irqsave(&phba->hbalock, iflags);
9400 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9401 spin_unlock_irqrestore(&phba->hbalock, iflags);
9403 return rc;
9407 * lpfc_extra_ring_setup - Extra ring setup function
9408 * @phba: Pointer to HBA context object.
9410 * This function is called while driver attaches with the
9411 * HBA to setup the extra ring. The extra ring is used
9412 * only when driver needs to support target mode functionality
9413 * or IP over FC functionalities.
9415 * This function is called with no lock held. SLI3 only.
9417 static int
9418 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9420 struct lpfc_sli *psli;
9421 struct lpfc_sli_ring *pring;
9423 psli = &phba->sli;
9425 /* Adjust cmd/rsp ring iocb entries more evenly */
9427 /* Take some away from the FCP ring */
9428 pring = &psli->sli3_ring[LPFC_FCP_RING];
9429 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9430 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9431 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9432 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9434 /* and give them to the extra ring */
9435 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
9437 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9438 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9439 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9440 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9442 /* Setup default profile for this ring */
9443 pring->iotag_max = 4096;
9444 pring->num_mask = 1;
9445 pring->prt[0].profile = 0; /* Mask 0 */
9446 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9447 pring->prt[0].type = phba->cfg_multi_ring_type;
9448 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9449 return 0;
9452 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9453 * @phba: Pointer to HBA context object.
9454 * @iocbq: Pointer to iocb object.
9456 * The async_event handler calls this routine when it receives
9457 * an ASYNC_STATUS_CN event from the port. The port generates
9458 * this event when an Abort Sequence request to an rport fails
9459 * twice in succession. The abort could be originated by the
9460 * driver or by the port. The ABTS could have been for an ELS
9461 * or FCP IO. The port only generates this event when an ABTS
9462 * fails to complete after one retry.
9464 static void
9465 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9466 struct lpfc_iocbq *iocbq)
9468 struct lpfc_nodelist *ndlp = NULL;
9469 uint16_t rpi = 0, vpi = 0;
9470 struct lpfc_vport *vport = NULL;
9472 /* The rpi in the ulpContext is vport-sensitive. */
9473 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9474 rpi = iocbq->iocb.ulpContext;
9476 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9477 "3092 Port generated ABTS async event "
9478 "on vpi %d rpi %d status 0x%x\n",
9479 vpi, rpi, iocbq->iocb.ulpStatus);
9481 vport = lpfc_find_vport_by_vpid(phba, vpi);
9482 if (!vport)
9483 goto err_exit;
9484 ndlp = lpfc_findnode_rpi(vport, rpi);
9485 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9486 goto err_exit;
9488 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9489 lpfc_sli_abts_recover_port(vport, ndlp);
9490 return;
9492 err_exit:
9493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9494 "3095 Event Context not found, no "
9495 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9496 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9497 vpi, rpi);
9500 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9501 * @phba: pointer to HBA context object.
9502 * @ndlp: nodelist pointer for the impacted rport.
9503 * @axri: pointer to the wcqe containing the failed exchange.
9505 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9506 * port. The port generates this event when an abort exchange request to an
9507 * rport fails twice in succession with no reply. The abort could be originated
9508 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9510 void
9511 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9512 struct lpfc_nodelist *ndlp,
9513 struct sli4_wcqe_xri_aborted *axri)
9515 struct lpfc_vport *vport;
9516 uint32_t ext_status = 0;
9518 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9519 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9520 "3115 Node Context not found, driver "
9521 "ignoring abts err event\n");
9522 return;
9525 vport = ndlp->vport;
9526 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9527 "3116 Port generated FCP XRI ABORT event on "
9528 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9529 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9530 bf_get(lpfc_wcqe_xa_xri, axri),
9531 bf_get(lpfc_wcqe_xa_status, axri),
9532 axri->parameter);
9535 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9536 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9537 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9539 ext_status = axri->parameter & IOERR_PARAM_MASK;
9540 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9541 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9542 lpfc_sli_abts_recover_port(vport, ndlp);
9546 * lpfc_sli_async_event_handler - ASYNC iocb handler function
9547 * @phba: Pointer to HBA context object.
9548 * @pring: Pointer to driver SLI ring object.
9549 * @iocbq: Pointer to iocb object.
9551 * This function is called by the slow ring event handler
9552 * function when there is an ASYNC event iocb in the ring.
9553 * This function is called with no lock held.
9554 * Currently this function handles only temperature related
9555 * ASYNC events. The function decodes the temperature sensor
9556 * event message and posts events for the management applications.
9558 static void
9559 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9560 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9562 IOCB_t *icmd;
9563 uint16_t evt_code;
9564 struct temp_event temp_event_data;
9565 struct Scsi_Host *shost;
9566 uint32_t *iocb_w;
9568 icmd = &iocbq->iocb;
9569 evt_code = icmd->un.asyncstat.evt_code;
9571 switch (evt_code) {
9572 case ASYNC_TEMP_WARN:
9573 case ASYNC_TEMP_SAFE:
9574 temp_event_data.data = (uint32_t) icmd->ulpContext;
9575 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9576 if (evt_code == ASYNC_TEMP_WARN) {
9577 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9578 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9579 "0347 Adapter is very hot, please take "
9580 "corrective action. temperature : %d Celsius\n",
9581 (uint32_t) icmd->ulpContext);
9582 } else {
9583 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9584 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9585 "0340 Adapter temperature is OK now. "
9586 "temperature : %d Celsius\n",
9587 (uint32_t) icmd->ulpContext);
9590 /* Send temperature change event to applications */
9591 shost = lpfc_shost_from_vport(phba->pport);
9592 fc_host_post_vendor_event(shost, fc_get_event_number(),
9593 sizeof(temp_event_data), (char *) &temp_event_data,
9594 LPFC_NL_VENDOR_ID);
9595 break;
9596 case ASYNC_STATUS_CN:
9597 lpfc_sli_abts_err_handler(phba, iocbq);
9598 break;
9599 default:
9600 iocb_w = (uint32_t *) icmd;
9601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9602 "0346 Ring %d handler: unexpected ASYNC_STATUS"
9603 " evt_code 0x%x\n"
9604 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9605 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9606 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9607 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9608 pring->ringno, icmd->un.asyncstat.evt_code,
9609 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9610 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9611 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9612 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9614 break;
9620 * lpfc_sli4_setup - SLI ring setup function
9621 * @phba: Pointer to HBA context object.
9623 * lpfc_sli_setup sets up rings of the SLI interface with
9624 * number of iocbs per ring and iotags. This function is
9625 * called while driver attach to the HBA and before the
9626 * interrupts are enabled. So there is no need for locking.
9628 * This function always returns 0.
9631 lpfc_sli4_setup(struct lpfc_hba *phba)
9633 struct lpfc_sli_ring *pring;
9635 pring = phba->sli4_hba.els_wq->pring;
9636 pring->num_mask = LPFC_MAX_RING_MASK;
9637 pring->prt[0].profile = 0; /* Mask 0 */
9638 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9639 pring->prt[0].type = FC_TYPE_ELS;
9640 pring->prt[0].lpfc_sli_rcv_unsol_event =
9641 lpfc_els_unsol_event;
9642 pring->prt[1].profile = 0; /* Mask 1 */
9643 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9644 pring->prt[1].type = FC_TYPE_ELS;
9645 pring->prt[1].lpfc_sli_rcv_unsol_event =
9646 lpfc_els_unsol_event;
9647 pring->prt[2].profile = 0; /* Mask 2 */
9648 /* NameServer Inquiry */
9649 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9650 /* NameServer */
9651 pring->prt[2].type = FC_TYPE_CT;
9652 pring->prt[2].lpfc_sli_rcv_unsol_event =
9653 lpfc_ct_unsol_event;
9654 pring->prt[3].profile = 0; /* Mask 3 */
9655 /* NameServer response */
9656 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9657 /* NameServer */
9658 pring->prt[3].type = FC_TYPE_CT;
9659 pring->prt[3].lpfc_sli_rcv_unsol_event =
9660 lpfc_ct_unsol_event;
9661 return 0;
9665 * lpfc_sli_setup - SLI ring setup function
9666 * @phba: Pointer to HBA context object.
9668 * lpfc_sli_setup sets up rings of the SLI interface with
9669 * number of iocbs per ring and iotags. This function is
9670 * called while driver attach to the HBA and before the
9671 * interrupts are enabled. So there is no need for locking.
9673 * This function always returns 0. SLI3 only.
9676 lpfc_sli_setup(struct lpfc_hba *phba)
9678 int i, totiocbsize = 0;
9679 struct lpfc_sli *psli = &phba->sli;
9680 struct lpfc_sli_ring *pring;
9682 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9683 psli->sli_flag = 0;
9685 psli->iocbq_lookup = NULL;
9686 psli->iocbq_lookup_len = 0;
9687 psli->last_iotag = 0;
9689 for (i = 0; i < psli->num_rings; i++) {
9690 pring = &psli->sli3_ring[i];
9691 switch (i) {
9692 case LPFC_FCP_RING: /* ring 0 - FCP */
9693 /* numCiocb and numRiocb are used in config_port */
9694 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9695 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9696 pring->sli.sli3.numCiocb +=
9697 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9698 pring->sli.sli3.numRiocb +=
9699 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9700 pring->sli.sli3.numCiocb +=
9701 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9702 pring->sli.sli3.numRiocb +=
9703 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9704 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9705 SLI3_IOCB_CMD_SIZE :
9706 SLI2_IOCB_CMD_SIZE;
9707 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9708 SLI3_IOCB_RSP_SIZE :
9709 SLI2_IOCB_RSP_SIZE;
9710 pring->iotag_ctr = 0;
9711 pring->iotag_max =
9712 (phba->cfg_hba_queue_depth * 2);
9713 pring->fast_iotag = pring->iotag_max;
9714 pring->num_mask = 0;
9715 break;
9716 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
9717 /* numCiocb and numRiocb are used in config_port */
9718 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9719 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9720 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9721 SLI3_IOCB_CMD_SIZE :
9722 SLI2_IOCB_CMD_SIZE;
9723 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9724 SLI3_IOCB_RSP_SIZE :
9725 SLI2_IOCB_RSP_SIZE;
9726 pring->iotag_max = phba->cfg_hba_queue_depth;
9727 pring->num_mask = 0;
9728 break;
9729 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9730 /* numCiocb and numRiocb are used in config_port */
9731 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9732 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9733 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9734 SLI3_IOCB_CMD_SIZE :
9735 SLI2_IOCB_CMD_SIZE;
9736 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9737 SLI3_IOCB_RSP_SIZE :
9738 SLI2_IOCB_RSP_SIZE;
9739 pring->fast_iotag = 0;
9740 pring->iotag_ctr = 0;
9741 pring->iotag_max = 4096;
9742 pring->lpfc_sli_rcv_async_status =
9743 lpfc_sli_async_event_handler;
9744 pring->num_mask = LPFC_MAX_RING_MASK;
9745 pring->prt[0].profile = 0; /* Mask 0 */
9746 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9747 pring->prt[0].type = FC_TYPE_ELS;
9748 pring->prt[0].lpfc_sli_rcv_unsol_event =
9749 lpfc_els_unsol_event;
9750 pring->prt[1].profile = 0; /* Mask 1 */
9751 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9752 pring->prt[1].type = FC_TYPE_ELS;
9753 pring->prt[1].lpfc_sli_rcv_unsol_event =
9754 lpfc_els_unsol_event;
9755 pring->prt[2].profile = 0; /* Mask 2 */
9756 /* NameServer Inquiry */
9757 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9758 /* NameServer */
9759 pring->prt[2].type = FC_TYPE_CT;
9760 pring->prt[2].lpfc_sli_rcv_unsol_event =
9761 lpfc_ct_unsol_event;
9762 pring->prt[3].profile = 0; /* Mask 3 */
9763 /* NameServer response */
9764 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9765 /* NameServer */
9766 pring->prt[3].type = FC_TYPE_CT;
9767 pring->prt[3].lpfc_sli_rcv_unsol_event =
9768 lpfc_ct_unsol_event;
9769 break;
9771 totiocbsize += (pring->sli.sli3.numCiocb *
9772 pring->sli.sli3.sizeCiocb) +
9773 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9775 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9776 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9777 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9778 "SLI2 SLIM Data: x%x x%lx\n",
9779 phba->brd_no, totiocbsize,
9780 (unsigned long) MAX_SLIM_IOCB_SIZE);
9782 if (phba->cfg_multi_ring_support == 2)
9783 lpfc_extra_ring_setup(phba);
9785 return 0;
9789 * lpfc_sli4_queue_init - Queue initialization function
9790 * @phba: Pointer to HBA context object.
9792 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
9793 * ring. This function also initializes ring indices of each ring.
9794 * This function is called during the initialization of the SLI
9795 * interface of an HBA.
9796 * This function is called with no lock held and always returns
9797 * 1.
9799 void
9800 lpfc_sli4_queue_init(struct lpfc_hba *phba)
9802 struct lpfc_sli *psli;
9803 struct lpfc_sli_ring *pring;
9804 int i;
9806 psli = &phba->sli;
9807 spin_lock_irq(&phba->hbalock);
9808 INIT_LIST_HEAD(&psli->mboxq);
9809 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9810 /* Initialize list headers for txq and txcmplq as double linked lists */
9811 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9812 pring = phba->sli4_hba.fcp_wq[i]->pring;
9813 pring->flag = 0;
9814 pring->ringno = LPFC_FCP_RING;
9815 INIT_LIST_HEAD(&pring->txq);
9816 INIT_LIST_HEAD(&pring->txcmplq);
9817 INIT_LIST_HEAD(&pring->iocb_continueq);
9818 spin_lock_init(&pring->ring_lock);
9820 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9821 pring = phba->sli4_hba.nvme_wq[i]->pring;
9822 pring->flag = 0;
9823 pring->ringno = LPFC_FCP_RING;
9824 INIT_LIST_HEAD(&pring->txq);
9825 INIT_LIST_HEAD(&pring->txcmplq);
9826 INIT_LIST_HEAD(&pring->iocb_continueq);
9827 spin_lock_init(&pring->ring_lock);
9829 pring = phba->sli4_hba.els_wq->pring;
9830 pring->flag = 0;
9831 pring->ringno = LPFC_ELS_RING;
9832 INIT_LIST_HEAD(&pring->txq);
9833 INIT_LIST_HEAD(&pring->txcmplq);
9834 INIT_LIST_HEAD(&pring->iocb_continueq);
9835 spin_lock_init(&pring->ring_lock);
9837 if (phba->cfg_nvme_io_channel) {
9838 pring = phba->sli4_hba.nvmels_wq->pring;
9839 pring->flag = 0;
9840 pring->ringno = LPFC_ELS_RING;
9841 INIT_LIST_HEAD(&pring->txq);
9842 INIT_LIST_HEAD(&pring->txcmplq);
9843 INIT_LIST_HEAD(&pring->iocb_continueq);
9844 spin_lock_init(&pring->ring_lock);
9847 if (phba->cfg_fof) {
9848 pring = phba->sli4_hba.oas_wq->pring;
9849 pring->flag = 0;
9850 pring->ringno = LPFC_FCP_RING;
9851 INIT_LIST_HEAD(&pring->txq);
9852 INIT_LIST_HEAD(&pring->txcmplq);
9853 INIT_LIST_HEAD(&pring->iocb_continueq);
9854 spin_lock_init(&pring->ring_lock);
9857 spin_unlock_irq(&phba->hbalock);
9861 * lpfc_sli_queue_init - Queue initialization function
9862 * @phba: Pointer to HBA context object.
9864 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9865 * ring. This function also initializes ring indices of each ring.
9866 * This function is called during the initialization of the SLI
9867 * interface of an HBA.
9868 * This function is called with no lock held and always returns
9869 * 1.
9871 void
9872 lpfc_sli_queue_init(struct lpfc_hba *phba)
9874 struct lpfc_sli *psli;
9875 struct lpfc_sli_ring *pring;
9876 int i;
9878 psli = &phba->sli;
9879 spin_lock_irq(&phba->hbalock);
9880 INIT_LIST_HEAD(&psli->mboxq);
9881 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9882 /* Initialize list headers for txq and txcmplq as double linked lists */
9883 for (i = 0; i < psli->num_rings; i++) {
9884 pring = &psli->sli3_ring[i];
9885 pring->ringno = i;
9886 pring->sli.sli3.next_cmdidx = 0;
9887 pring->sli.sli3.local_getidx = 0;
9888 pring->sli.sli3.cmdidx = 0;
9889 INIT_LIST_HEAD(&pring->iocb_continueq);
9890 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9891 INIT_LIST_HEAD(&pring->postbufq);
9892 pring->flag = 0;
9893 INIT_LIST_HEAD(&pring->txq);
9894 INIT_LIST_HEAD(&pring->txcmplq);
9895 spin_lock_init(&pring->ring_lock);
9897 spin_unlock_irq(&phba->hbalock);
9901 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9902 * @phba: Pointer to HBA context object.
9904 * This routine flushes the mailbox command subsystem. It will unconditionally
9905 * flush all the mailbox commands in the three possible stages in the mailbox
9906 * command sub-system: pending mailbox command queue; the outstanding mailbox
9907 * command; and completed mailbox command queue. It is caller's responsibility
9908 * to make sure that the driver is in the proper state to flush the mailbox
9909 * command sub-system. Namely, the posting of mailbox commands into the
9910 * pending mailbox command queue from the various clients must be stopped;
9911 * either the HBA is in a state that it will never works on the outstanding
9912 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9913 * mailbox command has been completed.
9915 static void
9916 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9918 LIST_HEAD(completions);
9919 struct lpfc_sli *psli = &phba->sli;
9920 LPFC_MBOXQ_t *pmb;
9921 unsigned long iflag;
9923 /* Flush all the mailbox commands in the mbox system */
9924 spin_lock_irqsave(&phba->hbalock, iflag);
9925 /* The pending mailbox command queue */
9926 list_splice_init(&phba->sli.mboxq, &completions);
9927 /* The outstanding active mailbox command */
9928 if (psli->mbox_active) {
9929 list_add_tail(&psli->mbox_active->list, &completions);
9930 psli->mbox_active = NULL;
9931 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9933 /* The completed mailbox command queue */
9934 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9935 spin_unlock_irqrestore(&phba->hbalock, iflag);
9937 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9938 while (!list_empty(&completions)) {
9939 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9940 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9941 if (pmb->mbox_cmpl)
9942 pmb->mbox_cmpl(phba, pmb);
9947 * lpfc_sli_host_down - Vport cleanup function
9948 * @vport: Pointer to virtual port object.
9950 * lpfc_sli_host_down is called to clean up the resources
9951 * associated with a vport before destroying virtual
9952 * port data structures.
9953 * This function does following operations:
9954 * - Free discovery resources associated with this virtual
9955 * port.
9956 * - Free iocbs associated with this virtual port in
9957 * the txq.
9958 * - Send abort for all iocb commands associated with this
9959 * vport in txcmplq.
9961 * This function is called with no lock held and always returns 1.
9964 lpfc_sli_host_down(struct lpfc_vport *vport)
9966 LIST_HEAD(completions);
9967 struct lpfc_hba *phba = vport->phba;
9968 struct lpfc_sli *psli = &phba->sli;
9969 struct lpfc_queue *qp = NULL;
9970 struct lpfc_sli_ring *pring;
9971 struct lpfc_iocbq *iocb, *next_iocb;
9972 int i;
9973 unsigned long flags = 0;
9974 uint16_t prev_pring_flag;
9976 lpfc_cleanup_discovery_resources(vport);
9978 spin_lock_irqsave(&phba->hbalock, flags);
9981 * Error everything on the txq since these iocbs
9982 * have not been given to the FW yet.
9983 * Also issue ABTS for everything on the txcmplq
9985 if (phba->sli_rev != LPFC_SLI_REV4) {
9986 for (i = 0; i < psli->num_rings; i++) {
9987 pring = &psli->sli3_ring[i];
9988 prev_pring_flag = pring->flag;
9989 /* Only slow rings */
9990 if (pring->ringno == LPFC_ELS_RING) {
9991 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9992 /* Set the lpfc data pending flag */
9993 set_bit(LPFC_DATA_READY, &phba->data_flags);
9995 list_for_each_entry_safe(iocb, next_iocb,
9996 &pring->txq, list) {
9997 if (iocb->vport != vport)
9998 continue;
9999 list_move_tail(&iocb->list, &completions);
10001 list_for_each_entry_safe(iocb, next_iocb,
10002 &pring->txcmplq, list) {
10003 if (iocb->vport != vport)
10004 continue;
10005 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10007 pring->flag = prev_pring_flag;
10009 } else {
10010 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10011 pring = qp->pring;
10012 if (!pring)
10013 continue;
10014 if (pring == phba->sli4_hba.els_wq->pring) {
10015 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10016 /* Set the lpfc data pending flag */
10017 set_bit(LPFC_DATA_READY, &phba->data_flags);
10019 prev_pring_flag = pring->flag;
10020 spin_lock_irq(&pring->ring_lock);
10021 list_for_each_entry_safe(iocb, next_iocb,
10022 &pring->txq, list) {
10023 if (iocb->vport != vport)
10024 continue;
10025 list_move_tail(&iocb->list, &completions);
10027 spin_unlock_irq(&pring->ring_lock);
10028 list_for_each_entry_safe(iocb, next_iocb,
10029 &pring->txcmplq, list) {
10030 if (iocb->vport != vport)
10031 continue;
10032 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10034 pring->flag = prev_pring_flag;
10037 spin_unlock_irqrestore(&phba->hbalock, flags);
10039 /* Cancel all the IOCBs from the completions list */
10040 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10041 IOERR_SLI_DOWN);
10042 return 1;
10046 * lpfc_sli_hba_down - Resource cleanup function for the HBA
10047 * @phba: Pointer to HBA context object.
10049 * This function cleans up all iocb, buffers, mailbox commands
10050 * while shutting down the HBA. This function is called with no
10051 * lock held and always returns 1.
10052 * This function does the following to cleanup driver resources:
10053 * - Free discovery resources for each virtual port
10054 * - Cleanup any pending fabric iocbs
10055 * - Iterate through the iocb txq and free each entry
10056 * in the list.
10057 * - Free up any buffer posted to the HBA
10058 * - Free mailbox commands in the mailbox queue.
10061 lpfc_sli_hba_down(struct lpfc_hba *phba)
10063 LIST_HEAD(completions);
10064 struct lpfc_sli *psli = &phba->sli;
10065 struct lpfc_queue *qp = NULL;
10066 struct lpfc_sli_ring *pring;
10067 struct lpfc_dmabuf *buf_ptr;
10068 unsigned long flags = 0;
10069 int i;
10071 /* Shutdown the mailbox command sub-system */
10072 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10074 lpfc_hba_down_prep(phba);
10076 lpfc_fabric_abort_hba(phba);
10078 spin_lock_irqsave(&phba->hbalock, flags);
10081 * Error everything on the txq since these iocbs
10082 * have not been given to the FW yet.
10084 if (phba->sli_rev != LPFC_SLI_REV4) {
10085 for (i = 0; i < psli->num_rings; i++) {
10086 pring = &psli->sli3_ring[i];
10087 /* Only slow rings */
10088 if (pring->ringno == LPFC_ELS_RING) {
10089 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10090 /* Set the lpfc data pending flag */
10091 set_bit(LPFC_DATA_READY, &phba->data_flags);
10093 list_splice_init(&pring->txq, &completions);
10095 } else {
10096 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10097 pring = qp->pring;
10098 if (!pring)
10099 continue;
10100 spin_lock_irq(&pring->ring_lock);
10101 list_splice_init(&pring->txq, &completions);
10102 spin_unlock_irq(&pring->ring_lock);
10103 if (pring == phba->sli4_hba.els_wq->pring) {
10104 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10105 /* Set the lpfc data pending flag */
10106 set_bit(LPFC_DATA_READY, &phba->data_flags);
10110 spin_unlock_irqrestore(&phba->hbalock, flags);
10112 /* Cancel all the IOCBs from the completions list */
10113 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10114 IOERR_SLI_DOWN);
10116 spin_lock_irqsave(&phba->hbalock, flags);
10117 list_splice_init(&phba->elsbuf, &completions);
10118 phba->elsbuf_cnt = 0;
10119 phba->elsbuf_prev_cnt = 0;
10120 spin_unlock_irqrestore(&phba->hbalock, flags);
10122 while (!list_empty(&completions)) {
10123 list_remove_head(&completions, buf_ptr,
10124 struct lpfc_dmabuf, list);
10125 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10126 kfree(buf_ptr);
10129 /* Return any active mbox cmds */
10130 del_timer_sync(&psli->mbox_tmo);
10132 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10133 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10134 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10136 return 1;
10140 * lpfc_sli_pcimem_bcopy - SLI memory copy function
10141 * @srcp: Source memory pointer.
10142 * @destp: Destination memory pointer.
10143 * @cnt: Number of words required to be copied.
10145 * This function is used for copying data between driver memory
10146 * and the SLI memory. This function also changes the endianness
10147 * of each word if native endianness is different from SLI
10148 * endianness. This function can be called with or without
10149 * lock.
10151 void
10152 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10154 uint32_t *src = srcp;
10155 uint32_t *dest = destp;
10156 uint32_t ldata;
10157 int i;
10159 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10160 ldata = *src;
10161 ldata = le32_to_cpu(ldata);
10162 *dest = ldata;
10163 src++;
10164 dest++;
10170 * lpfc_sli_bemem_bcopy - SLI memory copy function
10171 * @srcp: Source memory pointer.
10172 * @destp: Destination memory pointer.
10173 * @cnt: Number of words required to be copied.
10175 * This function is used for copying data between a data structure
10176 * with big endian representation to local endianness.
10177 * This function can be called with or without lock.
10179 void
10180 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10182 uint32_t *src = srcp;
10183 uint32_t *dest = destp;
10184 uint32_t ldata;
10185 int i;
10187 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10188 ldata = *src;
10189 ldata = be32_to_cpu(ldata);
10190 *dest = ldata;
10191 src++;
10192 dest++;
10197 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10198 * @phba: Pointer to HBA context object.
10199 * @pring: Pointer to driver SLI ring object.
10200 * @mp: Pointer to driver buffer object.
10202 * This function is called with no lock held.
10203 * It always return zero after adding the buffer to the postbufq
10204 * buffer list.
10207 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10208 struct lpfc_dmabuf *mp)
10210 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10211 later */
10212 spin_lock_irq(&phba->hbalock);
10213 list_add_tail(&mp->list, &pring->postbufq);
10214 pring->postbufq_cnt++;
10215 spin_unlock_irq(&phba->hbalock);
10216 return 0;
10220 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10221 * @phba: Pointer to HBA context object.
10223 * When HBQ is enabled, buffers are searched based on tags. This function
10224 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10225 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10226 * does not conflict with tags of buffer posted for unsolicited events.
10227 * The function returns the allocated tag. The function is called with
10228 * no locks held.
10230 uint32_t
10231 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10233 spin_lock_irq(&phba->hbalock);
10234 phba->buffer_tag_count++;
10236 * Always set the QUE_BUFTAG_BIT to distiguish between
10237 * a tag assigned by HBQ.
10239 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10240 spin_unlock_irq(&phba->hbalock);
10241 return phba->buffer_tag_count;
10245 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10246 * @phba: Pointer to HBA context object.
10247 * @pring: Pointer to driver SLI ring object.
10248 * @tag: Buffer tag.
10250 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10251 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10252 * iocb is posted to the response ring with the tag of the buffer.
10253 * This function searches the pring->postbufq list using the tag
10254 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10255 * iocb. If the buffer is found then lpfc_dmabuf object of the
10256 * buffer is returned to the caller else NULL is returned.
10257 * This function is called with no lock held.
10259 struct lpfc_dmabuf *
10260 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10261 uint32_t tag)
10263 struct lpfc_dmabuf *mp, *next_mp;
10264 struct list_head *slp = &pring->postbufq;
10266 /* Search postbufq, from the beginning, looking for a match on tag */
10267 spin_lock_irq(&phba->hbalock);
10268 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10269 if (mp->buffer_tag == tag) {
10270 list_del_init(&mp->list);
10271 pring->postbufq_cnt--;
10272 spin_unlock_irq(&phba->hbalock);
10273 return mp;
10277 spin_unlock_irq(&phba->hbalock);
10278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10279 "0402 Cannot find virtual addr for buffer tag on "
10280 "ring %d Data x%lx x%p x%p x%x\n",
10281 pring->ringno, (unsigned long) tag,
10282 slp->next, slp->prev, pring->postbufq_cnt);
10284 return NULL;
10288 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10289 * @phba: Pointer to HBA context object.
10290 * @pring: Pointer to driver SLI ring object.
10291 * @phys: DMA address of the buffer.
10293 * This function searches the buffer list using the dma_address
10294 * of unsolicited event to find the driver's lpfc_dmabuf object
10295 * corresponding to the dma_address. The function returns the
10296 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10297 * This function is called by the ct and els unsolicited event
10298 * handlers to get the buffer associated with the unsolicited
10299 * event.
10301 * This function is called with no lock held.
10303 struct lpfc_dmabuf *
10304 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10305 dma_addr_t phys)
10307 struct lpfc_dmabuf *mp, *next_mp;
10308 struct list_head *slp = &pring->postbufq;
10310 /* Search postbufq, from the beginning, looking for a match on phys */
10311 spin_lock_irq(&phba->hbalock);
10312 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10313 if (mp->phys == phys) {
10314 list_del_init(&mp->list);
10315 pring->postbufq_cnt--;
10316 spin_unlock_irq(&phba->hbalock);
10317 return mp;
10321 spin_unlock_irq(&phba->hbalock);
10322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10323 "0410 Cannot find virtual addr for mapped buf on "
10324 "ring %d Data x%llx x%p x%p x%x\n",
10325 pring->ringno, (unsigned long long)phys,
10326 slp->next, slp->prev, pring->postbufq_cnt);
10327 return NULL;
10331 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10332 * @phba: Pointer to HBA context object.
10333 * @cmdiocb: Pointer to driver command iocb object.
10334 * @rspiocb: Pointer to driver response iocb object.
10336 * This function is the completion handler for the abort iocbs for
10337 * ELS commands. This function is called from the ELS ring event
10338 * handler with no lock held. This function frees memory resources
10339 * associated with the abort iocb.
10341 static void
10342 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10343 struct lpfc_iocbq *rspiocb)
10345 IOCB_t *irsp = &rspiocb->iocb;
10346 uint16_t abort_iotag, abort_context;
10347 struct lpfc_iocbq *abort_iocb = NULL;
10349 if (irsp->ulpStatus) {
10352 * Assume that the port already completed and returned, or
10353 * will return the iocb. Just Log the message.
10355 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10356 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10358 spin_lock_irq(&phba->hbalock);
10359 if (phba->sli_rev < LPFC_SLI_REV4) {
10360 if (abort_iotag != 0 &&
10361 abort_iotag <= phba->sli.last_iotag)
10362 abort_iocb =
10363 phba->sli.iocbq_lookup[abort_iotag];
10364 } else
10365 /* For sli4 the abort_tag is the XRI,
10366 * so the abort routine puts the iotag of the iocb
10367 * being aborted in the context field of the abort
10368 * IOCB.
10370 abort_iocb = phba->sli.iocbq_lookup[abort_context];
10372 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10373 "0327 Cannot abort els iocb %p "
10374 "with tag %x context %x, abort status %x, "
10375 "abort code %x\n",
10376 abort_iocb, abort_iotag, abort_context,
10377 irsp->ulpStatus, irsp->un.ulpWord[4]);
10379 spin_unlock_irq(&phba->hbalock);
10381 lpfc_sli_release_iocbq(phba, cmdiocb);
10382 return;
10386 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
10387 * @phba: Pointer to HBA context object.
10388 * @cmdiocb: Pointer to driver command iocb object.
10389 * @rspiocb: Pointer to driver response iocb object.
10391 * The function is called from SLI ring event handler with no
10392 * lock held. This function is the completion handler for ELS commands
10393 * which are aborted. The function frees memory resources used for
10394 * the aborted ELS commands.
10396 static void
10397 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10398 struct lpfc_iocbq *rspiocb)
10400 IOCB_t *irsp = &rspiocb->iocb;
10402 /* ELS cmd tag <ulpIoTag> completes */
10403 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10404 "0139 Ignoring ELS cmd tag x%x completion Data: "
10405 "x%x x%x x%x\n",
10406 irsp->ulpIoTag, irsp->ulpStatus,
10407 irsp->un.ulpWord[4], irsp->ulpTimeout);
10408 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10409 lpfc_ct_free_iocb(phba, cmdiocb);
10410 else
10411 lpfc_els_free_iocb(phba, cmdiocb);
10412 return;
10416 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
10417 * @phba: Pointer to HBA context object.
10418 * @pring: Pointer to driver SLI ring object.
10419 * @cmdiocb: Pointer to driver command iocb object.
10421 * This function issues an abort iocb for the provided command iocb down to
10422 * the port. Other than the case the outstanding command iocb is an abort
10423 * request, this function issues abort out unconditionally. This function is
10424 * called with hbalock held. The function returns 0 when it fails due to
10425 * memory allocation failure or when the command iocb is an abort request.
10427 static int
10428 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10429 struct lpfc_iocbq *cmdiocb)
10431 struct lpfc_vport *vport = cmdiocb->vport;
10432 struct lpfc_iocbq *abtsiocbp;
10433 IOCB_t *icmd = NULL;
10434 IOCB_t *iabt = NULL;
10435 int retval;
10436 unsigned long iflags;
10438 lockdep_assert_held(&phba->hbalock);
10441 * There are certain command types we don't want to abort. And we
10442 * don't want to abort commands that are already in the process of
10443 * being aborted.
10445 icmd = &cmdiocb->iocb;
10446 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10447 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10448 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10449 return 0;
10451 /* issue ABTS for this IOCB based on iotag */
10452 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10453 if (abtsiocbp == NULL)
10454 return 0;
10456 /* This signals the response to set the correct status
10457 * before calling the completion handler
10459 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10461 iabt = &abtsiocbp->iocb;
10462 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10463 iabt->un.acxri.abortContextTag = icmd->ulpContext;
10464 if (phba->sli_rev == LPFC_SLI_REV4) {
10465 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10466 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10468 else
10469 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10470 iabt->ulpLe = 1;
10471 iabt->ulpClass = icmd->ulpClass;
10473 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10474 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
10475 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10476 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10477 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10478 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10480 if (phba->link_state >= LPFC_LINK_UP)
10481 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10482 else
10483 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10485 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10486 abtsiocbp->vport = vport;
10488 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10489 "0339 Abort xri x%x, original iotag x%x, "
10490 "abort cmd iotag x%x\n",
10491 iabt->un.acxri.abortIoTag,
10492 iabt->un.acxri.abortContextTag,
10493 abtsiocbp->iotag);
10495 if (phba->sli_rev == LPFC_SLI_REV4) {
10496 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10497 if (unlikely(pring == NULL))
10498 return 0;
10499 /* Note: both hbalock and ring_lock need to be set here */
10500 spin_lock_irqsave(&pring->ring_lock, iflags);
10501 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10502 abtsiocbp, 0);
10503 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10504 } else {
10505 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10506 abtsiocbp, 0);
10509 if (retval)
10510 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10513 * Caller to this routine should check for IOCB_ERROR
10514 * and handle it properly. This routine no longer removes
10515 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10517 return retval;
10521 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10522 * @phba: Pointer to HBA context object.
10523 * @pring: Pointer to driver SLI ring object.
10524 * @cmdiocb: Pointer to driver command iocb object.
10526 * This function issues an abort iocb for the provided command iocb. In case
10527 * of unloading, the abort iocb will not be issued to commands on the ELS
10528 * ring. Instead, the callback function shall be changed to those commands
10529 * so that nothing happens when them finishes. This function is called with
10530 * hbalock held. The function returns 0 when the command iocb is an abort
10531 * request.
10534 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10535 struct lpfc_iocbq *cmdiocb)
10537 struct lpfc_vport *vport = cmdiocb->vport;
10538 int retval = IOCB_ERROR;
10539 IOCB_t *icmd = NULL;
10541 lockdep_assert_held(&phba->hbalock);
10544 * There are certain command types we don't want to abort. And we
10545 * don't want to abort commands that are already in the process of
10546 * being aborted.
10548 icmd = &cmdiocb->iocb;
10549 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10550 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10551 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10552 return 0;
10555 * If we're unloading, don't abort iocb on the ELS ring, but change
10556 * the callback so that nothing happens when it finishes.
10558 if ((vport->load_flag & FC_UNLOADING) &&
10559 (pring->ringno == LPFC_ELS_RING)) {
10560 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10561 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10562 else
10563 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10564 goto abort_iotag_exit;
10567 /* Now, we try to issue the abort to the cmdiocb out */
10568 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10570 abort_iotag_exit:
10572 * Caller to this routine should check for IOCB_ERROR
10573 * and handle it properly. This routine no longer removes
10574 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10576 return retval;
10580 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10581 * @phba: Pointer to HBA context object.
10582 * @pring: Pointer to driver SLI ring object.
10583 * @cmdiocb: Pointer to driver command iocb object.
10585 * This function issues an abort iocb for the provided command iocb down to
10586 * the port. Other than the case the outstanding command iocb is an abort
10587 * request, this function issues abort out unconditionally. This function is
10588 * called with hbalock held. The function returns 0 when it fails due to
10589 * memory allocation failure or when the command iocb is an abort request.
10591 static int
10592 lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10593 struct lpfc_iocbq *cmdiocb)
10595 struct lpfc_vport *vport = cmdiocb->vport;
10596 struct lpfc_iocbq *abtsiocbp;
10597 union lpfc_wqe *abts_wqe;
10598 int retval;
10601 * There are certain command types we don't want to abort. And we
10602 * don't want to abort commands that are already in the process of
10603 * being aborted.
10605 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10606 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10607 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10608 return 0;
10610 /* issue ABTS for this io based on iotag */
10611 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10612 if (abtsiocbp == NULL)
10613 return 0;
10615 /* This signals the response to set the correct status
10616 * before calling the completion handler
10618 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10620 /* Complete prepping the abort wqe and issue to the FW. */
10621 abts_wqe = &abtsiocbp->wqe;
10622 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10623 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10625 /* Explicitly set reserved fields to zero.*/
10626 abts_wqe->abort_cmd.rsrvd4 = 0;
10627 abts_wqe->abort_cmd.rsrvd5 = 0;
10629 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10630 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10631 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10633 /* word 7 */
10634 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10635 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10636 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10637 cmdiocb->iocb.ulpClass);
10639 /* word 8 - tell the FW to abort the IO associated with this
10640 * outstanding exchange ID.
10642 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10644 /* word 9 - this is the iotag for the abts_wqe completion. */
10645 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10646 abtsiocbp->iotag);
10648 /* word 10 */
10649 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10650 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10651 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10653 /* word 11 */
10654 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10655 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10656 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10658 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10659 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10660 abtsiocbp->vport = vport;
10661 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
10662 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10663 if (retval == IOCB_ERROR) {
10664 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10665 "6147 Failed abts issue_wqe with status x%x "
10666 "for oxid x%x\n",
10667 retval, cmdiocb->sli4_xritag);
10668 lpfc_sli_release_iocbq(phba, abtsiocbp);
10669 return retval;
10672 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10673 "6148 Drv Abort NVME Request Issued for "
10674 "ox_id x%x on reqtag x%x\n",
10675 cmdiocb->sli4_xritag,
10676 abtsiocbp->iotag);
10678 return retval;
10682 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10683 * @phba: pointer to lpfc HBA data structure.
10685 * This routine will abort all pending and outstanding iocbs to an HBA.
10687 void
10688 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10690 struct lpfc_sli *psli = &phba->sli;
10691 struct lpfc_sli_ring *pring;
10692 struct lpfc_queue *qp = NULL;
10693 int i;
10695 if (phba->sli_rev != LPFC_SLI_REV4) {
10696 for (i = 0; i < psli->num_rings; i++) {
10697 pring = &psli->sli3_ring[i];
10698 lpfc_sli_abort_iocb_ring(phba, pring);
10700 return;
10702 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10703 pring = qp->pring;
10704 if (!pring)
10705 continue;
10706 lpfc_sli_abort_iocb_ring(phba, pring);
10711 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
10712 * @iocbq: Pointer to driver iocb object.
10713 * @vport: Pointer to driver virtual port object.
10714 * @tgt_id: SCSI ID of the target.
10715 * @lun_id: LUN ID of the scsi device.
10716 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10718 * This function acts as an iocb filter for functions which abort or count
10719 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10720 * 0 if the filtering criteria is met for the given iocb and will return
10721 * 1 if the filtering criteria is not met.
10722 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10723 * given iocb is for the SCSI device specified by vport, tgt_id and
10724 * lun_id parameter.
10725 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10726 * given iocb is for the SCSI target specified by vport and tgt_id
10727 * parameters.
10728 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10729 * given iocb is for the SCSI host associated with the given vport.
10730 * This function is called with no locks held.
10732 static int
10733 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10734 uint16_t tgt_id, uint64_t lun_id,
10735 lpfc_ctx_cmd ctx_cmd)
10737 struct lpfc_scsi_buf *lpfc_cmd;
10738 int rc = 1;
10740 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10741 return rc;
10743 if (iocbq->vport != vport)
10744 return rc;
10746 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10748 if (lpfc_cmd->pCmd == NULL)
10749 return rc;
10751 switch (ctx_cmd) {
10752 case LPFC_CTX_LUN:
10753 if ((lpfc_cmd->rdata->pnode) &&
10754 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10755 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
10756 rc = 0;
10757 break;
10758 case LPFC_CTX_TGT:
10759 if ((lpfc_cmd->rdata->pnode) &&
10760 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
10761 rc = 0;
10762 break;
10763 case LPFC_CTX_HOST:
10764 rc = 0;
10765 break;
10766 default:
10767 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
10768 __func__, ctx_cmd);
10769 break;
10772 return rc;
10776 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10777 * @vport: Pointer to virtual port.
10778 * @tgt_id: SCSI ID of the target.
10779 * @lun_id: LUN ID of the scsi device.
10780 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10782 * This function returns number of FCP commands pending for the vport.
10783 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10784 * commands pending on the vport associated with SCSI device specified
10785 * by tgt_id and lun_id parameters.
10786 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10787 * commands pending on the vport associated with SCSI target specified
10788 * by tgt_id parameter.
10789 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10790 * commands pending on the vport.
10791 * This function returns the number of iocbs which satisfy the filter.
10792 * This function is called without any lock held.
10795 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10796 lpfc_ctx_cmd ctx_cmd)
10798 struct lpfc_hba *phba = vport->phba;
10799 struct lpfc_iocbq *iocbq;
10800 int sum, i;
10802 spin_lock_irq(&phba->hbalock);
10803 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10804 iocbq = phba->sli.iocbq_lookup[i];
10806 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10807 ctx_cmd) == 0)
10808 sum++;
10810 spin_unlock_irq(&phba->hbalock);
10812 return sum;
10816 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10817 * @phba: Pointer to HBA context object
10818 * @cmdiocb: Pointer to command iocb object.
10819 * @rspiocb: Pointer to response iocb object.
10821 * This function is called when an aborted FCP iocb completes. This
10822 * function is called by the ring event handler with no lock held.
10823 * This function frees the iocb.
10825 void
10826 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10827 struct lpfc_iocbq *rspiocb)
10829 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10830 "3096 ABORT_XRI_CN completing on rpi x%x "
10831 "original iotag x%x, abort cmd iotag x%x "
10832 "status 0x%x, reason 0x%x\n",
10833 cmdiocb->iocb.un.acxri.abortContextTag,
10834 cmdiocb->iocb.un.acxri.abortIoTag,
10835 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10836 rspiocb->iocb.un.ulpWord[4]);
10837 lpfc_sli_release_iocbq(phba, cmdiocb);
10838 return;
10842 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10843 * @vport: Pointer to virtual port.
10844 * @pring: Pointer to driver SLI ring object.
10845 * @tgt_id: SCSI ID of the target.
10846 * @lun_id: LUN ID of the scsi device.
10847 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10849 * This function sends an abort command for every SCSI command
10850 * associated with the given virtual port pending on the ring
10851 * filtered by lpfc_sli_validate_fcp_iocb function.
10852 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10853 * FCP iocbs associated with lun specified by tgt_id and lun_id
10854 * parameters
10855 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10856 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10857 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10858 * FCP iocbs associated with virtual port.
10859 * This function returns number of iocbs it failed to abort.
10860 * This function is called with no locks held.
10863 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10864 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10866 struct lpfc_hba *phba = vport->phba;
10867 struct lpfc_iocbq *iocbq;
10868 struct lpfc_iocbq *abtsiocb;
10869 IOCB_t *cmd = NULL;
10870 int errcnt = 0, ret_val = 0;
10871 int i;
10873 for (i = 1; i <= phba->sli.last_iotag; i++) {
10874 iocbq = phba->sli.iocbq_lookup[i];
10876 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10877 abort_cmd) != 0)
10878 continue;
10881 * If the iocbq is already being aborted, don't take a second
10882 * action, but do count it.
10884 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10885 continue;
10887 /* issue ABTS for this IOCB based on iotag */
10888 abtsiocb = lpfc_sli_get_iocbq(phba);
10889 if (abtsiocb == NULL) {
10890 errcnt++;
10891 continue;
10894 /* indicate the IO is being aborted by the driver. */
10895 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10897 cmd = &iocbq->iocb;
10898 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10899 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10900 if (phba->sli_rev == LPFC_SLI_REV4)
10901 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10902 else
10903 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10904 abtsiocb->iocb.ulpLe = 1;
10905 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10906 abtsiocb->vport = vport;
10908 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10909 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
10910 if (iocbq->iocb_flag & LPFC_IO_FCP)
10911 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10912 if (iocbq->iocb_flag & LPFC_IO_FOF)
10913 abtsiocb->iocb_flag |= LPFC_IO_FOF;
10915 if (lpfc_is_link_up(phba))
10916 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10917 else
10918 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10920 /* Setup callback routine and issue the command. */
10921 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10922 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10923 abtsiocb, 0);
10924 if (ret_val == IOCB_ERROR) {
10925 lpfc_sli_release_iocbq(phba, abtsiocb);
10926 errcnt++;
10927 continue;
10931 return errcnt;
10935 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10936 * @vport: Pointer to virtual port.
10937 * @pring: Pointer to driver SLI ring object.
10938 * @tgt_id: SCSI ID of the target.
10939 * @lun_id: LUN ID of the scsi device.
10940 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10942 * This function sends an abort command for every SCSI command
10943 * associated with the given virtual port pending on the ring
10944 * filtered by lpfc_sli_validate_fcp_iocb function.
10945 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10946 * FCP iocbs associated with lun specified by tgt_id and lun_id
10947 * parameters
10948 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10949 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10950 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10951 * FCP iocbs associated with virtual port.
10952 * This function returns number of iocbs it aborted .
10953 * This function is called with no locks held right after a taskmgmt
10954 * command is sent.
10957 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10958 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10960 struct lpfc_hba *phba = vport->phba;
10961 struct lpfc_scsi_buf *lpfc_cmd;
10962 struct lpfc_iocbq *abtsiocbq;
10963 struct lpfc_nodelist *ndlp;
10964 struct lpfc_iocbq *iocbq;
10965 IOCB_t *icmd;
10966 int sum, i, ret_val;
10967 unsigned long iflags;
10968 struct lpfc_sli_ring *pring_s4;
10970 spin_lock_irq(&phba->hbalock);
10972 /* all I/Os are in process of being flushed */
10973 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10974 spin_unlock_irq(&phba->hbalock);
10975 return 0;
10977 sum = 0;
10979 for (i = 1; i <= phba->sli.last_iotag; i++) {
10980 iocbq = phba->sli.iocbq_lookup[i];
10982 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10983 cmd) != 0)
10984 continue;
10987 * If the iocbq is already being aborted, don't take a second
10988 * action, but do count it.
10990 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10991 continue;
10993 /* issue ABTS for this IOCB based on iotag */
10994 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10995 if (abtsiocbq == NULL)
10996 continue;
10998 icmd = &iocbq->iocb;
10999 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11000 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11001 if (phba->sli_rev == LPFC_SLI_REV4)
11002 abtsiocbq->iocb.un.acxri.abortIoTag =
11003 iocbq->sli4_xritag;
11004 else
11005 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11006 abtsiocbq->iocb.ulpLe = 1;
11007 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11008 abtsiocbq->vport = vport;
11010 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11011 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11012 if (iocbq->iocb_flag & LPFC_IO_FCP)
11013 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11014 if (iocbq->iocb_flag & LPFC_IO_FOF)
11015 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11017 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11018 ndlp = lpfc_cmd->rdata->pnode;
11020 if (lpfc_is_link_up(phba) &&
11021 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11022 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11023 else
11024 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11026 /* Setup callback routine and issue the command. */
11027 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11030 * Indicate the IO is being aborted by the driver and set
11031 * the caller's flag into the aborted IO.
11033 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11035 if (phba->sli_rev == LPFC_SLI_REV4) {
11036 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11037 if (pring_s4 == NULL)
11038 continue;
11039 /* Note: both hbalock and ring_lock must be set here */
11040 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11041 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11042 abtsiocbq, 0);
11043 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11044 } else {
11045 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11046 abtsiocbq, 0);
11050 if (ret_val == IOCB_ERROR)
11051 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11052 else
11053 sum++;
11055 spin_unlock_irq(&phba->hbalock);
11056 return sum;
11060 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11061 * @phba: Pointer to HBA context object.
11062 * @cmdiocbq: Pointer to command iocb.
11063 * @rspiocbq: Pointer to response iocb.
11065 * This function is the completion handler for iocbs issued using
11066 * lpfc_sli_issue_iocb_wait function. This function is called by the
11067 * ring event handler function without any lock held. This function
11068 * can be called from both worker thread context and interrupt
11069 * context. This function also can be called from other thread which
11070 * cleans up the SLI layer objects.
11071 * This function copy the contents of the response iocb to the
11072 * response iocb memory object provided by the caller of
11073 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11074 * sleeps for the iocb completion.
11076 static void
11077 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11078 struct lpfc_iocbq *cmdiocbq,
11079 struct lpfc_iocbq *rspiocbq)
11081 wait_queue_head_t *pdone_q;
11082 unsigned long iflags;
11083 struct lpfc_scsi_buf *lpfc_cmd;
11085 spin_lock_irqsave(&phba->hbalock, iflags);
11086 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11089 * A time out has occurred for the iocb. If a time out
11090 * completion handler has been supplied, call it. Otherwise,
11091 * just free the iocbq.
11094 spin_unlock_irqrestore(&phba->hbalock, iflags);
11095 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11096 cmdiocbq->wait_iocb_cmpl = NULL;
11097 if (cmdiocbq->iocb_cmpl)
11098 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11099 else
11100 lpfc_sli_release_iocbq(phba, cmdiocbq);
11101 return;
11104 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11105 if (cmdiocbq->context2 && rspiocbq)
11106 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11107 &rspiocbq->iocb, sizeof(IOCB_t));
11109 /* Set the exchange busy flag for task management commands */
11110 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11111 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11112 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11113 cur_iocbq);
11114 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11117 pdone_q = cmdiocbq->context_un.wait_queue;
11118 if (pdone_q)
11119 wake_up(pdone_q);
11120 spin_unlock_irqrestore(&phba->hbalock, iflags);
11121 return;
11125 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11126 * @phba: Pointer to HBA context object..
11127 * @piocbq: Pointer to command iocb.
11128 * @flag: Flag to test.
11130 * This routine grabs the hbalock and then test the iocb_flag to
11131 * see if the passed in flag is set.
11132 * Returns:
11133 * 1 if flag is set.
11134 * 0 if flag is not set.
11136 static int
11137 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11138 struct lpfc_iocbq *piocbq, uint32_t flag)
11140 unsigned long iflags;
11141 int ret;
11143 spin_lock_irqsave(&phba->hbalock, iflags);
11144 ret = piocbq->iocb_flag & flag;
11145 spin_unlock_irqrestore(&phba->hbalock, iflags);
11146 return ret;
11151 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11152 * @phba: Pointer to HBA context object..
11153 * @pring: Pointer to sli ring.
11154 * @piocb: Pointer to command iocb.
11155 * @prspiocbq: Pointer to response iocb.
11156 * @timeout: Timeout in number of seconds.
11158 * This function issues the iocb to firmware and waits for the
11159 * iocb to complete. The iocb_cmpl field of the shall be used
11160 * to handle iocbs which time out. If the field is NULL, the
11161 * function shall free the iocbq structure. If more clean up is
11162 * needed, the caller is expected to provide a completion function
11163 * that will provide the needed clean up. If the iocb command is
11164 * not completed within timeout seconds, the function will either
11165 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11166 * completion function set in the iocb_cmpl field and then return
11167 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11168 * resources if this function returns IOCB_TIMEDOUT.
11169 * The function waits for the iocb completion using an
11170 * non-interruptible wait.
11171 * This function will sleep while waiting for iocb completion.
11172 * So, this function should not be called from any context which
11173 * does not allow sleeping. Due to the same reason, this function
11174 * cannot be called with interrupt disabled.
11175 * This function assumes that the iocb completions occur while
11176 * this function sleep. So, this function cannot be called from
11177 * the thread which process iocb completion for this ring.
11178 * This function clears the iocb_flag of the iocb object before
11179 * issuing the iocb and the iocb completion handler sets this
11180 * flag and wakes this thread when the iocb completes.
11181 * The contents of the response iocb will be copied to prspiocbq
11182 * by the completion handler when the command completes.
11183 * This function returns IOCB_SUCCESS when success.
11184 * This function is called with no lock held.
11187 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11188 uint32_t ring_number,
11189 struct lpfc_iocbq *piocb,
11190 struct lpfc_iocbq *prspiocbq,
11191 uint32_t timeout)
11193 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11194 long timeleft, timeout_req = 0;
11195 int retval = IOCB_SUCCESS;
11196 uint32_t creg_val;
11197 struct lpfc_iocbq *iocb;
11198 int txq_cnt = 0;
11199 int txcmplq_cnt = 0;
11200 struct lpfc_sli_ring *pring;
11201 unsigned long iflags;
11202 bool iocb_completed = true;
11204 if (phba->sli_rev >= LPFC_SLI_REV4)
11205 pring = lpfc_sli4_calc_ring(phba, piocb);
11206 else
11207 pring = &phba->sli.sli3_ring[ring_number];
11209 * If the caller has provided a response iocbq buffer, then context2
11210 * is NULL or its an error.
11212 if (prspiocbq) {
11213 if (piocb->context2)
11214 return IOCB_ERROR;
11215 piocb->context2 = prspiocbq;
11218 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11219 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11220 piocb->context_un.wait_queue = &done_q;
11221 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11223 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11224 if (lpfc_readl(phba->HCregaddr, &creg_val))
11225 return IOCB_ERROR;
11226 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11227 writel(creg_val, phba->HCregaddr);
11228 readl(phba->HCregaddr); /* flush */
11231 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11232 SLI_IOCB_RET_IOCB);
11233 if (retval == IOCB_SUCCESS) {
11234 timeout_req = msecs_to_jiffies(timeout * 1000);
11235 timeleft = wait_event_timeout(done_q,
11236 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11237 timeout_req);
11238 spin_lock_irqsave(&phba->hbalock, iflags);
11239 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11242 * IOCB timed out. Inform the wake iocb wait
11243 * completion function and set local status
11246 iocb_completed = false;
11247 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11249 spin_unlock_irqrestore(&phba->hbalock, iflags);
11250 if (iocb_completed) {
11251 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11252 "0331 IOCB wake signaled\n");
11253 /* Note: we are not indicating if the IOCB has a success
11254 * status or not - that's for the caller to check.
11255 * IOCB_SUCCESS means just that the command was sent and
11256 * completed. Not that it completed successfully.
11257 * */
11258 } else if (timeleft == 0) {
11259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11260 "0338 IOCB wait timeout error - no "
11261 "wake response Data x%x\n", timeout);
11262 retval = IOCB_TIMEDOUT;
11263 } else {
11264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11265 "0330 IOCB wake NOT set, "
11266 "Data x%x x%lx\n",
11267 timeout, (timeleft / jiffies));
11268 retval = IOCB_TIMEDOUT;
11270 } else if (retval == IOCB_BUSY) {
11271 if (phba->cfg_log_verbose & LOG_SLI) {
11272 list_for_each_entry(iocb, &pring->txq, list) {
11273 txq_cnt++;
11275 list_for_each_entry(iocb, &pring->txcmplq, list) {
11276 txcmplq_cnt++;
11278 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11279 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11280 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11282 return retval;
11283 } else {
11284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11285 "0332 IOCB wait issue failed, Data x%x\n",
11286 retval);
11287 retval = IOCB_ERROR;
11290 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11291 if (lpfc_readl(phba->HCregaddr, &creg_val))
11292 return IOCB_ERROR;
11293 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11294 writel(creg_val, phba->HCregaddr);
11295 readl(phba->HCregaddr); /* flush */
11298 if (prspiocbq)
11299 piocb->context2 = NULL;
11301 piocb->context_un.wait_queue = NULL;
11302 piocb->iocb_cmpl = NULL;
11303 return retval;
11307 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11308 * @phba: Pointer to HBA context object.
11309 * @pmboxq: Pointer to driver mailbox object.
11310 * @timeout: Timeout in number of seconds.
11312 * This function issues the mailbox to firmware and waits for the
11313 * mailbox command to complete. If the mailbox command is not
11314 * completed within timeout seconds, it returns MBX_TIMEOUT.
11315 * The function waits for the mailbox completion using an
11316 * interruptible wait. If the thread is woken up due to a
11317 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11318 * should not free the mailbox resources, if this function returns
11319 * MBX_TIMEOUT.
11320 * This function will sleep while waiting for mailbox completion.
11321 * So, this function should not be called from any context which
11322 * does not allow sleeping. Due to the same reason, this function
11323 * cannot be called with interrupt disabled.
11324 * This function assumes that the mailbox completion occurs while
11325 * this function sleep. So, this function cannot be called from
11326 * the worker thread which processes mailbox completion.
11327 * This function is called in the context of HBA management
11328 * applications.
11329 * This function returns MBX_SUCCESS when successful.
11330 * This function is called with no lock held.
11333 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
11334 uint32_t timeout)
11336 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11337 MAILBOX_t *mb = NULL;
11338 int retval;
11339 unsigned long flag;
11341 /* The caller might set context1 for extended buffer */
11342 if (pmboxq->context1)
11343 mb = (MAILBOX_t *)pmboxq->context1;
11345 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
11346 /* setup wake call as IOCB callback */
11347 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11348 /* setup context field to pass wait_queue pointer to wake function */
11349 pmboxq->context1 = &done_q;
11351 /* now issue the command */
11352 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
11353 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
11354 wait_event_interruptible_timeout(done_q,
11355 pmboxq->mbox_flag & LPFC_MBX_WAKE,
11356 msecs_to_jiffies(timeout * 1000));
11358 spin_lock_irqsave(&phba->hbalock, flag);
11359 /* restore the possible extended buffer for free resource */
11360 pmboxq->context1 = (uint8_t *)mb;
11362 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11363 * else do not free the resources.
11365 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
11366 retval = MBX_SUCCESS;
11367 } else {
11368 retval = MBX_TIMEOUT;
11369 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11371 spin_unlock_irqrestore(&phba->hbalock, flag);
11372 } else {
11373 /* restore the possible extended buffer for free resource */
11374 pmboxq->context1 = (uint8_t *)mb;
11377 return retval;
11381 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
11382 * @phba: Pointer to HBA context.
11384 * This function is called to shutdown the driver's mailbox sub-system.
11385 * It first marks the mailbox sub-system is in a block state to prevent
11386 * the asynchronous mailbox command from issued off the pending mailbox
11387 * command queue. If the mailbox command sub-system shutdown is due to
11388 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11389 * the mailbox sub-system flush routine to forcefully bring down the
11390 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11391 * as with offline or HBA function reset), this routine will wait for the
11392 * outstanding mailbox command to complete before invoking the mailbox
11393 * sub-system flush routine to gracefully bring down mailbox sub-system.
11395 void
11396 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
11398 struct lpfc_sli *psli = &phba->sli;
11399 unsigned long timeout;
11401 if (mbx_action == LPFC_MBX_NO_WAIT) {
11402 /* delay 100ms for port state */
11403 msleep(100);
11404 lpfc_sli_mbox_sys_flush(phba);
11405 return;
11407 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
11409 spin_lock_irq(&phba->hbalock);
11410 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11412 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
11413 /* Determine how long we might wait for the active mailbox
11414 * command to be gracefully completed by firmware.
11416 if (phba->sli.mbox_active)
11417 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11418 phba->sli.mbox_active) *
11419 1000) + jiffies;
11420 spin_unlock_irq(&phba->hbalock);
11422 while (phba->sli.mbox_active) {
11423 /* Check active mailbox complete status every 2ms */
11424 msleep(2);
11425 if (time_after(jiffies, timeout))
11426 /* Timeout, let the mailbox flush routine to
11427 * forcefully release active mailbox command
11429 break;
11431 } else
11432 spin_unlock_irq(&phba->hbalock);
11434 lpfc_sli_mbox_sys_flush(phba);
11438 * lpfc_sli_eratt_read - read sli-3 error attention events
11439 * @phba: Pointer to HBA context.
11441 * This function is called to read the SLI3 device error attention registers
11442 * for possible error attention events. The caller must hold the hostlock
11443 * with spin_lock_irq().
11445 * This function returns 1 when there is Error Attention in the Host Attention
11446 * Register and returns 0 otherwise.
11448 static int
11449 lpfc_sli_eratt_read(struct lpfc_hba *phba)
11451 uint32_t ha_copy;
11453 /* Read chip Host Attention (HA) register */
11454 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11455 goto unplug_err;
11457 if (ha_copy & HA_ERATT) {
11458 /* Read host status register to retrieve error event */
11459 if (lpfc_sli_read_hs(phba))
11460 goto unplug_err;
11462 /* Check if there is a deferred error condition is active */
11463 if ((HS_FFER1 & phba->work_hs) &&
11464 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11465 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
11466 phba->hba_flag |= DEFER_ERATT;
11467 /* Clear all interrupt enable conditions */
11468 writel(0, phba->HCregaddr);
11469 readl(phba->HCregaddr);
11472 /* Set the driver HA work bitmap */
11473 phba->work_ha |= HA_ERATT;
11474 /* Indicate polling handles this ERATT */
11475 phba->hba_flag |= HBA_ERATT_HANDLED;
11476 return 1;
11478 return 0;
11480 unplug_err:
11481 /* Set the driver HS work bitmap */
11482 phba->work_hs |= UNPLUG_ERR;
11483 /* Set the driver HA work bitmap */
11484 phba->work_ha |= HA_ERATT;
11485 /* Indicate polling handles this ERATT */
11486 phba->hba_flag |= HBA_ERATT_HANDLED;
11487 return 1;
11491 * lpfc_sli4_eratt_read - read sli-4 error attention events
11492 * @phba: Pointer to HBA context.
11494 * This function is called to read the SLI4 device error attention registers
11495 * for possible error attention events. The caller must hold the hostlock
11496 * with spin_lock_irq().
11498 * This function returns 1 when there is Error Attention in the Host Attention
11499 * Register and returns 0 otherwise.
11501 static int
11502 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11504 uint32_t uerr_sta_hi, uerr_sta_lo;
11505 uint32_t if_type, portsmphr;
11506 struct lpfc_register portstat_reg;
11509 * For now, use the SLI4 device internal unrecoverable error
11510 * registers for error attention. This can be changed later.
11512 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11513 switch (if_type) {
11514 case LPFC_SLI_INTF_IF_TYPE_0:
11515 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11516 &uerr_sta_lo) ||
11517 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11518 &uerr_sta_hi)) {
11519 phba->work_hs |= UNPLUG_ERR;
11520 phba->work_ha |= HA_ERATT;
11521 phba->hba_flag |= HBA_ERATT_HANDLED;
11522 return 1;
11524 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11525 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11527 "1423 HBA Unrecoverable error: "
11528 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11529 "ue_mask_lo_reg=0x%x, "
11530 "ue_mask_hi_reg=0x%x\n",
11531 uerr_sta_lo, uerr_sta_hi,
11532 phba->sli4_hba.ue_mask_lo,
11533 phba->sli4_hba.ue_mask_hi);
11534 phba->work_status[0] = uerr_sta_lo;
11535 phba->work_status[1] = uerr_sta_hi;
11536 phba->work_ha |= HA_ERATT;
11537 phba->hba_flag |= HBA_ERATT_HANDLED;
11538 return 1;
11540 break;
11541 case LPFC_SLI_INTF_IF_TYPE_2:
11542 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11543 &portstat_reg.word0) ||
11544 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11545 &portsmphr)){
11546 phba->work_hs |= UNPLUG_ERR;
11547 phba->work_ha |= HA_ERATT;
11548 phba->hba_flag |= HBA_ERATT_HANDLED;
11549 return 1;
11551 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11552 phba->work_status[0] =
11553 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11554 phba->work_status[1] =
11555 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11557 "2885 Port Status Event: "
11558 "port status reg 0x%x, "
11559 "port smphr reg 0x%x, "
11560 "error 1=0x%x, error 2=0x%x\n",
11561 portstat_reg.word0,
11562 portsmphr,
11563 phba->work_status[0],
11564 phba->work_status[1]);
11565 phba->work_ha |= HA_ERATT;
11566 phba->hba_flag |= HBA_ERATT_HANDLED;
11567 return 1;
11569 break;
11570 case LPFC_SLI_INTF_IF_TYPE_1:
11571 default:
11572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11573 "2886 HBA Error Attention on unsupported "
11574 "if type %d.", if_type);
11575 return 1;
11578 return 0;
11582 * lpfc_sli_check_eratt - check error attention events
11583 * @phba: Pointer to HBA context.
11585 * This function is called from timer soft interrupt context to check HBA's
11586 * error attention register bit for error attention events.
11588 * This function returns 1 when there is Error Attention in the Host Attention
11589 * Register and returns 0 otherwise.
11592 lpfc_sli_check_eratt(struct lpfc_hba *phba)
11594 uint32_t ha_copy;
11596 /* If somebody is waiting to handle an eratt, don't process it
11597 * here. The brdkill function will do this.
11599 if (phba->link_flag & LS_IGNORE_ERATT)
11600 return 0;
11602 /* Check if interrupt handler handles this ERATT */
11603 spin_lock_irq(&phba->hbalock);
11604 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11605 /* Interrupt handler has handled ERATT */
11606 spin_unlock_irq(&phba->hbalock);
11607 return 0;
11611 * If there is deferred error attention, do not check for error
11612 * attention
11614 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11615 spin_unlock_irq(&phba->hbalock);
11616 return 0;
11619 /* If PCI channel is offline, don't process it */
11620 if (unlikely(pci_channel_offline(phba->pcidev))) {
11621 spin_unlock_irq(&phba->hbalock);
11622 return 0;
11625 switch (phba->sli_rev) {
11626 case LPFC_SLI_REV2:
11627 case LPFC_SLI_REV3:
11628 /* Read chip Host Attention (HA) register */
11629 ha_copy = lpfc_sli_eratt_read(phba);
11630 break;
11631 case LPFC_SLI_REV4:
11632 /* Read device Uncoverable Error (UERR) registers */
11633 ha_copy = lpfc_sli4_eratt_read(phba);
11634 break;
11635 default:
11636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11637 "0299 Invalid SLI revision (%d)\n",
11638 phba->sli_rev);
11639 ha_copy = 0;
11640 break;
11642 spin_unlock_irq(&phba->hbalock);
11644 return ha_copy;
11648 * lpfc_intr_state_check - Check device state for interrupt handling
11649 * @phba: Pointer to HBA context.
11651 * This inline routine checks whether a device or its PCI slot is in a state
11652 * that the interrupt should be handled.
11654 * This function returns 0 if the device or the PCI slot is in a state that
11655 * interrupt should be handled, otherwise -EIO.
11657 static inline int
11658 lpfc_intr_state_check(struct lpfc_hba *phba)
11660 /* If the pci channel is offline, ignore all the interrupts */
11661 if (unlikely(pci_channel_offline(phba->pcidev)))
11662 return -EIO;
11664 /* Update device level interrupt statistics */
11665 phba->sli.slistat.sli_intr++;
11667 /* Ignore all interrupts during initialization. */
11668 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11669 return -EIO;
11671 return 0;
11675 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
11676 * @irq: Interrupt number.
11677 * @dev_id: The device context pointer.
11679 * This function is directly called from the PCI layer as an interrupt
11680 * service routine when device with SLI-3 interface spec is enabled with
11681 * MSI-X multi-message interrupt mode and there are slow-path events in
11682 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11683 * interrupt mode, this function is called as part of the device-level
11684 * interrupt handler. When the PCI slot is in error recovery or the HBA
11685 * is undergoing initialization, the interrupt handler will not process
11686 * the interrupt. The link attention and ELS ring attention events are
11687 * handled by the worker thread. The interrupt handler signals the worker
11688 * thread and returns for these events. This function is called without
11689 * any lock held. It gets the hbalock to access and update SLI data
11690 * structures.
11692 * This function returns IRQ_HANDLED when interrupt is handled else it
11693 * returns IRQ_NONE.
11695 irqreturn_t
11696 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
11698 struct lpfc_hba *phba;
11699 uint32_t ha_copy, hc_copy;
11700 uint32_t work_ha_copy;
11701 unsigned long status;
11702 unsigned long iflag;
11703 uint32_t control;
11705 MAILBOX_t *mbox, *pmbox;
11706 struct lpfc_vport *vport;
11707 struct lpfc_nodelist *ndlp;
11708 struct lpfc_dmabuf *mp;
11709 LPFC_MBOXQ_t *pmb;
11710 int rc;
11713 * Get the driver's phba structure from the dev_id and
11714 * assume the HBA is not interrupting.
11716 phba = (struct lpfc_hba *)dev_id;
11718 if (unlikely(!phba))
11719 return IRQ_NONE;
11722 * Stuff needs to be attented to when this function is invoked as an
11723 * individual interrupt handler in MSI-X multi-message interrupt mode
11725 if (phba->intr_type == MSIX) {
11726 /* Check device state for handling interrupt */
11727 if (lpfc_intr_state_check(phba))
11728 return IRQ_NONE;
11729 /* Need to read HA REG for slow-path events */
11730 spin_lock_irqsave(&phba->hbalock, iflag);
11731 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11732 goto unplug_error;
11733 /* If somebody is waiting to handle an eratt don't process it
11734 * here. The brdkill function will do this.
11736 if (phba->link_flag & LS_IGNORE_ERATT)
11737 ha_copy &= ~HA_ERATT;
11738 /* Check the need for handling ERATT in interrupt handler */
11739 if (ha_copy & HA_ERATT) {
11740 if (phba->hba_flag & HBA_ERATT_HANDLED)
11741 /* ERATT polling has handled ERATT */
11742 ha_copy &= ~HA_ERATT;
11743 else
11744 /* Indicate interrupt handler handles ERATT */
11745 phba->hba_flag |= HBA_ERATT_HANDLED;
11749 * If there is deferred error attention, do not check for any
11750 * interrupt.
11752 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11753 spin_unlock_irqrestore(&phba->hbalock, iflag);
11754 return IRQ_NONE;
11757 /* Clear up only attention source related to slow-path */
11758 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11759 goto unplug_error;
11761 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11762 HC_LAINT_ENA | HC_ERINT_ENA),
11763 phba->HCregaddr);
11764 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11765 phba->HAregaddr);
11766 writel(hc_copy, phba->HCregaddr);
11767 readl(phba->HAregaddr); /* flush */
11768 spin_unlock_irqrestore(&phba->hbalock, iflag);
11769 } else
11770 ha_copy = phba->ha_copy;
11772 work_ha_copy = ha_copy & phba->work_ha_mask;
11774 if (work_ha_copy) {
11775 if (work_ha_copy & HA_LATT) {
11776 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11778 * Turn off Link Attention interrupts
11779 * until CLEAR_LA done
11781 spin_lock_irqsave(&phba->hbalock, iflag);
11782 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11783 if (lpfc_readl(phba->HCregaddr, &control))
11784 goto unplug_error;
11785 control &= ~HC_LAINT_ENA;
11786 writel(control, phba->HCregaddr);
11787 readl(phba->HCregaddr); /* flush */
11788 spin_unlock_irqrestore(&phba->hbalock, iflag);
11790 else
11791 work_ha_copy &= ~HA_LATT;
11794 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11796 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11797 * the only slow ring.
11799 status = (work_ha_copy &
11800 (HA_RXMASK << (4*LPFC_ELS_RING)));
11801 status >>= (4*LPFC_ELS_RING);
11802 if (status & HA_RXMASK) {
11803 spin_lock_irqsave(&phba->hbalock, iflag);
11804 if (lpfc_readl(phba->HCregaddr, &control))
11805 goto unplug_error;
11807 lpfc_debugfs_slow_ring_trc(phba,
11808 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11809 control, status,
11810 (uint32_t)phba->sli.slistat.sli_intr);
11812 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11813 lpfc_debugfs_slow_ring_trc(phba,
11814 "ISR Disable ring:"
11815 "pwork:x%x hawork:x%x wait:x%x",
11816 phba->work_ha, work_ha_copy,
11817 (uint32_t)((unsigned long)
11818 &phba->work_waitq));
11820 control &=
11821 ~(HC_R0INT_ENA << LPFC_ELS_RING);
11822 writel(control, phba->HCregaddr);
11823 readl(phba->HCregaddr); /* flush */
11825 else {
11826 lpfc_debugfs_slow_ring_trc(phba,
11827 "ISR slow ring: pwork:"
11828 "x%x hawork:x%x wait:x%x",
11829 phba->work_ha, work_ha_copy,
11830 (uint32_t)((unsigned long)
11831 &phba->work_waitq));
11833 spin_unlock_irqrestore(&phba->hbalock, iflag);
11836 spin_lock_irqsave(&phba->hbalock, iflag);
11837 if (work_ha_copy & HA_ERATT) {
11838 if (lpfc_sli_read_hs(phba))
11839 goto unplug_error;
11841 * Check if there is a deferred error condition
11842 * is active
11844 if ((HS_FFER1 & phba->work_hs) &&
11845 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11846 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11847 phba->work_hs)) {
11848 phba->hba_flag |= DEFER_ERATT;
11849 /* Clear all interrupt enable conditions */
11850 writel(0, phba->HCregaddr);
11851 readl(phba->HCregaddr);
11855 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11856 pmb = phba->sli.mbox_active;
11857 pmbox = &pmb->u.mb;
11858 mbox = phba->mbox;
11859 vport = pmb->vport;
11861 /* First check out the status word */
11862 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11863 if (pmbox->mbxOwner != OWN_HOST) {
11864 spin_unlock_irqrestore(&phba->hbalock, iflag);
11866 * Stray Mailbox Interrupt, mbxCommand <cmd>
11867 * mbxStatus <status>
11869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11870 LOG_SLI,
11871 "(%d):0304 Stray Mailbox "
11872 "Interrupt mbxCommand x%x "
11873 "mbxStatus x%x\n",
11874 (vport ? vport->vpi : 0),
11875 pmbox->mbxCommand,
11876 pmbox->mbxStatus);
11877 /* clear mailbox attention bit */
11878 work_ha_copy &= ~HA_MBATT;
11879 } else {
11880 phba->sli.mbox_active = NULL;
11881 spin_unlock_irqrestore(&phba->hbalock, iflag);
11882 phba->last_completion_time = jiffies;
11883 del_timer(&phba->sli.mbox_tmo);
11884 if (pmb->mbox_cmpl) {
11885 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11886 MAILBOX_CMD_SIZE);
11887 if (pmb->out_ext_byte_len &&
11888 pmb->context2)
11889 lpfc_sli_pcimem_bcopy(
11890 phba->mbox_ext,
11891 pmb->context2,
11892 pmb->out_ext_byte_len);
11894 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11895 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11897 lpfc_debugfs_disc_trc(vport,
11898 LPFC_DISC_TRC_MBOX_VPORT,
11899 "MBOX dflt rpi: : "
11900 "status:x%x rpi:x%x",
11901 (uint32_t)pmbox->mbxStatus,
11902 pmbox->un.varWords[0], 0);
11904 if (!pmbox->mbxStatus) {
11905 mp = (struct lpfc_dmabuf *)
11906 (pmb->context1);
11907 ndlp = (struct lpfc_nodelist *)
11908 pmb->context2;
11910 /* Reg_LOGIN of dflt RPI was
11911 * successful. new lets get
11912 * rid of the RPI using the
11913 * same mbox buffer.
11915 lpfc_unreg_login(phba,
11916 vport->vpi,
11917 pmbox->un.varWords[0],
11918 pmb);
11919 pmb->mbox_cmpl =
11920 lpfc_mbx_cmpl_dflt_rpi;
11921 pmb->context1 = mp;
11922 pmb->context2 = ndlp;
11923 pmb->vport = vport;
11924 rc = lpfc_sli_issue_mbox(phba,
11925 pmb,
11926 MBX_NOWAIT);
11927 if (rc != MBX_BUSY)
11928 lpfc_printf_log(phba,
11929 KERN_ERR,
11930 LOG_MBOX | LOG_SLI,
11931 "0350 rc should have"
11932 "been MBX_BUSY\n");
11933 if (rc != MBX_NOT_FINISHED)
11934 goto send_current_mbox;
11937 spin_lock_irqsave(
11938 &phba->pport->work_port_lock,
11939 iflag);
11940 phba->pport->work_port_events &=
11941 ~WORKER_MBOX_TMO;
11942 spin_unlock_irqrestore(
11943 &phba->pport->work_port_lock,
11944 iflag);
11945 lpfc_mbox_cmpl_put(phba, pmb);
11947 } else
11948 spin_unlock_irqrestore(&phba->hbalock, iflag);
11950 if ((work_ha_copy & HA_MBATT) &&
11951 (phba->sli.mbox_active == NULL)) {
11952 send_current_mbox:
11953 /* Process next mailbox command if there is one */
11954 do {
11955 rc = lpfc_sli_issue_mbox(phba, NULL,
11956 MBX_NOWAIT);
11957 } while (rc == MBX_NOT_FINISHED);
11958 if (rc != MBX_SUCCESS)
11959 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11960 LOG_SLI, "0349 rc should be "
11961 "MBX_SUCCESS\n");
11964 spin_lock_irqsave(&phba->hbalock, iflag);
11965 phba->work_ha |= work_ha_copy;
11966 spin_unlock_irqrestore(&phba->hbalock, iflag);
11967 lpfc_worker_wake_up(phba);
11969 return IRQ_HANDLED;
11970 unplug_error:
11971 spin_unlock_irqrestore(&phba->hbalock, iflag);
11972 return IRQ_HANDLED;
11974 } /* lpfc_sli_sp_intr_handler */
11977 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11978 * @irq: Interrupt number.
11979 * @dev_id: The device context pointer.
11981 * This function is directly called from the PCI layer as an interrupt
11982 * service routine when device with SLI-3 interface spec is enabled with
11983 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11984 * ring event in the HBA. However, when the device is enabled with either
11985 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11986 * device-level interrupt handler. When the PCI slot is in error recovery
11987 * or the HBA is undergoing initialization, the interrupt handler will not
11988 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11989 * the intrrupt context. This function is called without any lock held.
11990 * It gets the hbalock to access and update SLI data structures.
11992 * This function returns IRQ_HANDLED when interrupt is handled else it
11993 * returns IRQ_NONE.
11995 irqreturn_t
11996 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
11998 struct lpfc_hba *phba;
11999 uint32_t ha_copy;
12000 unsigned long status;
12001 unsigned long iflag;
12002 struct lpfc_sli_ring *pring;
12004 /* Get the driver's phba structure from the dev_id and
12005 * assume the HBA is not interrupting.
12007 phba = (struct lpfc_hba *) dev_id;
12009 if (unlikely(!phba))
12010 return IRQ_NONE;
12013 * Stuff needs to be attented to when this function is invoked as an
12014 * individual interrupt handler in MSI-X multi-message interrupt mode
12016 if (phba->intr_type == MSIX) {
12017 /* Check device state for handling interrupt */
12018 if (lpfc_intr_state_check(phba))
12019 return IRQ_NONE;
12020 /* Need to read HA REG for FCP ring and other ring events */
12021 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12022 return IRQ_HANDLED;
12023 /* Clear up only attention source related to fast-path */
12024 spin_lock_irqsave(&phba->hbalock, iflag);
12026 * If there is deferred error attention, do not check for
12027 * any interrupt.
12029 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12030 spin_unlock_irqrestore(&phba->hbalock, iflag);
12031 return IRQ_NONE;
12033 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12034 phba->HAregaddr);
12035 readl(phba->HAregaddr); /* flush */
12036 spin_unlock_irqrestore(&phba->hbalock, iflag);
12037 } else
12038 ha_copy = phba->ha_copy;
12041 * Process all events on FCP ring. Take the optimized path for FCP IO.
12043 ha_copy &= ~(phba->work_ha_mask);
12045 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12046 status >>= (4*LPFC_FCP_RING);
12047 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12048 if (status & HA_RXMASK)
12049 lpfc_sli_handle_fast_ring_event(phba, pring, status);
12051 if (phba->cfg_multi_ring_support == 2) {
12053 * Process all events on extra ring. Take the optimized path
12054 * for extra ring IO.
12056 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12057 status >>= (4*LPFC_EXTRA_RING);
12058 if (status & HA_RXMASK) {
12059 lpfc_sli_handle_fast_ring_event(phba,
12060 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12061 status);
12064 return IRQ_HANDLED;
12065 } /* lpfc_sli_fp_intr_handler */
12068 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12069 * @irq: Interrupt number.
12070 * @dev_id: The device context pointer.
12072 * This function is the HBA device-level interrupt handler to device with
12073 * SLI-3 interface spec, called from the PCI layer when either MSI or
12074 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12075 * requires driver attention. This function invokes the slow-path interrupt
12076 * attention handling function and fast-path interrupt attention handling
12077 * function in turn to process the relevant HBA attention events. This
12078 * function is called without any lock held. It gets the hbalock to access
12079 * and update SLI data structures.
12081 * This function returns IRQ_HANDLED when interrupt is handled, else it
12082 * returns IRQ_NONE.
12084 irqreturn_t
12085 lpfc_sli_intr_handler(int irq, void *dev_id)
12087 struct lpfc_hba *phba;
12088 irqreturn_t sp_irq_rc, fp_irq_rc;
12089 unsigned long status1, status2;
12090 uint32_t hc_copy;
12093 * Get the driver's phba structure from the dev_id and
12094 * assume the HBA is not interrupting.
12096 phba = (struct lpfc_hba *) dev_id;
12098 if (unlikely(!phba))
12099 return IRQ_NONE;
12101 /* Check device state for handling interrupt */
12102 if (lpfc_intr_state_check(phba))
12103 return IRQ_NONE;
12105 spin_lock(&phba->hbalock);
12106 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12107 spin_unlock(&phba->hbalock);
12108 return IRQ_HANDLED;
12111 if (unlikely(!phba->ha_copy)) {
12112 spin_unlock(&phba->hbalock);
12113 return IRQ_NONE;
12114 } else if (phba->ha_copy & HA_ERATT) {
12115 if (phba->hba_flag & HBA_ERATT_HANDLED)
12116 /* ERATT polling has handled ERATT */
12117 phba->ha_copy &= ~HA_ERATT;
12118 else
12119 /* Indicate interrupt handler handles ERATT */
12120 phba->hba_flag |= HBA_ERATT_HANDLED;
12124 * If there is deferred error attention, do not check for any interrupt.
12126 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12127 spin_unlock(&phba->hbalock);
12128 return IRQ_NONE;
12131 /* Clear attention sources except link and error attentions */
12132 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12133 spin_unlock(&phba->hbalock);
12134 return IRQ_HANDLED;
12136 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12137 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12138 phba->HCregaddr);
12139 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12140 writel(hc_copy, phba->HCregaddr);
12141 readl(phba->HAregaddr); /* flush */
12142 spin_unlock(&phba->hbalock);
12145 * Invokes slow-path host attention interrupt handling as appropriate.
12148 /* status of events with mailbox and link attention */
12149 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12151 /* status of events with ELS ring */
12152 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12153 status2 >>= (4*LPFC_ELS_RING);
12155 if (status1 || (status2 & HA_RXMASK))
12156 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12157 else
12158 sp_irq_rc = IRQ_NONE;
12161 * Invoke fast-path host attention interrupt handling as appropriate.
12164 /* status of events with FCP ring */
12165 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12166 status1 >>= (4*LPFC_FCP_RING);
12168 /* status of events with extra ring */
12169 if (phba->cfg_multi_ring_support == 2) {
12170 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12171 status2 >>= (4*LPFC_EXTRA_RING);
12172 } else
12173 status2 = 0;
12175 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12176 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12177 else
12178 fp_irq_rc = IRQ_NONE;
12180 /* Return device-level interrupt handling status */
12181 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12182 } /* lpfc_sli_intr_handler */
12185 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12186 * @phba: pointer to lpfc hba data structure.
12188 * This routine is invoked by the worker thread to process all the pending
12189 * SLI4 FCP abort XRI events.
12191 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12193 struct lpfc_cq_event *cq_event;
12195 /* First, declare the fcp xri abort event has been handled */
12196 spin_lock_irq(&phba->hbalock);
12197 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12198 spin_unlock_irq(&phba->hbalock);
12199 /* Now, handle all the fcp xri abort events */
12200 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12201 /* Get the first event from the head of the event queue */
12202 spin_lock_irq(&phba->hbalock);
12203 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12204 cq_event, struct lpfc_cq_event, list);
12205 spin_unlock_irq(&phba->hbalock);
12206 /* Notify aborted XRI for FCP work queue */
12207 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12208 /* Free the event processed back to the free pool */
12209 lpfc_sli4_cq_event_release(phba, cq_event);
12214 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12215 * @phba: pointer to lpfc hba data structure.
12217 * This routine is invoked by the worker thread to process all the pending
12218 * SLI4 els abort xri events.
12220 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12222 struct lpfc_cq_event *cq_event;
12224 /* First, declare the els xri abort event has been handled */
12225 spin_lock_irq(&phba->hbalock);
12226 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12227 spin_unlock_irq(&phba->hbalock);
12228 /* Now, handle all the els xri abort events */
12229 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12230 /* Get the first event from the head of the event queue */
12231 spin_lock_irq(&phba->hbalock);
12232 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12233 cq_event, struct lpfc_cq_event, list);
12234 spin_unlock_irq(&phba->hbalock);
12235 /* Notify aborted XRI for ELS work queue */
12236 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12237 /* Free the event processed back to the free pool */
12238 lpfc_sli4_cq_event_release(phba, cq_event);
12243 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12244 * @phba: pointer to lpfc hba data structure
12245 * @pIocbIn: pointer to the rspiocbq
12246 * @pIocbOut: pointer to the cmdiocbq
12247 * @wcqe: pointer to the complete wcqe
12249 * This routine transfers the fields of a command iocbq to a response iocbq
12250 * by copying all the IOCB fields from command iocbq and transferring the
12251 * completion status information from the complete wcqe.
12253 static void
12254 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12255 struct lpfc_iocbq *pIocbIn,
12256 struct lpfc_iocbq *pIocbOut,
12257 struct lpfc_wcqe_complete *wcqe)
12259 int numBdes, i;
12260 unsigned long iflags;
12261 uint32_t status, max_response;
12262 struct lpfc_dmabuf *dmabuf;
12263 struct ulp_bde64 *bpl, bde;
12264 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12266 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12267 sizeof(struct lpfc_iocbq) - offset);
12268 /* Map WCQE parameters into irspiocb parameters */
12269 status = bf_get(lpfc_wcqe_c_status, wcqe);
12270 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12271 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12272 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12273 pIocbIn->iocb.un.fcpi.fcpi_parm =
12274 pIocbOut->iocb.un.fcpi.fcpi_parm -
12275 wcqe->total_data_placed;
12276 else
12277 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12278 else {
12279 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12280 switch (pIocbOut->iocb.ulpCommand) {
12281 case CMD_ELS_REQUEST64_CR:
12282 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12283 bpl = (struct ulp_bde64 *)dmabuf->virt;
12284 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12285 max_response = bde.tus.f.bdeSize;
12286 break;
12287 case CMD_GEN_REQUEST64_CR:
12288 max_response = 0;
12289 if (!pIocbOut->context3)
12290 break;
12291 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12292 sizeof(struct ulp_bde64);
12293 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12294 bpl = (struct ulp_bde64 *)dmabuf->virt;
12295 for (i = 0; i < numBdes; i++) {
12296 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12297 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12298 max_response += bde.tus.f.bdeSize;
12300 break;
12301 default:
12302 max_response = wcqe->total_data_placed;
12303 break;
12305 if (max_response < wcqe->total_data_placed)
12306 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12307 else
12308 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12309 wcqe->total_data_placed;
12312 /* Convert BG errors for completion status */
12313 if (status == CQE_STATUS_DI_ERROR) {
12314 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12316 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12317 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12318 else
12319 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12321 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12322 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12323 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12324 BGS_GUARD_ERR_MASK;
12325 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12326 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12327 BGS_APPTAG_ERR_MASK;
12328 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12329 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12330 BGS_REFTAG_ERR_MASK;
12332 /* Check to see if there was any good data before the error */
12333 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12334 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12335 BGS_HI_WATER_MARK_PRESENT_MASK;
12336 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12337 wcqe->total_data_placed;
12341 * Set ALL the error bits to indicate we don't know what
12342 * type of error it is.
12344 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12345 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12346 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12347 BGS_GUARD_ERR_MASK);
12350 /* Pick up HBA exchange busy condition */
12351 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12352 spin_lock_irqsave(&phba->hbalock, iflags);
12353 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12354 spin_unlock_irqrestore(&phba->hbalock, iflags);
12359 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12360 * @phba: Pointer to HBA context object.
12361 * @wcqe: Pointer to work-queue completion queue entry.
12363 * This routine handles an ELS work-queue completion event and construct
12364 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12365 * discovery engine to handle.
12367 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12369 static struct lpfc_iocbq *
12370 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12371 struct lpfc_iocbq *irspiocbq)
12373 struct lpfc_sli_ring *pring;
12374 struct lpfc_iocbq *cmdiocbq;
12375 struct lpfc_wcqe_complete *wcqe;
12376 unsigned long iflags;
12378 pring = lpfc_phba_elsring(phba);
12380 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
12381 spin_lock_irqsave(&pring->ring_lock, iflags);
12382 pring->stats.iocb_event++;
12383 /* Look up the ELS command IOCB and create pseudo response IOCB */
12384 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12385 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12386 /* Put the iocb back on the txcmplq */
12387 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12388 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12390 if (unlikely(!cmdiocbq)) {
12391 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12392 "0386 ELS complete with no corresponding "
12393 "cmdiocb: iotag (%d)\n",
12394 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12395 lpfc_sli_release_iocbq(phba, irspiocbq);
12396 return NULL;
12399 /* Fake the irspiocbq and copy necessary response information */
12400 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
12402 return irspiocbq;
12406 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12407 * @phba: Pointer to HBA context object.
12408 * @cqe: Pointer to mailbox completion queue entry.
12410 * This routine process a mailbox completion queue entry with asynchrous
12411 * event.
12413 * Return: true if work posted to worker thread, otherwise false.
12415 static bool
12416 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12418 struct lpfc_cq_event *cq_event;
12419 unsigned long iflags;
12421 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12422 "0392 Async Event: word0:x%x, word1:x%x, "
12423 "word2:x%x, word3:x%x\n", mcqe->word0,
12424 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12426 /* Allocate a new internal CQ_EVENT entry */
12427 cq_event = lpfc_sli4_cq_event_alloc(phba);
12428 if (!cq_event) {
12429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12430 "0394 Failed to allocate CQ_EVENT entry\n");
12431 return false;
12434 /* Move the CQE into an asynchronous event entry */
12435 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12436 spin_lock_irqsave(&phba->hbalock, iflags);
12437 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12438 /* Set the async event flag */
12439 phba->hba_flag |= ASYNC_EVENT;
12440 spin_unlock_irqrestore(&phba->hbalock, iflags);
12442 return true;
12446 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12447 * @phba: Pointer to HBA context object.
12448 * @cqe: Pointer to mailbox completion queue entry.
12450 * This routine process a mailbox completion queue entry with mailbox
12451 * completion event.
12453 * Return: true if work posted to worker thread, otherwise false.
12455 static bool
12456 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12458 uint32_t mcqe_status;
12459 MAILBOX_t *mbox, *pmbox;
12460 struct lpfc_mqe *mqe;
12461 struct lpfc_vport *vport;
12462 struct lpfc_nodelist *ndlp;
12463 struct lpfc_dmabuf *mp;
12464 unsigned long iflags;
12465 LPFC_MBOXQ_t *pmb;
12466 bool workposted = false;
12467 int rc;
12469 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12470 if (!bf_get(lpfc_trailer_completed, mcqe))
12471 goto out_no_mqe_complete;
12473 /* Get the reference to the active mbox command */
12474 spin_lock_irqsave(&phba->hbalock, iflags);
12475 pmb = phba->sli.mbox_active;
12476 if (unlikely(!pmb)) {
12477 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12478 "1832 No pending MBOX command to handle\n");
12479 spin_unlock_irqrestore(&phba->hbalock, iflags);
12480 goto out_no_mqe_complete;
12482 spin_unlock_irqrestore(&phba->hbalock, iflags);
12483 mqe = &pmb->u.mqe;
12484 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12485 mbox = phba->mbox;
12486 vport = pmb->vport;
12488 /* Reset heartbeat timer */
12489 phba->last_completion_time = jiffies;
12490 del_timer(&phba->sli.mbox_tmo);
12492 /* Move mbox data to caller's mailbox region, do endian swapping */
12493 if (pmb->mbox_cmpl && mbox)
12494 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
12497 * For mcqe errors, conditionally move a modified error code to
12498 * the mbox so that the error will not be missed.
12500 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12501 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12502 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12503 bf_set(lpfc_mqe_status, mqe,
12504 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12506 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12507 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12508 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12509 "MBOX dflt rpi: status:x%x rpi:x%x",
12510 mcqe_status,
12511 pmbox->un.varWords[0], 0);
12512 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12513 mp = (struct lpfc_dmabuf *)(pmb->context1);
12514 ndlp = (struct lpfc_nodelist *)pmb->context2;
12515 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12516 * RID of the PPI using the same mbox buffer.
12518 lpfc_unreg_login(phba, vport->vpi,
12519 pmbox->un.varWords[0], pmb);
12520 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12521 pmb->context1 = mp;
12522 pmb->context2 = ndlp;
12523 pmb->vport = vport;
12524 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12525 if (rc != MBX_BUSY)
12526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12527 LOG_SLI, "0385 rc should "
12528 "have been MBX_BUSY\n");
12529 if (rc != MBX_NOT_FINISHED)
12530 goto send_current_mbox;
12533 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12534 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12535 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12537 /* There is mailbox completion work to do */
12538 spin_lock_irqsave(&phba->hbalock, iflags);
12539 __lpfc_mbox_cmpl_put(phba, pmb);
12540 phba->work_ha |= HA_MBATT;
12541 spin_unlock_irqrestore(&phba->hbalock, iflags);
12542 workposted = true;
12544 send_current_mbox:
12545 spin_lock_irqsave(&phba->hbalock, iflags);
12546 /* Release the mailbox command posting token */
12547 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12548 /* Setting active mailbox pointer need to be in sync to flag clear */
12549 phba->sli.mbox_active = NULL;
12550 spin_unlock_irqrestore(&phba->hbalock, iflags);
12551 /* Wake up worker thread to post the next pending mailbox command */
12552 lpfc_worker_wake_up(phba);
12553 out_no_mqe_complete:
12554 if (bf_get(lpfc_trailer_consumed, mcqe))
12555 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12556 return workposted;
12560 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12561 * @phba: Pointer to HBA context object.
12562 * @cqe: Pointer to mailbox completion queue entry.
12564 * This routine process a mailbox completion queue entry, it invokes the
12565 * proper mailbox complete handling or asynchrous event handling routine
12566 * according to the MCQE's async bit.
12568 * Return: true if work posted to worker thread, otherwise false.
12570 static bool
12571 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12573 struct lpfc_mcqe mcqe;
12574 bool workposted;
12576 /* Copy the mailbox MCQE and convert endian order as needed */
12577 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12579 /* Invoke the proper event handling routine */
12580 if (!bf_get(lpfc_trailer_async, &mcqe))
12581 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12582 else
12583 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12584 return workposted;
12588 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12589 * @phba: Pointer to HBA context object.
12590 * @cq: Pointer to associated CQ
12591 * @wcqe: Pointer to work-queue completion queue entry.
12593 * This routine handles an ELS work-queue completion event.
12595 * Return: true if work posted to worker thread, otherwise false.
12597 static bool
12598 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12599 struct lpfc_wcqe_complete *wcqe)
12601 struct lpfc_iocbq *irspiocbq;
12602 unsigned long iflags;
12603 struct lpfc_sli_ring *pring = cq->pring;
12604 int txq_cnt = 0;
12605 int txcmplq_cnt = 0;
12606 int fcp_txcmplq_cnt = 0;
12608 /* Get an irspiocbq for later ELS response processing use */
12609 irspiocbq = lpfc_sli_get_iocbq(phba);
12610 if (!irspiocbq) {
12611 if (!list_empty(&pring->txq))
12612 txq_cnt++;
12613 if (!list_empty(&pring->txcmplq))
12614 txcmplq_cnt++;
12615 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12616 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12617 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12618 txq_cnt, phba->iocb_cnt,
12619 fcp_txcmplq_cnt,
12620 txcmplq_cnt);
12621 return false;
12624 /* Save off the slow-path queue event for work thread to process */
12625 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12626 spin_lock_irqsave(&phba->hbalock, iflags);
12627 list_add_tail(&irspiocbq->cq_event.list,
12628 &phba->sli4_hba.sp_queue_event);
12629 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12630 spin_unlock_irqrestore(&phba->hbalock, iflags);
12632 return true;
12636 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12637 * @phba: Pointer to HBA context object.
12638 * @wcqe: Pointer to work-queue completion queue entry.
12640 * This routine handles slow-path WQ entry comsumed event by invoking the
12641 * proper WQ release routine to the slow-path WQ.
12643 static void
12644 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12645 struct lpfc_wcqe_release *wcqe)
12647 /* sanity check on queue memory */
12648 if (unlikely(!phba->sli4_hba.els_wq))
12649 return;
12650 /* Check for the slow-path ELS work queue */
12651 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12652 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12653 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12654 else
12655 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12656 "2579 Slow-path wqe consume event carries "
12657 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12658 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12659 phba->sli4_hba.els_wq->queue_id);
12663 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12664 * @phba: Pointer to HBA context object.
12665 * @cq: Pointer to a WQ completion queue.
12666 * @wcqe: Pointer to work-queue completion queue entry.
12668 * This routine handles an XRI abort event.
12670 * Return: true if work posted to worker thread, otherwise false.
12672 static bool
12673 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12674 struct lpfc_queue *cq,
12675 struct sli4_wcqe_xri_aborted *wcqe)
12677 bool workposted = false;
12678 struct lpfc_cq_event *cq_event;
12679 unsigned long iflags;
12681 /* Allocate a new internal CQ_EVENT entry */
12682 cq_event = lpfc_sli4_cq_event_alloc(phba);
12683 if (!cq_event) {
12684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12685 "0602 Failed to allocate CQ_EVENT entry\n");
12686 return false;
12689 /* Move the CQE into the proper xri abort event list */
12690 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12691 switch (cq->subtype) {
12692 case LPFC_FCP:
12693 spin_lock_irqsave(&phba->hbalock, iflags);
12694 list_add_tail(&cq_event->list,
12695 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12696 /* Set the fcp xri abort event flag */
12697 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12698 spin_unlock_irqrestore(&phba->hbalock, iflags);
12699 workposted = true;
12700 break;
12701 case LPFC_ELS:
12702 spin_lock_irqsave(&phba->hbalock, iflags);
12703 list_add_tail(&cq_event->list,
12704 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12705 /* Set the els xri abort event flag */
12706 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12707 spin_unlock_irqrestore(&phba->hbalock, iflags);
12708 workposted = true;
12709 break;
12710 default:
12711 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12712 "0603 Invalid work queue CQE subtype (x%x)\n",
12713 cq->subtype);
12714 workposted = false;
12715 break;
12717 return workposted;
12721 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12722 * @phba: Pointer to HBA context object.
12723 * @rcqe: Pointer to receive-queue completion queue entry.
12725 * This routine process a receive-queue completion queue entry.
12727 * Return: true if work posted to worker thread, otherwise false.
12729 static bool
12730 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12732 bool workposted = false;
12733 struct fc_frame_header *fc_hdr;
12734 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12735 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12736 struct hbq_dmabuf *dma_buf;
12737 uint32_t status, rq_id;
12738 unsigned long iflags;
12740 /* sanity check on queue memory */
12741 if (unlikely(!hrq) || unlikely(!drq))
12742 return workposted;
12744 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12745 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12746 else
12747 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12748 if (rq_id != hrq->queue_id)
12749 goto out;
12751 status = bf_get(lpfc_rcqe_status, rcqe);
12752 switch (status) {
12753 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12755 "2537 Receive Frame Truncated!!\n");
12756 hrq->RQ_buf_trunc++;
12757 case FC_STATUS_RQ_SUCCESS:
12758 lpfc_sli4_rq_release(hrq, drq);
12759 spin_lock_irqsave(&phba->hbalock, iflags);
12760 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12761 if (!dma_buf) {
12762 hrq->RQ_no_buf_found++;
12763 spin_unlock_irqrestore(&phba->hbalock, iflags);
12764 goto out;
12766 hrq->RQ_rcv_buf++;
12767 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12769 /* If a NVME LS event (type 0x28), treat it as Fast path */
12770 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12772 /* save off the frame for the word thread to process */
12773 list_add_tail(&dma_buf->cq_event.list,
12774 &phba->sli4_hba.sp_queue_event);
12775 /* Frame received */
12776 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12777 spin_unlock_irqrestore(&phba->hbalock, iflags);
12778 workposted = true;
12779 break;
12780 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12781 case FC_STATUS_INSUFF_BUF_FRM_DISC:
12782 hrq->RQ_no_posted_buf++;
12783 /* Post more buffers if possible */
12784 spin_lock_irqsave(&phba->hbalock, iflags);
12785 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12786 spin_unlock_irqrestore(&phba->hbalock, iflags);
12787 workposted = true;
12788 break;
12790 out:
12791 return workposted;
12795 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12796 * @phba: Pointer to HBA context object.
12797 * @cq: Pointer to the completion queue.
12798 * @wcqe: Pointer to a completion queue entry.
12800 * This routine process a slow-path work-queue or receive queue completion queue
12801 * entry.
12803 * Return: true if work posted to worker thread, otherwise false.
12805 static bool
12806 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12807 struct lpfc_cqe *cqe)
12809 struct lpfc_cqe cqevt;
12810 bool workposted = false;
12812 /* Copy the work queue CQE and convert endian order if needed */
12813 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12815 /* Check and process for different type of WCQE and dispatch */
12816 switch (bf_get(lpfc_cqe_code, &cqevt)) {
12817 case CQE_CODE_COMPL_WQE:
12818 /* Process the WQ/RQ complete event */
12819 phba->last_completion_time = jiffies;
12820 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12821 (struct lpfc_wcqe_complete *)&cqevt);
12822 break;
12823 case CQE_CODE_RELEASE_WQE:
12824 /* Process the WQ release event */
12825 lpfc_sli4_sp_handle_rel_wcqe(phba,
12826 (struct lpfc_wcqe_release *)&cqevt);
12827 break;
12828 case CQE_CODE_XRI_ABORTED:
12829 /* Process the WQ XRI abort event */
12830 phba->last_completion_time = jiffies;
12831 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12832 (struct sli4_wcqe_xri_aborted *)&cqevt);
12833 break;
12834 case CQE_CODE_RECEIVE:
12835 case CQE_CODE_RECEIVE_V1:
12836 /* Process the RQ event */
12837 phba->last_completion_time = jiffies;
12838 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12839 (struct lpfc_rcqe *)&cqevt);
12840 break;
12841 default:
12842 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12843 "0388 Not a valid WCQE code: x%x\n",
12844 bf_get(lpfc_cqe_code, &cqevt));
12845 break;
12847 return workposted;
12851 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12852 * @phba: Pointer to HBA context object.
12853 * @eqe: Pointer to fast-path event queue entry.
12855 * This routine process a event queue entry from the slow-path event queue.
12856 * It will check the MajorCode and MinorCode to determine this is for a
12857 * completion event on a completion queue, if not, an error shall be logged
12858 * and just return. Otherwise, it will get to the corresponding completion
12859 * queue and process all the entries on that completion queue, rearm the
12860 * completion queue, and then return.
12863 static void
12864 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12865 struct lpfc_queue *speq)
12867 struct lpfc_queue *cq = NULL, *childq;
12868 struct lpfc_cqe *cqe;
12869 bool workposted = false;
12870 int ecount = 0;
12871 uint16_t cqid;
12873 /* Get the reference to the corresponding CQ */
12874 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12876 list_for_each_entry(childq, &speq->child_list, list) {
12877 if (childq->queue_id == cqid) {
12878 cq = childq;
12879 break;
12882 if (unlikely(!cq)) {
12883 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12885 "0365 Slow-path CQ identifier "
12886 "(%d) does not exist\n", cqid);
12887 return;
12890 /* Save EQ associated with this CQ */
12891 cq->assoc_qp = speq;
12893 /* Process all the entries to the CQ */
12894 switch (cq->type) {
12895 case LPFC_MCQ:
12896 while ((cqe = lpfc_sli4_cq_get(cq))) {
12897 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12898 if (!(++ecount % cq->entry_repost))
12899 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12900 cq->CQ_mbox++;
12902 break;
12903 case LPFC_WCQ:
12904 while ((cqe = lpfc_sli4_cq_get(cq))) {
12905 if ((cq->subtype == LPFC_FCP) ||
12906 (cq->subtype == LPFC_NVME))
12907 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
12908 cqe);
12909 else
12910 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12911 cqe);
12912 if (!(++ecount % cq->entry_repost))
12913 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12916 /* Track the max number of CQEs processed in 1 EQ */
12917 if (ecount > cq->CQ_max_cqe)
12918 cq->CQ_max_cqe = ecount;
12919 break;
12920 default:
12921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12922 "0370 Invalid completion queue type (%d)\n",
12923 cq->type);
12924 return;
12927 /* Catch the no cq entry condition, log an error */
12928 if (unlikely(ecount == 0))
12929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12930 "0371 No entry from the CQ: identifier "
12931 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12933 /* In any case, flash and re-arm the RCQ */
12934 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12936 /* wake up worker thread if there are works to be done */
12937 if (workposted)
12938 lpfc_worker_wake_up(phba);
12942 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12943 * @phba: Pointer to HBA context object.
12944 * @cq: Pointer to associated CQ
12945 * @wcqe: Pointer to work-queue completion queue entry.
12947 * This routine process a fast-path work queue completion entry from fast-path
12948 * event queue for FCP command response completion.
12950 static void
12951 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12952 struct lpfc_wcqe_complete *wcqe)
12954 struct lpfc_sli_ring *pring = cq->pring;
12955 struct lpfc_iocbq *cmdiocbq;
12956 struct lpfc_iocbq irspiocbq;
12957 unsigned long iflags;
12959 /* Check for response status */
12960 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12961 /* If resource errors reported from HBA, reduce queue
12962 * depth of the SCSI device.
12964 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12965 IOSTAT_LOCAL_REJECT)) &&
12966 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12967 IOERR_NO_RESOURCES))
12968 phba->lpfc_rampdown_queue_depth(phba);
12970 /* Log the error status */
12971 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12972 "0373 FCP complete error: status=x%x, "
12973 "hw_status=x%x, total_data_specified=%d, "
12974 "parameter=x%x, word3=x%x\n",
12975 bf_get(lpfc_wcqe_c_status, wcqe),
12976 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12977 wcqe->total_data_placed, wcqe->parameter,
12978 wcqe->word3);
12981 /* Look up the FCP command IOCB and create pseudo response IOCB */
12982 spin_lock_irqsave(&pring->ring_lock, iflags);
12983 pring->stats.iocb_event++;
12984 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12985 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12986 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12987 if (unlikely(!cmdiocbq)) {
12988 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12989 "0374 FCP complete with no corresponding "
12990 "cmdiocb: iotag (%d)\n",
12991 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12992 return;
12995 if (cq->assoc_qp)
12996 cmdiocbq->isr_timestamp =
12997 cq->assoc_qp->isr_timestamp;
12999 if (cmdiocbq->iocb_cmpl == NULL) {
13000 if (cmdiocbq->wqe_cmpl) {
13001 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13002 spin_lock_irqsave(&phba->hbalock, iflags);
13003 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13004 spin_unlock_irqrestore(&phba->hbalock, iflags);
13007 /* Pass the cmd_iocb and the wcqe to the upper layer */
13008 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13009 return;
13011 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13012 "0375 FCP cmdiocb not callback function "
13013 "iotag: (%d)\n",
13014 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13015 return;
13018 /* Fake the irspiocb and copy necessary response information */
13019 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13021 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13022 spin_lock_irqsave(&phba->hbalock, iflags);
13023 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13024 spin_unlock_irqrestore(&phba->hbalock, iflags);
13027 /* Pass the cmd_iocb and the rsp state to the upper layer */
13028 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13032 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13033 * @phba: Pointer to HBA context object.
13034 * @cq: Pointer to completion queue.
13035 * @wcqe: Pointer to work-queue completion queue entry.
13037 * This routine handles an fast-path WQ entry comsumed event by invoking the
13038 * proper WQ release routine to the slow-path WQ.
13040 static void
13041 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13042 struct lpfc_wcqe_release *wcqe)
13044 struct lpfc_queue *childwq;
13045 bool wqid_matched = false;
13046 uint16_t hba_wqid;
13048 /* Check for fast-path FCP work queue release */
13049 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13050 list_for_each_entry(childwq, &cq->child_list, list) {
13051 if (childwq->queue_id == hba_wqid) {
13052 lpfc_sli4_wq_release(childwq,
13053 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13054 wqid_matched = true;
13055 break;
13058 /* Report warning log message if no match found */
13059 if (wqid_matched != true)
13060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13061 "2580 Fast-path wqe consume event carries "
13062 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13066 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13067 * @phba: Pointer to HBA context object.
13068 * @rcqe: Pointer to receive-queue completion queue entry.
13070 * This routine process a receive-queue completion queue entry.
13072 * Return: true if work posted to worker thread, otherwise false.
13074 static bool
13075 lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13076 struct lpfc_rcqe *rcqe)
13078 bool workposted = false;
13079 struct lpfc_queue *hrq;
13080 struct lpfc_queue *drq;
13081 struct rqb_dmabuf *dma_buf;
13082 struct fc_frame_header *fc_hdr;
13083 uint32_t status, rq_id;
13084 unsigned long iflags;
13085 uint32_t fctl, idx;
13087 if ((phba->nvmet_support == 0) ||
13088 (phba->sli4_hba.nvmet_cqset == NULL))
13089 return workposted;
13091 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13092 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13093 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13095 /* sanity check on queue memory */
13096 if (unlikely(!hrq) || unlikely(!drq))
13097 return workposted;
13099 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13100 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13101 else
13102 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13104 if ((phba->nvmet_support == 0) ||
13105 (rq_id != hrq->queue_id))
13106 return workposted;
13108 status = bf_get(lpfc_rcqe_status, rcqe);
13109 switch (status) {
13110 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13112 "6126 Receive Frame Truncated!!\n");
13113 hrq->RQ_buf_trunc++;
13114 break;
13115 case FC_STATUS_RQ_SUCCESS:
13116 lpfc_sli4_rq_release(hrq, drq);
13117 spin_lock_irqsave(&phba->hbalock, iflags);
13118 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13119 if (!dma_buf) {
13120 hrq->RQ_no_buf_found++;
13121 spin_unlock_irqrestore(&phba->hbalock, iflags);
13122 goto out;
13124 spin_unlock_irqrestore(&phba->hbalock, iflags);
13125 hrq->RQ_rcv_buf++;
13126 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13128 /* Just some basic sanity checks on FCP Command frame */
13129 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13130 fc_hdr->fh_f_ctl[1] << 8 |
13131 fc_hdr->fh_f_ctl[2]);
13132 if (((fctl &
13133 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13134 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13135 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13136 goto drop;
13138 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13139 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
13140 lpfc_nvmet_unsol_fcp_event(
13141 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13142 cq->assoc_qp->isr_timestamp);
13143 return false;
13145 drop:
13146 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13147 break;
13148 case FC_STATUS_INSUFF_BUF_NEED_BUF:
13149 case FC_STATUS_INSUFF_BUF_FRM_DISC:
13150 hrq->RQ_no_posted_buf++;
13151 /* Post more buffers if possible */
13152 spin_lock_irqsave(&phba->hbalock, iflags);
13153 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13154 spin_unlock_irqrestore(&phba->hbalock, iflags);
13155 workposted = true;
13156 break;
13158 out:
13159 return workposted;
13163 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13164 * @cq: Pointer to the completion queue.
13165 * @eqe: Pointer to fast-path completion queue entry.
13167 * This routine process a fast-path work queue completion entry from fast-path
13168 * event queue for FCP command response completion.
13170 static int
13171 lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13172 struct lpfc_cqe *cqe)
13174 struct lpfc_wcqe_release wcqe;
13175 bool workposted = false;
13177 /* Copy the work queue CQE and convert endian order if needed */
13178 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13180 /* Check and process for different type of WCQE and dispatch */
13181 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13182 case CQE_CODE_COMPL_WQE:
13183 case CQE_CODE_NVME_ERSP:
13184 cq->CQ_wq++;
13185 /* Process the WQ complete event */
13186 phba->last_completion_time = jiffies;
13187 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13188 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13189 (struct lpfc_wcqe_complete *)&wcqe);
13190 if (cq->subtype == LPFC_NVME_LS)
13191 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13192 (struct lpfc_wcqe_complete *)&wcqe);
13193 break;
13194 case CQE_CODE_RELEASE_WQE:
13195 cq->CQ_release_wqe++;
13196 /* Process the WQ release event */
13197 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13198 (struct lpfc_wcqe_release *)&wcqe);
13199 break;
13200 case CQE_CODE_XRI_ABORTED:
13201 cq->CQ_xri_aborted++;
13202 /* Process the WQ XRI abort event */
13203 phba->last_completion_time = jiffies;
13204 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13205 (struct sli4_wcqe_xri_aborted *)&wcqe);
13206 break;
13207 case CQE_CODE_RECEIVE_V1:
13208 case CQE_CODE_RECEIVE:
13209 phba->last_completion_time = jiffies;
13210 if (cq->subtype == LPFC_NVMET) {
13211 workposted = lpfc_sli4_nvmet_handle_rcqe(
13212 phba, cq, (struct lpfc_rcqe *)&wcqe);
13214 break;
13215 default:
13216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13217 "0144 Not a valid CQE code: x%x\n",
13218 bf_get(lpfc_wcqe_c_code, &wcqe));
13219 break;
13221 return workposted;
13225 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13226 * @phba: Pointer to HBA context object.
13227 * @eqe: Pointer to fast-path event queue entry.
13229 * This routine process a event queue entry from the fast-path event queue.
13230 * It will check the MajorCode and MinorCode to determine this is for a
13231 * completion event on a completion queue, if not, an error shall be logged
13232 * and just return. Otherwise, it will get to the corresponding completion
13233 * queue and process all the entries on the completion queue, rearm the
13234 * completion queue, and then return.
13236 static void
13237 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13238 uint32_t qidx)
13240 struct lpfc_queue *cq = NULL;
13241 struct lpfc_cqe *cqe;
13242 bool workposted = false;
13243 uint16_t cqid, id;
13244 int ecount = 0;
13246 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13248 "0366 Not a valid completion "
13249 "event: majorcode=x%x, minorcode=x%x\n",
13250 bf_get_le32(lpfc_eqe_major_code, eqe),
13251 bf_get_le32(lpfc_eqe_minor_code, eqe));
13252 return;
13255 /* Get the reference to the corresponding CQ */
13256 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13258 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13259 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13260 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13261 /* Process NVMET unsol rcv */
13262 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13263 goto process_cq;
13267 if (phba->sli4_hba.nvme_cq_map &&
13268 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13269 /* Process NVME / NVMET command completion */
13270 cq = phba->sli4_hba.nvme_cq[qidx];
13271 goto process_cq;
13274 if (phba->sli4_hba.fcp_cq_map &&
13275 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13276 /* Process FCP command completion */
13277 cq = phba->sli4_hba.fcp_cq[qidx];
13278 goto process_cq;
13281 if (phba->sli4_hba.nvmels_cq &&
13282 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13283 /* Process NVME unsol rcv */
13284 cq = phba->sli4_hba.nvmels_cq;
13287 /* Otherwise this is a Slow path event */
13288 if (cq == NULL) {
13289 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13290 return;
13293 process_cq:
13294 if (unlikely(cqid != cq->queue_id)) {
13295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13296 "0368 Miss-matched fast-path completion "
13297 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13298 cqid, cq->queue_id);
13299 return;
13302 /* Save EQ associated with this CQ */
13303 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13305 /* Process all the entries to the CQ */
13306 while ((cqe = lpfc_sli4_cq_get(cq))) {
13307 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13308 if (!(++ecount % cq->entry_repost))
13309 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13312 /* Track the max number of CQEs processed in 1 EQ */
13313 if (ecount > cq->CQ_max_cqe)
13314 cq->CQ_max_cqe = ecount;
13316 /* Catch the no cq entry condition */
13317 if (unlikely(ecount == 0))
13318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13319 "0369 No entry from fast-path completion "
13320 "queue fcpcqid=%d\n", cq->queue_id);
13322 /* In any case, flash and re-arm the CQ */
13323 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13325 /* wake up worker thread if there are works to be done */
13326 if (workposted)
13327 lpfc_worker_wake_up(phba);
13330 static void
13331 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13333 struct lpfc_eqe *eqe;
13335 /* walk all the EQ entries and drop on the floor */
13336 while ((eqe = lpfc_sli4_eq_get(eq)))
13339 /* Clear and re-arm the EQ */
13340 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13345 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13346 * entry
13347 * @phba: Pointer to HBA context object.
13348 * @eqe: Pointer to fast-path event queue entry.
13350 * This routine process a event queue entry from the Flash Optimized Fabric
13351 * event queue. It will check the MajorCode and MinorCode to determine this
13352 * is for a completion event on a completion queue, if not, an error shall be
13353 * logged and just return. Otherwise, it will get to the corresponding
13354 * completion queue and process all the entries on the completion queue, rearm
13355 * the completion queue, and then return.
13357 static void
13358 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13360 struct lpfc_queue *cq;
13361 struct lpfc_cqe *cqe;
13362 bool workposted = false;
13363 uint16_t cqid;
13364 int ecount = 0;
13366 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13368 "9147 Not a valid completion "
13369 "event: majorcode=x%x, minorcode=x%x\n",
13370 bf_get_le32(lpfc_eqe_major_code, eqe),
13371 bf_get_le32(lpfc_eqe_minor_code, eqe));
13372 return;
13375 /* Get the reference to the corresponding CQ */
13376 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13378 /* Next check for OAS */
13379 cq = phba->sli4_hba.oas_cq;
13380 if (unlikely(!cq)) {
13381 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13383 "9148 OAS completion queue "
13384 "does not exist\n");
13385 return;
13388 if (unlikely(cqid != cq->queue_id)) {
13389 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13390 "9149 Miss-matched fast-path compl "
13391 "queue id: eqcqid=%d, fcpcqid=%d\n",
13392 cqid, cq->queue_id);
13393 return;
13396 /* Process all the entries to the OAS CQ */
13397 while ((cqe = lpfc_sli4_cq_get(cq))) {
13398 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13399 if (!(++ecount % cq->entry_repost))
13400 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13403 /* Track the max number of CQEs processed in 1 EQ */
13404 if (ecount > cq->CQ_max_cqe)
13405 cq->CQ_max_cqe = ecount;
13407 /* Catch the no cq entry condition */
13408 if (unlikely(ecount == 0))
13409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13410 "9153 No entry from fast-path completion "
13411 "queue fcpcqid=%d\n", cq->queue_id);
13413 /* In any case, flash and re-arm the CQ */
13414 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13416 /* wake up worker thread if there are works to be done */
13417 if (workposted)
13418 lpfc_worker_wake_up(phba);
13422 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13423 * @irq: Interrupt number.
13424 * @dev_id: The device context pointer.
13426 * This function is directly called from the PCI layer as an interrupt
13427 * service routine when device with SLI-4 interface spec is enabled with
13428 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13429 * IOCB ring event in the HBA. However, when the device is enabled with either
13430 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13431 * device-level interrupt handler. When the PCI slot is in error recovery
13432 * or the HBA is undergoing initialization, the interrupt handler will not
13433 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13434 * the intrrupt context. This function is called without any lock held.
13435 * It gets the hbalock to access and update SLI data structures. Note that,
13436 * the EQ to CQ are one-to-one map such that the EQ index is
13437 * equal to that of CQ index.
13439 * This function returns IRQ_HANDLED when interrupt is handled else it
13440 * returns IRQ_NONE.
13442 irqreturn_t
13443 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13445 struct lpfc_hba *phba;
13446 struct lpfc_hba_eq_hdl *hba_eq_hdl;
13447 struct lpfc_queue *eq;
13448 struct lpfc_eqe *eqe;
13449 unsigned long iflag;
13450 int ecount = 0;
13452 /* Get the driver's phba structure from the dev_id */
13453 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13454 phba = hba_eq_hdl->phba;
13456 if (unlikely(!phba))
13457 return IRQ_NONE;
13459 /* Get to the EQ struct associated with this vector */
13460 eq = phba->sli4_hba.fof_eq;
13461 if (unlikely(!eq))
13462 return IRQ_NONE;
13464 /* Check device state for handling interrupt */
13465 if (unlikely(lpfc_intr_state_check(phba))) {
13466 eq->EQ_badstate++;
13467 /* Check again for link_state with lock held */
13468 spin_lock_irqsave(&phba->hbalock, iflag);
13469 if (phba->link_state < LPFC_LINK_DOWN)
13470 /* Flush, clear interrupt, and rearm the EQ */
13471 lpfc_sli4_eq_flush(phba, eq);
13472 spin_unlock_irqrestore(&phba->hbalock, iflag);
13473 return IRQ_NONE;
13477 * Process all the event on FCP fast-path EQ
13479 while ((eqe = lpfc_sli4_eq_get(eq))) {
13480 lpfc_sli4_fof_handle_eqe(phba, eqe);
13481 if (!(++ecount % eq->entry_repost))
13482 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13483 eq->EQ_processed++;
13486 /* Track the max number of EQEs processed in 1 intr */
13487 if (ecount > eq->EQ_max_eqe)
13488 eq->EQ_max_eqe = ecount;
13491 if (unlikely(ecount == 0)) {
13492 eq->EQ_no_entry++;
13494 if (phba->intr_type == MSIX)
13495 /* MSI-X treated interrupt served as no EQ share INT */
13496 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13497 "9145 MSI-X interrupt with no EQE\n");
13498 else {
13499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13500 "9146 ISR interrupt with no EQE\n");
13501 /* Non MSI-X treated on interrupt as EQ share INT */
13502 return IRQ_NONE;
13505 /* Always clear and re-arm the fast-path EQ */
13506 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13507 return IRQ_HANDLED;
13511 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
13512 * @irq: Interrupt number.
13513 * @dev_id: The device context pointer.
13515 * This function is directly called from the PCI layer as an interrupt
13516 * service routine when device with SLI-4 interface spec is enabled with
13517 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13518 * ring event in the HBA. However, when the device is enabled with either
13519 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13520 * device-level interrupt handler. When the PCI slot is in error recovery
13521 * or the HBA is undergoing initialization, the interrupt handler will not
13522 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13523 * the intrrupt context. This function is called without any lock held.
13524 * It gets the hbalock to access and update SLI data structures. Note that,
13525 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13526 * equal to that of FCP CQ index.
13528 * The link attention and ELS ring attention events are handled
13529 * by the worker thread. The interrupt handler signals the worker thread
13530 * and returns for these events. This function is called without any lock
13531 * held. It gets the hbalock to access and update SLI data structures.
13533 * This function returns IRQ_HANDLED when interrupt is handled else it
13534 * returns IRQ_NONE.
13536 irqreturn_t
13537 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
13539 struct lpfc_hba *phba;
13540 struct lpfc_hba_eq_hdl *hba_eq_hdl;
13541 struct lpfc_queue *fpeq;
13542 struct lpfc_eqe *eqe;
13543 unsigned long iflag;
13544 int ecount = 0;
13545 int hba_eqidx;
13547 /* Get the driver's phba structure from the dev_id */
13548 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13549 phba = hba_eq_hdl->phba;
13550 hba_eqidx = hba_eq_hdl->idx;
13552 if (unlikely(!phba))
13553 return IRQ_NONE;
13554 if (unlikely(!phba->sli4_hba.hba_eq))
13555 return IRQ_NONE;
13557 /* Get to the EQ struct associated with this vector */
13558 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
13559 if (unlikely(!fpeq))
13560 return IRQ_NONE;
13562 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13563 if (phba->ktime_on)
13564 fpeq->isr_timestamp = ktime_get_ns();
13565 #endif
13567 if (lpfc_fcp_look_ahead) {
13568 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
13569 lpfc_sli4_eq_clr_intr(fpeq);
13570 else {
13571 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13572 return IRQ_NONE;
13576 /* Check device state for handling interrupt */
13577 if (unlikely(lpfc_intr_state_check(phba))) {
13578 fpeq->EQ_badstate++;
13579 /* Check again for link_state with lock held */
13580 spin_lock_irqsave(&phba->hbalock, iflag);
13581 if (phba->link_state < LPFC_LINK_DOWN)
13582 /* Flush, clear interrupt, and rearm the EQ */
13583 lpfc_sli4_eq_flush(phba, fpeq);
13584 spin_unlock_irqrestore(&phba->hbalock, iflag);
13585 if (lpfc_fcp_look_ahead)
13586 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13587 return IRQ_NONE;
13591 * Process all the event on FCP fast-path EQ
13593 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
13594 if (eqe == NULL)
13595 break;
13597 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13598 if (!(++ecount % fpeq->entry_repost))
13599 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
13600 fpeq->EQ_processed++;
13603 /* Track the max number of EQEs processed in 1 intr */
13604 if (ecount > fpeq->EQ_max_eqe)
13605 fpeq->EQ_max_eqe = ecount;
13607 /* Always clear and re-arm the fast-path EQ */
13608 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13610 if (unlikely(ecount == 0)) {
13611 fpeq->EQ_no_entry++;
13613 if (lpfc_fcp_look_ahead) {
13614 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13615 return IRQ_NONE;
13618 if (phba->intr_type == MSIX)
13619 /* MSI-X treated interrupt served as no EQ share INT */
13620 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13621 "0358 MSI-X interrupt with no EQE\n");
13622 else
13623 /* Non MSI-X treated on interrupt as EQ share INT */
13624 return IRQ_NONE;
13627 if (lpfc_fcp_look_ahead)
13628 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13630 return IRQ_HANDLED;
13631 } /* lpfc_sli4_fp_intr_handler */
13634 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13635 * @irq: Interrupt number.
13636 * @dev_id: The device context pointer.
13638 * This function is the device-level interrupt handler to device with SLI-4
13639 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13640 * interrupt mode is enabled and there is an event in the HBA which requires
13641 * driver attention. This function invokes the slow-path interrupt attention
13642 * handling function and fast-path interrupt attention handling function in
13643 * turn to process the relevant HBA attention events. This function is called
13644 * without any lock held. It gets the hbalock to access and update SLI data
13645 * structures.
13647 * This function returns IRQ_HANDLED when interrupt is handled, else it
13648 * returns IRQ_NONE.
13650 irqreturn_t
13651 lpfc_sli4_intr_handler(int irq, void *dev_id)
13653 struct lpfc_hba *phba;
13654 irqreturn_t hba_irq_rc;
13655 bool hba_handled = false;
13656 int qidx;
13658 /* Get the driver's phba structure from the dev_id */
13659 phba = (struct lpfc_hba *)dev_id;
13661 if (unlikely(!phba))
13662 return IRQ_NONE;
13665 * Invoke fast-path host attention interrupt handling as appropriate.
13667 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
13668 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
13669 &phba->sli4_hba.hba_eq_hdl[qidx]);
13670 if (hba_irq_rc == IRQ_HANDLED)
13671 hba_handled |= true;
13674 if (phba->cfg_fof) {
13675 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
13676 &phba->sli4_hba.hba_eq_hdl[qidx]);
13677 if (hba_irq_rc == IRQ_HANDLED)
13678 hba_handled |= true;
13681 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
13682 } /* lpfc_sli4_intr_handler */
13685 * lpfc_sli4_queue_free - free a queue structure and associated memory
13686 * @queue: The queue structure to free.
13688 * This function frees a queue structure and the DMAable memory used for
13689 * the host resident queue. This function must be called after destroying the
13690 * queue on the HBA.
13692 void
13693 lpfc_sli4_queue_free(struct lpfc_queue *queue)
13695 struct lpfc_dmabuf *dmabuf;
13697 if (!queue)
13698 return;
13700 while (!list_empty(&queue->page_list)) {
13701 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13702 list);
13703 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
13704 dmabuf->virt, dmabuf->phys);
13705 kfree(dmabuf);
13707 if (queue->rqbp) {
13708 lpfc_free_rq_buffer(queue->phba, queue);
13709 kfree(queue->rqbp);
13711 kfree(queue->pring);
13712 kfree(queue);
13713 return;
13717 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13718 * @phba: The HBA that this queue is being created on.
13719 * @entry_size: The size of each queue entry for this queue.
13720 * @entry count: The number of entries that this queue will handle.
13722 * This function allocates a queue structure and the DMAable memory used for
13723 * the host resident queue. This function must be called before creating the
13724 * queue on the HBA.
13726 struct lpfc_queue *
13727 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13728 uint32_t entry_count)
13730 struct lpfc_queue *queue;
13731 struct lpfc_dmabuf *dmabuf;
13732 int x, total_qe_count;
13733 void *dma_pointer;
13734 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13736 if (!phba->sli4_hba.pc_sli4_params.supported)
13737 hw_page_size = SLI4_PAGE_SIZE;
13739 queue = kzalloc(sizeof(struct lpfc_queue) +
13740 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13741 if (!queue)
13742 return NULL;
13743 queue->page_count = (ALIGN(entry_size * entry_count,
13744 hw_page_size))/hw_page_size;
13746 /* If needed, Adjust page count to match the max the adapter supports */
13747 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13748 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13750 INIT_LIST_HEAD(&queue->list);
13751 INIT_LIST_HEAD(&queue->wq_list);
13752 INIT_LIST_HEAD(&queue->page_list);
13753 INIT_LIST_HEAD(&queue->child_list);
13754 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13755 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13756 if (!dmabuf)
13757 goto out_fail;
13758 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13759 hw_page_size, &dmabuf->phys,
13760 GFP_KERNEL);
13761 if (!dmabuf->virt) {
13762 kfree(dmabuf);
13763 goto out_fail;
13765 dmabuf->buffer_tag = x;
13766 list_add_tail(&dmabuf->list, &queue->page_list);
13767 /* initialize queue's entry array */
13768 dma_pointer = dmabuf->virt;
13769 for (; total_qe_count < entry_count &&
13770 dma_pointer < (hw_page_size + dmabuf->virt);
13771 total_qe_count++, dma_pointer += entry_size) {
13772 queue->qe[total_qe_count].address = dma_pointer;
13775 queue->entry_size = entry_size;
13776 queue->entry_count = entry_count;
13779 * entry_repost is calculated based on the number of entries in the
13780 * queue. This works out except for RQs. If buffers are NOT initially
13781 * posted for every RQE, entry_repost should be adjusted accordingly.
13783 queue->entry_repost = (entry_count >> 3);
13784 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13785 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
13786 queue->phba = phba;
13788 return queue;
13789 out_fail:
13790 lpfc_sli4_queue_free(queue);
13791 return NULL;
13795 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13796 * @phba: HBA structure that indicates port to create a queue on.
13797 * @pci_barset: PCI BAR set flag.
13799 * This function shall perform iomap of the specified PCI BAR address to host
13800 * memory address if not already done so and return it. The returned host
13801 * memory address can be NULL.
13803 static void __iomem *
13804 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13806 if (!phba->pcidev)
13807 return NULL;
13809 switch (pci_barset) {
13810 case WQ_PCI_BAR_0_AND_1:
13811 return phba->pci_bar0_memmap_p;
13812 case WQ_PCI_BAR_2_AND_3:
13813 return phba->pci_bar2_memmap_p;
13814 case WQ_PCI_BAR_4_AND_5:
13815 return phba->pci_bar4_memmap_p;
13816 default:
13817 break;
13819 return NULL;
13823 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
13824 * @phba: HBA structure that indicates port to create a queue on.
13825 * @startq: The starting FCP EQ to modify
13827 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13829 * The @phba struct is used to send mailbox command to HBA. The @startq
13830 * is used to get the starting FCP EQ to change.
13831 * This function is asynchronous and will wait for the mailbox
13832 * command to finish before continuing.
13834 * On success this function will return a zero. If unable to allocate enough
13835 * memory this function will return -ENOMEM. If the queue create mailbox command
13836 * fails this function will return -ENXIO.
13839 lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
13841 struct lpfc_mbx_modify_eq_delay *eq_delay;
13842 LPFC_MBOXQ_t *mbox;
13843 struct lpfc_queue *eq;
13844 int cnt, rc, length, status = 0;
13845 uint32_t shdr_status, shdr_add_status;
13846 uint32_t result;
13847 int qidx;
13848 union lpfc_sli4_cfg_shdr *shdr;
13849 uint16_t dmult;
13851 if (startq >= phba->io_channel_irqs)
13852 return 0;
13854 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13855 if (!mbox)
13856 return -ENOMEM;
13857 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13858 sizeof(struct lpfc_sli4_cfg_mhdr));
13859 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13860 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13861 length, LPFC_SLI4_MBX_EMBED);
13862 eq_delay = &mbox->u.mqe.un.eq_delay;
13864 /* Calculate delay multiper from maximum interrupt per second */
13865 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13866 if (result > LPFC_DMULT_CONST || result == 0)
13867 dmult = 0;
13868 else
13869 dmult = LPFC_DMULT_CONST/result - 1;
13871 cnt = 0;
13872 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13873 eq = phba->sli4_hba.hba_eq[qidx];
13874 if (!eq)
13875 continue;
13876 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13877 eq_delay->u.request.eq[cnt].phase = 0;
13878 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13879 cnt++;
13880 if (cnt >= LPFC_MAX_EQ_DELAY)
13881 break;
13883 eq_delay->u.request.num_eq = cnt;
13885 mbox->vport = phba->pport;
13886 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13887 mbox->context1 = NULL;
13888 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13889 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13890 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13891 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13892 if (shdr_status || shdr_add_status || rc) {
13893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13894 "2512 MODIFY_EQ_DELAY mailbox failed with "
13895 "status x%x add_status x%x, mbx status x%x\n",
13896 shdr_status, shdr_add_status, rc);
13897 status = -ENXIO;
13899 mempool_free(mbox, phba->mbox_mem_pool);
13900 return status;
13904 * lpfc_eq_create - Create an Event Queue on the HBA
13905 * @phba: HBA structure that indicates port to create a queue on.
13906 * @eq: The queue structure to use to create the event queue.
13907 * @imax: The maximum interrupt per second limit.
13909 * This function creates an event queue, as detailed in @eq, on a port,
13910 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13912 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13913 * is used to get the entry count and entry size that are necessary to
13914 * determine the number of pages to allocate and use for this queue. This
13915 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13916 * event queue. This function is asynchronous and will wait for the mailbox
13917 * command to finish before continuing.
13919 * On success this function will return a zero. If unable to allocate enough
13920 * memory this function will return -ENOMEM. If the queue create mailbox command
13921 * fails this function will return -ENXIO.
13924 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
13926 struct lpfc_mbx_eq_create *eq_create;
13927 LPFC_MBOXQ_t *mbox;
13928 int rc, length, status = 0;
13929 struct lpfc_dmabuf *dmabuf;
13930 uint32_t shdr_status, shdr_add_status;
13931 union lpfc_sli4_cfg_shdr *shdr;
13932 uint16_t dmult;
13933 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13935 /* sanity check on queue memory */
13936 if (!eq)
13937 return -ENODEV;
13938 if (!phba->sli4_hba.pc_sli4_params.supported)
13939 hw_page_size = SLI4_PAGE_SIZE;
13941 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13942 if (!mbox)
13943 return -ENOMEM;
13944 length = (sizeof(struct lpfc_mbx_eq_create) -
13945 sizeof(struct lpfc_sli4_cfg_mhdr));
13946 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13947 LPFC_MBOX_OPCODE_EQ_CREATE,
13948 length, LPFC_SLI4_MBX_EMBED);
13949 eq_create = &mbox->u.mqe.un.eq_create;
13950 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13951 eq->page_count);
13952 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13953 LPFC_EQE_SIZE);
13954 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
13955 /* don't setup delay multiplier using EQ_CREATE */
13956 dmult = 0;
13957 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13958 dmult);
13959 switch (eq->entry_count) {
13960 default:
13961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13962 "0360 Unsupported EQ count. (%d)\n",
13963 eq->entry_count);
13964 if (eq->entry_count < 256)
13965 return -EINVAL;
13966 /* otherwise default to smallest count (drop through) */
13967 case 256:
13968 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13969 LPFC_EQ_CNT_256);
13970 break;
13971 case 512:
13972 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13973 LPFC_EQ_CNT_512);
13974 break;
13975 case 1024:
13976 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13977 LPFC_EQ_CNT_1024);
13978 break;
13979 case 2048:
13980 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13981 LPFC_EQ_CNT_2048);
13982 break;
13983 case 4096:
13984 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13985 LPFC_EQ_CNT_4096);
13986 break;
13988 list_for_each_entry(dmabuf, &eq->page_list, list) {
13989 memset(dmabuf->virt, 0, hw_page_size);
13990 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13991 putPaddrLow(dmabuf->phys);
13992 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13993 putPaddrHigh(dmabuf->phys);
13995 mbox->vport = phba->pport;
13996 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13997 mbox->context1 = NULL;
13998 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13999 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14002 if (shdr_status || shdr_add_status || rc) {
14003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14004 "2500 EQ_CREATE mailbox failed with "
14005 "status x%x add_status x%x, mbx status x%x\n",
14006 shdr_status, shdr_add_status, rc);
14007 status = -ENXIO;
14009 eq->type = LPFC_EQ;
14010 eq->subtype = LPFC_NONE;
14011 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14012 if (eq->queue_id == 0xFFFF)
14013 status = -ENXIO;
14014 eq->host_index = 0;
14015 eq->hba_index = 0;
14017 mempool_free(mbox, phba->mbox_mem_pool);
14018 return status;
14022 * lpfc_cq_create - Create a Completion Queue on the HBA
14023 * @phba: HBA structure that indicates port to create a queue on.
14024 * @cq: The queue structure to use to create the completion queue.
14025 * @eq: The event queue to bind this completion queue to.
14027 * This function creates a completion queue, as detailed in @wq, on a port,
14028 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14030 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14031 * is used to get the entry count and entry size that are necessary to
14032 * determine the number of pages to allocate and use for this queue. The @eq
14033 * is used to indicate which event queue to bind this completion queue to. This
14034 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14035 * completion queue. This function is asynchronous and will wait for the mailbox
14036 * command to finish before continuing.
14038 * On success this function will return a zero. If unable to allocate enough
14039 * memory this function will return -ENOMEM. If the queue create mailbox command
14040 * fails this function will return -ENXIO.
14043 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14044 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14046 struct lpfc_mbx_cq_create *cq_create;
14047 struct lpfc_dmabuf *dmabuf;
14048 LPFC_MBOXQ_t *mbox;
14049 int rc, length, status = 0;
14050 uint32_t shdr_status, shdr_add_status;
14051 union lpfc_sli4_cfg_shdr *shdr;
14052 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14054 /* sanity check on queue memory */
14055 if (!cq || !eq)
14056 return -ENODEV;
14057 if (!phba->sli4_hba.pc_sli4_params.supported)
14058 hw_page_size = SLI4_PAGE_SIZE;
14060 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14061 if (!mbox)
14062 return -ENOMEM;
14063 length = (sizeof(struct lpfc_mbx_cq_create) -
14064 sizeof(struct lpfc_sli4_cfg_mhdr));
14065 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14066 LPFC_MBOX_OPCODE_CQ_CREATE,
14067 length, LPFC_SLI4_MBX_EMBED);
14068 cq_create = &mbox->u.mqe.un.cq_create;
14069 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14070 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14071 cq->page_count);
14072 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14073 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14074 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14075 phba->sli4_hba.pc_sli4_params.cqv);
14076 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14077 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14078 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
14079 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14080 eq->queue_id);
14081 } else {
14082 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14083 eq->queue_id);
14085 switch (cq->entry_count) {
14086 default:
14087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14088 "0361 Unsupported CQ count: "
14089 "entry cnt %d sz %d pg cnt %d repost %d\n",
14090 cq->entry_count, cq->entry_size,
14091 cq->page_count, cq->entry_repost);
14092 if (cq->entry_count < 256) {
14093 status = -EINVAL;
14094 goto out;
14096 /* otherwise default to smallest count (drop through) */
14097 case 256:
14098 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14099 LPFC_CQ_CNT_256);
14100 break;
14101 case 512:
14102 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14103 LPFC_CQ_CNT_512);
14104 break;
14105 case 1024:
14106 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14107 LPFC_CQ_CNT_1024);
14108 break;
14110 list_for_each_entry(dmabuf, &cq->page_list, list) {
14111 memset(dmabuf->virt, 0, hw_page_size);
14112 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14113 putPaddrLow(dmabuf->phys);
14114 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14115 putPaddrHigh(dmabuf->phys);
14117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14119 /* The IOCTL status is embedded in the mailbox subheader. */
14120 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14121 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14122 if (shdr_status || shdr_add_status || rc) {
14123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14124 "2501 CQ_CREATE mailbox failed with "
14125 "status x%x add_status x%x, mbx status x%x\n",
14126 shdr_status, shdr_add_status, rc);
14127 status = -ENXIO;
14128 goto out;
14130 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14131 if (cq->queue_id == 0xFFFF) {
14132 status = -ENXIO;
14133 goto out;
14135 /* link the cq onto the parent eq child list */
14136 list_add_tail(&cq->list, &eq->child_list);
14137 /* Set up completion queue's type and subtype */
14138 cq->type = type;
14139 cq->subtype = subtype;
14140 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14141 cq->assoc_qid = eq->queue_id;
14142 cq->host_index = 0;
14143 cq->hba_index = 0;
14145 out:
14146 mempool_free(mbox, phba->mbox_mem_pool);
14147 return status;
14151 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14152 * @phba: HBA structure that indicates port to create a queue on.
14153 * @cqp: The queue structure array to use to create the completion queues.
14154 * @eqp: The event queue array to bind these completion queues to.
14156 * This function creates a set of completion queue, s to support MRQ
14157 * as detailed in @cqp, on a port,
14158 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14160 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14161 * is used to get the entry count and entry size that are necessary to
14162 * determine the number of pages to allocate and use for this queue. The @eq
14163 * is used to indicate which event queue to bind this completion queue to. This
14164 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14165 * completion queue. This function is asynchronous and will wait for the mailbox
14166 * command to finish before continuing.
14168 * On success this function will return a zero. If unable to allocate enough
14169 * memory this function will return -ENOMEM. If the queue create mailbox command
14170 * fails this function will return -ENXIO.
14173 lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14174 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14176 struct lpfc_queue *cq;
14177 struct lpfc_queue *eq;
14178 struct lpfc_mbx_cq_create_set *cq_set;
14179 struct lpfc_dmabuf *dmabuf;
14180 LPFC_MBOXQ_t *mbox;
14181 int rc, length, alloclen, status = 0;
14182 int cnt, idx, numcq, page_idx = 0;
14183 uint32_t shdr_status, shdr_add_status;
14184 union lpfc_sli4_cfg_shdr *shdr;
14185 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14187 /* sanity check on queue memory */
14188 numcq = phba->cfg_nvmet_mrq;
14189 if (!cqp || !eqp || !numcq)
14190 return -ENODEV;
14191 if (!phba->sli4_hba.pc_sli4_params.supported)
14192 hw_page_size = SLI4_PAGE_SIZE;
14194 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14195 if (!mbox)
14196 return -ENOMEM;
14198 length = sizeof(struct lpfc_mbx_cq_create_set);
14199 length += ((numcq * cqp[0]->page_count) *
14200 sizeof(struct dma_address));
14201 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14202 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14203 LPFC_SLI4_MBX_NEMBED);
14204 if (alloclen < length) {
14205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14206 "3098 Allocated DMA memory size (%d) is "
14207 "less than the requested DMA memory size "
14208 "(%d)\n", alloclen, length);
14209 status = -ENOMEM;
14210 goto out;
14212 cq_set = mbox->sge_array->addr[0];
14213 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14214 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14216 for (idx = 0; idx < numcq; idx++) {
14217 cq = cqp[idx];
14218 eq = eqp[idx];
14219 if (!cq || !eq) {
14220 status = -ENOMEM;
14221 goto out;
14224 switch (idx) {
14225 case 0:
14226 bf_set(lpfc_mbx_cq_create_set_page_size,
14227 &cq_set->u.request,
14228 (hw_page_size / SLI4_PAGE_SIZE));
14229 bf_set(lpfc_mbx_cq_create_set_num_pages,
14230 &cq_set->u.request, cq->page_count);
14231 bf_set(lpfc_mbx_cq_create_set_evt,
14232 &cq_set->u.request, 1);
14233 bf_set(lpfc_mbx_cq_create_set_valid,
14234 &cq_set->u.request, 1);
14235 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14236 &cq_set->u.request, 0);
14237 bf_set(lpfc_mbx_cq_create_set_num_cq,
14238 &cq_set->u.request, numcq);
14239 switch (cq->entry_count) {
14240 default:
14241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14242 "3118 Bad CQ count. (%d)\n",
14243 cq->entry_count);
14244 if (cq->entry_count < 256) {
14245 status = -EINVAL;
14246 goto out;
14248 /* otherwise default to smallest (drop thru) */
14249 case 256:
14250 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14251 &cq_set->u.request, LPFC_CQ_CNT_256);
14252 break;
14253 case 512:
14254 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14255 &cq_set->u.request, LPFC_CQ_CNT_512);
14256 break;
14257 case 1024:
14258 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14259 &cq_set->u.request, LPFC_CQ_CNT_1024);
14260 break;
14262 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14263 &cq_set->u.request, eq->queue_id);
14264 break;
14265 case 1:
14266 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14267 &cq_set->u.request, eq->queue_id);
14268 break;
14269 case 2:
14270 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14271 &cq_set->u.request, eq->queue_id);
14272 break;
14273 case 3:
14274 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14275 &cq_set->u.request, eq->queue_id);
14276 break;
14277 case 4:
14278 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14279 &cq_set->u.request, eq->queue_id);
14280 break;
14281 case 5:
14282 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14283 &cq_set->u.request, eq->queue_id);
14284 break;
14285 case 6:
14286 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14287 &cq_set->u.request, eq->queue_id);
14288 break;
14289 case 7:
14290 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14291 &cq_set->u.request, eq->queue_id);
14292 break;
14293 case 8:
14294 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14295 &cq_set->u.request, eq->queue_id);
14296 break;
14297 case 9:
14298 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14299 &cq_set->u.request, eq->queue_id);
14300 break;
14301 case 10:
14302 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14303 &cq_set->u.request, eq->queue_id);
14304 break;
14305 case 11:
14306 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14307 &cq_set->u.request, eq->queue_id);
14308 break;
14309 case 12:
14310 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14311 &cq_set->u.request, eq->queue_id);
14312 break;
14313 case 13:
14314 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14315 &cq_set->u.request, eq->queue_id);
14316 break;
14317 case 14:
14318 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14319 &cq_set->u.request, eq->queue_id);
14320 break;
14321 case 15:
14322 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14323 &cq_set->u.request, eq->queue_id);
14324 break;
14327 /* link the cq onto the parent eq child list */
14328 list_add_tail(&cq->list, &eq->child_list);
14329 /* Set up completion queue's type and subtype */
14330 cq->type = type;
14331 cq->subtype = subtype;
14332 cq->assoc_qid = eq->queue_id;
14333 cq->host_index = 0;
14334 cq->hba_index = 0;
14336 rc = 0;
14337 list_for_each_entry(dmabuf, &cq->page_list, list) {
14338 memset(dmabuf->virt, 0, hw_page_size);
14339 cnt = page_idx + dmabuf->buffer_tag;
14340 cq_set->u.request.page[cnt].addr_lo =
14341 putPaddrLow(dmabuf->phys);
14342 cq_set->u.request.page[cnt].addr_hi =
14343 putPaddrHigh(dmabuf->phys);
14344 rc++;
14346 page_idx += rc;
14349 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14351 /* The IOCTL status is embedded in the mailbox subheader. */
14352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14354 if (shdr_status || shdr_add_status || rc) {
14355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14356 "3119 CQ_CREATE_SET mailbox failed with "
14357 "status x%x add_status x%x, mbx status x%x\n",
14358 shdr_status, shdr_add_status, rc);
14359 status = -ENXIO;
14360 goto out;
14362 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14363 if (rc == 0xFFFF) {
14364 status = -ENXIO;
14365 goto out;
14368 for (idx = 0; idx < numcq; idx++) {
14369 cq = cqp[idx];
14370 cq->queue_id = rc + idx;
14373 out:
14374 lpfc_sli4_mbox_cmd_free(phba, mbox);
14375 return status;
14379 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14380 * @phba: HBA structure that indicates port to create a queue on.
14381 * @mq: The queue structure to use to create the mailbox queue.
14382 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14383 * @cq: The completion queue to associate with this cq.
14385 * This function provides failback (fb) functionality when the
14386 * mq_create_ext fails on older FW generations. It's purpose is identical
14387 * to mq_create_ext otherwise.
14389 * This routine cannot fail as all attributes were previously accessed and
14390 * initialized in mq_create_ext.
14392 static void
14393 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14394 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14396 struct lpfc_mbx_mq_create *mq_create;
14397 struct lpfc_dmabuf *dmabuf;
14398 int length;
14400 length = (sizeof(struct lpfc_mbx_mq_create) -
14401 sizeof(struct lpfc_sli4_cfg_mhdr));
14402 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14403 LPFC_MBOX_OPCODE_MQ_CREATE,
14404 length, LPFC_SLI4_MBX_EMBED);
14405 mq_create = &mbox->u.mqe.un.mq_create;
14406 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14407 mq->page_count);
14408 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14409 cq->queue_id);
14410 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14411 switch (mq->entry_count) {
14412 case 16:
14413 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14414 LPFC_MQ_RING_SIZE_16);
14415 break;
14416 case 32:
14417 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14418 LPFC_MQ_RING_SIZE_32);
14419 break;
14420 case 64:
14421 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14422 LPFC_MQ_RING_SIZE_64);
14423 break;
14424 case 128:
14425 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14426 LPFC_MQ_RING_SIZE_128);
14427 break;
14429 list_for_each_entry(dmabuf, &mq->page_list, list) {
14430 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14431 putPaddrLow(dmabuf->phys);
14432 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14433 putPaddrHigh(dmabuf->phys);
14438 * lpfc_mq_create - Create a mailbox Queue on the HBA
14439 * @phba: HBA structure that indicates port to create a queue on.
14440 * @mq: The queue structure to use to create the mailbox queue.
14441 * @cq: The completion queue to associate with this cq.
14442 * @subtype: The queue's subtype.
14444 * This function creates a mailbox queue, as detailed in @mq, on a port,
14445 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14447 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14448 * is used to get the entry count and entry size that are necessary to
14449 * determine the number of pages to allocate and use for this queue. This
14450 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14451 * mailbox queue. This function is asynchronous and will wait for the mailbox
14452 * command to finish before continuing.
14454 * On success this function will return a zero. If unable to allocate enough
14455 * memory this function will return -ENOMEM. If the queue create mailbox command
14456 * fails this function will return -ENXIO.
14458 int32_t
14459 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14460 struct lpfc_queue *cq, uint32_t subtype)
14462 struct lpfc_mbx_mq_create *mq_create;
14463 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14464 struct lpfc_dmabuf *dmabuf;
14465 LPFC_MBOXQ_t *mbox;
14466 int rc, length, status = 0;
14467 uint32_t shdr_status, shdr_add_status;
14468 union lpfc_sli4_cfg_shdr *shdr;
14469 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14471 /* sanity check on queue memory */
14472 if (!mq || !cq)
14473 return -ENODEV;
14474 if (!phba->sli4_hba.pc_sli4_params.supported)
14475 hw_page_size = SLI4_PAGE_SIZE;
14477 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14478 if (!mbox)
14479 return -ENOMEM;
14480 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14481 sizeof(struct lpfc_sli4_cfg_mhdr));
14482 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14483 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14484 length, LPFC_SLI4_MBX_EMBED);
14486 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
14487 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
14488 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14489 &mq_create_ext->u.request, mq->page_count);
14490 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14491 &mq_create_ext->u.request, 1);
14492 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
14493 &mq_create_ext->u.request, 1);
14494 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14495 &mq_create_ext->u.request, 1);
14496 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14497 &mq_create_ext->u.request, 1);
14498 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14499 &mq_create_ext->u.request, 1);
14500 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
14501 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14502 phba->sli4_hba.pc_sli4_params.mqv);
14503 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14504 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14505 cq->queue_id);
14506 else
14507 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14508 cq->queue_id);
14509 switch (mq->entry_count) {
14510 default:
14511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14512 "0362 Unsupported MQ count. (%d)\n",
14513 mq->entry_count);
14514 if (mq->entry_count < 16) {
14515 status = -EINVAL;
14516 goto out;
14518 /* otherwise default to smallest count (drop through) */
14519 case 16:
14520 bf_set(lpfc_mq_context_ring_size,
14521 &mq_create_ext->u.request.context,
14522 LPFC_MQ_RING_SIZE_16);
14523 break;
14524 case 32:
14525 bf_set(lpfc_mq_context_ring_size,
14526 &mq_create_ext->u.request.context,
14527 LPFC_MQ_RING_SIZE_32);
14528 break;
14529 case 64:
14530 bf_set(lpfc_mq_context_ring_size,
14531 &mq_create_ext->u.request.context,
14532 LPFC_MQ_RING_SIZE_64);
14533 break;
14534 case 128:
14535 bf_set(lpfc_mq_context_ring_size,
14536 &mq_create_ext->u.request.context,
14537 LPFC_MQ_RING_SIZE_128);
14538 break;
14540 list_for_each_entry(dmabuf, &mq->page_list, list) {
14541 memset(dmabuf->virt, 0, hw_page_size);
14542 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14543 putPaddrLow(dmabuf->phys);
14544 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
14545 putPaddrHigh(dmabuf->phys);
14547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14548 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14549 &mq_create_ext->u.response);
14550 if (rc != MBX_SUCCESS) {
14551 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14552 "2795 MQ_CREATE_EXT failed with "
14553 "status x%x. Failback to MQ_CREATE.\n",
14554 rc);
14555 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14556 mq_create = &mbox->u.mqe.un.mq_create;
14557 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14558 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14559 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14560 &mq_create->u.response);
14563 /* The IOCTL status is embedded in the mailbox subheader. */
14564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14566 if (shdr_status || shdr_add_status || rc) {
14567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14568 "2502 MQ_CREATE mailbox failed with "
14569 "status x%x add_status x%x, mbx status x%x\n",
14570 shdr_status, shdr_add_status, rc);
14571 status = -ENXIO;
14572 goto out;
14574 if (mq->queue_id == 0xFFFF) {
14575 status = -ENXIO;
14576 goto out;
14578 mq->type = LPFC_MQ;
14579 mq->assoc_qid = cq->queue_id;
14580 mq->subtype = subtype;
14581 mq->host_index = 0;
14582 mq->hba_index = 0;
14584 /* link the mq onto the parent cq child list */
14585 list_add_tail(&mq->list, &cq->child_list);
14586 out:
14587 mempool_free(mbox, phba->mbox_mem_pool);
14588 return status;
14592 * lpfc_wq_create - Create a Work Queue on the HBA
14593 * @phba: HBA structure that indicates port to create a queue on.
14594 * @wq: The queue structure to use to create the work queue.
14595 * @cq: The completion queue to bind this work queue to.
14596 * @subtype: The subtype of the work queue indicating its functionality.
14598 * This function creates a work queue, as detailed in @wq, on a port, described
14599 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14601 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14602 * is used to get the entry count and entry size that are necessary to
14603 * determine the number of pages to allocate and use for this queue. The @cq
14604 * is used to indicate which completion queue to bind this work queue to. This
14605 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14606 * work queue. This function is asynchronous and will wait for the mailbox
14607 * command to finish before continuing.
14609 * On success this function will return a zero. If unable to allocate enough
14610 * memory this function will return -ENOMEM. If the queue create mailbox command
14611 * fails this function will return -ENXIO.
14614 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14615 struct lpfc_queue *cq, uint32_t subtype)
14617 struct lpfc_mbx_wq_create *wq_create;
14618 struct lpfc_dmabuf *dmabuf;
14619 LPFC_MBOXQ_t *mbox;
14620 int rc, length, status = 0;
14621 uint32_t shdr_status, shdr_add_status;
14622 union lpfc_sli4_cfg_shdr *shdr;
14623 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14624 struct dma_address *page;
14625 void __iomem *bar_memmap_p;
14626 uint32_t db_offset;
14627 uint16_t pci_barset;
14629 /* sanity check on queue memory */
14630 if (!wq || !cq)
14631 return -ENODEV;
14632 if (!phba->sli4_hba.pc_sli4_params.supported)
14633 hw_page_size = SLI4_PAGE_SIZE;
14635 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14636 if (!mbox)
14637 return -ENOMEM;
14638 length = (sizeof(struct lpfc_mbx_wq_create) -
14639 sizeof(struct lpfc_sli4_cfg_mhdr));
14640 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14641 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14642 length, LPFC_SLI4_MBX_EMBED);
14643 wq_create = &mbox->u.mqe.un.wq_create;
14644 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
14645 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14646 wq->page_count);
14647 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14648 cq->queue_id);
14650 /* wqv is the earliest version supported, NOT the latest */
14651 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14652 phba->sli4_hba.pc_sli4_params.wqv);
14654 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14655 case LPFC_Q_CREATE_VERSION_0:
14656 switch (wq->entry_size) {
14657 default:
14658 case 64:
14659 /* Nothing to do, version 0 ONLY supports 64 byte */
14660 page = wq_create->u.request.page;
14661 break;
14662 case 128:
14663 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14664 LPFC_WQ_SZ128_SUPPORT)) {
14665 status = -ERANGE;
14666 goto out;
14668 /* If we get here the HBA MUST also support V1 and
14669 * we MUST use it
14671 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14672 LPFC_Q_CREATE_VERSION_1);
14674 bf_set(lpfc_mbx_wq_create_wqe_count,
14675 &wq_create->u.request_1, wq->entry_count);
14676 bf_set(lpfc_mbx_wq_create_wqe_size,
14677 &wq_create->u.request_1,
14678 LPFC_WQ_WQE_SIZE_128);
14679 bf_set(lpfc_mbx_wq_create_page_size,
14680 &wq_create->u.request_1,
14681 LPFC_WQ_PAGE_SIZE_4096);
14682 page = wq_create->u.request_1.page;
14683 break;
14685 break;
14686 case LPFC_Q_CREATE_VERSION_1:
14687 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14688 wq->entry_count);
14689 switch (wq->entry_size) {
14690 default:
14691 case 64:
14692 bf_set(lpfc_mbx_wq_create_wqe_size,
14693 &wq_create->u.request_1,
14694 LPFC_WQ_WQE_SIZE_64);
14695 break;
14696 case 128:
14697 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14698 LPFC_WQ_SZ128_SUPPORT)) {
14699 status = -ERANGE;
14700 goto out;
14702 bf_set(lpfc_mbx_wq_create_wqe_size,
14703 &wq_create->u.request_1,
14704 LPFC_WQ_WQE_SIZE_128);
14705 break;
14707 bf_set(lpfc_mbx_wq_create_page_size,
14708 &wq_create->u.request_1,
14709 LPFC_WQ_PAGE_SIZE_4096);
14710 page = wq_create->u.request_1.page;
14711 break;
14712 default:
14713 status = -ERANGE;
14714 goto out;
14717 list_for_each_entry(dmabuf, &wq->page_list, list) {
14718 memset(dmabuf->virt, 0, hw_page_size);
14719 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14720 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
14723 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14724 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14726 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14727 /* The IOCTL status is embedded in the mailbox subheader. */
14728 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14729 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14730 if (shdr_status || shdr_add_status || rc) {
14731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14732 "2503 WQ_CREATE mailbox failed with "
14733 "status x%x add_status x%x, mbx status x%x\n",
14734 shdr_status, shdr_add_status, rc);
14735 status = -ENXIO;
14736 goto out;
14738 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14739 if (wq->queue_id == 0xFFFF) {
14740 status = -ENXIO;
14741 goto out;
14743 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14744 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14745 &wq_create->u.response);
14746 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14747 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14749 "3265 WQ[%d] doorbell format not "
14750 "supported: x%x\n", wq->queue_id,
14751 wq->db_format);
14752 status = -EINVAL;
14753 goto out;
14755 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14756 &wq_create->u.response);
14757 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14758 if (!bar_memmap_p) {
14759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14760 "3263 WQ[%d] failed to memmap pci "
14761 "barset:x%x\n", wq->queue_id,
14762 pci_barset);
14763 status = -ENOMEM;
14764 goto out;
14766 db_offset = wq_create->u.response.doorbell_offset;
14767 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14768 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14770 "3252 WQ[%d] doorbell offset not "
14771 "supported: x%x\n", wq->queue_id,
14772 db_offset);
14773 status = -EINVAL;
14774 goto out;
14776 wq->db_regaddr = bar_memmap_p + db_offset;
14777 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14778 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14779 "format:x%x\n", wq->queue_id, pci_barset,
14780 db_offset, wq->db_format);
14781 } else {
14782 wq->db_format = LPFC_DB_LIST_FORMAT;
14783 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14785 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14786 if (wq->pring == NULL) {
14787 status = -ENOMEM;
14788 goto out;
14790 wq->type = LPFC_WQ;
14791 wq->assoc_qid = cq->queue_id;
14792 wq->subtype = subtype;
14793 wq->host_index = 0;
14794 wq->hba_index = 0;
14795 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
14797 /* link the wq onto the parent cq child list */
14798 list_add_tail(&wq->list, &cq->child_list);
14799 out:
14800 mempool_free(mbox, phba->mbox_mem_pool);
14801 return status;
14805 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14806 * @phba: HBA structure that indicates port to create a queue on.
14807 * @rq: The queue structure to use for the receive queue.
14808 * @qno: The associated HBQ number
14811 * For SLI4 we need to adjust the RQ repost value based on
14812 * the number of buffers that are initially posted to the RQ.
14814 void
14815 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14817 uint32_t cnt;
14819 /* sanity check on queue memory */
14820 if (!rq)
14821 return;
14822 cnt = lpfc_hbq_defs[qno]->entry_count;
14824 /* Recalc repost for RQs based on buffers initially posted */
14825 cnt = (cnt >> 3);
14826 if (cnt < LPFC_QUEUE_MIN_REPOST)
14827 cnt = LPFC_QUEUE_MIN_REPOST;
14829 rq->entry_repost = cnt;
14833 * lpfc_rq_create - Create a Receive Queue on the HBA
14834 * @phba: HBA structure that indicates port to create a queue on.
14835 * @hrq: The queue structure to use to create the header receive queue.
14836 * @drq: The queue structure to use to create the data receive queue.
14837 * @cq: The completion queue to bind this work queue to.
14839 * This function creates a receive buffer queue pair , as detailed in @hrq and
14840 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14841 * to the HBA.
14843 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14844 * struct is used to get the entry count that is necessary to determine the
14845 * number of pages to use for this queue. The @cq is used to indicate which
14846 * completion queue to bind received buffers that are posted to these queues to.
14847 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14848 * receive queue pair. This function is asynchronous and will wait for the
14849 * mailbox command to finish before continuing.
14851 * On success this function will return a zero. If unable to allocate enough
14852 * memory this function will return -ENOMEM. If the queue create mailbox command
14853 * fails this function will return -ENXIO.
14856 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14857 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14859 struct lpfc_mbx_rq_create *rq_create;
14860 struct lpfc_dmabuf *dmabuf;
14861 LPFC_MBOXQ_t *mbox;
14862 int rc, length, status = 0;
14863 uint32_t shdr_status, shdr_add_status;
14864 union lpfc_sli4_cfg_shdr *shdr;
14865 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14866 void __iomem *bar_memmap_p;
14867 uint32_t db_offset;
14868 uint16_t pci_barset;
14870 /* sanity check on queue memory */
14871 if (!hrq || !drq || !cq)
14872 return -ENODEV;
14873 if (!phba->sli4_hba.pc_sli4_params.supported)
14874 hw_page_size = SLI4_PAGE_SIZE;
14876 if (hrq->entry_count != drq->entry_count)
14877 return -EINVAL;
14878 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14879 if (!mbox)
14880 return -ENOMEM;
14881 length = (sizeof(struct lpfc_mbx_rq_create) -
14882 sizeof(struct lpfc_sli4_cfg_mhdr));
14883 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14884 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14885 length, LPFC_SLI4_MBX_EMBED);
14886 rq_create = &mbox->u.mqe.un.rq_create;
14887 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14888 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14889 phba->sli4_hba.pc_sli4_params.rqv);
14890 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14891 bf_set(lpfc_rq_context_rqe_count_1,
14892 &rq_create->u.request.context,
14893 hrq->entry_count);
14894 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
14895 bf_set(lpfc_rq_context_rqe_size,
14896 &rq_create->u.request.context,
14897 LPFC_RQE_SIZE_8);
14898 bf_set(lpfc_rq_context_page_size,
14899 &rq_create->u.request.context,
14900 LPFC_RQ_PAGE_SIZE_4096);
14901 } else {
14902 switch (hrq->entry_count) {
14903 default:
14904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14905 "2535 Unsupported RQ count. (%d)\n",
14906 hrq->entry_count);
14907 if (hrq->entry_count < 512) {
14908 status = -EINVAL;
14909 goto out;
14911 /* otherwise default to smallest count (drop through) */
14912 case 512:
14913 bf_set(lpfc_rq_context_rqe_count,
14914 &rq_create->u.request.context,
14915 LPFC_RQ_RING_SIZE_512);
14916 break;
14917 case 1024:
14918 bf_set(lpfc_rq_context_rqe_count,
14919 &rq_create->u.request.context,
14920 LPFC_RQ_RING_SIZE_1024);
14921 break;
14922 case 2048:
14923 bf_set(lpfc_rq_context_rqe_count,
14924 &rq_create->u.request.context,
14925 LPFC_RQ_RING_SIZE_2048);
14926 break;
14927 case 4096:
14928 bf_set(lpfc_rq_context_rqe_count,
14929 &rq_create->u.request.context,
14930 LPFC_RQ_RING_SIZE_4096);
14931 break;
14933 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14934 LPFC_HDR_BUF_SIZE);
14936 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14937 cq->queue_id);
14938 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14939 hrq->page_count);
14940 list_for_each_entry(dmabuf, &hrq->page_list, list) {
14941 memset(dmabuf->virt, 0, hw_page_size);
14942 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14943 putPaddrLow(dmabuf->phys);
14944 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14945 putPaddrHigh(dmabuf->phys);
14947 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14948 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14950 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14951 /* The IOCTL status is embedded in the mailbox subheader. */
14952 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14953 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14954 if (shdr_status || shdr_add_status || rc) {
14955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14956 "2504 RQ_CREATE mailbox failed with "
14957 "status x%x add_status x%x, mbx status x%x\n",
14958 shdr_status, shdr_add_status, rc);
14959 status = -ENXIO;
14960 goto out;
14962 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14963 if (hrq->queue_id == 0xFFFF) {
14964 status = -ENXIO;
14965 goto out;
14968 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14969 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14970 &rq_create->u.response);
14971 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14972 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14974 "3262 RQ [%d] doorbell format not "
14975 "supported: x%x\n", hrq->queue_id,
14976 hrq->db_format);
14977 status = -EINVAL;
14978 goto out;
14981 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14982 &rq_create->u.response);
14983 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14984 if (!bar_memmap_p) {
14985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14986 "3269 RQ[%d] failed to memmap pci "
14987 "barset:x%x\n", hrq->queue_id,
14988 pci_barset);
14989 status = -ENOMEM;
14990 goto out;
14993 db_offset = rq_create->u.response.doorbell_offset;
14994 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14995 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14997 "3270 RQ[%d] doorbell offset not "
14998 "supported: x%x\n", hrq->queue_id,
14999 db_offset);
15000 status = -EINVAL;
15001 goto out;
15003 hrq->db_regaddr = bar_memmap_p + db_offset;
15004 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15005 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15006 "format:x%x\n", hrq->queue_id, pci_barset,
15007 db_offset, hrq->db_format);
15008 } else {
15009 hrq->db_format = LPFC_DB_RING_FORMAT;
15010 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15012 hrq->type = LPFC_HRQ;
15013 hrq->assoc_qid = cq->queue_id;
15014 hrq->subtype = subtype;
15015 hrq->host_index = 0;
15016 hrq->hba_index = 0;
15018 /* now create the data queue */
15019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15020 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15021 length, LPFC_SLI4_MBX_EMBED);
15022 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15023 phba->sli4_hba.pc_sli4_params.rqv);
15024 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15025 bf_set(lpfc_rq_context_rqe_count_1,
15026 &rq_create->u.request.context, hrq->entry_count);
15027 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
15028 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15029 LPFC_RQE_SIZE_8);
15030 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15031 (PAGE_SIZE/SLI4_PAGE_SIZE));
15032 } else {
15033 switch (drq->entry_count) {
15034 default:
15035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15036 "2536 Unsupported RQ count. (%d)\n",
15037 drq->entry_count);
15038 if (drq->entry_count < 512) {
15039 status = -EINVAL;
15040 goto out;
15042 /* otherwise default to smallest count (drop through) */
15043 case 512:
15044 bf_set(lpfc_rq_context_rqe_count,
15045 &rq_create->u.request.context,
15046 LPFC_RQ_RING_SIZE_512);
15047 break;
15048 case 1024:
15049 bf_set(lpfc_rq_context_rqe_count,
15050 &rq_create->u.request.context,
15051 LPFC_RQ_RING_SIZE_1024);
15052 break;
15053 case 2048:
15054 bf_set(lpfc_rq_context_rqe_count,
15055 &rq_create->u.request.context,
15056 LPFC_RQ_RING_SIZE_2048);
15057 break;
15058 case 4096:
15059 bf_set(lpfc_rq_context_rqe_count,
15060 &rq_create->u.request.context,
15061 LPFC_RQ_RING_SIZE_4096);
15062 break;
15064 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15065 LPFC_DATA_BUF_SIZE);
15067 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15068 cq->queue_id);
15069 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15070 drq->page_count);
15071 list_for_each_entry(dmabuf, &drq->page_list, list) {
15072 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15073 putPaddrLow(dmabuf->phys);
15074 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15075 putPaddrHigh(dmabuf->phys);
15077 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15078 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15079 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15080 /* The IOCTL status is embedded in the mailbox subheader. */
15081 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15082 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15083 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15084 if (shdr_status || shdr_add_status || rc) {
15085 status = -ENXIO;
15086 goto out;
15088 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15089 if (drq->queue_id == 0xFFFF) {
15090 status = -ENXIO;
15091 goto out;
15093 drq->type = LPFC_DRQ;
15094 drq->assoc_qid = cq->queue_id;
15095 drq->subtype = subtype;
15096 drq->host_index = 0;
15097 drq->hba_index = 0;
15099 /* link the header and data RQs onto the parent cq child list */
15100 list_add_tail(&hrq->list, &cq->child_list);
15101 list_add_tail(&drq->list, &cq->child_list);
15103 out:
15104 mempool_free(mbox, phba->mbox_mem_pool);
15105 return status;
15109 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15110 * @phba: HBA structure that indicates port to create a queue on.
15111 * @hrqp: The queue structure array to use to create the header receive queues.
15112 * @drqp: The queue structure array to use to create the data receive queues.
15113 * @cqp: The completion queue array to bind these receive queues to.
15115 * This function creates a receive buffer queue pair , as detailed in @hrq and
15116 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15117 * to the HBA.
15119 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15120 * struct is used to get the entry count that is necessary to determine the
15121 * number of pages to use for this queue. The @cq is used to indicate which
15122 * completion queue to bind received buffers that are posted to these queues to.
15123 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15124 * receive queue pair. This function is asynchronous and will wait for the
15125 * mailbox command to finish before continuing.
15127 * On success this function will return a zero. If unable to allocate enough
15128 * memory this function will return -ENOMEM. If the queue create mailbox command
15129 * fails this function will return -ENXIO.
15132 lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15133 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15134 uint32_t subtype)
15136 struct lpfc_queue *hrq, *drq, *cq;
15137 struct lpfc_mbx_rq_create_v2 *rq_create;
15138 struct lpfc_dmabuf *dmabuf;
15139 LPFC_MBOXQ_t *mbox;
15140 int rc, length, alloclen, status = 0;
15141 int cnt, idx, numrq, page_idx = 0;
15142 uint32_t shdr_status, shdr_add_status;
15143 union lpfc_sli4_cfg_shdr *shdr;
15144 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15146 numrq = phba->cfg_nvmet_mrq;
15147 /* sanity check on array memory */
15148 if (!hrqp || !drqp || !cqp || !numrq)
15149 return -ENODEV;
15150 if (!phba->sli4_hba.pc_sli4_params.supported)
15151 hw_page_size = SLI4_PAGE_SIZE;
15153 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15154 if (!mbox)
15155 return -ENOMEM;
15157 length = sizeof(struct lpfc_mbx_rq_create_v2);
15158 length += ((2 * numrq * hrqp[0]->page_count) *
15159 sizeof(struct dma_address));
15161 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15162 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15163 LPFC_SLI4_MBX_NEMBED);
15164 if (alloclen < length) {
15165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15166 "3099 Allocated DMA memory size (%d) is "
15167 "less than the requested DMA memory size "
15168 "(%d)\n", alloclen, length);
15169 status = -ENOMEM;
15170 goto out;
15175 rq_create = mbox->sge_array->addr[0];
15176 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15178 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15179 cnt = 0;
15181 for (idx = 0; idx < numrq; idx++) {
15182 hrq = hrqp[idx];
15183 drq = drqp[idx];
15184 cq = cqp[idx];
15186 if (hrq->entry_count != drq->entry_count) {
15187 status = -EINVAL;
15188 goto out;
15191 /* sanity check on queue memory */
15192 if (!hrq || !drq || !cq) {
15193 status = -ENODEV;
15194 goto out;
15197 if (idx == 0) {
15198 bf_set(lpfc_mbx_rq_create_num_pages,
15199 &rq_create->u.request,
15200 hrq->page_count);
15201 bf_set(lpfc_mbx_rq_create_rq_cnt,
15202 &rq_create->u.request, (numrq * 2));
15203 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15205 bf_set(lpfc_rq_context_base_cq,
15206 &rq_create->u.request.context,
15207 cq->queue_id);
15208 bf_set(lpfc_rq_context_data_size,
15209 &rq_create->u.request.context,
15210 LPFC_DATA_BUF_SIZE);
15211 bf_set(lpfc_rq_context_hdr_size,
15212 &rq_create->u.request.context,
15213 LPFC_HDR_BUF_SIZE);
15214 bf_set(lpfc_rq_context_rqe_count_1,
15215 &rq_create->u.request.context,
15216 hrq->entry_count);
15217 bf_set(lpfc_rq_context_rqe_size,
15218 &rq_create->u.request.context,
15219 LPFC_RQE_SIZE_8);
15220 bf_set(lpfc_rq_context_page_size,
15221 &rq_create->u.request.context,
15222 (PAGE_SIZE/SLI4_PAGE_SIZE));
15224 rc = 0;
15225 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15226 memset(dmabuf->virt, 0, hw_page_size);
15227 cnt = page_idx + dmabuf->buffer_tag;
15228 rq_create->u.request.page[cnt].addr_lo =
15229 putPaddrLow(dmabuf->phys);
15230 rq_create->u.request.page[cnt].addr_hi =
15231 putPaddrHigh(dmabuf->phys);
15232 rc++;
15234 page_idx += rc;
15236 rc = 0;
15237 list_for_each_entry(dmabuf, &drq->page_list, list) {
15238 memset(dmabuf->virt, 0, hw_page_size);
15239 cnt = page_idx + dmabuf->buffer_tag;
15240 rq_create->u.request.page[cnt].addr_lo =
15241 putPaddrLow(dmabuf->phys);
15242 rq_create->u.request.page[cnt].addr_hi =
15243 putPaddrHigh(dmabuf->phys);
15244 rc++;
15246 page_idx += rc;
15248 hrq->db_format = LPFC_DB_RING_FORMAT;
15249 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15250 hrq->type = LPFC_HRQ;
15251 hrq->assoc_qid = cq->queue_id;
15252 hrq->subtype = subtype;
15253 hrq->host_index = 0;
15254 hrq->hba_index = 0;
15256 drq->db_format = LPFC_DB_RING_FORMAT;
15257 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15258 drq->type = LPFC_DRQ;
15259 drq->assoc_qid = cq->queue_id;
15260 drq->subtype = subtype;
15261 drq->host_index = 0;
15262 drq->hba_index = 0;
15264 list_add_tail(&hrq->list, &cq->child_list);
15265 list_add_tail(&drq->list, &cq->child_list);
15268 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15269 /* The IOCTL status is embedded in the mailbox subheader. */
15270 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15271 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15272 if (shdr_status || shdr_add_status || rc) {
15273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15274 "3120 RQ_CREATE mailbox failed with "
15275 "status x%x add_status x%x, mbx status x%x\n",
15276 shdr_status, shdr_add_status, rc);
15277 status = -ENXIO;
15278 goto out;
15280 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15281 if (rc == 0xFFFF) {
15282 status = -ENXIO;
15283 goto out;
15286 /* Initialize all RQs with associated queue id */
15287 for (idx = 0; idx < numrq; idx++) {
15288 hrq = hrqp[idx];
15289 hrq->queue_id = rc + (2 * idx);
15290 drq = drqp[idx];
15291 drq->queue_id = rc + (2 * idx) + 1;
15294 out:
15295 lpfc_sli4_mbox_cmd_free(phba, mbox);
15296 return status;
15300 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15301 * @eq: The queue structure associated with the queue to destroy.
15303 * This function destroys a queue, as detailed in @eq by sending an mailbox
15304 * command, specific to the type of queue, to the HBA.
15306 * The @eq struct is used to get the queue ID of the queue to destroy.
15308 * On success this function will return a zero. If the queue destroy mailbox
15309 * command fails this function will return -ENXIO.
15312 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15314 LPFC_MBOXQ_t *mbox;
15315 int rc, length, status = 0;
15316 uint32_t shdr_status, shdr_add_status;
15317 union lpfc_sli4_cfg_shdr *shdr;
15319 /* sanity check on queue memory */
15320 if (!eq)
15321 return -ENODEV;
15322 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15323 if (!mbox)
15324 return -ENOMEM;
15325 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15326 sizeof(struct lpfc_sli4_cfg_mhdr));
15327 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15328 LPFC_MBOX_OPCODE_EQ_DESTROY,
15329 length, LPFC_SLI4_MBX_EMBED);
15330 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15331 eq->queue_id);
15332 mbox->vport = eq->phba->pport;
15333 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15335 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15336 /* The IOCTL status is embedded in the mailbox subheader. */
15337 shdr = (union lpfc_sli4_cfg_shdr *)
15338 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15339 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15340 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15341 if (shdr_status || shdr_add_status || rc) {
15342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15343 "2505 EQ_DESTROY mailbox failed with "
15344 "status x%x add_status x%x, mbx status x%x\n",
15345 shdr_status, shdr_add_status, rc);
15346 status = -ENXIO;
15349 /* Remove eq from any list */
15350 list_del_init(&eq->list);
15351 mempool_free(mbox, eq->phba->mbox_mem_pool);
15352 return status;
15356 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15357 * @cq: The queue structure associated with the queue to destroy.
15359 * This function destroys a queue, as detailed in @cq by sending an mailbox
15360 * command, specific to the type of queue, to the HBA.
15362 * The @cq struct is used to get the queue ID of the queue to destroy.
15364 * On success this function will return a zero. If the queue destroy mailbox
15365 * command fails this function will return -ENXIO.
15368 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15370 LPFC_MBOXQ_t *mbox;
15371 int rc, length, status = 0;
15372 uint32_t shdr_status, shdr_add_status;
15373 union lpfc_sli4_cfg_shdr *shdr;
15375 /* sanity check on queue memory */
15376 if (!cq)
15377 return -ENODEV;
15378 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15379 if (!mbox)
15380 return -ENOMEM;
15381 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15382 sizeof(struct lpfc_sli4_cfg_mhdr));
15383 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15384 LPFC_MBOX_OPCODE_CQ_DESTROY,
15385 length, LPFC_SLI4_MBX_EMBED);
15386 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15387 cq->queue_id);
15388 mbox->vport = cq->phba->pport;
15389 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15390 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15391 /* The IOCTL status is embedded in the mailbox subheader. */
15392 shdr = (union lpfc_sli4_cfg_shdr *)
15393 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15396 if (shdr_status || shdr_add_status || rc) {
15397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15398 "2506 CQ_DESTROY mailbox failed with "
15399 "status x%x add_status x%x, mbx status x%x\n",
15400 shdr_status, shdr_add_status, rc);
15401 status = -ENXIO;
15403 /* Remove cq from any list */
15404 list_del_init(&cq->list);
15405 mempool_free(mbox, cq->phba->mbox_mem_pool);
15406 return status;
15410 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15411 * @qm: The queue structure associated with the queue to destroy.
15413 * This function destroys a queue, as detailed in @mq by sending an mailbox
15414 * command, specific to the type of queue, to the HBA.
15416 * The @mq struct is used to get the queue ID of the queue to destroy.
15418 * On success this function will return a zero. If the queue destroy mailbox
15419 * command fails this function will return -ENXIO.
15422 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15424 LPFC_MBOXQ_t *mbox;
15425 int rc, length, status = 0;
15426 uint32_t shdr_status, shdr_add_status;
15427 union lpfc_sli4_cfg_shdr *shdr;
15429 /* sanity check on queue memory */
15430 if (!mq)
15431 return -ENODEV;
15432 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15433 if (!mbox)
15434 return -ENOMEM;
15435 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15436 sizeof(struct lpfc_sli4_cfg_mhdr));
15437 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15438 LPFC_MBOX_OPCODE_MQ_DESTROY,
15439 length, LPFC_SLI4_MBX_EMBED);
15440 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15441 mq->queue_id);
15442 mbox->vport = mq->phba->pport;
15443 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15444 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15445 /* The IOCTL status is embedded in the mailbox subheader. */
15446 shdr = (union lpfc_sli4_cfg_shdr *)
15447 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15448 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15449 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15450 if (shdr_status || shdr_add_status || rc) {
15451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15452 "2507 MQ_DESTROY mailbox failed with "
15453 "status x%x add_status x%x, mbx status x%x\n",
15454 shdr_status, shdr_add_status, rc);
15455 status = -ENXIO;
15457 /* Remove mq from any list */
15458 list_del_init(&mq->list);
15459 mempool_free(mbox, mq->phba->mbox_mem_pool);
15460 return status;
15464 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15465 * @wq: The queue structure associated with the queue to destroy.
15467 * This function destroys a queue, as detailed in @wq by sending an mailbox
15468 * command, specific to the type of queue, to the HBA.
15470 * The @wq struct is used to get the queue ID of the queue to destroy.
15472 * On success this function will return a zero. If the queue destroy mailbox
15473 * command fails this function will return -ENXIO.
15476 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15478 LPFC_MBOXQ_t *mbox;
15479 int rc, length, status = 0;
15480 uint32_t shdr_status, shdr_add_status;
15481 union lpfc_sli4_cfg_shdr *shdr;
15483 /* sanity check on queue memory */
15484 if (!wq)
15485 return -ENODEV;
15486 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15487 if (!mbox)
15488 return -ENOMEM;
15489 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15490 sizeof(struct lpfc_sli4_cfg_mhdr));
15491 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15492 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15493 length, LPFC_SLI4_MBX_EMBED);
15494 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15495 wq->queue_id);
15496 mbox->vport = wq->phba->pport;
15497 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15498 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15499 shdr = (union lpfc_sli4_cfg_shdr *)
15500 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15503 if (shdr_status || shdr_add_status || rc) {
15504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15505 "2508 WQ_DESTROY mailbox failed with "
15506 "status x%x add_status x%x, mbx status x%x\n",
15507 shdr_status, shdr_add_status, rc);
15508 status = -ENXIO;
15510 /* Remove wq from any list */
15511 list_del_init(&wq->list);
15512 mempool_free(mbox, wq->phba->mbox_mem_pool);
15513 return status;
15517 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15518 * @rq: The queue structure associated with the queue to destroy.
15520 * This function destroys a queue, as detailed in @rq by sending an mailbox
15521 * command, specific to the type of queue, to the HBA.
15523 * The @rq struct is used to get the queue ID of the queue to destroy.
15525 * On success this function will return a zero. If the queue destroy mailbox
15526 * command fails this function will return -ENXIO.
15529 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15530 struct lpfc_queue *drq)
15532 LPFC_MBOXQ_t *mbox;
15533 int rc, length, status = 0;
15534 uint32_t shdr_status, shdr_add_status;
15535 union lpfc_sli4_cfg_shdr *shdr;
15537 /* sanity check on queue memory */
15538 if (!hrq || !drq)
15539 return -ENODEV;
15540 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15541 if (!mbox)
15542 return -ENOMEM;
15543 length = (sizeof(struct lpfc_mbx_rq_destroy) -
15544 sizeof(struct lpfc_sli4_cfg_mhdr));
15545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15546 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15547 length, LPFC_SLI4_MBX_EMBED);
15548 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15549 hrq->queue_id);
15550 mbox->vport = hrq->phba->pport;
15551 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15552 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15553 /* The IOCTL status is embedded in the mailbox subheader. */
15554 shdr = (union lpfc_sli4_cfg_shdr *)
15555 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15556 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15557 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15558 if (shdr_status || shdr_add_status || rc) {
15559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15560 "2509 RQ_DESTROY mailbox failed with "
15561 "status x%x add_status x%x, mbx status x%x\n",
15562 shdr_status, shdr_add_status, rc);
15563 if (rc != MBX_TIMEOUT)
15564 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15565 return -ENXIO;
15567 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15568 drq->queue_id);
15569 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15570 shdr = (union lpfc_sli4_cfg_shdr *)
15571 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15572 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15573 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15574 if (shdr_status || shdr_add_status || rc) {
15575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15576 "2510 RQ_DESTROY mailbox failed with "
15577 "status x%x add_status x%x, mbx status x%x\n",
15578 shdr_status, shdr_add_status, rc);
15579 status = -ENXIO;
15581 list_del_init(&hrq->list);
15582 list_del_init(&drq->list);
15583 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15584 return status;
15588 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15589 * @phba: The virtual port for which this call being executed.
15590 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15591 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15592 * @xritag: the xritag that ties this io to the SGL pages.
15594 * This routine will post the sgl pages for the IO that has the xritag
15595 * that is in the iocbq structure. The xritag is assigned during iocbq
15596 * creation and persists for as long as the driver is loaded.
15597 * if the caller has fewer than 256 scatter gather segments to map then
15598 * pdma_phys_addr1 should be 0.
15599 * If the caller needs to map more than 256 scatter gather segment then
15600 * pdma_phys_addr1 should be a valid physical address.
15601 * physical address for SGLs must be 64 byte aligned.
15602 * If you are going to map 2 SGL's then the first one must have 256 entries
15603 * the second sgl can have between 1 and 256 entries.
15605 * Return codes:
15606 * 0 - Success
15607 * -ENXIO, -ENOMEM - Failure
15610 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15611 dma_addr_t pdma_phys_addr0,
15612 dma_addr_t pdma_phys_addr1,
15613 uint16_t xritag)
15615 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15616 LPFC_MBOXQ_t *mbox;
15617 int rc;
15618 uint32_t shdr_status, shdr_add_status;
15619 uint32_t mbox_tmo;
15620 union lpfc_sli4_cfg_shdr *shdr;
15622 if (xritag == NO_XRI) {
15623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15624 "0364 Invalid param:\n");
15625 return -EINVAL;
15628 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15629 if (!mbox)
15630 return -ENOMEM;
15632 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15633 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15634 sizeof(struct lpfc_mbx_post_sgl_pages) -
15635 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15637 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15638 &mbox->u.mqe.un.post_sgl_pages;
15639 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15640 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15642 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15643 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15644 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15645 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15647 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15648 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15649 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15650 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15651 if (!phba->sli4_hba.intr_enable)
15652 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15653 else {
15654 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15655 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15657 /* The IOCTL status is embedded in the mailbox subheader. */
15658 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15659 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15660 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15661 if (rc != MBX_TIMEOUT)
15662 mempool_free(mbox, phba->mbox_mem_pool);
15663 if (shdr_status || shdr_add_status || rc) {
15664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15665 "2511 POST_SGL mailbox failed with "
15666 "status x%x add_status x%x, mbx status x%x\n",
15667 shdr_status, shdr_add_status, rc);
15669 return 0;
15673 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
15674 * @phba: pointer to lpfc hba data structure.
15676 * This routine is invoked to post rpi header templates to the
15677 * HBA consistent with the SLI-4 interface spec. This routine
15678 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15679 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15681 * Returns
15682 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15683 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15685 static uint16_t
15686 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15688 unsigned long xri;
15691 * Fetch the next logical xri. Because this index is logical,
15692 * the driver starts at 0 each time.
15694 spin_lock_irq(&phba->hbalock);
15695 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15696 phba->sli4_hba.max_cfg_param.max_xri, 0);
15697 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15698 spin_unlock_irq(&phba->hbalock);
15699 return NO_XRI;
15700 } else {
15701 set_bit(xri, phba->sli4_hba.xri_bmask);
15702 phba->sli4_hba.max_cfg_param.xri_used++;
15704 spin_unlock_irq(&phba->hbalock);
15705 return xri;
15709 * lpfc_sli4_free_xri - Release an xri for reuse.
15710 * @phba: pointer to lpfc hba data structure.
15712 * This routine is invoked to release an xri to the pool of
15713 * available rpis maintained by the driver.
15715 static void
15716 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15718 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
15719 phba->sli4_hba.max_cfg_param.xri_used--;
15724 * lpfc_sli4_free_xri - Release an xri for reuse.
15725 * @phba: pointer to lpfc hba data structure.
15727 * This routine is invoked to release an xri to the pool of
15728 * available rpis maintained by the driver.
15730 void
15731 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15733 spin_lock_irq(&phba->hbalock);
15734 __lpfc_sli4_free_xri(phba, xri);
15735 spin_unlock_irq(&phba->hbalock);
15739 * lpfc_sli4_next_xritag - Get an xritag for the io
15740 * @phba: Pointer to HBA context object.
15742 * This function gets an xritag for the iocb. If there is no unused xritag
15743 * it will return 0xffff.
15744 * The function returns the allocated xritag if successful, else returns zero.
15745 * Zero is not a valid xritag.
15746 * The caller is not required to hold any lock.
15748 uint16_t
15749 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15751 uint16_t xri_index;
15753 xri_index = lpfc_sli4_alloc_xri(phba);
15754 if (xri_index == NO_XRI)
15755 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15756 "2004 Failed to allocate XRI.last XRITAG is %d"
15757 " Max XRI is %d, Used XRI is %d\n",
15758 xri_index,
15759 phba->sli4_hba.max_cfg_param.max_xri,
15760 phba->sli4_hba.max_cfg_param.xri_used);
15761 return xri_index;
15765 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
15766 * @phba: pointer to lpfc hba data structure.
15767 * @post_sgl_list: pointer to els sgl entry list.
15768 * @count: number of els sgl entries on the list.
15770 * This routine is invoked to post a block of driver's sgl pages to the
15771 * HBA using non-embedded mailbox command. No Lock is held. This routine
15772 * is only called when the driver is loading and after all IO has been
15773 * stopped.
15775 static int
15776 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
15777 struct list_head *post_sgl_list,
15778 int post_cnt)
15780 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
15781 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15782 struct sgl_page_pairs *sgl_pg_pairs;
15783 void *viraddr;
15784 LPFC_MBOXQ_t *mbox;
15785 uint32_t reqlen, alloclen, pg_pairs;
15786 uint32_t mbox_tmo;
15787 uint16_t xritag_start = 0;
15788 int rc = 0;
15789 uint32_t shdr_status, shdr_add_status;
15790 union lpfc_sli4_cfg_shdr *shdr;
15792 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
15793 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15794 if (reqlen > SLI4_PAGE_SIZE) {
15795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15796 "2559 Block sgl registration required DMA "
15797 "size (%d) great than a page\n", reqlen);
15798 return -ENOMEM;
15801 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15802 if (!mbox)
15803 return -ENOMEM;
15805 /* Allocate DMA memory and set up the non-embedded mailbox command */
15806 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15807 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15808 LPFC_SLI4_MBX_NEMBED);
15810 if (alloclen < reqlen) {
15811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15812 "0285 Allocated DMA memory size (%d) is "
15813 "less than the requested DMA memory "
15814 "size (%d)\n", alloclen, reqlen);
15815 lpfc_sli4_mbox_cmd_free(phba, mbox);
15816 return -ENOMEM;
15818 /* Set up the SGL pages in the non-embedded DMA pages */
15819 viraddr = mbox->sge_array->addr[0];
15820 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15821 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15823 pg_pairs = 0;
15824 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
15825 /* Set up the sge entry */
15826 sgl_pg_pairs->sgl_pg0_addr_lo =
15827 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15828 sgl_pg_pairs->sgl_pg0_addr_hi =
15829 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15830 sgl_pg_pairs->sgl_pg1_addr_lo =
15831 cpu_to_le32(putPaddrLow(0));
15832 sgl_pg_pairs->sgl_pg1_addr_hi =
15833 cpu_to_le32(putPaddrHigh(0));
15835 /* Keep the first xritag on the list */
15836 if (pg_pairs == 0)
15837 xritag_start = sglq_entry->sli4_xritag;
15838 sgl_pg_pairs++;
15839 pg_pairs++;
15842 /* Complete initialization and perform endian conversion. */
15843 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15844 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
15845 sgl->word0 = cpu_to_le32(sgl->word0);
15847 if (!phba->sli4_hba.intr_enable)
15848 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15849 else {
15850 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15851 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15853 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15854 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15855 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15856 if (rc != MBX_TIMEOUT)
15857 lpfc_sli4_mbox_cmd_free(phba, mbox);
15858 if (shdr_status || shdr_add_status || rc) {
15859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15860 "2513 POST_SGL_BLOCK mailbox command failed "
15861 "status x%x add_status x%x mbx status x%x\n",
15862 shdr_status, shdr_add_status, rc);
15863 rc = -ENXIO;
15865 return rc;
15869 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15870 * @phba: pointer to lpfc hba data structure.
15871 * @sblist: pointer to scsi buffer list.
15872 * @count: number of scsi buffers on the list.
15874 * This routine is invoked to post a block of @count scsi sgl pages from a
15875 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15876 * No Lock is held.
15880 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15881 struct list_head *sblist,
15882 int count)
15884 struct lpfc_scsi_buf *psb;
15885 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15886 struct sgl_page_pairs *sgl_pg_pairs;
15887 void *viraddr;
15888 LPFC_MBOXQ_t *mbox;
15889 uint32_t reqlen, alloclen, pg_pairs;
15890 uint32_t mbox_tmo;
15891 uint16_t xritag_start = 0;
15892 int rc = 0;
15893 uint32_t shdr_status, shdr_add_status;
15894 dma_addr_t pdma_phys_bpl1;
15895 union lpfc_sli4_cfg_shdr *shdr;
15897 /* Calculate the requested length of the dma memory */
15898 reqlen = count * sizeof(struct sgl_page_pairs) +
15899 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
15900 if (reqlen > SLI4_PAGE_SIZE) {
15901 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15902 "0217 Block sgl registration required DMA "
15903 "size (%d) great than a page\n", reqlen);
15904 return -ENOMEM;
15906 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15907 if (!mbox) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15909 "0283 Failed to allocate mbox cmd memory\n");
15910 return -ENOMEM;
15913 /* Allocate DMA memory and set up the non-embedded mailbox command */
15914 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15915 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15916 LPFC_SLI4_MBX_NEMBED);
15918 if (alloclen < reqlen) {
15919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15920 "2561 Allocated DMA memory size (%d) is "
15921 "less than the requested DMA memory "
15922 "size (%d)\n", alloclen, reqlen);
15923 lpfc_sli4_mbox_cmd_free(phba, mbox);
15924 return -ENOMEM;
15927 /* Get the first SGE entry from the non-embedded DMA memory */
15928 viraddr = mbox->sge_array->addr[0];
15930 /* Set up the SGL pages in the non-embedded DMA pages */
15931 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15932 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15934 pg_pairs = 0;
15935 list_for_each_entry(psb, sblist, list) {
15936 /* Set up the sge entry */
15937 sgl_pg_pairs->sgl_pg0_addr_lo =
15938 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15939 sgl_pg_pairs->sgl_pg0_addr_hi =
15940 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15941 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15942 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15943 else
15944 pdma_phys_bpl1 = 0;
15945 sgl_pg_pairs->sgl_pg1_addr_lo =
15946 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15947 sgl_pg_pairs->sgl_pg1_addr_hi =
15948 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
15949 /* Keep the first xritag on the list */
15950 if (pg_pairs == 0)
15951 xritag_start = psb->cur_iocbq.sli4_xritag;
15952 sgl_pg_pairs++;
15953 pg_pairs++;
15955 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15956 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
15957 /* Perform endian conversion if necessary */
15958 sgl->word0 = cpu_to_le32(sgl->word0);
15960 if (!phba->sli4_hba.intr_enable)
15961 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15962 else {
15963 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15964 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15966 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15967 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15968 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15969 if (rc != MBX_TIMEOUT)
15970 lpfc_sli4_mbox_cmd_free(phba, mbox);
15971 if (shdr_status || shdr_add_status || rc) {
15972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15973 "2564 POST_SGL_BLOCK mailbox command failed "
15974 "status x%x add_status x%x mbx status x%x\n",
15975 shdr_status, shdr_add_status, rc);
15976 rc = -ENXIO;
15978 return rc;
15981 static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
15982 static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
15985 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
15986 * @phba: pointer to lpfc_hba struct that the frame was received on
15987 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15989 * This function checks the fields in the @fc_hdr to see if the FC frame is a
15990 * valid type of frame that the LPFC driver will handle. This function will
15991 * return a zero if the frame is a valid frame or a non zero value when the
15992 * frame does not pass the check.
15994 static int
15995 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
15997 /* make rctl_names static to save stack space */
15998 struct fc_vft_header *fc_vft_hdr;
15999 uint32_t *header = (uint32_t *) fc_hdr;
16001 switch (fc_hdr->fh_r_ctl) {
16002 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16003 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16004 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16005 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16006 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16007 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16008 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16009 case FC_RCTL_DD_CMD_STATUS: /* command status */
16010 case FC_RCTL_ELS_REQ: /* extended link services request */
16011 case FC_RCTL_ELS_REP: /* extended link services reply */
16012 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16013 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16014 case FC_RCTL_BA_NOP: /* basic link service NOP */
16015 case FC_RCTL_BA_ABTS: /* basic link service abort */
16016 case FC_RCTL_BA_RMC: /* remove connection */
16017 case FC_RCTL_BA_ACC: /* basic accept */
16018 case FC_RCTL_BA_RJT: /* basic reject */
16019 case FC_RCTL_BA_PRMT:
16020 case FC_RCTL_ACK_1: /* acknowledge_1 */
16021 case FC_RCTL_ACK_0: /* acknowledge_0 */
16022 case FC_RCTL_P_RJT: /* port reject */
16023 case FC_RCTL_F_RJT: /* fabric reject */
16024 case FC_RCTL_P_BSY: /* port busy */
16025 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16026 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16027 case FC_RCTL_LCR: /* link credit reset */
16028 case FC_RCTL_END: /* end */
16029 break;
16030 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16031 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16032 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16033 return lpfc_fc_frame_check(phba, fc_hdr);
16034 default:
16035 goto drop;
16037 switch (fc_hdr->fh_type) {
16038 case FC_TYPE_BLS:
16039 case FC_TYPE_ELS:
16040 case FC_TYPE_FCP:
16041 case FC_TYPE_CT:
16042 case FC_TYPE_NVME:
16043 break;
16044 case FC_TYPE_IP:
16045 case FC_TYPE_ILS:
16046 default:
16047 goto drop;
16050 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16051 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16052 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16053 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16054 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
16055 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16056 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16057 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16058 be32_to_cpu(header[6]));
16059 return 0;
16060 drop:
16061 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16062 "2539 Dropped frame rctl:%s type:%s\n",
16063 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16064 lpfc_type_names[fc_hdr->fh_type]);
16065 return 1;
16069 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16070 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16072 * This function processes the FC header to retrieve the VFI from the VF
16073 * header, if one exists. This function will return the VFI if one exists
16074 * or 0 if no VSAN Header exists.
16076 static uint32_t
16077 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16079 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16081 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16082 return 0;
16083 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16087 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16088 * @phba: Pointer to the HBA structure to search for the vport on
16089 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16090 * @fcfi: The FC Fabric ID that the frame came from
16092 * This function searches the @phba for a vport that matches the content of the
16093 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16094 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16095 * returns the matching vport pointer or NULL if unable to match frame to a
16096 * vport.
16098 static struct lpfc_vport *
16099 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16100 uint16_t fcfi, uint32_t did)
16102 struct lpfc_vport **vports;
16103 struct lpfc_vport *vport = NULL;
16104 int i;
16106 if (did == Fabric_DID)
16107 return phba->pport;
16108 if ((phba->pport->fc_flag & FC_PT2PT) &&
16109 !(phba->link_state == LPFC_HBA_READY))
16110 return phba->pport;
16112 vports = lpfc_create_vport_work_array(phba);
16113 if (vports != NULL) {
16114 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16115 if (phba->fcf.fcfi == fcfi &&
16116 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16117 vports[i]->fc_myDID == did) {
16118 vport = vports[i];
16119 break;
16123 lpfc_destroy_vport_work_array(phba, vports);
16124 return vport;
16128 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16129 * @vport: The vport to work on.
16131 * This function updates the receive sequence time stamp for this vport. The
16132 * receive sequence time stamp indicates the time that the last frame of the
16133 * the sequence that has been idle for the longest amount of time was received.
16134 * the driver uses this time stamp to indicate if any received sequences have
16135 * timed out.
16137 static void
16138 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16140 struct lpfc_dmabuf *h_buf;
16141 struct hbq_dmabuf *dmabuf = NULL;
16143 /* get the oldest sequence on the rcv list */
16144 h_buf = list_get_first(&vport->rcv_buffer_list,
16145 struct lpfc_dmabuf, list);
16146 if (!h_buf)
16147 return;
16148 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16149 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16153 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16154 * @vport: The vport that the received sequences were sent to.
16156 * This function cleans up all outstanding received sequences. This is called
16157 * by the driver when a link event or user action invalidates all the received
16158 * sequences.
16160 void
16161 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16163 struct lpfc_dmabuf *h_buf, *hnext;
16164 struct lpfc_dmabuf *d_buf, *dnext;
16165 struct hbq_dmabuf *dmabuf = NULL;
16167 /* start with the oldest sequence on the rcv list */
16168 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16169 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16170 list_del_init(&dmabuf->hbuf.list);
16171 list_for_each_entry_safe(d_buf, dnext,
16172 &dmabuf->dbuf.list, list) {
16173 list_del_init(&d_buf->list);
16174 lpfc_in_buf_free(vport->phba, d_buf);
16176 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16181 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16182 * @vport: The vport that the received sequences were sent to.
16184 * This function determines whether any received sequences have timed out by
16185 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16186 * indicates that there is at least one timed out sequence this routine will
16187 * go through the received sequences one at a time from most inactive to most
16188 * active to determine which ones need to be cleaned up. Once it has determined
16189 * that a sequence needs to be cleaned up it will simply free up the resources
16190 * without sending an abort.
16192 void
16193 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16195 struct lpfc_dmabuf *h_buf, *hnext;
16196 struct lpfc_dmabuf *d_buf, *dnext;
16197 struct hbq_dmabuf *dmabuf = NULL;
16198 unsigned long timeout;
16199 int abort_count = 0;
16201 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16202 vport->rcv_buffer_time_stamp);
16203 if (list_empty(&vport->rcv_buffer_list) ||
16204 time_before(jiffies, timeout))
16205 return;
16206 /* start with the oldest sequence on the rcv list */
16207 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16208 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16209 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16210 dmabuf->time_stamp);
16211 if (time_before(jiffies, timeout))
16212 break;
16213 abort_count++;
16214 list_del_init(&dmabuf->hbuf.list);
16215 list_for_each_entry_safe(d_buf, dnext,
16216 &dmabuf->dbuf.list, list) {
16217 list_del_init(&d_buf->list);
16218 lpfc_in_buf_free(vport->phba, d_buf);
16220 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16222 if (abort_count)
16223 lpfc_update_rcv_time_stamp(vport);
16227 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16228 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16230 * This function searches through the existing incomplete sequences that have
16231 * been sent to this @vport. If the frame matches one of the incomplete
16232 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16233 * make up that sequence. If no sequence is found that matches this frame then
16234 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16235 * This function returns a pointer to the first dmabuf in the sequence list that
16236 * the frame was linked to.
16238 static struct hbq_dmabuf *
16239 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16241 struct fc_frame_header *new_hdr;
16242 struct fc_frame_header *temp_hdr;
16243 struct lpfc_dmabuf *d_buf;
16244 struct lpfc_dmabuf *h_buf;
16245 struct hbq_dmabuf *seq_dmabuf = NULL;
16246 struct hbq_dmabuf *temp_dmabuf = NULL;
16247 uint8_t found = 0;
16249 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16250 dmabuf->time_stamp = jiffies;
16251 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16253 /* Use the hdr_buf to find the sequence that this frame belongs to */
16254 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16255 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16256 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16257 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16258 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16259 continue;
16260 /* found a pending sequence that matches this frame */
16261 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16262 break;
16264 if (!seq_dmabuf) {
16266 * This indicates first frame received for this sequence.
16267 * Queue the buffer on the vport's rcv_buffer_list.
16269 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16270 lpfc_update_rcv_time_stamp(vport);
16271 return dmabuf;
16273 temp_hdr = seq_dmabuf->hbuf.virt;
16274 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16275 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16276 list_del_init(&seq_dmabuf->hbuf.list);
16277 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16278 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16279 lpfc_update_rcv_time_stamp(vport);
16280 return dmabuf;
16282 /* move this sequence to the tail to indicate a young sequence */
16283 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16284 seq_dmabuf->time_stamp = jiffies;
16285 lpfc_update_rcv_time_stamp(vport);
16286 if (list_empty(&seq_dmabuf->dbuf.list)) {
16287 temp_hdr = dmabuf->hbuf.virt;
16288 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16289 return seq_dmabuf;
16291 /* find the correct place in the sequence to insert this frame */
16292 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16293 while (!found) {
16294 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16295 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16297 * If the frame's sequence count is greater than the frame on
16298 * the list then insert the frame right after this frame
16300 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16301 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16302 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
16303 found = 1;
16304 break;
16307 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16308 break;
16309 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
16312 if (found)
16313 return seq_dmabuf;
16314 return NULL;
16318 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16319 * @vport: pointer to a vitural port
16320 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16322 * This function tries to abort from the partially assembed sequence, described
16323 * by the information from basic abbort @dmabuf. It checks to see whether such
16324 * partially assembled sequence held by the driver. If so, it shall free up all
16325 * the frames from the partially assembled sequence.
16327 * Return
16328 * true -- if there is matching partially assembled sequence present and all
16329 * the frames freed with the sequence;
16330 * false -- if there is no matching partially assembled sequence present so
16331 * nothing got aborted in the lower layer driver
16333 static bool
16334 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16335 struct hbq_dmabuf *dmabuf)
16337 struct fc_frame_header *new_hdr;
16338 struct fc_frame_header *temp_hdr;
16339 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16340 struct hbq_dmabuf *seq_dmabuf = NULL;
16342 /* Use the hdr_buf to find the sequence that matches this frame */
16343 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16344 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16345 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16346 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16347 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16348 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16349 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16350 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16351 continue;
16352 /* found a pending sequence that matches this frame */
16353 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16354 break;
16357 /* Free up all the frames from the partially assembled sequence */
16358 if (seq_dmabuf) {
16359 list_for_each_entry_safe(d_buf, n_buf,
16360 &seq_dmabuf->dbuf.list, list) {
16361 list_del_init(&d_buf->list);
16362 lpfc_in_buf_free(vport->phba, d_buf);
16364 return true;
16366 return false;
16370 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16371 * @vport: pointer to a vitural port
16372 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16374 * This function tries to abort from the assembed sequence from upper level
16375 * protocol, described by the information from basic abbort @dmabuf. It
16376 * checks to see whether such pending context exists at upper level protocol.
16377 * If so, it shall clean up the pending context.
16379 * Return
16380 * true -- if there is matching pending context of the sequence cleaned
16381 * at ulp;
16382 * false -- if there is no matching pending context of the sequence present
16383 * at ulp.
16385 static bool
16386 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16388 struct lpfc_hba *phba = vport->phba;
16389 int handled;
16391 /* Accepting abort at ulp with SLI4 only */
16392 if (phba->sli_rev < LPFC_SLI_REV4)
16393 return false;
16395 /* Register all caring upper level protocols to attend abort */
16396 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16397 if (handled)
16398 return true;
16400 return false;
16404 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
16405 * @phba: Pointer to HBA context object.
16406 * @cmd_iocbq: pointer to the command iocbq structure.
16407 * @rsp_iocbq: pointer to the response iocbq structure.
16409 * This function handles the sequence abort response iocb command complete
16410 * event. It properly releases the memory allocated to the sequence abort
16411 * accept iocb.
16413 static void
16414 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
16415 struct lpfc_iocbq *cmd_iocbq,
16416 struct lpfc_iocbq *rsp_iocbq)
16418 struct lpfc_nodelist *ndlp;
16420 if (cmd_iocbq) {
16421 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16422 lpfc_nlp_put(ndlp);
16423 lpfc_nlp_not_used(ndlp);
16424 lpfc_sli_release_iocbq(phba, cmd_iocbq);
16427 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16428 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16430 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16431 rsp_iocbq->iocb.ulpStatus,
16432 rsp_iocbq->iocb.un.ulpWord[4]);
16436 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16437 * @phba: Pointer to HBA context object.
16438 * @xri: xri id in transaction.
16440 * This function validates the xri maps to the known range of XRIs allocated an
16441 * used by the driver.
16443 uint16_t
16444 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16445 uint16_t xri)
16447 uint16_t i;
16449 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16450 if (xri == phba->sli4_hba.xri_ids[i])
16451 return i;
16453 return NO_XRI;
16457 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
16458 * @phba: Pointer to HBA context object.
16459 * @fc_hdr: pointer to a FC frame header.
16461 * This function sends a basic response to a previous unsol sequence abort
16462 * event after aborting the sequence handling.
16464 static void
16465 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16466 struct fc_frame_header *fc_hdr, bool aborted)
16468 struct lpfc_hba *phba = vport->phba;
16469 struct lpfc_iocbq *ctiocb = NULL;
16470 struct lpfc_nodelist *ndlp;
16471 uint16_t oxid, rxid, xri, lxri;
16472 uint32_t sid, fctl;
16473 IOCB_t *icmd;
16474 int rc;
16476 if (!lpfc_is_link_up(phba))
16477 return;
16479 sid = sli4_sid_from_fc_hdr(fc_hdr);
16480 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
16481 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
16483 ndlp = lpfc_findnode_did(vport, sid);
16484 if (!ndlp) {
16485 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
16486 if (!ndlp) {
16487 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16488 "1268 Failed to allocate ndlp for "
16489 "oxid:x%x SID:x%x\n", oxid, sid);
16490 return;
16492 lpfc_nlp_init(vport, ndlp, sid);
16493 /* Put ndlp onto pport node list */
16494 lpfc_enqueue_node(vport, ndlp);
16495 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16496 /* re-setup ndlp without removing from node list */
16497 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16498 if (!ndlp) {
16499 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16500 "3275 Failed to active ndlp found "
16501 "for oxid:x%x SID:x%x\n", oxid, sid);
16502 return;
16506 /* Allocate buffer for rsp iocb */
16507 ctiocb = lpfc_sli_get_iocbq(phba);
16508 if (!ctiocb)
16509 return;
16511 /* Extract the F_CTL field from FC_HDR */
16512 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16514 icmd = &ctiocb->iocb;
16515 icmd->un.xseq64.bdl.bdeSize = 0;
16516 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
16517 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16518 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16519 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16521 /* Fill in the rest of iocb fields */
16522 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16523 icmd->ulpBdeCount = 0;
16524 icmd->ulpLe = 1;
16525 icmd->ulpClass = CLASS3;
16526 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
16527 ctiocb->context1 = lpfc_nlp_get(ndlp);
16529 ctiocb->iocb_cmpl = NULL;
16530 ctiocb->vport = phba->pport;
16531 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
16532 ctiocb->sli4_lxritag = NO_XRI;
16533 ctiocb->sli4_xritag = NO_XRI;
16535 if (fctl & FC_FC_EX_CTX)
16536 /* Exchange responder sent the abort so we
16537 * own the oxid.
16539 xri = oxid;
16540 else
16541 xri = rxid;
16542 lxri = lpfc_sli4_xri_inrange(phba, xri);
16543 if (lxri != NO_XRI)
16544 lpfc_set_rrq_active(phba, ndlp, lxri,
16545 (xri == oxid) ? rxid : oxid, 0);
16546 /* For BA_ABTS from exchange responder, if the logical xri with
16547 * the oxid maps to the FCP XRI range, the port no longer has
16548 * that exchange context, send a BLS_RJT. Override the IOCB for
16549 * a BA_RJT.
16551 if ((fctl & FC_FC_EX_CTX) &&
16552 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
16553 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16554 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16555 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16556 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16559 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16560 * the driver no longer has that exchange, send a BLS_RJT. Override
16561 * the IOCB for a BA_RJT.
16563 if (aborted == false) {
16564 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16565 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16566 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16567 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16570 if (fctl & FC_FC_EX_CTX) {
16571 /* ABTS sent by responder to CT exchange, construction
16572 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16573 * field and RX_ID from ABTS for RX_ID field.
16575 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
16576 } else {
16577 /* ABTS sent by initiator to CT exchange, construction
16578 * of BA_ACC will need to allocate a new XRI as for the
16579 * XRI_TAG field.
16581 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
16583 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
16584 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
16586 /* Xmit CT abts response on exchange <xid> */
16587 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16588 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16589 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
16591 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16592 if (rc == IOCB_ERROR) {
16593 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16594 "2925 Failed to issue CT ABTS RSP x%x on "
16595 "xri x%x, Data x%x\n",
16596 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16597 phba->link_state);
16598 lpfc_nlp_put(ndlp);
16599 ctiocb->context1 = NULL;
16600 lpfc_sli_release_iocbq(phba, ctiocb);
16605 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16606 * @vport: Pointer to the vport on which this sequence was received
16607 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16609 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16610 * receive sequence is only partially assembed by the driver, it shall abort
16611 * the partially assembled frames for the sequence. Otherwise, if the
16612 * unsolicited receive sequence has been completely assembled and passed to
16613 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16614 * unsolicited sequence has been aborted. After that, it will issue a basic
16615 * accept to accept the abort.
16617 static void
16618 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16619 struct hbq_dmabuf *dmabuf)
16621 struct lpfc_hba *phba = vport->phba;
16622 struct fc_frame_header fc_hdr;
16623 uint32_t fctl;
16624 bool aborted;
16626 /* Make a copy of fc_hdr before the dmabuf being released */
16627 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
16628 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
16630 if (fctl & FC_FC_EX_CTX) {
16631 /* ABTS by responder to exchange, no cleanup needed */
16632 aborted = true;
16633 } else {
16634 /* ABTS by initiator to exchange, need to do cleanup */
16635 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16636 if (aborted == false)
16637 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
16639 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16641 /* Respond with BA_ACC or BA_RJT accordingly */
16642 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
16646 * lpfc_seq_complete - Indicates if a sequence is complete
16647 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16649 * This function checks the sequence, starting with the frame described by
16650 * @dmabuf, to see if all the frames associated with this sequence are present.
16651 * the frames associated with this sequence are linked to the @dmabuf using the
16652 * dbuf list. This function looks for two major things. 1) That the first frame
16653 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16654 * set. 3) That there are no holes in the sequence count. The function will
16655 * return 1 when the sequence is complete, otherwise it will return 0.
16657 static int
16658 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16660 struct fc_frame_header *hdr;
16661 struct lpfc_dmabuf *d_buf;
16662 struct hbq_dmabuf *seq_dmabuf;
16663 uint32_t fctl;
16664 int seq_count = 0;
16666 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16667 /* make sure first fame of sequence has a sequence count of zero */
16668 if (hdr->fh_seq_cnt != seq_count)
16669 return 0;
16670 fctl = (hdr->fh_f_ctl[0] << 16 |
16671 hdr->fh_f_ctl[1] << 8 |
16672 hdr->fh_f_ctl[2]);
16673 /* If last frame of sequence we can return success. */
16674 if (fctl & FC_FC_END_SEQ)
16675 return 1;
16676 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16677 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16678 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16679 /* If there is a hole in the sequence count then fail. */
16680 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
16681 return 0;
16682 fctl = (hdr->fh_f_ctl[0] << 16 |
16683 hdr->fh_f_ctl[1] << 8 |
16684 hdr->fh_f_ctl[2]);
16685 /* If last frame of sequence we can return success. */
16686 if (fctl & FC_FC_END_SEQ)
16687 return 1;
16689 return 0;
16693 * lpfc_prep_seq - Prep sequence for ULP processing
16694 * @vport: Pointer to the vport on which this sequence was received
16695 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16697 * This function takes a sequence, described by a list of frames, and creates
16698 * a list of iocbq structures to describe the sequence. This iocbq list will be
16699 * used to issue to the generic unsolicited sequence handler. This routine
16700 * returns a pointer to the first iocbq in the list. If the function is unable
16701 * to allocate an iocbq then it throw out the received frames that were not
16702 * able to be described and return a pointer to the first iocbq. If unable to
16703 * allocate any iocbqs (including the first) this function will return NULL.
16705 static struct lpfc_iocbq *
16706 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16708 struct hbq_dmabuf *hbq_buf;
16709 struct lpfc_dmabuf *d_buf, *n_buf;
16710 struct lpfc_iocbq *first_iocbq, *iocbq;
16711 struct fc_frame_header *fc_hdr;
16712 uint32_t sid;
16713 uint32_t len, tot_len;
16714 struct ulp_bde64 *pbde;
16716 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16717 /* remove from receive buffer list */
16718 list_del_init(&seq_dmabuf->hbuf.list);
16719 lpfc_update_rcv_time_stamp(vport);
16720 /* get the Remote Port's SID */
16721 sid = sli4_sid_from_fc_hdr(fc_hdr);
16722 tot_len = 0;
16723 /* Get an iocbq struct to fill in. */
16724 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16725 if (first_iocbq) {
16726 /* Initialize the first IOCB. */
16727 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
16728 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
16729 first_iocbq->vport = vport;
16731 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16732 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16733 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16734 first_iocbq->iocb.un.rcvels.parmRo =
16735 sli4_did_from_fc_hdr(fc_hdr);
16736 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16737 } else
16738 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
16739 first_iocbq->iocb.ulpContext = NO_XRI;
16740 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16741 be16_to_cpu(fc_hdr->fh_ox_id);
16742 /* iocbq is prepped for internal consumption. Physical vpi. */
16743 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16744 vport->phba->vpi_ids[vport->vpi];
16745 /* put the first buffer into the first IOCBq */
16746 tot_len = bf_get(lpfc_rcqe_length,
16747 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16749 first_iocbq->context2 = &seq_dmabuf->dbuf;
16750 first_iocbq->context3 = NULL;
16751 first_iocbq->iocb.ulpBdeCount = 1;
16752 if (tot_len > LPFC_DATA_BUF_SIZE)
16753 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16754 LPFC_DATA_BUF_SIZE;
16755 else
16756 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16758 first_iocbq->iocb.un.rcvels.remoteID = sid;
16760 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16762 iocbq = first_iocbq;
16764 * Each IOCBq can have two Buffers assigned, so go through the list
16765 * of buffers for this sequence and save two buffers in each IOCBq
16767 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16768 if (!iocbq) {
16769 lpfc_in_buf_free(vport->phba, d_buf);
16770 continue;
16772 if (!iocbq->context3) {
16773 iocbq->context3 = d_buf;
16774 iocbq->iocb.ulpBdeCount++;
16775 /* We need to get the size out of the right CQE */
16776 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16777 len = bf_get(lpfc_rcqe_length,
16778 &hbq_buf->cq_event.cqe.rcqe_cmpl);
16779 pbde = (struct ulp_bde64 *)
16780 &iocbq->iocb.unsli3.sli3Words[4];
16781 if (len > LPFC_DATA_BUF_SIZE)
16782 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16783 else
16784 pbde->tus.f.bdeSize = len;
16786 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16787 tot_len += len;
16788 } else {
16789 iocbq = lpfc_sli_get_iocbq(vport->phba);
16790 if (!iocbq) {
16791 if (first_iocbq) {
16792 first_iocbq->iocb.ulpStatus =
16793 IOSTAT_FCP_RSP_ERROR;
16794 first_iocbq->iocb.un.ulpWord[4] =
16795 IOERR_NO_RESOURCES;
16797 lpfc_in_buf_free(vport->phba, d_buf);
16798 continue;
16800 /* We need to get the size out of the right CQE */
16801 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16802 len = bf_get(lpfc_rcqe_length,
16803 &hbq_buf->cq_event.cqe.rcqe_cmpl);
16804 iocbq->context2 = d_buf;
16805 iocbq->context3 = NULL;
16806 iocbq->iocb.ulpBdeCount = 1;
16807 if (len > LPFC_DATA_BUF_SIZE)
16808 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16809 LPFC_DATA_BUF_SIZE;
16810 else
16811 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16813 tot_len += len;
16814 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16816 iocbq->iocb.un.rcvels.remoteID = sid;
16817 list_add_tail(&iocbq->list, &first_iocbq->list);
16820 return first_iocbq;
16823 static void
16824 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16825 struct hbq_dmabuf *seq_dmabuf)
16827 struct fc_frame_header *fc_hdr;
16828 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16829 struct lpfc_hba *phba = vport->phba;
16831 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16832 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16833 if (!iocbq) {
16834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16835 "2707 Ring %d handler: Failed to allocate "
16836 "iocb Rctl x%x Type x%x received\n",
16837 LPFC_ELS_RING,
16838 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16839 return;
16841 if (!lpfc_complete_unsol_iocb(phba,
16842 phba->sli4_hba.els_wq->pring,
16843 iocbq, fc_hdr->fh_r_ctl,
16844 fc_hdr->fh_type))
16845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16846 "2540 Ring %d handler: unexpected Rctl "
16847 "x%x Type x%x received\n",
16848 LPFC_ELS_RING,
16849 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16851 /* Free iocb created in lpfc_prep_seq */
16852 list_for_each_entry_safe(curr_iocb, next_iocb,
16853 &iocbq->list, list) {
16854 list_del_init(&curr_iocb->list);
16855 lpfc_sli_release_iocbq(phba, curr_iocb);
16857 lpfc_sli_release_iocbq(phba, iocbq);
16861 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16862 * @phba: Pointer to HBA context object.
16864 * This function is called with no lock held. This function processes all
16865 * the received buffers and gives it to upper layers when a received buffer
16866 * indicates that it is the final frame in the sequence. The interrupt
16867 * service routine processes received buffers at interrupt contexts.
16868 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16869 * appropriate receive function when the final frame in a sequence is received.
16871 void
16872 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16873 struct hbq_dmabuf *dmabuf)
16875 struct hbq_dmabuf *seq_dmabuf;
16876 struct fc_frame_header *fc_hdr;
16877 struct lpfc_vport *vport;
16878 uint32_t fcfi;
16879 uint32_t did;
16881 /* Process each received buffer */
16882 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16884 /* check to see if this a valid type of frame */
16885 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16886 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16887 return;
16890 if ((bf_get(lpfc_cqe_code,
16891 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16892 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16893 &dmabuf->cq_event.cqe.rcqe_cmpl);
16894 else
16895 fcfi = bf_get(lpfc_rcqe_fcf_id,
16896 &dmabuf->cq_event.cqe.rcqe_cmpl);
16898 /* d_id this frame is directed to */
16899 did = sli4_did_from_fc_hdr(fc_hdr);
16901 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
16902 if (!vport) {
16903 /* throw out the frame */
16904 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16905 return;
16908 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16909 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16910 (did != Fabric_DID)) {
16912 * Throw out the frame if we are not pt2pt.
16913 * The pt2pt protocol allows for discovery frames
16914 * to be received without a registered VPI.
16916 if (!(vport->fc_flag & FC_PT2PT) ||
16917 (phba->link_state == LPFC_HBA_READY)) {
16918 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16919 return;
16923 /* Handle the basic abort sequence (BA_ABTS) event */
16924 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16925 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16926 return;
16929 /* Link this frame */
16930 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16931 if (!seq_dmabuf) {
16932 /* unable to add frame to vport - throw it out */
16933 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16934 return;
16936 /* If not last frame in sequence continue processing frames. */
16937 if (!lpfc_seq_complete(seq_dmabuf))
16938 return;
16940 /* Send the complete sequence to the upper layer protocol */
16941 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
16945 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16946 * @phba: pointer to lpfc hba data structure.
16948 * This routine is invoked to post rpi header templates to the
16949 * HBA consistent with the SLI-4 interface spec. This routine
16950 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16951 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
16953 * This routine does not require any locks. It's usage is expected
16954 * to be driver load or reset recovery when the driver is
16955 * sequential.
16957 * Return codes
16958 * 0 - successful
16959 * -EIO - The mailbox failed to complete successfully.
16960 * When this error occurs, the driver is not guaranteed
16961 * to have any rpi regions posted to the device and
16962 * must either attempt to repost the regions or take a
16963 * fatal error.
16966 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
16968 struct lpfc_rpi_hdr *rpi_page;
16969 uint32_t rc = 0;
16970 uint16_t lrpi = 0;
16972 /* SLI4 ports that support extents do not require RPI headers. */
16973 if (!phba->sli4_hba.rpi_hdrs_in_use)
16974 goto exit;
16975 if (phba->sli4_hba.extents_in_use)
16976 return -EIO;
16978 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
16980 * Assign the rpi headers a physical rpi only if the driver
16981 * has not initialized those resources. A port reset only
16982 * needs the headers posted.
16984 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
16985 LPFC_RPI_RSRC_RDY)
16986 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
16988 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
16989 if (rc != MBX_SUCCESS) {
16990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16991 "2008 Error %d posting all rpi "
16992 "headers\n", rc);
16993 rc = -EIO;
16994 break;
16998 exit:
16999 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17000 LPFC_RPI_RSRC_RDY);
17001 return rc;
17005 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17006 * @phba: pointer to lpfc hba data structure.
17007 * @rpi_page: pointer to the rpi memory region.
17009 * This routine is invoked to post a single rpi header to the
17010 * HBA consistent with the SLI-4 interface spec. This memory region
17011 * maps up to 64 rpi context regions.
17013 * Return codes
17014 * 0 - successful
17015 * -ENOMEM - No available memory
17016 * -EIO - The mailbox failed to complete successfully.
17019 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17021 LPFC_MBOXQ_t *mboxq;
17022 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17023 uint32_t rc = 0;
17024 uint32_t shdr_status, shdr_add_status;
17025 union lpfc_sli4_cfg_shdr *shdr;
17027 /* SLI4 ports that support extents do not require RPI headers. */
17028 if (!phba->sli4_hba.rpi_hdrs_in_use)
17029 return rc;
17030 if (phba->sli4_hba.extents_in_use)
17031 return -EIO;
17033 /* The port is notified of the header region via a mailbox command. */
17034 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17035 if (!mboxq) {
17036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17037 "2001 Unable to allocate memory for issuing "
17038 "SLI_CONFIG_SPECIAL mailbox command\n");
17039 return -ENOMEM;
17042 /* Post all rpi memory regions to the port. */
17043 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
17044 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17045 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17046 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
17047 sizeof(struct lpfc_sli4_cfg_mhdr),
17048 LPFC_SLI4_MBX_EMBED);
17051 /* Post the physical rpi to the port for this rpi header. */
17052 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17053 rpi_page->start_rpi);
17054 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17055 hdr_tmpl, rpi_page->page_count);
17057 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17058 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
17059 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17060 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17061 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17062 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17063 if (rc != MBX_TIMEOUT)
17064 mempool_free(mboxq, phba->mbox_mem_pool);
17065 if (shdr_status || shdr_add_status || rc) {
17066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17067 "2514 POST_RPI_HDR mailbox failed with "
17068 "status x%x add_status x%x, mbx status x%x\n",
17069 shdr_status, shdr_add_status, rc);
17070 rc = -ENXIO;
17072 return rc;
17076 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17077 * @phba: pointer to lpfc hba data structure.
17079 * This routine is invoked to post rpi header templates to the
17080 * HBA consistent with the SLI-4 interface spec. This routine
17081 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17082 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17084 * Returns
17085 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17086 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17089 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17091 unsigned long rpi;
17092 uint16_t max_rpi, rpi_limit;
17093 uint16_t rpi_remaining, lrpi = 0;
17094 struct lpfc_rpi_hdr *rpi_hdr;
17095 unsigned long iflag;
17098 * Fetch the next logical rpi. Because this index is logical,
17099 * the driver starts at 0 each time.
17101 spin_lock_irqsave(&phba->hbalock, iflag);
17102 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17103 rpi_limit = phba->sli4_hba.next_rpi;
17105 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17106 if (rpi >= rpi_limit)
17107 rpi = LPFC_RPI_ALLOC_ERROR;
17108 else {
17109 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17110 phba->sli4_hba.max_cfg_param.rpi_used++;
17111 phba->sli4_hba.rpi_count++;
17113 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17114 "0001 rpi:%x max:%x lim:%x\n",
17115 (int) rpi, max_rpi, rpi_limit);
17118 * Don't try to allocate more rpi header regions if the device limit
17119 * has been exhausted.
17121 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17122 (phba->sli4_hba.rpi_count >= max_rpi)) {
17123 spin_unlock_irqrestore(&phba->hbalock, iflag);
17124 return rpi;
17128 * RPI header postings are not required for SLI4 ports capable of
17129 * extents.
17131 if (!phba->sli4_hba.rpi_hdrs_in_use) {
17132 spin_unlock_irqrestore(&phba->hbalock, iflag);
17133 return rpi;
17137 * If the driver is running low on rpi resources, allocate another
17138 * page now. Note that the next_rpi value is used because
17139 * it represents how many are actually in use whereas max_rpi notes
17140 * how many are supported max by the device.
17142 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
17143 spin_unlock_irqrestore(&phba->hbalock, iflag);
17144 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17145 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17146 if (!rpi_hdr) {
17147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17148 "2002 Error Could not grow rpi "
17149 "count\n");
17150 } else {
17151 lrpi = rpi_hdr->start_rpi;
17152 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17153 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17157 return rpi;
17161 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17162 * @phba: pointer to lpfc hba data structure.
17164 * This routine is invoked to release an rpi to the pool of
17165 * available rpis maintained by the driver.
17167 static void
17168 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17170 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17171 phba->sli4_hba.rpi_count--;
17172 phba->sli4_hba.max_cfg_param.rpi_used--;
17177 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17178 * @phba: pointer to lpfc hba data structure.
17180 * This routine is invoked to release an rpi to the pool of
17181 * available rpis maintained by the driver.
17183 void
17184 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17186 spin_lock_irq(&phba->hbalock);
17187 __lpfc_sli4_free_rpi(phba, rpi);
17188 spin_unlock_irq(&phba->hbalock);
17192 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17193 * @phba: pointer to lpfc hba data structure.
17195 * This routine is invoked to remove the memory region that
17196 * provided rpi via a bitmask.
17198 void
17199 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17201 kfree(phba->sli4_hba.rpi_bmask);
17202 kfree(phba->sli4_hba.rpi_ids);
17203 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
17207 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17208 * @phba: pointer to lpfc hba data structure.
17210 * This routine is invoked to remove the memory region that
17211 * provided rpi via a bitmask.
17214 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17215 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
17217 LPFC_MBOXQ_t *mboxq;
17218 struct lpfc_hba *phba = ndlp->phba;
17219 int rc;
17221 /* The port is notified of the header region via a mailbox command. */
17222 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17223 if (!mboxq)
17224 return -ENOMEM;
17226 /* Post all rpi memory regions to the port. */
17227 lpfc_resume_rpi(mboxq, ndlp);
17228 if (cmpl) {
17229 mboxq->mbox_cmpl = cmpl;
17230 mboxq->context1 = arg;
17231 mboxq->context2 = ndlp;
17232 } else
17233 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17234 mboxq->vport = ndlp->vport;
17235 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17236 if (rc == MBX_NOT_FINISHED) {
17237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17238 "2010 Resume RPI Mailbox failed "
17239 "status %d, mbxStatus x%x\n", rc,
17240 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17241 mempool_free(mboxq, phba->mbox_mem_pool);
17242 return -EIO;
17244 return 0;
17248 * lpfc_sli4_init_vpi - Initialize a vpi with the port
17249 * @vport: Pointer to the vport for which the vpi is being initialized
17251 * This routine is invoked to activate a vpi with the port.
17253 * Returns:
17254 * 0 success
17255 * -Evalue otherwise
17258 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
17260 LPFC_MBOXQ_t *mboxq;
17261 int rc = 0;
17262 int retval = MBX_SUCCESS;
17263 uint32_t mbox_tmo;
17264 struct lpfc_hba *phba = vport->phba;
17265 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17266 if (!mboxq)
17267 return -ENOMEM;
17268 lpfc_init_vpi(phba, mboxq, vport->vpi);
17269 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
17270 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
17271 if (rc != MBX_SUCCESS) {
17272 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
17273 "2022 INIT VPI Mailbox failed "
17274 "status %d, mbxStatus x%x\n", rc,
17275 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17276 retval = -EIO;
17278 if (rc != MBX_TIMEOUT)
17279 mempool_free(mboxq, vport->phba->mbox_mem_pool);
17281 return retval;
17285 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17286 * @phba: pointer to lpfc hba data structure.
17287 * @mboxq: Pointer to mailbox object.
17289 * This routine is invoked to manually add a single FCF record. The caller
17290 * must pass a completely initialized FCF_Record. This routine takes
17291 * care of the nonembedded mailbox operations.
17293 static void
17294 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17296 void *virt_addr;
17297 union lpfc_sli4_cfg_shdr *shdr;
17298 uint32_t shdr_status, shdr_add_status;
17300 virt_addr = mboxq->sge_array->addr[0];
17301 /* The IOCTL status is embedded in the mailbox subheader. */
17302 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17303 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17304 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17306 if ((shdr_status || shdr_add_status) &&
17307 (shdr_status != STATUS_FCF_IN_USE))
17308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17309 "2558 ADD_FCF_RECORD mailbox failed with "
17310 "status x%x add_status x%x\n",
17311 shdr_status, shdr_add_status);
17313 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17317 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17318 * @phba: pointer to lpfc hba data structure.
17319 * @fcf_record: pointer to the initialized fcf record to add.
17321 * This routine is invoked to manually add a single FCF record. The caller
17322 * must pass a completely initialized FCF_Record. This routine takes
17323 * care of the nonembedded mailbox operations.
17326 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17328 int rc = 0;
17329 LPFC_MBOXQ_t *mboxq;
17330 uint8_t *bytep;
17331 void *virt_addr;
17332 struct lpfc_mbx_sge sge;
17333 uint32_t alloc_len, req_len;
17334 uint32_t fcfindex;
17336 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17337 if (!mboxq) {
17338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17339 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17340 return -ENOMEM;
17343 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17344 sizeof(uint32_t);
17346 /* Allocate DMA memory and set up the non-embedded mailbox command */
17347 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17348 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17349 req_len, LPFC_SLI4_MBX_NEMBED);
17350 if (alloc_len < req_len) {
17351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17352 "2523 Allocated DMA memory size (x%x) is "
17353 "less than the requested DMA memory "
17354 "size (x%x)\n", alloc_len, req_len);
17355 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17356 return -ENOMEM;
17360 * Get the first SGE entry from the non-embedded DMA memory. This
17361 * routine only uses a single SGE.
17363 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
17364 virt_addr = mboxq->sge_array->addr[0];
17366 * Configure the FCF record for FCFI 0. This is the driver's
17367 * hardcoded default and gets used in nonFIP mode.
17369 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17370 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17371 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17374 * Copy the fcf_index and the FCF Record Data. The data starts after
17375 * the FCoE header plus word10. The data copy needs to be endian
17376 * correct.
17378 bytep += sizeof(uint32_t);
17379 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17380 mboxq->vport = phba->pport;
17381 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17382 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17383 if (rc == MBX_NOT_FINISHED) {
17384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17385 "2515 ADD_FCF_RECORD mailbox failed with "
17386 "status 0x%x\n", rc);
17387 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17388 rc = -EIO;
17389 } else
17390 rc = 0;
17392 return rc;
17396 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17397 * @phba: pointer to lpfc hba data structure.
17398 * @fcf_record: pointer to the fcf record to write the default data.
17399 * @fcf_index: FCF table entry index.
17401 * This routine is invoked to build the driver's default FCF record. The
17402 * values used are hardcoded. This routine handles memory initialization.
17405 void
17406 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17407 struct fcf_record *fcf_record,
17408 uint16_t fcf_index)
17410 memset(fcf_record, 0, sizeof(struct fcf_record));
17411 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17412 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17413 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17414 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17415 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17416 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17417 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17418 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17419 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17420 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17421 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17422 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17423 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
17424 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
17425 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17426 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17427 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17428 /* Set the VLAN bit map */
17429 if (phba->valid_vlan) {
17430 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17431 = 1 << (phba->vlan_id % 8);
17436 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
17437 * @phba: pointer to lpfc hba data structure.
17438 * @fcf_index: FCF table entry offset.
17440 * This routine is invoked to scan the entire FCF table by reading FCF
17441 * record and processing it one at a time starting from the @fcf_index
17442 * for initial FCF discovery or fast FCF failover rediscovery.
17444 * Return 0 if the mailbox command is submitted successfully, none 0
17445 * otherwise.
17448 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17450 int rc = 0, error;
17451 LPFC_MBOXQ_t *mboxq;
17453 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
17454 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
17455 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17456 if (!mboxq) {
17457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17458 "2000 Failed to allocate mbox for "
17459 "READ_FCF cmd\n");
17460 error = -ENOMEM;
17461 goto fail_fcf_scan;
17463 /* Construct the read FCF record mailbox command */
17464 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17465 if (rc) {
17466 error = -EINVAL;
17467 goto fail_fcf_scan;
17469 /* Issue the mailbox command asynchronously */
17470 mboxq->vport = phba->pport;
17471 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
17473 spin_lock_irq(&phba->hbalock);
17474 phba->hba_flag |= FCF_TS_INPROG;
17475 spin_unlock_irq(&phba->hbalock);
17477 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17478 if (rc == MBX_NOT_FINISHED)
17479 error = -EIO;
17480 else {
17481 /* Reset eligible FCF count for new scan */
17482 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
17483 phba->fcf.eligible_fcf_cnt = 0;
17484 error = 0;
17486 fail_fcf_scan:
17487 if (error) {
17488 if (mboxq)
17489 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17490 /* FCF scan failed, clear FCF_TS_INPROG flag */
17491 spin_lock_irq(&phba->hbalock);
17492 phba->hba_flag &= ~FCF_TS_INPROG;
17493 spin_unlock_irq(&phba->hbalock);
17495 return error;
17499 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
17500 * @phba: pointer to lpfc hba data structure.
17501 * @fcf_index: FCF table entry offset.
17503 * This routine is invoked to read an FCF record indicated by @fcf_index
17504 * and to use it for FLOGI roundrobin FCF failover.
17506 * Return 0 if the mailbox command is submitted successfully, none 0
17507 * otherwise.
17510 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17512 int rc = 0, error;
17513 LPFC_MBOXQ_t *mboxq;
17515 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17516 if (!mboxq) {
17517 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17518 "2763 Failed to allocate mbox for "
17519 "READ_FCF cmd\n");
17520 error = -ENOMEM;
17521 goto fail_fcf_read;
17523 /* Construct the read FCF record mailbox command */
17524 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17525 if (rc) {
17526 error = -EINVAL;
17527 goto fail_fcf_read;
17529 /* Issue the mailbox command asynchronously */
17530 mboxq->vport = phba->pport;
17531 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17532 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17533 if (rc == MBX_NOT_FINISHED)
17534 error = -EIO;
17535 else
17536 error = 0;
17538 fail_fcf_read:
17539 if (error && mboxq)
17540 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17541 return error;
17545 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17546 * @phba: pointer to lpfc hba data structure.
17547 * @fcf_index: FCF table entry offset.
17549 * This routine is invoked to read an FCF record indicated by @fcf_index to
17550 * determine whether it's eligible for FLOGI roundrobin failover list.
17552 * Return 0 if the mailbox command is submitted successfully, none 0
17553 * otherwise.
17556 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17558 int rc = 0, error;
17559 LPFC_MBOXQ_t *mboxq;
17561 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17562 if (!mboxq) {
17563 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17564 "2758 Failed to allocate mbox for "
17565 "READ_FCF cmd\n");
17566 error = -ENOMEM;
17567 goto fail_fcf_read;
17569 /* Construct the read FCF record mailbox command */
17570 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17571 if (rc) {
17572 error = -EINVAL;
17573 goto fail_fcf_read;
17575 /* Issue the mailbox command asynchronously */
17576 mboxq->vport = phba->pport;
17577 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17578 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17579 if (rc == MBX_NOT_FINISHED)
17580 error = -EIO;
17581 else
17582 error = 0;
17584 fail_fcf_read:
17585 if (error && mboxq)
17586 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17587 return error;
17591 * lpfc_check_next_fcf_pri_level
17592 * phba pointer to the lpfc_hba struct for this port.
17593 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17594 * routine when the rr_bmask is empty. The FCF indecies are put into the
17595 * rr_bmask based on their priority level. Starting from the highest priority
17596 * to the lowest. The most likely FCF candidate will be in the highest
17597 * priority group. When this routine is called it searches the fcf_pri list for
17598 * next lowest priority group and repopulates the rr_bmask with only those
17599 * fcf_indexes.
17600 * returns:
17601 * 1=success 0=failure
17603 static int
17604 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17606 uint16_t next_fcf_pri;
17607 uint16_t last_index;
17608 struct lpfc_fcf_pri *fcf_pri;
17609 int rc;
17610 int ret = 0;
17612 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17613 LPFC_SLI4_FCF_TBL_INDX_MAX);
17614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17615 "3060 Last IDX %d\n", last_index);
17617 /* Verify the priority list has 2 or more entries */
17618 spin_lock_irq(&phba->hbalock);
17619 if (list_empty(&phba->fcf.fcf_pri_list) ||
17620 list_is_singular(&phba->fcf.fcf_pri_list)) {
17621 spin_unlock_irq(&phba->hbalock);
17622 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17623 "3061 Last IDX %d\n", last_index);
17624 return 0; /* Empty rr list */
17626 spin_unlock_irq(&phba->hbalock);
17628 next_fcf_pri = 0;
17630 * Clear the rr_bmask and set all of the bits that are at this
17631 * priority.
17633 memset(phba->fcf.fcf_rr_bmask, 0,
17634 sizeof(*phba->fcf.fcf_rr_bmask));
17635 spin_lock_irq(&phba->hbalock);
17636 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17637 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17638 continue;
17640 * the 1st priority that has not FLOGI failed
17641 * will be the highest.
17643 if (!next_fcf_pri)
17644 next_fcf_pri = fcf_pri->fcf_rec.priority;
17645 spin_unlock_irq(&phba->hbalock);
17646 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17647 rc = lpfc_sli4_fcf_rr_index_set(phba,
17648 fcf_pri->fcf_rec.fcf_index);
17649 if (rc)
17650 return 0;
17652 spin_lock_irq(&phba->hbalock);
17655 * if next_fcf_pri was not set above and the list is not empty then
17656 * we have failed flogis on all of them. So reset flogi failed
17657 * and start at the beginning.
17659 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17660 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17661 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17663 * the 1st priority that has not FLOGI failed
17664 * will be the highest.
17666 if (!next_fcf_pri)
17667 next_fcf_pri = fcf_pri->fcf_rec.priority;
17668 spin_unlock_irq(&phba->hbalock);
17669 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17670 rc = lpfc_sli4_fcf_rr_index_set(phba,
17671 fcf_pri->fcf_rec.fcf_index);
17672 if (rc)
17673 return 0;
17675 spin_lock_irq(&phba->hbalock);
17677 } else
17678 ret = 1;
17679 spin_unlock_irq(&phba->hbalock);
17681 return ret;
17684 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17685 * @phba: pointer to lpfc hba data structure.
17687 * This routine is to get the next eligible FCF record index in a round
17688 * robin fashion. If the next eligible FCF record index equals to the
17689 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
17690 * shall be returned, otherwise, the next eligible FCF record's index
17691 * shall be returned.
17693 uint16_t
17694 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17696 uint16_t next_fcf_index;
17698 initial_priority:
17699 /* Search start from next bit of currently registered FCF index */
17700 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17702 next_priority:
17703 /* Determine the next fcf index to check */
17704 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
17705 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17706 LPFC_SLI4_FCF_TBL_INDX_MAX,
17707 next_fcf_index);
17709 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
17710 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17712 * If we have wrapped then we need to clear the bits that
17713 * have been tested so that we can detect when we should
17714 * change the priority level.
17716 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17717 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
17721 /* Check roundrobin failover list empty condition */
17722 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17723 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17725 * If next fcf index is not found check if there are lower
17726 * Priority level fcf's in the fcf_priority list.
17727 * Set up the rr_bmask with all of the avaiable fcf bits
17728 * at that level and continue the selection process.
17730 if (lpfc_check_next_fcf_pri_level(phba))
17731 goto initial_priority;
17732 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17733 "2844 No roundrobin failover FCF available\n");
17734 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17735 return LPFC_FCOE_FCF_NEXT_NONE;
17736 else {
17737 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17738 "3063 Only FCF available idx %d, flag %x\n",
17739 next_fcf_index,
17740 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17741 return next_fcf_index;
17745 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17746 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
17747 LPFC_FCF_FLOGI_FAILED) {
17748 if (list_is_singular(&phba->fcf.fcf_pri_list))
17749 return LPFC_FCOE_FCF_NEXT_NONE;
17751 goto next_priority;
17754 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17755 "2845 Get next roundrobin failover FCF (x%x)\n",
17756 next_fcf_index);
17758 return next_fcf_index;
17762 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17763 * @phba: pointer to lpfc hba data structure.
17765 * This routine sets the FCF record index in to the eligible bmask for
17766 * roundrobin failover search. It checks to make sure that the index
17767 * does not go beyond the range of the driver allocated bmask dimension
17768 * before setting the bit.
17770 * Returns 0 if the index bit successfully set, otherwise, it returns
17771 * -EINVAL.
17774 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17776 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17777 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17778 "2610 FCF (x%x) reached driver's book "
17779 "keeping dimension:x%x\n",
17780 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17781 return -EINVAL;
17783 /* Set the eligible FCF record index bmask */
17784 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17786 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17787 "2790 Set FCF (x%x) to roundrobin FCF failover "
17788 "bmask\n", fcf_index);
17790 return 0;
17794 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
17795 * @phba: pointer to lpfc hba data structure.
17797 * This routine clears the FCF record index from the eligible bmask for
17798 * roundrobin failover search. It checks to make sure that the index
17799 * does not go beyond the range of the driver allocated bmask dimension
17800 * before clearing the bit.
17802 void
17803 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17805 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
17806 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17807 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17808 "2762 FCF (x%x) reached driver's book "
17809 "keeping dimension:x%x\n",
17810 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17811 return;
17813 /* Clear the eligible FCF record index bmask */
17814 spin_lock_irq(&phba->hbalock);
17815 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17816 list) {
17817 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17818 list_del_init(&fcf_pri->list);
17819 break;
17822 spin_unlock_irq(&phba->hbalock);
17823 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17825 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17826 "2791 Clear FCF (x%x) from roundrobin failover "
17827 "bmask\n", fcf_index);
17831 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17832 * @phba: pointer to lpfc hba data structure.
17834 * This routine is the completion routine for the rediscover FCF table mailbox
17835 * command. If the mailbox command returned failure, it will try to stop the
17836 * FCF rediscover wait timer.
17838 static void
17839 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17841 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17842 uint32_t shdr_status, shdr_add_status;
17844 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17846 shdr_status = bf_get(lpfc_mbox_hdr_status,
17847 &redisc_fcf->header.cfg_shdr.response);
17848 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17849 &redisc_fcf->header.cfg_shdr.response);
17850 if (shdr_status || shdr_add_status) {
17851 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17852 "2746 Requesting for FCF rediscovery failed "
17853 "status x%x add_status x%x\n",
17854 shdr_status, shdr_add_status);
17855 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
17856 spin_lock_irq(&phba->hbalock);
17857 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
17858 spin_unlock_irq(&phba->hbalock);
17860 * CVL event triggered FCF rediscover request failed,
17861 * last resort to re-try current registered FCF entry.
17863 lpfc_retry_pport_discovery(phba);
17864 } else {
17865 spin_lock_irq(&phba->hbalock);
17866 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
17867 spin_unlock_irq(&phba->hbalock);
17869 * DEAD FCF event triggered FCF rediscover request
17870 * failed, last resort to fail over as a link down
17871 * to FCF registration.
17873 lpfc_sli4_fcf_dead_failthrough(phba);
17875 } else {
17876 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17877 "2775 Start FCF rediscover quiescent timer\n");
17879 * Start FCF rediscovery wait timer for pending FCF
17880 * before rescan FCF record table.
17882 lpfc_fcf_redisc_wait_start_timer(phba);
17885 mempool_free(mbox, phba->mbox_mem_pool);
17889 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
17890 * @phba: pointer to lpfc hba data structure.
17892 * This routine is invoked to request for rediscovery of the entire FCF table
17893 * by the port.
17896 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17898 LPFC_MBOXQ_t *mbox;
17899 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17900 int rc, length;
17902 /* Cancel retry delay timers to all vports before FCF rediscover */
17903 lpfc_cancel_all_vport_retry_delay_timer(phba);
17905 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17906 if (!mbox) {
17907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17908 "2745 Failed to allocate mbox for "
17909 "requesting FCF rediscover.\n");
17910 return -ENOMEM;
17913 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17914 sizeof(struct lpfc_sli4_cfg_mhdr));
17915 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17916 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17917 length, LPFC_SLI4_MBX_EMBED);
17919 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17920 /* Set count to 0 for invalidating the entire FCF database */
17921 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17923 /* Issue the mailbox command asynchronously */
17924 mbox->vport = phba->pport;
17925 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17926 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17928 if (rc == MBX_NOT_FINISHED) {
17929 mempool_free(mbox, phba->mbox_mem_pool);
17930 return -EIO;
17932 return 0;
17936 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17937 * @phba: pointer to lpfc hba data structure.
17939 * This function is the failover routine as a last resort to the FCF DEAD
17940 * event when driver failed to perform fast FCF failover.
17942 void
17943 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17945 uint32_t link_state;
17948 * Last resort as FCF DEAD event failover will treat this as
17949 * a link down, but save the link state because we don't want
17950 * it to be changed to Link Down unless it is already down.
17952 link_state = phba->link_state;
17953 lpfc_linkdown(phba);
17954 phba->link_state = link_state;
17956 /* Unregister FCF if no devices connected to it */
17957 lpfc_unregister_unused_fcf(phba);
17961 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
17962 * @phba: pointer to lpfc hba data structure.
17963 * @rgn23_data: pointer to configure region 23 data.
17965 * This function gets SLI3 port configure region 23 data through memory dump
17966 * mailbox command. When it successfully retrieves data, the size of the data
17967 * will be returned, otherwise, 0 will be returned.
17969 static uint32_t
17970 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
17972 LPFC_MBOXQ_t *pmb = NULL;
17973 MAILBOX_t *mb;
17974 uint32_t offset = 0;
17975 int rc;
17977 if (!rgn23_data)
17978 return 0;
17980 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17981 if (!pmb) {
17982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17983 "2600 failed to allocate mailbox memory\n");
17984 return 0;
17986 mb = &pmb->u.mb;
17988 do {
17989 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
17990 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
17992 if (rc != MBX_SUCCESS) {
17993 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17994 "2601 failed to read config "
17995 "region 23, rc 0x%x Status 0x%x\n",
17996 rc, mb->mbxStatus);
17997 mb->un.varDmp.word_cnt = 0;
18000 * dump mem may return a zero when finished or we got a
18001 * mailbox error, either way we are done.
18003 if (mb->un.varDmp.word_cnt == 0)
18004 break;
18005 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18006 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18008 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18009 rgn23_data + offset,
18010 mb->un.varDmp.word_cnt);
18011 offset += mb->un.varDmp.word_cnt;
18012 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18014 mempool_free(pmb, phba->mbox_mem_pool);
18015 return offset;
18019 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18020 * @phba: pointer to lpfc hba data structure.
18021 * @rgn23_data: pointer to configure region 23 data.
18023 * This function gets SLI4 port configure region 23 data through memory dump
18024 * mailbox command. When it successfully retrieves data, the size of the data
18025 * will be returned, otherwise, 0 will be returned.
18027 static uint32_t
18028 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18030 LPFC_MBOXQ_t *mboxq = NULL;
18031 struct lpfc_dmabuf *mp = NULL;
18032 struct lpfc_mqe *mqe;
18033 uint32_t data_length = 0;
18034 int rc;
18036 if (!rgn23_data)
18037 return 0;
18039 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18040 if (!mboxq) {
18041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18042 "3105 failed to allocate mailbox memory\n");
18043 return 0;
18046 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18047 goto out;
18048 mqe = &mboxq->u.mqe;
18049 mp = (struct lpfc_dmabuf *) mboxq->context1;
18050 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18051 if (rc)
18052 goto out;
18053 data_length = mqe->un.mb_words[5];
18054 if (data_length == 0)
18055 goto out;
18056 if (data_length > DMP_RGN23_SIZE) {
18057 data_length = 0;
18058 goto out;
18060 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18061 out:
18062 mempool_free(mboxq, phba->mbox_mem_pool);
18063 if (mp) {
18064 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18065 kfree(mp);
18067 return data_length;
18071 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18072 * @phba: pointer to lpfc hba data structure.
18074 * This function read region 23 and parse TLV for port status to
18075 * decide if the user disaled the port. If the TLV indicates the
18076 * port is disabled, the hba_flag is set accordingly.
18078 void
18079 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18081 uint8_t *rgn23_data = NULL;
18082 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18083 uint32_t offset = 0;
18085 /* Get adapter Region 23 data */
18086 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18087 if (!rgn23_data)
18088 goto out;
18090 if (phba->sli_rev < LPFC_SLI_REV4)
18091 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18092 else {
18093 if_type = bf_get(lpfc_sli_intf_if_type,
18094 &phba->sli4_hba.sli_intf);
18095 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18096 goto out;
18097 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18100 if (!data_size)
18101 goto out;
18103 /* Check the region signature first */
18104 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18106 "2619 Config region 23 has bad signature\n");
18107 goto out;
18109 offset += 4;
18111 /* Check the data structure version */
18112 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18114 "2620 Config region 23 has bad version\n");
18115 goto out;
18117 offset += 4;
18119 /* Parse TLV entries in the region */
18120 while (offset < data_size) {
18121 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18122 break;
18124 * If the TLV is not driver specific TLV or driver id is
18125 * not linux driver id, skip the record.
18127 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18128 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18129 (rgn23_data[offset + 3] != 0)) {
18130 offset += rgn23_data[offset + 1] * 4 + 4;
18131 continue;
18134 /* Driver found a driver specific TLV in the config region */
18135 sub_tlv_len = rgn23_data[offset + 1] * 4;
18136 offset += 4;
18137 tlv_offset = 0;
18140 * Search for configured port state sub-TLV.
18142 while ((offset < data_size) &&
18143 (tlv_offset < sub_tlv_len)) {
18144 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18145 offset += 4;
18146 tlv_offset += 4;
18147 break;
18149 if (rgn23_data[offset] != PORT_STE_TYPE) {
18150 offset += rgn23_data[offset + 1] * 4 + 4;
18151 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18152 continue;
18155 /* This HBA contains PORT_STE configured */
18156 if (!rgn23_data[offset + 2])
18157 phba->hba_flag |= LINK_DISABLED;
18159 goto out;
18163 out:
18164 kfree(rgn23_data);
18165 return;
18169 * lpfc_wr_object - write an object to the firmware
18170 * @phba: HBA structure that indicates port to create a queue on.
18171 * @dmabuf_list: list of dmabufs to write to the port.
18172 * @size: the total byte value of the objects to write to the port.
18173 * @offset: the current offset to be used to start the transfer.
18175 * This routine will create a wr_object mailbox command to send to the port.
18176 * the mailbox command will be constructed using the dma buffers described in
18177 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18178 * BDEs that the imbedded mailbox can support. The @offset variable will be
18179 * used to indicate the starting offset of the transfer and will also return
18180 * the offset after the write object mailbox has completed. @size is used to
18181 * determine the end of the object and whether the eof bit should be set.
18183 * Return 0 is successful and offset will contain the the new offset to use
18184 * for the next write.
18185 * Return negative value for error cases.
18188 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18189 uint32_t size, uint32_t *offset)
18191 struct lpfc_mbx_wr_object *wr_object;
18192 LPFC_MBOXQ_t *mbox;
18193 int rc = 0, i = 0;
18194 uint32_t shdr_status, shdr_add_status;
18195 uint32_t mbox_tmo;
18196 union lpfc_sli4_cfg_shdr *shdr;
18197 struct lpfc_dmabuf *dmabuf;
18198 uint32_t written = 0;
18200 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18201 if (!mbox)
18202 return -ENOMEM;
18204 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18205 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18206 sizeof(struct lpfc_mbx_wr_object) -
18207 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18209 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18210 wr_object->u.request.write_offset = *offset;
18211 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18212 wr_object->u.request.object_name[0] =
18213 cpu_to_le32(wr_object->u.request.object_name[0]);
18214 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18215 list_for_each_entry(dmabuf, dmabuf_list, list) {
18216 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18217 break;
18218 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18219 wr_object->u.request.bde[i].addrHigh =
18220 putPaddrHigh(dmabuf->phys);
18221 if (written + SLI4_PAGE_SIZE >= size) {
18222 wr_object->u.request.bde[i].tus.f.bdeSize =
18223 (size - written);
18224 written += (size - written);
18225 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18226 } else {
18227 wr_object->u.request.bde[i].tus.f.bdeSize =
18228 SLI4_PAGE_SIZE;
18229 written += SLI4_PAGE_SIZE;
18231 i++;
18233 wr_object->u.request.bde_count = i;
18234 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18235 if (!phba->sli4_hba.intr_enable)
18236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18237 else {
18238 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18239 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18241 /* The IOCTL status is embedded in the mailbox subheader. */
18242 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18243 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18244 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18245 if (rc != MBX_TIMEOUT)
18246 mempool_free(mbox, phba->mbox_mem_pool);
18247 if (shdr_status || shdr_add_status || rc) {
18248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18249 "3025 Write Object mailbox failed with "
18250 "status x%x add_status x%x, mbx status x%x\n",
18251 shdr_status, shdr_add_status, rc);
18252 rc = -ENXIO;
18253 } else
18254 *offset += wr_object->u.response.actual_write_length;
18255 return rc;
18259 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18260 * @vport: pointer to vport data structure.
18262 * This function iterate through the mailboxq and clean up all REG_LOGIN
18263 * and REG_VPI mailbox commands associated with the vport. This function
18264 * is called when driver want to restart discovery of the vport due to
18265 * a Clear Virtual Link event.
18267 void
18268 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18270 struct lpfc_hba *phba = vport->phba;
18271 LPFC_MBOXQ_t *mb, *nextmb;
18272 struct lpfc_dmabuf *mp;
18273 struct lpfc_nodelist *ndlp;
18274 struct lpfc_nodelist *act_mbx_ndlp = NULL;
18275 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
18276 LIST_HEAD(mbox_cmd_list);
18277 uint8_t restart_loop;
18279 /* Clean up internally queued mailbox commands with the vport */
18280 spin_lock_irq(&phba->hbalock);
18281 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18282 if (mb->vport != vport)
18283 continue;
18285 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18286 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18287 continue;
18289 list_del(&mb->list);
18290 list_add_tail(&mb->list, &mbox_cmd_list);
18292 /* Clean up active mailbox command with the vport */
18293 mb = phba->sli.mbox_active;
18294 if (mb && (mb->vport == vport)) {
18295 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18296 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18297 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18298 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18299 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18300 /* Put reference count for delayed processing */
18301 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18302 /* Unregister the RPI when mailbox complete */
18303 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18306 /* Cleanup any mailbox completions which are not yet processed */
18307 do {
18308 restart_loop = 0;
18309 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18311 * If this mailox is already processed or it is
18312 * for another vport ignore it.
18314 if ((mb->vport != vport) ||
18315 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18316 continue;
18318 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18319 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18320 continue;
18322 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18323 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18324 ndlp = (struct lpfc_nodelist *)mb->context2;
18325 /* Unregister the RPI when mailbox complete */
18326 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18327 restart_loop = 1;
18328 spin_unlock_irq(&phba->hbalock);
18329 spin_lock(shost->host_lock);
18330 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18331 spin_unlock(shost->host_lock);
18332 spin_lock_irq(&phba->hbalock);
18333 break;
18336 } while (restart_loop);
18338 spin_unlock_irq(&phba->hbalock);
18340 /* Release the cleaned-up mailbox commands */
18341 while (!list_empty(&mbox_cmd_list)) {
18342 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
18343 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18344 mp = (struct lpfc_dmabuf *) (mb->context1);
18345 if (mp) {
18346 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18347 kfree(mp);
18349 ndlp = (struct lpfc_nodelist *) mb->context2;
18350 mb->context2 = NULL;
18351 if (ndlp) {
18352 spin_lock(shost->host_lock);
18353 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18354 spin_unlock(shost->host_lock);
18355 lpfc_nlp_put(ndlp);
18358 mempool_free(mb, phba->mbox_mem_pool);
18361 /* Release the ndlp with the cleaned-up active mailbox command */
18362 if (act_mbx_ndlp) {
18363 spin_lock(shost->host_lock);
18364 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18365 spin_unlock(shost->host_lock);
18366 lpfc_nlp_put(act_mbx_ndlp);
18371 * lpfc_drain_txq - Drain the txq
18372 * @phba: Pointer to HBA context object.
18374 * This function attempt to submit IOCBs on the txq
18375 * to the adapter. For SLI4 adapters, the txq contains
18376 * ELS IOCBs that have been deferred because the there
18377 * are no SGLs. This congestion can occur with large
18378 * vport counts during node discovery.
18381 uint32_t
18382 lpfc_drain_txq(struct lpfc_hba *phba)
18384 LIST_HEAD(completions);
18385 struct lpfc_sli_ring *pring;
18386 struct lpfc_iocbq *piocbq = NULL;
18387 unsigned long iflags = 0;
18388 char *fail_msg = NULL;
18389 struct lpfc_sglq *sglq;
18390 union lpfc_wqe128 wqe128;
18391 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
18392 uint32_t txq_cnt = 0;
18394 pring = lpfc_phba_elsring(phba);
18396 spin_lock_irqsave(&pring->ring_lock, iflags);
18397 list_for_each_entry(piocbq, &pring->txq, list) {
18398 txq_cnt++;
18401 if (txq_cnt > pring->txq_max)
18402 pring->txq_max = txq_cnt;
18404 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18406 while (!list_empty(&pring->txq)) {
18407 spin_lock_irqsave(&pring->ring_lock, iflags);
18409 piocbq = lpfc_sli_ringtx_get(phba, pring);
18410 if (!piocbq) {
18411 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18413 "2823 txq empty and txq_cnt is %d\n ",
18414 txq_cnt);
18415 break;
18417 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
18418 if (!sglq) {
18419 __lpfc_sli_ringtx_put(phba, pring, piocbq);
18420 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18421 break;
18423 txq_cnt--;
18425 /* The xri and iocb resources secured,
18426 * attempt to issue request
18428 piocbq->sli4_lxritag = sglq->sli4_lxritag;
18429 piocbq->sli4_xritag = sglq->sli4_xritag;
18430 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18431 fail_msg = "to convert bpl to sgl";
18432 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
18433 fail_msg = "to convert iocb to wqe";
18434 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
18435 fail_msg = " - Wq is full";
18436 else
18437 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18439 if (fail_msg) {
18440 /* Failed means we can't issue and need to cancel */
18441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18442 "2822 IOCB failed %s iotag 0x%x "
18443 "xri 0x%x\n",
18444 fail_msg,
18445 piocbq->iotag, piocbq->sli4_xritag);
18446 list_add_tail(&piocbq->list, &completions);
18448 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18451 /* Cancel all the IOCBs that cannot be issued */
18452 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18453 IOERR_SLI_ABORTED);
18455 return txq_cnt;
18459 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18460 * @phba: Pointer to HBA context object.
18461 * @pwqe: Pointer to command WQE.
18462 * @sglq: Pointer to the scatter gather queue object.
18464 * This routine converts the bpl or bde that is in the WQE
18465 * to a sgl list for the sli4 hardware. The physical address
18466 * of the bpl/bde is converted back to a virtual address.
18467 * If the WQE contains a BPL then the list of BDE's is
18468 * converted to sli4_sge's. If the WQE contains a single
18469 * BDE then it is converted to a single sli_sge.
18470 * The WQE is still in cpu endianness so the contents of
18471 * the bpl can be used without byte swapping.
18473 * Returns valid XRI = Success, NO_XRI = Failure.
18475 static uint16_t
18476 lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18477 struct lpfc_sglq *sglq)
18479 uint16_t xritag = NO_XRI;
18480 struct ulp_bde64 *bpl = NULL;
18481 struct ulp_bde64 bde;
18482 struct sli4_sge *sgl = NULL;
18483 struct lpfc_dmabuf *dmabuf;
18484 union lpfc_wqe *wqe;
18485 int numBdes = 0;
18486 int i = 0;
18487 uint32_t offset = 0; /* accumulated offset in the sg request list */
18488 int inbound = 0; /* number of sg reply entries inbound from firmware */
18489 uint32_t cmd;
18491 if (!pwqeq || !sglq)
18492 return xritag;
18494 sgl = (struct sli4_sge *)sglq->sgl;
18495 wqe = &pwqeq->wqe;
18496 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18498 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18499 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18500 return sglq->sli4_xritag;
18501 numBdes = pwqeq->rsvd2;
18502 if (numBdes) {
18503 /* The addrHigh and addrLow fields within the WQE
18504 * have not been byteswapped yet so there is no
18505 * need to swap them back.
18507 if (pwqeq->context3)
18508 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18509 else
18510 return xritag;
18512 bpl = (struct ulp_bde64 *)dmabuf->virt;
18513 if (!bpl)
18514 return xritag;
18516 for (i = 0; i < numBdes; i++) {
18517 /* Should already be byte swapped. */
18518 sgl->addr_hi = bpl->addrHigh;
18519 sgl->addr_lo = bpl->addrLow;
18521 sgl->word2 = le32_to_cpu(sgl->word2);
18522 if ((i+1) == numBdes)
18523 bf_set(lpfc_sli4_sge_last, sgl, 1);
18524 else
18525 bf_set(lpfc_sli4_sge_last, sgl, 0);
18526 /* swap the size field back to the cpu so we
18527 * can assign it to the sgl.
18529 bde.tus.w = le32_to_cpu(bpl->tus.w);
18530 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18531 /* The offsets in the sgl need to be accumulated
18532 * separately for the request and reply lists.
18533 * The request is always first, the reply follows.
18535 switch (cmd) {
18536 case CMD_GEN_REQUEST64_WQE:
18537 /* add up the reply sg entries */
18538 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18539 inbound++;
18540 /* first inbound? reset the offset */
18541 if (inbound == 1)
18542 offset = 0;
18543 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18544 bf_set(lpfc_sli4_sge_type, sgl,
18545 LPFC_SGE_TYPE_DATA);
18546 offset += bde.tus.f.bdeSize;
18547 break;
18548 case CMD_FCP_TRSP64_WQE:
18549 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18550 bf_set(lpfc_sli4_sge_type, sgl,
18551 LPFC_SGE_TYPE_DATA);
18552 break;
18553 case CMD_FCP_TSEND64_WQE:
18554 case CMD_FCP_TRECEIVE64_WQE:
18555 bf_set(lpfc_sli4_sge_type, sgl,
18556 bpl->tus.f.bdeFlags);
18557 if (i < 3)
18558 offset = 0;
18559 else
18560 offset += bde.tus.f.bdeSize;
18561 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18562 break;
18564 sgl->word2 = cpu_to_le32(sgl->word2);
18565 bpl++;
18566 sgl++;
18568 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18569 /* The addrHigh and addrLow fields of the BDE have not
18570 * been byteswapped yet so they need to be swapped
18571 * before putting them in the sgl.
18573 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18574 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18575 sgl->word2 = le32_to_cpu(sgl->word2);
18576 bf_set(lpfc_sli4_sge_last, sgl, 1);
18577 sgl->word2 = cpu_to_le32(sgl->word2);
18578 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18580 return sglq->sli4_xritag;
18584 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18585 * @phba: Pointer to HBA context object.
18586 * @ring_number: Base sli ring number
18587 * @pwqe: Pointer to command WQE.
18590 lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18591 struct lpfc_iocbq *pwqe)
18593 union lpfc_wqe *wqe = &pwqe->wqe;
18594 struct lpfc_nvmet_rcv_ctx *ctxp;
18595 struct lpfc_queue *wq;
18596 struct lpfc_sglq *sglq;
18597 struct lpfc_sli_ring *pring;
18598 unsigned long iflags;
18600 /* NVME_LS and NVME_LS ABTS requests. */
18601 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18602 pring = phba->sli4_hba.nvmels_wq->pring;
18603 spin_lock_irqsave(&pring->ring_lock, iflags);
18604 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18605 if (!sglq) {
18606 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18607 return WQE_BUSY;
18609 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18610 pwqe->sli4_xritag = sglq->sli4_xritag;
18611 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18612 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18613 return WQE_ERROR;
18615 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18616 pwqe->sli4_xritag);
18617 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18618 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18619 return WQE_ERROR;
18621 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18622 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18623 return 0;
18626 /* NVME_FCREQ and NVME_ABTS requests */
18627 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18628 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18629 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18631 spin_lock_irqsave(&pring->ring_lock, iflags);
18632 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18633 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18634 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18635 if (lpfc_sli4_wq_put(wq, wqe)) {
18636 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18637 return WQE_ERROR;
18639 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18640 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18641 return 0;
18644 /* NVMET requests */
18645 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18646 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18647 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18649 spin_lock_irqsave(&pring->ring_lock, iflags);
18650 ctxp = pwqe->context2;
18651 sglq = ctxp->rqb_buffer->sglq;
18652 if (pwqe->sli4_xritag == NO_XRI) {
18653 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18654 pwqe->sli4_xritag = sglq->sli4_xritag;
18656 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18657 pwqe->sli4_xritag);
18658 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18659 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18660 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18661 if (lpfc_sli4_wq_put(wq, wqe)) {
18662 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18663 return WQE_ERROR;
18665 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18666 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18667 return 0;
18669 return WQE_ERROR;