[SCSI] lpfc 8.3.27: Fix queue allocation failure recovery
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / lpfc / lpfc_sli.c
blobc430aada02bebf67aa4230184e192c5630c7da2a
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
71 static IOCB_t *
72 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
74 return &iocbq->iocb;
77 /**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89 static uint32_t
90 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
92 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93 struct lpfc_register doorbell;
94 uint32_t host_index;
96 /* If the host has not yet processed the next entry then we are done */
97 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98 return -ENOMEM;
99 /* set consumption flag every once in a while */
100 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
101 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
102 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
104 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
106 /* Update the host index before invoking device */
107 host_index = q->host_index;
108 q->host_index = ((q->host_index + 1) % q->entry_count);
110 /* Ring Doorbell */
111 doorbell.word0 = 0;
112 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
118 return 0;
122 * lpfc_sli4_wq_release - Updates internal hba index for WQ
123 * @q: The Work Queue to operate on.
124 * @index: The index to advance the hba index to.
126 * This routine will update the HBA index of a queue to reflect consumption of
127 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128 * an entry the host calls this function to update the queue's internal
129 * pointers. This routine returns the number of entries that were consumed by
130 * the HBA.
132 static uint32_t
133 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
135 uint32_t released = 0;
137 if (q->hba_index == index)
138 return 0;
139 do {
140 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141 released++;
142 } while (q->hba_index != index);
143 return released;
147 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148 * @q: The Mailbox Queue to operate on.
149 * @wqe: The Mailbox Queue Entry to put on the Work queue.
151 * This routine will copy the contents of @mqe to the next available entry on
152 * the @q. This function will then ring the Work Queue Doorbell to signal the
153 * HBA to start processing the Work Queue Entry. This function returns 0 if
154 * successful. If no entries are available on @q then this function will return
155 * -ENOMEM.
156 * The caller is expected to hold the hbalock when calling this routine.
158 static uint32_t
159 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
161 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162 struct lpfc_register doorbell;
163 uint32_t host_index;
165 /* If the host has not yet processed the next entry then we are done */
166 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167 return -ENOMEM;
168 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169 /* Save off the mailbox pointer for completion */
170 q->phba->mbox = (MAILBOX_t *)temp_mqe;
172 /* Update the host index before invoking device */
173 host_index = q->host_index;
174 q->host_index = ((q->host_index + 1) % q->entry_count);
176 /* Ring Doorbell */
177 doorbell.word0 = 0;
178 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182 return 0;
186 * lpfc_sli4_mq_release - Updates internal hba index for MQ
187 * @q: The Mailbox Queue to operate on.
189 * This routine will update the HBA index of a queue to reflect consumption of
190 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191 * an entry the host calls this function to update the queue's internal
192 * pointers. This routine returns the number of entries that were consumed by
193 * the HBA.
195 static uint32_t
196 lpfc_sli4_mq_release(struct lpfc_queue *q)
198 /* Clear the mailbox pointer for completion */
199 q->phba->mbox = NULL;
200 q->hba_index = ((q->hba_index + 1) % q->entry_count);
201 return 1;
205 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206 * @q: The Event Queue to get the first valid EQE from
208 * This routine will get the first valid Event Queue Entry from @q, update
209 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210 * the Queue (no more work to do), or the Queue is full of EQEs that have been
211 * processed, but not popped back to the HBA then this routine will return NULL.
213 static struct lpfc_eqe *
214 lpfc_sli4_eq_get(struct lpfc_queue *q)
216 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
218 /* If the next EQE is not valid then we are done */
219 if (!bf_get_le32(lpfc_eqe_valid, eqe))
220 return NULL;
221 /* If the host has not yet processed the next entry then we are done */
222 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223 return NULL;
225 q->hba_index = ((q->hba_index + 1) % q->entry_count);
226 return eqe;
230 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231 * @q: The Event Queue that the host has completed processing for.
232 * @arm: Indicates whether the host wants to arms this CQ.
234 * This routine will mark all Event Queue Entries on @q, from the last
235 * known completed entry to the last entry that was processed, as completed
236 * by clearing the valid bit for each completion queue entry. Then it will
237 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238 * The internal host index in the @q will be updated by this routine to indicate
239 * that the host has finished processing the entries. The @arm parameter
240 * indicates that the queue should be rearmed when ringing the doorbell.
242 * This function will return the number of EQEs that were popped.
244 uint32_t
245 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
247 uint32_t released = 0;
248 struct lpfc_eqe *temp_eqe;
249 struct lpfc_register doorbell;
251 /* while there are valid entries */
252 while (q->hba_index != q->host_index) {
253 temp_eqe = q->qe[q->host_index].eqe;
254 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
255 released++;
256 q->host_index = ((q->host_index + 1) % q->entry_count);
258 if (unlikely(released == 0 && !arm))
259 return 0;
261 /* ring doorbell for number popped */
262 doorbell.word0 = 0;
263 if (arm) {
264 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
267 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
271 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273 readl(q->phba->sli4_hba.EQCQDBregaddr);
274 return released;
278 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279 * @q: The Completion Queue to get the first valid CQE from
281 * This routine will get the first valid Completion Queue Entry from @q, update
282 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283 * the Queue (no more work to do), or the Queue is full of CQEs that have been
284 * processed, but not popped back to the HBA then this routine will return NULL.
286 static struct lpfc_cqe *
287 lpfc_sli4_cq_get(struct lpfc_queue *q)
289 struct lpfc_cqe *cqe;
291 /* If the next CQE is not valid then we are done */
292 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
293 return NULL;
294 /* If the host has not yet processed the next entry then we are done */
295 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296 return NULL;
298 cqe = q->qe[q->hba_index].cqe;
299 q->hba_index = ((q->hba_index + 1) % q->entry_count);
300 return cqe;
304 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305 * @q: The Completion Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
308 * This routine will mark all Completion queue entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
316 * This function will return the number of CQEs that were released.
318 uint32_t
319 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
321 uint32_t released = 0;
322 struct lpfc_cqe *temp_qe;
323 struct lpfc_register doorbell;
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_qe = q->qe[q->host_index].cqe;
328 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
332 if (unlikely(released == 0 && !arm))
333 return 0;
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm)
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343 return released;
347 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348 * @q: The Header Receive Queue to operate on.
349 * @wqe: The Receive Queue Entry to put on the Receive queue.
351 * This routine will copy the contents of @wqe to the next available entry on
352 * the @q. This function will then ring the Receive Queue Doorbell to signal the
353 * HBA to start processing the Receive Queue Entry. This function returns the
354 * index that the rqe was copied to if successful. If no entries are available
355 * on @q then this function will return -ENOMEM.
356 * The caller is expected to hold the hbalock when calling this routine.
358 static int
359 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
362 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364 struct lpfc_register doorbell;
365 int put_index = hq->host_index;
367 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368 return -EINVAL;
369 if (hq->host_index != dq->host_index)
370 return -EINVAL;
371 /* If the host has not yet processed the next entry then we are done */
372 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373 return -EBUSY;
374 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
377 /* Update the host index to point to the next slot */
378 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
381 /* Ring The Header Receive Queue Doorbell */
382 if (!(hq->host_index % hq->entry_repost)) {
383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
385 hq->entry_repost);
386 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
389 return put_index;
393 * lpfc_sli4_rq_release - Updates internal hba index for RQ
394 * @q: The Header Receive Queue to operate on.
396 * This routine will update the HBA index of a queue to reflect consumption of
397 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398 * consumed an entry the host calls this function to update the queue's
399 * internal pointers. This routine returns the number of entries that were
400 * consumed by the HBA.
402 static uint32_t
403 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
405 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406 return 0;
407 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409 return 1;
413 * lpfc_cmd_iocb - Get next command iocb entry in the ring
414 * @phba: Pointer to HBA context object.
415 * @pring: Pointer to driver SLI ring object.
417 * This function returns pointer to next command iocb entry
418 * in the command ring. The caller must hold hbalock to prevent
419 * other threads consume the next command iocb.
420 * SLI-2/SLI-3 provide different sized iocbs.
422 static inline IOCB_t *
423 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
425 return (IOCB_t *) (((char *) pring->cmdringaddr) +
426 pring->cmdidx * phba->iocb_cmd_size);
430 * lpfc_resp_iocb - Get next response iocb entry in the ring
431 * @phba: Pointer to HBA context object.
432 * @pring: Pointer to driver SLI ring object.
434 * This function returns pointer to next response iocb entry
435 * in the response ring. The caller must hold hbalock to make sure
436 * that no other thread consume the next response iocb.
437 * SLI-2/SLI-3 provide different sized iocbs.
439 static inline IOCB_t *
440 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
442 return (IOCB_t *) (((char *) pring->rspringaddr) +
443 pring->rspidx * phba->iocb_rsp_size);
447 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
448 * @phba: Pointer to HBA context object.
450 * This function is called with hbalock held. This function
451 * allocates a new driver iocb object from the iocb pool. If the
452 * allocation is successful, it returns pointer to the newly
453 * allocated iocb object else it returns NULL.
455 static struct lpfc_iocbq *
456 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
458 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459 struct lpfc_iocbq * iocbq = NULL;
461 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
462 if (iocbq)
463 phba->iocb_cnt++;
464 if (phba->iocb_cnt > phba->iocb_max)
465 phba->iocb_max = phba->iocb_cnt;
466 return iocbq;
470 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471 * @phba: Pointer to HBA context object.
472 * @xritag: XRI value.
474 * This function clears the sglq pointer from the array of acive
475 * sglq's. The xritag that is passed in is used to index into the
476 * array. Before the xritag can be used it needs to be adjusted
477 * by subtracting the xribase.
479 * Returns sglq ponter = success, NULL = Failure.
481 static struct lpfc_sglq *
482 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
484 struct lpfc_sglq *sglq;
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
488 return sglq;
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
501 * Returns sglq ponter = success, NULL = Failure.
503 struct lpfc_sglq *
504 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
506 struct lpfc_sglq *sglq;
508 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
509 return sglq;
513 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514 * @phba: Pointer to HBA context object.
515 * @ndlp: nodelist pointer for this target.
516 * @xritag: xri used in this exchange.
517 * @rxid: Remote Exchange ID.
518 * @send_rrq: Flag used to determine if we should send rrq els cmd.
520 * This function is called with hbalock held.
521 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522 * rrq struct and adds it to the active_rrq_list.
524 * returns 0 for rrq slot for this xri
525 * < 0 Were not able to get rrq mem or invalid parameter.
527 static int
528 __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
531 struct lpfc_node_rrq *rrq;
532 int empty;
533 uint32_t did = 0;
536 if (!ndlp)
537 return -EINVAL;
539 if (!phba->cfg_enable_rrq)
540 return -EINVAL;
542 if (phba->pport->load_flag & FC_UNLOADING) {
543 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544 goto out;
546 did = ndlp->nlp_DID;
549 * set the active bit even if there is no mem available.
551 if (NLP_CHK_FREE_REQ(ndlp))
552 goto out;
554 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555 goto out;
557 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
558 goto out;
560 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561 if (rrq) {
562 rrq->send_rrq = send_rrq;
563 rrq->xritag = xritag;
564 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565 rrq->ndlp = ndlp;
566 rrq->nlp_DID = ndlp->nlp_DID;
567 rrq->vport = ndlp->vport;
568 rrq->rxid = rxid;
569 empty = list_empty(&phba->active_rrq_list);
570 rrq->send_rrq = send_rrq;
571 list_add_tail(&rrq->list, &phba->active_rrq_list);
572 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573 phba->hba_flag |= HBA_RRQ_ACTIVE;
574 if (empty)
575 lpfc_worker_wake_up(phba);
577 return 0;
579 out:
580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582 " DID:0x%x Send:%d\n",
583 xritag, rxid, did, send_rrq);
584 return -EINVAL;
588 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
589 * @phba: Pointer to HBA context object.
590 * @xritag: xri used in this exchange.
591 * @rrq: The RRQ to be cleared.
594 void
595 lpfc_clr_rrq_active(struct lpfc_hba *phba,
596 uint16_t xritag,
597 struct lpfc_node_rrq *rrq)
599 struct lpfc_nodelist *ndlp = NULL;
601 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
604 /* The target DID could have been swapped (cable swap)
605 * we should use the ndlp from the findnode if it is
606 * available.
608 if ((!ndlp) && rrq->ndlp)
609 ndlp = rrq->ndlp;
611 if (!ndlp)
612 goto out;
614 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
615 rrq->send_rrq = 0;
616 rrq->xritag = 0;
617 rrq->rrq_stop_time = 0;
619 out:
620 mempool_free(rrq, phba->rrq_pool);
624 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625 * @phba: Pointer to HBA context object.
627 * This function is called with hbalock held. This function
628 * Checks if stop_time (ratov from setting rrq active) has
629 * been reached, if it has and the send_rrq flag is set then
630 * it will call lpfc_send_rrq. If the send_rrq flag is not set
631 * then it will just call the routine to clear the rrq and
632 * free the rrq resource.
633 * The timer is set to the next rrq that is going to expire before
634 * leaving the routine.
637 void
638 lpfc_handle_rrq_active(struct lpfc_hba *phba)
640 struct lpfc_node_rrq *rrq;
641 struct lpfc_node_rrq *nextrrq;
642 unsigned long next_time;
643 unsigned long iflags;
644 LIST_HEAD(send_rrq);
646 spin_lock_irqsave(&phba->hbalock, iflags);
647 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648 next_time = jiffies + HZ * (phba->fc_ratov + 1);
649 list_for_each_entry_safe(rrq, nextrrq,
650 &phba->active_rrq_list, list) {
651 if (time_after(jiffies, rrq->rrq_stop_time))
652 list_move(&rrq->list, &send_rrq);
653 else if (time_before(rrq->rrq_stop_time, next_time))
654 next_time = rrq->rrq_stop_time;
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
659 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660 list_del(&rrq->list);
661 if (!rrq->send_rrq)
662 /* this call will free the rrq */
663 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664 else if (lpfc_send_rrq(phba, rrq)) {
665 /* if we send the rrq then the completion handler
666 * will clear the bit in the xribitmap.
668 lpfc_clr_rrq_active(phba, rrq->xritag,
669 rrq);
675 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676 * @vport: Pointer to vport context object.
677 * @xri: The xri used in the exchange.
678 * @did: The targets DID for this exchange.
680 * returns NULL = rrq not found in the phba->active_rrq_list.
681 * rrq = rrq for this xri and target.
683 struct lpfc_node_rrq *
684 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long iflags;
691 if (phba->sli_rev != LPFC_SLI_REV4)
692 return NULL;
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695 if (rrq->vport == vport && rrq->xritag == xri &&
696 rrq->nlp_DID == did){
697 list_del(&rrq->list);
698 spin_unlock_irqrestore(&phba->hbalock, iflags);
699 return rrq;
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return NULL;
707 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708 * @vport: Pointer to vport context object.
709 * @ndlp: Pointer to the lpfc_node_list structure.
710 * If ndlp is NULL Remove all active RRQs for this vport from the
711 * phba->active_rrq_list and clear the rrq.
712 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
714 void
715 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
718 struct lpfc_hba *phba = vport->phba;
719 struct lpfc_node_rrq *rrq;
720 struct lpfc_node_rrq *nextrrq;
721 unsigned long iflags;
722 LIST_HEAD(rrq_list);
724 if (phba->sli_rev != LPFC_SLI_REV4)
725 return;
726 if (!ndlp) {
727 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
730 spin_lock_irqsave(&phba->hbalock, iflags);
731 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
733 list_move(&rrq->list, &rrq_list);
734 spin_unlock_irqrestore(&phba->hbalock, iflags);
736 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737 list_del(&rrq->list);
738 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
743 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744 * @phba: Pointer to HBA context object.
746 * Remove all rrqs from the phba->active_rrq_list and free them by
747 * calling __lpfc_clr_active_rrq
750 void
751 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long next_time;
756 unsigned long iflags;
757 LIST_HEAD(rrq_list);
759 if (phba->sli_rev != LPFC_SLI_REV4)
760 return;
761 spin_lock_irqsave(&phba->hbalock, iflags);
762 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763 next_time = jiffies + HZ * (phba->fc_ratov * 2);
764 list_splice_init(&phba->active_rrq_list, &rrq_list);
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
767 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
768 list_del(&rrq->list);
769 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
771 if (!list_empty(&phba->active_rrq_list))
772 mod_timer(&phba->rrq_tmr, next_time);
777 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
778 * @phba: Pointer to HBA context object.
779 * @ndlp: Targets nodelist pointer for this exchange.
780 * @xritag the xri in the bitmap to test.
782 * This function is called with hbalock held. This function
783 * returns 0 = rrq not active for this xri
784 * 1 = rrq is valid for this xri.
787 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
788 uint16_t xritag)
790 if (!ndlp)
791 return 0;
792 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
793 return 1;
794 else
795 return 0;
799 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: nodelist pointer for this target.
802 * @xritag: xri used in this exchange.
803 * @rxid: Remote Exchange ID.
804 * @send_rrq: Flag used to determine if we should send rrq els cmd.
806 * This function takes the hbalock.
807 * The active bit is always set in the active rrq xri_bitmap even
808 * if there is no slot avaiable for the other rrq information.
810 * returns 0 rrq actived for this xri
811 * < 0 No memory or invalid ndlp.
814 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
817 int ret;
818 unsigned long iflags;
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return ret;
827 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828 * @phba: Pointer to HBA context object.
829 * @piocb: Pointer to the iocbq.
831 * This function is called with hbalock held. This function
832 * gets a new driver sglq object from the sglq list. If the
833 * list is not empty then it is successful, it returns pointer to the newly
834 * allocated sglq object else it returns NULL.
836 static struct lpfc_sglq *
837 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
839 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840 struct lpfc_sglq *sglq = NULL;
841 struct lpfc_sglq *start_sglq = NULL;
842 struct lpfc_scsi_buf *lpfc_cmd;
843 struct lpfc_nodelist *ndlp;
844 int found = 0;
846 if (piocbq->iocb_flag & LPFC_IO_FCP) {
847 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848 ndlp = lpfc_cmd->rdata->pnode;
849 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
851 ndlp = piocbq->context_un.ndlp;
852 else
853 ndlp = piocbq->context1;
855 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
856 start_sglq = sglq;
857 while (!found) {
858 if (!sglq)
859 return NULL;
860 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
861 /* This xri has an rrq outstanding for this DID.
862 * put it back in the list and get another xri.
864 list_add_tail(&sglq->list, lpfc_sgl_list);
865 sglq = NULL;
866 list_remove_head(lpfc_sgl_list, sglq,
867 struct lpfc_sglq, list);
868 if (sglq == start_sglq) {
869 sglq = NULL;
870 break;
871 } else
872 continue;
874 sglq->ndlp = ndlp;
875 found = 1;
876 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
877 sglq->state = SGL_ALLOCATED;
879 return sglq;
883 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
884 * @phba: Pointer to HBA context object.
886 * This function is called with no lock held. This function
887 * allocates a new driver iocb object from the iocb pool. If the
888 * allocation is successful, it returns pointer to the newly
889 * allocated iocb object else it returns NULL.
891 struct lpfc_iocbq *
892 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
894 struct lpfc_iocbq * iocbq = NULL;
895 unsigned long iflags;
897 spin_lock_irqsave(&phba->hbalock, iflags);
898 iocbq = __lpfc_sli_get_iocbq(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return iocbq;
904 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905 * @phba: Pointer to HBA context object.
906 * @iocbq: Pointer to driver iocb object.
908 * This function is called with hbalock held to release driver
909 * iocb object to the iocb pool. The iotag in the iocb object
910 * does not change for each use of the iocb object. This function
911 * clears all other fields of the iocb object when it is freed.
912 * The sqlq structure that holds the xritag and phys and virtual
913 * mappings for the scatter gather list is retrieved from the
914 * active array of sglq. The get of the sglq pointer also clears
915 * the entry in the array. If the status of the IO indiactes that
916 * this IO was aborted then the sglq entry it put on the
917 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918 * IO has good status or fails for any other reason then the sglq
919 * entry is added to the free list (lpfc_sgl_list).
921 static void
922 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
924 struct lpfc_sglq *sglq;
925 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
926 unsigned long iflag = 0;
927 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
929 if (iocbq->sli4_xritag == NO_XRI)
930 sglq = NULL;
931 else
932 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
934 if (sglq) {
935 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936 (sglq->state != SGL_XRI_ABORTED)) {
937 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938 iflag);
939 list_add(&sglq->list,
940 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941 spin_unlock_irqrestore(
942 &phba->sli4_hba.abts_sgl_list_lock, iflag);
943 } else {
944 sglq->state = SGL_FREED;
945 sglq->ndlp = NULL;
946 list_add_tail(&sglq->list,
947 &phba->sli4_hba.lpfc_sgl_list);
949 /* Check if TXQ queue needs to be serviced */
950 if (pring->txq_cnt)
951 lpfc_worker_wake_up(phba);
957 * Clean all volatile data fields, preserve iotag and node struct.
959 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
960 iocbq->sli4_lxritag = NO_XRI;
961 iocbq->sli4_xritag = NO_XRI;
962 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
967 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
968 * @phba: Pointer to HBA context object.
969 * @iocbq: Pointer to driver iocb object.
971 * This function is called with hbalock held to release driver
972 * iocb object to the iocb pool. The iotag in the iocb object
973 * does not change for each use of the iocb object. This function
974 * clears all other fields of the iocb object when it is freed.
976 static void
977 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
979 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
982 * Clean all volatile data fields, preserve iotag and node struct.
984 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
985 iocbq->sli4_xritag = NO_XRI;
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
990 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
999 static void
1000 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1002 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1003 phba->iocb_cnt--;
1007 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1008 * @phba: Pointer to HBA context object.
1009 * @iocbq: Pointer to driver iocb object.
1011 * This function is called with no lock held to release the iocb to
1012 * iocb pool.
1014 void
1015 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1017 unsigned long iflags;
1020 * Clean all volatile data fields, preserve iotag and node struct.
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 __lpfc_sli_release_iocbq(phba, iocbq);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1028 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029 * @phba: Pointer to HBA context object.
1030 * @iocblist: List of IOCBs.
1031 * @ulpstatus: ULP status in IOCB command field.
1032 * @ulpWord4: ULP word-4 in IOCB command field.
1034 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035 * on the list by invoking the complete callback function associated with the
1036 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037 * fields.
1039 void
1040 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041 uint32_t ulpstatus, uint32_t ulpWord4)
1043 struct lpfc_iocbq *piocb;
1045 while (!list_empty(iocblist)) {
1046 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1048 if (!piocb->iocb_cmpl)
1049 lpfc_sli_release_iocbq(phba, piocb);
1050 else {
1051 piocb->iocb.ulpStatus = ulpstatus;
1052 piocb->iocb.un.ulpWord[4] = ulpWord4;
1053 (piocb->iocb_cmpl) (phba, piocb, piocb);
1056 return;
1060 * lpfc_sli_iocb_cmd_type - Get the iocb type
1061 * @iocb_cmnd: iocb command code.
1063 * This function is called by ring event handler function to get the iocb type.
1064 * This function translates the iocb command to an iocb command type used to
1065 * decide the final disposition of each completed IOCB.
1066 * The function returns
1067 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068 * LPFC_SOL_IOCB if it is a solicited iocb completion
1069 * LPFC_ABORT_IOCB if it is an abort iocb
1070 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1072 * The caller is not required to hold any lock.
1074 static lpfc_iocb_type
1075 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1077 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1079 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080 return 0;
1082 switch (iocb_cmnd) {
1083 case CMD_XMIT_SEQUENCE_CR:
1084 case CMD_XMIT_SEQUENCE_CX:
1085 case CMD_XMIT_BCAST_CN:
1086 case CMD_XMIT_BCAST_CX:
1087 case CMD_ELS_REQUEST_CR:
1088 case CMD_ELS_REQUEST_CX:
1089 case CMD_CREATE_XRI_CR:
1090 case CMD_CREATE_XRI_CX:
1091 case CMD_GET_RPI_CN:
1092 case CMD_XMIT_ELS_RSP_CX:
1093 case CMD_GET_RPI_CR:
1094 case CMD_FCP_IWRITE_CR:
1095 case CMD_FCP_IWRITE_CX:
1096 case CMD_FCP_IREAD_CR:
1097 case CMD_FCP_IREAD_CX:
1098 case CMD_FCP_ICMND_CR:
1099 case CMD_FCP_ICMND_CX:
1100 case CMD_FCP_TSEND_CX:
1101 case CMD_FCP_TRSP_CX:
1102 case CMD_FCP_TRECEIVE_CX:
1103 case CMD_FCP_AUTO_TRSP_CX:
1104 case CMD_ADAPTER_MSG:
1105 case CMD_ADAPTER_DUMP:
1106 case CMD_XMIT_SEQUENCE64_CR:
1107 case CMD_XMIT_SEQUENCE64_CX:
1108 case CMD_XMIT_BCAST64_CN:
1109 case CMD_XMIT_BCAST64_CX:
1110 case CMD_ELS_REQUEST64_CR:
1111 case CMD_ELS_REQUEST64_CX:
1112 case CMD_FCP_IWRITE64_CR:
1113 case CMD_FCP_IWRITE64_CX:
1114 case CMD_FCP_IREAD64_CR:
1115 case CMD_FCP_IREAD64_CX:
1116 case CMD_FCP_ICMND64_CR:
1117 case CMD_FCP_ICMND64_CX:
1118 case CMD_FCP_TSEND64_CX:
1119 case CMD_FCP_TRSP64_CX:
1120 case CMD_FCP_TRECEIVE64_CX:
1121 case CMD_GEN_REQUEST64_CR:
1122 case CMD_GEN_REQUEST64_CX:
1123 case CMD_XMIT_ELS_RSP64_CX:
1124 case DSSCMD_IWRITE64_CR:
1125 case DSSCMD_IWRITE64_CX:
1126 case DSSCMD_IREAD64_CR:
1127 case DSSCMD_IREAD64_CX:
1128 type = LPFC_SOL_IOCB;
1129 break;
1130 case CMD_ABORT_XRI_CN:
1131 case CMD_ABORT_XRI_CX:
1132 case CMD_CLOSE_XRI_CN:
1133 case CMD_CLOSE_XRI_CX:
1134 case CMD_XRI_ABORTED_CX:
1135 case CMD_ABORT_MXRI64_CN:
1136 case CMD_XMIT_BLS_RSP64_CX:
1137 type = LPFC_ABORT_IOCB;
1138 break;
1139 case CMD_RCV_SEQUENCE_CX:
1140 case CMD_RCV_ELS_REQ_CX:
1141 case CMD_RCV_SEQUENCE64_CX:
1142 case CMD_RCV_ELS_REQ64_CX:
1143 case CMD_ASYNC_STATUS:
1144 case CMD_IOCB_RCV_SEQ64_CX:
1145 case CMD_IOCB_RCV_ELS64_CX:
1146 case CMD_IOCB_RCV_CONT64_CX:
1147 case CMD_IOCB_RET_XRI64_CX:
1148 type = LPFC_UNSOL_IOCB;
1149 break;
1150 case CMD_IOCB_XMIT_MSEQ64_CR:
1151 case CMD_IOCB_XMIT_MSEQ64_CX:
1152 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153 case CMD_IOCB_RCV_ELS_LIST64_CX:
1154 case CMD_IOCB_CLOSE_EXTENDED_CN:
1155 case CMD_IOCB_ABORT_EXTENDED_CN:
1156 case CMD_IOCB_RET_HBQE64_CN:
1157 case CMD_IOCB_FCP_IBIDIR64_CR:
1158 case CMD_IOCB_FCP_IBIDIR64_CX:
1159 case CMD_IOCB_FCP_ITASKMGT64_CX:
1160 case CMD_IOCB_LOGENTRY_CN:
1161 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162 printk("%s - Unhandled SLI-3 Command x%x\n",
1163 __func__, iocb_cmnd);
1164 type = LPFC_UNKNOWN_IOCB;
1165 break;
1166 default:
1167 type = LPFC_UNKNOWN_IOCB;
1168 break;
1171 return type;
1175 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1176 * @phba: Pointer to HBA context object.
1178 * This function is called from SLI initialization code
1179 * to configure every ring of the HBA's SLI interface. The
1180 * caller is not required to hold any lock. This function issues
1181 * a config_ring mailbox command for each ring.
1182 * This function returns zero if successful else returns a negative
1183 * error code.
1185 static int
1186 lpfc_sli_ring_map(struct lpfc_hba *phba)
1188 struct lpfc_sli *psli = &phba->sli;
1189 LPFC_MBOXQ_t *pmb;
1190 MAILBOX_t *pmbox;
1191 int i, rc, ret = 0;
1193 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194 if (!pmb)
1195 return -ENOMEM;
1196 pmbox = &pmb->u.mb;
1197 phba->link_state = LPFC_INIT_MBX_CMDS;
1198 for (i = 0; i < psli->num_rings; i++) {
1199 lpfc_config_ring(phba, i, pmb);
1200 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201 if (rc != MBX_SUCCESS) {
1202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1203 "0446 Adapter failed to init (%d), "
1204 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205 "ring %d\n",
1206 rc, pmbox->mbxCommand,
1207 pmbox->mbxStatus, i);
1208 phba->link_state = LPFC_HBA_ERROR;
1209 ret = -ENXIO;
1210 break;
1213 mempool_free(pmb, phba->mbox_mem_pool);
1214 return ret;
1218 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1219 * @phba: Pointer to HBA context object.
1220 * @pring: Pointer to driver SLI ring object.
1221 * @piocb: Pointer to the driver iocb object.
1223 * This function is called with hbalock held. The function adds the
1224 * new iocb to txcmplq of the given ring. This function always returns
1225 * 0. If this function is called for ELS ring, this function checks if
1226 * there is a vport associated with the ELS command. This function also
1227 * starts els_tmofunc timer if this is an ELS command.
1229 static int
1230 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231 struct lpfc_iocbq *piocb)
1233 list_add_tail(&piocb->list, &pring->txcmplq);
1234 piocb->iocb_flag |= LPFC_IO_ON_Q;
1235 pring->txcmplq_cnt++;
1236 if (pring->txcmplq_cnt > pring->txcmplq_max)
1237 pring->txcmplq_max = pring->txcmplq_cnt;
1239 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242 if (!piocb->vport)
1243 BUG();
1244 else
1245 mod_timer(&piocb->vport->els_tmofunc,
1246 jiffies + HZ * (phba->fc_ratov << 1));
1250 return 0;
1254 * lpfc_sli_ringtx_get - Get first element of the txq
1255 * @phba: Pointer to HBA context object.
1256 * @pring: Pointer to driver SLI ring object.
1258 * This function is called with hbalock held to get next
1259 * iocb in txq of the given ring. If there is any iocb in
1260 * the txq, the function returns first iocb in the list after
1261 * removing the iocb from the list, else it returns NULL.
1263 struct lpfc_iocbq *
1264 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1266 struct lpfc_iocbq *cmd_iocb;
1268 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269 if (cmd_iocb != NULL)
1270 pring->txq_cnt--;
1271 return cmd_iocb;
1275 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1276 * @phba: Pointer to HBA context object.
1277 * @pring: Pointer to driver SLI ring object.
1279 * This function is called with hbalock held and the caller must post the
1280 * iocb without releasing the lock. If the caller releases the lock,
1281 * iocb slot returned by the function is not guaranteed to be available.
1282 * The function returns pointer to the next available iocb slot if there
1283 * is available slot in the ring, else it returns NULL.
1284 * If the get index of the ring is ahead of the put index, the function
1285 * will post an error attention event to the worker thread to take the
1286 * HBA to offline state.
1288 static IOCB_t *
1289 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1291 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1292 uint32_t max_cmd_idx = pring->numCiocb;
1293 if ((pring->next_cmdidx == pring->cmdidx) &&
1294 (++pring->next_cmdidx >= max_cmd_idx))
1295 pring->next_cmdidx = 0;
1297 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1299 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1301 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1303 "0315 Ring %d issue: portCmdGet %d "
1304 "is bigger than cmd ring %d\n",
1305 pring->ringno,
1306 pring->local_getidx, max_cmd_idx);
1308 phba->link_state = LPFC_HBA_ERROR;
1310 * All error attention handlers are posted to
1311 * worker thread
1313 phba->work_ha |= HA_ERATT;
1314 phba->work_hs = HS_FFER3;
1316 lpfc_worker_wake_up(phba);
1318 return NULL;
1321 if (pring->local_getidx == pring->next_cmdidx)
1322 return NULL;
1325 return lpfc_cmd_iocb(phba, pring);
1329 * lpfc_sli_next_iotag - Get an iotag for the iocb
1330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1333 * This function gets an iotag for the iocb. If there is no unused iotag and
1334 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335 * array and assigns a new iotag.
1336 * The function returns the allocated iotag if successful, else returns zero.
1337 * Zero is not a valid iotag.
1338 * The caller is not required to hold any lock.
1340 uint16_t
1341 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1343 struct lpfc_iocbq **new_arr;
1344 struct lpfc_iocbq **old_arr;
1345 size_t new_len;
1346 struct lpfc_sli *psli = &phba->sli;
1347 uint16_t iotag;
1349 spin_lock_irq(&phba->hbalock);
1350 iotag = psli->last_iotag;
1351 if(++iotag < psli->iocbq_lookup_len) {
1352 psli->last_iotag = iotag;
1353 psli->iocbq_lookup[iotag] = iocbq;
1354 spin_unlock_irq(&phba->hbalock);
1355 iocbq->iotag = iotag;
1356 return iotag;
1357 } else if (psli->iocbq_lookup_len < (0xffff
1358 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1360 spin_unlock_irq(&phba->hbalock);
1361 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1362 GFP_KERNEL);
1363 if (new_arr) {
1364 spin_lock_irq(&phba->hbalock);
1365 old_arr = psli->iocbq_lookup;
1366 if (new_len <= psli->iocbq_lookup_len) {
1367 /* highly unprobable case */
1368 kfree(new_arr);
1369 iotag = psli->last_iotag;
1370 if(++iotag < psli->iocbq_lookup_len) {
1371 psli->last_iotag = iotag;
1372 psli->iocbq_lookup[iotag] = iocbq;
1373 spin_unlock_irq(&phba->hbalock);
1374 iocbq->iotag = iotag;
1375 return iotag;
1377 spin_unlock_irq(&phba->hbalock);
1378 return 0;
1380 if (psli->iocbq_lookup)
1381 memcpy(new_arr, old_arr,
1382 ((psli->last_iotag + 1) *
1383 sizeof (struct lpfc_iocbq *)));
1384 psli->iocbq_lookup = new_arr;
1385 psli->iocbq_lookup_len = new_len;
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
1388 spin_unlock_irq(&phba->hbalock);
1389 iocbq->iotag = iotag;
1390 kfree(old_arr);
1391 return iotag;
1393 } else
1394 spin_unlock_irq(&phba->hbalock);
1396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1397 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398 psli->last_iotag);
1400 return 0;
1404 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 * @iocb: Pointer to iocb slot in the ring.
1408 * @nextiocb: Pointer to driver iocb object which need to be
1409 * posted to firmware.
1411 * This function is called with hbalock held to post a new iocb to
1412 * the firmware. This function copies the new iocb to ring iocb slot and
1413 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414 * a completion call back for this iocb else the function will free the
1415 * iocb object.
1417 static void
1418 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1422 * Set up an iotag
1424 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1427 if (pring->ringno == LPFC_ELS_RING) {
1428 lpfc_debugfs_slow_ring_trc(phba,
1429 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1430 *(((uint32_t *) &nextiocb->iocb) + 4),
1431 *(((uint32_t *) &nextiocb->iocb) + 6),
1432 *(((uint32_t *) &nextiocb->iocb) + 7));
1436 * Issue iocb command to adapter
1438 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1439 wmb();
1440 pring->stats.iocb_cmd++;
1443 * If there is no completion routine to call, we can release the
1444 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1447 if (nextiocb->iocb_cmpl)
1448 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1449 else
1450 __lpfc_sli_release_iocbq(phba, nextiocb);
1453 * Let the HBA know what IOCB slot will be the next one the
1454 * driver will put a command into.
1456 pring->cmdidx = pring->next_cmdidx;
1457 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1461 * lpfc_sli_update_full_ring - Update the chip attention register
1462 * @phba: Pointer to HBA context object.
1463 * @pring: Pointer to driver SLI ring object.
1465 * The caller is not required to hold any lock for calling this function.
1466 * This function updates the chip attention bits for the ring to inform firmware
1467 * that there are pending work to be done for this ring and requests an
1468 * interrupt when there is space available in the ring. This function is
1469 * called when the driver is unable to post more iocbs to the ring due
1470 * to unavailability of space in the ring.
1472 static void
1473 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1475 int ringno = pring->ringno;
1477 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1479 wmb();
1482 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483 * The HBA will tell us when an IOCB entry is available.
1485 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486 readl(phba->CAregaddr); /* flush */
1488 pring->stats.iocb_cmd_full++;
1492 * lpfc_sli_update_ring - Update chip attention register
1493 * @phba: Pointer to HBA context object.
1494 * @pring: Pointer to driver SLI ring object.
1496 * This function updates the chip attention register bit for the
1497 * given ring to inform HBA that there is more work to be done
1498 * in this ring. The caller is not required to hold any lock.
1500 static void
1501 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1503 int ringno = pring->ringno;
1506 * Tell the HBA that there is work to do in this ring.
1508 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509 wmb();
1510 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511 readl(phba->CAregaddr); /* flush */
1516 * lpfc_sli_resume_iocb - Process iocbs in the txq
1517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1520 * This function is called with hbalock held to post pending iocbs
1521 * in the txq to the firmware. This function is called when driver
1522 * detects space available in the ring.
1524 static void
1525 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1527 IOCB_t *iocb;
1528 struct lpfc_iocbq *nextiocb;
1531 * Check to see if:
1532 * (a) there is anything on the txq to send
1533 * (b) link is up
1534 * (c) link attention events can be processed (fcp ring only)
1535 * (d) IOCB processing is not blocked by the outstanding mbox command.
1537 if (pring->txq_cnt &&
1538 lpfc_is_link_up(phba) &&
1539 (pring->ringno != phba->sli.fcp_ring ||
1540 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1542 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1546 if (iocb)
1547 lpfc_sli_update_ring(phba, pring);
1548 else
1549 lpfc_sli_update_full_ring(phba, pring);
1552 return;
1556 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1557 * @phba: Pointer to HBA context object.
1558 * @hbqno: HBQ number.
1560 * This function is called with hbalock held to get the next
1561 * available slot for the given HBQ. If there is free slot
1562 * available for the HBQ it will return pointer to the next available
1563 * HBQ entry else it will return NULL.
1565 static struct lpfc_hbq_entry *
1566 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1568 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1570 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572 hbqp->next_hbqPutIdx = 0;
1574 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1575 uint32_t raw_index = phba->hbq_get[hbqno];
1576 uint32_t getidx = le32_to_cpu(raw_index);
1578 hbqp->local_hbqGetIdx = getidx;
1580 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581 lpfc_printf_log(phba, KERN_ERR,
1582 LOG_SLI | LOG_VPORT,
1583 "1802 HBQ %d: local_hbqGetIdx "
1584 "%u is > than hbqp->entry_count %u\n",
1585 hbqno, hbqp->local_hbqGetIdx,
1586 hbqp->entry_count);
1588 phba->link_state = LPFC_HBA_ERROR;
1589 return NULL;
1592 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593 return NULL;
1596 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597 hbqp->hbqPutIdx;
1601 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1602 * @phba: Pointer to HBA context object.
1604 * This function is called with no lock held to free all the
1605 * hbq buffers while uninitializing the SLI interface. It also
1606 * frees the HBQ buffers returned by the firmware but not yet
1607 * processed by the upper layers.
1609 void
1610 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1612 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613 struct hbq_dmabuf *hbq_buf;
1614 unsigned long flags;
1615 int i, hbq_count;
1616 uint32_t hbqno;
1618 hbq_count = lpfc_sli_hbq_count();
1619 /* Return all memory used by all HBQs */
1620 spin_lock_irqsave(&phba->hbalock, flags);
1621 for (i = 0; i < hbq_count; ++i) {
1622 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623 &phba->hbqs[i].hbq_buffer_list, list) {
1624 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625 list_del(&hbq_buf->dbuf.list);
1626 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1628 phba->hbqs[i].buffer_count = 0;
1630 /* Return all HBQ buffer that are in-fly */
1631 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632 list) {
1633 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634 list_del(&hbq_buf->dbuf.list);
1635 if (hbq_buf->tag == -1) {
1636 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637 (phba, hbq_buf);
1638 } else {
1639 hbqno = hbq_buf->tag >> 16;
1640 if (hbqno >= LPFC_MAX_HBQS)
1641 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642 (phba, hbq_buf);
1643 else
1644 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645 hbq_buf);
1649 /* Mark the HBQs not in use */
1650 phba->hbq_in_use = 0;
1651 spin_unlock_irqrestore(&phba->hbalock, flags);
1655 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 * @hbq_buf: Pointer to HBQ buffer.
1660 * This function is called with the hbalock held to post a
1661 * hbq buffer to the firmware. If the function finds an empty
1662 * slot in the HBQ, it will post the buffer. The function will return
1663 * pointer to the hbq entry if it successfully post the buffer
1664 * else it will return NULL.
1666 static int
1667 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1668 struct hbq_dmabuf *hbq_buf)
1670 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1674 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675 * @phba: Pointer to HBA context object.
1676 * @hbqno: HBQ number.
1677 * @hbq_buf: Pointer to HBQ buffer.
1679 * This function is called with the hbalock held to post a hbq buffer to the
1680 * firmware. If the function finds an empty slot in the HBQ, it will post the
1681 * buffer and place it on the hbq_buffer_list. The function will return zero if
1682 * it successfully post the buffer else it will return an error.
1684 static int
1685 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686 struct hbq_dmabuf *hbq_buf)
1688 struct lpfc_hbq_entry *hbqe;
1689 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1691 /* Get next HBQ entry slot to use */
1692 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693 if (hbqe) {
1694 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1696 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1698 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1699 hbqe->bde.tus.f.bdeFlags = 0;
1700 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702 /* Sync SLIM */
1703 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1705 /* flush */
1706 readl(phba->hbq_put + hbqno);
1707 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1708 return 0;
1709 } else
1710 return -ENOMEM;
1714 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715 * @phba: Pointer to HBA context object.
1716 * @hbqno: HBQ number.
1717 * @hbq_buf: Pointer to HBQ buffer.
1719 * This function is called with the hbalock held to post an RQE to the SLI4
1720 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721 * the hbq_buffer_list and return zero, otherwise it will return an error.
1723 static int
1724 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725 struct hbq_dmabuf *hbq_buf)
1727 int rc;
1728 struct lpfc_rqe hrqe;
1729 struct lpfc_rqe drqe;
1731 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736 &hrqe, &drqe);
1737 if (rc < 0)
1738 return rc;
1739 hbq_buf->tag = rc;
1740 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741 return 0;
1744 /* HBQ for ELS and CT traffic. */
1745 static struct lpfc_hbq_init lpfc_els_hbq = {
1746 .rn = 1,
1747 .entry_count = 256,
1748 .mask_count = 0,
1749 .profile = 0,
1750 .ring_mask = (1 << LPFC_ELS_RING),
1751 .buffer_count = 0,
1752 .init_count = 40,
1753 .add_count = 40,
1756 /* HBQ for the extra ring if needed */
1757 static struct lpfc_hbq_init lpfc_extra_hbq = {
1758 .rn = 1,
1759 .entry_count = 200,
1760 .mask_count = 0,
1761 .profile = 0,
1762 .ring_mask = (1 << LPFC_EXTRA_RING),
1763 .buffer_count = 0,
1764 .init_count = 0,
1765 .add_count = 5,
1768 /* Array of HBQs */
1769 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1770 &lpfc_els_hbq,
1771 &lpfc_extra_hbq,
1775 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @count: Number of HBQ buffers to be posted.
1780 * This function is called with no lock held to post more hbq buffers to the
1781 * given HBQ. The function returns the number of HBQ buffers successfully
1782 * posted.
1784 static int
1785 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1787 uint32_t i, posted = 0;
1788 unsigned long flags;
1789 struct hbq_dmabuf *hbq_buffer;
1790 LIST_HEAD(hbq_buf_list);
1791 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1792 return 0;
1794 if ((phba->hbqs[hbqno].buffer_count + count) >
1795 lpfc_hbq_defs[hbqno]->entry_count)
1796 count = lpfc_hbq_defs[hbqno]->entry_count -
1797 phba->hbqs[hbqno].buffer_count;
1798 if (!count)
1799 return 0;
1800 /* Allocate HBQ entries */
1801 for (i = 0; i < count; i++) {
1802 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803 if (!hbq_buffer)
1804 break;
1805 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1807 /* Check whether HBQ is still in use */
1808 spin_lock_irqsave(&phba->hbalock, flags);
1809 if (!phba->hbq_in_use)
1810 goto err;
1811 while (!list_empty(&hbq_buf_list)) {
1812 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813 dbuf.list);
1814 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815 (hbqno << 16));
1816 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1817 phba->hbqs[hbqno].buffer_count++;
1818 posted++;
1819 } else
1820 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1822 spin_unlock_irqrestore(&phba->hbalock, flags);
1823 return posted;
1824 err:
1825 spin_unlock_irqrestore(&phba->hbalock, flags);
1826 while (!list_empty(&hbq_buf_list)) {
1827 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828 dbuf.list);
1829 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1831 return 0;
1835 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1836 * @phba: Pointer to HBA context object.
1837 * @qno: HBQ number.
1839 * This function posts more buffers to the HBQ. This function
1840 * is called with no lock held. The function returns the number of HBQ entries
1841 * successfully allocated.
1844 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1846 if (phba->sli_rev == LPFC_SLI_REV4)
1847 return 0;
1848 else
1849 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850 lpfc_hbq_defs[qno]->add_count);
1854 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1855 * @phba: Pointer to HBA context object.
1856 * @qno: HBQ queue number.
1858 * This function is called from SLI initialization code path with
1859 * no lock held to post initial HBQ buffers to firmware. The
1860 * function returns the number of HBQ entries successfully allocated.
1862 static int
1863 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1865 if (phba->sli_rev == LPFC_SLI_REV4)
1866 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1867 lpfc_hbq_defs[qno]->entry_count);
1868 else
1869 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870 lpfc_hbq_defs[qno]->init_count);
1874 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875 * @phba: Pointer to HBA context object.
1876 * @hbqno: HBQ number.
1878 * This function removes the first hbq buffer on an hbq list and returns a
1879 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1881 static struct hbq_dmabuf *
1882 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1884 struct lpfc_dmabuf *d_buf;
1886 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887 if (!d_buf)
1888 return NULL;
1889 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1893 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1894 * @phba: Pointer to HBA context object.
1895 * @tag: Tag of the hbq buffer.
1897 * This function is called with hbalock held. This function searches
1898 * for the hbq buffer associated with the given tag in the hbq buffer
1899 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900 * it returns NULL.
1902 static struct hbq_dmabuf *
1903 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1905 struct lpfc_dmabuf *d_buf;
1906 struct hbq_dmabuf *hbq_buf;
1907 uint32_t hbqno;
1909 hbqno = tag >> 16;
1910 if (hbqno >= LPFC_MAX_HBQS)
1911 return NULL;
1913 spin_lock_irq(&phba->hbalock);
1914 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1915 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1916 if (hbq_buf->tag == tag) {
1917 spin_unlock_irq(&phba->hbalock);
1918 return hbq_buf;
1921 spin_unlock_irq(&phba->hbalock);
1922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1923 "1803 Bad hbq tag. Data: x%x x%x\n",
1924 tag, phba->hbqs[tag >> 16].buffer_count);
1925 return NULL;
1929 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1930 * @phba: Pointer to HBA context object.
1931 * @hbq_buffer: Pointer to HBQ buffer.
1933 * This function is called with hbalock. This function gives back
1934 * the hbq buffer to firmware. If the HBQ does not have space to
1935 * post the buffer, it will free the buffer.
1937 void
1938 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1940 uint32_t hbqno;
1942 if (hbq_buffer) {
1943 hbqno = hbq_buffer->tag >> 16;
1944 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1945 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1950 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1951 * @mbxCommand: mailbox command code.
1953 * This function is called by the mailbox event handler function to verify
1954 * that the completed mailbox command is a legitimate mailbox command. If the
1955 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956 * and the mailbox event handler will take the HBA offline.
1958 static int
1959 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1961 uint8_t ret;
1963 switch (mbxCommand) {
1964 case MBX_LOAD_SM:
1965 case MBX_READ_NV:
1966 case MBX_WRITE_NV:
1967 case MBX_WRITE_VPARMS:
1968 case MBX_RUN_BIU_DIAG:
1969 case MBX_INIT_LINK:
1970 case MBX_DOWN_LINK:
1971 case MBX_CONFIG_LINK:
1972 case MBX_CONFIG_RING:
1973 case MBX_RESET_RING:
1974 case MBX_READ_CONFIG:
1975 case MBX_READ_RCONFIG:
1976 case MBX_READ_SPARM:
1977 case MBX_READ_STATUS:
1978 case MBX_READ_RPI:
1979 case MBX_READ_XRI:
1980 case MBX_READ_REV:
1981 case MBX_READ_LNK_STAT:
1982 case MBX_REG_LOGIN:
1983 case MBX_UNREG_LOGIN:
1984 case MBX_CLEAR_LA:
1985 case MBX_DUMP_MEMORY:
1986 case MBX_DUMP_CONTEXT:
1987 case MBX_RUN_DIAGS:
1988 case MBX_RESTART:
1989 case MBX_UPDATE_CFG:
1990 case MBX_DOWN_LOAD:
1991 case MBX_DEL_LD_ENTRY:
1992 case MBX_RUN_PROGRAM:
1993 case MBX_SET_MASK:
1994 case MBX_SET_VARIABLE:
1995 case MBX_UNREG_D_ID:
1996 case MBX_KILL_BOARD:
1997 case MBX_CONFIG_FARP:
1998 case MBX_BEACON:
1999 case MBX_LOAD_AREA:
2000 case MBX_RUN_BIU_DIAG64:
2001 case MBX_CONFIG_PORT:
2002 case MBX_READ_SPARM64:
2003 case MBX_READ_RPI64:
2004 case MBX_REG_LOGIN64:
2005 case MBX_READ_TOPOLOGY:
2006 case MBX_WRITE_WWN:
2007 case MBX_SET_DEBUG:
2008 case MBX_LOAD_EXP_ROM:
2009 case MBX_ASYNCEVT_ENABLE:
2010 case MBX_REG_VPI:
2011 case MBX_UNREG_VPI:
2012 case MBX_HEARTBEAT:
2013 case MBX_PORT_CAPABILITIES:
2014 case MBX_PORT_IOV_CONTROL:
2015 case MBX_SLI4_CONFIG:
2016 case MBX_SLI4_REQ_FTRS:
2017 case MBX_REG_FCFI:
2018 case MBX_UNREG_FCFI:
2019 case MBX_REG_VFI:
2020 case MBX_UNREG_VFI:
2021 case MBX_INIT_VPI:
2022 case MBX_INIT_VFI:
2023 case MBX_RESUME_RPI:
2024 case MBX_READ_EVENT_LOG_STATUS:
2025 case MBX_READ_EVENT_LOG:
2026 case MBX_SECURITY_MGMT:
2027 case MBX_AUTH_PORT:
2028 ret = mbxCommand;
2029 break;
2030 default:
2031 ret = MBX_SHUTDOWN;
2032 break;
2034 return ret;
2038 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2039 * @phba: Pointer to HBA context object.
2040 * @pmboxq: Pointer to mailbox command.
2042 * This is completion handler function for mailbox commands issued from
2043 * lpfc_sli_issue_mbox_wait function. This function is called by the
2044 * mailbox event handler function with no lock held. This function
2045 * will wake up thread waiting on the wait queue pointed by context1
2046 * of the mailbox.
2048 void
2049 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2051 wait_queue_head_t *pdone_q;
2052 unsigned long drvr_flag;
2055 * If pdone_q is empty, the driver thread gave up waiting and
2056 * continued running.
2058 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2060 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061 if (pdone_q)
2062 wake_up_interruptible(pdone_q);
2063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2064 return;
2069 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2070 * @phba: Pointer to HBA context object.
2071 * @pmb: Pointer to mailbox object.
2073 * This function is the default mailbox completion handler. It
2074 * frees the memory resources associated with the completed mailbox
2075 * command. If the completed command is a REG_LOGIN mailbox command,
2076 * this function will issue a UREG_LOGIN to re-claim the RPI.
2078 void
2079 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2081 struct lpfc_vport *vport = pmb->vport;
2082 struct lpfc_dmabuf *mp;
2083 struct lpfc_nodelist *ndlp;
2084 struct Scsi_Host *shost;
2085 uint16_t rpi, vpi;
2086 int rc;
2088 mp = (struct lpfc_dmabuf *) (pmb->context1);
2090 if (mp) {
2091 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092 kfree(mp);
2096 * If a REG_LOGIN succeeded after node is destroyed or node
2097 * is in re-discovery driver need to cleanup the RPI.
2099 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2100 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101 !pmb->u.mb.mbxStatus) {
2102 rpi = pmb->u.mb.un.varWords[0];
2103 vpi = pmb->u.mb.un.varRegLogin.vpi;
2104 lpfc_unreg_login(phba, vpi, rpi, pmb);
2105 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107 if (rc != MBX_NOT_FINISHED)
2108 return;
2111 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112 !(phba->pport->load_flag & FC_UNLOADING) &&
2113 !pmb->u.mb.mbxStatus) {
2114 shost = lpfc_shost_from_vport(vport);
2115 spin_lock_irq(shost->host_lock);
2116 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118 spin_unlock_irq(shost->host_lock);
2121 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123 lpfc_nlp_put(ndlp);
2124 pmb->context2 = NULL;
2127 /* Check security permission status on INIT_LINK mailbox command */
2128 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131 "2860 SLI authentication is required "
2132 "for INIT_LINK but has not done yet\n");
2134 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136 else
2137 mempool_free(pmb, phba->mbox_mem_pool);
2141 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2142 * @phba: Pointer to HBA context object.
2144 * This function is called with no lock held. This function processes all
2145 * the completed mailbox commands and gives it to upper layers. The interrupt
2146 * service routine processes mailbox completion interrupt and adds completed
2147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150 * function returns the mailbox commands to the upper layer by calling the
2151 * completion handler function of each mailbox.
2154 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2156 MAILBOX_t *pmbox;
2157 LPFC_MBOXQ_t *pmb;
2158 int rc;
2159 LIST_HEAD(cmplq);
2161 phba->sli.slistat.mbox_event++;
2163 /* Get all completed mailboxe buffers into the cmplq */
2164 spin_lock_irq(&phba->hbalock);
2165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166 spin_unlock_irq(&phba->hbalock);
2168 /* Get a Mailbox buffer to setup mailbox commands for callback */
2169 do {
2170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171 if (pmb == NULL)
2172 break;
2174 pmbox = &pmb->u.mb;
2176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177 if (pmb->vport) {
2178 lpfc_debugfs_disc_trc(pmb->vport,
2179 LPFC_DISC_TRC_MBOX_VPORT,
2180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181 (uint32_t)pmbox->mbxCommand,
2182 pmbox->un.varWords[0],
2183 pmbox->un.varWords[1]);
2185 else {
2186 lpfc_debugfs_disc_trc(phba->pport,
2187 LPFC_DISC_TRC_MBOX,
2188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2189 (uint32_t)pmbox->mbxCommand,
2190 pmbox->un.varWords[0],
2191 pmbox->un.varWords[1]);
2196 * It is a fatal error if unknown mbox command completion.
2198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199 MBX_SHUTDOWN) {
2200 /* Unknown mailbox command compl */
2201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2202 "(%d):0323 Unknown Mailbox command "
2203 "x%x (x%x/x%x) Cmpl\n",
2204 pmb->vport ? pmb->vport->vpi : 0,
2205 pmbox->mbxCommand,
2206 lpfc_sli_config_mbox_subsys_get(phba,
2207 pmb),
2208 lpfc_sli_config_mbox_opcode_get(phba,
2209 pmb));
2210 phba->link_state = LPFC_HBA_ERROR;
2211 phba->work_hs = HS_FFER3;
2212 lpfc_handle_eratt(phba);
2213 continue;
2216 if (pmbox->mbxStatus) {
2217 phba->sli.slistat.mbox_stat_err++;
2218 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2219 /* Mbox cmd cmpl error - RETRYing */
2220 lpfc_printf_log(phba, KERN_INFO,
2221 LOG_MBOX | LOG_SLI,
2222 "(%d):0305 Mbox cmd cmpl "
2223 "error - RETRYing Data: x%x "
2224 "(x%x/x%x) x%x x%x x%x\n",
2225 pmb->vport ? pmb->vport->vpi : 0,
2226 pmbox->mbxCommand,
2227 lpfc_sli_config_mbox_subsys_get(phba,
2228 pmb),
2229 lpfc_sli_config_mbox_opcode_get(phba,
2230 pmb),
2231 pmbox->mbxStatus,
2232 pmbox->un.varWords[0],
2233 pmb->vport->port_state);
2234 pmbox->mbxStatus = 0;
2235 pmbox->mbxOwner = OWN_HOST;
2236 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2237 if (rc != MBX_NOT_FINISHED)
2238 continue;
2242 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2243 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2244 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2245 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2246 pmb->vport ? pmb->vport->vpi : 0,
2247 pmbox->mbxCommand,
2248 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2250 pmb->mbox_cmpl,
2251 *((uint32_t *) pmbox),
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1],
2254 pmbox->un.varWords[2],
2255 pmbox->un.varWords[3],
2256 pmbox->un.varWords[4],
2257 pmbox->un.varWords[5],
2258 pmbox->un.varWords[6],
2259 pmbox->un.varWords[7]);
2261 if (pmb->mbox_cmpl)
2262 pmb->mbox_cmpl(phba,pmb);
2263 } while (1);
2264 return 0;
2268 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 * @tag: buffer tag.
2273 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2274 * is set in the tag the buffer is posted for a particular exchange,
2275 * the function will return the buffer without replacing the buffer.
2276 * If the buffer is for unsolicited ELS or CT traffic, this function
2277 * returns the buffer and also posts another buffer to the firmware.
2279 static struct lpfc_dmabuf *
2280 lpfc_sli_get_buff(struct lpfc_hba *phba,
2281 struct lpfc_sli_ring *pring,
2282 uint32_t tag)
2284 struct hbq_dmabuf *hbq_entry;
2286 if (tag & QUE_BUFTAG_BIT)
2287 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2288 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2289 if (!hbq_entry)
2290 return NULL;
2291 return &hbq_entry->dbuf;
2295 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2296 * @phba: Pointer to HBA context object.
2297 * @pring: Pointer to driver SLI ring object.
2298 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2299 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2300 * @fch_type: the type for the first frame of the sequence.
2302 * This function is called with no lock held. This function uses the r_ctl and
2303 * type of the received sequence to find the correct callback function to call
2304 * to process the sequence.
2306 static int
2307 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2308 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2309 uint32_t fch_type)
2311 int i;
2313 /* unSolicited Responses */
2314 if (pring->prt[0].profile) {
2315 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2316 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2317 saveq);
2318 return 1;
2320 /* We must search, based on rctl / type
2321 for the right routine */
2322 for (i = 0; i < pring->num_mask; i++) {
2323 if ((pring->prt[i].rctl == fch_r_ctl) &&
2324 (pring->prt[i].type == fch_type)) {
2325 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2326 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2327 (phba, pring, saveq);
2328 return 1;
2331 return 0;
2335 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2336 * @phba: Pointer to HBA context object.
2337 * @pring: Pointer to driver SLI ring object.
2338 * @saveq: Pointer to the unsolicited iocb.
2340 * This function is called with no lock held by the ring event handler
2341 * when there is an unsolicited iocb posted to the response ring by the
2342 * firmware. This function gets the buffer associated with the iocbs
2343 * and calls the event handler for the ring. This function handles both
2344 * qring buffers and hbq buffers.
2345 * When the function returns 1 the caller can free the iocb object otherwise
2346 * upper layer functions will free the iocb objects.
2348 static int
2349 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2350 struct lpfc_iocbq *saveq)
2352 IOCB_t * irsp;
2353 WORD5 * w5p;
2354 uint32_t Rctl, Type;
2355 uint32_t match;
2356 struct lpfc_iocbq *iocbq;
2357 struct lpfc_dmabuf *dmzbuf;
2359 match = 0;
2360 irsp = &(saveq->iocb);
2362 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2363 if (pring->lpfc_sli_rcv_async_status)
2364 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2365 else
2366 lpfc_printf_log(phba,
2367 KERN_WARNING,
2368 LOG_SLI,
2369 "0316 Ring %d handler: unexpected "
2370 "ASYNC_STATUS iocb received evt_code "
2371 "0x%x\n",
2372 pring->ringno,
2373 irsp->un.asyncstat.evt_code);
2374 return 1;
2377 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2378 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2379 if (irsp->ulpBdeCount > 0) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->un.ulpWord[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2385 if (irsp->ulpBdeCount > 1) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[3]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2391 if (irsp->ulpBdeCount > 2) {
2392 dmzbuf = lpfc_sli_get_buff(phba, pring,
2393 irsp->unsli3.sli3Words[7]);
2394 lpfc_in_buf_free(phba, dmzbuf);
2397 return 1;
2400 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2401 if (irsp->ulpBdeCount != 0) {
2402 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2403 irsp->un.ulpWord[3]);
2404 if (!saveq->context2)
2405 lpfc_printf_log(phba,
2406 KERN_ERR,
2407 LOG_SLI,
2408 "0341 Ring %d Cannot find buffer for "
2409 "an unsolicited iocb. tag 0x%x\n",
2410 pring->ringno,
2411 irsp->un.ulpWord[3]);
2413 if (irsp->ulpBdeCount == 2) {
2414 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2415 irsp->unsli3.sli3Words[7]);
2416 if (!saveq->context3)
2417 lpfc_printf_log(phba,
2418 KERN_ERR,
2419 LOG_SLI,
2420 "0342 Ring %d Cannot find buffer for an"
2421 " unsolicited iocb. tag 0x%x\n",
2422 pring->ringno,
2423 irsp->unsli3.sli3Words[7]);
2425 list_for_each_entry(iocbq, &saveq->list, list) {
2426 irsp = &(iocbq->iocb);
2427 if (irsp->ulpBdeCount != 0) {
2428 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
2430 if (!iocbq->context2)
2431 lpfc_printf_log(phba,
2432 KERN_ERR,
2433 LOG_SLI,
2434 "0343 Ring %d Cannot find "
2435 "buffer for an unsolicited iocb"
2436 ". tag 0x%x\n", pring->ringno,
2437 irsp->un.ulpWord[3]);
2439 if (irsp->ulpBdeCount == 2) {
2440 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2441 irsp->unsli3.sli3Words[7]);
2442 if (!iocbq->context3)
2443 lpfc_printf_log(phba,
2444 KERN_ERR,
2445 LOG_SLI,
2446 "0344 Ring %d Cannot find "
2447 "buffer for an unsolicited "
2448 "iocb. tag 0x%x\n",
2449 pring->ringno,
2450 irsp->unsli3.sli3Words[7]);
2454 if (irsp->ulpBdeCount != 0 &&
2455 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2456 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2457 int found = 0;
2459 /* search continue save q for same XRI */
2460 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2461 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2462 saveq->iocb.unsli3.rcvsli3.ox_id) {
2463 list_add_tail(&saveq->list, &iocbq->list);
2464 found = 1;
2465 break;
2468 if (!found)
2469 list_add_tail(&saveq->clist,
2470 &pring->iocb_continue_saveq);
2471 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2472 list_del_init(&iocbq->clist);
2473 saveq = iocbq;
2474 irsp = &(saveq->iocb);
2475 } else
2476 return 0;
2478 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2479 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2480 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2481 Rctl = FC_RCTL_ELS_REQ;
2482 Type = FC_TYPE_ELS;
2483 } else {
2484 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2485 Rctl = w5p->hcsw.Rctl;
2486 Type = w5p->hcsw.Type;
2488 /* Firmware Workaround */
2489 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2490 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2491 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2492 Rctl = FC_RCTL_ELS_REQ;
2493 Type = FC_TYPE_ELS;
2494 w5p->hcsw.Rctl = Rctl;
2495 w5p->hcsw.Type = Type;
2499 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2500 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2501 "0313 Ring %d handler: unexpected Rctl x%x "
2502 "Type x%x received\n",
2503 pring->ringno, Rctl, Type);
2505 return 1;
2509 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2510 * @phba: Pointer to HBA context object.
2511 * @pring: Pointer to driver SLI ring object.
2512 * @prspiocb: Pointer to response iocb object.
2514 * This function looks up the iocb_lookup table to get the command iocb
2515 * corresponding to the given response iocb using the iotag of the
2516 * response iocb. This function is called with the hbalock held.
2517 * This function returns the command iocb object if it finds the command
2518 * iocb else returns NULL.
2520 static struct lpfc_iocbq *
2521 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2522 struct lpfc_sli_ring *pring,
2523 struct lpfc_iocbq *prspiocb)
2525 struct lpfc_iocbq *cmd_iocb = NULL;
2526 uint16_t iotag;
2528 iotag = prspiocb->iocb.ulpIoTag;
2530 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2531 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2532 list_del_init(&cmd_iocb->list);
2533 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2534 pring->txcmplq_cnt--;
2535 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2537 return cmd_iocb;
2540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2541 "0317 iotag x%x is out off "
2542 "range: max iotag x%x wd0 x%x\n",
2543 iotag, phba->sli.last_iotag,
2544 *(((uint32_t *) &prspiocb->iocb) + 7));
2545 return NULL;
2549 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @iotag: IOCB tag.
2554 * This function looks up the iocb_lookup table to get the command iocb
2555 * corresponding to the given iotag. This function is called with the
2556 * hbalock held.
2557 * This function returns the command iocb object if it finds the command
2558 * iocb else returns NULL.
2560 static struct lpfc_iocbq *
2561 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2562 struct lpfc_sli_ring *pring, uint16_t iotag)
2564 struct lpfc_iocbq *cmd_iocb;
2566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2568 list_del_init(&cmd_iocb->list);
2569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2571 pring->txcmplq_cnt--;
2573 return cmd_iocb;
2576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2577 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2578 iotag, phba->sli.last_iotag);
2579 return NULL;
2583 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @saveq: Pointer to the response iocb to be processed.
2588 * This function is called by the ring event handler for non-fcp
2589 * rings when there is a new response iocb in the response ring.
2590 * The caller is not required to hold any locks. This function
2591 * gets the command iocb associated with the response iocb and
2592 * calls the completion handler for the command iocb. If there
2593 * is no completion handler, the function will free the resources
2594 * associated with command iocb. If the response iocb is for
2595 * an already aborted command iocb, the status of the completion
2596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2597 * This function always returns 1.
2599 static int
2600 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2601 struct lpfc_iocbq *saveq)
2603 struct lpfc_iocbq *cmdiocbp;
2604 int rc = 1;
2605 unsigned long iflag;
2607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2608 spin_lock_irqsave(&phba->hbalock, iflag);
2609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2612 if (cmdiocbp) {
2613 if (cmdiocbp->iocb_cmpl) {
2615 * If an ELS command failed send an event to mgmt
2616 * application.
2618 if (saveq->iocb.ulpStatus &&
2619 (pring->ringno == LPFC_ELS_RING) &&
2620 (cmdiocbp->iocb.ulpCommand ==
2621 CMD_ELS_REQUEST64_CR))
2622 lpfc_send_els_failure_event(phba,
2623 cmdiocbp, saveq);
2626 * Post all ELS completions to the worker thread.
2627 * All other are passed to the completion callback.
2629 if (pring->ringno == LPFC_ELS_RING) {
2630 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2631 (cmdiocbp->iocb_flag &
2632 LPFC_DRIVER_ABORTED)) {
2633 spin_lock_irqsave(&phba->hbalock,
2634 iflag);
2635 cmdiocbp->iocb_flag &=
2636 ~LPFC_DRIVER_ABORTED;
2637 spin_unlock_irqrestore(&phba->hbalock,
2638 iflag);
2639 saveq->iocb.ulpStatus =
2640 IOSTAT_LOCAL_REJECT;
2641 saveq->iocb.un.ulpWord[4] =
2642 IOERR_SLI_ABORTED;
2644 /* Firmware could still be in progress
2645 * of DMAing payload, so don't free data
2646 * buffer till after a hbeat.
2648 spin_lock_irqsave(&phba->hbalock,
2649 iflag);
2650 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (saveq->iocb_flag &
2656 LPFC_EXCHANGE_BUSY) {
2657 /* Set cmdiocb flag for the
2658 * exchange busy so sgl (xri)
2659 * will not be released until
2660 * the abort xri is received
2661 * from hba.
2663 spin_lock_irqsave(
2664 &phba->hbalock, iflag);
2665 cmdiocbp->iocb_flag |=
2666 LPFC_EXCHANGE_BUSY;
2667 spin_unlock_irqrestore(
2668 &phba->hbalock, iflag);
2670 if (cmdiocbp->iocb_flag &
2671 LPFC_DRIVER_ABORTED) {
2673 * Clear LPFC_DRIVER_ABORTED
2674 * bit in case it was driver
2675 * initiated abort.
2677 spin_lock_irqsave(
2678 &phba->hbalock, iflag);
2679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
2681 spin_unlock_irqrestore(
2682 &phba->hbalock, iflag);
2683 cmdiocbp->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 cmdiocbp->iocb.un.ulpWord[4] =
2686 IOERR_ABORT_REQUESTED;
2688 * For SLI4, irsiocb contains
2689 * NO_XRI in sli_xritag, it
2690 * shall not affect releasing
2691 * sgl (xri) process.
2693 saveq->iocb.ulpStatus =
2694 IOSTAT_LOCAL_REJECT;
2695 saveq->iocb.un.ulpWord[4] =
2696 IOERR_SLI_ABORTED;
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 saveq->iocb_flag |=
2700 LPFC_DELAY_MEM_FREE;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2706 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2707 } else
2708 lpfc_sli_release_iocbq(phba, cmdiocbp);
2709 } else {
2711 * Unknown initiating command based on the response iotag.
2712 * This could be the case on the ELS ring because of
2713 * lpfc_els_abort().
2715 if (pring->ringno != LPFC_ELS_RING) {
2717 * Ring <ringno> handler: unexpected completion IoTag
2718 * <IoTag>
2720 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2721 "0322 Ring %d handler: "
2722 "unexpected completion IoTag x%x "
2723 "Data: x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 saveq->iocb.ulpIoTag,
2726 saveq->iocb.ulpStatus,
2727 saveq->iocb.un.ulpWord[4],
2728 saveq->iocb.ulpCommand,
2729 saveq->iocb.ulpContext);
2733 return rc;
2737 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2738 * @phba: Pointer to HBA context object.
2739 * @pring: Pointer to driver SLI ring object.
2741 * This function is called from the iocb ring event handlers when
2742 * put pointer is ahead of the get pointer for a ring. This function signal
2743 * an error attention condition to the worker thread and the worker
2744 * thread will transition the HBA to offline state.
2746 static void
2747 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2749 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2752 * rsp ring <portRspMax>
2754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2755 "0312 Ring %d handler: portRspPut %d "
2756 "is bigger than rsp ring %d\n",
2757 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2758 pring->numRiocb);
2760 phba->link_state = LPFC_HBA_ERROR;
2763 * All error attention handlers are posted to
2764 * worker thread
2766 phba->work_ha |= HA_ERATT;
2767 phba->work_hs = HS_FFER3;
2769 lpfc_worker_wake_up(phba);
2771 return;
2775 * lpfc_poll_eratt - Error attention polling timer timeout handler
2776 * @ptr: Pointer to address of HBA context object.
2778 * This function is invoked by the Error Attention polling timer when the
2779 * timer times out. It will check the SLI Error Attention register for
2780 * possible attention events. If so, it will post an Error Attention event
2781 * and wake up worker thread to process it. Otherwise, it will set up the
2782 * Error Attention polling timer for the next poll.
2784 void lpfc_poll_eratt(unsigned long ptr)
2786 struct lpfc_hba *phba;
2787 uint32_t eratt = 0;
2789 phba = (struct lpfc_hba *)ptr;
2791 /* Check chip HA register for error event */
2792 eratt = lpfc_sli_check_eratt(phba);
2794 if (eratt)
2795 /* Tell the worker thread there is work to do */
2796 lpfc_worker_wake_up(phba);
2797 else
2798 /* Restart the timer for next eratt poll */
2799 mod_timer(&phba->eratt_poll, jiffies +
2800 HZ * LPFC_ERATT_POLL_INTERVAL);
2801 return;
2806 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @mask: Host attention register mask for this ring.
2811 * This function is called from the interrupt context when there is a ring
2812 * event for the fcp ring. The caller does not hold any lock.
2813 * The function processes each response iocb in the response ring until it
2814 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2815 * LE bit set. The function will call the completion handler of the command iocb
2816 * if the response iocb indicates a completion for a command iocb or it is
2817 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2818 * function if this is an unsolicited iocb.
2819 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2820 * to check it explicitly.
2823 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2824 struct lpfc_sli_ring *pring, uint32_t mask)
2826 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2827 IOCB_t *irsp = NULL;
2828 IOCB_t *entry = NULL;
2829 struct lpfc_iocbq *cmdiocbq = NULL;
2830 struct lpfc_iocbq rspiocbq;
2831 uint32_t status;
2832 uint32_t portRspPut, portRspMax;
2833 int rc = 1;
2834 lpfc_iocb_type type;
2835 unsigned long iflag;
2836 uint32_t rsp_cmpl = 0;
2838 spin_lock_irqsave(&phba->hbalock, iflag);
2839 pring->stats.iocb_event++;
2842 * The next available response entry should never exceed the maximum
2843 * entries. If it does, treat it as an adapter hardware error.
2845 portRspMax = pring->numRiocb;
2846 portRspPut = le32_to_cpu(pgp->rspPutInx);
2847 if (unlikely(portRspPut >= portRspMax)) {
2848 lpfc_sli_rsp_pointers_error(phba, pring);
2849 spin_unlock_irqrestore(&phba->hbalock, iflag);
2850 return 1;
2852 if (phba->fcp_ring_in_use) {
2853 spin_unlock_irqrestore(&phba->hbalock, iflag);
2854 return 1;
2855 } else
2856 phba->fcp_ring_in_use = 1;
2858 rmb();
2859 while (pring->rspidx != portRspPut) {
2861 * Fetch an entry off the ring and copy it into a local data
2862 * structure. The copy involves a byte-swap since the
2863 * network byte order and pci byte orders are different.
2865 entry = lpfc_resp_iocb(phba, pring);
2866 phba->last_completion_time = jiffies;
2868 if (++pring->rspidx >= portRspMax)
2869 pring->rspidx = 0;
2871 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2872 (uint32_t *) &rspiocbq.iocb,
2873 phba->iocb_rsp_size);
2874 INIT_LIST_HEAD(&(rspiocbq.list));
2875 irsp = &rspiocbq.iocb;
2877 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2878 pring->stats.iocb_rsp++;
2879 rsp_cmpl++;
2881 if (unlikely(irsp->ulpStatus)) {
2883 * If resource errors reported from HBA, reduce
2884 * queuedepths of the SCSI device.
2886 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2887 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2888 spin_unlock_irqrestore(&phba->hbalock, iflag);
2889 phba->lpfc_rampdown_queue_depth(phba);
2890 spin_lock_irqsave(&phba->hbalock, iflag);
2893 /* Rsp ring <ringno> error: IOCB */
2894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2895 "0336 Rsp Ring %d error: IOCB Data: "
2896 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2897 pring->ringno,
2898 irsp->un.ulpWord[0],
2899 irsp->un.ulpWord[1],
2900 irsp->un.ulpWord[2],
2901 irsp->un.ulpWord[3],
2902 irsp->un.ulpWord[4],
2903 irsp->un.ulpWord[5],
2904 *(uint32_t *)&irsp->un1,
2905 *((uint32_t *)&irsp->un1 + 1));
2908 switch (type) {
2909 case LPFC_ABORT_IOCB:
2910 case LPFC_SOL_IOCB:
2912 * Idle exchange closed via ABTS from port. No iocb
2913 * resources need to be recovered.
2915 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2917 "0333 IOCB cmd 0x%x"
2918 " processed. Skipping"
2919 " completion\n",
2920 irsp->ulpCommand);
2921 break;
2924 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2925 &rspiocbq);
2926 if (unlikely(!cmdiocbq))
2927 break;
2928 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2929 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2930 if (cmdiocbq->iocb_cmpl) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2933 &rspiocbq);
2934 spin_lock_irqsave(&phba->hbalock, iflag);
2936 break;
2937 case LPFC_UNSOL_IOCB:
2938 spin_unlock_irqrestore(&phba->hbalock, iflag);
2939 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2940 spin_lock_irqsave(&phba->hbalock, iflag);
2941 break;
2942 default:
2943 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2944 char adaptermsg[LPFC_MAX_ADPTMSG];
2945 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2946 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2947 MAX_MSG_DATA);
2948 dev_warn(&((phba->pcidev)->dev),
2949 "lpfc%d: %s\n",
2950 phba->brd_no, adaptermsg);
2951 } else {
2952 /* Unknown IOCB command */
2953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2954 "0334 Unknown IOCB command "
2955 "Data: x%x, x%x x%x x%x x%x\n",
2956 type, irsp->ulpCommand,
2957 irsp->ulpStatus,
2958 irsp->ulpIoTag,
2959 irsp->ulpContext);
2961 break;
2965 * The response IOCB has been processed. Update the ring
2966 * pointer in SLIM. If the port response put pointer has not
2967 * been updated, sync the pgp->rspPutInx and fetch the new port
2968 * response put pointer.
2970 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2972 if (pring->rspidx == portRspPut)
2973 portRspPut = le32_to_cpu(pgp->rspPutInx);
2976 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2977 pring->stats.iocb_rsp_full++;
2978 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2979 writel(status, phba->CAregaddr);
2980 readl(phba->CAregaddr);
2982 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2983 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2984 pring->stats.iocb_cmd_empty++;
2986 /* Force update of the local copy of cmdGetInx */
2987 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2988 lpfc_sli_resume_iocb(phba, pring);
2990 if ((pring->lpfc_sli_cmd_available))
2991 (pring->lpfc_sli_cmd_available) (phba, pring);
2995 phba->fcp_ring_in_use = 0;
2996 spin_unlock_irqrestore(&phba->hbalock, iflag);
2997 return rc;
3001 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3002 * @phba: Pointer to HBA context object.
3003 * @pring: Pointer to driver SLI ring object.
3004 * @rspiocbp: Pointer to driver response IOCB object.
3006 * This function is called from the worker thread when there is a slow-path
3007 * response IOCB to process. This function chains all the response iocbs until
3008 * seeing the iocb with the LE bit set. The function will call
3009 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3010 * completion of a command iocb. The function will call the
3011 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3012 * The function frees the resources or calls the completion handler if this
3013 * iocb is an abort completion. The function returns NULL when the response
3014 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3015 * this function shall chain the iocb on to the iocb_continueq and return the
3016 * response iocb passed in.
3018 static struct lpfc_iocbq *
3019 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3020 struct lpfc_iocbq *rspiocbp)
3022 struct lpfc_iocbq *saveq;
3023 struct lpfc_iocbq *cmdiocbp;
3024 struct lpfc_iocbq *next_iocb;
3025 IOCB_t *irsp = NULL;
3026 uint32_t free_saveq;
3027 uint8_t iocb_cmd_type;
3028 lpfc_iocb_type type;
3029 unsigned long iflag;
3030 int rc;
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 /* First add the response iocb to the countinueq list */
3034 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3035 pring->iocb_continueq_cnt++;
3037 /* Now, determine whether the list is completed for processing */
3038 irsp = &rspiocbp->iocb;
3039 if (irsp->ulpLe) {
3041 * By default, the driver expects to free all resources
3042 * associated with this iocb completion.
3044 free_saveq = 1;
3045 saveq = list_get_first(&pring->iocb_continueq,
3046 struct lpfc_iocbq, list);
3047 irsp = &(saveq->iocb);
3048 list_del_init(&pring->iocb_continueq);
3049 pring->iocb_continueq_cnt = 0;
3051 pring->stats.iocb_rsp++;
3054 * If resource errors reported from HBA, reduce
3055 * queuedepths of the SCSI device.
3057 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3058 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3064 if (irsp->ulpStatus) {
3065 /* Rsp ring <ringno> error: IOCB */
3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3067 "0328 Rsp Ring %d error: "
3068 "IOCB Data: "
3069 "x%x x%x x%x x%x "
3070 "x%x x%x x%x x%x "
3071 "x%x x%x x%x x%x "
3072 "x%x x%x x%x x%x\n",
3073 pring->ringno,
3074 irsp->un.ulpWord[0],
3075 irsp->un.ulpWord[1],
3076 irsp->un.ulpWord[2],
3077 irsp->un.ulpWord[3],
3078 irsp->un.ulpWord[4],
3079 irsp->un.ulpWord[5],
3080 *(((uint32_t *) irsp) + 6),
3081 *(((uint32_t *) irsp) + 7),
3082 *(((uint32_t *) irsp) + 8),
3083 *(((uint32_t *) irsp) + 9),
3084 *(((uint32_t *) irsp) + 10),
3085 *(((uint32_t *) irsp) + 11),
3086 *(((uint32_t *) irsp) + 12),
3087 *(((uint32_t *) irsp) + 13),
3088 *(((uint32_t *) irsp) + 14),
3089 *(((uint32_t *) irsp) + 15));
3093 * Fetch the IOCB command type and call the correct completion
3094 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3095 * get freed back to the lpfc_iocb_list by the discovery
3096 * kernel thread.
3098 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3099 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3100 switch (type) {
3101 case LPFC_SOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 break;
3107 case LPFC_UNSOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 if (!rc)
3112 free_saveq = 0;
3113 break;
3115 case LPFC_ABORT_IOCB:
3116 cmdiocbp = NULL;
3117 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3118 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3119 saveq);
3120 if (cmdiocbp) {
3121 /* Call the specified completion routine */
3122 if (cmdiocbp->iocb_cmpl) {
3123 spin_unlock_irqrestore(&phba->hbalock,
3124 iflag);
3125 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3126 saveq);
3127 spin_lock_irqsave(&phba->hbalock,
3128 iflag);
3129 } else
3130 __lpfc_sli_release_iocbq(phba,
3131 cmdiocbp);
3133 break;
3135 case LPFC_UNKNOWN_IOCB:
3136 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3137 char adaptermsg[LPFC_MAX_ADPTMSG];
3138 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3139 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3140 MAX_MSG_DATA);
3141 dev_warn(&((phba->pcidev)->dev),
3142 "lpfc%d: %s\n",
3143 phba->brd_no, adaptermsg);
3144 } else {
3145 /* Unknown IOCB command */
3146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147 "0335 Unknown IOCB "
3148 "command Data: x%x "
3149 "x%x x%x x%x\n",
3150 irsp->ulpCommand,
3151 irsp->ulpStatus,
3152 irsp->ulpIoTag,
3153 irsp->ulpContext);
3155 break;
3158 if (free_saveq) {
3159 list_for_each_entry_safe(rspiocbp, next_iocb,
3160 &saveq->list, list) {
3161 list_del(&rspiocbp->list);
3162 __lpfc_sli_release_iocbq(phba, rspiocbp);
3164 __lpfc_sli_release_iocbq(phba, saveq);
3166 rspiocbp = NULL;
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rspiocbp;
3173 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @mask: Host attention register mask for this ring.
3178 * This routine wraps the actual slow_ring event process routine from the
3179 * API jump table function pointer from the lpfc_hba struct.
3181 void
3182 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3183 struct lpfc_sli_ring *pring, uint32_t mask)
3185 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3189 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3190 * @phba: Pointer to HBA context object.
3191 * @pring: Pointer to driver SLI ring object.
3192 * @mask: Host attention register mask for this ring.
3194 * This function is called from the worker thread when there is a ring event
3195 * for non-fcp rings. The caller does not hold any lock. The function will
3196 * remove each response iocb in the response ring and calls the handle
3197 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3199 static void
3200 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3201 struct lpfc_sli_ring *pring, uint32_t mask)
3203 struct lpfc_pgp *pgp;
3204 IOCB_t *entry;
3205 IOCB_t *irsp = NULL;
3206 struct lpfc_iocbq *rspiocbp = NULL;
3207 uint32_t portRspPut, portRspMax;
3208 unsigned long iflag;
3209 uint32_t status;
3211 pgp = &phba->port_gp[pring->ringno];
3212 spin_lock_irqsave(&phba->hbalock, iflag);
3213 pring->stats.iocb_event++;
3216 * The next available response entry should never exceed the maximum
3217 * entries. If it does, treat it as an adapter hardware error.
3219 portRspMax = pring->numRiocb;
3220 portRspPut = le32_to_cpu(pgp->rspPutInx);
3221 if (portRspPut >= portRspMax) {
3223 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3224 * rsp ring <portRspMax>
3226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3227 "0303 Ring %d handler: portRspPut %d "
3228 "is bigger than rsp ring %d\n",
3229 pring->ringno, portRspPut, portRspMax);
3231 phba->link_state = LPFC_HBA_ERROR;
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
3234 phba->work_hs = HS_FFER3;
3235 lpfc_handle_eratt(phba);
3237 return;
3240 rmb();
3241 while (pring->rspidx != portRspPut) {
3243 * Build a completion list and call the appropriate handler.
3244 * The process is to get the next available response iocb, get
3245 * a free iocb from the list, copy the response data into the
3246 * free iocb, insert to the continuation list, and update the
3247 * next response index to slim. This process makes response
3248 * iocb's in the ring available to DMA as fast as possible but
3249 * pays a penalty for a copy operation. Since the iocb is
3250 * only 32 bytes, this penalty is considered small relative to
3251 * the PCI reads for register values and a slim write. When
3252 * the ulpLe field is set, the entire Command has been
3253 * received.
3255 entry = lpfc_resp_iocb(phba, pring);
3257 phba->last_completion_time = jiffies;
3258 rspiocbp = __lpfc_sli_get_iocbq(phba);
3259 if (rspiocbp == NULL) {
3260 printk(KERN_ERR "%s: out of buffers! Failing "
3261 "completion.\n", __func__);
3262 break;
3265 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3266 phba->iocb_rsp_size);
3267 irsp = &rspiocbp->iocb;
3269 if (++pring->rspidx >= portRspMax)
3270 pring->rspidx = 0;
3272 if (pring->ringno == LPFC_ELS_RING) {
3273 lpfc_debugfs_slow_ring_trc(phba,
3274 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3275 *(((uint32_t *) irsp) + 4),
3276 *(((uint32_t *) irsp) + 6),
3277 *(((uint32_t *) irsp) + 7));
3280 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
3282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 /* Handle the response IOCB */
3284 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3285 spin_lock_irqsave(&phba->hbalock, iflag);
3288 * If the port response put pointer has not been updated, sync
3289 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3290 * response put pointer.
3292 if (pring->rspidx == portRspPut) {
3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3295 } /* while (pring->rspidx != portRspPut) */
3297 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3298 /* At least one response entry has been freed */
3299 pring->stats.iocb_rsp_full++;
3300 /* SET RxRE_RSP in Chip Att register */
3301 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3302 writel(status, phba->CAregaddr);
3303 readl(phba->CAregaddr); /* flush */
3305 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3306 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3307 pring->stats.iocb_cmd_empty++;
3309 /* Force update of the local copy of cmdGetInx */
3310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3311 lpfc_sli_resume_iocb(phba, pring);
3313 if ((pring->lpfc_sli_cmd_available))
3314 (pring->lpfc_sli_cmd_available) (phba, pring);
3318 spin_unlock_irqrestore(&phba->hbalock, iflag);
3319 return;
3323 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3324 * @phba: Pointer to HBA context object.
3325 * @pring: Pointer to driver SLI ring object.
3326 * @mask: Host attention register mask for this ring.
3328 * This function is called from the worker thread when there is a pending
3329 * ELS response iocb on the driver internal slow-path response iocb worker
3330 * queue. The caller does not hold any lock. The function will remove each
3331 * response iocb from the response worker queue and calls the handle
3332 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3334 static void
3335 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3336 struct lpfc_sli_ring *pring, uint32_t mask)
3338 struct lpfc_iocbq *irspiocbq;
3339 struct hbq_dmabuf *dmabuf;
3340 struct lpfc_cq_event *cq_event;
3341 unsigned long iflag;
3343 spin_lock_irqsave(&phba->hbalock, iflag);
3344 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3347 /* Get the response iocb from the head of work queue */
3348 spin_lock_irqsave(&phba->hbalock, iflag);
3349 list_remove_head(&phba->sli4_hba.sp_queue_event,
3350 cq_event, struct lpfc_cq_event, list);
3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
3353 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3354 case CQE_CODE_COMPL_WQE:
3355 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3356 cq_event);
3357 /* Translate ELS WCQE to response IOCBQ */
3358 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3359 irspiocbq);
3360 if (irspiocbq)
3361 lpfc_sli_sp_handle_rspiocb(phba, pring,
3362 irspiocbq);
3363 break;
3364 case CQE_CODE_RECEIVE:
3365 case CQE_CODE_RECEIVE_V1:
3366 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3367 cq_event);
3368 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3369 break;
3370 default:
3371 break;
3377 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3378 * @phba: Pointer to HBA context object.
3379 * @pring: Pointer to driver SLI ring object.
3381 * This function aborts all iocbs in the given ring and frees all the iocb
3382 * objects in txq. This function issues an abort iocb for all the iocb commands
3383 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3384 * the return of this function. The caller is not required to hold any locks.
3386 void
3387 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3389 LIST_HEAD(completions);
3390 struct lpfc_iocbq *iocb, *next_iocb;
3392 if (pring->ringno == LPFC_ELS_RING) {
3393 lpfc_fabric_abort_hba(phba);
3396 /* Error everything on txq and txcmplq
3397 * First do the txq.
3399 spin_lock_irq(&phba->hbalock);
3400 list_splice_init(&pring->txq, &completions);
3401 pring->txq_cnt = 0;
3403 /* Next issue ABTS for everything on the txcmplq */
3404 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3405 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3407 spin_unlock_irq(&phba->hbalock);
3409 /* Cancel all the IOCBs from the completions list */
3410 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3411 IOERR_SLI_ABORTED);
3415 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3416 * @phba: Pointer to HBA context object.
3418 * This function flushes all iocbs in the fcp ring and frees all the iocb
3419 * objects in txq and txcmplq. This function will not issue abort iocbs
3420 * for all the iocb commands in txcmplq, they will just be returned with
3421 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3422 * slot has been permanently disabled.
3424 void
3425 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3427 LIST_HEAD(txq);
3428 LIST_HEAD(txcmplq);
3429 struct lpfc_sli *psli = &phba->sli;
3430 struct lpfc_sli_ring *pring;
3432 /* Currently, only one fcp ring */
3433 pring = &psli->ring[psli->fcp_ring];
3435 spin_lock_irq(&phba->hbalock);
3436 /* Retrieve everything on txq */
3437 list_splice_init(&pring->txq, &txq);
3438 pring->txq_cnt = 0;
3440 /* Retrieve everything on the txcmplq */
3441 list_splice_init(&pring->txcmplq, &txcmplq);
3442 pring->txcmplq_cnt = 0;
3443 spin_unlock_irq(&phba->hbalock);
3445 /* Flush the txq */
3446 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3447 IOERR_SLI_DOWN);
3449 /* Flush the txcmpq */
3450 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3451 IOERR_SLI_DOWN);
3455 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3456 * @phba: Pointer to HBA context object.
3457 * @mask: Bit mask to be checked.
3459 * This function reads the host status register and compares
3460 * with the provided bit mask to check if HBA completed
3461 * the restart. This function will wait in a loop for the
3462 * HBA to complete restart. If the HBA does not restart within
3463 * 15 iterations, the function will reset the HBA again. The
3464 * function returns 1 when HBA fail to restart otherwise returns
3465 * zero.
3467 static int
3468 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3470 uint32_t status;
3471 int i = 0;
3472 int retval = 0;
3474 /* Read the HBA Host Status Register */
3475 if (lpfc_readl(phba->HSregaddr, &status))
3476 return 1;
3479 * Check status register every 100ms for 5 retries, then every
3480 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3481 * every 2.5 sec for 4.
3482 * Break our of the loop if errors occurred during init.
3484 while (((status & mask) != mask) &&
3485 !(status & HS_FFERM) &&
3486 i++ < 20) {
3488 if (i <= 5)
3489 msleep(10);
3490 else if (i <= 10)
3491 msleep(500);
3492 else
3493 msleep(2500);
3495 if (i == 15) {
3496 /* Do post */
3497 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3498 lpfc_sli_brdrestart(phba);
3500 /* Read the HBA Host Status Register */
3501 if (lpfc_readl(phba->HSregaddr, &status)) {
3502 retval = 1;
3503 break;
3507 /* Check to see if any errors occurred during init */
3508 if ((status & HS_FFERM) || (i >= 20)) {
3509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3510 "2751 Adapter failed to restart, "
3511 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3512 status,
3513 readl(phba->MBslimaddr + 0xa8),
3514 readl(phba->MBslimaddr + 0xac));
3515 phba->link_state = LPFC_HBA_ERROR;
3516 retval = 1;
3519 return retval;
3523 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3524 * @phba: Pointer to HBA context object.
3525 * @mask: Bit mask to be checked.
3527 * This function checks the host status register to check if HBA is
3528 * ready. This function will wait in a loop for the HBA to be ready
3529 * If the HBA is not ready , the function will will reset the HBA PCI
3530 * function again. The function returns 1 when HBA fail to be ready
3531 * otherwise returns zero.
3533 static int
3534 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3536 uint32_t status;
3537 int retval = 0;
3539 /* Read the HBA Host Status Register */
3540 status = lpfc_sli4_post_status_check(phba);
3542 if (status) {
3543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3544 lpfc_sli_brdrestart(phba);
3545 status = lpfc_sli4_post_status_check(phba);
3548 /* Check to see if any errors occurred during init */
3549 if (status) {
3550 phba->link_state = LPFC_HBA_ERROR;
3551 retval = 1;
3552 } else
3553 phba->sli4_hba.intr_enable = 0;
3555 return retval;
3559 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3560 * @phba: Pointer to HBA context object.
3561 * @mask: Bit mask to be checked.
3563 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3564 * from the API jump table function pointer from the lpfc_hba struct.
3567 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3569 return phba->lpfc_sli_brdready(phba, mask);
3572 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3575 * lpfc_reset_barrier - Make HBA ready for HBA reset
3576 * @phba: Pointer to HBA context object.
3578 * This function is called before resetting an HBA. This
3579 * function requests HBA to quiesce DMAs before a reset.
3581 void lpfc_reset_barrier(struct lpfc_hba *phba)
3583 uint32_t __iomem *resp_buf;
3584 uint32_t __iomem *mbox_buf;
3585 volatile uint32_t mbox;
3586 uint32_t hc_copy, ha_copy, resp_data;
3587 int i;
3588 uint8_t hdrtype;
3590 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3591 if (hdrtype != 0x80 ||
3592 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3593 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3594 return;
3597 * Tell the other part of the chip to suspend temporarily all
3598 * its DMA activity.
3600 resp_buf = phba->MBslimaddr;
3602 /* Disable the error attention */
3603 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3604 return;
3605 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
3607 phba->link_flag |= LS_IGNORE_ERATT;
3609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3610 return;
3611 if (ha_copy & HA_ERATT) {
3612 /* Clear Chip error bit */
3613 writel(HA_ERATT, phba->HAregaddr);
3614 phba->pport->stopped = 1;
3617 mbox = 0;
3618 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3619 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3621 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3622 mbox_buf = phba->MBslimaddr;
3623 writel(mbox, mbox_buf);
3625 for (i = 0; i < 50; i++) {
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN))
3629 mdelay(1);
3630 else
3631 break;
3633 resp_data = 0;
3634 if (lpfc_readl((resp_buf + 1), &resp_data))
3635 return;
3636 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3637 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3638 phba->pport->stopped)
3639 goto restore_hc;
3640 else
3641 goto clear_errat;
3644 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3645 resp_data = 0;
3646 for (i = 0; i < 500; i++) {
3647 if (lpfc_readl(resp_buf, &resp_data))
3648 return;
3649 if (resp_data != mbox)
3650 mdelay(1);
3651 else
3652 break;
3655 clear_errat:
3657 while (++i < 500) {
3658 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3659 return;
3660 if (!(ha_copy & HA_ERATT))
3661 mdelay(1);
3662 else
3663 break;
3666 if (readl(phba->HAregaddr) & HA_ERATT) {
3667 writel(HA_ERATT, phba->HAregaddr);
3668 phba->pport->stopped = 1;
3671 restore_hc:
3672 phba->link_flag &= ~LS_IGNORE_ERATT;
3673 writel(hc_copy, phba->HCregaddr);
3674 readl(phba->HCregaddr); /* flush */
3678 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3679 * @phba: Pointer to HBA context object.
3681 * This function issues a kill_board mailbox command and waits for
3682 * the error attention interrupt. This function is called for stopping
3683 * the firmware processing. The caller is not required to hold any
3684 * locks. This function calls lpfc_hba_down_post function to free
3685 * any pending commands after the kill. The function will return 1 when it
3686 * fails to kill the board else will return 0.
3689 lpfc_sli_brdkill(struct lpfc_hba *phba)
3691 struct lpfc_sli *psli;
3692 LPFC_MBOXQ_t *pmb;
3693 uint32_t status;
3694 uint32_t ha_copy;
3695 int retval;
3696 int i = 0;
3698 psli = &phba->sli;
3700 /* Kill HBA */
3701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3702 "0329 Kill HBA Data: x%x x%x\n",
3703 phba->pport->port_state, psli->sli_flag);
3705 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3706 if (!pmb)
3707 return 1;
3709 /* Disable the error attention */
3710 spin_lock_irq(&phba->hbalock);
3711 if (lpfc_readl(phba->HCregaddr, &status)) {
3712 spin_unlock_irq(&phba->hbalock);
3713 mempool_free(pmb, phba->mbox_mem_pool);
3714 return 1;
3716 status &= ~HC_ERINT_ENA;
3717 writel(status, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
3719 phba->link_flag |= LS_IGNORE_ERATT;
3720 spin_unlock_irq(&phba->hbalock);
3722 lpfc_kill_board(phba, pmb);
3723 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3724 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3726 if (retval != MBX_SUCCESS) {
3727 if (retval != MBX_BUSY)
3728 mempool_free(pmb, phba->mbox_mem_pool);
3729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3730 "2752 KILL_BOARD command failed retval %d\n",
3731 retval);
3732 spin_lock_irq(&phba->hbalock);
3733 phba->link_flag &= ~LS_IGNORE_ERATT;
3734 spin_unlock_irq(&phba->hbalock);
3735 return 1;
3738 spin_lock_irq(&phba->hbalock);
3739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3740 spin_unlock_irq(&phba->hbalock);
3742 mempool_free(pmb, phba->mbox_mem_pool);
3744 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3745 * attention every 100ms for 3 seconds. If we don't get ERATT after
3746 * 3 seconds we still set HBA_ERROR state because the status of the
3747 * board is now undefined.
3749 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3750 return 1;
3751 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3752 mdelay(100);
3753 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3754 return 1;
3757 del_timer_sync(&psli->mbox_tmo);
3758 if (ha_copy & HA_ERATT) {
3759 writel(HA_ERATT, phba->HAregaddr);
3760 phba->pport->stopped = 1;
3762 spin_lock_irq(&phba->hbalock);
3763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3764 psli->mbox_active = NULL;
3765 phba->link_flag &= ~LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
3768 lpfc_hba_down_post(phba);
3769 phba->link_state = LPFC_HBA_ERROR;
3771 return ha_copy & HA_ERATT ? 0 : 1;
3775 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3776 * @phba: Pointer to HBA context object.
3778 * This function resets the HBA by writing HC_INITFF to the control
3779 * register. After the HBA resets, this function resets all the iocb ring
3780 * indices. This function disables PCI layer parity checking during
3781 * the reset.
3782 * This function returns 0 always.
3783 * The caller is not required to hold any locks.
3786 lpfc_sli_brdreset(struct lpfc_hba *phba)
3788 struct lpfc_sli *psli;
3789 struct lpfc_sli_ring *pring;
3790 uint16_t cfg_value;
3791 int i;
3793 psli = &phba->sli;
3795 /* Reset HBA */
3796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3797 "0325 Reset HBA Data: x%x x%x\n",
3798 phba->pport->port_state, psli->sli_flag);
3800 /* perform board reset */
3801 phba->fc_eventTag = 0;
3802 phba->link_events = 0;
3803 phba->pport->fc_myDID = 0;
3804 phba->pport->fc_prevDID = 0;
3806 /* Turn off parity checking and serr during the physical reset */
3807 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3809 (cfg_value &
3810 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3812 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3814 /* Now toggle INITFF bit in the Host Control Register */
3815 writel(HC_INITFF, phba->HCregaddr);
3816 mdelay(1);
3817 readl(phba->HCregaddr); /* flush */
3818 writel(0, phba->HCregaddr);
3819 readl(phba->HCregaddr); /* flush */
3821 /* Restore PCI cmd register */
3822 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3824 /* Initialize relevant SLI info */
3825 for (i = 0; i < psli->num_rings; i++) {
3826 pring = &psli->ring[i];
3827 pring->flag = 0;
3828 pring->rspidx = 0;
3829 pring->next_cmdidx = 0;
3830 pring->local_getidx = 0;
3831 pring->cmdidx = 0;
3832 pring->missbufcnt = 0;
3835 phba->link_state = LPFC_WARM_START;
3836 return 0;
3840 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3841 * @phba: Pointer to HBA context object.
3843 * This function resets a SLI4 HBA. This function disables PCI layer parity
3844 * checking during resets the device. The caller is not required to hold
3845 * any locks.
3847 * This function returns 0 always.
3850 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3852 struct lpfc_sli *psli = &phba->sli;
3853 uint16_t cfg_value;
3854 uint8_t qindx;
3856 /* Reset HBA */
3857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3858 "0295 Reset HBA Data: x%x x%x\n",
3859 phba->pport->port_state, psli->sli_flag);
3861 /* perform board reset */
3862 phba->fc_eventTag = 0;
3863 phba->link_events = 0;
3864 phba->pport->fc_myDID = 0;
3865 phba->pport->fc_prevDID = 0;
3867 spin_lock_irq(&phba->hbalock);
3868 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3869 phba->fcf.fcf_flag = 0;
3870 /* Clean up the child queue list for the CQs */
3871 list_del_init(&phba->sli4_hba.mbx_wq->list);
3872 list_del_init(&phba->sli4_hba.els_wq->list);
3873 list_del_init(&phba->sli4_hba.hdr_rq->list);
3874 list_del_init(&phba->sli4_hba.dat_rq->list);
3875 list_del_init(&phba->sli4_hba.mbx_cq->list);
3876 list_del_init(&phba->sli4_hba.els_cq->list);
3877 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3878 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3879 qindx = 0;
3881 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3882 while (++qindx < phba->cfg_fcp_eq_count);
3883 spin_unlock_irq(&phba->hbalock);
3885 /* Now physically reset the device */
3886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3887 "0389 Performing PCI function reset!\n");
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3892 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3894 /* Perform FCoE PCI function reset */
3895 lpfc_pci_function_reset(phba);
3897 /* Restore PCI cmd register */
3898 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3900 return 0;
3904 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3905 * @phba: Pointer to HBA context object.
3907 * This function is called in the SLI initialization code path to
3908 * restart the HBA. The caller is not required to hold any lock.
3909 * This function writes MBX_RESTART mailbox command to the SLIM and
3910 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3911 * function to free any pending commands. The function enables
3912 * POST only during the first initialization. The function returns zero.
3913 * The function does not guarantee completion of MBX_RESTART mailbox
3914 * command before the return of this function.
3916 static int
3917 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3919 MAILBOX_t *mb;
3920 struct lpfc_sli *psli;
3921 volatile uint32_t word0;
3922 void __iomem *to_slim;
3923 uint32_t hba_aer_enabled;
3925 spin_lock_irq(&phba->hbalock);
3927 /* Take PCIe device Advanced Error Reporting (AER) state */
3928 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3930 psli = &phba->sli;
3932 /* Restart HBA */
3933 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3934 "0337 Restart HBA Data: x%x x%x\n",
3935 phba->pport->port_state, psli->sli_flag);
3937 word0 = 0;
3938 mb = (MAILBOX_t *) &word0;
3939 mb->mbxCommand = MBX_RESTART;
3940 mb->mbxHc = 1;
3942 lpfc_reset_barrier(phba);
3944 to_slim = phba->MBslimaddr;
3945 writel(*(uint32_t *) mb, to_slim);
3946 readl(to_slim); /* flush */
3948 /* Only skip post after fc_ffinit is completed */
3949 if (phba->pport->port_state)
3950 word0 = 1; /* This is really setting up word1 */
3951 else
3952 word0 = 0; /* This is really setting up word1 */
3953 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3954 writel(*(uint32_t *) mb, to_slim);
3955 readl(to_slim); /* flush */
3957 lpfc_sli_brdreset(phba);
3958 phba->pport->stopped = 0;
3959 phba->link_state = LPFC_INIT_START;
3960 phba->hba_flag = 0;
3961 spin_unlock_irq(&phba->hbalock);
3963 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3964 psli->stats_start = get_seconds();
3966 /* Give the INITFF and Post time to settle. */
3967 mdelay(100);
3969 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3970 if (hba_aer_enabled)
3971 pci_disable_pcie_error_reporting(phba->pcidev);
3973 lpfc_hba_down_post(phba);
3975 return 0;
3979 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3980 * @phba: Pointer to HBA context object.
3982 * This function is called in the SLI initialization code path to restart
3983 * a SLI4 HBA. The caller is not required to hold any lock.
3984 * At the end of the function, it calls lpfc_hba_down_post function to
3985 * free any pending commands.
3987 static int
3988 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3990 struct lpfc_sli *psli = &phba->sli;
3991 uint32_t hba_aer_enabled;
3993 /* Restart HBA */
3994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3995 "0296 Restart HBA Data: x%x x%x\n",
3996 phba->pport->port_state, psli->sli_flag);
3998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4001 lpfc_sli4_brdreset(phba);
4003 spin_lock_irq(&phba->hbalock);
4004 phba->pport->stopped = 0;
4005 phba->link_state = LPFC_INIT_START;
4006 phba->hba_flag = 0;
4007 spin_unlock_irq(&phba->hbalock);
4009 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4010 psli->stats_start = get_seconds();
4012 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4013 if (hba_aer_enabled)
4014 pci_disable_pcie_error_reporting(phba->pcidev);
4016 lpfc_hba_down_post(phba);
4018 return 0;
4022 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4023 * @phba: Pointer to HBA context object.
4025 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4026 * API jump table function pointer from the lpfc_hba struct.
4029 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4031 return phba->lpfc_sli_brdrestart(phba);
4035 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4036 * @phba: Pointer to HBA context object.
4038 * This function is called after a HBA restart to wait for successful
4039 * restart of the HBA. Successful restart of the HBA is indicated by
4040 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4041 * iteration, the function will restart the HBA again. The function returns
4042 * zero if HBA successfully restarted else returns negative error code.
4044 static int
4045 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4047 uint32_t status, i = 0;
4049 /* Read the HBA Host Status Register */
4050 if (lpfc_readl(phba->HSregaddr, &status))
4051 return -EIO;
4053 /* Check status register to see what current state is */
4054 i = 0;
4055 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4057 /* Check every 10ms for 10 retries, then every 100ms for 90
4058 * retries, then every 1 sec for 50 retires for a total of
4059 * ~60 seconds before reset the board again and check every
4060 * 1 sec for 50 retries. The up to 60 seconds before the
4061 * board ready is required by the Falcon FIPS zeroization
4062 * complete, and any reset the board in between shall cause
4063 * restart of zeroization, further delay the board ready.
4065 if (i++ >= 200) {
4066 /* Adapter failed to init, timeout, status reg
4067 <status> */
4068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4069 "0436 Adapter failed to init, "
4070 "timeout, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
4074 phba->link_state = LPFC_HBA_ERROR;
4075 return -ETIMEDOUT;
4078 /* Check to see if any errors occurred during init */
4079 if (status & HS_FFERM) {
4080 /* ERROR: During chipset initialization */
4081 /* Adapter failed to init, chipset, status reg
4082 <status> */
4083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4084 "0437 Adapter failed to init, "
4085 "chipset, status reg x%x, "
4086 "FW Data: A8 x%x AC x%x\n", status,
4087 readl(phba->MBslimaddr + 0xa8),
4088 readl(phba->MBslimaddr + 0xac));
4089 phba->link_state = LPFC_HBA_ERROR;
4090 return -EIO;
4093 if (i <= 10)
4094 msleep(10);
4095 else if (i <= 100)
4096 msleep(100);
4097 else
4098 msleep(1000);
4100 if (i == 150) {
4101 /* Do post */
4102 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4103 lpfc_sli_brdrestart(phba);
4105 /* Read the HBA Host Status Register */
4106 if (lpfc_readl(phba->HSregaddr, &status))
4107 return -EIO;
4110 /* Check to see if any errors occurred during init */
4111 if (status & HS_FFERM) {
4112 /* ERROR: During chipset initialization */
4113 /* Adapter failed to init, chipset, status reg <status> */
4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4115 "0438 Adapter failed to init, chipset, "
4116 "status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
4120 phba->link_state = LPFC_HBA_ERROR;
4121 return -EIO;
4124 /* Clear all interrupt enable conditions */
4125 writel(0, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
4128 /* setup host attn register */
4129 writel(0xffffffff, phba->HAregaddr);
4130 readl(phba->HAregaddr); /* flush */
4131 return 0;
4135 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4137 * This function calculates and returns the number of HBQs required to be
4138 * configured.
4141 lpfc_sli_hbq_count(void)
4143 return ARRAY_SIZE(lpfc_hbq_defs);
4147 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4149 * This function adds the number of hbq entries in every HBQ to get
4150 * the total number of hbq entries required for the HBA and returns
4151 * the total count.
4153 static int
4154 lpfc_sli_hbq_entry_count(void)
4156 int hbq_count = lpfc_sli_hbq_count();
4157 int count = 0;
4158 int i;
4160 for (i = 0; i < hbq_count; ++i)
4161 count += lpfc_hbq_defs[i]->entry_count;
4162 return count;
4166 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4168 * This function calculates amount of memory required for all hbq entries
4169 * to be configured and returns the total memory required.
4172 lpfc_sli_hbq_size(void)
4174 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4178 * lpfc_sli_hbq_setup - configure and initialize HBQs
4179 * @phba: Pointer to HBA context object.
4181 * This function is called during the SLI initialization to configure
4182 * all the HBQs and post buffers to the HBQ. The caller is not
4183 * required to hold any locks. This function will return zero if successful
4184 * else it will return negative error code.
4186 static int
4187 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4189 int hbq_count = lpfc_sli_hbq_count();
4190 LPFC_MBOXQ_t *pmb;
4191 MAILBOX_t *pmbox;
4192 uint32_t hbqno;
4193 uint32_t hbq_entry_index;
4195 /* Get a Mailbox buffer to setup mailbox
4196 * commands for HBA initialization
4198 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4200 if (!pmb)
4201 return -ENOMEM;
4203 pmbox = &pmb->u.mb;
4205 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4206 phba->link_state = LPFC_INIT_MBX_CMDS;
4207 phba->hbq_in_use = 1;
4209 hbq_entry_index = 0;
4210 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4211 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4212 phba->hbqs[hbqno].hbqPutIdx = 0;
4213 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4214 phba->hbqs[hbqno].entry_count =
4215 lpfc_hbq_defs[hbqno]->entry_count;
4216 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4217 hbq_entry_index, pmb);
4218 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4220 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4221 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4222 mbxStatus <status>, ring <num> */
4224 lpfc_printf_log(phba, KERN_ERR,
4225 LOG_SLI | LOG_VPORT,
4226 "1805 Adapter failed to init. "
4227 "Data: x%x x%x x%x\n",
4228 pmbox->mbxCommand,
4229 pmbox->mbxStatus, hbqno);
4231 phba->link_state = LPFC_HBA_ERROR;
4232 mempool_free(pmb, phba->mbox_mem_pool);
4233 return -ENXIO;
4236 phba->hbq_count = hbq_count;
4238 mempool_free(pmb, phba->mbox_mem_pool);
4240 /* Initially populate or replenish the HBQs */
4241 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4242 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4243 return 0;
4247 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4248 * @phba: Pointer to HBA context object.
4250 * This function is called during the SLI initialization to configure
4251 * all the HBQs and post buffers to the HBQ. The caller is not
4252 * required to hold any locks. This function will return zero if successful
4253 * else it will return negative error code.
4255 static int
4256 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4258 phba->hbq_in_use = 1;
4259 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4260 phba->hbq_count = 1;
4261 /* Initially populate or replenish the HBQs */
4262 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4263 return 0;
4267 * lpfc_sli_config_port - Issue config port mailbox command
4268 * @phba: Pointer to HBA context object.
4269 * @sli_mode: sli mode - 2/3
4271 * This function is called by the sli intialization code path
4272 * to issue config_port mailbox command. This function restarts the
4273 * HBA firmware and issues a config_port mailbox command to configure
4274 * the SLI interface in the sli mode specified by sli_mode
4275 * variable. The caller is not required to hold any locks.
4276 * The function returns 0 if successful, else returns negative error
4277 * code.
4280 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4282 LPFC_MBOXQ_t *pmb;
4283 uint32_t resetcount = 0, rc = 0, done = 0;
4285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!pmb) {
4287 phba->link_state = LPFC_HBA_ERROR;
4288 return -ENOMEM;
4291 phba->sli_rev = sli_mode;
4292 while (resetcount < 2 && !done) {
4293 spin_lock_irq(&phba->hbalock);
4294 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4295 spin_unlock_irq(&phba->hbalock);
4296 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4297 lpfc_sli_brdrestart(phba);
4298 rc = lpfc_sli_chipset_init(phba);
4299 if (rc)
4300 break;
4302 spin_lock_irq(&phba->hbalock);
4303 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4304 spin_unlock_irq(&phba->hbalock);
4305 resetcount++;
4307 /* Call pre CONFIG_PORT mailbox command initialization. A
4308 * value of 0 means the call was successful. Any other
4309 * nonzero value is a failure, but if ERESTART is returned,
4310 * the driver may reset the HBA and try again.
4312 rc = lpfc_config_port_prep(phba);
4313 if (rc == -ERESTART) {
4314 phba->link_state = LPFC_LINK_UNKNOWN;
4315 continue;
4316 } else if (rc)
4317 break;
4319 phba->link_state = LPFC_INIT_MBX_CMDS;
4320 lpfc_config_port(phba, pmb);
4321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4322 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4323 LPFC_SLI3_HBQ_ENABLED |
4324 LPFC_SLI3_CRP_ENABLED |
4325 LPFC_SLI3_BG_ENABLED |
4326 LPFC_SLI3_DSS_ENABLED);
4327 if (rc != MBX_SUCCESS) {
4328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4329 "0442 Adapter failed to init, mbxCmd x%x "
4330 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4331 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4332 spin_lock_irq(&phba->hbalock);
4333 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4334 spin_unlock_irq(&phba->hbalock);
4335 rc = -ENXIO;
4336 } else {
4337 /* Allow asynchronous mailbox command to go through */
4338 spin_lock_irq(&phba->hbalock);
4339 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4340 spin_unlock_irq(&phba->hbalock);
4341 done = 1;
4344 if (!done) {
4345 rc = -EINVAL;
4346 goto do_prep_failed;
4348 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4349 if (!pmb->u.mb.un.varCfgPort.cMA) {
4350 rc = -ENXIO;
4351 goto do_prep_failed;
4353 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4354 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4355 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4356 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4357 phba->max_vpi : phba->max_vports;
4359 } else
4360 phba->max_vpi = 0;
4361 phba->fips_level = 0;
4362 phba->fips_spec_rev = 0;
4363 if (pmb->u.mb.un.varCfgPort.gdss) {
4364 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4365 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4366 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4368 "2850 Security Crypto Active. FIPS x%d "
4369 "(Spec Rev: x%d)",
4370 phba->fips_level, phba->fips_spec_rev);
4372 if (pmb->u.mb.un.varCfgPort.sec_err) {
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "2856 Config Port Security Crypto "
4375 "Error: x%x ",
4376 pmb->u.mb.un.varCfgPort.sec_err);
4378 if (pmb->u.mb.un.varCfgPort.gerbm)
4379 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4380 if (pmb->u.mb.un.varCfgPort.gcrp)
4381 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4383 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4384 phba->port_gp = phba->mbox->us.s3_pgp.port;
4386 if (phba->cfg_enable_bg) {
4387 if (pmb->u.mb.un.varCfgPort.gbg)
4388 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4389 else
4390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4391 "0443 Adapter did not grant "
4392 "BlockGuard\n");
4394 } else {
4395 phba->hbq_get = NULL;
4396 phba->port_gp = phba->mbox->us.s2.port;
4397 phba->max_vpi = 0;
4399 do_prep_failed:
4400 mempool_free(pmb, phba->mbox_mem_pool);
4401 return rc;
4406 * lpfc_sli_hba_setup - SLI intialization function
4407 * @phba: Pointer to HBA context object.
4409 * This function is the main SLI intialization function. This function
4410 * is called by the HBA intialization code, HBA reset code and HBA
4411 * error attention handler code. Caller is not required to hold any
4412 * locks. This function issues config_port mailbox command to configure
4413 * the SLI, setup iocb rings and HBQ rings. In the end the function
4414 * calls the config_port_post function to issue init_link mailbox
4415 * command and to start the discovery. The function will return zero
4416 * if successful, else it will return negative error code.
4419 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4421 uint32_t rc;
4422 int mode = 3, i;
4423 int longs;
4425 switch (lpfc_sli_mode) {
4426 case 2:
4427 if (phba->cfg_enable_npiv) {
4428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4429 "1824 NPIV enabled: Override lpfc_sli_mode "
4430 "parameter (%d) to auto (0).\n",
4431 lpfc_sli_mode);
4432 break;
4434 mode = 2;
4435 break;
4436 case 0:
4437 case 3:
4438 break;
4439 default:
4440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4441 "1819 Unrecognized lpfc_sli_mode "
4442 "parameter: %d.\n", lpfc_sli_mode);
4444 break;
4447 rc = lpfc_sli_config_port(phba, mode);
4449 if (rc && lpfc_sli_mode == 3)
4450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4451 "1820 Unable to select SLI-3. "
4452 "Not supported by adapter.\n");
4453 if (rc && mode != 2)
4454 rc = lpfc_sli_config_port(phba, 2);
4455 if (rc)
4456 goto lpfc_sli_hba_setup_error;
4458 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4459 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4460 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4461 if (!rc) {
4462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4463 "2709 This device supports "
4464 "Advanced Error Reporting (AER)\n");
4465 spin_lock_irq(&phba->hbalock);
4466 phba->hba_flag |= HBA_AER_ENABLED;
4467 spin_unlock_irq(&phba->hbalock);
4468 } else {
4469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4470 "2708 This device does not support "
4471 "Advanced Error Reporting (AER)\n");
4472 phba->cfg_aer_support = 0;
4476 if (phba->sli_rev == 3) {
4477 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4478 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4479 } else {
4480 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4481 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4482 phba->sli3_options = 0;
4485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4486 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4487 phba->sli_rev, phba->max_vpi);
4488 rc = lpfc_sli_ring_map(phba);
4490 if (rc)
4491 goto lpfc_sli_hba_setup_error;
4493 /* Initialize VPIs. */
4494 if (phba->sli_rev == LPFC_SLI_REV3) {
4496 * The VPI bitmask and physical ID array are allocated
4497 * and initialized once only - at driver load. A port
4498 * reset doesn't need to reinitialize this memory.
4500 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4501 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4502 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4503 GFP_KERNEL);
4504 if (!phba->vpi_bmask) {
4505 rc = -ENOMEM;
4506 goto lpfc_sli_hba_setup_error;
4509 phba->vpi_ids = kzalloc(
4510 (phba->max_vpi+1) * sizeof(uint16_t),
4511 GFP_KERNEL);
4512 if (!phba->vpi_ids) {
4513 kfree(phba->vpi_bmask);
4514 rc = -ENOMEM;
4515 goto lpfc_sli_hba_setup_error;
4517 for (i = 0; i < phba->max_vpi; i++)
4518 phba->vpi_ids[i] = i;
4522 /* Init HBQs */
4523 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4524 rc = lpfc_sli_hbq_setup(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4528 spin_lock_irq(&phba->hbalock);
4529 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4530 spin_unlock_irq(&phba->hbalock);
4532 rc = lpfc_config_port_post(phba);
4533 if (rc)
4534 goto lpfc_sli_hba_setup_error;
4536 return rc;
4538 lpfc_sli_hba_setup_error:
4539 phba->link_state = LPFC_HBA_ERROR;
4540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4541 "0445 Firmware initialization failed\n");
4542 return rc;
4546 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4547 * @phba: Pointer to HBA context object.
4548 * @mboxq: mailbox pointer.
4549 * This function issue a dump mailbox command to read config region
4550 * 23 and parse the records in the region and populate driver
4551 * data structure.
4553 static int
4554 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4555 LPFC_MBOXQ_t *mboxq)
4557 struct lpfc_dmabuf *mp;
4558 struct lpfc_mqe *mqe;
4559 uint32_t data_length;
4560 int rc;
4562 /* Program the default value of vlan_id and fc_map */
4563 phba->valid_vlan = 0;
4564 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4565 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4566 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4568 mqe = &mboxq->u.mqe;
4569 if (lpfc_dump_fcoe_param(phba, mboxq))
4570 return -ENOMEM;
4572 mp = (struct lpfc_dmabuf *) mboxq->context1;
4573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4576 "(%d):2571 Mailbox cmd x%x Status x%x "
4577 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4578 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4579 "CQ: x%x x%x x%x x%x\n",
4580 mboxq->vport ? mboxq->vport->vpi : 0,
4581 bf_get(lpfc_mqe_command, mqe),
4582 bf_get(lpfc_mqe_status, mqe),
4583 mqe->un.mb_words[0], mqe->un.mb_words[1],
4584 mqe->un.mb_words[2], mqe->un.mb_words[3],
4585 mqe->un.mb_words[4], mqe->un.mb_words[5],
4586 mqe->un.mb_words[6], mqe->un.mb_words[7],
4587 mqe->un.mb_words[8], mqe->un.mb_words[9],
4588 mqe->un.mb_words[10], mqe->un.mb_words[11],
4589 mqe->un.mb_words[12], mqe->un.mb_words[13],
4590 mqe->un.mb_words[14], mqe->un.mb_words[15],
4591 mqe->un.mb_words[16], mqe->un.mb_words[50],
4592 mboxq->mcqe.word0,
4593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4594 mboxq->mcqe.trailer);
4596 if (rc) {
4597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
4599 return -EIO;
4601 data_length = mqe->un.mb_words[5];
4602 if (data_length > DMP_RGN23_SIZE) {
4603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
4605 return -EIO;
4608 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4609 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4610 kfree(mp);
4611 return 0;
4615 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4616 * @phba: pointer to lpfc hba data structure.
4617 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4618 * @vpd: pointer to the memory to hold resulting port vpd data.
4619 * @vpd_size: On input, the number of bytes allocated to @vpd.
4620 * On output, the number of data bytes in @vpd.
4622 * This routine executes a READ_REV SLI4 mailbox command. In
4623 * addition, this routine gets the port vpd data.
4625 * Return codes
4626 * 0 - successful
4627 * -ENOMEM - could not allocated memory.
4629 static int
4630 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4631 uint8_t *vpd, uint32_t *vpd_size)
4633 int rc = 0;
4634 uint32_t dma_size;
4635 struct lpfc_dmabuf *dmabuf;
4636 struct lpfc_mqe *mqe;
4638 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4639 if (!dmabuf)
4640 return -ENOMEM;
4643 * Get a DMA buffer for the vpd data resulting from the READ_REV
4644 * mailbox command.
4646 dma_size = *vpd_size;
4647 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4648 dma_size,
4649 &dmabuf->phys,
4650 GFP_KERNEL);
4651 if (!dmabuf->virt) {
4652 kfree(dmabuf);
4653 return -ENOMEM;
4655 memset(dmabuf->virt, 0, dma_size);
4658 * The SLI4 implementation of READ_REV conflicts at word1,
4659 * bits 31:16 and SLI4 adds vpd functionality not present
4660 * in SLI3. This code corrects the conflicts.
4662 lpfc_read_rev(phba, mboxq);
4663 mqe = &mboxq->u.mqe;
4664 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4665 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4666 mqe->un.read_rev.word1 &= 0x0000FFFF;
4667 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4668 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4670 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4671 if (rc) {
4672 dma_free_coherent(&phba->pcidev->dev, dma_size,
4673 dmabuf->virt, dmabuf->phys);
4674 kfree(dmabuf);
4675 return -EIO;
4679 * The available vpd length cannot be bigger than the
4680 * DMA buffer passed to the port. Catch the less than
4681 * case and update the caller's size.
4683 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4684 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4686 memcpy(vpd, dmabuf->virt, *vpd_size);
4688 dma_free_coherent(&phba->pcidev->dev, dma_size,
4689 dmabuf->virt, dmabuf->phys);
4690 kfree(dmabuf);
4691 return 0;
4695 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4696 * @phba: pointer to lpfc hba data structure.
4698 * This routine retrieves SLI4 device physical port name this PCI function
4699 * is attached to.
4701 * Return codes
4702 * 0 - sucessful
4703 * otherwise - failed to retrieve physical port name
4705 static int
4706 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4708 LPFC_MBOXQ_t *mboxq;
4709 struct lpfc_mbx_read_config *rd_config;
4710 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4711 struct lpfc_controller_attribute *cntl_attr;
4712 struct lpfc_mbx_get_port_name *get_port_name;
4713 void *virtaddr = NULL;
4714 uint32_t alloclen, reqlen;
4715 uint32_t shdr_status, shdr_add_status;
4716 union lpfc_sli4_cfg_shdr *shdr;
4717 char cport_name = 0;
4718 int rc;
4720 /* We assume nothing at this point */
4721 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4722 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4724 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4725 if (!mboxq)
4726 return -ENOMEM;
4728 /* obtain link type and link number via READ_CONFIG */
4729 lpfc_read_config(phba, mboxq);
4730 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4731 if (rc == MBX_SUCCESS) {
4732 rd_config = &mboxq->u.mqe.un.rd_config;
4733 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
4734 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4735 phba->sli4_hba.lnk_info.lnk_tp =
4736 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
4737 phba->sli4_hba.lnk_info.lnk_no =
4738 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
4739 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4740 "3081 lnk_type:%d, lnk_numb:%d\n",
4741 phba->sli4_hba.lnk_info.lnk_tp,
4742 phba->sli4_hba.lnk_info.lnk_no);
4743 goto retrieve_ppname;
4744 } else
4745 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4746 "3082 Mailbox (x%x) returned ldv:x0\n",
4747 bf_get(lpfc_mqe_command,
4748 &mboxq->u.mqe));
4749 } else
4750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4751 "3083 Mailbox (x%x) failed, status:x%x\n",
4752 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4753 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4755 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4756 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4757 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4758 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4759 LPFC_SLI4_MBX_NEMBED);
4760 if (alloclen < reqlen) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4762 "3084 Allocated DMA memory size (%d) is "
4763 "less than the requested DMA memory size "
4764 "(%d)\n", alloclen, reqlen);
4765 rc = -ENOMEM;
4766 goto out_free_mboxq;
4768 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4769 virtaddr = mboxq->sge_array->addr[0];
4770 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4771 shdr = &mbx_cntl_attr->cfg_shdr;
4772 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4773 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4774 if (shdr_status || shdr_add_status || rc) {
4775 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4776 "3085 Mailbox x%x (x%x/x%x) failed, "
4777 "rc:x%x, status:x%x, add_status:x%x\n",
4778 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4779 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4780 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4781 rc, shdr_status, shdr_add_status);
4782 rc = -ENXIO;
4783 goto out_free_mboxq;
4785 cntl_attr = &mbx_cntl_attr->cntl_attr;
4786 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4787 phba->sli4_hba.lnk_info.lnk_tp =
4788 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4789 phba->sli4_hba.lnk_info.lnk_no =
4790 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4791 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4792 "3086 lnk_type:%d, lnk_numb:%d\n",
4793 phba->sli4_hba.lnk_info.lnk_tp,
4794 phba->sli4_hba.lnk_info.lnk_no);
4796 retrieve_ppname:
4797 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4798 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4799 sizeof(struct lpfc_mbx_get_port_name) -
4800 sizeof(struct lpfc_sli4_cfg_mhdr),
4801 LPFC_SLI4_MBX_EMBED);
4802 get_port_name = &mboxq->u.mqe.un.get_port_name;
4803 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4804 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4805 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4806 phba->sli4_hba.lnk_info.lnk_tp);
4807 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4808 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4810 if (shdr_status || shdr_add_status || rc) {
4811 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4812 "3087 Mailbox x%x (x%x/x%x) failed: "
4813 "rc:x%x, status:x%x, add_status:x%x\n",
4814 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4815 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4816 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4817 rc, shdr_status, shdr_add_status);
4818 rc = -ENXIO;
4819 goto out_free_mboxq;
4821 switch (phba->sli4_hba.lnk_info.lnk_no) {
4822 case LPFC_LINK_NUMBER_0:
4823 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4824 &get_port_name->u.response);
4825 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4826 break;
4827 case LPFC_LINK_NUMBER_1:
4828 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4829 &get_port_name->u.response);
4830 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4831 break;
4832 case LPFC_LINK_NUMBER_2:
4833 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4834 &get_port_name->u.response);
4835 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4836 break;
4837 case LPFC_LINK_NUMBER_3:
4838 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4839 &get_port_name->u.response);
4840 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4841 break;
4842 default:
4843 break;
4846 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4847 phba->Port[0] = cport_name;
4848 phba->Port[1] = '\0';
4849 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4850 "3091 SLI get port name: %s\n", phba->Port);
4853 out_free_mboxq:
4854 if (rc != MBX_TIMEOUT) {
4855 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4856 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4857 else
4858 mempool_free(mboxq, phba->mbox_mem_pool);
4860 return rc;
4864 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4865 * @phba: pointer to lpfc hba data structure.
4867 * This routine is called to explicitly arm the SLI4 device's completion and
4868 * event queues
4870 static void
4871 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4873 uint8_t fcp_eqidx;
4875 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4876 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4877 fcp_eqidx = 0;
4879 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4880 LPFC_QUEUE_REARM);
4881 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
4882 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4883 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4884 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4885 LPFC_QUEUE_REARM);
4889 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4890 * @phba: Pointer to HBA context object.
4891 * @type: The resource extent type.
4892 * @extnt_count: buffer to hold port available extent count.
4893 * @extnt_size: buffer to hold element count per extent.
4895 * This function calls the port and retrievs the number of available
4896 * extents and their size for a particular extent type.
4898 * Returns: 0 if successful. Nonzero otherwise.
4901 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4902 uint16_t *extnt_count, uint16_t *extnt_size)
4904 int rc = 0;
4905 uint32_t length;
4906 uint32_t mbox_tmo;
4907 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4908 LPFC_MBOXQ_t *mbox;
4910 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4911 if (!mbox)
4912 return -ENOMEM;
4914 /* Find out how many extents are available for this resource type */
4915 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4916 sizeof(struct lpfc_sli4_cfg_mhdr));
4917 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4918 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4919 length, LPFC_SLI4_MBX_EMBED);
4921 /* Send an extents count of 0 - the GET doesn't use it. */
4922 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4923 LPFC_SLI4_MBX_EMBED);
4924 if (unlikely(rc)) {
4925 rc = -EIO;
4926 goto err_exit;
4929 if (!phba->sli4_hba.intr_enable)
4930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4931 else {
4932 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4933 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4935 if (unlikely(rc)) {
4936 rc = -EIO;
4937 goto err_exit;
4940 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4941 if (bf_get(lpfc_mbox_hdr_status,
4942 &rsrc_info->header.cfg_shdr.response)) {
4943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4944 "2930 Failed to get resource extents "
4945 "Status 0x%x Add'l Status 0x%x\n",
4946 bf_get(lpfc_mbox_hdr_status,
4947 &rsrc_info->header.cfg_shdr.response),
4948 bf_get(lpfc_mbox_hdr_add_status,
4949 &rsrc_info->header.cfg_shdr.response));
4950 rc = -EIO;
4951 goto err_exit;
4954 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4955 &rsrc_info->u.rsp);
4956 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4957 &rsrc_info->u.rsp);
4958 err_exit:
4959 mempool_free(mbox, phba->mbox_mem_pool);
4960 return rc;
4964 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4965 * @phba: Pointer to HBA context object.
4966 * @type: The extent type to check.
4968 * This function reads the current available extents from the port and checks
4969 * if the extent count or extent size has changed since the last access.
4970 * Callers use this routine post port reset to understand if there is a
4971 * extent reprovisioning requirement.
4973 * Returns:
4974 * -Error: error indicates problem.
4975 * 1: Extent count or size has changed.
4976 * 0: No changes.
4978 static int
4979 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4981 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4982 uint16_t size_diff, rsrc_ext_size;
4983 int rc = 0;
4984 struct lpfc_rsrc_blks *rsrc_entry;
4985 struct list_head *rsrc_blk_list = NULL;
4987 size_diff = 0;
4988 curr_ext_cnt = 0;
4989 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4990 &rsrc_ext_cnt,
4991 &rsrc_ext_size);
4992 if (unlikely(rc))
4993 return -EIO;
4995 switch (type) {
4996 case LPFC_RSC_TYPE_FCOE_RPI:
4997 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4998 break;
4999 case LPFC_RSC_TYPE_FCOE_VPI:
5000 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5001 break;
5002 case LPFC_RSC_TYPE_FCOE_XRI:
5003 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5004 break;
5005 case LPFC_RSC_TYPE_FCOE_VFI:
5006 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5007 break;
5008 default:
5009 break;
5012 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5013 curr_ext_cnt++;
5014 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5015 size_diff++;
5018 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5019 rc = 1;
5021 return rc;
5025 * lpfc_sli4_cfg_post_extnts -
5026 * @phba: Pointer to HBA context object.
5027 * @extnt_cnt - number of available extents.
5028 * @type - the extent type (rpi, xri, vfi, vpi).
5029 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5030 * @mbox - pointer to the caller's allocated mailbox structure.
5032 * This function executes the extents allocation request. It also
5033 * takes care of the amount of memory needed to allocate or get the
5034 * allocated extents. It is the caller's responsibility to evaluate
5035 * the response.
5037 * Returns:
5038 * -Error: Error value describes the condition found.
5039 * 0: if successful
5041 static int
5042 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5043 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5045 int rc = 0;
5046 uint32_t req_len;
5047 uint32_t emb_len;
5048 uint32_t alloc_len, mbox_tmo;
5050 /* Calculate the total requested length of the dma memory */
5051 req_len = *extnt_cnt * sizeof(uint16_t);
5054 * Calculate the size of an embedded mailbox. The uint32_t
5055 * accounts for extents-specific word.
5057 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5058 sizeof(uint32_t);
5061 * Presume the allocation and response will fit into an embedded
5062 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5064 *emb = LPFC_SLI4_MBX_EMBED;
5065 if (req_len > emb_len) {
5066 req_len = *extnt_cnt * sizeof(uint16_t) +
5067 sizeof(union lpfc_sli4_cfg_shdr) +
5068 sizeof(uint32_t);
5069 *emb = LPFC_SLI4_MBX_NEMBED;
5072 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5073 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5074 req_len, *emb);
5075 if (alloc_len < req_len) {
5076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5077 "2982 Allocated DMA memory size (x%x) is "
5078 "less than the requested DMA memory "
5079 "size (x%x)\n", alloc_len, req_len);
5080 return -ENOMEM;
5082 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5083 if (unlikely(rc))
5084 return -EIO;
5086 if (!phba->sli4_hba.intr_enable)
5087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5088 else {
5089 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5090 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5093 if (unlikely(rc))
5094 rc = -EIO;
5095 return rc;
5099 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5100 * @phba: Pointer to HBA context object.
5101 * @type: The resource extent type to allocate.
5103 * This function allocates the number of elements for the specified
5104 * resource type.
5106 static int
5107 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5109 bool emb = false;
5110 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5111 uint16_t rsrc_id, rsrc_start, j, k;
5112 uint16_t *ids;
5113 int i, rc;
5114 unsigned long longs;
5115 unsigned long *bmask;
5116 struct lpfc_rsrc_blks *rsrc_blks;
5117 LPFC_MBOXQ_t *mbox;
5118 uint32_t length;
5119 struct lpfc_id_range *id_array = NULL;
5120 void *virtaddr = NULL;
5121 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5122 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5123 struct list_head *ext_blk_list;
5125 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5126 &rsrc_cnt,
5127 &rsrc_size);
5128 if (unlikely(rc))
5129 return -EIO;
5131 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5133 "3009 No available Resource Extents "
5134 "for resource type 0x%x: Count: 0x%x, "
5135 "Size 0x%x\n", type, rsrc_cnt,
5136 rsrc_size);
5137 return -ENOMEM;
5140 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5141 "2903 Available Resource Extents "
5142 "for resource type 0x%x: Count: 0x%x, "
5143 "Size 0x%x\n", type, rsrc_cnt,
5144 rsrc_size);
5146 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5147 if (!mbox)
5148 return -ENOMEM;
5150 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5151 if (unlikely(rc)) {
5152 rc = -EIO;
5153 goto err_exit;
5157 * Figure out where the response is located. Then get local pointers
5158 * to the response data. The port does not guarantee to respond to
5159 * all extents counts request so update the local variable with the
5160 * allocated count from the port.
5162 if (emb == LPFC_SLI4_MBX_EMBED) {
5163 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5164 id_array = &rsrc_ext->u.rsp.id[0];
5165 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5166 } else {
5167 virtaddr = mbox->sge_array->addr[0];
5168 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5169 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5170 id_array = &n_rsrc->id;
5173 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5174 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5177 * Based on the resource size and count, correct the base and max
5178 * resource values.
5180 length = sizeof(struct lpfc_rsrc_blks);
5181 switch (type) {
5182 case LPFC_RSC_TYPE_FCOE_RPI:
5183 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5184 sizeof(unsigned long),
5185 GFP_KERNEL);
5186 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5187 rc = -ENOMEM;
5188 goto err_exit;
5190 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5191 sizeof(uint16_t),
5192 GFP_KERNEL);
5193 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5194 kfree(phba->sli4_hba.rpi_bmask);
5195 rc = -ENOMEM;
5196 goto err_exit;
5200 * The next_rpi was initialized with the maximum available
5201 * count but the port may allocate a smaller number. Catch
5202 * that case and update the next_rpi.
5204 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5206 /* Initialize local ptrs for common extent processing later. */
5207 bmask = phba->sli4_hba.rpi_bmask;
5208 ids = phba->sli4_hba.rpi_ids;
5209 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5210 break;
5211 case LPFC_RSC_TYPE_FCOE_VPI:
5212 phba->vpi_bmask = kzalloc(longs *
5213 sizeof(unsigned long),
5214 GFP_KERNEL);
5215 if (unlikely(!phba->vpi_bmask)) {
5216 rc = -ENOMEM;
5217 goto err_exit;
5219 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5220 sizeof(uint16_t),
5221 GFP_KERNEL);
5222 if (unlikely(!phba->vpi_ids)) {
5223 kfree(phba->vpi_bmask);
5224 rc = -ENOMEM;
5225 goto err_exit;
5228 /* Initialize local ptrs for common extent processing later. */
5229 bmask = phba->vpi_bmask;
5230 ids = phba->vpi_ids;
5231 ext_blk_list = &phba->lpfc_vpi_blk_list;
5232 break;
5233 case LPFC_RSC_TYPE_FCOE_XRI:
5234 phba->sli4_hba.xri_bmask = kzalloc(longs *
5235 sizeof(unsigned long),
5236 GFP_KERNEL);
5237 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5238 rc = -ENOMEM;
5239 goto err_exit;
5241 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5242 sizeof(uint16_t),
5243 GFP_KERNEL);
5244 if (unlikely(!phba->sli4_hba.xri_ids)) {
5245 kfree(phba->sli4_hba.xri_bmask);
5246 rc = -ENOMEM;
5247 goto err_exit;
5250 /* Initialize local ptrs for common extent processing later. */
5251 bmask = phba->sli4_hba.xri_bmask;
5252 ids = phba->sli4_hba.xri_ids;
5253 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_VFI:
5256 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5257 sizeof(unsigned long),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5260 rc = -ENOMEM;
5261 goto err_exit;
5263 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5264 sizeof(uint16_t),
5265 GFP_KERNEL);
5266 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5267 kfree(phba->sli4_hba.vfi_bmask);
5268 rc = -ENOMEM;
5269 goto err_exit;
5272 /* Initialize local ptrs for common extent processing later. */
5273 bmask = phba->sli4_hba.vfi_bmask;
5274 ids = phba->sli4_hba.vfi_ids;
5275 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5276 break;
5277 default:
5278 /* Unsupported Opcode. Fail call. */
5279 id_array = NULL;
5280 bmask = NULL;
5281 ids = NULL;
5282 ext_blk_list = NULL;
5283 goto err_exit;
5287 * Complete initializing the extent configuration with the
5288 * allocated ids assigned to this function. The bitmask serves
5289 * as an index into the array and manages the available ids. The
5290 * array just stores the ids communicated to the port via the wqes.
5292 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5293 if ((i % 2) == 0)
5294 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5295 &id_array[k]);
5296 else
5297 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5298 &id_array[k]);
5300 rsrc_blks = kzalloc(length, GFP_KERNEL);
5301 if (unlikely(!rsrc_blks)) {
5302 rc = -ENOMEM;
5303 kfree(bmask);
5304 kfree(ids);
5305 goto err_exit;
5307 rsrc_blks->rsrc_start = rsrc_id;
5308 rsrc_blks->rsrc_size = rsrc_size;
5309 list_add_tail(&rsrc_blks->list, ext_blk_list);
5310 rsrc_start = rsrc_id;
5311 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5312 phba->sli4_hba.scsi_xri_start = rsrc_start +
5313 lpfc_sli4_get_els_iocb_cnt(phba);
5315 while (rsrc_id < (rsrc_start + rsrc_size)) {
5316 ids[j] = rsrc_id;
5317 rsrc_id++;
5318 j++;
5320 /* Entire word processed. Get next word.*/
5321 if ((i % 2) == 1)
5322 k++;
5324 err_exit:
5325 lpfc_sli4_mbox_cmd_free(phba, mbox);
5326 return rc;
5330 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5331 * @phba: Pointer to HBA context object.
5332 * @type: the extent's type.
5334 * This function deallocates all extents of a particular resource type.
5335 * SLI4 does not allow for deallocating a particular extent range. It
5336 * is the caller's responsibility to release all kernel memory resources.
5338 static int
5339 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5341 int rc;
5342 uint32_t length, mbox_tmo = 0;
5343 LPFC_MBOXQ_t *mbox;
5344 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5345 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5347 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5348 if (!mbox)
5349 return -ENOMEM;
5352 * This function sends an embedded mailbox because it only sends the
5353 * the resource type. All extents of this type are released by the
5354 * port.
5356 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5357 sizeof(struct lpfc_sli4_cfg_mhdr));
5358 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5359 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5360 length, LPFC_SLI4_MBX_EMBED);
5362 /* Send an extents count of 0 - the dealloc doesn't use it. */
5363 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5364 LPFC_SLI4_MBX_EMBED);
5365 if (unlikely(rc)) {
5366 rc = -EIO;
5367 goto out_free_mbox;
5369 if (!phba->sli4_hba.intr_enable)
5370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5371 else {
5372 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5373 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5375 if (unlikely(rc)) {
5376 rc = -EIO;
5377 goto out_free_mbox;
5380 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5381 if (bf_get(lpfc_mbox_hdr_status,
5382 &dealloc_rsrc->header.cfg_shdr.response)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "2919 Failed to release resource extents "
5385 "for type %d - Status 0x%x Add'l Status 0x%x. "
5386 "Resource memory not released.\n",
5387 type,
5388 bf_get(lpfc_mbox_hdr_status,
5389 &dealloc_rsrc->header.cfg_shdr.response),
5390 bf_get(lpfc_mbox_hdr_add_status,
5391 &dealloc_rsrc->header.cfg_shdr.response));
5392 rc = -EIO;
5393 goto out_free_mbox;
5396 /* Release kernel memory resources for the specific type. */
5397 switch (type) {
5398 case LPFC_RSC_TYPE_FCOE_VPI:
5399 kfree(phba->vpi_bmask);
5400 kfree(phba->vpi_ids);
5401 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5402 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5403 &phba->lpfc_vpi_blk_list, list) {
5404 list_del_init(&rsrc_blk->list);
5405 kfree(rsrc_blk);
5407 break;
5408 case LPFC_RSC_TYPE_FCOE_XRI:
5409 kfree(phba->sli4_hba.xri_bmask);
5410 kfree(phba->sli4_hba.xri_ids);
5411 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5412 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5413 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5414 list_del_init(&rsrc_blk->list);
5415 kfree(rsrc_blk);
5417 break;
5418 case LPFC_RSC_TYPE_FCOE_VFI:
5419 kfree(phba->sli4_hba.vfi_bmask);
5420 kfree(phba->sli4_hba.vfi_ids);
5421 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5422 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5423 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5424 list_del_init(&rsrc_blk->list);
5425 kfree(rsrc_blk);
5427 break;
5428 case LPFC_RSC_TYPE_FCOE_RPI:
5429 /* RPI bitmask and physical id array are cleaned up earlier. */
5430 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5431 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5432 list_del_init(&rsrc_blk->list);
5433 kfree(rsrc_blk);
5435 break;
5436 default:
5437 break;
5440 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5442 out_free_mbox:
5443 mempool_free(mbox, phba->mbox_mem_pool);
5444 return rc;
5448 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5449 * @phba: Pointer to HBA context object.
5451 * This function allocates all SLI4 resource identifiers.
5454 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5456 int i, rc, error = 0;
5457 uint16_t count, base;
5458 unsigned long longs;
5460 if (phba->sli4_hba.extents_in_use) {
5462 * The port supports resource extents. The XRI, VPI, VFI, RPI
5463 * resource extent count must be read and allocated before
5464 * provisioning the resource id arrays.
5466 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5467 LPFC_IDX_RSRC_RDY) {
5469 * Extent-based resources are set - the driver could
5470 * be in a port reset. Figure out if any corrective
5471 * actions need to be taken.
5473 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5474 LPFC_RSC_TYPE_FCOE_VFI);
5475 if (rc != 0)
5476 error++;
5477 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5478 LPFC_RSC_TYPE_FCOE_VPI);
5479 if (rc != 0)
5480 error++;
5481 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5482 LPFC_RSC_TYPE_FCOE_XRI);
5483 if (rc != 0)
5484 error++;
5485 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5486 LPFC_RSC_TYPE_FCOE_RPI);
5487 if (rc != 0)
5488 error++;
5491 * It's possible that the number of resources
5492 * provided to this port instance changed between
5493 * resets. Detect this condition and reallocate
5494 * resources. Otherwise, there is no action.
5496 if (error) {
5497 lpfc_printf_log(phba, KERN_INFO,
5498 LOG_MBOX | LOG_INIT,
5499 "2931 Detected extent resource "
5500 "change. Reallocating all "
5501 "extents.\n");
5502 rc = lpfc_sli4_dealloc_extent(phba,
5503 LPFC_RSC_TYPE_FCOE_VFI);
5504 rc = lpfc_sli4_dealloc_extent(phba,
5505 LPFC_RSC_TYPE_FCOE_VPI);
5506 rc = lpfc_sli4_dealloc_extent(phba,
5507 LPFC_RSC_TYPE_FCOE_XRI);
5508 rc = lpfc_sli4_dealloc_extent(phba,
5509 LPFC_RSC_TYPE_FCOE_RPI);
5510 } else
5511 return 0;
5514 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5515 if (unlikely(rc))
5516 goto err_exit;
5518 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5519 if (unlikely(rc))
5520 goto err_exit;
5522 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5523 if (unlikely(rc))
5524 goto err_exit;
5526 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5527 if (unlikely(rc))
5528 goto err_exit;
5529 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5530 LPFC_IDX_RSRC_RDY);
5531 return rc;
5532 } else {
5534 * The port does not support resource extents. The XRI, VPI,
5535 * VFI, RPI resource ids were determined from READ_CONFIG.
5536 * Just allocate the bitmasks and provision the resource id
5537 * arrays. If a port reset is active, the resources don't
5538 * need any action - just exit.
5540 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5541 LPFC_IDX_RSRC_RDY)
5542 return 0;
5544 /* RPIs. */
5545 count = phba->sli4_hba.max_cfg_param.max_rpi;
5546 base = phba->sli4_hba.max_cfg_param.rpi_base;
5547 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5548 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5549 sizeof(unsigned long),
5550 GFP_KERNEL);
5551 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5552 rc = -ENOMEM;
5553 goto err_exit;
5555 phba->sli4_hba.rpi_ids = kzalloc(count *
5556 sizeof(uint16_t),
5557 GFP_KERNEL);
5558 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5559 rc = -ENOMEM;
5560 goto free_rpi_bmask;
5563 for (i = 0; i < count; i++)
5564 phba->sli4_hba.rpi_ids[i] = base + i;
5566 /* VPIs. */
5567 count = phba->sli4_hba.max_cfg_param.max_vpi;
5568 base = phba->sli4_hba.max_cfg_param.vpi_base;
5569 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5570 phba->vpi_bmask = kzalloc(longs *
5571 sizeof(unsigned long),
5572 GFP_KERNEL);
5573 if (unlikely(!phba->vpi_bmask)) {
5574 rc = -ENOMEM;
5575 goto free_rpi_ids;
5577 phba->vpi_ids = kzalloc(count *
5578 sizeof(uint16_t),
5579 GFP_KERNEL);
5580 if (unlikely(!phba->vpi_ids)) {
5581 rc = -ENOMEM;
5582 goto free_vpi_bmask;
5585 for (i = 0; i < count; i++)
5586 phba->vpi_ids[i] = base + i;
5588 /* XRIs. */
5589 count = phba->sli4_hba.max_cfg_param.max_xri;
5590 base = phba->sli4_hba.max_cfg_param.xri_base;
5591 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5592 phba->sli4_hba.xri_bmask = kzalloc(longs *
5593 sizeof(unsigned long),
5594 GFP_KERNEL);
5595 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5596 rc = -ENOMEM;
5597 goto free_vpi_ids;
5599 phba->sli4_hba.xri_ids = kzalloc(count *
5600 sizeof(uint16_t),
5601 GFP_KERNEL);
5602 if (unlikely(!phba->sli4_hba.xri_ids)) {
5603 rc = -ENOMEM;
5604 goto free_xri_bmask;
5607 for (i = 0; i < count; i++)
5608 phba->sli4_hba.xri_ids[i] = base + i;
5610 /* VFIs. */
5611 count = phba->sli4_hba.max_cfg_param.max_vfi;
5612 base = phba->sli4_hba.max_cfg_param.vfi_base;
5613 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5614 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5615 sizeof(unsigned long),
5616 GFP_KERNEL);
5617 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5618 rc = -ENOMEM;
5619 goto free_xri_ids;
5621 phba->sli4_hba.vfi_ids = kzalloc(count *
5622 sizeof(uint16_t),
5623 GFP_KERNEL);
5624 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5625 rc = -ENOMEM;
5626 goto free_vfi_bmask;
5629 for (i = 0; i < count; i++)
5630 phba->sli4_hba.vfi_ids[i] = base + i;
5633 * Mark all resources ready. An HBA reset doesn't need
5634 * to reset the initialization.
5636 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5637 LPFC_IDX_RSRC_RDY);
5638 return 0;
5641 free_vfi_bmask:
5642 kfree(phba->sli4_hba.vfi_bmask);
5643 free_xri_ids:
5644 kfree(phba->sli4_hba.xri_ids);
5645 free_xri_bmask:
5646 kfree(phba->sli4_hba.xri_bmask);
5647 free_vpi_ids:
5648 kfree(phba->vpi_ids);
5649 free_vpi_bmask:
5650 kfree(phba->vpi_bmask);
5651 free_rpi_ids:
5652 kfree(phba->sli4_hba.rpi_ids);
5653 free_rpi_bmask:
5654 kfree(phba->sli4_hba.rpi_bmask);
5655 err_exit:
5656 return rc;
5660 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5661 * @phba: Pointer to HBA context object.
5663 * This function allocates the number of elements for the specified
5664 * resource type.
5667 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5669 if (phba->sli4_hba.extents_in_use) {
5670 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5671 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5672 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5673 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5674 } else {
5675 kfree(phba->vpi_bmask);
5676 kfree(phba->vpi_ids);
5677 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5678 kfree(phba->sli4_hba.xri_bmask);
5679 kfree(phba->sli4_hba.xri_ids);
5680 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5681 kfree(phba->sli4_hba.vfi_bmask);
5682 kfree(phba->sli4_hba.vfi_ids);
5683 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5684 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5687 return 0;
5691 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5692 * @phba: Pointer to HBA context object.
5693 * @type: The resource extent type.
5694 * @extnt_count: buffer to hold port extent count response
5695 * @extnt_size: buffer to hold port extent size response.
5697 * This function calls the port to read the host allocated extents
5698 * for a particular type.
5701 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5702 uint16_t *extnt_cnt, uint16_t *extnt_size)
5704 bool emb;
5705 int rc = 0;
5706 uint16_t curr_blks = 0;
5707 uint32_t req_len, emb_len;
5708 uint32_t alloc_len, mbox_tmo;
5709 struct list_head *blk_list_head;
5710 struct lpfc_rsrc_blks *rsrc_blk;
5711 LPFC_MBOXQ_t *mbox;
5712 void *virtaddr = NULL;
5713 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5714 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5715 union lpfc_sli4_cfg_shdr *shdr;
5717 switch (type) {
5718 case LPFC_RSC_TYPE_FCOE_VPI:
5719 blk_list_head = &phba->lpfc_vpi_blk_list;
5720 break;
5721 case LPFC_RSC_TYPE_FCOE_XRI:
5722 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5723 break;
5724 case LPFC_RSC_TYPE_FCOE_VFI:
5725 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5726 break;
5727 case LPFC_RSC_TYPE_FCOE_RPI:
5728 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5729 break;
5730 default:
5731 return -EIO;
5734 /* Count the number of extents currently allocatd for this type. */
5735 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5736 if (curr_blks == 0) {
5738 * The GET_ALLOCATED mailbox does not return the size,
5739 * just the count. The size should be just the size
5740 * stored in the current allocated block and all sizes
5741 * for an extent type are the same so set the return
5742 * value now.
5744 *extnt_size = rsrc_blk->rsrc_size;
5746 curr_blks++;
5749 /* Calculate the total requested length of the dma memory. */
5750 req_len = curr_blks * sizeof(uint16_t);
5753 * Calculate the size of an embedded mailbox. The uint32_t
5754 * accounts for extents-specific word.
5756 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5757 sizeof(uint32_t);
5760 * Presume the allocation and response will fit into an embedded
5761 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5763 emb = LPFC_SLI4_MBX_EMBED;
5764 req_len = emb_len;
5765 if (req_len > emb_len) {
5766 req_len = curr_blks * sizeof(uint16_t) +
5767 sizeof(union lpfc_sli4_cfg_shdr) +
5768 sizeof(uint32_t);
5769 emb = LPFC_SLI4_MBX_NEMBED;
5772 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5773 if (!mbox)
5774 return -ENOMEM;
5775 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5777 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5778 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5779 req_len, emb);
5780 if (alloc_len < req_len) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5782 "2983 Allocated DMA memory size (x%x) is "
5783 "less than the requested DMA memory "
5784 "size (x%x)\n", alloc_len, req_len);
5785 rc = -ENOMEM;
5786 goto err_exit;
5788 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5789 if (unlikely(rc)) {
5790 rc = -EIO;
5791 goto err_exit;
5794 if (!phba->sli4_hba.intr_enable)
5795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5796 else {
5797 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5798 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5801 if (unlikely(rc)) {
5802 rc = -EIO;
5803 goto err_exit;
5807 * Figure out where the response is located. Then get local pointers
5808 * to the response data. The port does not guarantee to respond to
5809 * all extents counts request so update the local variable with the
5810 * allocated count from the port.
5812 if (emb == LPFC_SLI4_MBX_EMBED) {
5813 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5814 shdr = &rsrc_ext->header.cfg_shdr;
5815 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5816 } else {
5817 virtaddr = mbox->sge_array->addr[0];
5818 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5819 shdr = &n_rsrc->cfg_shdr;
5820 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5823 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5825 "2984 Failed to read allocated resources "
5826 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5827 type,
5828 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5829 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5830 rc = -EIO;
5831 goto err_exit;
5833 err_exit:
5834 lpfc_sli4_mbox_cmd_free(phba, mbox);
5835 return rc;
5839 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5840 * @phba: Pointer to HBA context object.
5842 * This function is the main SLI4 device intialization PCI function. This
5843 * function is called by the HBA intialization code, HBA reset code and
5844 * HBA error attention handler code. Caller is not required to hold any
5845 * locks.
5848 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5850 int rc;
5851 LPFC_MBOXQ_t *mboxq;
5852 struct lpfc_mqe *mqe;
5853 uint8_t *vpd;
5854 uint32_t vpd_size;
5855 uint32_t ftr_rsp = 0;
5856 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5857 struct lpfc_vport *vport = phba->pport;
5858 struct lpfc_dmabuf *mp;
5860 /* Perform a PCI function reset to start from clean */
5861 rc = lpfc_pci_function_reset(phba);
5862 if (unlikely(rc))
5863 return -ENODEV;
5865 /* Check the HBA Host Status Register for readyness */
5866 rc = lpfc_sli4_post_status_check(phba);
5867 if (unlikely(rc))
5868 return -ENODEV;
5869 else {
5870 spin_lock_irq(&phba->hbalock);
5871 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5872 spin_unlock_irq(&phba->hbalock);
5876 * Allocate a single mailbox container for initializing the
5877 * port.
5879 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5880 if (!mboxq)
5881 return -ENOMEM;
5884 * Continue initialization with default values even if driver failed
5885 * to read FCoE param config regions
5887 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
5888 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
5889 "2570 Failed to read FCoE parameters\n");
5891 /* Issue READ_REV to collect vpd and FW information. */
5892 vpd_size = SLI4_PAGE_SIZE;
5893 vpd = kzalloc(vpd_size, GFP_KERNEL);
5894 if (!vpd) {
5895 rc = -ENOMEM;
5896 goto out_free_mbox;
5899 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
5900 if (unlikely(rc)) {
5901 kfree(vpd);
5902 goto out_free_mbox;
5904 mqe = &mboxq->u.mqe;
5905 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5906 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
5907 phba->hba_flag |= HBA_FCOE_MODE;
5908 else
5909 phba->hba_flag &= ~HBA_FCOE_MODE;
5911 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5912 LPFC_DCBX_CEE_MODE)
5913 phba->hba_flag |= HBA_FIP_SUPPORT;
5914 else
5915 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5917 if (phba->sli_rev != LPFC_SLI_REV4) {
5918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5919 "0376 READ_REV Error. SLI Level %d "
5920 "FCoE enabled %d\n",
5921 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
5922 rc = -EIO;
5923 kfree(vpd);
5924 goto out_free_mbox;
5928 * Retrieve sli4 device physical port name, failure of doing it
5929 * is considered as non-fatal.
5931 rc = lpfc_sli4_retrieve_pport_name(phba);
5932 if (!rc)
5933 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5934 "3080 Successful retrieving SLI4 device "
5935 "physical port name: %s.\n", phba->Port);
5938 * Evaluate the read rev and vpd data. Populate the driver
5939 * state with the results. If this routine fails, the failure
5940 * is not fatal as the driver will use generic values.
5942 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5943 if (unlikely(!rc)) {
5944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5945 "0377 Error %d parsing vpd. "
5946 "Using defaults.\n", rc);
5947 rc = 0;
5949 kfree(vpd);
5951 /* Save information as VPD data */
5952 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5953 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5954 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5955 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5956 &mqe->un.read_rev);
5957 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5958 &mqe->un.read_rev);
5959 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5960 &mqe->un.read_rev);
5961 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5962 &mqe->un.read_rev);
5963 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5964 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5965 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5966 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5967 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5968 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5969 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5970 "(%d):0380 READ_REV Status x%x "
5971 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5972 mboxq->vport ? mboxq->vport->vpi : 0,
5973 bf_get(lpfc_mqe_status, mqe),
5974 phba->vpd.rev.opFwName,
5975 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5976 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
5979 * Discover the port's supported feature set and match it against the
5980 * hosts requests.
5982 lpfc_request_features(phba, mboxq);
5983 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5984 if (unlikely(rc)) {
5985 rc = -EIO;
5986 goto out_free_mbox;
5990 * The port must support FCP initiator mode as this is the
5991 * only mode running in the host.
5993 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5994 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5995 "0378 No support for fcpi mode.\n");
5996 ftr_rsp++;
5998 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5999 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6000 else
6001 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6003 * If the port cannot support the host's requested features
6004 * then turn off the global config parameters to disable the
6005 * feature in the driver. This is not a fatal error.
6007 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6008 if (phba->cfg_enable_bg) {
6009 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6010 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6011 else
6012 ftr_rsp++;
6015 if (phba->max_vpi && phba->cfg_enable_npiv &&
6016 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6017 ftr_rsp++;
6019 if (ftr_rsp) {
6020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6021 "0379 Feature Mismatch Data: x%08x %08x "
6022 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6023 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6024 phba->cfg_enable_npiv, phba->max_vpi);
6025 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6026 phba->cfg_enable_bg = 0;
6027 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6028 phba->cfg_enable_npiv = 0;
6031 /* These SLI3 features are assumed in SLI4 */
6032 spin_lock_irq(&phba->hbalock);
6033 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6034 spin_unlock_irq(&phba->hbalock);
6037 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6038 * calls depends on these resources to complete port setup.
6040 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6041 if (rc) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6043 "2920 Failed to alloc Resource IDs "
6044 "rc = x%x\n", rc);
6045 goto out_free_mbox;
6048 /* Read the port's service parameters. */
6049 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6050 if (rc) {
6051 phba->link_state = LPFC_HBA_ERROR;
6052 rc = -ENOMEM;
6053 goto out_free_mbox;
6056 mboxq->vport = vport;
6057 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6058 mp = (struct lpfc_dmabuf *) mboxq->context1;
6059 if (rc == MBX_SUCCESS) {
6060 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6061 rc = 0;
6065 * This memory was allocated by the lpfc_read_sparam routine. Release
6066 * it to the mbuf pool.
6068 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6069 kfree(mp);
6070 mboxq->context1 = NULL;
6071 if (unlikely(rc)) {
6072 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6073 "0382 READ_SPARAM command failed "
6074 "status %d, mbxStatus x%x\n",
6075 rc, bf_get(lpfc_mqe_status, mqe));
6076 phba->link_state = LPFC_HBA_ERROR;
6077 rc = -EIO;
6078 goto out_free_mbox;
6081 lpfc_update_vport_wwn(vport);
6083 /* Update the fc_host data structures with new wwn. */
6084 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6085 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6087 /* Register SGL pool to the device using non-embedded mailbox command */
6088 if (!phba->sli4_hba.extents_in_use) {
6089 rc = lpfc_sli4_post_els_sgl_list(phba);
6090 if (unlikely(rc)) {
6091 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6092 "0582 Error %d during els sgl post "
6093 "operation\n", rc);
6094 rc = -ENODEV;
6095 goto out_free_mbox;
6097 } else {
6098 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6099 if (unlikely(rc)) {
6100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6101 "2560 Error %d during els sgl post "
6102 "operation\n", rc);
6103 rc = -ENODEV;
6104 goto out_free_mbox;
6108 /* Register SCSI SGL pool to the device */
6109 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6110 if (unlikely(rc)) {
6111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6112 "0383 Error %d during scsi sgl post "
6113 "operation\n", rc);
6114 /* Some Scsi buffers were moved to the abort scsi list */
6115 /* A pci function reset will repost them */
6116 rc = -ENODEV;
6117 goto out_free_mbox;
6120 /* Post the rpi header region to the device. */
6121 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6122 if (unlikely(rc)) {
6123 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6124 "0393 Error %d during rpi post operation\n",
6125 rc);
6126 rc = -ENODEV;
6127 goto out_free_mbox;
6130 /* Create all the SLI4 queues */
6131 rc = lpfc_sli4_queue_create(phba);
6132 if (rc) {
6133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6134 "3089 Failed to allocate queues\n");
6135 rc = -ENODEV;
6136 goto out_stop_timers;
6138 /* Set up all the queues to the device */
6139 rc = lpfc_sli4_queue_setup(phba);
6140 if (unlikely(rc)) {
6141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6142 "0381 Error %d during queue setup.\n ", rc);
6143 goto out_destroy_queue;
6146 /* Arm the CQs and then EQs on device */
6147 lpfc_sli4_arm_cqeq_intr(phba);
6149 /* Indicate device interrupt mode */
6150 phba->sli4_hba.intr_enable = 1;
6152 /* Allow asynchronous mailbox command to go through */
6153 spin_lock_irq(&phba->hbalock);
6154 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6155 spin_unlock_irq(&phba->hbalock);
6157 /* Post receive buffers to the device */
6158 lpfc_sli4_rb_setup(phba);
6160 /* Reset HBA FCF states after HBA reset */
6161 phba->fcf.fcf_flag = 0;
6162 phba->fcf.current_rec.flag = 0;
6164 /* Start the ELS watchdog timer */
6165 mod_timer(&vport->els_tmofunc,
6166 jiffies + HZ * (phba->fc_ratov * 2));
6168 /* Start heart beat timer */
6169 mod_timer(&phba->hb_tmofunc,
6170 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6171 phba->hb_outstanding = 0;
6172 phba->last_completion_time = jiffies;
6174 /* Start error attention (ERATT) polling timer */
6175 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6177 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6178 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6179 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6180 if (!rc) {
6181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6182 "2829 This device supports "
6183 "Advanced Error Reporting (AER)\n");
6184 spin_lock_irq(&phba->hbalock);
6185 phba->hba_flag |= HBA_AER_ENABLED;
6186 spin_unlock_irq(&phba->hbalock);
6187 } else {
6188 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6189 "2830 This device does not support "
6190 "Advanced Error Reporting (AER)\n");
6191 phba->cfg_aer_support = 0;
6193 rc = 0;
6196 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6198 * The FC Port needs to register FCFI (index 0)
6200 lpfc_reg_fcfi(phba, mboxq);
6201 mboxq->vport = phba->pport;
6202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6203 if (rc != MBX_SUCCESS)
6204 goto out_unset_queue;
6205 rc = 0;
6206 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6207 &mboxq->u.mqe.un.reg_fcfi);
6210 * The port is ready, set the host's link state to LINK_DOWN
6211 * in preparation for link interrupts.
6213 spin_lock_irq(&phba->hbalock);
6214 phba->link_state = LPFC_LINK_DOWN;
6215 spin_unlock_irq(&phba->hbalock);
6216 if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6217 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6218 if (rc)
6219 goto out_unset_queue;
6221 mempool_free(mboxq, phba->mbox_mem_pool);
6222 return rc;
6223 out_unset_queue:
6224 /* Unset all the queues set up in this routine when error out */
6225 lpfc_sli4_queue_unset(phba);
6226 out_destroy_queue:
6227 lpfc_sli4_queue_destroy(phba);
6228 out_stop_timers:
6229 lpfc_stop_hba_timers(phba);
6230 out_free_mbox:
6231 mempool_free(mboxq, phba->mbox_mem_pool);
6232 return rc;
6236 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6237 * @ptr: context object - pointer to hba structure.
6239 * This is the callback function for mailbox timer. The mailbox
6240 * timer is armed when a new mailbox command is issued and the timer
6241 * is deleted when the mailbox complete. The function is called by
6242 * the kernel timer code when a mailbox does not complete within
6243 * expected time. This function wakes up the worker thread to
6244 * process the mailbox timeout and returns. All the processing is
6245 * done by the worker thread function lpfc_mbox_timeout_handler.
6247 void
6248 lpfc_mbox_timeout(unsigned long ptr)
6250 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6251 unsigned long iflag;
6252 uint32_t tmo_posted;
6254 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6255 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6256 if (!tmo_posted)
6257 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6258 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6260 if (!tmo_posted)
6261 lpfc_worker_wake_up(phba);
6262 return;
6267 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6268 * @phba: Pointer to HBA context object.
6270 * This function is called from worker thread when a mailbox command times out.
6271 * The caller is not required to hold any locks. This function will reset the
6272 * HBA and recover all the pending commands.
6274 void
6275 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6277 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6278 MAILBOX_t *mb = &pmbox->u.mb;
6279 struct lpfc_sli *psli = &phba->sli;
6280 struct lpfc_sli_ring *pring;
6282 /* Check the pmbox pointer first. There is a race condition
6283 * between the mbox timeout handler getting executed in the
6284 * worklist and the mailbox actually completing. When this
6285 * race condition occurs, the mbox_active will be NULL.
6287 spin_lock_irq(&phba->hbalock);
6288 if (pmbox == NULL) {
6289 lpfc_printf_log(phba, KERN_WARNING,
6290 LOG_MBOX | LOG_SLI,
6291 "0353 Active Mailbox cleared - mailbox timeout "
6292 "exiting\n");
6293 spin_unlock_irq(&phba->hbalock);
6294 return;
6297 /* Mbox cmd <mbxCommand> timeout */
6298 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6299 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6300 mb->mbxCommand,
6301 phba->pport->port_state,
6302 phba->sli.sli_flag,
6303 phba->sli.mbox_active);
6304 spin_unlock_irq(&phba->hbalock);
6306 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6307 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6308 * it to fail all outstanding SCSI IO.
6310 spin_lock_irq(&phba->pport->work_port_lock);
6311 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6312 spin_unlock_irq(&phba->pport->work_port_lock);
6313 spin_lock_irq(&phba->hbalock);
6314 phba->link_state = LPFC_LINK_UNKNOWN;
6315 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6316 spin_unlock_irq(&phba->hbalock);
6318 pring = &psli->ring[psli->fcp_ring];
6319 lpfc_sli_abort_iocb_ring(phba, pring);
6321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6322 "0345 Resetting board due to mailbox timeout\n");
6324 /* Reset the HBA device */
6325 lpfc_reset_hba(phba);
6329 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6330 * @phba: Pointer to HBA context object.
6331 * @pmbox: Pointer to mailbox object.
6332 * @flag: Flag indicating how the mailbox need to be processed.
6334 * This function is called by discovery code and HBA management code
6335 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6336 * function gets the hbalock to protect the data structures.
6337 * The mailbox command can be submitted in polling mode, in which case
6338 * this function will wait in a polling loop for the completion of the
6339 * mailbox.
6340 * If the mailbox is submitted in no_wait mode (not polling) the
6341 * function will submit the command and returns immediately without waiting
6342 * for the mailbox completion. The no_wait is supported only when HBA
6343 * is in SLI2/SLI3 mode - interrupts are enabled.
6344 * The SLI interface allows only one mailbox pending at a time. If the
6345 * mailbox is issued in polling mode and there is already a mailbox
6346 * pending, then the function will return an error. If the mailbox is issued
6347 * in NO_WAIT mode and there is a mailbox pending already, the function
6348 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6349 * The sli layer owns the mailbox object until the completion of mailbox
6350 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6351 * return codes the caller owns the mailbox command after the return of
6352 * the function.
6354 static int
6355 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6356 uint32_t flag)
6358 MAILBOX_t *mb;
6359 struct lpfc_sli *psli = &phba->sli;
6360 uint32_t status, evtctr;
6361 uint32_t ha_copy, hc_copy;
6362 int i;
6363 unsigned long timeout;
6364 unsigned long drvr_flag = 0;
6365 uint32_t word0, ldata;
6366 void __iomem *to_slim;
6367 int processing_queue = 0;
6369 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6370 if (!pmbox) {
6371 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6372 /* processing mbox queue from intr_handler */
6373 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6374 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6375 return MBX_SUCCESS;
6377 processing_queue = 1;
6378 pmbox = lpfc_mbox_get(phba);
6379 if (!pmbox) {
6380 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6381 return MBX_SUCCESS;
6385 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6386 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6387 if(!pmbox->vport) {
6388 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6389 lpfc_printf_log(phba, KERN_ERR,
6390 LOG_MBOX | LOG_VPORT,
6391 "1806 Mbox x%x failed. No vport\n",
6392 pmbox->u.mb.mbxCommand);
6393 dump_stack();
6394 goto out_not_finished;
6398 /* If the PCI channel is in offline state, do not post mbox. */
6399 if (unlikely(pci_channel_offline(phba->pcidev))) {
6400 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6401 goto out_not_finished;
6404 /* If HBA has a deferred error attention, fail the iocb. */
6405 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6406 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6407 goto out_not_finished;
6410 psli = &phba->sli;
6412 mb = &pmbox->u.mb;
6413 status = MBX_SUCCESS;
6415 if (phba->link_state == LPFC_HBA_ERROR) {
6416 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6418 /* Mbox command <mbxCommand> cannot issue */
6419 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6420 "(%d):0311 Mailbox command x%x cannot "
6421 "issue Data: x%x x%x\n",
6422 pmbox->vport ? pmbox->vport->vpi : 0,
6423 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6424 goto out_not_finished;
6427 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6428 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6429 !(hc_copy & HC_MBINT_ENA)) {
6430 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6431 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6432 "(%d):2528 Mailbox command x%x cannot "
6433 "issue Data: x%x x%x\n",
6434 pmbox->vport ? pmbox->vport->vpi : 0,
6435 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6436 goto out_not_finished;
6440 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6441 /* Polling for a mbox command when another one is already active
6442 * is not allowed in SLI. Also, the driver must have established
6443 * SLI2 mode to queue and process multiple mbox commands.
6446 if (flag & MBX_POLL) {
6447 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6449 /* Mbox command <mbxCommand> cannot issue */
6450 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6451 "(%d):2529 Mailbox command x%x "
6452 "cannot issue Data: x%x x%x\n",
6453 pmbox->vport ? pmbox->vport->vpi : 0,
6454 pmbox->u.mb.mbxCommand,
6455 psli->sli_flag, flag);
6456 goto out_not_finished;
6459 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6460 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6461 /* Mbox command <mbxCommand> cannot issue */
6462 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6463 "(%d):2530 Mailbox command x%x "
6464 "cannot issue Data: x%x x%x\n",
6465 pmbox->vport ? pmbox->vport->vpi : 0,
6466 pmbox->u.mb.mbxCommand,
6467 psli->sli_flag, flag);
6468 goto out_not_finished;
6471 /* Another mailbox command is still being processed, queue this
6472 * command to be processed later.
6474 lpfc_mbox_put(phba, pmbox);
6476 /* Mbox cmd issue - BUSY */
6477 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6478 "(%d):0308 Mbox cmd issue - BUSY Data: "
6479 "x%x x%x x%x x%x\n",
6480 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6481 mb->mbxCommand, phba->pport->port_state,
6482 psli->sli_flag, flag);
6484 psli->slistat.mbox_busy++;
6485 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6487 if (pmbox->vport) {
6488 lpfc_debugfs_disc_trc(pmbox->vport,
6489 LPFC_DISC_TRC_MBOX_VPORT,
6490 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6491 (uint32_t)mb->mbxCommand,
6492 mb->un.varWords[0], mb->un.varWords[1]);
6494 else {
6495 lpfc_debugfs_disc_trc(phba->pport,
6496 LPFC_DISC_TRC_MBOX,
6497 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6498 (uint32_t)mb->mbxCommand,
6499 mb->un.varWords[0], mb->un.varWords[1]);
6502 return MBX_BUSY;
6505 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6507 /* If we are not polling, we MUST be in SLI2 mode */
6508 if (flag != MBX_POLL) {
6509 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6510 (mb->mbxCommand != MBX_KILL_BOARD)) {
6511 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6512 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6513 /* Mbox command <mbxCommand> cannot issue */
6514 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6515 "(%d):2531 Mailbox command x%x "
6516 "cannot issue Data: x%x x%x\n",
6517 pmbox->vport ? pmbox->vport->vpi : 0,
6518 pmbox->u.mb.mbxCommand,
6519 psli->sli_flag, flag);
6520 goto out_not_finished;
6522 /* timeout active mbox command */
6523 mod_timer(&psli->mbox_tmo, (jiffies +
6524 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
6527 /* Mailbox cmd <cmd> issue */
6528 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6529 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6530 "x%x\n",
6531 pmbox->vport ? pmbox->vport->vpi : 0,
6532 mb->mbxCommand, phba->pport->port_state,
6533 psli->sli_flag, flag);
6535 if (mb->mbxCommand != MBX_HEARTBEAT) {
6536 if (pmbox->vport) {
6537 lpfc_debugfs_disc_trc(pmbox->vport,
6538 LPFC_DISC_TRC_MBOX_VPORT,
6539 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6540 (uint32_t)mb->mbxCommand,
6541 mb->un.varWords[0], mb->un.varWords[1]);
6543 else {
6544 lpfc_debugfs_disc_trc(phba->pport,
6545 LPFC_DISC_TRC_MBOX,
6546 "MBOX Send: cmd:x%x mb:x%x x%x",
6547 (uint32_t)mb->mbxCommand,
6548 mb->un.varWords[0], mb->un.varWords[1]);
6552 psli->slistat.mbox_cmd++;
6553 evtctr = psli->slistat.mbox_event;
6555 /* next set own bit for the adapter and copy over command word */
6556 mb->mbxOwner = OWN_CHIP;
6558 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6559 /* Populate mbox extension offset word. */
6560 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6561 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6562 = (uint8_t *)phba->mbox_ext
6563 - (uint8_t *)phba->mbox;
6566 /* Copy the mailbox extension data */
6567 if (pmbox->in_ext_byte_len && pmbox->context2) {
6568 lpfc_sli_pcimem_bcopy(pmbox->context2,
6569 (uint8_t *)phba->mbox_ext,
6570 pmbox->in_ext_byte_len);
6572 /* Copy command data to host SLIM area */
6573 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6574 } else {
6575 /* Populate mbox extension offset word. */
6576 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6577 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6578 = MAILBOX_HBA_EXT_OFFSET;
6580 /* Copy the mailbox extension data */
6581 if (pmbox->in_ext_byte_len && pmbox->context2) {
6582 lpfc_memcpy_to_slim(phba->MBslimaddr +
6583 MAILBOX_HBA_EXT_OFFSET,
6584 pmbox->context2, pmbox->in_ext_byte_len);
6587 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6588 /* copy command data into host mbox for cmpl */
6589 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
6592 /* First copy mbox command data to HBA SLIM, skip past first
6593 word */
6594 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6595 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6596 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6598 /* Next copy over first word, with mbxOwner set */
6599 ldata = *((uint32_t *)mb);
6600 to_slim = phba->MBslimaddr;
6601 writel(ldata, to_slim);
6602 readl(to_slim); /* flush */
6604 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6605 /* switch over to host mailbox */
6606 psli->sli_flag |= LPFC_SLI_ACTIVE;
6610 wmb();
6612 switch (flag) {
6613 case MBX_NOWAIT:
6614 /* Set up reference to mailbox command */
6615 psli->mbox_active = pmbox;
6616 /* Interrupt board to do it */
6617 writel(CA_MBATT, phba->CAregaddr);
6618 readl(phba->CAregaddr); /* flush */
6619 /* Don't wait for it to finish, just return */
6620 break;
6622 case MBX_POLL:
6623 /* Set up null reference to mailbox command */
6624 psli->mbox_active = NULL;
6625 /* Interrupt board to do it */
6626 writel(CA_MBATT, phba->CAregaddr);
6627 readl(phba->CAregaddr); /* flush */
6629 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6630 /* First read mbox status word */
6631 word0 = *((uint32_t *)phba->mbox);
6632 word0 = le32_to_cpu(word0);
6633 } else {
6634 /* First read mbox status word */
6635 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6636 spin_unlock_irqrestore(&phba->hbalock,
6637 drvr_flag);
6638 goto out_not_finished;
6642 /* Read the HBA Host Attention Register */
6643 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6644 spin_unlock_irqrestore(&phba->hbalock,
6645 drvr_flag);
6646 goto out_not_finished;
6648 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6649 1000) + jiffies;
6650 i = 0;
6651 /* Wait for command to complete */
6652 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6653 (!(ha_copy & HA_MBATT) &&
6654 (phba->link_state > LPFC_WARM_START))) {
6655 if (time_after(jiffies, timeout)) {
6656 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6657 spin_unlock_irqrestore(&phba->hbalock,
6658 drvr_flag);
6659 goto out_not_finished;
6662 /* Check if we took a mbox interrupt while we were
6663 polling */
6664 if (((word0 & OWN_CHIP) != OWN_CHIP)
6665 && (evtctr != psli->slistat.mbox_event))
6666 break;
6668 if (i++ > 10) {
6669 spin_unlock_irqrestore(&phba->hbalock,
6670 drvr_flag);
6671 msleep(1);
6672 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6675 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6676 /* First copy command data */
6677 word0 = *((uint32_t *)phba->mbox);
6678 word0 = le32_to_cpu(word0);
6679 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6680 MAILBOX_t *slimmb;
6681 uint32_t slimword0;
6682 /* Check real SLIM for any errors */
6683 slimword0 = readl(phba->MBslimaddr);
6684 slimmb = (MAILBOX_t *) & slimword0;
6685 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6686 && slimmb->mbxStatus) {
6687 psli->sli_flag &=
6688 ~LPFC_SLI_ACTIVE;
6689 word0 = slimword0;
6692 } else {
6693 /* First copy command data */
6694 word0 = readl(phba->MBslimaddr);
6696 /* Read the HBA Host Attention Register */
6697 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6698 spin_unlock_irqrestore(&phba->hbalock,
6699 drvr_flag);
6700 goto out_not_finished;
6704 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6705 /* copy results back to user */
6706 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
6707 /* Copy the mailbox extension data */
6708 if (pmbox->out_ext_byte_len && pmbox->context2) {
6709 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6710 pmbox->context2,
6711 pmbox->out_ext_byte_len);
6713 } else {
6714 /* First copy command data */
6715 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6716 MAILBOX_CMD_SIZE);
6717 /* Copy the mailbox extension data */
6718 if (pmbox->out_ext_byte_len && pmbox->context2) {
6719 lpfc_memcpy_from_slim(pmbox->context2,
6720 phba->MBslimaddr +
6721 MAILBOX_HBA_EXT_OFFSET,
6722 pmbox->out_ext_byte_len);
6726 writel(HA_MBATT, phba->HAregaddr);
6727 readl(phba->HAregaddr); /* flush */
6729 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6730 status = mb->mbxStatus;
6733 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6734 return status;
6736 out_not_finished:
6737 if (processing_queue) {
6738 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
6739 lpfc_mbox_cmpl_put(phba, pmbox);
6741 return MBX_NOT_FINISHED;
6745 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6746 * @phba: Pointer to HBA context object.
6748 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6749 * the driver internal pending mailbox queue. It will then try to wait out the
6750 * possible outstanding mailbox command before return.
6752 * Returns:
6753 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6754 * the outstanding mailbox command timed out.
6756 static int
6757 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6759 struct lpfc_sli *psli = &phba->sli;
6760 int rc = 0;
6761 unsigned long timeout = 0;
6763 /* Mark the asynchronous mailbox command posting as blocked */
6764 spin_lock_irq(&phba->hbalock);
6765 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
6766 /* Determine how long we might wait for the active mailbox
6767 * command to be gracefully completed by firmware.
6769 if (phba->sli.mbox_active)
6770 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6771 phba->sli.mbox_active) *
6772 1000) + jiffies;
6773 spin_unlock_irq(&phba->hbalock);
6775 /* Wait for the outstnading mailbox command to complete */
6776 while (phba->sli.mbox_active) {
6777 /* Check active mailbox complete status every 2ms */
6778 msleep(2);
6779 if (time_after(jiffies, timeout)) {
6780 /* Timeout, marked the outstanding cmd not complete */
6781 rc = 1;
6782 break;
6786 /* Can not cleanly block async mailbox command, fails it */
6787 if (rc) {
6788 spin_lock_irq(&phba->hbalock);
6789 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6790 spin_unlock_irq(&phba->hbalock);
6792 return rc;
6796 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6797 * @phba: Pointer to HBA context object.
6799 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6800 * commands from the driver internal pending mailbox queue. It makes sure
6801 * that there is no outstanding mailbox command before resuming posting
6802 * asynchronous mailbox commands. If, for any reason, there is outstanding
6803 * mailbox command, it will try to wait it out before resuming asynchronous
6804 * mailbox command posting.
6806 static void
6807 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6809 struct lpfc_sli *psli = &phba->sli;
6811 spin_lock_irq(&phba->hbalock);
6812 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6813 /* Asynchronous mailbox posting is not blocked, do nothing */
6814 spin_unlock_irq(&phba->hbalock);
6815 return;
6818 /* Outstanding synchronous mailbox command is guaranteed to be done,
6819 * successful or timeout, after timing-out the outstanding mailbox
6820 * command shall always be removed, so just unblock posting async
6821 * mailbox command and resume
6823 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6824 spin_unlock_irq(&phba->hbalock);
6826 /* wake up worker thread to post asynchronlous mailbox command */
6827 lpfc_worker_wake_up(phba);
6831 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6832 * @phba: Pointer to HBA context object.
6833 * @mboxq: Pointer to mailbox object.
6835 * The function posts a mailbox to the port. The mailbox is expected
6836 * to be comletely filled in and ready for the port to operate on it.
6837 * This routine executes a synchronous completion operation on the
6838 * mailbox by polling for its completion.
6840 * The caller must not be holding any locks when calling this routine.
6842 * Returns:
6843 * MBX_SUCCESS - mailbox posted successfully
6844 * Any of the MBX error values.
6846 static int
6847 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6849 int rc = MBX_SUCCESS;
6850 unsigned long iflag;
6851 uint32_t db_ready;
6852 uint32_t mcqe_status;
6853 uint32_t mbx_cmnd;
6854 unsigned long timeout;
6855 struct lpfc_sli *psli = &phba->sli;
6856 struct lpfc_mqe *mb = &mboxq->u.mqe;
6857 struct lpfc_bmbx_create *mbox_rgn;
6858 struct dma_address *dma_address;
6859 struct lpfc_register bmbx_reg;
6862 * Only one mailbox can be active to the bootstrap mailbox region
6863 * at a time and there is no queueing provided.
6865 spin_lock_irqsave(&phba->hbalock, iflag);
6866 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6867 spin_unlock_irqrestore(&phba->hbalock, iflag);
6868 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6869 "(%d):2532 Mailbox command x%x (x%x/x%x) "
6870 "cannot issue Data: x%x x%x\n",
6871 mboxq->vport ? mboxq->vport->vpi : 0,
6872 mboxq->u.mb.mbxCommand,
6873 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6874 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6875 psli->sli_flag, MBX_POLL);
6876 return MBXERR_ERROR;
6878 /* The server grabs the token and owns it until release */
6879 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6880 phba->sli.mbox_active = mboxq;
6881 spin_unlock_irqrestore(&phba->hbalock, iflag);
6884 * Initialize the bootstrap memory region to avoid stale data areas
6885 * in the mailbox post. Then copy the caller's mailbox contents to
6886 * the bmbx mailbox region.
6888 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6889 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6890 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6891 sizeof(struct lpfc_mqe));
6893 /* Post the high mailbox dma address to the port and wait for ready. */
6894 dma_address = &phba->sli4_hba.bmbx.dma_address;
6895 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6897 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
6898 * 1000) + jiffies;
6899 do {
6900 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6901 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6902 if (!db_ready)
6903 msleep(2);
6905 if (time_after(jiffies, timeout)) {
6906 rc = MBXERR_ERROR;
6907 goto exit;
6909 } while (!db_ready);
6911 /* Post the low mailbox dma address to the port. */
6912 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
6913 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
6914 * 1000) + jiffies;
6915 do {
6916 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6917 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6918 if (!db_ready)
6919 msleep(2);
6921 if (time_after(jiffies, timeout)) {
6922 rc = MBXERR_ERROR;
6923 goto exit;
6925 } while (!db_ready);
6928 * Read the CQ to ensure the mailbox has completed.
6929 * If so, update the mailbox status so that the upper layers
6930 * can complete the request normally.
6932 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6933 sizeof(struct lpfc_mqe));
6934 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6935 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6936 sizeof(struct lpfc_mcqe));
6937 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
6939 * When the CQE status indicates a failure and the mailbox status
6940 * indicates success then copy the CQE status into the mailbox status
6941 * (and prefix it with x4000).
6943 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
6944 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6945 bf_set(lpfc_mqe_status, mb,
6946 (LPFC_MBX_ERROR_RANGE | mcqe_status));
6947 rc = MBXERR_ERROR;
6948 } else
6949 lpfc_sli4_swap_str(phba, mboxq);
6951 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6952 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
6953 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6954 " x%x x%x CQ: x%x x%x x%x x%x\n",
6955 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6956 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6957 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6958 bf_get(lpfc_mqe_status, mb),
6959 mb->un.mb_words[0], mb->un.mb_words[1],
6960 mb->un.mb_words[2], mb->un.mb_words[3],
6961 mb->un.mb_words[4], mb->un.mb_words[5],
6962 mb->un.mb_words[6], mb->un.mb_words[7],
6963 mb->un.mb_words[8], mb->un.mb_words[9],
6964 mb->un.mb_words[10], mb->un.mb_words[11],
6965 mb->un.mb_words[12], mboxq->mcqe.word0,
6966 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6967 mboxq->mcqe.trailer);
6968 exit:
6969 /* We are holding the token, no needed for lock when release */
6970 spin_lock_irqsave(&phba->hbalock, iflag);
6971 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6972 phba->sli.mbox_active = NULL;
6973 spin_unlock_irqrestore(&phba->hbalock, iflag);
6974 return rc;
6978 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6979 * @phba: Pointer to HBA context object.
6980 * @pmbox: Pointer to mailbox object.
6981 * @flag: Flag indicating how the mailbox need to be processed.
6983 * This function is called by discovery code and HBA management code to submit
6984 * a mailbox command to firmware with SLI-4 interface spec.
6986 * Return codes the caller owns the mailbox command after the return of the
6987 * function.
6989 static int
6990 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
6991 uint32_t flag)
6993 struct lpfc_sli *psli = &phba->sli;
6994 unsigned long iflags;
6995 int rc;
6997 /* dump from issue mailbox command if setup */
6998 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7000 rc = lpfc_mbox_dev_check(phba);
7001 if (unlikely(rc)) {
7002 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7003 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7004 "cannot issue Data: x%x x%x\n",
7005 mboxq->vport ? mboxq->vport->vpi : 0,
7006 mboxq->u.mb.mbxCommand,
7007 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7008 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7009 psli->sli_flag, flag);
7010 goto out_not_finished;
7013 /* Detect polling mode and jump to a handler */
7014 if (!phba->sli4_hba.intr_enable) {
7015 if (flag == MBX_POLL)
7016 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7017 else
7018 rc = -EIO;
7019 if (rc != MBX_SUCCESS)
7020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7021 "(%d):2541 Mailbox command x%x "
7022 "(x%x/x%x) cannot issue Data: "
7023 "x%x x%x\n",
7024 mboxq->vport ? mboxq->vport->vpi : 0,
7025 mboxq->u.mb.mbxCommand,
7026 lpfc_sli_config_mbox_subsys_get(phba,
7027 mboxq),
7028 lpfc_sli_config_mbox_opcode_get(phba,
7029 mboxq),
7030 psli->sli_flag, flag);
7031 return rc;
7032 } else if (flag == MBX_POLL) {
7033 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7034 "(%d):2542 Try to issue mailbox command "
7035 "x%x (x%x/x%x) synchronously ahead of async"
7036 "mailbox command queue: x%x x%x\n",
7037 mboxq->vport ? mboxq->vport->vpi : 0,
7038 mboxq->u.mb.mbxCommand,
7039 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7040 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7041 psli->sli_flag, flag);
7042 /* Try to block the asynchronous mailbox posting */
7043 rc = lpfc_sli4_async_mbox_block(phba);
7044 if (!rc) {
7045 /* Successfully blocked, now issue sync mbox cmd */
7046 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7047 if (rc != MBX_SUCCESS)
7048 lpfc_printf_log(phba, KERN_ERR,
7049 LOG_MBOX | LOG_SLI,
7050 "(%d):2597 Mailbox command "
7051 "x%x (x%x/x%x) cannot issue "
7052 "Data: x%x x%x\n",
7053 mboxq->vport ?
7054 mboxq->vport->vpi : 0,
7055 mboxq->u.mb.mbxCommand,
7056 lpfc_sli_config_mbox_subsys_get(phba,
7057 mboxq),
7058 lpfc_sli_config_mbox_opcode_get(phba,
7059 mboxq),
7060 psli->sli_flag, flag);
7061 /* Unblock the async mailbox posting afterward */
7062 lpfc_sli4_async_mbox_unblock(phba);
7064 return rc;
7067 /* Now, interrupt mode asynchrous mailbox command */
7068 rc = lpfc_mbox_cmd_check(phba, mboxq);
7069 if (rc) {
7070 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7071 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7072 "cannot issue Data: x%x x%x\n",
7073 mboxq->vport ? mboxq->vport->vpi : 0,
7074 mboxq->u.mb.mbxCommand,
7075 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7076 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7077 psli->sli_flag, flag);
7078 goto out_not_finished;
7081 /* Put the mailbox command to the driver internal FIFO */
7082 psli->slistat.mbox_busy++;
7083 spin_lock_irqsave(&phba->hbalock, iflags);
7084 lpfc_mbox_put(phba, mboxq);
7085 spin_unlock_irqrestore(&phba->hbalock, iflags);
7086 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7087 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7088 "x%x (x%x/x%x) x%x x%x x%x\n",
7089 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7090 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7091 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7092 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7093 phba->pport->port_state,
7094 psli->sli_flag, MBX_NOWAIT);
7095 /* Wake up worker thread to transport mailbox command from head */
7096 lpfc_worker_wake_up(phba);
7098 return MBX_BUSY;
7100 out_not_finished:
7101 return MBX_NOT_FINISHED;
7105 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7106 * @phba: Pointer to HBA context object.
7108 * This function is called by worker thread to send a mailbox command to
7109 * SLI4 HBA firmware.
7113 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7115 struct lpfc_sli *psli = &phba->sli;
7116 LPFC_MBOXQ_t *mboxq;
7117 int rc = MBX_SUCCESS;
7118 unsigned long iflags;
7119 struct lpfc_mqe *mqe;
7120 uint32_t mbx_cmnd;
7122 /* Check interrupt mode before post async mailbox command */
7123 if (unlikely(!phba->sli4_hba.intr_enable))
7124 return MBX_NOT_FINISHED;
7126 /* Check for mailbox command service token */
7127 spin_lock_irqsave(&phba->hbalock, iflags);
7128 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7129 spin_unlock_irqrestore(&phba->hbalock, iflags);
7130 return MBX_NOT_FINISHED;
7132 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7133 spin_unlock_irqrestore(&phba->hbalock, iflags);
7134 return MBX_NOT_FINISHED;
7136 if (unlikely(phba->sli.mbox_active)) {
7137 spin_unlock_irqrestore(&phba->hbalock, iflags);
7138 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7139 "0384 There is pending active mailbox cmd\n");
7140 return MBX_NOT_FINISHED;
7142 /* Take the mailbox command service token */
7143 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7145 /* Get the next mailbox command from head of queue */
7146 mboxq = lpfc_mbox_get(phba);
7148 /* If no more mailbox command waiting for post, we're done */
7149 if (!mboxq) {
7150 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7151 spin_unlock_irqrestore(&phba->hbalock, iflags);
7152 return MBX_SUCCESS;
7154 phba->sli.mbox_active = mboxq;
7155 spin_unlock_irqrestore(&phba->hbalock, iflags);
7157 /* Check device readiness for posting mailbox command */
7158 rc = lpfc_mbox_dev_check(phba);
7159 if (unlikely(rc))
7160 /* Driver clean routine will clean up pending mailbox */
7161 goto out_not_finished;
7163 /* Prepare the mbox command to be posted */
7164 mqe = &mboxq->u.mqe;
7165 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7167 /* Start timer for the mbox_tmo and log some mailbox post messages */
7168 mod_timer(&psli->mbox_tmo, (jiffies +
7169 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
7171 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7172 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7173 "x%x x%x\n",
7174 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7175 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7176 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7177 phba->pport->port_state, psli->sli_flag);
7179 if (mbx_cmnd != MBX_HEARTBEAT) {
7180 if (mboxq->vport) {
7181 lpfc_debugfs_disc_trc(mboxq->vport,
7182 LPFC_DISC_TRC_MBOX_VPORT,
7183 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7184 mbx_cmnd, mqe->un.mb_words[0],
7185 mqe->un.mb_words[1]);
7186 } else {
7187 lpfc_debugfs_disc_trc(phba->pport,
7188 LPFC_DISC_TRC_MBOX,
7189 "MBOX Send: cmd:x%x mb:x%x x%x",
7190 mbx_cmnd, mqe->un.mb_words[0],
7191 mqe->un.mb_words[1]);
7194 psli->slistat.mbox_cmd++;
7196 /* Post the mailbox command to the port */
7197 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7198 if (rc != MBX_SUCCESS) {
7199 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7200 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7201 "cannot issue Data: x%x x%x\n",
7202 mboxq->vport ? mboxq->vport->vpi : 0,
7203 mboxq->u.mb.mbxCommand,
7204 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7205 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7206 psli->sli_flag, MBX_NOWAIT);
7207 goto out_not_finished;
7210 return rc;
7212 out_not_finished:
7213 spin_lock_irqsave(&phba->hbalock, iflags);
7214 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7215 __lpfc_mbox_cmpl_put(phba, mboxq);
7216 /* Release the token */
7217 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7218 phba->sli.mbox_active = NULL;
7219 spin_unlock_irqrestore(&phba->hbalock, iflags);
7221 return MBX_NOT_FINISHED;
7225 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7226 * @phba: Pointer to HBA context object.
7227 * @pmbox: Pointer to mailbox object.
7228 * @flag: Flag indicating how the mailbox need to be processed.
7230 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7231 * the API jump table function pointer from the lpfc_hba struct.
7233 * Return codes the caller owns the mailbox command after the return of the
7234 * function.
7237 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7239 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7243 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7244 * @phba: The hba struct for which this call is being executed.
7245 * @dev_grp: The HBA PCI-Device group number.
7247 * This routine sets up the mbox interface API function jump table in @phba
7248 * struct.
7249 * Returns: 0 - success, -ENODEV - failure.
7252 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7255 switch (dev_grp) {
7256 case LPFC_PCI_DEV_LP:
7257 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7258 phba->lpfc_sli_handle_slow_ring_event =
7259 lpfc_sli_handle_slow_ring_event_s3;
7260 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7261 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7262 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7263 break;
7264 case LPFC_PCI_DEV_OC:
7265 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7266 phba->lpfc_sli_handle_slow_ring_event =
7267 lpfc_sli_handle_slow_ring_event_s4;
7268 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7269 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7270 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7271 break;
7272 default:
7273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7274 "1420 Invalid HBA PCI-device group: 0x%x\n",
7275 dev_grp);
7276 return -ENODEV;
7277 break;
7279 return 0;
7283 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7284 * @phba: Pointer to HBA context object.
7285 * @pring: Pointer to driver SLI ring object.
7286 * @piocb: Pointer to address of newly added command iocb.
7288 * This function is called with hbalock held to add a command
7289 * iocb to the txq when SLI layer cannot submit the command iocb
7290 * to the ring.
7292 void
7293 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7294 struct lpfc_iocbq *piocb)
7296 /* Insert the caller's iocb in the txq tail for later processing. */
7297 list_add_tail(&piocb->list, &pring->txq);
7298 pring->txq_cnt++;
7302 * lpfc_sli_next_iocb - Get the next iocb in the txq
7303 * @phba: Pointer to HBA context object.
7304 * @pring: Pointer to driver SLI ring object.
7305 * @piocb: Pointer to address of newly added command iocb.
7307 * This function is called with hbalock held before a new
7308 * iocb is submitted to the firmware. This function checks
7309 * txq to flush the iocbs in txq to Firmware before
7310 * submitting new iocbs to the Firmware.
7311 * If there are iocbs in the txq which need to be submitted
7312 * to firmware, lpfc_sli_next_iocb returns the first element
7313 * of the txq after dequeuing it from txq.
7314 * If there is no iocb in the txq then the function will return
7315 * *piocb and *piocb is set to NULL. Caller needs to check
7316 * *piocb to find if there are more commands in the txq.
7318 static struct lpfc_iocbq *
7319 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7320 struct lpfc_iocbq **piocb)
7322 struct lpfc_iocbq * nextiocb;
7324 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7325 if (!nextiocb) {
7326 nextiocb = *piocb;
7327 *piocb = NULL;
7330 return nextiocb;
7334 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7335 * @phba: Pointer to HBA context object.
7336 * @ring_number: SLI ring number to issue iocb on.
7337 * @piocb: Pointer to command iocb.
7338 * @flag: Flag indicating if this command can be put into txq.
7340 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7341 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7342 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7343 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7344 * this function allows only iocbs for posting buffers. This function finds
7345 * next available slot in the command ring and posts the command to the
7346 * available slot and writes the port attention register to request HBA start
7347 * processing new iocb. If there is no slot available in the ring and
7348 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7349 * the function returns IOCB_BUSY.
7351 * This function is called with hbalock held. The function will return success
7352 * after it successfully submit the iocb to firmware or after adding to the
7353 * txq.
7355 static int
7356 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7357 struct lpfc_iocbq *piocb, uint32_t flag)
7359 struct lpfc_iocbq *nextiocb;
7360 IOCB_t *iocb;
7361 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7363 if (piocb->iocb_cmpl && (!piocb->vport) &&
7364 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7365 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7366 lpfc_printf_log(phba, KERN_ERR,
7367 LOG_SLI | LOG_VPORT,
7368 "1807 IOCB x%x failed. No vport\n",
7369 piocb->iocb.ulpCommand);
7370 dump_stack();
7371 return IOCB_ERROR;
7375 /* If the PCI channel is in offline state, do not post iocbs. */
7376 if (unlikely(pci_channel_offline(phba->pcidev)))
7377 return IOCB_ERROR;
7379 /* If HBA has a deferred error attention, fail the iocb. */
7380 if (unlikely(phba->hba_flag & DEFER_ERATT))
7381 return IOCB_ERROR;
7384 * We should never get an IOCB if we are in a < LINK_DOWN state
7386 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7387 return IOCB_ERROR;
7390 * Check to see if we are blocking IOCB processing because of a
7391 * outstanding event.
7393 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7394 goto iocb_busy;
7396 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7398 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7399 * can be issued if the link is not up.
7401 switch (piocb->iocb.ulpCommand) {
7402 case CMD_GEN_REQUEST64_CR:
7403 case CMD_GEN_REQUEST64_CX:
7404 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7405 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7406 FC_RCTL_DD_UNSOL_CMD) ||
7407 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7408 MENLO_TRANSPORT_TYPE))
7410 goto iocb_busy;
7411 break;
7412 case CMD_QUE_RING_BUF_CN:
7413 case CMD_QUE_RING_BUF64_CN:
7415 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7416 * completion, iocb_cmpl MUST be 0.
7418 if (piocb->iocb_cmpl)
7419 piocb->iocb_cmpl = NULL;
7420 /*FALLTHROUGH*/
7421 case CMD_CREATE_XRI_CR:
7422 case CMD_CLOSE_XRI_CN:
7423 case CMD_CLOSE_XRI_CX:
7424 break;
7425 default:
7426 goto iocb_busy;
7430 * For FCP commands, we must be in a state where we can process link
7431 * attention events.
7433 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7434 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7435 goto iocb_busy;
7438 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7439 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7440 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7442 if (iocb)
7443 lpfc_sli_update_ring(phba, pring);
7444 else
7445 lpfc_sli_update_full_ring(phba, pring);
7447 if (!piocb)
7448 return IOCB_SUCCESS;
7450 goto out_busy;
7452 iocb_busy:
7453 pring->stats.iocb_cmd_delay++;
7455 out_busy:
7457 if (!(flag & SLI_IOCB_RET_IOCB)) {
7458 __lpfc_sli_ringtx_put(phba, pring, piocb);
7459 return IOCB_SUCCESS;
7462 return IOCB_BUSY;
7466 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7467 * @phba: Pointer to HBA context object.
7468 * @piocb: Pointer to command iocb.
7469 * @sglq: Pointer to the scatter gather queue object.
7471 * This routine converts the bpl or bde that is in the IOCB
7472 * to a sgl list for the sli4 hardware. The physical address
7473 * of the bpl/bde is converted back to a virtual address.
7474 * If the IOCB contains a BPL then the list of BDE's is
7475 * converted to sli4_sge's. If the IOCB contains a single
7476 * BDE then it is converted to a single sli_sge.
7477 * The IOCB is still in cpu endianess so the contents of
7478 * the bpl can be used without byte swapping.
7480 * Returns valid XRI = Success, NO_XRI = Failure.
7482 static uint16_t
7483 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7484 struct lpfc_sglq *sglq)
7486 uint16_t xritag = NO_XRI;
7487 struct ulp_bde64 *bpl = NULL;
7488 struct ulp_bde64 bde;
7489 struct sli4_sge *sgl = NULL;
7490 IOCB_t *icmd;
7491 int numBdes = 0;
7492 int i = 0;
7493 uint32_t offset = 0; /* accumulated offset in the sg request list */
7494 int inbound = 0; /* number of sg reply entries inbound from firmware */
7496 if (!piocbq || !sglq)
7497 return xritag;
7499 sgl = (struct sli4_sge *)sglq->sgl;
7500 icmd = &piocbq->iocb;
7501 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7502 numBdes = icmd->un.genreq64.bdl.bdeSize /
7503 sizeof(struct ulp_bde64);
7504 /* The addrHigh and addrLow fields within the IOCB
7505 * have not been byteswapped yet so there is no
7506 * need to swap them back.
7508 bpl = (struct ulp_bde64 *)
7509 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7511 if (!bpl)
7512 return xritag;
7514 for (i = 0; i < numBdes; i++) {
7515 /* Should already be byte swapped. */
7516 sgl->addr_hi = bpl->addrHigh;
7517 sgl->addr_lo = bpl->addrLow;
7519 sgl->word2 = le32_to_cpu(sgl->word2);
7520 if ((i+1) == numBdes)
7521 bf_set(lpfc_sli4_sge_last, sgl, 1);
7522 else
7523 bf_set(lpfc_sli4_sge_last, sgl, 0);
7524 /* swap the size field back to the cpu so we
7525 * can assign it to the sgl.
7527 bde.tus.w = le32_to_cpu(bpl->tus.w);
7528 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7529 /* The offsets in the sgl need to be accumulated
7530 * separately for the request and reply lists.
7531 * The request is always first, the reply follows.
7533 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7534 /* add up the reply sg entries */
7535 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7536 inbound++;
7537 /* first inbound? reset the offset */
7538 if (inbound == 1)
7539 offset = 0;
7540 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7541 offset += bde.tus.f.bdeSize;
7543 sgl->word2 = cpu_to_le32(sgl->word2);
7544 bpl++;
7545 sgl++;
7547 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7548 /* The addrHigh and addrLow fields of the BDE have not
7549 * been byteswapped yet so they need to be swapped
7550 * before putting them in the sgl.
7552 sgl->addr_hi =
7553 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7554 sgl->addr_lo =
7555 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7556 sgl->word2 = le32_to_cpu(sgl->word2);
7557 bf_set(lpfc_sli4_sge_last, sgl, 1);
7558 sgl->word2 = cpu_to_le32(sgl->word2);
7559 sgl->sge_len =
7560 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7562 return sglq->sli4_xritag;
7566 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7567 * @phba: Pointer to HBA context object.
7569 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7570 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7571 * held.
7573 * Return: index into SLI4 fast-path FCP queue index.
7575 static uint32_t
7576 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7578 ++phba->fcp_qidx;
7579 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7580 phba->fcp_qidx = 0;
7582 return phba->fcp_qidx;
7586 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7587 * @phba: Pointer to HBA context object.
7588 * @piocb: Pointer to command iocb.
7589 * @wqe: Pointer to the work queue entry.
7591 * This routine converts the iocb command to its Work Queue Entry
7592 * equivalent. The wqe pointer should not have any fields set when
7593 * this routine is called because it will memcpy over them.
7594 * This routine does not set the CQ_ID or the WQEC bits in the
7595 * wqe.
7597 * Returns: 0 = Success, IOCB_ERROR = Failure.
7599 static int
7600 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7601 union lpfc_wqe *wqe)
7603 uint32_t xmit_len = 0, total_len = 0;
7604 uint8_t ct = 0;
7605 uint32_t fip;
7606 uint32_t abort_tag;
7607 uint8_t command_type = ELS_COMMAND_NON_FIP;
7608 uint8_t cmnd;
7609 uint16_t xritag;
7610 uint16_t abrt_iotag;
7611 struct lpfc_iocbq *abrtiocbq;
7612 struct ulp_bde64 *bpl = NULL;
7613 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7614 int numBdes, i;
7615 struct ulp_bde64 bde;
7616 struct lpfc_nodelist *ndlp;
7618 fip = phba->hba_flag & HBA_FIP_SUPPORT;
7619 /* The fcp commands will set command type */
7620 if (iocbq->iocb_flag & LPFC_IO_FCP)
7621 command_type = FCP_COMMAND;
7622 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7623 command_type = ELS_COMMAND_FIP;
7624 else
7625 command_type = ELS_COMMAND_NON_FIP;
7627 /* Some of the fields are in the right position already */
7628 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7629 abort_tag = (uint32_t) iocbq->iotag;
7630 xritag = iocbq->sli4_xritag;
7631 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
7632 /* words0-2 bpl convert bde */
7633 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7634 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7635 sizeof(struct ulp_bde64);
7636 bpl = (struct ulp_bde64 *)
7637 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7638 if (!bpl)
7639 return IOCB_ERROR;
7641 /* Should already be byte swapped. */
7642 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7643 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7644 /* swap the size field back to the cpu so we
7645 * can assign it to the sgl.
7647 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
7648 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7649 total_len = 0;
7650 for (i = 0; i < numBdes; i++) {
7651 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7652 total_len += bde.tus.f.bdeSize;
7654 } else
7655 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
7657 iocbq->iocb.ulpIoTag = iocbq->iotag;
7658 cmnd = iocbq->iocb.ulpCommand;
7660 switch (iocbq->iocb.ulpCommand) {
7661 case CMD_ELS_REQUEST64_CR:
7662 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7663 if (!iocbq->iocb.ulpLe) {
7664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7665 "2007 Only Limited Edition cmd Format"
7666 " supported 0x%x\n",
7667 iocbq->iocb.ulpCommand);
7668 return IOCB_ERROR;
7670 wqe->els_req.payload_len = xmit_len;
7671 /* Els_reguest64 has a TMO */
7672 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7673 iocbq->iocb.ulpTimeout);
7674 /* Need a VF for word 4 set the vf bit*/
7675 bf_set(els_req64_vf, &wqe->els_req, 0);
7676 /* And a VFID for word 12 */
7677 bf_set(els_req64_vfid, &wqe->els_req, 0);
7678 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7679 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7680 iocbq->iocb.ulpContext);
7681 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7682 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
7683 /* CCP CCPE PV PRI in word10 were set in the memcpy */
7684 if (command_type == ELS_COMMAND_FIP) {
7685 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7686 >> LPFC_FIP_ELS_ID_SHIFT);
7688 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7689 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7690 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7691 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7692 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7693 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7694 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7695 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7696 break;
7697 case CMD_XMIT_SEQUENCE64_CX:
7698 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7699 iocbq->iocb.un.ulpWord[3]);
7700 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7701 iocbq->iocb.unsli3.rcvsli3.ox_id);
7702 /* The entire sequence is transmitted for this IOCB */
7703 xmit_len = total_len;
7704 cmnd = CMD_XMIT_SEQUENCE64_CR;
7705 case CMD_XMIT_SEQUENCE64_CR:
7706 /* word3 iocb=io_tag32 wqe=reserved */
7707 wqe->xmit_sequence.rsvd3 = 0;
7708 /* word4 relative_offset memcpy */
7709 /* word5 r_ctl/df_ctl memcpy */
7710 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7711 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7712 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7713 LPFC_WQE_IOD_WRITE);
7714 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7715 LPFC_WQE_LENLOC_WORD12);
7716 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
7717 wqe->xmit_sequence.xmit_len = xmit_len;
7718 command_type = OTHER_COMMAND;
7719 break;
7720 case CMD_XMIT_BCAST64_CN:
7721 /* word3 iocb=iotag32 wqe=seq_payload_len */
7722 wqe->xmit_bcast64.seq_payload_len = xmit_len;
7723 /* word4 iocb=rsvd wqe=rsvd */
7724 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7725 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
7726 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
7727 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7728 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7729 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7730 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7731 LPFC_WQE_LENLOC_WORD3);
7732 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7733 break;
7734 case CMD_FCP_IWRITE64_CR:
7735 command_type = FCP_COMMAND_DATA_OUT;
7736 /* word3 iocb=iotag wqe=payload_offset_len */
7737 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7738 wqe->fcp_iwrite.payload_offset_len =
7739 xmit_len + sizeof(struct fcp_rsp);
7740 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7741 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7742 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7743 iocbq->iocb.ulpFCP2Rcvy);
7744 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7745 /* Always open the exchange */
7746 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7747 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7748 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7749 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7750 LPFC_WQE_LENLOC_WORD4);
7751 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7752 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7753 break;
7754 case CMD_FCP_IREAD64_CR:
7755 /* word3 iocb=iotag wqe=payload_offset_len */
7756 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7757 wqe->fcp_iread.payload_offset_len =
7758 xmit_len + sizeof(struct fcp_rsp);
7759 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7760 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7761 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7762 iocbq->iocb.ulpFCP2Rcvy);
7763 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
7764 /* Always open the exchange */
7765 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
7766 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7767 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7768 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7769 LPFC_WQE_LENLOC_WORD4);
7770 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7771 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7772 break;
7773 case CMD_FCP_ICMND64_CR:
7774 /* word3 iocb=IO_TAG wqe=reserved */
7775 wqe->fcp_icmd.rsrvd3 = 0;
7776 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
7777 /* Always open the exchange */
7778 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7779 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7780 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7781 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7782 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7783 LPFC_WQE_LENLOC_NONE);
7784 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
7785 break;
7786 case CMD_GEN_REQUEST64_CR:
7787 /* For this command calculate the xmit length of the
7788 * request bde.
7790 xmit_len = 0;
7791 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7792 sizeof(struct ulp_bde64);
7793 for (i = 0; i < numBdes; i++) {
7794 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7795 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7796 break;
7797 xmit_len += bde.tus.f.bdeSize;
7799 /* word3 iocb=IO_TAG wqe=request_payload_len */
7800 wqe->gen_req.request_payload_len = xmit_len;
7801 /* word4 iocb=parameter wqe=relative_offset memcpy */
7802 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
7803 /* word6 context tag copied in memcpy */
7804 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7805 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7806 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7807 "2015 Invalid CT %x command 0x%x\n",
7808 ct, iocbq->iocb.ulpCommand);
7809 return IOCB_ERROR;
7811 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7812 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7813 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7814 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7815 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7816 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7817 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7818 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
7819 command_type = OTHER_COMMAND;
7820 break;
7821 case CMD_XMIT_ELS_RSP64_CX:
7822 ndlp = (struct lpfc_nodelist *)iocbq->context1;
7823 /* words0-2 BDE memcpy */
7824 /* word3 iocb=iotag32 wqe=response_payload_len */
7825 wqe->xmit_els_rsp.response_payload_len = xmit_len;
7826 /* word4 iocb=did wge=rsvd. */
7827 wqe->xmit_els_rsp.rsvd4 = 0;
7828 /* word5 iocb=rsvd wge=did */
7829 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7830 iocbq->iocb.un.elsreq64.remoteID);
7831 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7832 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7833 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7834 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7835 iocbq->iocb.unsli3.rcvsli3.ox_id);
7836 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
7837 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
7838 phba->vpi_ids[iocbq->vport->vpi]);
7839 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7840 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7841 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7842 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7843 LPFC_WQE_LENLOC_WORD3);
7844 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
7845 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7846 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
7847 command_type = OTHER_COMMAND;
7848 break;
7849 case CMD_CLOSE_XRI_CN:
7850 case CMD_ABORT_XRI_CN:
7851 case CMD_ABORT_XRI_CX:
7852 /* words 0-2 memcpy should be 0 rserved */
7853 /* port will send abts */
7854 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7855 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7856 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7857 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7858 } else
7859 fip = 0;
7861 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
7863 * The link is down, or the command was ELS_FIP
7864 * so the fw does not need to send abts
7865 * on the wire.
7867 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7868 else
7869 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7870 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
7871 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7872 wqe->abort_cmd.rsrvd5 = 0;
7873 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
7874 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7875 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
7877 * The abort handler will send us CMD_ABORT_XRI_CN or
7878 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7880 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7881 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7882 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7883 LPFC_WQE_LENLOC_NONE);
7884 cmnd = CMD_ABORT_XRI_CX;
7885 command_type = OTHER_COMMAND;
7886 xritag = 0;
7887 break;
7888 case CMD_XMIT_BLS_RSP64_CX:
7889 /* As BLS ABTS RSP WQE is very different from other WQEs,
7890 * we re-construct this WQE here based on information in
7891 * iocbq from scratch.
7893 memset(wqe, 0, sizeof(union lpfc_wqe));
7894 /* OX_ID is invariable to who sent ABTS to CT exchange */
7895 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
7896 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7897 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
7898 LPFC_ABTS_UNSOL_INT) {
7899 /* ABTS sent by initiator to CT exchange, the
7900 * RX_ID field will be filled with the newly
7901 * allocated responder XRI.
7903 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7904 iocbq->sli4_xritag);
7905 } else {
7906 /* ABTS sent by responder to CT exchange, the
7907 * RX_ID field will be filled with the responder
7908 * RX_ID from ABTS.
7910 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7911 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
7913 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7914 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7915 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7916 iocbq->iocb.ulpContext);
7917 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7918 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7919 LPFC_WQE_LENLOC_NONE);
7920 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7921 command_type = OTHER_COMMAND;
7922 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7923 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7924 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7925 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7926 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7927 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7928 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7931 break;
7932 case CMD_XRI_ABORTED_CX:
7933 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
7934 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7935 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7936 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7937 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7938 default:
7939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7940 "2014 Invalid command 0x%x\n",
7941 iocbq->iocb.ulpCommand);
7942 return IOCB_ERROR;
7943 break;
7946 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7947 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7948 wqe->generic.wqe_com.abort_tag = abort_tag;
7949 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7950 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7951 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7952 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
7953 return 0;
7957 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7958 * @phba: Pointer to HBA context object.
7959 * @ring_number: SLI ring number to issue iocb on.
7960 * @piocb: Pointer to command iocb.
7961 * @flag: Flag indicating if this command can be put into txq.
7963 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7964 * an iocb command to an HBA with SLI-4 interface spec.
7966 * This function is called with hbalock held. The function will return success
7967 * after it successfully submit the iocb to firmware or after adding to the
7968 * txq.
7970 static int
7971 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7972 struct lpfc_iocbq *piocb, uint32_t flag)
7974 struct lpfc_sglq *sglq;
7975 union lpfc_wqe wqe;
7976 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7978 if (piocb->sli4_xritag == NO_XRI) {
7979 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7980 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7981 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7982 sglq = NULL;
7983 else {
7984 if (pring->txq_cnt) {
7985 if (!(flag & SLI_IOCB_RET_IOCB)) {
7986 __lpfc_sli_ringtx_put(phba,
7987 pring, piocb);
7988 return IOCB_SUCCESS;
7989 } else {
7990 return IOCB_BUSY;
7992 } else {
7993 sglq = __lpfc_sli_get_sglq(phba, piocb);
7994 if (!sglq) {
7995 if (!(flag & SLI_IOCB_RET_IOCB)) {
7996 __lpfc_sli_ringtx_put(phba,
7997 pring,
7998 piocb);
7999 return IOCB_SUCCESS;
8000 } else
8001 return IOCB_BUSY;
8005 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8006 /* These IO's already have an XRI and a mapped sgl. */
8007 sglq = NULL;
8008 } else {
8010 * This is a continuation of a commandi,(CX) so this
8011 * sglq is on the active list
8013 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8014 if (!sglq)
8015 return IOCB_ERROR;
8018 if (sglq) {
8019 piocb->sli4_lxritag = sglq->sli4_lxritag;
8020 piocb->sli4_xritag = sglq->sli4_xritag;
8021 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8022 return IOCB_ERROR;
8025 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8026 return IOCB_ERROR;
8028 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8029 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8031 * For FCP command IOCB, get a new WQ index to distribute
8032 * WQE across the WQsr. On the other hand, for abort IOCB,
8033 * it carries the same WQ index to the original command
8034 * IOCB.
8036 if (piocb->iocb_flag & LPFC_IO_FCP)
8037 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8038 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8039 &wqe))
8040 return IOCB_ERROR;
8041 } else {
8042 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8043 return IOCB_ERROR;
8045 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8047 return 0;
8051 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8053 * This routine wraps the actual lockless version for issusing IOCB function
8054 * pointer from the lpfc_hba struct.
8056 * Return codes:
8057 * IOCB_ERROR - Error
8058 * IOCB_SUCCESS - Success
8059 * IOCB_BUSY - Busy
8062 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8063 struct lpfc_iocbq *piocb, uint32_t flag)
8065 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8069 * lpfc_sli_api_table_setup - Set up sli api function jump table
8070 * @phba: The hba struct for which this call is being executed.
8071 * @dev_grp: The HBA PCI-Device group number.
8073 * This routine sets up the SLI interface API function jump table in @phba
8074 * struct.
8075 * Returns: 0 - success, -ENODEV - failure.
8078 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8081 switch (dev_grp) {
8082 case LPFC_PCI_DEV_LP:
8083 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8084 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8085 break;
8086 case LPFC_PCI_DEV_OC:
8087 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8088 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8089 break;
8090 default:
8091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8092 "1419 Invalid HBA PCI-device group: 0x%x\n",
8093 dev_grp);
8094 return -ENODEV;
8095 break;
8097 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8098 return 0;
8102 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8103 * @phba: Pointer to HBA context object.
8104 * @pring: Pointer to driver SLI ring object.
8105 * @piocb: Pointer to command iocb.
8106 * @flag: Flag indicating if this command can be put into txq.
8108 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8109 * function. This function gets the hbalock and calls
8110 * __lpfc_sli_issue_iocb function and will return the error returned
8111 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8112 * functions which do not hold hbalock.
8115 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8116 struct lpfc_iocbq *piocb, uint32_t flag)
8118 unsigned long iflags;
8119 int rc;
8121 spin_lock_irqsave(&phba->hbalock, iflags);
8122 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8123 spin_unlock_irqrestore(&phba->hbalock, iflags);
8125 return rc;
8129 * lpfc_extra_ring_setup - Extra ring setup function
8130 * @phba: Pointer to HBA context object.
8132 * This function is called while driver attaches with the
8133 * HBA to setup the extra ring. The extra ring is used
8134 * only when driver needs to support target mode functionality
8135 * or IP over FC functionalities.
8137 * This function is called with no lock held.
8139 static int
8140 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8142 struct lpfc_sli *psli;
8143 struct lpfc_sli_ring *pring;
8145 psli = &phba->sli;
8147 /* Adjust cmd/rsp ring iocb entries more evenly */
8149 /* Take some away from the FCP ring */
8150 pring = &psli->ring[psli->fcp_ring];
8151 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8152 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8153 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8154 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8156 /* and give them to the extra ring */
8157 pring = &psli->ring[psli->extra_ring];
8159 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8160 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8161 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8162 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8164 /* Setup default profile for this ring */
8165 pring->iotag_max = 4096;
8166 pring->num_mask = 1;
8167 pring->prt[0].profile = 0; /* Mask 0 */
8168 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8169 pring->prt[0].type = phba->cfg_multi_ring_type;
8170 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8171 return 0;
8175 * lpfc_sli_async_event_handler - ASYNC iocb handler function
8176 * @phba: Pointer to HBA context object.
8177 * @pring: Pointer to driver SLI ring object.
8178 * @iocbq: Pointer to iocb object.
8180 * This function is called by the slow ring event handler
8181 * function when there is an ASYNC event iocb in the ring.
8182 * This function is called with no lock held.
8183 * Currently this function handles only temperature related
8184 * ASYNC events. The function decodes the temperature sensor
8185 * event message and posts events for the management applications.
8187 static void
8188 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8189 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8191 IOCB_t *icmd;
8192 uint16_t evt_code;
8193 uint16_t temp;
8194 struct temp_event temp_event_data;
8195 struct Scsi_Host *shost;
8196 uint32_t *iocb_w;
8198 icmd = &iocbq->iocb;
8199 evt_code = icmd->un.asyncstat.evt_code;
8200 temp = icmd->ulpContext;
8202 if ((evt_code != ASYNC_TEMP_WARN) &&
8203 (evt_code != ASYNC_TEMP_SAFE)) {
8204 iocb_w = (uint32_t *) icmd;
8205 lpfc_printf_log(phba,
8206 KERN_ERR,
8207 LOG_SLI,
8208 "0346 Ring %d handler: unexpected ASYNC_STATUS"
8209 " evt_code 0x%x\n"
8210 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8211 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8212 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8213 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
8214 pring->ringno,
8215 icmd->un.asyncstat.evt_code,
8216 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8217 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8218 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8219 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8221 return;
8223 temp_event_data.data = (uint32_t)temp;
8224 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8225 if (evt_code == ASYNC_TEMP_WARN) {
8226 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8227 lpfc_printf_log(phba,
8228 KERN_ERR,
8229 LOG_TEMP,
8230 "0347 Adapter is very hot, please take "
8231 "corrective action. temperature : %d Celsius\n",
8232 temp);
8234 if (evt_code == ASYNC_TEMP_SAFE) {
8235 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8236 lpfc_printf_log(phba,
8237 KERN_ERR,
8238 LOG_TEMP,
8239 "0340 Adapter temperature is OK now. "
8240 "temperature : %d Celsius\n",
8241 temp);
8244 /* Send temperature change event to applications */
8245 shost = lpfc_shost_from_vport(phba->pport);
8246 fc_host_post_vendor_event(shost, fc_get_event_number(),
8247 sizeof(temp_event_data), (char *) &temp_event_data,
8248 LPFC_NL_VENDOR_ID);
8254 * lpfc_sli_setup - SLI ring setup function
8255 * @phba: Pointer to HBA context object.
8257 * lpfc_sli_setup sets up rings of the SLI interface with
8258 * number of iocbs per ring and iotags. This function is
8259 * called while driver attach to the HBA and before the
8260 * interrupts are enabled. So there is no need for locking.
8262 * This function always returns 0.
8265 lpfc_sli_setup(struct lpfc_hba *phba)
8267 int i, totiocbsize = 0;
8268 struct lpfc_sli *psli = &phba->sli;
8269 struct lpfc_sli_ring *pring;
8271 psli->num_rings = MAX_CONFIGURED_RINGS;
8272 psli->sli_flag = 0;
8273 psli->fcp_ring = LPFC_FCP_RING;
8274 psli->next_ring = LPFC_FCP_NEXT_RING;
8275 psli->extra_ring = LPFC_EXTRA_RING;
8277 psli->iocbq_lookup = NULL;
8278 psli->iocbq_lookup_len = 0;
8279 psli->last_iotag = 0;
8281 for (i = 0; i < psli->num_rings; i++) {
8282 pring = &psli->ring[i];
8283 switch (i) {
8284 case LPFC_FCP_RING: /* ring 0 - FCP */
8285 /* numCiocb and numRiocb are used in config_port */
8286 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8287 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8288 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8289 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8290 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8291 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8292 pring->sizeCiocb = (phba->sli_rev == 3) ?
8293 SLI3_IOCB_CMD_SIZE :
8294 SLI2_IOCB_CMD_SIZE;
8295 pring->sizeRiocb = (phba->sli_rev == 3) ?
8296 SLI3_IOCB_RSP_SIZE :
8297 SLI2_IOCB_RSP_SIZE;
8298 pring->iotag_ctr = 0;
8299 pring->iotag_max =
8300 (phba->cfg_hba_queue_depth * 2);
8301 pring->fast_iotag = pring->iotag_max;
8302 pring->num_mask = 0;
8303 break;
8304 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
8305 /* numCiocb and numRiocb are used in config_port */
8306 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8307 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8308 pring->sizeCiocb = (phba->sli_rev == 3) ?
8309 SLI3_IOCB_CMD_SIZE :
8310 SLI2_IOCB_CMD_SIZE;
8311 pring->sizeRiocb = (phba->sli_rev == 3) ?
8312 SLI3_IOCB_RSP_SIZE :
8313 SLI2_IOCB_RSP_SIZE;
8314 pring->iotag_max = phba->cfg_hba_queue_depth;
8315 pring->num_mask = 0;
8316 break;
8317 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8318 /* numCiocb and numRiocb are used in config_port */
8319 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8320 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8321 pring->sizeCiocb = (phba->sli_rev == 3) ?
8322 SLI3_IOCB_CMD_SIZE :
8323 SLI2_IOCB_CMD_SIZE;
8324 pring->sizeRiocb = (phba->sli_rev == 3) ?
8325 SLI3_IOCB_RSP_SIZE :
8326 SLI2_IOCB_RSP_SIZE;
8327 pring->fast_iotag = 0;
8328 pring->iotag_ctr = 0;
8329 pring->iotag_max = 4096;
8330 pring->lpfc_sli_rcv_async_status =
8331 lpfc_sli_async_event_handler;
8332 pring->num_mask = LPFC_MAX_RING_MASK;
8333 pring->prt[0].profile = 0; /* Mask 0 */
8334 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8335 pring->prt[0].type = FC_TYPE_ELS;
8336 pring->prt[0].lpfc_sli_rcv_unsol_event =
8337 lpfc_els_unsol_event;
8338 pring->prt[1].profile = 0; /* Mask 1 */
8339 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8340 pring->prt[1].type = FC_TYPE_ELS;
8341 pring->prt[1].lpfc_sli_rcv_unsol_event =
8342 lpfc_els_unsol_event;
8343 pring->prt[2].profile = 0; /* Mask 2 */
8344 /* NameServer Inquiry */
8345 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
8346 /* NameServer */
8347 pring->prt[2].type = FC_TYPE_CT;
8348 pring->prt[2].lpfc_sli_rcv_unsol_event =
8349 lpfc_ct_unsol_event;
8350 pring->prt[3].profile = 0; /* Mask 3 */
8351 /* NameServer response */
8352 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
8353 /* NameServer */
8354 pring->prt[3].type = FC_TYPE_CT;
8355 pring->prt[3].lpfc_sli_rcv_unsol_event =
8356 lpfc_ct_unsol_event;
8357 /* abort unsolicited sequence */
8358 pring->prt[4].profile = 0; /* Mask 4 */
8359 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8360 pring->prt[4].type = FC_TYPE_BLS;
8361 pring->prt[4].lpfc_sli_rcv_unsol_event =
8362 lpfc_sli4_ct_abort_unsol_event;
8363 break;
8365 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
8366 (pring->numRiocb * pring->sizeRiocb);
8368 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
8369 /* Too many cmd / rsp ring entries in SLI2 SLIM */
8370 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8371 "SLI2 SLIM Data: x%x x%lx\n",
8372 phba->brd_no, totiocbsize,
8373 (unsigned long) MAX_SLIM_IOCB_SIZE);
8375 if (phba->cfg_multi_ring_support == 2)
8376 lpfc_extra_ring_setup(phba);
8378 return 0;
8382 * lpfc_sli_queue_setup - Queue initialization function
8383 * @phba: Pointer to HBA context object.
8385 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8386 * ring. This function also initializes ring indices of each ring.
8387 * This function is called during the initialization of the SLI
8388 * interface of an HBA.
8389 * This function is called with no lock held and always returns
8390 * 1.
8393 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8395 struct lpfc_sli *psli;
8396 struct lpfc_sli_ring *pring;
8397 int i;
8399 psli = &phba->sli;
8400 spin_lock_irq(&phba->hbalock);
8401 INIT_LIST_HEAD(&psli->mboxq);
8402 INIT_LIST_HEAD(&psli->mboxq_cmpl);
8403 /* Initialize list headers for txq and txcmplq as double linked lists */
8404 for (i = 0; i < psli->num_rings; i++) {
8405 pring = &psli->ring[i];
8406 pring->ringno = i;
8407 pring->next_cmdidx = 0;
8408 pring->local_getidx = 0;
8409 pring->cmdidx = 0;
8410 INIT_LIST_HEAD(&pring->txq);
8411 INIT_LIST_HEAD(&pring->txcmplq);
8412 INIT_LIST_HEAD(&pring->iocb_continueq);
8413 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
8414 INIT_LIST_HEAD(&pring->postbufq);
8416 spin_unlock_irq(&phba->hbalock);
8417 return 1;
8421 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8422 * @phba: Pointer to HBA context object.
8424 * This routine flushes the mailbox command subsystem. It will unconditionally
8425 * flush all the mailbox commands in the three possible stages in the mailbox
8426 * command sub-system: pending mailbox command queue; the outstanding mailbox
8427 * command; and completed mailbox command queue. It is caller's responsibility
8428 * to make sure that the driver is in the proper state to flush the mailbox
8429 * command sub-system. Namely, the posting of mailbox commands into the
8430 * pending mailbox command queue from the various clients must be stopped;
8431 * either the HBA is in a state that it will never works on the outstanding
8432 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8433 * mailbox command has been completed.
8435 static void
8436 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8438 LIST_HEAD(completions);
8439 struct lpfc_sli *psli = &phba->sli;
8440 LPFC_MBOXQ_t *pmb;
8441 unsigned long iflag;
8443 /* Flush all the mailbox commands in the mbox system */
8444 spin_lock_irqsave(&phba->hbalock, iflag);
8445 /* The pending mailbox command queue */
8446 list_splice_init(&phba->sli.mboxq, &completions);
8447 /* The outstanding active mailbox command */
8448 if (psli->mbox_active) {
8449 list_add_tail(&psli->mbox_active->list, &completions);
8450 psli->mbox_active = NULL;
8451 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8453 /* The completed mailbox command queue */
8454 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8455 spin_unlock_irqrestore(&phba->hbalock, iflag);
8457 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8458 while (!list_empty(&completions)) {
8459 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8460 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8461 if (pmb->mbox_cmpl)
8462 pmb->mbox_cmpl(phba, pmb);
8467 * lpfc_sli_host_down - Vport cleanup function
8468 * @vport: Pointer to virtual port object.
8470 * lpfc_sli_host_down is called to clean up the resources
8471 * associated with a vport before destroying virtual
8472 * port data structures.
8473 * This function does following operations:
8474 * - Free discovery resources associated with this virtual
8475 * port.
8476 * - Free iocbs associated with this virtual port in
8477 * the txq.
8478 * - Send abort for all iocb commands associated with this
8479 * vport in txcmplq.
8481 * This function is called with no lock held and always returns 1.
8484 lpfc_sli_host_down(struct lpfc_vport *vport)
8486 LIST_HEAD(completions);
8487 struct lpfc_hba *phba = vport->phba;
8488 struct lpfc_sli *psli = &phba->sli;
8489 struct lpfc_sli_ring *pring;
8490 struct lpfc_iocbq *iocb, *next_iocb;
8491 int i;
8492 unsigned long flags = 0;
8493 uint16_t prev_pring_flag;
8495 lpfc_cleanup_discovery_resources(vport);
8497 spin_lock_irqsave(&phba->hbalock, flags);
8498 for (i = 0; i < psli->num_rings; i++) {
8499 pring = &psli->ring[i];
8500 prev_pring_flag = pring->flag;
8501 /* Only slow rings */
8502 if (pring->ringno == LPFC_ELS_RING) {
8503 pring->flag |= LPFC_DEFERRED_RING_EVENT;
8504 /* Set the lpfc data pending flag */
8505 set_bit(LPFC_DATA_READY, &phba->data_flags);
8508 * Error everything on the txq since these iocbs have not been
8509 * given to the FW yet.
8511 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8512 if (iocb->vport != vport)
8513 continue;
8514 list_move_tail(&iocb->list, &completions);
8515 pring->txq_cnt--;
8518 /* Next issue ABTS for everything on the txcmplq */
8519 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8520 list) {
8521 if (iocb->vport != vport)
8522 continue;
8523 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8526 pring->flag = prev_pring_flag;
8529 spin_unlock_irqrestore(&phba->hbalock, flags);
8531 /* Cancel all the IOCBs from the completions list */
8532 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8533 IOERR_SLI_DOWN);
8534 return 1;
8538 * lpfc_sli_hba_down - Resource cleanup function for the HBA
8539 * @phba: Pointer to HBA context object.
8541 * This function cleans up all iocb, buffers, mailbox commands
8542 * while shutting down the HBA. This function is called with no
8543 * lock held and always returns 1.
8544 * This function does the following to cleanup driver resources:
8545 * - Free discovery resources for each virtual port
8546 * - Cleanup any pending fabric iocbs
8547 * - Iterate through the iocb txq and free each entry
8548 * in the list.
8549 * - Free up any buffer posted to the HBA
8550 * - Free mailbox commands in the mailbox queue.
8553 lpfc_sli_hba_down(struct lpfc_hba *phba)
8555 LIST_HEAD(completions);
8556 struct lpfc_sli *psli = &phba->sli;
8557 struct lpfc_sli_ring *pring;
8558 struct lpfc_dmabuf *buf_ptr;
8559 unsigned long flags = 0;
8560 int i;
8562 /* Shutdown the mailbox command sub-system */
8563 lpfc_sli_mbox_sys_shutdown(phba);
8565 lpfc_hba_down_prep(phba);
8567 lpfc_fabric_abort_hba(phba);
8569 spin_lock_irqsave(&phba->hbalock, flags);
8570 for (i = 0; i < psli->num_rings; i++) {
8571 pring = &psli->ring[i];
8572 /* Only slow rings */
8573 if (pring->ringno == LPFC_ELS_RING) {
8574 pring->flag |= LPFC_DEFERRED_RING_EVENT;
8575 /* Set the lpfc data pending flag */
8576 set_bit(LPFC_DATA_READY, &phba->data_flags);
8580 * Error everything on the txq since these iocbs have not been
8581 * given to the FW yet.
8583 list_splice_init(&pring->txq, &completions);
8584 pring->txq_cnt = 0;
8587 spin_unlock_irqrestore(&phba->hbalock, flags);
8589 /* Cancel all the IOCBs from the completions list */
8590 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8591 IOERR_SLI_DOWN);
8593 spin_lock_irqsave(&phba->hbalock, flags);
8594 list_splice_init(&phba->elsbuf, &completions);
8595 phba->elsbuf_cnt = 0;
8596 phba->elsbuf_prev_cnt = 0;
8597 spin_unlock_irqrestore(&phba->hbalock, flags);
8599 while (!list_empty(&completions)) {
8600 list_remove_head(&completions, buf_ptr,
8601 struct lpfc_dmabuf, list);
8602 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8603 kfree(buf_ptr);
8606 /* Return any active mbox cmds */
8607 del_timer_sync(&psli->mbox_tmo);
8609 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
8610 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8611 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
8613 return 1;
8617 * lpfc_sli_pcimem_bcopy - SLI memory copy function
8618 * @srcp: Source memory pointer.
8619 * @destp: Destination memory pointer.
8620 * @cnt: Number of words required to be copied.
8622 * This function is used for copying data between driver memory
8623 * and the SLI memory. This function also changes the endianness
8624 * of each word if native endianness is different from SLI
8625 * endianness. This function can be called with or without
8626 * lock.
8628 void
8629 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8631 uint32_t *src = srcp;
8632 uint32_t *dest = destp;
8633 uint32_t ldata;
8634 int i;
8636 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8637 ldata = *src;
8638 ldata = le32_to_cpu(ldata);
8639 *dest = ldata;
8640 src++;
8641 dest++;
8647 * lpfc_sli_bemem_bcopy - SLI memory copy function
8648 * @srcp: Source memory pointer.
8649 * @destp: Destination memory pointer.
8650 * @cnt: Number of words required to be copied.
8652 * This function is used for copying data between a data structure
8653 * with big endian representation to local endianness.
8654 * This function can be called with or without lock.
8656 void
8657 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8659 uint32_t *src = srcp;
8660 uint32_t *dest = destp;
8661 uint32_t ldata;
8662 int i;
8664 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8665 ldata = *src;
8666 ldata = be32_to_cpu(ldata);
8667 *dest = ldata;
8668 src++;
8669 dest++;
8674 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
8675 * @phba: Pointer to HBA context object.
8676 * @pring: Pointer to driver SLI ring object.
8677 * @mp: Pointer to driver buffer object.
8679 * This function is called with no lock held.
8680 * It always return zero after adding the buffer to the postbufq
8681 * buffer list.
8684 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8685 struct lpfc_dmabuf *mp)
8687 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8688 later */
8689 spin_lock_irq(&phba->hbalock);
8690 list_add_tail(&mp->list, &pring->postbufq);
8691 pring->postbufq_cnt++;
8692 spin_unlock_irq(&phba->hbalock);
8693 return 0;
8697 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
8698 * @phba: Pointer to HBA context object.
8700 * When HBQ is enabled, buffers are searched based on tags. This function
8701 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8702 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8703 * does not conflict with tags of buffer posted for unsolicited events.
8704 * The function returns the allocated tag. The function is called with
8705 * no locks held.
8707 uint32_t
8708 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8710 spin_lock_irq(&phba->hbalock);
8711 phba->buffer_tag_count++;
8713 * Always set the QUE_BUFTAG_BIT to distiguish between
8714 * a tag assigned by HBQ.
8716 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8717 spin_unlock_irq(&phba->hbalock);
8718 return phba->buffer_tag_count;
8722 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
8723 * @phba: Pointer to HBA context object.
8724 * @pring: Pointer to driver SLI ring object.
8725 * @tag: Buffer tag.
8727 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8728 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8729 * iocb is posted to the response ring with the tag of the buffer.
8730 * This function searches the pring->postbufq list using the tag
8731 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8732 * iocb. If the buffer is found then lpfc_dmabuf object of the
8733 * buffer is returned to the caller else NULL is returned.
8734 * This function is called with no lock held.
8736 struct lpfc_dmabuf *
8737 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8738 uint32_t tag)
8740 struct lpfc_dmabuf *mp, *next_mp;
8741 struct list_head *slp = &pring->postbufq;
8743 /* Search postbufq, from the beginning, looking for a match on tag */
8744 spin_lock_irq(&phba->hbalock);
8745 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8746 if (mp->buffer_tag == tag) {
8747 list_del_init(&mp->list);
8748 pring->postbufq_cnt--;
8749 spin_unlock_irq(&phba->hbalock);
8750 return mp;
8754 spin_unlock_irq(&phba->hbalock);
8755 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8756 "0402 Cannot find virtual addr for buffer tag on "
8757 "ring %d Data x%lx x%p x%p x%x\n",
8758 pring->ringno, (unsigned long) tag,
8759 slp->next, slp->prev, pring->postbufq_cnt);
8761 return NULL;
8765 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
8766 * @phba: Pointer to HBA context object.
8767 * @pring: Pointer to driver SLI ring object.
8768 * @phys: DMA address of the buffer.
8770 * This function searches the buffer list using the dma_address
8771 * of unsolicited event to find the driver's lpfc_dmabuf object
8772 * corresponding to the dma_address. The function returns the
8773 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8774 * This function is called by the ct and els unsolicited event
8775 * handlers to get the buffer associated with the unsolicited
8776 * event.
8778 * This function is called with no lock held.
8780 struct lpfc_dmabuf *
8781 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8782 dma_addr_t phys)
8784 struct lpfc_dmabuf *mp, *next_mp;
8785 struct list_head *slp = &pring->postbufq;
8787 /* Search postbufq, from the beginning, looking for a match on phys */
8788 spin_lock_irq(&phba->hbalock);
8789 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8790 if (mp->phys == phys) {
8791 list_del_init(&mp->list);
8792 pring->postbufq_cnt--;
8793 spin_unlock_irq(&phba->hbalock);
8794 return mp;
8798 spin_unlock_irq(&phba->hbalock);
8799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8800 "0410 Cannot find virtual addr for mapped buf on "
8801 "ring %d Data x%llx x%p x%p x%x\n",
8802 pring->ringno, (unsigned long long)phys,
8803 slp->next, slp->prev, pring->postbufq_cnt);
8804 return NULL;
8808 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
8809 * @phba: Pointer to HBA context object.
8810 * @cmdiocb: Pointer to driver command iocb object.
8811 * @rspiocb: Pointer to driver response iocb object.
8813 * This function is the completion handler for the abort iocbs for
8814 * ELS commands. This function is called from the ELS ring event
8815 * handler with no lock held. This function frees memory resources
8816 * associated with the abort iocb.
8818 static void
8819 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8820 struct lpfc_iocbq *rspiocb)
8822 IOCB_t *irsp = &rspiocb->iocb;
8823 uint16_t abort_iotag, abort_context;
8824 struct lpfc_iocbq *abort_iocb;
8825 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8827 abort_iocb = NULL;
8829 if (irsp->ulpStatus) {
8830 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8831 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8833 spin_lock_irq(&phba->hbalock);
8834 if (phba->sli_rev < LPFC_SLI_REV4) {
8835 if (abort_iotag != 0 &&
8836 abort_iotag <= phba->sli.last_iotag)
8837 abort_iocb =
8838 phba->sli.iocbq_lookup[abort_iotag];
8839 } else
8840 /* For sli4 the abort_tag is the XRI,
8841 * so the abort routine puts the iotag of the iocb
8842 * being aborted in the context field of the abort
8843 * IOCB.
8845 abort_iocb = phba->sli.iocbq_lookup[abort_context];
8848 * If the iocb is not found in Firmware queue the iocb
8849 * might have completed already. Do not free it again.
8851 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
8852 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8853 spin_unlock_irq(&phba->hbalock);
8854 lpfc_sli_release_iocbq(phba, cmdiocb);
8855 return;
8857 /* For SLI4 the ulpContext field for abort IOCB
8858 * holds the iotag of the IOCB being aborted so
8859 * the local abort_context needs to be reset to
8860 * match the aborted IOCBs ulpContext.
8862 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8863 abort_context = abort_iocb->iocb.ulpContext;
8866 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8867 "0327 Cannot abort els iocb %p "
8868 "with tag %x context %x, abort status %x, "
8869 "abort code %x\n",
8870 abort_iocb, abort_iotag, abort_context,
8871 irsp->ulpStatus, irsp->un.ulpWord[4]);
8873 * make sure we have the right iocbq before taking it
8874 * off the txcmplq and try to call completion routine.
8876 if (!abort_iocb ||
8877 abort_iocb->iocb.ulpContext != abort_context ||
8878 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8879 spin_unlock_irq(&phba->hbalock);
8880 else if (phba->sli_rev < LPFC_SLI_REV4) {
8882 * leave the SLI4 aborted command on the txcmplq
8883 * list and the command complete WCQE's XB bit
8884 * will tell whether the SGL (XRI) can be released
8885 * immediately or to the aborted SGL list for the
8886 * following abort XRI from the HBA.
8888 list_del_init(&abort_iocb->list);
8889 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8890 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8891 pring->txcmplq_cnt--;
8894 /* Firmware could still be in progress of DMAing
8895 * payload, so don't free data buffer till after
8896 * a hbeat.
8898 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
8899 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
8900 spin_unlock_irq(&phba->hbalock);
8902 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
8903 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
8904 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
8905 } else
8906 spin_unlock_irq(&phba->hbalock);
8909 lpfc_sli_release_iocbq(phba, cmdiocb);
8910 return;
8914 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
8915 * @phba: Pointer to HBA context object.
8916 * @cmdiocb: Pointer to driver command iocb object.
8917 * @rspiocb: Pointer to driver response iocb object.
8919 * The function is called from SLI ring event handler with no
8920 * lock held. This function is the completion handler for ELS commands
8921 * which are aborted. The function frees memory resources used for
8922 * the aborted ELS commands.
8924 static void
8925 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8926 struct lpfc_iocbq *rspiocb)
8928 IOCB_t *irsp = &rspiocb->iocb;
8930 /* ELS cmd tag <ulpIoTag> completes */
8931 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
8932 "0139 Ignoring ELS cmd tag x%x completion Data: "
8933 "x%x x%x x%x\n",
8934 irsp->ulpIoTag, irsp->ulpStatus,
8935 irsp->un.ulpWord[4], irsp->ulpTimeout);
8936 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8937 lpfc_ct_free_iocb(phba, cmdiocb);
8938 else
8939 lpfc_els_free_iocb(phba, cmdiocb);
8940 return;
8944 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
8945 * @phba: Pointer to HBA context object.
8946 * @pring: Pointer to driver SLI ring object.
8947 * @cmdiocb: Pointer to driver command iocb object.
8949 * This function issues an abort iocb for the provided command iocb down to
8950 * the port. Other than the case the outstanding command iocb is an abort
8951 * request, this function issues abort out unconditionally. This function is
8952 * called with hbalock held. The function returns 0 when it fails due to
8953 * memory allocation failure or when the command iocb is an abort request.
8955 static int
8956 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8957 struct lpfc_iocbq *cmdiocb)
8959 struct lpfc_vport *vport = cmdiocb->vport;
8960 struct lpfc_iocbq *abtsiocbp;
8961 IOCB_t *icmd = NULL;
8962 IOCB_t *iabt = NULL;
8963 int retval;
8966 * There are certain command types we don't want to abort. And we
8967 * don't want to abort commands that are already in the process of
8968 * being aborted.
8970 icmd = &cmdiocb->iocb;
8971 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
8972 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8973 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
8974 return 0;
8976 /* issue ABTS for this IOCB based on iotag */
8977 abtsiocbp = __lpfc_sli_get_iocbq(phba);
8978 if (abtsiocbp == NULL)
8979 return 0;
8981 /* This signals the response to set the correct status
8982 * before calling the completion handler
8984 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
8986 iabt = &abtsiocbp->iocb;
8987 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
8988 iabt->un.acxri.abortContextTag = icmd->ulpContext;
8989 if (phba->sli_rev == LPFC_SLI_REV4) {
8990 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
8991 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
8993 else
8994 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
8995 iabt->ulpLe = 1;
8996 iabt->ulpClass = icmd->ulpClass;
8998 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
8999 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9000 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9001 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9003 if (phba->link_state >= LPFC_LINK_UP)
9004 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9005 else
9006 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9008 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9010 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9011 "0339 Abort xri x%x, original iotag x%x, "
9012 "abort cmd iotag x%x\n",
9013 iabt->un.acxri.abortIoTag,
9014 iabt->un.acxri.abortContextTag,
9015 abtsiocbp->iotag);
9016 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
9018 if (retval)
9019 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9022 * Caller to this routine should check for IOCB_ERROR
9023 * and handle it properly. This routine no longer removes
9024 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9026 return retval;
9030 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9031 * @phba: Pointer to HBA context object.
9032 * @pring: Pointer to driver SLI ring object.
9033 * @cmdiocb: Pointer to driver command iocb object.
9035 * This function issues an abort iocb for the provided command iocb. In case
9036 * of unloading, the abort iocb will not be issued to commands on the ELS
9037 * ring. Instead, the callback function shall be changed to those commands
9038 * so that nothing happens when them finishes. This function is called with
9039 * hbalock held. The function returns 0 when the command iocb is an abort
9040 * request.
9043 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9044 struct lpfc_iocbq *cmdiocb)
9046 struct lpfc_vport *vport = cmdiocb->vport;
9047 int retval = IOCB_ERROR;
9048 IOCB_t *icmd = NULL;
9051 * There are certain command types we don't want to abort. And we
9052 * don't want to abort commands that are already in the process of
9053 * being aborted.
9055 icmd = &cmdiocb->iocb;
9056 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9057 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9058 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9059 return 0;
9062 * If we're unloading, don't abort iocb on the ELS ring, but change
9063 * the callback so that nothing happens when it finishes.
9065 if ((vport->load_flag & FC_UNLOADING) &&
9066 (pring->ringno == LPFC_ELS_RING)) {
9067 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9068 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9069 else
9070 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9071 goto abort_iotag_exit;
9074 /* Now, we try to issue the abort to the cmdiocb out */
9075 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9077 abort_iotag_exit:
9079 * Caller to this routine should check for IOCB_ERROR
9080 * and handle it properly. This routine no longer removes
9081 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9083 return retval;
9087 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9088 * @phba: Pointer to HBA context object.
9089 * @pring: Pointer to driver SLI ring object.
9091 * This function aborts all iocbs in the given ring and frees all the iocb
9092 * objects in txq. This function issues abort iocbs unconditionally for all
9093 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9094 * to complete before the return of this function. The caller is not required
9095 * to hold any locks.
9097 static void
9098 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9100 LIST_HEAD(completions);
9101 struct lpfc_iocbq *iocb, *next_iocb;
9103 if (pring->ringno == LPFC_ELS_RING)
9104 lpfc_fabric_abort_hba(phba);
9106 spin_lock_irq(&phba->hbalock);
9108 /* Take off all the iocbs on txq for cancelling */
9109 list_splice_init(&pring->txq, &completions);
9110 pring->txq_cnt = 0;
9112 /* Next issue ABTS for everything on the txcmplq */
9113 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9114 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9116 spin_unlock_irq(&phba->hbalock);
9118 /* Cancel all the IOCBs from the completions list */
9119 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9120 IOERR_SLI_ABORTED);
9124 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9125 * @phba: pointer to lpfc HBA data structure.
9127 * This routine will abort all pending and outstanding iocbs to an HBA.
9129 void
9130 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9132 struct lpfc_sli *psli = &phba->sli;
9133 struct lpfc_sli_ring *pring;
9134 int i;
9136 for (i = 0; i < psli->num_rings; i++) {
9137 pring = &psli->ring[i];
9138 lpfc_sli_iocb_ring_abort(phba, pring);
9143 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9144 * @iocbq: Pointer to driver iocb object.
9145 * @vport: Pointer to driver virtual port object.
9146 * @tgt_id: SCSI ID of the target.
9147 * @lun_id: LUN ID of the scsi device.
9148 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9150 * This function acts as an iocb filter for functions which abort or count
9151 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9152 * 0 if the filtering criteria is met for the given iocb and will return
9153 * 1 if the filtering criteria is not met.
9154 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9155 * given iocb is for the SCSI device specified by vport, tgt_id and
9156 * lun_id parameter.
9157 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9158 * given iocb is for the SCSI target specified by vport and tgt_id
9159 * parameters.
9160 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9161 * given iocb is for the SCSI host associated with the given vport.
9162 * This function is called with no locks held.
9164 static int
9165 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9166 uint16_t tgt_id, uint64_t lun_id,
9167 lpfc_ctx_cmd ctx_cmd)
9169 struct lpfc_scsi_buf *lpfc_cmd;
9170 int rc = 1;
9172 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9173 return rc;
9175 if (iocbq->vport != vport)
9176 return rc;
9178 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9180 if (lpfc_cmd->pCmd == NULL)
9181 return rc;
9183 switch (ctx_cmd) {
9184 case LPFC_CTX_LUN:
9185 if ((lpfc_cmd->rdata->pnode) &&
9186 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9187 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9188 rc = 0;
9189 break;
9190 case LPFC_CTX_TGT:
9191 if ((lpfc_cmd->rdata->pnode) &&
9192 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9193 rc = 0;
9194 break;
9195 case LPFC_CTX_HOST:
9196 rc = 0;
9197 break;
9198 default:
9199 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9200 __func__, ctx_cmd);
9201 break;
9204 return rc;
9208 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
9209 * @vport: Pointer to virtual port.
9210 * @tgt_id: SCSI ID of the target.
9211 * @lun_id: LUN ID of the scsi device.
9212 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9214 * This function returns number of FCP commands pending for the vport.
9215 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9216 * commands pending on the vport associated with SCSI device specified
9217 * by tgt_id and lun_id parameters.
9218 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9219 * commands pending on the vport associated with SCSI target specified
9220 * by tgt_id parameter.
9221 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9222 * commands pending on the vport.
9223 * This function returns the number of iocbs which satisfy the filter.
9224 * This function is called without any lock held.
9227 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9228 lpfc_ctx_cmd ctx_cmd)
9230 struct lpfc_hba *phba = vport->phba;
9231 struct lpfc_iocbq *iocbq;
9232 int sum, i;
9234 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9235 iocbq = phba->sli.iocbq_lookup[i];
9237 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9238 ctx_cmd) == 0)
9239 sum++;
9242 return sum;
9246 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
9247 * @phba: Pointer to HBA context object
9248 * @cmdiocb: Pointer to command iocb object.
9249 * @rspiocb: Pointer to response iocb object.
9251 * This function is called when an aborted FCP iocb completes. This
9252 * function is called by the ring event handler with no lock held.
9253 * This function frees the iocb.
9255 void
9256 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9257 struct lpfc_iocbq *rspiocb)
9259 lpfc_sli_release_iocbq(phba, cmdiocb);
9260 return;
9264 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
9265 * @vport: Pointer to virtual port.
9266 * @pring: Pointer to driver SLI ring object.
9267 * @tgt_id: SCSI ID of the target.
9268 * @lun_id: LUN ID of the scsi device.
9269 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9271 * This function sends an abort command for every SCSI command
9272 * associated with the given virtual port pending on the ring
9273 * filtered by lpfc_sli_validate_fcp_iocb function.
9274 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9275 * FCP iocbs associated with lun specified by tgt_id and lun_id
9276 * parameters
9277 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9278 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9279 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9280 * FCP iocbs associated with virtual port.
9281 * This function returns number of iocbs it failed to abort.
9282 * This function is called with no locks held.
9285 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9286 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
9288 struct lpfc_hba *phba = vport->phba;
9289 struct lpfc_iocbq *iocbq;
9290 struct lpfc_iocbq *abtsiocb;
9291 IOCB_t *cmd = NULL;
9292 int errcnt = 0, ret_val = 0;
9293 int i;
9295 for (i = 1; i <= phba->sli.last_iotag; i++) {
9296 iocbq = phba->sli.iocbq_lookup[i];
9298 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
9299 abort_cmd) != 0)
9300 continue;
9302 /* issue ABTS for this IOCB based on iotag */
9303 abtsiocb = lpfc_sli_get_iocbq(phba);
9304 if (abtsiocb == NULL) {
9305 errcnt++;
9306 continue;
9309 cmd = &iocbq->iocb;
9310 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9311 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
9312 if (phba->sli_rev == LPFC_SLI_REV4)
9313 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9314 else
9315 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
9316 abtsiocb->iocb.ulpLe = 1;
9317 abtsiocb->iocb.ulpClass = cmd->ulpClass;
9318 abtsiocb->vport = phba->pport;
9320 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9321 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
9322 if (iocbq->iocb_flag & LPFC_IO_FCP)
9323 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9325 if (lpfc_is_link_up(phba))
9326 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9327 else
9328 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9330 /* Setup callback routine and issue the command. */
9331 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
9332 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9333 abtsiocb, 0);
9334 if (ret_val == IOCB_ERROR) {
9335 lpfc_sli_release_iocbq(phba, abtsiocb);
9336 errcnt++;
9337 continue;
9341 return errcnt;
9345 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
9346 * @phba: Pointer to HBA context object.
9347 * @cmdiocbq: Pointer to command iocb.
9348 * @rspiocbq: Pointer to response iocb.
9350 * This function is the completion handler for iocbs issued using
9351 * lpfc_sli_issue_iocb_wait function. This function is called by the
9352 * ring event handler function without any lock held. This function
9353 * can be called from both worker thread context and interrupt
9354 * context. This function also can be called from other thread which
9355 * cleans up the SLI layer objects.
9356 * This function copy the contents of the response iocb to the
9357 * response iocb memory object provided by the caller of
9358 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9359 * sleeps for the iocb completion.
9361 static void
9362 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9363 struct lpfc_iocbq *cmdiocbq,
9364 struct lpfc_iocbq *rspiocbq)
9366 wait_queue_head_t *pdone_q;
9367 unsigned long iflags;
9368 struct lpfc_scsi_buf *lpfc_cmd;
9370 spin_lock_irqsave(&phba->hbalock, iflags);
9371 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9372 if (cmdiocbq->context2 && rspiocbq)
9373 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9374 &rspiocbq->iocb, sizeof(IOCB_t));
9376 /* Set the exchange busy flag for task management commands */
9377 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9378 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9379 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9380 cur_iocbq);
9381 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9384 pdone_q = cmdiocbq->context_un.wait_queue;
9385 if (pdone_q)
9386 wake_up(pdone_q);
9387 spin_unlock_irqrestore(&phba->hbalock, iflags);
9388 return;
9392 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9393 * @phba: Pointer to HBA context object..
9394 * @piocbq: Pointer to command iocb.
9395 * @flag: Flag to test.
9397 * This routine grabs the hbalock and then test the iocb_flag to
9398 * see if the passed in flag is set.
9399 * Returns:
9400 * 1 if flag is set.
9401 * 0 if flag is not set.
9403 static int
9404 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9405 struct lpfc_iocbq *piocbq, uint32_t flag)
9407 unsigned long iflags;
9408 int ret;
9410 spin_lock_irqsave(&phba->hbalock, iflags);
9411 ret = piocbq->iocb_flag & flag;
9412 spin_unlock_irqrestore(&phba->hbalock, iflags);
9413 return ret;
9418 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
9419 * @phba: Pointer to HBA context object..
9420 * @pring: Pointer to sli ring.
9421 * @piocb: Pointer to command iocb.
9422 * @prspiocbq: Pointer to response iocb.
9423 * @timeout: Timeout in number of seconds.
9425 * This function issues the iocb to firmware and waits for the
9426 * iocb to complete. If the iocb command is not
9427 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9428 * Caller should not free the iocb resources if this function
9429 * returns IOCB_TIMEDOUT.
9430 * The function waits for the iocb completion using an
9431 * non-interruptible wait.
9432 * This function will sleep while waiting for iocb completion.
9433 * So, this function should not be called from any context which
9434 * does not allow sleeping. Due to the same reason, this function
9435 * cannot be called with interrupt disabled.
9436 * This function assumes that the iocb completions occur while
9437 * this function sleep. So, this function cannot be called from
9438 * the thread which process iocb completion for this ring.
9439 * This function clears the iocb_flag of the iocb object before
9440 * issuing the iocb and the iocb completion handler sets this
9441 * flag and wakes this thread when the iocb completes.
9442 * The contents of the response iocb will be copied to prspiocbq
9443 * by the completion handler when the command completes.
9444 * This function returns IOCB_SUCCESS when success.
9445 * This function is called with no lock held.
9448 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
9449 uint32_t ring_number,
9450 struct lpfc_iocbq *piocb,
9451 struct lpfc_iocbq *prspiocbq,
9452 uint32_t timeout)
9454 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9455 long timeleft, timeout_req = 0;
9456 int retval = IOCB_SUCCESS;
9457 uint32_t creg_val;
9458 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9460 * If the caller has provided a response iocbq buffer, then context2
9461 * is NULL or its an error.
9463 if (prspiocbq) {
9464 if (piocb->context2)
9465 return IOCB_ERROR;
9466 piocb->context2 = prspiocbq;
9469 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9470 piocb->context_un.wait_queue = &done_q;
9471 piocb->iocb_flag &= ~LPFC_IO_WAKE;
9473 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9474 if (lpfc_readl(phba->HCregaddr, &creg_val))
9475 return IOCB_ERROR;
9476 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9477 writel(creg_val, phba->HCregaddr);
9478 readl(phba->HCregaddr); /* flush */
9481 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9482 SLI_IOCB_RET_IOCB);
9483 if (retval == IOCB_SUCCESS) {
9484 timeout_req = timeout * HZ;
9485 timeleft = wait_event_timeout(done_q,
9486 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
9487 timeout_req);
9489 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9490 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9491 "0331 IOCB wake signaled\n");
9492 } else if (timeleft == 0) {
9493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9494 "0338 IOCB wait timeout error - no "
9495 "wake response Data x%x\n", timeout);
9496 retval = IOCB_TIMEDOUT;
9497 } else {
9498 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9499 "0330 IOCB wake NOT set, "
9500 "Data x%x x%lx\n",
9501 timeout, (timeleft / jiffies));
9502 retval = IOCB_TIMEDOUT;
9504 } else if (retval == IOCB_BUSY) {
9505 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9506 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9507 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9508 return retval;
9509 } else {
9510 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9511 "0332 IOCB wait issue failed, Data x%x\n",
9512 retval);
9513 retval = IOCB_ERROR;
9516 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9517 if (lpfc_readl(phba->HCregaddr, &creg_val))
9518 return IOCB_ERROR;
9519 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9520 writel(creg_val, phba->HCregaddr);
9521 readl(phba->HCregaddr); /* flush */
9524 if (prspiocbq)
9525 piocb->context2 = NULL;
9527 piocb->context_un.wait_queue = NULL;
9528 piocb->iocb_cmpl = NULL;
9529 return retval;
9533 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
9534 * @phba: Pointer to HBA context object.
9535 * @pmboxq: Pointer to driver mailbox object.
9536 * @timeout: Timeout in number of seconds.
9538 * This function issues the mailbox to firmware and waits for the
9539 * mailbox command to complete. If the mailbox command is not
9540 * completed within timeout seconds, it returns MBX_TIMEOUT.
9541 * The function waits for the mailbox completion using an
9542 * interruptible wait. If the thread is woken up due to a
9543 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9544 * should not free the mailbox resources, if this function returns
9545 * MBX_TIMEOUT.
9546 * This function will sleep while waiting for mailbox completion.
9547 * So, this function should not be called from any context which
9548 * does not allow sleeping. Due to the same reason, this function
9549 * cannot be called with interrupt disabled.
9550 * This function assumes that the mailbox completion occurs while
9551 * this function sleep. So, this function cannot be called from
9552 * the worker thread which processes mailbox completion.
9553 * This function is called in the context of HBA management
9554 * applications.
9555 * This function returns MBX_SUCCESS when successful.
9556 * This function is called with no lock held.
9559 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
9560 uint32_t timeout)
9562 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9563 int retval;
9564 unsigned long flag;
9566 /* The caller must leave context1 empty. */
9567 if (pmboxq->context1)
9568 return MBX_NOT_FINISHED;
9570 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
9571 /* setup wake call as IOCB callback */
9572 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9573 /* setup context field to pass wait_queue pointer to wake function */
9574 pmboxq->context1 = &done_q;
9576 /* now issue the command */
9577 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
9578 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
9579 wait_event_interruptible_timeout(done_q,
9580 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9581 timeout * HZ);
9583 spin_lock_irqsave(&phba->hbalock, flag);
9584 pmboxq->context1 = NULL;
9586 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9587 * else do not free the resources.
9589 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
9590 retval = MBX_SUCCESS;
9591 lpfc_sli4_swap_str(phba, pmboxq);
9592 } else {
9593 retval = MBX_TIMEOUT;
9594 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9596 spin_unlock_irqrestore(&phba->hbalock, flag);
9599 return retval;
9603 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
9604 * @phba: Pointer to HBA context.
9606 * This function is called to shutdown the driver's mailbox sub-system.
9607 * It first marks the mailbox sub-system is in a block state to prevent
9608 * the asynchronous mailbox command from issued off the pending mailbox
9609 * command queue. If the mailbox command sub-system shutdown is due to
9610 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9611 * the mailbox sub-system flush routine to forcefully bring down the
9612 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9613 * as with offline or HBA function reset), this routine will wait for the
9614 * outstanding mailbox command to complete before invoking the mailbox
9615 * sub-system flush routine to gracefully bring down mailbox sub-system.
9617 void
9618 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
9620 struct lpfc_sli *psli = &phba->sli;
9621 unsigned long timeout;
9623 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
9624 spin_lock_irq(&phba->hbalock);
9625 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9626 spin_unlock_irq(&phba->hbalock);
9628 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
9629 spin_lock_irq(&phba->hbalock);
9630 /* Determine how long we might wait for the active mailbox
9631 * command to be gracefully completed by firmware.
9633 if (phba->sli.mbox_active)
9634 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9635 phba->sli.mbox_active) *
9636 1000) + jiffies;
9637 spin_unlock_irq(&phba->hbalock);
9639 while (phba->sli.mbox_active) {
9640 /* Check active mailbox complete status every 2ms */
9641 msleep(2);
9642 if (time_after(jiffies, timeout))
9643 /* Timeout, let the mailbox flush routine to
9644 * forcefully release active mailbox command
9646 break;
9649 lpfc_sli_mbox_sys_flush(phba);
9653 * lpfc_sli_eratt_read - read sli-3 error attention events
9654 * @phba: Pointer to HBA context.
9656 * This function is called to read the SLI3 device error attention registers
9657 * for possible error attention events. The caller must hold the hostlock
9658 * with spin_lock_irq().
9660 * This function returns 1 when there is Error Attention in the Host Attention
9661 * Register and returns 0 otherwise.
9663 static int
9664 lpfc_sli_eratt_read(struct lpfc_hba *phba)
9666 uint32_t ha_copy;
9668 /* Read chip Host Attention (HA) register */
9669 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9670 goto unplug_err;
9672 if (ha_copy & HA_ERATT) {
9673 /* Read host status register to retrieve error event */
9674 if (lpfc_sli_read_hs(phba))
9675 goto unplug_err;
9677 /* Check if there is a deferred error condition is active */
9678 if ((HS_FFER1 & phba->work_hs) &&
9679 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
9680 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
9681 phba->hba_flag |= DEFER_ERATT;
9682 /* Clear all interrupt enable conditions */
9683 writel(0, phba->HCregaddr);
9684 readl(phba->HCregaddr);
9687 /* Set the driver HA work bitmap */
9688 phba->work_ha |= HA_ERATT;
9689 /* Indicate polling handles this ERATT */
9690 phba->hba_flag |= HBA_ERATT_HANDLED;
9691 return 1;
9693 return 0;
9695 unplug_err:
9696 /* Set the driver HS work bitmap */
9697 phba->work_hs |= UNPLUG_ERR;
9698 /* Set the driver HA work bitmap */
9699 phba->work_ha |= HA_ERATT;
9700 /* Indicate polling handles this ERATT */
9701 phba->hba_flag |= HBA_ERATT_HANDLED;
9702 return 1;
9706 * lpfc_sli4_eratt_read - read sli-4 error attention events
9707 * @phba: Pointer to HBA context.
9709 * This function is called to read the SLI4 device error attention registers
9710 * for possible error attention events. The caller must hold the hostlock
9711 * with spin_lock_irq().
9713 * This function returns 1 when there is Error Attention in the Host Attention
9714 * Register and returns 0 otherwise.
9716 static int
9717 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9719 uint32_t uerr_sta_hi, uerr_sta_lo;
9720 uint32_t if_type, portsmphr;
9721 struct lpfc_register portstat_reg;
9724 * For now, use the SLI4 device internal unrecoverable error
9725 * registers for error attention. This can be changed later.
9727 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9728 switch (if_type) {
9729 case LPFC_SLI_INTF_IF_TYPE_0:
9730 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9731 &uerr_sta_lo) ||
9732 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9733 &uerr_sta_hi)) {
9734 phba->work_hs |= UNPLUG_ERR;
9735 phba->work_ha |= HA_ERATT;
9736 phba->hba_flag |= HBA_ERATT_HANDLED;
9737 return 1;
9739 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9740 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9742 "1423 HBA Unrecoverable error: "
9743 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9744 "ue_mask_lo_reg=0x%x, "
9745 "ue_mask_hi_reg=0x%x\n",
9746 uerr_sta_lo, uerr_sta_hi,
9747 phba->sli4_hba.ue_mask_lo,
9748 phba->sli4_hba.ue_mask_hi);
9749 phba->work_status[0] = uerr_sta_lo;
9750 phba->work_status[1] = uerr_sta_hi;
9751 phba->work_ha |= HA_ERATT;
9752 phba->hba_flag |= HBA_ERATT_HANDLED;
9753 return 1;
9755 break;
9756 case LPFC_SLI_INTF_IF_TYPE_2:
9757 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9758 &portstat_reg.word0) ||
9759 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9760 &portsmphr)){
9761 phba->work_hs |= UNPLUG_ERR;
9762 phba->work_ha |= HA_ERATT;
9763 phba->hba_flag |= HBA_ERATT_HANDLED;
9764 return 1;
9766 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9767 phba->work_status[0] =
9768 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9769 phba->work_status[1] =
9770 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9772 "2885 Port Error Detected: "
9773 "port status reg 0x%x, "
9774 "port smphr reg 0x%x, "
9775 "error 1=0x%x, error 2=0x%x\n",
9776 portstat_reg.word0,
9777 portsmphr,
9778 phba->work_status[0],
9779 phba->work_status[1]);
9780 phba->work_ha |= HA_ERATT;
9781 phba->hba_flag |= HBA_ERATT_HANDLED;
9782 return 1;
9784 break;
9785 case LPFC_SLI_INTF_IF_TYPE_1:
9786 default:
9787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9788 "2886 HBA Error Attention on unsupported "
9789 "if type %d.", if_type);
9790 return 1;
9793 return 0;
9797 * lpfc_sli_check_eratt - check error attention events
9798 * @phba: Pointer to HBA context.
9800 * This function is called from timer soft interrupt context to check HBA's
9801 * error attention register bit for error attention events.
9803 * This function returns 1 when there is Error Attention in the Host Attention
9804 * Register and returns 0 otherwise.
9807 lpfc_sli_check_eratt(struct lpfc_hba *phba)
9809 uint32_t ha_copy;
9811 /* If somebody is waiting to handle an eratt, don't process it
9812 * here. The brdkill function will do this.
9814 if (phba->link_flag & LS_IGNORE_ERATT)
9815 return 0;
9817 /* Check if interrupt handler handles this ERATT */
9818 spin_lock_irq(&phba->hbalock);
9819 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9820 /* Interrupt handler has handled ERATT */
9821 spin_unlock_irq(&phba->hbalock);
9822 return 0;
9826 * If there is deferred error attention, do not check for error
9827 * attention
9829 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9830 spin_unlock_irq(&phba->hbalock);
9831 return 0;
9834 /* If PCI channel is offline, don't process it */
9835 if (unlikely(pci_channel_offline(phba->pcidev))) {
9836 spin_unlock_irq(&phba->hbalock);
9837 return 0;
9840 switch (phba->sli_rev) {
9841 case LPFC_SLI_REV2:
9842 case LPFC_SLI_REV3:
9843 /* Read chip Host Attention (HA) register */
9844 ha_copy = lpfc_sli_eratt_read(phba);
9845 break;
9846 case LPFC_SLI_REV4:
9847 /* Read device Uncoverable Error (UERR) registers */
9848 ha_copy = lpfc_sli4_eratt_read(phba);
9849 break;
9850 default:
9851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9852 "0299 Invalid SLI revision (%d)\n",
9853 phba->sli_rev);
9854 ha_copy = 0;
9855 break;
9857 spin_unlock_irq(&phba->hbalock);
9859 return ha_copy;
9863 * lpfc_intr_state_check - Check device state for interrupt handling
9864 * @phba: Pointer to HBA context.
9866 * This inline routine checks whether a device or its PCI slot is in a state
9867 * that the interrupt should be handled.
9869 * This function returns 0 if the device or the PCI slot is in a state that
9870 * interrupt should be handled, otherwise -EIO.
9872 static inline int
9873 lpfc_intr_state_check(struct lpfc_hba *phba)
9875 /* If the pci channel is offline, ignore all the interrupts */
9876 if (unlikely(pci_channel_offline(phba->pcidev)))
9877 return -EIO;
9879 /* Update device level interrupt statistics */
9880 phba->sli.slistat.sli_intr++;
9882 /* Ignore all interrupts during initialization. */
9883 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9884 return -EIO;
9886 return 0;
9890 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
9891 * @irq: Interrupt number.
9892 * @dev_id: The device context pointer.
9894 * This function is directly called from the PCI layer as an interrupt
9895 * service routine when device with SLI-3 interface spec is enabled with
9896 * MSI-X multi-message interrupt mode and there are slow-path events in
9897 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9898 * interrupt mode, this function is called as part of the device-level
9899 * interrupt handler. When the PCI slot is in error recovery or the HBA
9900 * is undergoing initialization, the interrupt handler will not process
9901 * the interrupt. The link attention and ELS ring attention events are
9902 * handled by the worker thread. The interrupt handler signals the worker
9903 * thread and returns for these events. This function is called without
9904 * any lock held. It gets the hbalock to access and update SLI data
9905 * structures.
9907 * This function returns IRQ_HANDLED when interrupt is handled else it
9908 * returns IRQ_NONE.
9910 irqreturn_t
9911 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
9913 struct lpfc_hba *phba;
9914 uint32_t ha_copy, hc_copy;
9915 uint32_t work_ha_copy;
9916 unsigned long status;
9917 unsigned long iflag;
9918 uint32_t control;
9920 MAILBOX_t *mbox, *pmbox;
9921 struct lpfc_vport *vport;
9922 struct lpfc_nodelist *ndlp;
9923 struct lpfc_dmabuf *mp;
9924 LPFC_MBOXQ_t *pmb;
9925 int rc;
9928 * Get the driver's phba structure from the dev_id and
9929 * assume the HBA is not interrupting.
9931 phba = (struct lpfc_hba *)dev_id;
9933 if (unlikely(!phba))
9934 return IRQ_NONE;
9937 * Stuff needs to be attented to when this function is invoked as an
9938 * individual interrupt handler in MSI-X multi-message interrupt mode
9940 if (phba->intr_type == MSIX) {
9941 /* Check device state for handling interrupt */
9942 if (lpfc_intr_state_check(phba))
9943 return IRQ_NONE;
9944 /* Need to read HA REG for slow-path events */
9945 spin_lock_irqsave(&phba->hbalock, iflag);
9946 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9947 goto unplug_error;
9948 /* If somebody is waiting to handle an eratt don't process it
9949 * here. The brdkill function will do this.
9951 if (phba->link_flag & LS_IGNORE_ERATT)
9952 ha_copy &= ~HA_ERATT;
9953 /* Check the need for handling ERATT in interrupt handler */
9954 if (ha_copy & HA_ERATT) {
9955 if (phba->hba_flag & HBA_ERATT_HANDLED)
9956 /* ERATT polling has handled ERATT */
9957 ha_copy &= ~HA_ERATT;
9958 else
9959 /* Indicate interrupt handler handles ERATT */
9960 phba->hba_flag |= HBA_ERATT_HANDLED;
9964 * If there is deferred error attention, do not check for any
9965 * interrupt.
9967 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9968 spin_unlock_irqrestore(&phba->hbalock, iflag);
9969 return IRQ_NONE;
9972 /* Clear up only attention source related to slow-path */
9973 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9974 goto unplug_error;
9976 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9977 HC_LAINT_ENA | HC_ERINT_ENA),
9978 phba->HCregaddr);
9979 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9980 phba->HAregaddr);
9981 writel(hc_copy, phba->HCregaddr);
9982 readl(phba->HAregaddr); /* flush */
9983 spin_unlock_irqrestore(&phba->hbalock, iflag);
9984 } else
9985 ha_copy = phba->ha_copy;
9987 work_ha_copy = ha_copy & phba->work_ha_mask;
9989 if (work_ha_copy) {
9990 if (work_ha_copy & HA_LATT) {
9991 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
9993 * Turn off Link Attention interrupts
9994 * until CLEAR_LA done
9996 spin_lock_irqsave(&phba->hbalock, iflag);
9997 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9998 if (lpfc_readl(phba->HCregaddr, &control))
9999 goto unplug_error;
10000 control &= ~HC_LAINT_ENA;
10001 writel(control, phba->HCregaddr);
10002 readl(phba->HCregaddr); /* flush */
10003 spin_unlock_irqrestore(&phba->hbalock, iflag);
10005 else
10006 work_ha_copy &= ~HA_LATT;
10009 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
10011 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10012 * the only slow ring.
10014 status = (work_ha_copy &
10015 (HA_RXMASK << (4*LPFC_ELS_RING)));
10016 status >>= (4*LPFC_ELS_RING);
10017 if (status & HA_RXMASK) {
10018 spin_lock_irqsave(&phba->hbalock, iflag);
10019 if (lpfc_readl(phba->HCregaddr, &control))
10020 goto unplug_error;
10022 lpfc_debugfs_slow_ring_trc(phba,
10023 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10024 control, status,
10025 (uint32_t)phba->sli.slistat.sli_intr);
10027 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
10028 lpfc_debugfs_slow_ring_trc(phba,
10029 "ISR Disable ring:"
10030 "pwork:x%x hawork:x%x wait:x%x",
10031 phba->work_ha, work_ha_copy,
10032 (uint32_t)((unsigned long)
10033 &phba->work_waitq));
10035 control &=
10036 ~(HC_R0INT_ENA << LPFC_ELS_RING);
10037 writel(control, phba->HCregaddr);
10038 readl(phba->HCregaddr); /* flush */
10040 else {
10041 lpfc_debugfs_slow_ring_trc(phba,
10042 "ISR slow ring: pwork:"
10043 "x%x hawork:x%x wait:x%x",
10044 phba->work_ha, work_ha_copy,
10045 (uint32_t)((unsigned long)
10046 &phba->work_waitq));
10048 spin_unlock_irqrestore(&phba->hbalock, iflag);
10051 spin_lock_irqsave(&phba->hbalock, iflag);
10052 if (work_ha_copy & HA_ERATT) {
10053 if (lpfc_sli_read_hs(phba))
10054 goto unplug_error;
10056 * Check if there is a deferred error condition
10057 * is active
10059 if ((HS_FFER1 & phba->work_hs) &&
10060 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10061 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10062 phba->work_hs)) {
10063 phba->hba_flag |= DEFER_ERATT;
10064 /* Clear all interrupt enable conditions */
10065 writel(0, phba->HCregaddr);
10066 readl(phba->HCregaddr);
10070 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
10071 pmb = phba->sli.mbox_active;
10072 pmbox = &pmb->u.mb;
10073 mbox = phba->mbox;
10074 vport = pmb->vport;
10076 /* First check out the status word */
10077 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10078 if (pmbox->mbxOwner != OWN_HOST) {
10079 spin_unlock_irqrestore(&phba->hbalock, iflag);
10081 * Stray Mailbox Interrupt, mbxCommand <cmd>
10082 * mbxStatus <status>
10084 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10085 LOG_SLI,
10086 "(%d):0304 Stray Mailbox "
10087 "Interrupt mbxCommand x%x "
10088 "mbxStatus x%x\n",
10089 (vport ? vport->vpi : 0),
10090 pmbox->mbxCommand,
10091 pmbox->mbxStatus);
10092 /* clear mailbox attention bit */
10093 work_ha_copy &= ~HA_MBATT;
10094 } else {
10095 phba->sli.mbox_active = NULL;
10096 spin_unlock_irqrestore(&phba->hbalock, iflag);
10097 phba->last_completion_time = jiffies;
10098 del_timer(&phba->sli.mbox_tmo);
10099 if (pmb->mbox_cmpl) {
10100 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10101 MAILBOX_CMD_SIZE);
10102 if (pmb->out_ext_byte_len &&
10103 pmb->context2)
10104 lpfc_sli_pcimem_bcopy(
10105 phba->mbox_ext,
10106 pmb->context2,
10107 pmb->out_ext_byte_len);
10109 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10110 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10112 lpfc_debugfs_disc_trc(vport,
10113 LPFC_DISC_TRC_MBOX_VPORT,
10114 "MBOX dflt rpi: : "
10115 "status:x%x rpi:x%x",
10116 (uint32_t)pmbox->mbxStatus,
10117 pmbox->un.varWords[0], 0);
10119 if (!pmbox->mbxStatus) {
10120 mp = (struct lpfc_dmabuf *)
10121 (pmb->context1);
10122 ndlp = (struct lpfc_nodelist *)
10123 pmb->context2;
10125 /* Reg_LOGIN of dflt RPI was
10126 * successful. new lets get
10127 * rid of the RPI using the
10128 * same mbox buffer.
10130 lpfc_unreg_login(phba,
10131 vport->vpi,
10132 pmbox->un.varWords[0],
10133 pmb);
10134 pmb->mbox_cmpl =
10135 lpfc_mbx_cmpl_dflt_rpi;
10136 pmb->context1 = mp;
10137 pmb->context2 = ndlp;
10138 pmb->vport = vport;
10139 rc = lpfc_sli_issue_mbox(phba,
10140 pmb,
10141 MBX_NOWAIT);
10142 if (rc != MBX_BUSY)
10143 lpfc_printf_log(phba,
10144 KERN_ERR,
10145 LOG_MBOX | LOG_SLI,
10146 "0350 rc should have"
10147 "been MBX_BUSY\n");
10148 if (rc != MBX_NOT_FINISHED)
10149 goto send_current_mbox;
10152 spin_lock_irqsave(
10153 &phba->pport->work_port_lock,
10154 iflag);
10155 phba->pport->work_port_events &=
10156 ~WORKER_MBOX_TMO;
10157 spin_unlock_irqrestore(
10158 &phba->pport->work_port_lock,
10159 iflag);
10160 lpfc_mbox_cmpl_put(phba, pmb);
10162 } else
10163 spin_unlock_irqrestore(&phba->hbalock, iflag);
10165 if ((work_ha_copy & HA_MBATT) &&
10166 (phba->sli.mbox_active == NULL)) {
10167 send_current_mbox:
10168 /* Process next mailbox command if there is one */
10169 do {
10170 rc = lpfc_sli_issue_mbox(phba, NULL,
10171 MBX_NOWAIT);
10172 } while (rc == MBX_NOT_FINISHED);
10173 if (rc != MBX_SUCCESS)
10174 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10175 LOG_SLI, "0349 rc should be "
10176 "MBX_SUCCESS\n");
10179 spin_lock_irqsave(&phba->hbalock, iflag);
10180 phba->work_ha |= work_ha_copy;
10181 spin_unlock_irqrestore(&phba->hbalock, iflag);
10182 lpfc_worker_wake_up(phba);
10184 return IRQ_HANDLED;
10185 unplug_error:
10186 spin_unlock_irqrestore(&phba->hbalock, iflag);
10187 return IRQ_HANDLED;
10189 } /* lpfc_sli_sp_intr_handler */
10192 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
10193 * @irq: Interrupt number.
10194 * @dev_id: The device context pointer.
10196 * This function is directly called from the PCI layer as an interrupt
10197 * service routine when device with SLI-3 interface spec is enabled with
10198 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10199 * ring event in the HBA. However, when the device is enabled with either
10200 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10201 * device-level interrupt handler. When the PCI slot is in error recovery
10202 * or the HBA is undergoing initialization, the interrupt handler will not
10203 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10204 * the intrrupt context. This function is called without any lock held.
10205 * It gets the hbalock to access and update SLI data structures.
10207 * This function returns IRQ_HANDLED when interrupt is handled else it
10208 * returns IRQ_NONE.
10210 irqreturn_t
10211 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
10213 struct lpfc_hba *phba;
10214 uint32_t ha_copy;
10215 unsigned long status;
10216 unsigned long iflag;
10218 /* Get the driver's phba structure from the dev_id and
10219 * assume the HBA is not interrupting.
10221 phba = (struct lpfc_hba *) dev_id;
10223 if (unlikely(!phba))
10224 return IRQ_NONE;
10227 * Stuff needs to be attented to when this function is invoked as an
10228 * individual interrupt handler in MSI-X multi-message interrupt mode
10230 if (phba->intr_type == MSIX) {
10231 /* Check device state for handling interrupt */
10232 if (lpfc_intr_state_check(phba))
10233 return IRQ_NONE;
10234 /* Need to read HA REG for FCP ring and other ring events */
10235 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10236 return IRQ_HANDLED;
10237 /* Clear up only attention source related to fast-path */
10238 spin_lock_irqsave(&phba->hbalock, iflag);
10240 * If there is deferred error attention, do not check for
10241 * any interrupt.
10243 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10244 spin_unlock_irqrestore(&phba->hbalock, iflag);
10245 return IRQ_NONE;
10247 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10248 phba->HAregaddr);
10249 readl(phba->HAregaddr); /* flush */
10250 spin_unlock_irqrestore(&phba->hbalock, iflag);
10251 } else
10252 ha_copy = phba->ha_copy;
10255 * Process all events on FCP ring. Take the optimized path for FCP IO.
10257 ha_copy &= ~(phba->work_ha_mask);
10259 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10260 status >>= (4*LPFC_FCP_RING);
10261 if (status & HA_RXMASK)
10262 lpfc_sli_handle_fast_ring_event(phba,
10263 &phba->sli.ring[LPFC_FCP_RING],
10264 status);
10266 if (phba->cfg_multi_ring_support == 2) {
10268 * Process all events on extra ring. Take the optimized path
10269 * for extra ring IO.
10271 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10272 status >>= (4*LPFC_EXTRA_RING);
10273 if (status & HA_RXMASK) {
10274 lpfc_sli_handle_fast_ring_event(phba,
10275 &phba->sli.ring[LPFC_EXTRA_RING],
10276 status);
10279 return IRQ_HANDLED;
10280 } /* lpfc_sli_fp_intr_handler */
10283 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
10284 * @irq: Interrupt number.
10285 * @dev_id: The device context pointer.
10287 * This function is the HBA device-level interrupt handler to device with
10288 * SLI-3 interface spec, called from the PCI layer when either MSI or
10289 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10290 * requires driver attention. This function invokes the slow-path interrupt
10291 * attention handling function and fast-path interrupt attention handling
10292 * function in turn to process the relevant HBA attention events. This
10293 * function is called without any lock held. It gets the hbalock to access
10294 * and update SLI data structures.
10296 * This function returns IRQ_HANDLED when interrupt is handled, else it
10297 * returns IRQ_NONE.
10299 irqreturn_t
10300 lpfc_sli_intr_handler(int irq, void *dev_id)
10302 struct lpfc_hba *phba;
10303 irqreturn_t sp_irq_rc, fp_irq_rc;
10304 unsigned long status1, status2;
10305 uint32_t hc_copy;
10308 * Get the driver's phba structure from the dev_id and
10309 * assume the HBA is not interrupting.
10311 phba = (struct lpfc_hba *) dev_id;
10313 if (unlikely(!phba))
10314 return IRQ_NONE;
10316 /* Check device state for handling interrupt */
10317 if (lpfc_intr_state_check(phba))
10318 return IRQ_NONE;
10320 spin_lock(&phba->hbalock);
10321 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10322 spin_unlock(&phba->hbalock);
10323 return IRQ_HANDLED;
10326 if (unlikely(!phba->ha_copy)) {
10327 spin_unlock(&phba->hbalock);
10328 return IRQ_NONE;
10329 } else if (phba->ha_copy & HA_ERATT) {
10330 if (phba->hba_flag & HBA_ERATT_HANDLED)
10331 /* ERATT polling has handled ERATT */
10332 phba->ha_copy &= ~HA_ERATT;
10333 else
10334 /* Indicate interrupt handler handles ERATT */
10335 phba->hba_flag |= HBA_ERATT_HANDLED;
10339 * If there is deferred error attention, do not check for any interrupt.
10341 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10342 spin_unlock(&phba->hbalock);
10343 return IRQ_NONE;
10346 /* Clear attention sources except link and error attentions */
10347 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10348 spin_unlock(&phba->hbalock);
10349 return IRQ_HANDLED;
10351 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10352 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10353 phba->HCregaddr);
10354 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
10355 writel(hc_copy, phba->HCregaddr);
10356 readl(phba->HAregaddr); /* flush */
10357 spin_unlock(&phba->hbalock);
10360 * Invokes slow-path host attention interrupt handling as appropriate.
10363 /* status of events with mailbox and link attention */
10364 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10366 /* status of events with ELS ring */
10367 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10368 status2 >>= (4*LPFC_ELS_RING);
10370 if (status1 || (status2 & HA_RXMASK))
10371 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
10372 else
10373 sp_irq_rc = IRQ_NONE;
10376 * Invoke fast-path host attention interrupt handling as appropriate.
10379 /* status of events with FCP ring */
10380 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10381 status1 >>= (4*LPFC_FCP_RING);
10383 /* status of events with extra ring */
10384 if (phba->cfg_multi_ring_support == 2) {
10385 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10386 status2 >>= (4*LPFC_EXTRA_RING);
10387 } else
10388 status2 = 0;
10390 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
10391 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
10392 else
10393 fp_irq_rc = IRQ_NONE;
10395 /* Return device-level interrupt handling status */
10396 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
10397 } /* lpfc_sli_intr_handler */
10400 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10401 * @phba: pointer to lpfc hba data structure.
10403 * This routine is invoked by the worker thread to process all the pending
10404 * SLI4 FCP abort XRI events.
10406 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10408 struct lpfc_cq_event *cq_event;
10410 /* First, declare the fcp xri abort event has been handled */
10411 spin_lock_irq(&phba->hbalock);
10412 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10413 spin_unlock_irq(&phba->hbalock);
10414 /* Now, handle all the fcp xri abort events */
10415 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10416 /* Get the first event from the head of the event queue */
10417 spin_lock_irq(&phba->hbalock);
10418 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10419 cq_event, struct lpfc_cq_event, list);
10420 spin_unlock_irq(&phba->hbalock);
10421 /* Notify aborted XRI for FCP work queue */
10422 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10423 /* Free the event processed back to the free pool */
10424 lpfc_sli4_cq_event_release(phba, cq_event);
10429 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10430 * @phba: pointer to lpfc hba data structure.
10432 * This routine is invoked by the worker thread to process all the pending
10433 * SLI4 els abort xri events.
10435 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10437 struct lpfc_cq_event *cq_event;
10439 /* First, declare the els xri abort event has been handled */
10440 spin_lock_irq(&phba->hbalock);
10441 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10442 spin_unlock_irq(&phba->hbalock);
10443 /* Now, handle all the els xri abort events */
10444 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10445 /* Get the first event from the head of the event queue */
10446 spin_lock_irq(&phba->hbalock);
10447 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10448 cq_event, struct lpfc_cq_event, list);
10449 spin_unlock_irq(&phba->hbalock);
10450 /* Notify aborted XRI for ELS work queue */
10451 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10452 /* Free the event processed back to the free pool */
10453 lpfc_sli4_cq_event_release(phba, cq_event);
10458 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10459 * @phba: pointer to lpfc hba data structure
10460 * @pIocbIn: pointer to the rspiocbq
10461 * @pIocbOut: pointer to the cmdiocbq
10462 * @wcqe: pointer to the complete wcqe
10464 * This routine transfers the fields of a command iocbq to a response iocbq
10465 * by copying all the IOCB fields from command iocbq and transferring the
10466 * completion status information from the complete wcqe.
10468 static void
10469 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10470 struct lpfc_iocbq *pIocbIn,
10471 struct lpfc_iocbq *pIocbOut,
10472 struct lpfc_wcqe_complete *wcqe)
10474 unsigned long iflags;
10475 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10477 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10478 sizeof(struct lpfc_iocbq) - offset);
10479 /* Map WCQE parameters into irspiocb parameters */
10480 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10481 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10482 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10483 pIocbIn->iocb.un.fcpi.fcpi_parm =
10484 pIocbOut->iocb.un.fcpi.fcpi_parm -
10485 wcqe->total_data_placed;
10486 else
10487 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10488 else {
10489 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
10490 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10493 /* Pick up HBA exchange busy condition */
10494 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10495 spin_lock_irqsave(&phba->hbalock, iflags);
10496 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10497 spin_unlock_irqrestore(&phba->hbalock, iflags);
10502 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10503 * @phba: Pointer to HBA context object.
10504 * @wcqe: Pointer to work-queue completion queue entry.
10506 * This routine handles an ELS work-queue completion event and construct
10507 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10508 * discovery engine to handle.
10510 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10512 static struct lpfc_iocbq *
10513 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10514 struct lpfc_iocbq *irspiocbq)
10516 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10517 struct lpfc_iocbq *cmdiocbq;
10518 struct lpfc_wcqe_complete *wcqe;
10519 unsigned long iflags;
10521 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10522 spin_lock_irqsave(&phba->hbalock, iflags);
10523 pring->stats.iocb_event++;
10524 /* Look up the ELS command IOCB and create pseudo response IOCB */
10525 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10526 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10527 spin_unlock_irqrestore(&phba->hbalock, iflags);
10529 if (unlikely(!cmdiocbq)) {
10530 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10531 "0386 ELS complete with no corresponding "
10532 "cmdiocb: iotag (%d)\n",
10533 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10534 lpfc_sli_release_iocbq(phba, irspiocbq);
10535 return NULL;
10538 /* Fake the irspiocbq and copy necessary response information */
10539 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
10541 return irspiocbq;
10545 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10546 * @phba: Pointer to HBA context object.
10547 * @cqe: Pointer to mailbox completion queue entry.
10549 * This routine process a mailbox completion queue entry with asynchrous
10550 * event.
10552 * Return: true if work posted to worker thread, otherwise false.
10554 static bool
10555 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10557 struct lpfc_cq_event *cq_event;
10558 unsigned long iflags;
10560 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10561 "0392 Async Event: word0:x%x, word1:x%x, "
10562 "word2:x%x, word3:x%x\n", mcqe->word0,
10563 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10565 /* Allocate a new internal CQ_EVENT entry */
10566 cq_event = lpfc_sli4_cq_event_alloc(phba);
10567 if (!cq_event) {
10568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10569 "0394 Failed to allocate CQ_EVENT entry\n");
10570 return false;
10573 /* Move the CQE into an asynchronous event entry */
10574 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10575 spin_lock_irqsave(&phba->hbalock, iflags);
10576 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10577 /* Set the async event flag */
10578 phba->hba_flag |= ASYNC_EVENT;
10579 spin_unlock_irqrestore(&phba->hbalock, iflags);
10581 return true;
10585 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10586 * @phba: Pointer to HBA context object.
10587 * @cqe: Pointer to mailbox completion queue entry.
10589 * This routine process a mailbox completion queue entry with mailbox
10590 * completion event.
10592 * Return: true if work posted to worker thread, otherwise false.
10594 static bool
10595 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10597 uint32_t mcqe_status;
10598 MAILBOX_t *mbox, *pmbox;
10599 struct lpfc_mqe *mqe;
10600 struct lpfc_vport *vport;
10601 struct lpfc_nodelist *ndlp;
10602 struct lpfc_dmabuf *mp;
10603 unsigned long iflags;
10604 LPFC_MBOXQ_t *pmb;
10605 bool workposted = false;
10606 int rc;
10608 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10609 if (!bf_get(lpfc_trailer_completed, mcqe))
10610 goto out_no_mqe_complete;
10612 /* Get the reference to the active mbox command */
10613 spin_lock_irqsave(&phba->hbalock, iflags);
10614 pmb = phba->sli.mbox_active;
10615 if (unlikely(!pmb)) {
10616 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10617 "1832 No pending MBOX command to handle\n");
10618 spin_unlock_irqrestore(&phba->hbalock, iflags);
10619 goto out_no_mqe_complete;
10621 spin_unlock_irqrestore(&phba->hbalock, iflags);
10622 mqe = &pmb->u.mqe;
10623 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10624 mbox = phba->mbox;
10625 vport = pmb->vport;
10627 /* Reset heartbeat timer */
10628 phba->last_completion_time = jiffies;
10629 del_timer(&phba->sli.mbox_tmo);
10631 /* Move mbox data to caller's mailbox region, do endian swapping */
10632 if (pmb->mbox_cmpl && mbox)
10633 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
10636 * For mcqe errors, conditionally move a modified error code to
10637 * the mbox so that the error will not be missed.
10639 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10640 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10641 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10642 bf_set(lpfc_mqe_status, mqe,
10643 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10645 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10646 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10647 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10648 "MBOX dflt rpi: status:x%x rpi:x%x",
10649 mcqe_status,
10650 pmbox->un.varWords[0], 0);
10651 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10652 mp = (struct lpfc_dmabuf *)(pmb->context1);
10653 ndlp = (struct lpfc_nodelist *)pmb->context2;
10654 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10655 * RID of the PPI using the same mbox buffer.
10657 lpfc_unreg_login(phba, vport->vpi,
10658 pmbox->un.varWords[0], pmb);
10659 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10660 pmb->context1 = mp;
10661 pmb->context2 = ndlp;
10662 pmb->vport = vport;
10663 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10664 if (rc != MBX_BUSY)
10665 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10666 LOG_SLI, "0385 rc should "
10667 "have been MBX_BUSY\n");
10668 if (rc != MBX_NOT_FINISHED)
10669 goto send_current_mbox;
10672 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10673 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10674 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10676 /* There is mailbox completion work to do */
10677 spin_lock_irqsave(&phba->hbalock, iflags);
10678 __lpfc_mbox_cmpl_put(phba, pmb);
10679 phba->work_ha |= HA_MBATT;
10680 spin_unlock_irqrestore(&phba->hbalock, iflags);
10681 workposted = true;
10683 send_current_mbox:
10684 spin_lock_irqsave(&phba->hbalock, iflags);
10685 /* Release the mailbox command posting token */
10686 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10687 /* Setting active mailbox pointer need to be in sync to flag clear */
10688 phba->sli.mbox_active = NULL;
10689 spin_unlock_irqrestore(&phba->hbalock, iflags);
10690 /* Wake up worker thread to post the next pending mailbox command */
10691 lpfc_worker_wake_up(phba);
10692 out_no_mqe_complete:
10693 if (bf_get(lpfc_trailer_consumed, mcqe))
10694 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10695 return workposted;
10699 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10700 * @phba: Pointer to HBA context object.
10701 * @cqe: Pointer to mailbox completion queue entry.
10703 * This routine process a mailbox completion queue entry, it invokes the
10704 * proper mailbox complete handling or asynchrous event handling routine
10705 * according to the MCQE's async bit.
10707 * Return: true if work posted to worker thread, otherwise false.
10709 static bool
10710 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10712 struct lpfc_mcqe mcqe;
10713 bool workposted;
10715 /* Copy the mailbox MCQE and convert endian order as needed */
10716 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10718 /* Invoke the proper event handling routine */
10719 if (!bf_get(lpfc_trailer_async, &mcqe))
10720 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10721 else
10722 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10723 return workposted;
10727 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10728 * @phba: Pointer to HBA context object.
10729 * @wcqe: Pointer to work-queue completion queue entry.
10731 * This routine handles an ELS work-queue completion event.
10733 * Return: true if work posted to worker thread, otherwise false.
10735 static bool
10736 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10737 struct lpfc_wcqe_complete *wcqe)
10739 struct lpfc_iocbq *irspiocbq;
10740 unsigned long iflags;
10741 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
10743 /* Get an irspiocbq for later ELS response processing use */
10744 irspiocbq = lpfc_sli_get_iocbq(phba);
10745 if (!irspiocbq) {
10746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10747 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10748 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10749 pring->txq_cnt, phba->iocb_cnt,
10750 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10751 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
10752 return false;
10755 /* Save off the slow-path queue event for work thread to process */
10756 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
10757 spin_lock_irqsave(&phba->hbalock, iflags);
10758 list_add_tail(&irspiocbq->cq_event.list,
10759 &phba->sli4_hba.sp_queue_event);
10760 phba->hba_flag |= HBA_SP_QUEUE_EVT;
10761 spin_unlock_irqrestore(&phba->hbalock, iflags);
10763 return true;
10767 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10768 * @phba: Pointer to HBA context object.
10769 * @wcqe: Pointer to work-queue completion queue entry.
10771 * This routine handles slow-path WQ entry comsumed event by invoking the
10772 * proper WQ release routine to the slow-path WQ.
10774 static void
10775 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10776 struct lpfc_wcqe_release *wcqe)
10778 /* Check for the slow-path ELS work queue */
10779 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10780 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10781 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10782 else
10783 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10784 "2579 Slow-path wqe consume event carries "
10785 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10786 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10787 phba->sli4_hba.els_wq->queue_id);
10791 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10792 * @phba: Pointer to HBA context object.
10793 * @cq: Pointer to a WQ completion queue.
10794 * @wcqe: Pointer to work-queue completion queue entry.
10796 * This routine handles an XRI abort event.
10798 * Return: true if work posted to worker thread, otherwise false.
10800 static bool
10801 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10802 struct lpfc_queue *cq,
10803 struct sli4_wcqe_xri_aborted *wcqe)
10805 bool workposted = false;
10806 struct lpfc_cq_event *cq_event;
10807 unsigned long iflags;
10809 /* Allocate a new internal CQ_EVENT entry */
10810 cq_event = lpfc_sli4_cq_event_alloc(phba);
10811 if (!cq_event) {
10812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10813 "0602 Failed to allocate CQ_EVENT entry\n");
10814 return false;
10817 /* Move the CQE into the proper xri abort event list */
10818 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10819 switch (cq->subtype) {
10820 case LPFC_FCP:
10821 spin_lock_irqsave(&phba->hbalock, iflags);
10822 list_add_tail(&cq_event->list,
10823 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10824 /* Set the fcp xri abort event flag */
10825 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10826 spin_unlock_irqrestore(&phba->hbalock, iflags);
10827 workposted = true;
10828 break;
10829 case LPFC_ELS:
10830 spin_lock_irqsave(&phba->hbalock, iflags);
10831 list_add_tail(&cq_event->list,
10832 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10833 /* Set the els xri abort event flag */
10834 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10835 spin_unlock_irqrestore(&phba->hbalock, iflags);
10836 workposted = true;
10837 break;
10838 default:
10839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10840 "0603 Invalid work queue CQE subtype (x%x)\n",
10841 cq->subtype);
10842 workposted = false;
10843 break;
10845 return workposted;
10849 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
10850 * @phba: Pointer to HBA context object.
10851 * @rcqe: Pointer to receive-queue completion queue entry.
10853 * This routine process a receive-queue completion queue entry.
10855 * Return: true if work posted to worker thread, otherwise false.
10857 static bool
10858 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
10860 bool workposted = false;
10861 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10862 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10863 struct hbq_dmabuf *dma_buf;
10864 uint32_t status, rq_id;
10865 unsigned long iflags;
10867 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10868 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10869 else
10870 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10871 if (rq_id != hrq->queue_id)
10872 goto out;
10874 status = bf_get(lpfc_rcqe_status, rcqe);
10875 switch (status) {
10876 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10878 "2537 Receive Frame Truncated!!\n");
10879 case FC_STATUS_RQ_SUCCESS:
10880 lpfc_sli4_rq_release(hrq, drq);
10881 spin_lock_irqsave(&phba->hbalock, iflags);
10882 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10883 if (!dma_buf) {
10884 spin_unlock_irqrestore(&phba->hbalock, iflags);
10885 goto out;
10887 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
10888 /* save off the frame for the word thread to process */
10889 list_add_tail(&dma_buf->cq_event.list,
10890 &phba->sli4_hba.sp_queue_event);
10891 /* Frame received */
10892 phba->hba_flag |= HBA_SP_QUEUE_EVT;
10893 spin_unlock_irqrestore(&phba->hbalock, iflags);
10894 workposted = true;
10895 break;
10896 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10897 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10898 /* Post more buffers if possible */
10899 spin_lock_irqsave(&phba->hbalock, iflags);
10900 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10901 spin_unlock_irqrestore(&phba->hbalock, iflags);
10902 workposted = true;
10903 break;
10905 out:
10906 return workposted;
10910 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10911 * @phba: Pointer to HBA context object.
10912 * @cq: Pointer to the completion queue.
10913 * @wcqe: Pointer to a completion queue entry.
10915 * This routine process a slow-path work-queue or receive queue completion queue
10916 * entry.
10918 * Return: true if work posted to worker thread, otherwise false.
10920 static bool
10921 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10922 struct lpfc_cqe *cqe)
10924 struct lpfc_cqe cqevt;
10925 bool workposted = false;
10927 /* Copy the work queue CQE and convert endian order if needed */
10928 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
10930 /* Check and process for different type of WCQE and dispatch */
10931 switch (bf_get(lpfc_cqe_code, &cqevt)) {
10932 case CQE_CODE_COMPL_WQE:
10933 /* Process the WQ/RQ complete event */
10934 phba->last_completion_time = jiffies;
10935 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
10936 (struct lpfc_wcqe_complete *)&cqevt);
10937 break;
10938 case CQE_CODE_RELEASE_WQE:
10939 /* Process the WQ release event */
10940 lpfc_sli4_sp_handle_rel_wcqe(phba,
10941 (struct lpfc_wcqe_release *)&cqevt);
10942 break;
10943 case CQE_CODE_XRI_ABORTED:
10944 /* Process the WQ XRI abort event */
10945 phba->last_completion_time = jiffies;
10946 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
10947 (struct sli4_wcqe_xri_aborted *)&cqevt);
10948 break;
10949 case CQE_CODE_RECEIVE:
10950 case CQE_CODE_RECEIVE_V1:
10951 /* Process the RQ event */
10952 phba->last_completion_time = jiffies;
10953 workposted = lpfc_sli4_sp_handle_rcqe(phba,
10954 (struct lpfc_rcqe *)&cqevt);
10955 break;
10956 default:
10957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10958 "0388 Not a valid WCQE code: x%x\n",
10959 bf_get(lpfc_cqe_code, &cqevt));
10960 break;
10962 return workposted;
10966 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10967 * @phba: Pointer to HBA context object.
10968 * @eqe: Pointer to fast-path event queue entry.
10970 * This routine process a event queue entry from the slow-path event queue.
10971 * It will check the MajorCode and MinorCode to determine this is for a
10972 * completion event on a completion queue, if not, an error shall be logged
10973 * and just return. Otherwise, it will get to the corresponding completion
10974 * queue and process all the entries on that completion queue, rearm the
10975 * completion queue, and then return.
10978 static void
10979 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10981 struct lpfc_queue *cq = NULL, *childq, *speq;
10982 struct lpfc_cqe *cqe;
10983 bool workposted = false;
10984 int ecount = 0;
10985 uint16_t cqid;
10987 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
10988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10989 "0359 Not a valid slow-path completion "
10990 "event: majorcode=x%x, minorcode=x%x\n",
10991 bf_get_le32(lpfc_eqe_major_code, eqe),
10992 bf_get_le32(lpfc_eqe_minor_code, eqe));
10993 return;
10996 /* Get the reference to the corresponding CQ */
10997 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
10999 /* Search for completion queue pointer matching this cqid */
11000 speq = phba->sli4_hba.sp_eq;
11001 list_for_each_entry(childq, &speq->child_list, list) {
11002 if (childq->queue_id == cqid) {
11003 cq = childq;
11004 break;
11007 if (unlikely(!cq)) {
11008 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11010 "0365 Slow-path CQ identifier "
11011 "(%d) does not exist\n", cqid);
11012 return;
11015 /* Process all the entries to the CQ */
11016 switch (cq->type) {
11017 case LPFC_MCQ:
11018 while ((cqe = lpfc_sli4_cq_get(cq))) {
11019 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
11020 if (!(++ecount % cq->entry_repost))
11021 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11023 break;
11024 case LPFC_WCQ:
11025 while ((cqe = lpfc_sli4_cq_get(cq))) {
11026 if (cq->subtype == LPFC_FCP)
11027 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11028 cqe);
11029 else
11030 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11031 cqe);
11032 if (!(++ecount % cq->entry_repost))
11033 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11035 break;
11036 default:
11037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11038 "0370 Invalid completion queue type (%d)\n",
11039 cq->type);
11040 return;
11043 /* Catch the no cq entry condition, log an error */
11044 if (unlikely(ecount == 0))
11045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11046 "0371 No entry from the CQ: identifier "
11047 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11049 /* In any case, flash and re-arm the RCQ */
11050 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11052 /* wake up worker thread if there are works to be done */
11053 if (workposted)
11054 lpfc_worker_wake_up(phba);
11058 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11059 * @eqe: Pointer to fast-path completion queue entry.
11061 * This routine process a fast-path work queue completion entry from fast-path
11062 * event queue for FCP command response completion.
11064 static void
11065 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11066 struct lpfc_wcqe_complete *wcqe)
11068 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11069 struct lpfc_iocbq *cmdiocbq;
11070 struct lpfc_iocbq irspiocbq;
11071 unsigned long iflags;
11073 spin_lock_irqsave(&phba->hbalock, iflags);
11074 pring->stats.iocb_event++;
11075 spin_unlock_irqrestore(&phba->hbalock, iflags);
11077 /* Check for response status */
11078 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11079 /* If resource errors reported from HBA, reduce queue
11080 * depth of the SCSI device.
11082 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11083 IOSTAT_LOCAL_REJECT) &&
11084 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11085 phba->lpfc_rampdown_queue_depth(phba);
11087 /* Log the error status */
11088 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11089 "0373 FCP complete error: status=x%x, "
11090 "hw_status=x%x, total_data_specified=%d, "
11091 "parameter=x%x, word3=x%x\n",
11092 bf_get(lpfc_wcqe_c_status, wcqe),
11093 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11094 wcqe->total_data_placed, wcqe->parameter,
11095 wcqe->word3);
11098 /* Look up the FCP command IOCB and create pseudo response IOCB */
11099 spin_lock_irqsave(&phba->hbalock, iflags);
11100 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11101 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11102 spin_unlock_irqrestore(&phba->hbalock, iflags);
11103 if (unlikely(!cmdiocbq)) {
11104 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11105 "0374 FCP complete with no corresponding "
11106 "cmdiocb: iotag (%d)\n",
11107 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11108 return;
11110 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11111 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11112 "0375 FCP cmdiocb not callback function "
11113 "iotag: (%d)\n",
11114 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11115 return;
11118 /* Fake the irspiocb and copy necessary response information */
11119 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
11121 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11122 spin_lock_irqsave(&phba->hbalock, iflags);
11123 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11124 spin_unlock_irqrestore(&phba->hbalock, iflags);
11127 /* Pass the cmd_iocb and the rsp state to the upper layer */
11128 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11132 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11133 * @phba: Pointer to HBA context object.
11134 * @cq: Pointer to completion queue.
11135 * @wcqe: Pointer to work-queue completion queue entry.
11137 * This routine handles an fast-path WQ entry comsumed event by invoking the
11138 * proper WQ release routine to the slow-path WQ.
11140 static void
11141 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11142 struct lpfc_wcqe_release *wcqe)
11144 struct lpfc_queue *childwq;
11145 bool wqid_matched = false;
11146 uint16_t fcp_wqid;
11148 /* Check for fast-path FCP work queue release */
11149 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11150 list_for_each_entry(childwq, &cq->child_list, list) {
11151 if (childwq->queue_id == fcp_wqid) {
11152 lpfc_sli4_wq_release(childwq,
11153 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11154 wqid_matched = true;
11155 break;
11158 /* Report warning log message if no match found */
11159 if (wqid_matched != true)
11160 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11161 "2580 Fast-path wqe consume event carries "
11162 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11166 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11167 * @cq: Pointer to the completion queue.
11168 * @eqe: Pointer to fast-path completion queue entry.
11170 * This routine process a fast-path work queue completion entry from fast-path
11171 * event queue for FCP command response completion.
11173 static int
11174 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11175 struct lpfc_cqe *cqe)
11177 struct lpfc_wcqe_release wcqe;
11178 bool workposted = false;
11180 /* Copy the work queue CQE and convert endian order if needed */
11181 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11183 /* Check and process for different type of WCQE and dispatch */
11184 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11185 case CQE_CODE_COMPL_WQE:
11186 /* Process the WQ complete event */
11187 phba->last_completion_time = jiffies;
11188 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11189 (struct lpfc_wcqe_complete *)&wcqe);
11190 break;
11191 case CQE_CODE_RELEASE_WQE:
11192 /* Process the WQ release event */
11193 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11194 (struct lpfc_wcqe_release *)&wcqe);
11195 break;
11196 case CQE_CODE_XRI_ABORTED:
11197 /* Process the WQ XRI abort event */
11198 phba->last_completion_time = jiffies;
11199 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11200 (struct sli4_wcqe_xri_aborted *)&wcqe);
11201 break;
11202 default:
11203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11204 "0144 Not a valid WCQE code: x%x\n",
11205 bf_get(lpfc_wcqe_c_code, &wcqe));
11206 break;
11208 return workposted;
11212 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11213 * @phba: Pointer to HBA context object.
11214 * @eqe: Pointer to fast-path event queue entry.
11216 * This routine process a event queue entry from the fast-path event queue.
11217 * It will check the MajorCode and MinorCode to determine this is for a
11218 * completion event on a completion queue, if not, an error shall be logged
11219 * and just return. Otherwise, it will get to the corresponding completion
11220 * queue and process all the entries on the completion queue, rearm the
11221 * completion queue, and then return.
11223 static void
11224 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11225 uint32_t fcp_cqidx)
11227 struct lpfc_queue *cq;
11228 struct lpfc_cqe *cqe;
11229 bool workposted = false;
11230 uint16_t cqid;
11231 int ecount = 0;
11233 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
11234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11235 "0366 Not a valid fast-path completion "
11236 "event: majorcode=x%x, minorcode=x%x\n",
11237 bf_get_le32(lpfc_eqe_major_code, eqe),
11238 bf_get_le32(lpfc_eqe_minor_code, eqe));
11239 return;
11242 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11243 if (unlikely(!cq)) {
11244 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11246 "0367 Fast-path completion queue "
11247 "does not exist\n");
11248 return;
11251 /* Get the reference to the corresponding CQ */
11252 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11253 if (unlikely(cqid != cq->queue_id)) {
11254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11255 "0368 Miss-matched fast-path completion "
11256 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11257 cqid, cq->queue_id);
11258 return;
11261 /* Process all the entries to the CQ */
11262 while ((cqe = lpfc_sli4_cq_get(cq))) {
11263 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
11264 if (!(++ecount % cq->entry_repost))
11265 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11268 /* Catch the no cq entry condition */
11269 if (unlikely(ecount == 0))
11270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11271 "0369 No entry from fast-path completion "
11272 "queue fcpcqid=%d\n", cq->queue_id);
11274 /* In any case, flash and re-arm the CQ */
11275 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11277 /* wake up worker thread if there are works to be done */
11278 if (workposted)
11279 lpfc_worker_wake_up(phba);
11282 static void
11283 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11285 struct lpfc_eqe *eqe;
11287 /* walk all the EQ entries and drop on the floor */
11288 while ((eqe = lpfc_sli4_eq_get(eq)))
11291 /* Clear and re-arm the EQ */
11292 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11296 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11297 * @irq: Interrupt number.
11298 * @dev_id: The device context pointer.
11300 * This function is directly called from the PCI layer as an interrupt
11301 * service routine when device with SLI-4 interface spec is enabled with
11302 * MSI-X multi-message interrupt mode and there are slow-path events in
11303 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11304 * interrupt mode, this function is called as part of the device-level
11305 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11306 * undergoing initialization, the interrupt handler will not process the
11307 * interrupt. The link attention and ELS ring attention events are handled
11308 * by the worker thread. The interrupt handler signals the worker thread
11309 * and returns for these events. This function is called without any lock
11310 * held. It gets the hbalock to access and update SLI data structures.
11312 * This function returns IRQ_HANDLED when interrupt is handled else it
11313 * returns IRQ_NONE.
11315 irqreturn_t
11316 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11318 struct lpfc_hba *phba;
11319 struct lpfc_queue *speq;
11320 struct lpfc_eqe *eqe;
11321 unsigned long iflag;
11322 int ecount = 0;
11325 * Get the driver's phba structure from the dev_id
11327 phba = (struct lpfc_hba *)dev_id;
11329 if (unlikely(!phba))
11330 return IRQ_NONE;
11332 /* Get to the EQ struct associated with this vector */
11333 speq = phba->sli4_hba.sp_eq;
11334 if (unlikely(!speq))
11335 return IRQ_NONE;
11337 /* Check device state for handling interrupt */
11338 if (unlikely(lpfc_intr_state_check(phba))) {
11339 /* Check again for link_state with lock held */
11340 spin_lock_irqsave(&phba->hbalock, iflag);
11341 if (phba->link_state < LPFC_LINK_DOWN)
11342 /* Flush, clear interrupt, and rearm the EQ */
11343 lpfc_sli4_eq_flush(phba, speq);
11344 spin_unlock_irqrestore(&phba->hbalock, iflag);
11345 return IRQ_NONE;
11349 * Process all the event on FCP slow-path EQ
11351 while ((eqe = lpfc_sli4_eq_get(speq))) {
11352 lpfc_sli4_sp_handle_eqe(phba, eqe);
11353 if (!(++ecount % speq->entry_repost))
11354 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11357 /* Always clear and re-arm the slow-path EQ */
11358 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11360 /* Catch the no cq entry condition */
11361 if (unlikely(ecount == 0)) {
11362 if (phba->intr_type == MSIX)
11363 /* MSI-X treated interrupt served as no EQ share INT */
11364 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11365 "0357 MSI-X interrupt with no EQE\n");
11366 else
11367 /* Non MSI-X treated on interrupt as EQ share INT */
11368 return IRQ_NONE;
11371 return IRQ_HANDLED;
11372 } /* lpfc_sli4_sp_intr_handler */
11375 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11376 * @irq: Interrupt number.
11377 * @dev_id: The device context pointer.
11379 * This function is directly called from the PCI layer as an interrupt
11380 * service routine when device with SLI-4 interface spec is enabled with
11381 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11382 * ring event in the HBA. However, when the device is enabled with either
11383 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11384 * device-level interrupt handler. When the PCI slot is in error recovery
11385 * or the HBA is undergoing initialization, the interrupt handler will not
11386 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11387 * the intrrupt context. This function is called without any lock held.
11388 * It gets the hbalock to access and update SLI data structures. Note that,
11389 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11390 * equal to that of FCP CQ index.
11392 * This function returns IRQ_HANDLED when interrupt is handled else it
11393 * returns IRQ_NONE.
11395 irqreturn_t
11396 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11398 struct lpfc_hba *phba;
11399 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11400 struct lpfc_queue *fpeq;
11401 struct lpfc_eqe *eqe;
11402 unsigned long iflag;
11403 int ecount = 0;
11404 uint32_t fcp_eqidx;
11406 /* Get the driver's phba structure from the dev_id */
11407 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11408 phba = fcp_eq_hdl->phba;
11409 fcp_eqidx = fcp_eq_hdl->idx;
11411 if (unlikely(!phba))
11412 return IRQ_NONE;
11413 if (unlikely(!phba->sli4_hba.fp_eq))
11414 return IRQ_NONE;
11416 /* Get to the EQ struct associated with this vector */
11417 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11419 /* Check device state for handling interrupt */
11420 if (unlikely(lpfc_intr_state_check(phba))) {
11421 /* Check again for link_state with lock held */
11422 spin_lock_irqsave(&phba->hbalock, iflag);
11423 if (phba->link_state < LPFC_LINK_DOWN)
11424 /* Flush, clear interrupt, and rearm the EQ */
11425 lpfc_sli4_eq_flush(phba, fpeq);
11426 spin_unlock_irqrestore(&phba->hbalock, iflag);
11427 return IRQ_NONE;
11431 * Process all the event on FCP fast-path EQ
11433 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11434 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
11435 if (!(++ecount % fpeq->entry_repost))
11436 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11439 /* Always clear and re-arm the fast-path EQ */
11440 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11442 if (unlikely(ecount == 0)) {
11443 if (phba->intr_type == MSIX)
11444 /* MSI-X treated interrupt served as no EQ share INT */
11445 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11446 "0358 MSI-X interrupt with no EQE\n");
11447 else
11448 /* Non MSI-X treated on interrupt as EQ share INT */
11449 return IRQ_NONE;
11452 return IRQ_HANDLED;
11453 } /* lpfc_sli4_fp_intr_handler */
11456 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11457 * @irq: Interrupt number.
11458 * @dev_id: The device context pointer.
11460 * This function is the device-level interrupt handler to device with SLI-4
11461 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11462 * interrupt mode is enabled and there is an event in the HBA which requires
11463 * driver attention. This function invokes the slow-path interrupt attention
11464 * handling function and fast-path interrupt attention handling function in
11465 * turn to process the relevant HBA attention events. This function is called
11466 * without any lock held. It gets the hbalock to access and update SLI data
11467 * structures.
11469 * This function returns IRQ_HANDLED when interrupt is handled, else it
11470 * returns IRQ_NONE.
11472 irqreturn_t
11473 lpfc_sli4_intr_handler(int irq, void *dev_id)
11475 struct lpfc_hba *phba;
11476 irqreturn_t sp_irq_rc, fp_irq_rc;
11477 bool fp_handled = false;
11478 uint32_t fcp_eqidx;
11480 /* Get the driver's phba structure from the dev_id */
11481 phba = (struct lpfc_hba *)dev_id;
11483 if (unlikely(!phba))
11484 return IRQ_NONE;
11487 * Invokes slow-path host attention interrupt handling as appropriate.
11489 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11492 * Invoke fast-path host attention interrupt handling as appropriate.
11494 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11495 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11496 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11497 if (fp_irq_rc == IRQ_HANDLED)
11498 fp_handled |= true;
11501 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11502 } /* lpfc_sli4_intr_handler */
11505 * lpfc_sli4_queue_free - free a queue structure and associated memory
11506 * @queue: The queue structure to free.
11508 * This function frees a queue structure and the DMAable memory used for
11509 * the host resident queue. This function must be called after destroying the
11510 * queue on the HBA.
11512 void
11513 lpfc_sli4_queue_free(struct lpfc_queue *queue)
11515 struct lpfc_dmabuf *dmabuf;
11517 if (!queue)
11518 return;
11520 while (!list_empty(&queue->page_list)) {
11521 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11522 list);
11523 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
11524 dmabuf->virt, dmabuf->phys);
11525 kfree(dmabuf);
11527 kfree(queue);
11528 return;
11532 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11533 * @phba: The HBA that this queue is being created on.
11534 * @entry_size: The size of each queue entry for this queue.
11535 * @entry count: The number of entries that this queue will handle.
11537 * This function allocates a queue structure and the DMAable memory used for
11538 * the host resident queue. This function must be called before creating the
11539 * queue on the HBA.
11541 struct lpfc_queue *
11542 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11543 uint32_t entry_count)
11545 struct lpfc_queue *queue;
11546 struct lpfc_dmabuf *dmabuf;
11547 int x, total_qe_count;
11548 void *dma_pointer;
11549 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11551 if (!phba->sli4_hba.pc_sli4_params.supported)
11552 hw_page_size = SLI4_PAGE_SIZE;
11554 queue = kzalloc(sizeof(struct lpfc_queue) +
11555 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11556 if (!queue)
11557 return NULL;
11558 queue->page_count = (ALIGN(entry_size * entry_count,
11559 hw_page_size))/hw_page_size;
11560 INIT_LIST_HEAD(&queue->list);
11561 INIT_LIST_HEAD(&queue->page_list);
11562 INIT_LIST_HEAD(&queue->child_list);
11563 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11564 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11565 if (!dmabuf)
11566 goto out_fail;
11567 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
11568 hw_page_size, &dmabuf->phys,
11569 GFP_KERNEL);
11570 if (!dmabuf->virt) {
11571 kfree(dmabuf);
11572 goto out_fail;
11574 memset(dmabuf->virt, 0, hw_page_size);
11575 dmabuf->buffer_tag = x;
11576 list_add_tail(&dmabuf->list, &queue->page_list);
11577 /* initialize queue's entry array */
11578 dma_pointer = dmabuf->virt;
11579 for (; total_qe_count < entry_count &&
11580 dma_pointer < (hw_page_size + dmabuf->virt);
11581 total_qe_count++, dma_pointer += entry_size) {
11582 queue->qe[total_qe_count].address = dma_pointer;
11585 queue->entry_size = entry_size;
11586 queue->entry_count = entry_count;
11589 * entry_repost is calculated based on the number of entries in the
11590 * queue. This works out except for RQs. If buffers are NOT initially
11591 * posted for every RQE, entry_repost should be adjusted accordingly.
11593 queue->entry_repost = (entry_count >> 3);
11594 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11595 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
11596 queue->phba = phba;
11598 return queue;
11599 out_fail:
11600 lpfc_sli4_queue_free(queue);
11601 return NULL;
11605 * lpfc_eq_create - Create an Event Queue on the HBA
11606 * @phba: HBA structure that indicates port to create a queue on.
11607 * @eq: The queue structure to use to create the event queue.
11608 * @imax: The maximum interrupt per second limit.
11610 * This function creates an event queue, as detailed in @eq, on a port,
11611 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11613 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11614 * is used to get the entry count and entry size that are necessary to
11615 * determine the number of pages to allocate and use for this queue. This
11616 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11617 * event queue. This function is asynchronous and will wait for the mailbox
11618 * command to finish before continuing.
11620 * On success this function will return a zero. If unable to allocate enough
11621 * memory this function will return -ENOMEM. If the queue create mailbox command
11622 * fails this function will return -ENXIO.
11624 uint32_t
11625 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11627 struct lpfc_mbx_eq_create *eq_create;
11628 LPFC_MBOXQ_t *mbox;
11629 int rc, length, status = 0;
11630 struct lpfc_dmabuf *dmabuf;
11631 uint32_t shdr_status, shdr_add_status;
11632 union lpfc_sli4_cfg_shdr *shdr;
11633 uint16_t dmult;
11634 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11636 if (!phba->sli4_hba.pc_sli4_params.supported)
11637 hw_page_size = SLI4_PAGE_SIZE;
11639 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11640 if (!mbox)
11641 return -ENOMEM;
11642 length = (sizeof(struct lpfc_mbx_eq_create) -
11643 sizeof(struct lpfc_sli4_cfg_mhdr));
11644 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11645 LPFC_MBOX_OPCODE_EQ_CREATE,
11646 length, LPFC_SLI4_MBX_EMBED);
11647 eq_create = &mbox->u.mqe.un.eq_create;
11648 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11649 eq->page_count);
11650 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11651 LPFC_EQE_SIZE);
11652 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11653 /* Calculate delay multiper from maximum interrupt per second */
11654 dmult = LPFC_DMULT_CONST/imax - 1;
11655 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11656 dmult);
11657 switch (eq->entry_count) {
11658 default:
11659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11660 "0360 Unsupported EQ count. (%d)\n",
11661 eq->entry_count);
11662 if (eq->entry_count < 256)
11663 return -EINVAL;
11664 /* otherwise default to smallest count (drop through) */
11665 case 256:
11666 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11667 LPFC_EQ_CNT_256);
11668 break;
11669 case 512:
11670 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11671 LPFC_EQ_CNT_512);
11672 break;
11673 case 1024:
11674 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11675 LPFC_EQ_CNT_1024);
11676 break;
11677 case 2048:
11678 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11679 LPFC_EQ_CNT_2048);
11680 break;
11681 case 4096:
11682 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11683 LPFC_EQ_CNT_4096);
11684 break;
11686 list_for_each_entry(dmabuf, &eq->page_list, list) {
11687 memset(dmabuf->virt, 0, hw_page_size);
11688 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11689 putPaddrLow(dmabuf->phys);
11690 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11691 putPaddrHigh(dmabuf->phys);
11693 mbox->vport = phba->pport;
11694 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11695 mbox->context1 = NULL;
11696 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11697 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11698 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11699 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11700 if (shdr_status || shdr_add_status || rc) {
11701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11702 "2500 EQ_CREATE mailbox failed with "
11703 "status x%x add_status x%x, mbx status x%x\n",
11704 shdr_status, shdr_add_status, rc);
11705 status = -ENXIO;
11707 eq->type = LPFC_EQ;
11708 eq->subtype = LPFC_NONE;
11709 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11710 if (eq->queue_id == 0xFFFF)
11711 status = -ENXIO;
11712 eq->host_index = 0;
11713 eq->hba_index = 0;
11715 mempool_free(mbox, phba->mbox_mem_pool);
11716 return status;
11720 * lpfc_cq_create - Create a Completion Queue on the HBA
11721 * @phba: HBA structure that indicates port to create a queue on.
11722 * @cq: The queue structure to use to create the completion queue.
11723 * @eq: The event queue to bind this completion queue to.
11725 * This function creates a completion queue, as detailed in @wq, on a port,
11726 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11728 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11729 * is used to get the entry count and entry size that are necessary to
11730 * determine the number of pages to allocate and use for this queue. The @eq
11731 * is used to indicate which event queue to bind this completion queue to. This
11732 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11733 * completion queue. This function is asynchronous and will wait for the mailbox
11734 * command to finish before continuing.
11736 * On success this function will return a zero. If unable to allocate enough
11737 * memory this function will return -ENOMEM. If the queue create mailbox command
11738 * fails this function will return -ENXIO.
11740 uint32_t
11741 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11742 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11744 struct lpfc_mbx_cq_create *cq_create;
11745 struct lpfc_dmabuf *dmabuf;
11746 LPFC_MBOXQ_t *mbox;
11747 int rc, length, status = 0;
11748 uint32_t shdr_status, shdr_add_status;
11749 union lpfc_sli4_cfg_shdr *shdr;
11750 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11752 if (!phba->sli4_hba.pc_sli4_params.supported)
11753 hw_page_size = SLI4_PAGE_SIZE;
11755 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11756 if (!mbox)
11757 return -ENOMEM;
11758 length = (sizeof(struct lpfc_mbx_cq_create) -
11759 sizeof(struct lpfc_sli4_cfg_mhdr));
11760 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11761 LPFC_MBOX_OPCODE_CQ_CREATE,
11762 length, LPFC_SLI4_MBX_EMBED);
11763 cq_create = &mbox->u.mqe.un.cq_create;
11764 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
11765 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11766 cq->page_count);
11767 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11768 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
11769 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11770 phba->sli4_hba.pc_sli4_params.cqv);
11771 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
11772 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11773 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
11774 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11775 eq->queue_id);
11776 } else {
11777 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11778 eq->queue_id);
11780 switch (cq->entry_count) {
11781 default:
11782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11783 "0361 Unsupported CQ count. (%d)\n",
11784 cq->entry_count);
11785 if (cq->entry_count < 256)
11786 return -EINVAL;
11787 /* otherwise default to smallest count (drop through) */
11788 case 256:
11789 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11790 LPFC_CQ_CNT_256);
11791 break;
11792 case 512:
11793 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11794 LPFC_CQ_CNT_512);
11795 break;
11796 case 1024:
11797 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11798 LPFC_CQ_CNT_1024);
11799 break;
11801 list_for_each_entry(dmabuf, &cq->page_list, list) {
11802 memset(dmabuf->virt, 0, hw_page_size);
11803 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11804 putPaddrLow(dmabuf->phys);
11805 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11806 putPaddrHigh(dmabuf->phys);
11808 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11810 /* The IOCTL status is embedded in the mailbox subheader. */
11811 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11812 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11813 if (shdr_status || shdr_add_status || rc) {
11814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11815 "2501 CQ_CREATE mailbox failed with "
11816 "status x%x add_status x%x, mbx status x%x\n",
11817 shdr_status, shdr_add_status, rc);
11818 status = -ENXIO;
11819 goto out;
11821 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11822 if (cq->queue_id == 0xFFFF) {
11823 status = -ENXIO;
11824 goto out;
11826 /* link the cq onto the parent eq child list */
11827 list_add_tail(&cq->list, &eq->child_list);
11828 /* Set up completion queue's type and subtype */
11829 cq->type = type;
11830 cq->subtype = subtype;
11831 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11832 cq->assoc_qid = eq->queue_id;
11833 cq->host_index = 0;
11834 cq->hba_index = 0;
11836 out:
11837 mempool_free(mbox, phba->mbox_mem_pool);
11838 return status;
11842 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
11843 * @phba: HBA structure that indicates port to create a queue on.
11844 * @mq: The queue structure to use to create the mailbox queue.
11845 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11846 * @cq: The completion queue to associate with this cq.
11848 * This function provides failback (fb) functionality when the
11849 * mq_create_ext fails on older FW generations. It's purpose is identical
11850 * to mq_create_ext otherwise.
11852 * This routine cannot fail as all attributes were previously accessed and
11853 * initialized in mq_create_ext.
11855 static void
11856 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11857 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
11859 struct lpfc_mbx_mq_create *mq_create;
11860 struct lpfc_dmabuf *dmabuf;
11861 int length;
11863 length = (sizeof(struct lpfc_mbx_mq_create) -
11864 sizeof(struct lpfc_sli4_cfg_mhdr));
11865 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11866 LPFC_MBOX_OPCODE_MQ_CREATE,
11867 length, LPFC_SLI4_MBX_EMBED);
11868 mq_create = &mbox->u.mqe.un.mq_create;
11869 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
11870 mq->page_count);
11871 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
11872 cq->queue_id);
11873 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11874 switch (mq->entry_count) {
11875 case 16:
11876 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11877 LPFC_MQ_RING_SIZE_16);
11878 break;
11879 case 32:
11880 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11881 LPFC_MQ_RING_SIZE_32);
11882 break;
11883 case 64:
11884 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11885 LPFC_MQ_RING_SIZE_64);
11886 break;
11887 case 128:
11888 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11889 LPFC_MQ_RING_SIZE_128);
11890 break;
11892 list_for_each_entry(dmabuf, &mq->page_list, list) {
11893 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11894 putPaddrLow(dmabuf->phys);
11895 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11896 putPaddrHigh(dmabuf->phys);
11901 * lpfc_mq_create - Create a mailbox Queue on the HBA
11902 * @phba: HBA structure that indicates port to create a queue on.
11903 * @mq: The queue structure to use to create the mailbox queue.
11904 * @cq: The completion queue to associate with this cq.
11905 * @subtype: The queue's subtype.
11907 * This function creates a mailbox queue, as detailed in @mq, on a port,
11908 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11910 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11911 * is used to get the entry count and entry size that are necessary to
11912 * determine the number of pages to allocate and use for this queue. This
11913 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11914 * mailbox queue. This function is asynchronous and will wait for the mailbox
11915 * command to finish before continuing.
11917 * On success this function will return a zero. If unable to allocate enough
11918 * memory this function will return -ENOMEM. If the queue create mailbox command
11919 * fails this function will return -ENXIO.
11921 int32_t
11922 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11923 struct lpfc_queue *cq, uint32_t subtype)
11925 struct lpfc_mbx_mq_create *mq_create;
11926 struct lpfc_mbx_mq_create_ext *mq_create_ext;
11927 struct lpfc_dmabuf *dmabuf;
11928 LPFC_MBOXQ_t *mbox;
11929 int rc, length, status = 0;
11930 uint32_t shdr_status, shdr_add_status;
11931 union lpfc_sli4_cfg_shdr *shdr;
11932 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11934 if (!phba->sli4_hba.pc_sli4_params.supported)
11935 hw_page_size = SLI4_PAGE_SIZE;
11937 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11938 if (!mbox)
11939 return -ENOMEM;
11940 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
11941 sizeof(struct lpfc_sli4_cfg_mhdr));
11942 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11943 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
11944 length, LPFC_SLI4_MBX_EMBED);
11946 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
11947 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
11948 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11949 &mq_create_ext->u.request, mq->page_count);
11950 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11951 &mq_create_ext->u.request, 1);
11952 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
11953 &mq_create_ext->u.request, 1);
11954 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11955 &mq_create_ext->u.request, 1);
11956 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11957 &mq_create_ext->u.request, 1);
11958 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11959 &mq_create_ext->u.request, 1);
11960 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
11961 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11962 phba->sli4_hba.pc_sli4_params.mqv);
11963 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11964 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11965 cq->queue_id);
11966 else
11967 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11968 cq->queue_id);
11969 switch (mq->entry_count) {
11970 default:
11971 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11972 "0362 Unsupported MQ count. (%d)\n",
11973 mq->entry_count);
11974 if (mq->entry_count < 16)
11975 return -EINVAL;
11976 /* otherwise default to smallest count (drop through) */
11977 case 16:
11978 bf_set(lpfc_mq_context_ring_size,
11979 &mq_create_ext->u.request.context,
11980 LPFC_MQ_RING_SIZE_16);
11981 break;
11982 case 32:
11983 bf_set(lpfc_mq_context_ring_size,
11984 &mq_create_ext->u.request.context,
11985 LPFC_MQ_RING_SIZE_32);
11986 break;
11987 case 64:
11988 bf_set(lpfc_mq_context_ring_size,
11989 &mq_create_ext->u.request.context,
11990 LPFC_MQ_RING_SIZE_64);
11991 break;
11992 case 128:
11993 bf_set(lpfc_mq_context_ring_size,
11994 &mq_create_ext->u.request.context,
11995 LPFC_MQ_RING_SIZE_128);
11996 break;
11998 list_for_each_entry(dmabuf, &mq->page_list, list) {
11999 memset(dmabuf->virt, 0, hw_page_size);
12000 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12001 putPaddrLow(dmabuf->phys);
12002 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
12003 putPaddrHigh(dmabuf->phys);
12005 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12006 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12007 &mq_create_ext->u.response);
12008 if (rc != MBX_SUCCESS) {
12009 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12010 "2795 MQ_CREATE_EXT failed with "
12011 "status x%x. Failback to MQ_CREATE.\n",
12012 rc);
12013 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12014 mq_create = &mbox->u.mqe.un.mq_create;
12015 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12016 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12017 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12018 &mq_create->u.response);
12021 /* The IOCTL status is embedded in the mailbox subheader. */
12022 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12023 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12024 if (shdr_status || shdr_add_status || rc) {
12025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12026 "2502 MQ_CREATE mailbox failed with "
12027 "status x%x add_status x%x, mbx status x%x\n",
12028 shdr_status, shdr_add_status, rc);
12029 status = -ENXIO;
12030 goto out;
12032 if (mq->queue_id == 0xFFFF) {
12033 status = -ENXIO;
12034 goto out;
12036 mq->type = LPFC_MQ;
12037 mq->assoc_qid = cq->queue_id;
12038 mq->subtype = subtype;
12039 mq->host_index = 0;
12040 mq->hba_index = 0;
12042 /* link the mq onto the parent cq child list */
12043 list_add_tail(&mq->list, &cq->child_list);
12044 out:
12045 mempool_free(mbox, phba->mbox_mem_pool);
12046 return status;
12050 * lpfc_wq_create - Create a Work Queue on the HBA
12051 * @phba: HBA structure that indicates port to create a queue on.
12052 * @wq: The queue structure to use to create the work queue.
12053 * @cq: The completion queue to bind this work queue to.
12054 * @subtype: The subtype of the work queue indicating its functionality.
12056 * This function creates a work queue, as detailed in @wq, on a port, described
12057 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12059 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12060 * is used to get the entry count and entry size that are necessary to
12061 * determine the number of pages to allocate and use for this queue. The @cq
12062 * is used to indicate which completion queue to bind this work queue to. This
12063 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12064 * work queue. This function is asynchronous and will wait for the mailbox
12065 * command to finish before continuing.
12067 * On success this function will return a zero. If unable to allocate enough
12068 * memory this function will return -ENOMEM. If the queue create mailbox command
12069 * fails this function will return -ENXIO.
12071 uint32_t
12072 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12073 struct lpfc_queue *cq, uint32_t subtype)
12075 struct lpfc_mbx_wq_create *wq_create;
12076 struct lpfc_dmabuf *dmabuf;
12077 LPFC_MBOXQ_t *mbox;
12078 int rc, length, status = 0;
12079 uint32_t shdr_status, shdr_add_status;
12080 union lpfc_sli4_cfg_shdr *shdr;
12081 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12082 struct dma_address *page;
12084 if (!phba->sli4_hba.pc_sli4_params.supported)
12085 hw_page_size = SLI4_PAGE_SIZE;
12087 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12088 if (!mbox)
12089 return -ENOMEM;
12090 length = (sizeof(struct lpfc_mbx_wq_create) -
12091 sizeof(struct lpfc_sli4_cfg_mhdr));
12092 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12093 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12094 length, LPFC_SLI4_MBX_EMBED);
12095 wq_create = &mbox->u.mqe.un.wq_create;
12096 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
12097 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12098 wq->page_count);
12099 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12100 cq->queue_id);
12101 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12102 phba->sli4_hba.pc_sli4_params.wqv);
12103 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12104 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12105 wq->entry_count);
12106 switch (wq->entry_size) {
12107 default:
12108 case 64:
12109 bf_set(lpfc_mbx_wq_create_wqe_size,
12110 &wq_create->u.request_1,
12111 LPFC_WQ_WQE_SIZE_64);
12112 break;
12113 case 128:
12114 bf_set(lpfc_mbx_wq_create_wqe_size,
12115 &wq_create->u.request_1,
12116 LPFC_WQ_WQE_SIZE_128);
12117 break;
12119 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12120 (PAGE_SIZE/SLI4_PAGE_SIZE));
12121 page = wq_create->u.request_1.page;
12122 } else {
12123 page = wq_create->u.request.page;
12125 list_for_each_entry(dmabuf, &wq->page_list, list) {
12126 memset(dmabuf->virt, 0, hw_page_size);
12127 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12128 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
12130 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12131 /* The IOCTL status is embedded in the mailbox subheader. */
12132 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12133 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12134 if (shdr_status || shdr_add_status || rc) {
12135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12136 "2503 WQ_CREATE mailbox failed with "
12137 "status x%x add_status x%x, mbx status x%x\n",
12138 shdr_status, shdr_add_status, rc);
12139 status = -ENXIO;
12140 goto out;
12142 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12143 if (wq->queue_id == 0xFFFF) {
12144 status = -ENXIO;
12145 goto out;
12147 wq->type = LPFC_WQ;
12148 wq->assoc_qid = cq->queue_id;
12149 wq->subtype = subtype;
12150 wq->host_index = 0;
12151 wq->hba_index = 0;
12153 /* link the wq onto the parent cq child list */
12154 list_add_tail(&wq->list, &cq->child_list);
12155 out:
12156 mempool_free(mbox, phba->mbox_mem_pool);
12157 return status;
12161 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12162 * @phba: HBA structure that indicates port to create a queue on.
12163 * @rq: The queue structure to use for the receive queue.
12164 * @qno: The associated HBQ number
12167 * For SLI4 we need to adjust the RQ repost value based on
12168 * the number of buffers that are initially posted to the RQ.
12170 void
12171 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12173 uint32_t cnt;
12175 cnt = lpfc_hbq_defs[qno]->entry_count;
12177 /* Recalc repost for RQs based on buffers initially posted */
12178 cnt = (cnt >> 3);
12179 if (cnt < LPFC_QUEUE_MIN_REPOST)
12180 cnt = LPFC_QUEUE_MIN_REPOST;
12182 rq->entry_repost = cnt;
12186 * lpfc_rq_create - Create a Receive Queue on the HBA
12187 * @phba: HBA structure that indicates port to create a queue on.
12188 * @hrq: The queue structure to use to create the header receive queue.
12189 * @drq: The queue structure to use to create the data receive queue.
12190 * @cq: The completion queue to bind this work queue to.
12192 * This function creates a receive buffer queue pair , as detailed in @hrq and
12193 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12194 * to the HBA.
12196 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12197 * struct is used to get the entry count that is necessary to determine the
12198 * number of pages to use for this queue. The @cq is used to indicate which
12199 * completion queue to bind received buffers that are posted to these queues to.
12200 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12201 * receive queue pair. This function is asynchronous and will wait for the
12202 * mailbox command to finish before continuing.
12204 * On success this function will return a zero. If unable to allocate enough
12205 * memory this function will return -ENOMEM. If the queue create mailbox command
12206 * fails this function will return -ENXIO.
12208 uint32_t
12209 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12210 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12212 struct lpfc_mbx_rq_create *rq_create;
12213 struct lpfc_dmabuf *dmabuf;
12214 LPFC_MBOXQ_t *mbox;
12215 int rc, length, status = 0;
12216 uint32_t shdr_status, shdr_add_status;
12217 union lpfc_sli4_cfg_shdr *shdr;
12218 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12220 if (!phba->sli4_hba.pc_sli4_params.supported)
12221 hw_page_size = SLI4_PAGE_SIZE;
12223 if (hrq->entry_count != drq->entry_count)
12224 return -EINVAL;
12225 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12226 if (!mbox)
12227 return -ENOMEM;
12228 length = (sizeof(struct lpfc_mbx_rq_create) -
12229 sizeof(struct lpfc_sli4_cfg_mhdr));
12230 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12231 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12232 length, LPFC_SLI4_MBX_EMBED);
12233 rq_create = &mbox->u.mqe.un.rq_create;
12234 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12235 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12236 phba->sli4_hba.pc_sli4_params.rqv);
12237 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12238 bf_set(lpfc_rq_context_rqe_count_1,
12239 &rq_create->u.request.context,
12240 hrq->entry_count);
12241 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
12242 bf_set(lpfc_rq_context_rqe_size,
12243 &rq_create->u.request.context,
12244 LPFC_RQE_SIZE_8);
12245 bf_set(lpfc_rq_context_page_size,
12246 &rq_create->u.request.context,
12247 (PAGE_SIZE/SLI4_PAGE_SIZE));
12248 } else {
12249 switch (hrq->entry_count) {
12250 default:
12251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12252 "2535 Unsupported RQ count. (%d)\n",
12253 hrq->entry_count);
12254 if (hrq->entry_count < 512)
12255 return -EINVAL;
12256 /* otherwise default to smallest count (drop through) */
12257 case 512:
12258 bf_set(lpfc_rq_context_rqe_count,
12259 &rq_create->u.request.context,
12260 LPFC_RQ_RING_SIZE_512);
12261 break;
12262 case 1024:
12263 bf_set(lpfc_rq_context_rqe_count,
12264 &rq_create->u.request.context,
12265 LPFC_RQ_RING_SIZE_1024);
12266 break;
12267 case 2048:
12268 bf_set(lpfc_rq_context_rqe_count,
12269 &rq_create->u.request.context,
12270 LPFC_RQ_RING_SIZE_2048);
12271 break;
12272 case 4096:
12273 bf_set(lpfc_rq_context_rqe_count,
12274 &rq_create->u.request.context,
12275 LPFC_RQ_RING_SIZE_4096);
12276 break;
12278 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12279 LPFC_HDR_BUF_SIZE);
12281 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12282 cq->queue_id);
12283 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12284 hrq->page_count);
12285 list_for_each_entry(dmabuf, &hrq->page_list, list) {
12286 memset(dmabuf->virt, 0, hw_page_size);
12287 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12288 putPaddrLow(dmabuf->phys);
12289 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12290 putPaddrHigh(dmabuf->phys);
12292 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12293 /* The IOCTL status is embedded in the mailbox subheader. */
12294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12296 if (shdr_status || shdr_add_status || rc) {
12297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12298 "2504 RQ_CREATE mailbox failed with "
12299 "status x%x add_status x%x, mbx status x%x\n",
12300 shdr_status, shdr_add_status, rc);
12301 status = -ENXIO;
12302 goto out;
12304 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12305 if (hrq->queue_id == 0xFFFF) {
12306 status = -ENXIO;
12307 goto out;
12309 hrq->type = LPFC_HRQ;
12310 hrq->assoc_qid = cq->queue_id;
12311 hrq->subtype = subtype;
12312 hrq->host_index = 0;
12313 hrq->hba_index = 0;
12315 /* now create the data queue */
12316 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12317 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12318 length, LPFC_SLI4_MBX_EMBED);
12319 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12320 phba->sli4_hba.pc_sli4_params.rqv);
12321 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12322 bf_set(lpfc_rq_context_rqe_count_1,
12323 &rq_create->u.request.context, hrq->entry_count);
12324 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
12325 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12326 LPFC_RQE_SIZE_8);
12327 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12328 (PAGE_SIZE/SLI4_PAGE_SIZE));
12329 } else {
12330 switch (drq->entry_count) {
12331 default:
12332 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12333 "2536 Unsupported RQ count. (%d)\n",
12334 drq->entry_count);
12335 if (drq->entry_count < 512)
12336 return -EINVAL;
12337 /* otherwise default to smallest count (drop through) */
12338 case 512:
12339 bf_set(lpfc_rq_context_rqe_count,
12340 &rq_create->u.request.context,
12341 LPFC_RQ_RING_SIZE_512);
12342 break;
12343 case 1024:
12344 bf_set(lpfc_rq_context_rqe_count,
12345 &rq_create->u.request.context,
12346 LPFC_RQ_RING_SIZE_1024);
12347 break;
12348 case 2048:
12349 bf_set(lpfc_rq_context_rqe_count,
12350 &rq_create->u.request.context,
12351 LPFC_RQ_RING_SIZE_2048);
12352 break;
12353 case 4096:
12354 bf_set(lpfc_rq_context_rqe_count,
12355 &rq_create->u.request.context,
12356 LPFC_RQ_RING_SIZE_4096);
12357 break;
12359 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12360 LPFC_DATA_BUF_SIZE);
12362 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12363 cq->queue_id);
12364 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12365 drq->page_count);
12366 list_for_each_entry(dmabuf, &drq->page_list, list) {
12367 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12368 putPaddrLow(dmabuf->phys);
12369 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12370 putPaddrHigh(dmabuf->phys);
12372 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12373 /* The IOCTL status is embedded in the mailbox subheader. */
12374 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12377 if (shdr_status || shdr_add_status || rc) {
12378 status = -ENXIO;
12379 goto out;
12381 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12382 if (drq->queue_id == 0xFFFF) {
12383 status = -ENXIO;
12384 goto out;
12386 drq->type = LPFC_DRQ;
12387 drq->assoc_qid = cq->queue_id;
12388 drq->subtype = subtype;
12389 drq->host_index = 0;
12390 drq->hba_index = 0;
12392 /* link the header and data RQs onto the parent cq child list */
12393 list_add_tail(&hrq->list, &cq->child_list);
12394 list_add_tail(&drq->list, &cq->child_list);
12396 out:
12397 mempool_free(mbox, phba->mbox_mem_pool);
12398 return status;
12402 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12403 * @eq: The queue structure associated with the queue to destroy.
12405 * This function destroys a queue, as detailed in @eq by sending an mailbox
12406 * command, specific to the type of queue, to the HBA.
12408 * The @eq struct is used to get the queue ID of the queue to destroy.
12410 * On success this function will return a zero. If the queue destroy mailbox
12411 * command fails this function will return -ENXIO.
12413 uint32_t
12414 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12416 LPFC_MBOXQ_t *mbox;
12417 int rc, length, status = 0;
12418 uint32_t shdr_status, shdr_add_status;
12419 union lpfc_sli4_cfg_shdr *shdr;
12421 if (!eq)
12422 return -ENODEV;
12423 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12424 if (!mbox)
12425 return -ENOMEM;
12426 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12427 sizeof(struct lpfc_sli4_cfg_mhdr));
12428 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12429 LPFC_MBOX_OPCODE_EQ_DESTROY,
12430 length, LPFC_SLI4_MBX_EMBED);
12431 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12432 eq->queue_id);
12433 mbox->vport = eq->phba->pport;
12434 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12436 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12437 /* The IOCTL status is embedded in the mailbox subheader. */
12438 shdr = (union lpfc_sli4_cfg_shdr *)
12439 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12440 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12441 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12442 if (shdr_status || shdr_add_status || rc) {
12443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12444 "2505 EQ_DESTROY mailbox failed with "
12445 "status x%x add_status x%x, mbx status x%x\n",
12446 shdr_status, shdr_add_status, rc);
12447 status = -ENXIO;
12450 /* Remove eq from any list */
12451 list_del_init(&eq->list);
12452 mempool_free(mbox, eq->phba->mbox_mem_pool);
12453 return status;
12457 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12458 * @cq: The queue structure associated with the queue to destroy.
12460 * This function destroys a queue, as detailed in @cq by sending an mailbox
12461 * command, specific to the type of queue, to the HBA.
12463 * The @cq struct is used to get the queue ID of the queue to destroy.
12465 * On success this function will return a zero. If the queue destroy mailbox
12466 * command fails this function will return -ENXIO.
12468 uint32_t
12469 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12471 LPFC_MBOXQ_t *mbox;
12472 int rc, length, status = 0;
12473 uint32_t shdr_status, shdr_add_status;
12474 union lpfc_sli4_cfg_shdr *shdr;
12476 if (!cq)
12477 return -ENODEV;
12478 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12479 if (!mbox)
12480 return -ENOMEM;
12481 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12482 sizeof(struct lpfc_sli4_cfg_mhdr));
12483 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12484 LPFC_MBOX_OPCODE_CQ_DESTROY,
12485 length, LPFC_SLI4_MBX_EMBED);
12486 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12487 cq->queue_id);
12488 mbox->vport = cq->phba->pport;
12489 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12490 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12491 /* The IOCTL status is embedded in the mailbox subheader. */
12492 shdr = (union lpfc_sli4_cfg_shdr *)
12493 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12494 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12495 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12496 if (shdr_status || shdr_add_status || rc) {
12497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12498 "2506 CQ_DESTROY mailbox failed with "
12499 "status x%x add_status x%x, mbx status x%x\n",
12500 shdr_status, shdr_add_status, rc);
12501 status = -ENXIO;
12503 /* Remove cq from any list */
12504 list_del_init(&cq->list);
12505 mempool_free(mbox, cq->phba->mbox_mem_pool);
12506 return status;
12510 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12511 * @qm: The queue structure associated with the queue to destroy.
12513 * This function destroys a queue, as detailed in @mq by sending an mailbox
12514 * command, specific to the type of queue, to the HBA.
12516 * The @mq struct is used to get the queue ID of the queue to destroy.
12518 * On success this function will return a zero. If the queue destroy mailbox
12519 * command fails this function will return -ENXIO.
12521 uint32_t
12522 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12524 LPFC_MBOXQ_t *mbox;
12525 int rc, length, status = 0;
12526 uint32_t shdr_status, shdr_add_status;
12527 union lpfc_sli4_cfg_shdr *shdr;
12529 if (!mq)
12530 return -ENODEV;
12531 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12532 if (!mbox)
12533 return -ENOMEM;
12534 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12535 sizeof(struct lpfc_sli4_cfg_mhdr));
12536 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12537 LPFC_MBOX_OPCODE_MQ_DESTROY,
12538 length, LPFC_SLI4_MBX_EMBED);
12539 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12540 mq->queue_id);
12541 mbox->vport = mq->phba->pport;
12542 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12543 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12544 /* The IOCTL status is embedded in the mailbox subheader. */
12545 shdr = (union lpfc_sli4_cfg_shdr *)
12546 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12547 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12548 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12549 if (shdr_status || shdr_add_status || rc) {
12550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12551 "2507 MQ_DESTROY mailbox failed with "
12552 "status x%x add_status x%x, mbx status x%x\n",
12553 shdr_status, shdr_add_status, rc);
12554 status = -ENXIO;
12556 /* Remove mq from any list */
12557 list_del_init(&mq->list);
12558 mempool_free(mbox, mq->phba->mbox_mem_pool);
12559 return status;
12563 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12564 * @wq: The queue structure associated with the queue to destroy.
12566 * This function destroys a queue, as detailed in @wq by sending an mailbox
12567 * command, specific to the type of queue, to the HBA.
12569 * The @wq struct is used to get the queue ID of the queue to destroy.
12571 * On success this function will return a zero. If the queue destroy mailbox
12572 * command fails this function will return -ENXIO.
12574 uint32_t
12575 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12577 LPFC_MBOXQ_t *mbox;
12578 int rc, length, status = 0;
12579 uint32_t shdr_status, shdr_add_status;
12580 union lpfc_sli4_cfg_shdr *shdr;
12582 if (!wq)
12583 return -ENODEV;
12584 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12585 if (!mbox)
12586 return -ENOMEM;
12587 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12588 sizeof(struct lpfc_sli4_cfg_mhdr));
12589 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12590 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12591 length, LPFC_SLI4_MBX_EMBED);
12592 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12593 wq->queue_id);
12594 mbox->vport = wq->phba->pport;
12595 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12596 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12597 shdr = (union lpfc_sli4_cfg_shdr *)
12598 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12599 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12600 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12601 if (shdr_status || shdr_add_status || rc) {
12602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12603 "2508 WQ_DESTROY mailbox failed with "
12604 "status x%x add_status x%x, mbx status x%x\n",
12605 shdr_status, shdr_add_status, rc);
12606 status = -ENXIO;
12608 /* Remove wq from any list */
12609 list_del_init(&wq->list);
12610 mempool_free(mbox, wq->phba->mbox_mem_pool);
12611 return status;
12615 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12616 * @rq: The queue structure associated with the queue to destroy.
12618 * This function destroys a queue, as detailed in @rq by sending an mailbox
12619 * command, specific to the type of queue, to the HBA.
12621 * The @rq struct is used to get the queue ID of the queue to destroy.
12623 * On success this function will return a zero. If the queue destroy mailbox
12624 * command fails this function will return -ENXIO.
12626 uint32_t
12627 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12628 struct lpfc_queue *drq)
12630 LPFC_MBOXQ_t *mbox;
12631 int rc, length, status = 0;
12632 uint32_t shdr_status, shdr_add_status;
12633 union lpfc_sli4_cfg_shdr *shdr;
12635 if (!hrq || !drq)
12636 return -ENODEV;
12637 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12638 if (!mbox)
12639 return -ENOMEM;
12640 length = (sizeof(struct lpfc_mbx_rq_destroy) -
12641 sizeof(struct lpfc_sli4_cfg_mhdr));
12642 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12643 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12644 length, LPFC_SLI4_MBX_EMBED);
12645 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12646 hrq->queue_id);
12647 mbox->vport = hrq->phba->pport;
12648 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12649 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12650 /* The IOCTL status is embedded in the mailbox subheader. */
12651 shdr = (union lpfc_sli4_cfg_shdr *)
12652 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12653 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12654 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12655 if (shdr_status || shdr_add_status || rc) {
12656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12657 "2509 RQ_DESTROY mailbox failed with "
12658 "status x%x add_status x%x, mbx status x%x\n",
12659 shdr_status, shdr_add_status, rc);
12660 if (rc != MBX_TIMEOUT)
12661 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12662 return -ENXIO;
12664 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12665 drq->queue_id);
12666 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12667 shdr = (union lpfc_sli4_cfg_shdr *)
12668 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12669 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12670 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12671 if (shdr_status || shdr_add_status || rc) {
12672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12673 "2510 RQ_DESTROY mailbox failed with "
12674 "status x%x add_status x%x, mbx status x%x\n",
12675 shdr_status, shdr_add_status, rc);
12676 status = -ENXIO;
12678 list_del_init(&hrq->list);
12679 list_del_init(&drq->list);
12680 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12681 return status;
12685 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12686 * @phba: The virtual port for which this call being executed.
12687 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12688 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12689 * @xritag: the xritag that ties this io to the SGL pages.
12691 * This routine will post the sgl pages for the IO that has the xritag
12692 * that is in the iocbq structure. The xritag is assigned during iocbq
12693 * creation and persists for as long as the driver is loaded.
12694 * if the caller has fewer than 256 scatter gather segments to map then
12695 * pdma_phys_addr1 should be 0.
12696 * If the caller needs to map more than 256 scatter gather segment then
12697 * pdma_phys_addr1 should be a valid physical address.
12698 * physical address for SGLs must be 64 byte aligned.
12699 * If you are going to map 2 SGL's then the first one must have 256 entries
12700 * the second sgl can have between 1 and 256 entries.
12702 * Return codes:
12703 * 0 - Success
12704 * -ENXIO, -ENOMEM - Failure
12707 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12708 dma_addr_t pdma_phys_addr0,
12709 dma_addr_t pdma_phys_addr1,
12710 uint16_t xritag)
12712 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12713 LPFC_MBOXQ_t *mbox;
12714 int rc;
12715 uint32_t shdr_status, shdr_add_status;
12716 uint32_t mbox_tmo;
12717 union lpfc_sli4_cfg_shdr *shdr;
12719 if (xritag == NO_XRI) {
12720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12721 "0364 Invalid param:\n");
12722 return -EINVAL;
12725 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12726 if (!mbox)
12727 return -ENOMEM;
12729 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12730 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12731 sizeof(struct lpfc_mbx_post_sgl_pages) -
12732 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
12734 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12735 &mbox->u.mqe.un.post_sgl_pages;
12736 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12737 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12739 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12740 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12741 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12742 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12744 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12745 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12746 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12747 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12748 if (!phba->sli4_hba.intr_enable)
12749 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12750 else {
12751 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
12752 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12754 /* The IOCTL status is embedded in the mailbox subheader. */
12755 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12756 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12757 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12758 if (rc != MBX_TIMEOUT)
12759 mempool_free(mbox, phba->mbox_mem_pool);
12760 if (shdr_status || shdr_add_status || rc) {
12761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12762 "2511 POST_SGL mailbox failed with "
12763 "status x%x add_status x%x, mbx status x%x\n",
12764 shdr_status, shdr_add_status, rc);
12765 rc = -ENXIO;
12767 return 0;
12771 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
12772 * @phba: pointer to lpfc hba data structure.
12774 * This routine is invoked to post rpi header templates to the
12775 * HBA consistent with the SLI-4 interface spec. This routine
12776 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12777 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
12779 * Returns
12780 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12781 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12783 uint16_t
12784 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12786 unsigned long xri;
12789 * Fetch the next logical xri. Because this index is logical,
12790 * the driver starts at 0 each time.
12792 spin_lock_irq(&phba->hbalock);
12793 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12794 phba->sli4_hba.max_cfg_param.max_xri, 0);
12795 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12796 spin_unlock_irq(&phba->hbalock);
12797 return NO_XRI;
12798 } else {
12799 set_bit(xri, phba->sli4_hba.xri_bmask);
12800 phba->sli4_hba.max_cfg_param.xri_used++;
12801 phba->sli4_hba.xri_count++;
12804 spin_unlock_irq(&phba->hbalock);
12805 return xri;
12809 * lpfc_sli4_free_xri - Release an xri for reuse.
12810 * @phba: pointer to lpfc hba data structure.
12812 * This routine is invoked to release an xri to the pool of
12813 * available rpis maintained by the driver.
12815 void
12816 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12818 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12819 phba->sli4_hba.xri_count--;
12820 phba->sli4_hba.max_cfg_param.xri_used--;
12825 * lpfc_sli4_free_xri - Release an xri for reuse.
12826 * @phba: pointer to lpfc hba data structure.
12828 * This routine is invoked to release an xri to the pool of
12829 * available rpis maintained by the driver.
12831 void
12832 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12834 spin_lock_irq(&phba->hbalock);
12835 __lpfc_sli4_free_xri(phba, xri);
12836 spin_unlock_irq(&phba->hbalock);
12840 * lpfc_sli4_next_xritag - Get an xritag for the io
12841 * @phba: Pointer to HBA context object.
12843 * This function gets an xritag for the iocb. If there is no unused xritag
12844 * it will return 0xffff.
12845 * The function returns the allocated xritag if successful, else returns zero.
12846 * Zero is not a valid xritag.
12847 * The caller is not required to hold any lock.
12849 uint16_t
12850 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12852 uint16_t xri_index;
12854 xri_index = lpfc_sli4_alloc_xri(phba);
12855 if (xri_index != NO_XRI)
12856 return xri_index;
12858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12859 "2004 Failed to allocate XRI.last XRITAG is %d"
12860 " Max XRI is %d, Used XRI is %d\n",
12861 xri_index,
12862 phba->sli4_hba.max_cfg_param.max_xri,
12863 phba->sli4_hba.max_cfg_param.xri_used);
12864 return NO_XRI;
12868 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
12869 * @phba: pointer to lpfc hba data structure.
12871 * This routine is invoked to post a block of driver's sgl pages to the
12872 * HBA using non-embedded mailbox command. No Lock is held. This routine
12873 * is only called when the driver is loading and after all IO has been
12874 * stopped.
12877 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
12879 struct lpfc_sglq *sglq_entry;
12880 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12881 struct sgl_page_pairs *sgl_pg_pairs;
12882 void *viraddr;
12883 LPFC_MBOXQ_t *mbox;
12884 uint32_t reqlen, alloclen, pg_pairs;
12885 uint32_t mbox_tmo;
12886 uint16_t xritag_start = 0, lxri = 0;
12887 int els_xri_cnt, rc = 0;
12888 uint32_t shdr_status, shdr_add_status;
12889 union lpfc_sli4_cfg_shdr *shdr;
12891 /* The number of sgls to be posted */
12892 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12894 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12895 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
12896 if (reqlen > SLI4_PAGE_SIZE) {
12897 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12898 "2559 Block sgl registration required DMA "
12899 "size (%d) great than a page\n", reqlen);
12900 return -ENOMEM;
12902 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12903 if (!mbox)
12904 return -ENOMEM;
12906 /* Allocate DMA memory and set up the non-embedded mailbox command */
12907 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12908 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12909 LPFC_SLI4_MBX_NEMBED);
12911 if (alloclen < reqlen) {
12912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12913 "0285 Allocated DMA memory size (%d) is "
12914 "less than the requested DMA memory "
12915 "size (%d)\n", alloclen, reqlen);
12916 lpfc_sli4_mbox_cmd_free(phba, mbox);
12917 return -ENOMEM;
12919 /* Set up the SGL pages in the non-embedded DMA pages */
12920 viraddr = mbox->sge_array->addr[0];
12921 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12922 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12924 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12925 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
12928 * Assign the sglq a physical xri only if the driver has not
12929 * initialized those resources. A port reset only needs
12930 * the sglq's posted.
12932 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12933 LPFC_XRI_RSRC_RDY) {
12934 lxri = lpfc_sli4_next_xritag(phba);
12935 if (lxri == NO_XRI) {
12936 lpfc_sli4_mbox_cmd_free(phba, mbox);
12937 return -ENOMEM;
12939 sglq_entry->sli4_lxritag = lxri;
12940 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12943 /* Set up the sge entry */
12944 sgl_pg_pairs->sgl_pg0_addr_lo =
12945 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12946 sgl_pg_pairs->sgl_pg0_addr_hi =
12947 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12948 sgl_pg_pairs->sgl_pg1_addr_lo =
12949 cpu_to_le32(putPaddrLow(0));
12950 sgl_pg_pairs->sgl_pg1_addr_hi =
12951 cpu_to_le32(putPaddrHigh(0));
12953 /* Keep the first xritag on the list */
12954 if (pg_pairs == 0)
12955 xritag_start = sglq_entry->sli4_xritag;
12956 sgl_pg_pairs++;
12959 /* Complete initialization and perform endian conversion. */
12960 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
12961 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
12962 sgl->word0 = cpu_to_le32(sgl->word0);
12963 if (!phba->sli4_hba.intr_enable)
12964 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12965 else {
12966 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
12967 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12969 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12970 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12971 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12972 if (rc != MBX_TIMEOUT)
12973 lpfc_sli4_mbox_cmd_free(phba, mbox);
12974 if (shdr_status || shdr_add_status || rc) {
12975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12976 "2513 POST_SGL_BLOCK mailbox command failed "
12977 "status x%x add_status x%x mbx status x%x\n",
12978 shdr_status, shdr_add_status, rc);
12979 rc = -ENXIO;
12982 if (rc == 0)
12983 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
12984 LPFC_XRI_RSRC_RDY);
12985 return rc;
12989 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
12990 * @phba: pointer to lpfc hba data structure.
12992 * This routine is invoked to post a block of driver's sgl pages to the
12993 * HBA using non-embedded mailbox command. No Lock is held. This routine
12994 * is only called when the driver is loading and after all IO has been
12995 * stopped.
12998 lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13000 struct lpfc_sglq *sglq_entry;
13001 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13002 struct sgl_page_pairs *sgl_pg_pairs;
13003 void *viraddr;
13004 LPFC_MBOXQ_t *mbox;
13005 uint32_t reqlen, alloclen, index;
13006 uint32_t mbox_tmo;
13007 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13008 uint16_t xritag_start = 0, lxri = 0;
13009 struct lpfc_rsrc_blks *rsrc_blk;
13010 int cnt, ttl_cnt, rc = 0;
13011 int loop_cnt;
13012 uint32_t shdr_status, shdr_add_status;
13013 union lpfc_sli4_cfg_shdr *shdr;
13015 /* The number of sgls to be posted */
13016 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13018 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13019 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13020 if (reqlen > SLI4_PAGE_SIZE) {
13021 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13022 "2989 Block sgl registration required DMA "
13023 "size (%d) great than a page\n", reqlen);
13024 return -ENOMEM;
13027 cnt = 0;
13028 ttl_cnt = 0;
13029 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13030 list) {
13031 rsrc_start = rsrc_blk->rsrc_start;
13032 rsrc_size = rsrc_blk->rsrc_size;
13034 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13035 "3014 Working ELS Extent start %d, cnt %d\n",
13036 rsrc_start, rsrc_size);
13038 loop_cnt = min(els_xri_cnt, rsrc_size);
13039 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13040 loop_cnt = els_xri_cnt - ttl_cnt;
13041 ttl_cnt = els_xri_cnt;
13044 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13045 if (!mbox)
13046 return -ENOMEM;
13048 * Allocate DMA memory and set up the non-embedded mailbox
13049 * command.
13051 alloclen = lpfc_sli4_config(phba, mbox,
13052 LPFC_MBOX_SUBSYSTEM_FCOE,
13053 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13054 reqlen, LPFC_SLI4_MBX_NEMBED);
13055 if (alloclen < reqlen) {
13056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13057 "2987 Allocated DMA memory size (%d) "
13058 "is less than the requested DMA memory "
13059 "size (%d)\n", alloclen, reqlen);
13060 lpfc_sli4_mbox_cmd_free(phba, mbox);
13061 return -ENOMEM;
13064 /* Set up the SGL pages in the non-embedded DMA pages */
13065 viraddr = mbox->sge_array->addr[0];
13066 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13067 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13070 * The starting resource may not begin at zero. Control
13071 * the loop variants via the block resource parameters,
13072 * but handle the sge pointers with a zero-based index
13073 * that doesn't get reset per loop pass.
13075 for (index = rsrc_start;
13076 index < rsrc_start + loop_cnt;
13077 index++) {
13078 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13081 * Assign the sglq a physical xri only if the driver
13082 * has not initialized those resources. A port reset
13083 * only needs the sglq's posted.
13085 if (bf_get(lpfc_xri_rsrc_rdy,
13086 &phba->sli4_hba.sli4_flags) !=
13087 LPFC_XRI_RSRC_RDY) {
13088 lxri = lpfc_sli4_next_xritag(phba);
13089 if (lxri == NO_XRI) {
13090 lpfc_sli4_mbox_cmd_free(phba, mbox);
13091 rc = -ENOMEM;
13092 goto err_exit;
13094 sglq_entry->sli4_lxritag = lxri;
13095 sglq_entry->sli4_xritag =
13096 phba->sli4_hba.xri_ids[lxri];
13099 /* Set up the sge entry */
13100 sgl_pg_pairs->sgl_pg0_addr_lo =
13101 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13102 sgl_pg_pairs->sgl_pg0_addr_hi =
13103 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13104 sgl_pg_pairs->sgl_pg1_addr_lo =
13105 cpu_to_le32(putPaddrLow(0));
13106 sgl_pg_pairs->sgl_pg1_addr_hi =
13107 cpu_to_le32(putPaddrHigh(0));
13109 /* Track the starting physical XRI for the mailbox. */
13110 if (index == rsrc_start)
13111 xritag_start = sglq_entry->sli4_xritag;
13112 sgl_pg_pairs++;
13113 cnt++;
13116 /* Complete initialization and perform endian conversion. */
13117 rsrc_blk->rsrc_used += loop_cnt;
13118 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13119 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13120 sgl->word0 = cpu_to_le32(sgl->word0);
13122 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13123 "3015 Post ELS Extent SGL, start %d, "
13124 "cnt %d, used %d\n",
13125 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13126 if (!phba->sli4_hba.intr_enable)
13127 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13128 else {
13129 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13130 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13132 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13133 shdr_status = bf_get(lpfc_mbox_hdr_status,
13134 &shdr->response);
13135 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13136 &shdr->response);
13137 if (rc != MBX_TIMEOUT)
13138 lpfc_sli4_mbox_cmd_free(phba, mbox);
13139 if (shdr_status || shdr_add_status || rc) {
13140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13141 "2988 POST_SGL_BLOCK mailbox "
13142 "command failed status x%x "
13143 "add_status x%x mbx status x%x\n",
13144 shdr_status, shdr_add_status, rc);
13145 rc = -ENXIO;
13146 goto err_exit;
13148 if (ttl_cnt >= els_xri_cnt)
13149 break;
13152 err_exit:
13153 if (rc == 0)
13154 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13155 LPFC_XRI_RSRC_RDY);
13156 return rc;
13160 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13161 * @phba: pointer to lpfc hba data structure.
13162 * @sblist: pointer to scsi buffer list.
13163 * @count: number of scsi buffers on the list.
13165 * This routine is invoked to post a block of @count scsi sgl pages from a
13166 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13167 * No Lock is held.
13171 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13172 int cnt)
13174 struct lpfc_scsi_buf *psb;
13175 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13176 struct sgl_page_pairs *sgl_pg_pairs;
13177 void *viraddr;
13178 LPFC_MBOXQ_t *mbox;
13179 uint32_t reqlen, alloclen, pg_pairs;
13180 uint32_t mbox_tmo;
13181 uint16_t xritag_start = 0;
13182 int rc = 0;
13183 uint32_t shdr_status, shdr_add_status;
13184 dma_addr_t pdma_phys_bpl1;
13185 union lpfc_sli4_cfg_shdr *shdr;
13187 /* Calculate the requested length of the dma memory */
13188 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13189 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13190 if (reqlen > SLI4_PAGE_SIZE) {
13191 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13192 "0217 Block sgl registration required DMA "
13193 "size (%d) great than a page\n", reqlen);
13194 return -ENOMEM;
13196 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13197 if (!mbox) {
13198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13199 "0283 Failed to allocate mbox cmd memory\n");
13200 return -ENOMEM;
13203 /* Allocate DMA memory and set up the non-embedded mailbox command */
13204 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13205 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13206 LPFC_SLI4_MBX_NEMBED);
13208 if (alloclen < reqlen) {
13209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13210 "2561 Allocated DMA memory size (%d) is "
13211 "less than the requested DMA memory "
13212 "size (%d)\n", alloclen, reqlen);
13213 lpfc_sli4_mbox_cmd_free(phba, mbox);
13214 return -ENOMEM;
13217 /* Get the first SGE entry from the non-embedded DMA memory */
13218 viraddr = mbox->sge_array->addr[0];
13220 /* Set up the SGL pages in the non-embedded DMA pages */
13221 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13222 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13224 pg_pairs = 0;
13225 list_for_each_entry(psb, sblist, list) {
13226 /* Set up the sge entry */
13227 sgl_pg_pairs->sgl_pg0_addr_lo =
13228 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13229 sgl_pg_pairs->sgl_pg0_addr_hi =
13230 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13231 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13232 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13233 else
13234 pdma_phys_bpl1 = 0;
13235 sgl_pg_pairs->sgl_pg1_addr_lo =
13236 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13237 sgl_pg_pairs->sgl_pg1_addr_hi =
13238 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13239 /* Keep the first xritag on the list */
13240 if (pg_pairs == 0)
13241 xritag_start = psb->cur_iocbq.sli4_xritag;
13242 sgl_pg_pairs++;
13243 pg_pairs++;
13245 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13246 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13247 /* Perform endian conversion if necessary */
13248 sgl->word0 = cpu_to_le32(sgl->word0);
13250 if (!phba->sli4_hba.intr_enable)
13251 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13252 else {
13253 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13254 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13256 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13257 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13258 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13259 if (rc != MBX_TIMEOUT)
13260 lpfc_sli4_mbox_cmd_free(phba, mbox);
13261 if (shdr_status || shdr_add_status || rc) {
13262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13263 "2564 POST_SGL_BLOCK mailbox command failed "
13264 "status x%x add_status x%x mbx status x%x\n",
13265 shdr_status, shdr_add_status, rc);
13266 rc = -ENXIO;
13268 return rc;
13272 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13273 * @phba: pointer to lpfc hba data structure.
13274 * @sblist: pointer to scsi buffer list.
13275 * @count: number of scsi buffers on the list.
13277 * This routine is invoked to post a block of @count scsi sgl pages from a
13278 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13279 * No Lock is held.
13283 lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13284 int cnt)
13286 struct lpfc_scsi_buf *psb = NULL;
13287 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13288 struct sgl_page_pairs *sgl_pg_pairs;
13289 void *viraddr;
13290 LPFC_MBOXQ_t *mbox;
13291 uint32_t reqlen, alloclen, pg_pairs;
13292 uint32_t mbox_tmo;
13293 uint16_t xri_start = 0, scsi_xri_start;
13294 uint16_t rsrc_range;
13295 int rc = 0, avail_cnt;
13296 uint32_t shdr_status, shdr_add_status;
13297 dma_addr_t pdma_phys_bpl1;
13298 union lpfc_sli4_cfg_shdr *shdr;
13299 struct lpfc_rsrc_blks *rsrc_blk;
13300 uint32_t xri_cnt = 0;
13302 /* Calculate the total requested length of the dma memory */
13303 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13304 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13305 if (reqlen > SLI4_PAGE_SIZE) {
13306 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13307 "2932 Block sgl registration required DMA "
13308 "size (%d) great than a page\n", reqlen);
13309 return -ENOMEM;
13313 * The use of extents requires the driver to post the sgl headers
13314 * in multiple postings to meet the contiguous resource assignment.
13316 psb = list_prepare_entry(psb, sblist, list);
13317 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13318 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13319 list) {
13320 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13321 if (rsrc_range < scsi_xri_start)
13322 continue;
13323 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13324 continue;
13325 else
13326 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13328 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13329 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13331 * Allocate DMA memory and set up the non-embedded mailbox
13332 * command. The mbox is used to post an SGL page per loop
13333 * but the DMA memory has a use-once semantic so the mailbox
13334 * is used and freed per loop pass.
13336 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13337 if (!mbox) {
13338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13339 "2933 Failed to allocate mbox cmd "
13340 "memory\n");
13341 return -ENOMEM;
13343 alloclen = lpfc_sli4_config(phba, mbox,
13344 LPFC_MBOX_SUBSYSTEM_FCOE,
13345 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13346 reqlen,
13347 LPFC_SLI4_MBX_NEMBED);
13348 if (alloclen < reqlen) {
13349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13350 "2934 Allocated DMA memory size (%d) "
13351 "is less than the requested DMA memory "
13352 "size (%d)\n", alloclen, reqlen);
13353 lpfc_sli4_mbox_cmd_free(phba, mbox);
13354 return -ENOMEM;
13357 /* Get the first SGE entry from the non-embedded DMA memory */
13358 viraddr = mbox->sge_array->addr[0];
13360 /* Set up the SGL pages in the non-embedded DMA pages */
13361 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13362 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13364 /* pg_pairs tracks posted SGEs per loop iteration. */
13365 pg_pairs = 0;
13366 list_for_each_entry_continue(psb, sblist, list) {
13367 /* Set up the sge entry */
13368 sgl_pg_pairs->sgl_pg0_addr_lo =
13369 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13370 sgl_pg_pairs->sgl_pg0_addr_hi =
13371 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13372 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13373 pdma_phys_bpl1 = psb->dma_phys_bpl +
13374 SGL_PAGE_SIZE;
13375 else
13376 pdma_phys_bpl1 = 0;
13377 sgl_pg_pairs->sgl_pg1_addr_lo =
13378 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13379 sgl_pg_pairs->sgl_pg1_addr_hi =
13380 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13381 /* Keep the first xri for this extent. */
13382 if (pg_pairs == 0)
13383 xri_start = psb->cur_iocbq.sli4_xritag;
13384 sgl_pg_pairs++;
13385 pg_pairs++;
13386 xri_cnt++;
13389 * Track two exit conditions - the loop has constructed
13390 * all of the caller's SGE pairs or all available
13391 * resource IDs in this extent are consumed.
13393 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13394 break;
13396 rsrc_blk->rsrc_used += pg_pairs;
13397 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13398 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13400 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13401 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13402 "blk use %d\n",
13403 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13404 /* Perform endian conversion if necessary */
13405 sgl->word0 = cpu_to_le32(sgl->word0);
13406 if (!phba->sli4_hba.intr_enable)
13407 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13408 else {
13409 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13410 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13412 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13413 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13414 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13415 &shdr->response);
13416 if (rc != MBX_TIMEOUT)
13417 lpfc_sli4_mbox_cmd_free(phba, mbox);
13418 if (shdr_status || shdr_add_status || rc) {
13419 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13420 "2935 POST_SGL_BLOCK mailbox command "
13421 "failed status x%x add_status x%x "
13422 "mbx status x%x\n",
13423 shdr_status, shdr_add_status, rc);
13424 return -ENXIO;
13427 /* Post only what is requested. */
13428 if (xri_cnt >= cnt)
13429 break;
13431 return rc;
13435 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13436 * @phba: pointer to lpfc_hba struct that the frame was received on
13437 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13439 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13440 * valid type of frame that the LPFC driver will handle. This function will
13441 * return a zero if the frame is a valid frame or a non zero value when the
13442 * frame does not pass the check.
13444 static int
13445 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13447 /* make rctl_names static to save stack space */
13448 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
13449 char *type_names[] = FC_TYPE_NAMES_INIT;
13450 struct fc_vft_header *fc_vft_hdr;
13451 uint32_t *header = (uint32_t *) fc_hdr;
13453 switch (fc_hdr->fh_r_ctl) {
13454 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13455 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13456 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13457 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13458 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13459 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13460 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13461 case FC_RCTL_DD_CMD_STATUS: /* command status */
13462 case FC_RCTL_ELS_REQ: /* extended link services request */
13463 case FC_RCTL_ELS_REP: /* extended link services reply */
13464 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13465 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13466 case FC_RCTL_BA_NOP: /* basic link service NOP */
13467 case FC_RCTL_BA_ABTS: /* basic link service abort */
13468 case FC_RCTL_BA_RMC: /* remove connection */
13469 case FC_RCTL_BA_ACC: /* basic accept */
13470 case FC_RCTL_BA_RJT: /* basic reject */
13471 case FC_RCTL_BA_PRMT:
13472 case FC_RCTL_ACK_1: /* acknowledge_1 */
13473 case FC_RCTL_ACK_0: /* acknowledge_0 */
13474 case FC_RCTL_P_RJT: /* port reject */
13475 case FC_RCTL_F_RJT: /* fabric reject */
13476 case FC_RCTL_P_BSY: /* port busy */
13477 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13478 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13479 case FC_RCTL_LCR: /* link credit reset */
13480 case FC_RCTL_END: /* end */
13481 break;
13482 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13483 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13484 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13485 return lpfc_fc_frame_check(phba, fc_hdr);
13486 default:
13487 goto drop;
13489 switch (fc_hdr->fh_type) {
13490 case FC_TYPE_BLS:
13491 case FC_TYPE_ELS:
13492 case FC_TYPE_FCP:
13493 case FC_TYPE_CT:
13494 break;
13495 case FC_TYPE_IP:
13496 case FC_TYPE_ILS:
13497 default:
13498 goto drop;
13501 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13502 "2538 Received frame rctl:%s type:%s "
13503 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
13504 rctl_names[fc_hdr->fh_r_ctl],
13505 type_names[fc_hdr->fh_type],
13506 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13507 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13508 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
13509 return 0;
13510 drop:
13511 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13512 "2539 Dropped frame rctl:%s type:%s\n",
13513 rctl_names[fc_hdr->fh_r_ctl],
13514 type_names[fc_hdr->fh_type]);
13515 return 1;
13519 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13520 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13522 * This function processes the FC header to retrieve the VFI from the VF
13523 * header, if one exists. This function will return the VFI if one exists
13524 * or 0 if no VSAN Header exists.
13526 static uint32_t
13527 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13529 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13531 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13532 return 0;
13533 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13537 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13538 * @phba: Pointer to the HBA structure to search for the vport on
13539 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13540 * @fcfi: The FC Fabric ID that the frame came from
13542 * This function searches the @phba for a vport that matches the content of the
13543 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13544 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13545 * returns the matching vport pointer or NULL if unable to match frame to a
13546 * vport.
13548 static struct lpfc_vport *
13549 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13550 uint16_t fcfi)
13552 struct lpfc_vport **vports;
13553 struct lpfc_vport *vport = NULL;
13554 int i;
13555 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13556 fc_hdr->fh_d_id[1] << 8 |
13557 fc_hdr->fh_d_id[2]);
13558 if (did == Fabric_DID)
13559 return phba->pport;
13560 vports = lpfc_create_vport_work_array(phba);
13561 if (vports != NULL)
13562 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13563 if (phba->fcf.fcfi == fcfi &&
13564 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13565 vports[i]->fc_myDID == did) {
13566 vport = vports[i];
13567 break;
13570 lpfc_destroy_vport_work_array(phba, vports);
13571 return vport;
13575 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13576 * @vport: The vport to work on.
13578 * This function updates the receive sequence time stamp for this vport. The
13579 * receive sequence time stamp indicates the time that the last frame of the
13580 * the sequence that has been idle for the longest amount of time was received.
13581 * the driver uses this time stamp to indicate if any received sequences have
13582 * timed out.
13584 void
13585 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13587 struct lpfc_dmabuf *h_buf;
13588 struct hbq_dmabuf *dmabuf = NULL;
13590 /* get the oldest sequence on the rcv list */
13591 h_buf = list_get_first(&vport->rcv_buffer_list,
13592 struct lpfc_dmabuf, list);
13593 if (!h_buf)
13594 return;
13595 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13596 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13600 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13601 * @vport: The vport that the received sequences were sent to.
13603 * This function cleans up all outstanding received sequences. This is called
13604 * by the driver when a link event or user action invalidates all the received
13605 * sequences.
13607 void
13608 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13610 struct lpfc_dmabuf *h_buf, *hnext;
13611 struct lpfc_dmabuf *d_buf, *dnext;
13612 struct hbq_dmabuf *dmabuf = NULL;
13614 /* start with the oldest sequence on the rcv list */
13615 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13616 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13617 list_del_init(&dmabuf->hbuf.list);
13618 list_for_each_entry_safe(d_buf, dnext,
13619 &dmabuf->dbuf.list, list) {
13620 list_del_init(&d_buf->list);
13621 lpfc_in_buf_free(vport->phba, d_buf);
13623 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13628 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13629 * @vport: The vport that the received sequences were sent to.
13631 * This function determines whether any received sequences have timed out by
13632 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13633 * indicates that there is at least one timed out sequence this routine will
13634 * go through the received sequences one at a time from most inactive to most
13635 * active to determine which ones need to be cleaned up. Once it has determined
13636 * that a sequence needs to be cleaned up it will simply free up the resources
13637 * without sending an abort.
13639 void
13640 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13642 struct lpfc_dmabuf *h_buf, *hnext;
13643 struct lpfc_dmabuf *d_buf, *dnext;
13644 struct hbq_dmabuf *dmabuf = NULL;
13645 unsigned long timeout;
13646 int abort_count = 0;
13648 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13649 vport->rcv_buffer_time_stamp);
13650 if (list_empty(&vport->rcv_buffer_list) ||
13651 time_before(jiffies, timeout))
13652 return;
13653 /* start with the oldest sequence on the rcv list */
13654 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13655 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13656 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13657 dmabuf->time_stamp);
13658 if (time_before(jiffies, timeout))
13659 break;
13660 abort_count++;
13661 list_del_init(&dmabuf->hbuf.list);
13662 list_for_each_entry_safe(d_buf, dnext,
13663 &dmabuf->dbuf.list, list) {
13664 list_del_init(&d_buf->list);
13665 lpfc_in_buf_free(vport->phba, d_buf);
13667 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13669 if (abort_count)
13670 lpfc_update_rcv_time_stamp(vport);
13674 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13675 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13677 * This function searches through the existing incomplete sequences that have
13678 * been sent to this @vport. If the frame matches one of the incomplete
13679 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13680 * make up that sequence. If no sequence is found that matches this frame then
13681 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13682 * This function returns a pointer to the first dmabuf in the sequence list that
13683 * the frame was linked to.
13685 static struct hbq_dmabuf *
13686 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13688 struct fc_frame_header *new_hdr;
13689 struct fc_frame_header *temp_hdr;
13690 struct lpfc_dmabuf *d_buf;
13691 struct lpfc_dmabuf *h_buf;
13692 struct hbq_dmabuf *seq_dmabuf = NULL;
13693 struct hbq_dmabuf *temp_dmabuf = NULL;
13695 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13696 dmabuf->time_stamp = jiffies;
13697 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13698 /* Use the hdr_buf to find the sequence that this frame belongs to */
13699 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13700 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13701 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13702 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13703 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13704 continue;
13705 /* found a pending sequence that matches this frame */
13706 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13707 break;
13709 if (!seq_dmabuf) {
13711 * This indicates first frame received for this sequence.
13712 * Queue the buffer on the vport's rcv_buffer_list.
13714 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13715 lpfc_update_rcv_time_stamp(vport);
13716 return dmabuf;
13718 temp_hdr = seq_dmabuf->hbuf.virt;
13719 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13720 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13721 list_del_init(&seq_dmabuf->hbuf.list);
13722 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13723 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13724 lpfc_update_rcv_time_stamp(vport);
13725 return dmabuf;
13727 /* move this sequence to the tail to indicate a young sequence */
13728 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13729 seq_dmabuf->time_stamp = jiffies;
13730 lpfc_update_rcv_time_stamp(vport);
13731 if (list_empty(&seq_dmabuf->dbuf.list)) {
13732 temp_hdr = dmabuf->hbuf.virt;
13733 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13734 return seq_dmabuf;
13736 /* find the correct place in the sequence to insert this frame */
13737 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13738 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13739 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13741 * If the frame's sequence count is greater than the frame on
13742 * the list then insert the frame right after this frame
13744 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13745 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
13746 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13747 return seq_dmabuf;
13750 return NULL;
13754 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13755 * @vport: pointer to a vitural port
13756 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13758 * This function tries to abort from the partially assembed sequence, described
13759 * by the information from basic abbort @dmabuf. It checks to see whether such
13760 * partially assembled sequence held by the driver. If so, it shall free up all
13761 * the frames from the partially assembled sequence.
13763 * Return
13764 * true -- if there is matching partially assembled sequence present and all
13765 * the frames freed with the sequence;
13766 * false -- if there is no matching partially assembled sequence present so
13767 * nothing got aborted in the lower layer driver
13769 static bool
13770 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13771 struct hbq_dmabuf *dmabuf)
13773 struct fc_frame_header *new_hdr;
13774 struct fc_frame_header *temp_hdr;
13775 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13776 struct hbq_dmabuf *seq_dmabuf = NULL;
13778 /* Use the hdr_buf to find the sequence that matches this frame */
13779 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13780 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13781 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13782 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13783 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13784 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13785 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13786 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13787 continue;
13788 /* found a pending sequence that matches this frame */
13789 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13790 break;
13793 /* Free up all the frames from the partially assembled sequence */
13794 if (seq_dmabuf) {
13795 list_for_each_entry_safe(d_buf, n_buf,
13796 &seq_dmabuf->dbuf.list, list) {
13797 list_del_init(&d_buf->list);
13798 lpfc_in_buf_free(vport->phba, d_buf);
13800 return true;
13802 return false;
13806 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
13807 * @phba: Pointer to HBA context object.
13808 * @cmd_iocbq: pointer to the command iocbq structure.
13809 * @rsp_iocbq: pointer to the response iocbq structure.
13811 * This function handles the sequence abort response iocb command complete
13812 * event. It properly releases the memory allocated to the sequence abort
13813 * accept iocb.
13815 static void
13816 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
13817 struct lpfc_iocbq *cmd_iocbq,
13818 struct lpfc_iocbq *rsp_iocbq)
13820 if (cmd_iocbq)
13821 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13825 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13826 * @phba: Pointer to HBA context object.
13827 * @xri: xri id in transaction.
13829 * This function validates the xri maps to the known range of XRIs allocated an
13830 * used by the driver.
13832 uint16_t
13833 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13834 uint16_t xri)
13836 int i;
13838 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13839 if (xri == phba->sli4_hba.xri_ids[i])
13840 return i;
13842 return NO_XRI;
13847 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
13848 * @phba: Pointer to HBA context object.
13849 * @fc_hdr: pointer to a FC frame header.
13851 * This function sends a basic response to a previous unsol sequence abort
13852 * event after aborting the sequence handling.
13854 static void
13855 lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
13856 struct fc_frame_header *fc_hdr)
13858 struct lpfc_iocbq *ctiocb = NULL;
13859 struct lpfc_nodelist *ndlp;
13860 uint16_t oxid, rxid;
13861 uint32_t sid, fctl;
13862 IOCB_t *icmd;
13863 int rc;
13865 if (!lpfc_is_link_up(phba))
13866 return;
13868 sid = sli4_sid_from_fc_hdr(fc_hdr);
13869 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
13870 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
13872 ndlp = lpfc_findnode_did(phba->pport, sid);
13873 if (!ndlp) {
13874 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13875 "1268 Find ndlp returned NULL for oxid:x%x "
13876 "SID:x%x\n", oxid, sid);
13877 return;
13879 if (lpfc_sli4_xri_inrange(phba, rxid))
13880 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
13882 /* Allocate buffer for rsp iocb */
13883 ctiocb = lpfc_sli_get_iocbq(phba);
13884 if (!ctiocb)
13885 return;
13887 /* Extract the F_CTL field from FC_HDR */
13888 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13890 icmd = &ctiocb->iocb;
13891 icmd->un.xseq64.bdl.bdeSize = 0;
13892 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
13893 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13894 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13895 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13897 /* Fill in the rest of iocb fields */
13898 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13899 icmd->ulpBdeCount = 0;
13900 icmd->ulpLe = 1;
13901 icmd->ulpClass = CLASS3;
13902 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
13903 ctiocb->context1 = ndlp;
13905 ctiocb->iocb_cmpl = NULL;
13906 ctiocb->vport = phba->pport;
13907 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
13908 ctiocb->sli4_lxritag = NO_XRI;
13909 ctiocb->sli4_xritag = NO_XRI;
13911 /* If the oxid maps to the FCP XRI range or if it is out of range,
13912 * send a BLS_RJT. The driver no longer has that exchange.
13913 * Override the IOCB for a BA_RJT.
13915 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13916 phba->sli4_hba.max_cfg_param.xri_base) ||
13917 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13918 phba->sli4_hba.max_cfg_param.xri_base)) {
13919 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13920 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13921 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13922 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13925 if (fctl & FC_FC_EX_CTX) {
13926 /* ABTS sent by responder to CT exchange, construction
13927 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13928 * field and RX_ID from ABTS for RX_ID field.
13930 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13931 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
13932 } else {
13933 /* ABTS sent by initiator to CT exchange, construction
13934 * of BA_ACC will need to allocate a new XRI as for the
13935 * XRI_TAG and RX_ID fields.
13937 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13938 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
13940 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
13942 /* Xmit CT abts response on exchange <xid> */
13943 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13944 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13945 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13947 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13948 if (rc == IOCB_ERROR) {
13949 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13950 "2925 Failed to issue CT ABTS RSP x%x on "
13951 "xri x%x, Data x%x\n",
13952 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13953 phba->link_state);
13954 lpfc_sli_release_iocbq(phba, ctiocb);
13959 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13960 * @vport: Pointer to the vport on which this sequence was received
13961 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13963 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13964 * receive sequence is only partially assembed by the driver, it shall abort
13965 * the partially assembled frames for the sequence. Otherwise, if the
13966 * unsolicited receive sequence has been completely assembled and passed to
13967 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13968 * unsolicited sequence has been aborted. After that, it will issue a basic
13969 * accept to accept the abort.
13971 void
13972 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13973 struct hbq_dmabuf *dmabuf)
13975 struct lpfc_hba *phba = vport->phba;
13976 struct fc_frame_header fc_hdr;
13977 uint32_t fctl;
13978 bool abts_par;
13980 /* Make a copy of fc_hdr before the dmabuf being released */
13981 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
13982 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
13984 if (fctl & FC_FC_EX_CTX) {
13986 * ABTS sent by responder to exchange, just free the buffer
13988 lpfc_in_buf_free(phba, &dmabuf->dbuf);
13989 } else {
13991 * ABTS sent by initiator to exchange, need to do cleanup
13993 /* Try to abort partially assembled seq */
13994 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
13996 /* Send abort to ULP if partially seq abort failed */
13997 if (abts_par == false)
13998 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
13999 else
14000 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14002 /* Send basic accept (BA_ACC) to the abort requester */
14003 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
14007 * lpfc_seq_complete - Indicates if a sequence is complete
14008 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14010 * This function checks the sequence, starting with the frame described by
14011 * @dmabuf, to see if all the frames associated with this sequence are present.
14012 * the frames associated with this sequence are linked to the @dmabuf using the
14013 * dbuf list. This function looks for two major things. 1) That the first frame
14014 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14015 * set. 3) That there are no holes in the sequence count. The function will
14016 * return 1 when the sequence is complete, otherwise it will return 0.
14018 static int
14019 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14021 struct fc_frame_header *hdr;
14022 struct lpfc_dmabuf *d_buf;
14023 struct hbq_dmabuf *seq_dmabuf;
14024 uint32_t fctl;
14025 int seq_count = 0;
14027 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14028 /* make sure first fame of sequence has a sequence count of zero */
14029 if (hdr->fh_seq_cnt != seq_count)
14030 return 0;
14031 fctl = (hdr->fh_f_ctl[0] << 16 |
14032 hdr->fh_f_ctl[1] << 8 |
14033 hdr->fh_f_ctl[2]);
14034 /* If last frame of sequence we can return success. */
14035 if (fctl & FC_FC_END_SEQ)
14036 return 1;
14037 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14038 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14039 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14040 /* If there is a hole in the sequence count then fail. */
14041 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
14042 return 0;
14043 fctl = (hdr->fh_f_ctl[0] << 16 |
14044 hdr->fh_f_ctl[1] << 8 |
14045 hdr->fh_f_ctl[2]);
14046 /* If last frame of sequence we can return success. */
14047 if (fctl & FC_FC_END_SEQ)
14048 return 1;
14050 return 0;
14054 * lpfc_prep_seq - Prep sequence for ULP processing
14055 * @vport: Pointer to the vport on which this sequence was received
14056 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14058 * This function takes a sequence, described by a list of frames, and creates
14059 * a list of iocbq structures to describe the sequence. This iocbq list will be
14060 * used to issue to the generic unsolicited sequence handler. This routine
14061 * returns a pointer to the first iocbq in the list. If the function is unable
14062 * to allocate an iocbq then it throw out the received frames that were not
14063 * able to be described and return a pointer to the first iocbq. If unable to
14064 * allocate any iocbqs (including the first) this function will return NULL.
14066 static struct lpfc_iocbq *
14067 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14069 struct hbq_dmabuf *hbq_buf;
14070 struct lpfc_dmabuf *d_buf, *n_buf;
14071 struct lpfc_iocbq *first_iocbq, *iocbq;
14072 struct fc_frame_header *fc_hdr;
14073 uint32_t sid;
14074 uint32_t len, tot_len;
14075 struct ulp_bde64 *pbde;
14077 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14078 /* remove from receive buffer list */
14079 list_del_init(&seq_dmabuf->hbuf.list);
14080 lpfc_update_rcv_time_stamp(vport);
14081 /* get the Remote Port's SID */
14082 sid = sli4_sid_from_fc_hdr(fc_hdr);
14083 tot_len = 0;
14084 /* Get an iocbq struct to fill in. */
14085 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14086 if (first_iocbq) {
14087 /* Initialize the first IOCB. */
14088 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
14089 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14090 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
14091 first_iocbq->iocb.ulpContext = NO_XRI;
14092 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14093 be16_to_cpu(fc_hdr->fh_ox_id);
14094 /* iocbq is prepped for internal consumption. Physical vpi. */
14095 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14096 vport->phba->vpi_ids[vport->vpi];
14097 /* put the first buffer into the first IOCBq */
14098 first_iocbq->context2 = &seq_dmabuf->dbuf;
14099 first_iocbq->context3 = NULL;
14100 first_iocbq->iocb.ulpBdeCount = 1;
14101 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14102 LPFC_DATA_BUF_SIZE;
14103 first_iocbq->iocb.un.rcvels.remoteID = sid;
14104 tot_len = bf_get(lpfc_rcqe_length,
14105 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14106 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14108 iocbq = first_iocbq;
14110 * Each IOCBq can have two Buffers assigned, so go through the list
14111 * of buffers for this sequence and save two buffers in each IOCBq
14113 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14114 if (!iocbq) {
14115 lpfc_in_buf_free(vport->phba, d_buf);
14116 continue;
14118 if (!iocbq->context3) {
14119 iocbq->context3 = d_buf;
14120 iocbq->iocb.ulpBdeCount++;
14121 pbde = (struct ulp_bde64 *)
14122 &iocbq->iocb.unsli3.sli3Words[4];
14123 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14125 /* We need to get the size out of the right CQE */
14126 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14127 len = bf_get(lpfc_rcqe_length,
14128 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14129 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14130 tot_len += len;
14131 } else {
14132 iocbq = lpfc_sli_get_iocbq(vport->phba);
14133 if (!iocbq) {
14134 if (first_iocbq) {
14135 first_iocbq->iocb.ulpStatus =
14136 IOSTAT_FCP_RSP_ERROR;
14137 first_iocbq->iocb.un.ulpWord[4] =
14138 IOERR_NO_RESOURCES;
14140 lpfc_in_buf_free(vport->phba, d_buf);
14141 continue;
14143 iocbq->context2 = d_buf;
14144 iocbq->context3 = NULL;
14145 iocbq->iocb.ulpBdeCount = 1;
14146 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14147 LPFC_DATA_BUF_SIZE;
14149 /* We need to get the size out of the right CQE */
14150 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14151 len = bf_get(lpfc_rcqe_length,
14152 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14153 tot_len += len;
14154 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14156 iocbq->iocb.un.rcvels.remoteID = sid;
14157 list_add_tail(&iocbq->list, &first_iocbq->list);
14160 return first_iocbq;
14163 static void
14164 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14165 struct hbq_dmabuf *seq_dmabuf)
14167 struct fc_frame_header *fc_hdr;
14168 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14169 struct lpfc_hba *phba = vport->phba;
14171 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14172 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14173 if (!iocbq) {
14174 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14175 "2707 Ring %d handler: Failed to allocate "
14176 "iocb Rctl x%x Type x%x received\n",
14177 LPFC_ELS_RING,
14178 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14179 return;
14181 if (!lpfc_complete_unsol_iocb(phba,
14182 &phba->sli.ring[LPFC_ELS_RING],
14183 iocbq, fc_hdr->fh_r_ctl,
14184 fc_hdr->fh_type))
14185 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14186 "2540 Ring %d handler: unexpected Rctl "
14187 "x%x Type x%x received\n",
14188 LPFC_ELS_RING,
14189 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14191 /* Free iocb created in lpfc_prep_seq */
14192 list_for_each_entry_safe(curr_iocb, next_iocb,
14193 &iocbq->list, list) {
14194 list_del_init(&curr_iocb->list);
14195 lpfc_sli_release_iocbq(phba, curr_iocb);
14197 lpfc_sli_release_iocbq(phba, iocbq);
14201 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14202 * @phba: Pointer to HBA context object.
14204 * This function is called with no lock held. This function processes all
14205 * the received buffers and gives it to upper layers when a received buffer
14206 * indicates that it is the final frame in the sequence. The interrupt
14207 * service routine processes received buffers at interrupt contexts and adds
14208 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14209 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14210 * appropriate receive function when the final frame in a sequence is received.
14212 void
14213 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14214 struct hbq_dmabuf *dmabuf)
14216 struct hbq_dmabuf *seq_dmabuf;
14217 struct fc_frame_header *fc_hdr;
14218 struct lpfc_vport *vport;
14219 uint32_t fcfi;
14221 /* Process each received buffer */
14222 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14223 /* check to see if this a valid type of frame */
14224 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14225 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14226 return;
14228 if ((bf_get(lpfc_cqe_code,
14229 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14230 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14231 &dmabuf->cq_event.cqe.rcqe_cmpl);
14232 else
14233 fcfi = bf_get(lpfc_rcqe_fcf_id,
14234 &dmabuf->cq_event.cqe.rcqe_cmpl);
14235 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
14236 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
14237 /* throw out the frame */
14238 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14239 return;
14241 /* Handle the basic abort sequence (BA_ABTS) event */
14242 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14243 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14244 return;
14247 /* Link this frame */
14248 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14249 if (!seq_dmabuf) {
14250 /* unable to add frame to vport - throw it out */
14251 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14252 return;
14254 /* If not last frame in sequence continue processing frames. */
14255 if (!lpfc_seq_complete(seq_dmabuf))
14256 return;
14258 /* Send the complete sequence to the upper layer protocol */
14259 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
14263 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14264 * @phba: pointer to lpfc hba data structure.
14266 * This routine is invoked to post rpi header templates to the
14267 * HBA consistent with the SLI-4 interface spec. This routine
14268 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14269 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14271 * This routine does not require any locks. It's usage is expected
14272 * to be driver load or reset recovery when the driver is
14273 * sequential.
14275 * Return codes
14276 * 0 - successful
14277 * -EIO - The mailbox failed to complete successfully.
14278 * When this error occurs, the driver is not guaranteed
14279 * to have any rpi regions posted to the device and
14280 * must either attempt to repost the regions or take a
14281 * fatal error.
14284 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14286 struct lpfc_rpi_hdr *rpi_page;
14287 uint32_t rc = 0;
14288 uint16_t lrpi = 0;
14290 /* SLI4 ports that support extents do not require RPI headers. */
14291 if (!phba->sli4_hba.rpi_hdrs_in_use)
14292 goto exit;
14293 if (phba->sli4_hba.extents_in_use)
14294 return -EIO;
14296 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
14298 * Assign the rpi headers a physical rpi only if the driver
14299 * has not initialized those resources. A port reset only
14300 * needs the headers posted.
14302 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14303 LPFC_RPI_RSRC_RDY)
14304 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14306 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14307 if (rc != MBX_SUCCESS) {
14308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14309 "2008 Error %d posting all rpi "
14310 "headers\n", rc);
14311 rc = -EIO;
14312 break;
14316 exit:
14317 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14318 LPFC_RPI_RSRC_RDY);
14319 return rc;
14323 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14324 * @phba: pointer to lpfc hba data structure.
14325 * @rpi_page: pointer to the rpi memory region.
14327 * This routine is invoked to post a single rpi header to the
14328 * HBA consistent with the SLI-4 interface spec. This memory region
14329 * maps up to 64 rpi context regions.
14331 * Return codes
14332 * 0 - successful
14333 * -ENOMEM - No available memory
14334 * -EIO - The mailbox failed to complete successfully.
14337 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14339 LPFC_MBOXQ_t *mboxq;
14340 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14341 uint32_t rc = 0;
14342 uint32_t shdr_status, shdr_add_status;
14343 union lpfc_sli4_cfg_shdr *shdr;
14345 /* SLI4 ports that support extents do not require RPI headers. */
14346 if (!phba->sli4_hba.rpi_hdrs_in_use)
14347 return rc;
14348 if (phba->sli4_hba.extents_in_use)
14349 return -EIO;
14351 /* The port is notified of the header region via a mailbox command. */
14352 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14353 if (!mboxq) {
14354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14355 "2001 Unable to allocate memory for issuing "
14356 "SLI_CONFIG_SPECIAL mailbox command\n");
14357 return -ENOMEM;
14360 /* Post all rpi memory regions to the port. */
14361 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
14362 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14363 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14364 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
14365 sizeof(struct lpfc_sli4_cfg_mhdr),
14366 LPFC_SLI4_MBX_EMBED);
14369 /* Post the physical rpi to the port for this rpi header. */
14370 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14371 rpi_page->start_rpi);
14372 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14373 hdr_tmpl, rpi_page->page_count);
14375 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14376 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
14377 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
14378 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14379 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14380 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14381 if (rc != MBX_TIMEOUT)
14382 mempool_free(mboxq, phba->mbox_mem_pool);
14383 if (shdr_status || shdr_add_status || rc) {
14384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14385 "2514 POST_RPI_HDR mailbox failed with "
14386 "status x%x add_status x%x, mbx status x%x\n",
14387 shdr_status, shdr_add_status, rc);
14388 rc = -ENXIO;
14390 return rc;
14394 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14395 * @phba: pointer to lpfc hba data structure.
14397 * This routine is invoked to post rpi header templates to the
14398 * HBA consistent with the SLI-4 interface spec. This routine
14399 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14400 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14402 * Returns
14403 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14404 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14407 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14409 unsigned long rpi;
14410 uint16_t max_rpi, rpi_limit;
14411 uint16_t rpi_remaining, lrpi = 0;
14412 struct lpfc_rpi_hdr *rpi_hdr;
14414 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
14415 rpi_limit = phba->sli4_hba.next_rpi;
14418 * Fetch the next logical rpi. Because this index is logical,
14419 * the driver starts at 0 each time.
14421 spin_lock_irq(&phba->hbalock);
14422 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14423 if (rpi >= rpi_limit)
14424 rpi = LPFC_RPI_ALLOC_ERROR;
14425 else {
14426 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14427 phba->sli4_hba.max_cfg_param.rpi_used++;
14428 phba->sli4_hba.rpi_count++;
14432 * Don't try to allocate more rpi header regions if the device limit
14433 * has been exhausted.
14435 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14436 (phba->sli4_hba.rpi_count >= max_rpi)) {
14437 spin_unlock_irq(&phba->hbalock);
14438 return rpi;
14442 * RPI header postings are not required for SLI4 ports capable of
14443 * extents.
14445 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14446 spin_unlock_irq(&phba->hbalock);
14447 return rpi;
14451 * If the driver is running low on rpi resources, allocate another
14452 * page now. Note that the next_rpi value is used because
14453 * it represents how many are actually in use whereas max_rpi notes
14454 * how many are supported max by the device.
14456 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
14457 spin_unlock_irq(&phba->hbalock);
14458 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14459 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14460 if (!rpi_hdr) {
14461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14462 "2002 Error Could not grow rpi "
14463 "count\n");
14464 } else {
14465 lrpi = rpi_hdr->start_rpi;
14466 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14467 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14471 return rpi;
14475 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14476 * @phba: pointer to lpfc hba data structure.
14478 * This routine is invoked to release an rpi to the pool of
14479 * available rpis maintained by the driver.
14481 void
14482 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14484 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14485 phba->sli4_hba.rpi_count--;
14486 phba->sli4_hba.max_cfg_param.rpi_used--;
14491 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14492 * @phba: pointer to lpfc hba data structure.
14494 * This routine is invoked to release an rpi to the pool of
14495 * available rpis maintained by the driver.
14497 void
14498 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14500 spin_lock_irq(&phba->hbalock);
14501 __lpfc_sli4_free_rpi(phba, rpi);
14502 spin_unlock_irq(&phba->hbalock);
14506 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14507 * @phba: pointer to lpfc hba data structure.
14509 * This routine is invoked to remove the memory region that
14510 * provided rpi via a bitmask.
14512 void
14513 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14515 kfree(phba->sli4_hba.rpi_bmask);
14516 kfree(phba->sli4_hba.rpi_ids);
14517 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
14521 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14522 * @phba: pointer to lpfc hba data structure.
14524 * This routine is invoked to remove the memory region that
14525 * provided rpi via a bitmask.
14528 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14530 LPFC_MBOXQ_t *mboxq;
14531 struct lpfc_hba *phba = ndlp->phba;
14532 int rc;
14534 /* The port is notified of the header region via a mailbox command. */
14535 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14536 if (!mboxq)
14537 return -ENOMEM;
14539 /* Post all rpi memory regions to the port. */
14540 lpfc_resume_rpi(mboxq, ndlp);
14541 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14542 if (rc == MBX_NOT_FINISHED) {
14543 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14544 "2010 Resume RPI Mailbox failed "
14545 "status %d, mbxStatus x%x\n", rc,
14546 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14547 mempool_free(mboxq, phba->mbox_mem_pool);
14548 return -EIO;
14550 return 0;
14554 * lpfc_sli4_init_vpi - Initialize a vpi with the port
14555 * @vport: Pointer to the vport for which the vpi is being initialized
14557 * This routine is invoked to activate a vpi with the port.
14559 * Returns:
14560 * 0 success
14561 * -Evalue otherwise
14564 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
14566 LPFC_MBOXQ_t *mboxq;
14567 int rc = 0;
14568 int retval = MBX_SUCCESS;
14569 uint32_t mbox_tmo;
14570 struct lpfc_hba *phba = vport->phba;
14571 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14572 if (!mboxq)
14573 return -ENOMEM;
14574 lpfc_init_vpi(phba, mboxq, vport->vpi);
14575 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
14576 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
14577 if (rc != MBX_SUCCESS) {
14578 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
14579 "2022 INIT VPI Mailbox failed "
14580 "status %d, mbxStatus x%x\n", rc,
14581 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14582 retval = -EIO;
14584 if (rc != MBX_TIMEOUT)
14585 mempool_free(mboxq, vport->phba->mbox_mem_pool);
14587 return retval;
14591 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14592 * @phba: pointer to lpfc hba data structure.
14593 * @mboxq: Pointer to mailbox object.
14595 * This routine is invoked to manually add a single FCF record. The caller
14596 * must pass a completely initialized FCF_Record. This routine takes
14597 * care of the nonembedded mailbox operations.
14599 static void
14600 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14602 void *virt_addr;
14603 union lpfc_sli4_cfg_shdr *shdr;
14604 uint32_t shdr_status, shdr_add_status;
14606 virt_addr = mboxq->sge_array->addr[0];
14607 /* The IOCTL status is embedded in the mailbox subheader. */
14608 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14612 if ((shdr_status || shdr_add_status) &&
14613 (shdr_status != STATUS_FCF_IN_USE))
14614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14615 "2558 ADD_FCF_RECORD mailbox failed with "
14616 "status x%x add_status x%x\n",
14617 shdr_status, shdr_add_status);
14619 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14623 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14624 * @phba: pointer to lpfc hba data structure.
14625 * @fcf_record: pointer to the initialized fcf record to add.
14627 * This routine is invoked to manually add a single FCF record. The caller
14628 * must pass a completely initialized FCF_Record. This routine takes
14629 * care of the nonembedded mailbox operations.
14632 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14634 int rc = 0;
14635 LPFC_MBOXQ_t *mboxq;
14636 uint8_t *bytep;
14637 void *virt_addr;
14638 dma_addr_t phys_addr;
14639 struct lpfc_mbx_sge sge;
14640 uint32_t alloc_len, req_len;
14641 uint32_t fcfindex;
14643 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14644 if (!mboxq) {
14645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14646 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14647 return -ENOMEM;
14650 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14651 sizeof(uint32_t);
14653 /* Allocate DMA memory and set up the non-embedded mailbox command */
14654 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14655 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14656 req_len, LPFC_SLI4_MBX_NEMBED);
14657 if (alloc_len < req_len) {
14658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14659 "2523 Allocated DMA memory size (x%x) is "
14660 "less than the requested DMA memory "
14661 "size (x%x)\n", alloc_len, req_len);
14662 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14663 return -ENOMEM;
14667 * Get the first SGE entry from the non-embedded DMA memory. This
14668 * routine only uses a single SGE.
14670 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14671 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
14672 virt_addr = mboxq->sge_array->addr[0];
14674 * Configure the FCF record for FCFI 0. This is the driver's
14675 * hardcoded default and gets used in nonFIP mode.
14677 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14678 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14679 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14682 * Copy the fcf_index and the FCF Record Data. The data starts after
14683 * the FCoE header plus word10. The data copy needs to be endian
14684 * correct.
14686 bytep += sizeof(uint32_t);
14687 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14688 mboxq->vport = phba->pport;
14689 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14690 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14691 if (rc == MBX_NOT_FINISHED) {
14692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14693 "2515 ADD_FCF_RECORD mailbox failed with "
14694 "status 0x%x\n", rc);
14695 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14696 rc = -EIO;
14697 } else
14698 rc = 0;
14700 return rc;
14704 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14705 * @phba: pointer to lpfc hba data structure.
14706 * @fcf_record: pointer to the fcf record to write the default data.
14707 * @fcf_index: FCF table entry index.
14709 * This routine is invoked to build the driver's default FCF record. The
14710 * values used are hardcoded. This routine handles memory initialization.
14713 void
14714 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14715 struct fcf_record *fcf_record,
14716 uint16_t fcf_index)
14718 memset(fcf_record, 0, sizeof(struct fcf_record));
14719 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14720 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14721 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14722 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14723 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14724 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14725 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14726 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14727 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14728 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14729 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14730 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14731 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
14732 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
14733 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14734 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14735 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14736 /* Set the VLAN bit map */
14737 if (phba->valid_vlan) {
14738 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14739 = 1 << (phba->vlan_id % 8);
14744 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
14745 * @phba: pointer to lpfc hba data structure.
14746 * @fcf_index: FCF table entry offset.
14748 * This routine is invoked to scan the entire FCF table by reading FCF
14749 * record and processing it one at a time starting from the @fcf_index
14750 * for initial FCF discovery or fast FCF failover rediscovery.
14752 * Return 0 if the mailbox command is submitted successfully, none 0
14753 * otherwise.
14756 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14758 int rc = 0, error;
14759 LPFC_MBOXQ_t *mboxq;
14761 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
14762 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14763 if (!mboxq) {
14764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14765 "2000 Failed to allocate mbox for "
14766 "READ_FCF cmd\n");
14767 error = -ENOMEM;
14768 goto fail_fcf_scan;
14770 /* Construct the read FCF record mailbox command */
14771 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14772 if (rc) {
14773 error = -EINVAL;
14774 goto fail_fcf_scan;
14776 /* Issue the mailbox command asynchronously */
14777 mboxq->vport = phba->pport;
14778 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
14780 spin_lock_irq(&phba->hbalock);
14781 phba->hba_flag |= FCF_TS_INPROG;
14782 spin_unlock_irq(&phba->hbalock);
14784 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14785 if (rc == MBX_NOT_FINISHED)
14786 error = -EIO;
14787 else {
14788 /* Reset eligible FCF count for new scan */
14789 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
14790 phba->fcf.eligible_fcf_cnt = 0;
14791 error = 0;
14793 fail_fcf_scan:
14794 if (error) {
14795 if (mboxq)
14796 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14797 /* FCF scan failed, clear FCF_TS_INPROG flag */
14798 spin_lock_irq(&phba->hbalock);
14799 phba->hba_flag &= ~FCF_TS_INPROG;
14800 spin_unlock_irq(&phba->hbalock);
14802 return error;
14806 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
14807 * @phba: pointer to lpfc hba data structure.
14808 * @fcf_index: FCF table entry offset.
14810 * This routine is invoked to read an FCF record indicated by @fcf_index
14811 * and to use it for FLOGI roundrobin FCF failover.
14813 * Return 0 if the mailbox command is submitted successfully, none 0
14814 * otherwise.
14817 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14819 int rc = 0, error;
14820 LPFC_MBOXQ_t *mboxq;
14822 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14823 if (!mboxq) {
14824 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14825 "2763 Failed to allocate mbox for "
14826 "READ_FCF cmd\n");
14827 error = -ENOMEM;
14828 goto fail_fcf_read;
14830 /* Construct the read FCF record mailbox command */
14831 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14832 if (rc) {
14833 error = -EINVAL;
14834 goto fail_fcf_read;
14836 /* Issue the mailbox command asynchronously */
14837 mboxq->vport = phba->pport;
14838 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14839 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14840 if (rc == MBX_NOT_FINISHED)
14841 error = -EIO;
14842 else
14843 error = 0;
14845 fail_fcf_read:
14846 if (error && mboxq)
14847 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14848 return error;
14852 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14853 * @phba: pointer to lpfc hba data structure.
14854 * @fcf_index: FCF table entry offset.
14856 * This routine is invoked to read an FCF record indicated by @fcf_index to
14857 * determine whether it's eligible for FLOGI roundrobin failover list.
14859 * Return 0 if the mailbox command is submitted successfully, none 0
14860 * otherwise.
14863 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14865 int rc = 0, error;
14866 LPFC_MBOXQ_t *mboxq;
14868 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14869 if (!mboxq) {
14870 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14871 "2758 Failed to allocate mbox for "
14872 "READ_FCF cmd\n");
14873 error = -ENOMEM;
14874 goto fail_fcf_read;
14876 /* Construct the read FCF record mailbox command */
14877 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14878 if (rc) {
14879 error = -EINVAL;
14880 goto fail_fcf_read;
14882 /* Issue the mailbox command asynchronously */
14883 mboxq->vport = phba->pport;
14884 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14885 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14886 if (rc == MBX_NOT_FINISHED)
14887 error = -EIO;
14888 else
14889 error = 0;
14891 fail_fcf_read:
14892 if (error && mboxq)
14893 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14894 return error;
14898 * lpfc_check_next_fcf_pri
14899 * phba pointer to the lpfc_hba struct for this port.
14900 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14901 * routine when the rr_bmask is empty. The FCF indecies are put into the
14902 * rr_bmask based on their priority level. Starting from the highest priority
14903 * to the lowest. The most likely FCF candidate will be in the highest
14904 * priority group. When this routine is called it searches the fcf_pri list for
14905 * next lowest priority group and repopulates the rr_bmask with only those
14906 * fcf_indexes.
14907 * returns:
14908 * 1=success 0=failure
14911 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14913 uint16_t next_fcf_pri;
14914 uint16_t last_index;
14915 struct lpfc_fcf_pri *fcf_pri;
14916 int rc;
14917 int ret = 0;
14919 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14920 LPFC_SLI4_FCF_TBL_INDX_MAX);
14921 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14922 "3060 Last IDX %d\n", last_index);
14923 if (list_empty(&phba->fcf.fcf_pri_list)) {
14924 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14925 "3061 Last IDX %d\n", last_index);
14926 return 0; /* Empty rr list */
14928 next_fcf_pri = 0;
14930 * Clear the rr_bmask and set all of the bits that are at this
14931 * priority.
14933 memset(phba->fcf.fcf_rr_bmask, 0,
14934 sizeof(*phba->fcf.fcf_rr_bmask));
14935 spin_lock_irq(&phba->hbalock);
14936 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14937 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14938 continue;
14940 * the 1st priority that has not FLOGI failed
14941 * will be the highest.
14943 if (!next_fcf_pri)
14944 next_fcf_pri = fcf_pri->fcf_rec.priority;
14945 spin_unlock_irq(&phba->hbalock);
14946 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14947 rc = lpfc_sli4_fcf_rr_index_set(phba,
14948 fcf_pri->fcf_rec.fcf_index);
14949 if (rc)
14950 return 0;
14952 spin_lock_irq(&phba->hbalock);
14955 * if next_fcf_pri was not set above and the list is not empty then
14956 * we have failed flogis on all of them. So reset flogi failed
14957 * and start at the begining.
14959 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14960 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14961 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14963 * the 1st priority that has not FLOGI failed
14964 * will be the highest.
14966 if (!next_fcf_pri)
14967 next_fcf_pri = fcf_pri->fcf_rec.priority;
14968 spin_unlock_irq(&phba->hbalock);
14969 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14970 rc = lpfc_sli4_fcf_rr_index_set(phba,
14971 fcf_pri->fcf_rec.fcf_index);
14972 if (rc)
14973 return 0;
14975 spin_lock_irq(&phba->hbalock);
14977 } else
14978 ret = 1;
14979 spin_unlock_irq(&phba->hbalock);
14981 return ret;
14984 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
14985 * @phba: pointer to lpfc hba data structure.
14987 * This routine is to get the next eligible FCF record index in a round
14988 * robin fashion. If the next eligible FCF record index equals to the
14989 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
14990 * shall be returned, otherwise, the next eligible FCF record's index
14991 * shall be returned.
14993 uint16_t
14994 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
14996 uint16_t next_fcf_index;
14998 /* Search start from next bit of currently registered FCF index */
14999 next_priority:
15000 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15001 LPFC_SLI4_FCF_TBL_INDX_MAX;
15002 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15003 LPFC_SLI4_FCF_TBL_INDX_MAX,
15004 next_fcf_index);
15006 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
15007 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15009 * If we have wrapped then we need to clear the bits that
15010 * have been tested so that we can detect when we should
15011 * change the priority level.
15013 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15014 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
15018 /* Check roundrobin failover list empty condition */
15019 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15020 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15022 * If next fcf index is not found check if there are lower
15023 * Priority level fcf's in the fcf_priority list.
15024 * Set up the rr_bmask with all of the avaiable fcf bits
15025 * at that level and continue the selection process.
15027 if (lpfc_check_next_fcf_pri_level(phba))
15028 goto next_priority;
15029 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15030 "2844 No roundrobin failover FCF available\n");
15031 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15032 return LPFC_FCOE_FCF_NEXT_NONE;
15033 else {
15034 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15035 "3063 Only FCF available idx %d, flag %x\n",
15036 next_fcf_index,
15037 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15038 return next_fcf_index;
15042 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15043 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15044 LPFC_FCF_FLOGI_FAILED)
15045 goto next_priority;
15047 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15048 "2845 Get next roundrobin failover FCF (x%x)\n",
15049 next_fcf_index);
15051 return next_fcf_index;
15055 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15056 * @phba: pointer to lpfc hba data structure.
15058 * This routine sets the FCF record index in to the eligible bmask for
15059 * roundrobin failover search. It checks to make sure that the index
15060 * does not go beyond the range of the driver allocated bmask dimension
15061 * before setting the bit.
15063 * Returns 0 if the index bit successfully set, otherwise, it returns
15064 * -EINVAL.
15067 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15069 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15070 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15071 "2610 FCF (x%x) reached driver's book "
15072 "keeping dimension:x%x\n",
15073 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15074 return -EINVAL;
15076 /* Set the eligible FCF record index bmask */
15077 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15079 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15080 "2790 Set FCF (x%x) to roundrobin FCF failover "
15081 "bmask\n", fcf_index);
15083 return 0;
15087 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
15088 * @phba: pointer to lpfc hba data structure.
15090 * This routine clears the FCF record index from the eligible bmask for
15091 * roundrobin failover search. It checks to make sure that the index
15092 * does not go beyond the range of the driver allocated bmask dimension
15093 * before clearing the bit.
15095 void
15096 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15098 struct lpfc_fcf_pri *fcf_pri;
15099 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15100 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15101 "2762 FCF (x%x) reached driver's book "
15102 "keeping dimension:x%x\n",
15103 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15104 return;
15106 /* Clear the eligible FCF record index bmask */
15107 spin_lock_irq(&phba->hbalock);
15108 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15109 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15110 list_del_init(&fcf_pri->list);
15111 break;
15114 spin_unlock_irq(&phba->hbalock);
15115 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15117 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15118 "2791 Clear FCF (x%x) from roundrobin failover "
15119 "bmask\n", fcf_index);
15123 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15124 * @phba: pointer to lpfc hba data structure.
15126 * This routine is the completion routine for the rediscover FCF table mailbox
15127 * command. If the mailbox command returned failure, it will try to stop the
15128 * FCF rediscover wait timer.
15130 void
15131 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15133 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15134 uint32_t shdr_status, shdr_add_status;
15136 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15138 shdr_status = bf_get(lpfc_mbox_hdr_status,
15139 &redisc_fcf->header.cfg_shdr.response);
15140 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15141 &redisc_fcf->header.cfg_shdr.response);
15142 if (shdr_status || shdr_add_status) {
15143 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15144 "2746 Requesting for FCF rediscovery failed "
15145 "status x%x add_status x%x\n",
15146 shdr_status, shdr_add_status);
15147 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
15148 spin_lock_irq(&phba->hbalock);
15149 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
15150 spin_unlock_irq(&phba->hbalock);
15152 * CVL event triggered FCF rediscover request failed,
15153 * last resort to re-try current registered FCF entry.
15155 lpfc_retry_pport_discovery(phba);
15156 } else {
15157 spin_lock_irq(&phba->hbalock);
15158 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
15159 spin_unlock_irq(&phba->hbalock);
15161 * DEAD FCF event triggered FCF rediscover request
15162 * failed, last resort to fail over as a link down
15163 * to FCF registration.
15165 lpfc_sli4_fcf_dead_failthrough(phba);
15167 } else {
15168 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15169 "2775 Start FCF rediscover quiescent timer\n");
15171 * Start FCF rediscovery wait timer for pending FCF
15172 * before rescan FCF record table.
15174 lpfc_fcf_redisc_wait_start_timer(phba);
15177 mempool_free(mbox, phba->mbox_mem_pool);
15181 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
15182 * @phba: pointer to lpfc hba data structure.
15184 * This routine is invoked to request for rediscovery of the entire FCF table
15185 * by the port.
15188 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15190 LPFC_MBOXQ_t *mbox;
15191 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15192 int rc, length;
15194 /* Cancel retry delay timers to all vports before FCF rediscover */
15195 lpfc_cancel_all_vport_retry_delay_timer(phba);
15197 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15198 if (!mbox) {
15199 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15200 "2745 Failed to allocate mbox for "
15201 "requesting FCF rediscover.\n");
15202 return -ENOMEM;
15205 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15206 sizeof(struct lpfc_sli4_cfg_mhdr));
15207 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15208 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15209 length, LPFC_SLI4_MBX_EMBED);
15211 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15212 /* Set count to 0 for invalidating the entire FCF database */
15213 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15215 /* Issue the mailbox command asynchronously */
15216 mbox->vport = phba->pport;
15217 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15218 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15220 if (rc == MBX_NOT_FINISHED) {
15221 mempool_free(mbox, phba->mbox_mem_pool);
15222 return -EIO;
15224 return 0;
15228 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15229 * @phba: pointer to lpfc hba data structure.
15231 * This function is the failover routine as a last resort to the FCF DEAD
15232 * event when driver failed to perform fast FCF failover.
15234 void
15235 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15237 uint32_t link_state;
15240 * Last resort as FCF DEAD event failover will treat this as
15241 * a link down, but save the link state because we don't want
15242 * it to be changed to Link Down unless it is already down.
15244 link_state = phba->link_state;
15245 lpfc_linkdown(phba);
15246 phba->link_state = link_state;
15248 /* Unregister FCF if no devices connected to it */
15249 lpfc_unregister_unused_fcf(phba);
15253 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15254 * @phba: pointer to lpfc hba data structure.
15256 * This function read region 23 and parse TLV for port status to
15257 * decide if the user disaled the port. If the TLV indicates the
15258 * port is disabled, the hba_flag is set accordingly.
15260 void
15261 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15263 LPFC_MBOXQ_t *pmb = NULL;
15264 MAILBOX_t *mb;
15265 uint8_t *rgn23_data = NULL;
15266 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
15267 int rc;
15269 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15270 if (!pmb) {
15271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15272 "2600 lpfc_sli_read_serdes_param failed to"
15273 " allocate mailbox memory\n");
15274 goto out;
15276 mb = &pmb->u.mb;
15278 /* Get adapter Region 23 data */
15279 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15280 if (!rgn23_data)
15281 goto out;
15283 do {
15284 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15285 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15287 if (rc != MBX_SUCCESS) {
15288 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15289 "2601 lpfc_sli_read_link_ste failed to"
15290 " read config region 23 rc 0x%x Status 0x%x\n",
15291 rc, mb->mbxStatus);
15292 mb->un.varDmp.word_cnt = 0;
15295 * dump mem may return a zero when finished or we got a
15296 * mailbox error, either way we are done.
15298 if (mb->un.varDmp.word_cnt == 0)
15299 break;
15300 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15301 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15303 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15304 rgn23_data + offset,
15305 mb->un.varDmp.word_cnt);
15306 offset += mb->un.varDmp.word_cnt;
15307 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15309 data_size = offset;
15310 offset = 0;
15312 if (!data_size)
15313 goto out;
15315 /* Check the region signature first */
15316 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15318 "2619 Config region 23 has bad signature\n");
15319 goto out;
15321 offset += 4;
15323 /* Check the data structure version */
15324 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15326 "2620 Config region 23 has bad version\n");
15327 goto out;
15329 offset += 4;
15331 /* Parse TLV entries in the region */
15332 while (offset < data_size) {
15333 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15334 break;
15336 * If the TLV is not driver specific TLV or driver id is
15337 * not linux driver id, skip the record.
15339 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15340 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15341 (rgn23_data[offset + 3] != 0)) {
15342 offset += rgn23_data[offset + 1] * 4 + 4;
15343 continue;
15346 /* Driver found a driver specific TLV in the config region */
15347 sub_tlv_len = rgn23_data[offset + 1] * 4;
15348 offset += 4;
15349 tlv_offset = 0;
15352 * Search for configured port state sub-TLV.
15354 while ((offset < data_size) &&
15355 (tlv_offset < sub_tlv_len)) {
15356 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15357 offset += 4;
15358 tlv_offset += 4;
15359 break;
15361 if (rgn23_data[offset] != PORT_STE_TYPE) {
15362 offset += rgn23_data[offset + 1] * 4 + 4;
15363 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15364 continue;
15367 /* This HBA contains PORT_STE configured */
15368 if (!rgn23_data[offset + 2])
15369 phba->hba_flag |= LINK_DISABLED;
15371 goto out;
15374 out:
15375 if (pmb)
15376 mempool_free(pmb, phba->mbox_mem_pool);
15377 kfree(rgn23_data);
15378 return;
15382 * lpfc_wr_object - write an object to the firmware
15383 * @phba: HBA structure that indicates port to create a queue on.
15384 * @dmabuf_list: list of dmabufs to write to the port.
15385 * @size: the total byte value of the objects to write to the port.
15386 * @offset: the current offset to be used to start the transfer.
15388 * This routine will create a wr_object mailbox command to send to the port.
15389 * the mailbox command will be constructed using the dma buffers described in
15390 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15391 * BDEs that the imbedded mailbox can support. The @offset variable will be
15392 * used to indicate the starting offset of the transfer and will also return
15393 * the offset after the write object mailbox has completed. @size is used to
15394 * determine the end of the object and whether the eof bit should be set.
15396 * Return 0 is successful and offset will contain the the new offset to use
15397 * for the next write.
15398 * Return negative value for error cases.
15401 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15402 uint32_t size, uint32_t *offset)
15404 struct lpfc_mbx_wr_object *wr_object;
15405 LPFC_MBOXQ_t *mbox;
15406 int rc = 0, i = 0;
15407 uint32_t shdr_status, shdr_add_status;
15408 uint32_t mbox_tmo;
15409 union lpfc_sli4_cfg_shdr *shdr;
15410 struct lpfc_dmabuf *dmabuf;
15411 uint32_t written = 0;
15413 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15414 if (!mbox)
15415 return -ENOMEM;
15417 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15418 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15419 sizeof(struct lpfc_mbx_wr_object) -
15420 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15422 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15423 wr_object->u.request.write_offset = *offset;
15424 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15425 wr_object->u.request.object_name[0] =
15426 cpu_to_le32(wr_object->u.request.object_name[0]);
15427 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15428 list_for_each_entry(dmabuf, dmabuf_list, list) {
15429 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15430 break;
15431 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15432 wr_object->u.request.bde[i].addrHigh =
15433 putPaddrHigh(dmabuf->phys);
15434 if (written + SLI4_PAGE_SIZE >= size) {
15435 wr_object->u.request.bde[i].tus.f.bdeSize =
15436 (size - written);
15437 written += (size - written);
15438 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15439 } else {
15440 wr_object->u.request.bde[i].tus.f.bdeSize =
15441 SLI4_PAGE_SIZE;
15442 written += SLI4_PAGE_SIZE;
15444 i++;
15446 wr_object->u.request.bde_count = i;
15447 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15448 if (!phba->sli4_hba.intr_enable)
15449 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15450 else {
15451 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
15452 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15454 /* The IOCTL status is embedded in the mailbox subheader. */
15455 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15456 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15457 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15458 if (rc != MBX_TIMEOUT)
15459 mempool_free(mbox, phba->mbox_mem_pool);
15460 if (shdr_status || shdr_add_status || rc) {
15461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15462 "3025 Write Object mailbox failed with "
15463 "status x%x add_status x%x, mbx status x%x\n",
15464 shdr_status, shdr_add_status, rc);
15465 rc = -ENXIO;
15466 } else
15467 *offset += wr_object->u.response.actual_write_length;
15468 return rc;
15472 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15473 * @vport: pointer to vport data structure.
15475 * This function iterate through the mailboxq and clean up all REG_LOGIN
15476 * and REG_VPI mailbox commands associated with the vport. This function
15477 * is called when driver want to restart discovery of the vport due to
15478 * a Clear Virtual Link event.
15480 void
15481 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15483 struct lpfc_hba *phba = vport->phba;
15484 LPFC_MBOXQ_t *mb, *nextmb;
15485 struct lpfc_dmabuf *mp;
15486 struct lpfc_nodelist *ndlp;
15487 struct lpfc_nodelist *act_mbx_ndlp = NULL;
15488 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
15489 LIST_HEAD(mbox_cmd_list);
15490 uint8_t restart_loop;
15492 /* Clean up internally queued mailbox commands with the vport */
15493 spin_lock_irq(&phba->hbalock);
15494 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15495 if (mb->vport != vport)
15496 continue;
15498 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15499 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15500 continue;
15502 list_del(&mb->list);
15503 list_add_tail(&mb->list, &mbox_cmd_list);
15505 /* Clean up active mailbox command with the vport */
15506 mb = phba->sli.mbox_active;
15507 if (mb && (mb->vport == vport)) {
15508 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15509 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15510 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15511 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15512 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15513 /* Put reference count for delayed processing */
15514 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15515 /* Unregister the RPI when mailbox complete */
15516 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15519 /* Cleanup any mailbox completions which are not yet processed */
15520 do {
15521 restart_loop = 0;
15522 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15524 * If this mailox is already processed or it is
15525 * for another vport ignore it.
15527 if ((mb->vport != vport) ||
15528 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15529 continue;
15531 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15532 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15533 continue;
15535 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15536 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15537 ndlp = (struct lpfc_nodelist *)mb->context2;
15538 /* Unregister the RPI when mailbox complete */
15539 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15540 restart_loop = 1;
15541 spin_unlock_irq(&phba->hbalock);
15542 spin_lock(shost->host_lock);
15543 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15544 spin_unlock(shost->host_lock);
15545 spin_lock_irq(&phba->hbalock);
15546 break;
15549 } while (restart_loop);
15551 spin_unlock_irq(&phba->hbalock);
15553 /* Release the cleaned-up mailbox commands */
15554 while (!list_empty(&mbox_cmd_list)) {
15555 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
15556 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15557 mp = (struct lpfc_dmabuf *) (mb->context1);
15558 if (mp) {
15559 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15560 kfree(mp);
15562 ndlp = (struct lpfc_nodelist *) mb->context2;
15563 mb->context2 = NULL;
15564 if (ndlp) {
15565 spin_lock(shost->host_lock);
15566 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15567 spin_unlock(shost->host_lock);
15568 lpfc_nlp_put(ndlp);
15571 mempool_free(mb, phba->mbox_mem_pool);
15574 /* Release the ndlp with the cleaned-up active mailbox command */
15575 if (act_mbx_ndlp) {
15576 spin_lock(shost->host_lock);
15577 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15578 spin_unlock(shost->host_lock);
15579 lpfc_nlp_put(act_mbx_ndlp);
15584 * lpfc_drain_txq - Drain the txq
15585 * @phba: Pointer to HBA context object.
15587 * This function attempt to submit IOCBs on the txq
15588 * to the adapter. For SLI4 adapters, the txq contains
15589 * ELS IOCBs that have been deferred because the there
15590 * are no SGLs. This congestion can occur with large
15591 * vport counts during node discovery.
15594 uint32_t
15595 lpfc_drain_txq(struct lpfc_hba *phba)
15597 LIST_HEAD(completions);
15598 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15599 struct lpfc_iocbq *piocbq = 0;
15600 unsigned long iflags = 0;
15601 char *fail_msg = NULL;
15602 struct lpfc_sglq *sglq;
15603 union lpfc_wqe wqe;
15605 spin_lock_irqsave(&phba->hbalock, iflags);
15606 if (pring->txq_cnt > pring->txq_max)
15607 pring->txq_max = pring->txq_cnt;
15609 spin_unlock_irqrestore(&phba->hbalock, iflags);
15611 while (pring->txq_cnt) {
15612 spin_lock_irqsave(&phba->hbalock, iflags);
15614 piocbq = lpfc_sli_ringtx_get(phba, pring);
15615 sglq = __lpfc_sli_get_sglq(phba, piocbq);
15616 if (!sglq) {
15617 __lpfc_sli_ringtx_put(phba, pring, piocbq);
15618 spin_unlock_irqrestore(&phba->hbalock, iflags);
15619 break;
15620 } else {
15621 if (!piocbq) {
15622 /* The txq_cnt out of sync. This should
15623 * never happen
15625 sglq = __lpfc_clear_active_sglq(phba,
15626 sglq->sli4_lxritag);
15627 spin_unlock_irqrestore(&phba->hbalock, iflags);
15628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15629 "2823 txq empty and txq_cnt is %d\n ",
15630 pring->txq_cnt);
15631 break;
15635 /* The xri and iocb resources secured,
15636 * attempt to issue request
15638 piocbq->sli4_lxritag = sglq->sli4_lxritag;
15639 piocbq->sli4_xritag = sglq->sli4_xritag;
15640 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15641 fail_msg = "to convert bpl to sgl";
15642 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15643 fail_msg = "to convert iocb to wqe";
15644 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15645 fail_msg = " - Wq is full";
15646 else
15647 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15649 if (fail_msg) {
15650 /* Failed means we can't issue and need to cancel */
15651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15652 "2822 IOCB failed %s iotag 0x%x "
15653 "xri 0x%x\n",
15654 fail_msg,
15655 piocbq->iotag, piocbq->sli4_xritag);
15656 list_add_tail(&piocbq->list, &completions);
15658 spin_unlock_irqrestore(&phba->hbalock, iflags);
15661 /* Cancel all the IOCBs that cannot be issued */
15662 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15663 IOERR_SLI_ABORTED);
15665 return pring->txq_cnt;