blob: d597e15a1974d325413f79ac05c7ae4455276c95 [file] [log] [blame]
James Smart43140ca2017-03-04 09:30:34 -08001
dea31012005-04-17 16:05:31 -05002/*******************************************************************
3 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04004 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08005 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
6 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07007 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08009 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050011 * *
12 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013 * modify it under the terms of version 2 of the GNU General *
14 * Public License as published by the Free Software Foundation. *
15 * This program is distributed in the hope that it will be useful. *
16 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
17 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
18 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
19 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20 * TO BE LEGALLY INVALID. See the GNU General Public License for *
21 * more details, a copy of which can be found in the file COPYING *
22 * included with this package. *
dea31012005-04-17 16:05:31 -050023 *******************************************************************/
24
dea31012005-04-17 16:05:31 -050025#include <linux/blkdev.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010030#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040036#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040037#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040038#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050039
James Smart895427b2017-02-12 13:52:30 -080040#include <linux/nvme-fc-driver.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080049#include "lpfc_scsi.h"
50#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080051#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050055#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040056#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070057#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050058
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
James Smart4f774512009-05-22 14:52:35 -040067
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040072 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040075static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070077static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
James Smart895427b2017-02-12 13:52:30 -080079static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040080 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040082 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070083static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84 struct lpfc_eqe *eqe, uint32_t qidx);
James Smarte8d3c3b2013-10-10 12:21:30 -040085static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
86static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080087static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
88 struct lpfc_sli_ring *pring,
89 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040090
James Smart4f774512009-05-22 14:52:35 -040091static IOCB_t *
92lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
93{
94 return &iocbq->iocb;
95}
96
97/**
98 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
99 * @q: The Work Queue to operate on.
100 * @wqe: The work Queue Entry to put on the Work queue.
101 *
102 * This routine will copy the contents of @wqe to the next available entry on
103 * the @q. This function will then ring the Work Queue Doorbell to signal the
104 * HBA to start processing the Work Queue Entry. This function returns 0 if
105 * successful. If no entries are available on @q then this function will return
106 * -ENOMEM.
107 * The caller is expected to hold the hbalock when calling this routine.
108 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700109static int
James Smart4f774512009-05-22 14:52:35 -0400110lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
111{
James Smart2e90f4b2011-12-13 13:22:37 -0500112 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400113 struct lpfc_register doorbell;
114 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400115 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400116
James Smart2e90f4b2011-12-13 13:22:37 -0500117 /* sanity check on queue memory */
118 if (unlikely(!q))
119 return -ENOMEM;
120 temp_wqe = q->qe[q->host_index].wqe;
121
James Smart4f774512009-05-22 14:52:35 -0400122 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400123 idx = ((q->host_index + 1) % q->entry_count);
124 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400125 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700126 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400127 }
128 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400129 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500130 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400131 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800132 else
133 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500134 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
135 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400136 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800137 /* ensure WQE bcopy flushed before doorbell write */
138 wmb();
James Smart4f774512009-05-22 14:52:35 -0400139
140 /* Update the host index before invoking device */
141 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400142
143 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400144
145 /* Ring Doorbell */
146 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500147 if (q->db_format == LPFC_DB_LIST_FORMAT) {
148 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
149 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
150 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
151 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
152 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
153 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
154 } else {
155 return -EINVAL;
156 }
157 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400158
159 return 0;
160}
161
162/**
163 * lpfc_sli4_wq_release - Updates internal hba index for WQ
164 * @q: The Work Queue to operate on.
165 * @index: The index to advance the hba index to.
166 *
167 * This routine will update the HBA index of a queue to reflect consumption of
168 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
169 * an entry the host calls this function to update the queue's internal
170 * pointers. This routine returns the number of entries that were consumed by
171 * the HBA.
172 **/
173static uint32_t
174lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
175{
176 uint32_t released = 0;
177
James Smart2e90f4b2011-12-13 13:22:37 -0500178 /* sanity check on queue memory */
179 if (unlikely(!q))
180 return 0;
181
James Smart4f774512009-05-22 14:52:35 -0400182 if (q->hba_index == index)
183 return 0;
184 do {
185 q->hba_index = ((q->hba_index + 1) % q->entry_count);
186 released++;
187 } while (q->hba_index != index);
188 return released;
189}
190
191/**
192 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
193 * @q: The Mailbox Queue to operate on.
194 * @wqe: The Mailbox Queue Entry to put on the Work queue.
195 *
196 * This routine will copy the contents of @mqe to the next available entry on
197 * the @q. This function will then ring the Work Queue Doorbell to signal the
198 * HBA to start processing the Work Queue Entry. This function returns 0 if
199 * successful. If no entries are available on @q then this function will return
200 * -ENOMEM.
201 * The caller is expected to hold the hbalock when calling this routine.
202 **/
203static uint32_t
204lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
205{
James Smart2e90f4b2011-12-13 13:22:37 -0500206 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400207 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400208
James Smart2e90f4b2011-12-13 13:22:37 -0500209 /* sanity check on queue memory */
210 if (unlikely(!q))
211 return -ENOMEM;
212 temp_mqe = q->qe[q->host_index].mqe;
213
James Smart4f774512009-05-22 14:52:35 -0400214 /* If the host has not yet processed the next entry then we are done */
215 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
216 return -ENOMEM;
217 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
218 /* Save off the mailbox pointer for completion */
219 q->phba->mbox = (MAILBOX_t *)temp_mqe;
220
221 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400222 q->host_index = ((q->host_index + 1) % q->entry_count);
223
224 /* Ring Doorbell */
225 doorbell.word0 = 0;
226 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
227 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
228 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400229 return 0;
230}
231
232/**
233 * lpfc_sli4_mq_release - Updates internal hba index for MQ
234 * @q: The Mailbox Queue to operate on.
235 *
236 * This routine will update the HBA index of a queue to reflect consumption of
237 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
238 * an entry the host calls this function to update the queue's internal
239 * pointers. This routine returns the number of entries that were consumed by
240 * the HBA.
241 **/
242static uint32_t
243lpfc_sli4_mq_release(struct lpfc_queue *q)
244{
James Smart2e90f4b2011-12-13 13:22:37 -0500245 /* sanity check on queue memory */
246 if (unlikely(!q))
247 return 0;
248
James Smart4f774512009-05-22 14:52:35 -0400249 /* Clear the mailbox pointer for completion */
250 q->phba->mbox = NULL;
251 q->hba_index = ((q->hba_index + 1) % q->entry_count);
252 return 1;
253}
254
255/**
256 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
257 * @q: The Event Queue to get the first valid EQE from
258 *
259 * This routine will get the first valid Event Queue Entry from @q, update
260 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
261 * the Queue (no more work to do), or the Queue is full of EQEs that have been
262 * processed, but not popped back to the HBA then this routine will return NULL.
263 **/
264static struct lpfc_eqe *
265lpfc_sli4_eq_get(struct lpfc_queue *q)
266{
James Smart2e90f4b2011-12-13 13:22:37 -0500267 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400268 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500269
270 /* sanity check on queue memory */
271 if (unlikely(!q))
272 return NULL;
273 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400274
275 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400276 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400277 return NULL;
278 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400279 idx = ((q->hba_index + 1) % q->entry_count);
280 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400281 return NULL;
282
James Smart027140e2012-08-03 12:35:44 -0400283 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400284
285 /*
286 * insert barrier for instruction interlock : data from the hardware
287 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800288 * upon. Speculative instructions were allowing a bcopy at the start
289 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
290 * after our return, to copy data before the valid bit check above
291 * was done. As such, some of the copied data was stale. The barrier
292 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400293 */
294 mb();
James Smart4f774512009-05-22 14:52:35 -0400295 return eqe;
296}
297
298/**
James Smartba20c852012-08-03 12:36:52 -0400299 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
300 * @q: The Event Queue to disable interrupts
301 *
302 **/
303static inline void
304lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
305{
306 struct lpfc_register doorbell;
307
308 doorbell.word0 = 0;
309 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
310 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
311 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
312 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
313 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
314 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
315}
316
317/**
James Smart4f774512009-05-22 14:52:35 -0400318 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
319 * @q: The Event Queue that the host has completed processing for.
320 * @arm: Indicates whether the host wants to arms this CQ.
321 *
322 * This routine will mark all Event Queue Entries on @q, from the last
323 * known completed entry to the last entry that was processed, as completed
324 * by clearing the valid bit for each completion queue entry. Then it will
325 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
326 * The internal host index in the @q will be updated by this routine to indicate
327 * that the host has finished processing the entries. The @arm parameter
328 * indicates that the queue should be rearmed when ringing the doorbell.
329 *
330 * This function will return the number of EQEs that were popped.
331 **/
332uint32_t
333lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
334{
335 uint32_t released = 0;
336 struct lpfc_eqe *temp_eqe;
337 struct lpfc_register doorbell;
338
James Smart2e90f4b2011-12-13 13:22:37 -0500339 /* sanity check on queue memory */
340 if (unlikely(!q))
341 return 0;
342
James Smart4f774512009-05-22 14:52:35 -0400343 /* while there are valid entries */
344 while (q->hba_index != q->host_index) {
345 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400346 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400347 released++;
348 q->host_index = ((q->host_index + 1) % q->entry_count);
349 }
350 if (unlikely(released == 0 && !arm))
351 return 0;
352
353 /* ring doorbell for number popped */
354 doorbell.word0 = 0;
355 if (arm) {
356 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
357 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
358 }
359 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
360 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500361 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
362 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
363 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400364 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500365 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
366 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
367 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400368 return released;
369}
370
371/**
372 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
373 * @q: The Completion Queue to get the first valid CQE from
374 *
375 * This routine will get the first valid Completion Queue Entry from @q, update
376 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
377 * the Queue (no more work to do), or the Queue is full of CQEs that have been
378 * processed, but not popped back to the HBA then this routine will return NULL.
379 **/
380static struct lpfc_cqe *
381lpfc_sli4_cq_get(struct lpfc_queue *q)
382{
383 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400384 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400385
James Smart2e90f4b2011-12-13 13:22:37 -0500386 /* sanity check on queue memory */
387 if (unlikely(!q))
388 return NULL;
389
James Smart4f774512009-05-22 14:52:35 -0400390 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400391 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400392 return NULL;
393 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400394 idx = ((q->hba_index + 1) % q->entry_count);
395 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400396 return NULL;
397
398 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400399 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400400
401 /*
402 * insert barrier for instruction interlock : data from the hardware
403 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800404 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
405 * instructions allowing action on content before valid bit checked,
406 * add barrier here as well. May not be needed as "content" is a
407 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400408 */
409 mb();
James Smart4f774512009-05-22 14:52:35 -0400410 return cqe;
411}
412
413/**
414 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
415 * @q: The Completion Queue that the host has completed processing for.
416 * @arm: Indicates whether the host wants to arms this CQ.
417 *
418 * This routine will mark all Completion queue entries on @q, from the last
419 * known completed entry to the last entry that was processed, as completed
420 * by clearing the valid bit for each completion queue entry. Then it will
421 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
422 * The internal host index in the @q will be updated by this routine to indicate
423 * that the host has finished processing the entries. The @arm parameter
424 * indicates that the queue should be rearmed when ringing the doorbell.
425 *
426 * This function will return the number of CQEs that were released.
427 **/
428uint32_t
429lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
430{
431 uint32_t released = 0;
432 struct lpfc_cqe *temp_qe;
433 struct lpfc_register doorbell;
434
James Smart2e90f4b2011-12-13 13:22:37 -0500435 /* sanity check on queue memory */
436 if (unlikely(!q))
437 return 0;
James Smart4f774512009-05-22 14:52:35 -0400438 /* while there are valid entries */
439 while (q->hba_index != q->host_index) {
440 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400441 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400442 released++;
443 q->host_index = ((q->host_index + 1) % q->entry_count);
444 }
445 if (unlikely(released == 0 && !arm))
446 return 0;
447
448 /* ring doorbell for number popped */
449 doorbell.word0 = 0;
450 if (arm)
451 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
452 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
453 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500454 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
455 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
456 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400457 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
458 return released;
459}
460
461/**
462 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
463 * @q: The Header Receive Queue to operate on.
464 * @wqe: The Receive Queue Entry to put on the Receive queue.
465 *
466 * This routine will copy the contents of @wqe to the next available entry on
467 * the @q. This function will then ring the Receive Queue Doorbell to signal the
468 * HBA to start processing the Receive Queue Entry. This function returns the
469 * index that the rqe was copied to if successful. If no entries are available
470 * on @q then this function will return -ENOMEM.
471 * The caller is expected to hold the hbalock when calling this routine.
472 **/
James Smart895427b2017-02-12 13:52:30 -0800473int
James Smart4f774512009-05-22 14:52:35 -0400474lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
475 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
476{
James Smart2e90f4b2011-12-13 13:22:37 -0500477 struct lpfc_rqe *temp_hrqe;
478 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400479 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800480 int hq_put_index;
481 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400482
James Smart2e90f4b2011-12-13 13:22:37 -0500483 /* sanity check on queue memory */
484 if (unlikely(!hq) || unlikely(!dq))
485 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800486 hq_put_index = hq->host_index;
487 dq_put_index = dq->host_index;
488 temp_hrqe = hq->qe[hq_put_index].rqe;
489 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500490
James Smart4f774512009-05-22 14:52:35 -0400491 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
492 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800493 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400494 return -EINVAL;
495 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800496 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400497 return -EBUSY;
498 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
499 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
500
501 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800502 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
503 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700504 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400505
506 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400507 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400508 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500509 if (hq->db_format == LPFC_DB_RING_FORMAT) {
510 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
511 hq->entry_repost);
512 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
513 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
514 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
515 hq->entry_repost);
516 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
517 hq->host_index);
518 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
519 } else {
520 return -EINVAL;
521 }
522 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400523 }
James Smartcbc5de12017-12-08 17:18:04 -0800524 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400525}
526
527/**
528 * lpfc_sli4_rq_release - Updates internal hba index for RQ
529 * @q: The Header Receive Queue to operate on.
530 *
531 * This routine will update the HBA index of a queue to reflect consumption of
532 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
533 * consumed an entry the host calls this function to update the queue's
534 * internal pointers. This routine returns the number of entries that were
535 * consumed by the HBA.
536 **/
537static uint32_t
538lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
539{
James Smart2e90f4b2011-12-13 13:22:37 -0500540 /* sanity check on queue memory */
541 if (unlikely(!hq) || unlikely(!dq))
542 return 0;
543
James Smart4f774512009-05-22 14:52:35 -0400544 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
545 return 0;
546 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
547 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
548 return 1;
549}
550
James Smarte59058c2008-08-24 21:49:00 -0400551/**
James Smart3621a712009-04-06 18:47:14 -0400552 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400553 * @phba: Pointer to HBA context object.
554 * @pring: Pointer to driver SLI ring object.
555 *
556 * This function returns pointer to next command iocb entry
557 * in the command ring. The caller must hold hbalock to prevent
558 * other threads consume the next command iocb.
559 * SLI-2/SLI-3 provide different sized iocbs.
560 **/
James Smarted957682007-06-17 19:56:37 -0500561static inline IOCB_t *
562lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
563{
James Smart7e56aa22012-08-03 12:35:34 -0400564 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
565 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500566}
567
James Smarte59058c2008-08-24 21:49:00 -0400568/**
James Smart3621a712009-04-06 18:47:14 -0400569 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400570 * @phba: Pointer to HBA context object.
571 * @pring: Pointer to driver SLI ring object.
572 *
573 * This function returns pointer to next response iocb entry
574 * in the response ring. The caller must hold hbalock to make sure
575 * that no other thread consume the next response iocb.
576 * SLI-2/SLI-3 provide different sized iocbs.
577 **/
James Smarted957682007-06-17 19:56:37 -0500578static inline IOCB_t *
579lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
580{
James Smart7e56aa22012-08-03 12:35:34 -0400581 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
582 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500583}
584
James Smarte59058c2008-08-24 21:49:00 -0400585/**
James Smart3621a712009-04-06 18:47:14 -0400586 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400587 * @phba: Pointer to HBA context object.
588 *
589 * This function is called with hbalock held. This function
590 * allocates a new driver iocb object from the iocb pool. If the
591 * allocation is successful, it returns pointer to the newly
592 * allocated iocb object else it returns NULL.
593 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400594struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500595__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400596{
597 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
598 struct lpfc_iocbq * iocbq = NULL;
599
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100600 lockdep_assert_held(&phba->hbalock);
601
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400602 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400603 if (iocbq)
604 phba->iocb_cnt++;
605 if (phba->iocb_cnt > phba->iocb_max)
606 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400607 return iocbq;
608}
609
James Smarte59058c2008-08-24 21:49:00 -0400610/**
James Smartda0436e2009-05-22 14:51:39 -0400611 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
612 * @phba: Pointer to HBA context object.
613 * @xritag: XRI value.
614 *
615 * This function clears the sglq pointer from the array of acive
616 * sglq's. The xritag that is passed in is used to index into the
617 * array. Before the xritag can be used it needs to be adjusted
618 * by subtracting the xribase.
619 *
620 * Returns sglq ponter = success, NULL = Failure.
621 **/
James Smart895427b2017-02-12 13:52:30 -0800622struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400623__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
624{
James Smartda0436e2009-05-22 14:51:39 -0400625 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400626
627 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
628 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400629 return sglq;
630}
631
632/**
633 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
634 * @phba: Pointer to HBA context object.
635 * @xritag: XRI value.
636 *
637 * This function returns the sglq pointer from the array of acive
638 * sglq's. The xritag that is passed in is used to index into the
639 * array. Before the xritag can be used it needs to be adjusted
640 * by subtracting the xribase.
641 *
642 * Returns sglq ponter = success, NULL = Failure.
643 **/
James Smart0f65ff62010-02-26 14:14:23 -0500644struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400645__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
646{
James Smartda0436e2009-05-22 14:51:39 -0400647 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400648
649 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400650 return sglq;
651}
652
653/**
James Smart1151e3e2011-02-16 12:39:35 -0500654 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500655 * @phba: Pointer to HBA context object.
656 * @xritag: xri used in this exchange.
657 * @rrq: The RRQ to be cleared.
658 *
James Smart19ca7602010-11-20 23:11:55 -0500659 **/
James Smart1151e3e2011-02-16 12:39:35 -0500660void
661lpfc_clr_rrq_active(struct lpfc_hba *phba,
662 uint16_t xritag,
663 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500664{
James Smart1151e3e2011-02-16 12:39:35 -0500665 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500666
James Smart1151e3e2011-02-16 12:39:35 -0500667 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
668 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500669
670 /* The target DID could have been swapped (cable swap)
671 * we should use the ndlp from the findnode if it is
672 * available.
673 */
James Smart1151e3e2011-02-16 12:39:35 -0500674 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500675 ndlp = rrq->ndlp;
676
James Smart1151e3e2011-02-16 12:39:35 -0500677 if (!ndlp)
678 goto out;
679
James Smartcff261f2013-12-17 20:29:47 -0500680 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500681 rrq->send_rrq = 0;
682 rrq->xritag = 0;
683 rrq->rrq_stop_time = 0;
684 }
James Smart1151e3e2011-02-16 12:39:35 -0500685out:
James Smart19ca7602010-11-20 23:11:55 -0500686 mempool_free(rrq, phba->rrq_pool);
687}
688
689/**
690 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
691 * @phba: Pointer to HBA context object.
692 *
693 * This function is called with hbalock held. This function
694 * Checks if stop_time (ratov from setting rrq active) has
695 * been reached, if it has and the send_rrq flag is set then
696 * it will call lpfc_send_rrq. If the send_rrq flag is not set
697 * then it will just call the routine to clear the rrq and
698 * free the rrq resource.
699 * The timer is set to the next rrq that is going to expire before
700 * leaving the routine.
701 *
702 **/
703void
704lpfc_handle_rrq_active(struct lpfc_hba *phba)
705{
706 struct lpfc_node_rrq *rrq;
707 struct lpfc_node_rrq *nextrrq;
708 unsigned long next_time;
709 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500710 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500711
712 spin_lock_irqsave(&phba->hbalock, iflags);
713 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400714 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500715 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500716 &phba->active_rrq_list, list) {
717 if (time_after(jiffies, rrq->rrq_stop_time))
718 list_move(&rrq->list, &send_rrq);
719 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500720 next_time = rrq->rrq_stop_time;
721 }
722 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500723 if ((!list_empty(&phba->active_rrq_list)) &&
724 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500725 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500726 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
727 list_del(&rrq->list);
728 if (!rrq->send_rrq)
729 /* this call will free the rrq */
730 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
731 else if (lpfc_send_rrq(phba, rrq)) {
732 /* if we send the rrq then the completion handler
733 * will clear the bit in the xribitmap.
734 */
735 lpfc_clr_rrq_active(phba, rrq->xritag,
736 rrq);
737 }
738 }
James Smart19ca7602010-11-20 23:11:55 -0500739}
740
741/**
742 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
743 * @vport: Pointer to vport context object.
744 * @xri: The xri used in the exchange.
745 * @did: The targets DID for this exchange.
746 *
747 * returns NULL = rrq not found in the phba->active_rrq_list.
748 * rrq = rrq for this xri and target.
749 **/
750struct lpfc_node_rrq *
751lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
752{
753 struct lpfc_hba *phba = vport->phba;
754 struct lpfc_node_rrq *rrq;
755 struct lpfc_node_rrq *nextrrq;
756 unsigned long iflags;
757
758 if (phba->sli_rev != LPFC_SLI_REV4)
759 return NULL;
760 spin_lock_irqsave(&phba->hbalock, iflags);
761 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
762 if (rrq->vport == vport && rrq->xritag == xri &&
763 rrq->nlp_DID == did){
764 list_del(&rrq->list);
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766 return rrq;
767 }
768 }
769 spin_unlock_irqrestore(&phba->hbalock, iflags);
770 return NULL;
771}
772
773/**
774 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
775 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500776 * @ndlp: Pointer to the lpfc_node_list structure.
777 * If ndlp is NULL Remove all active RRQs for this vport from the
778 * phba->active_rrq_list and clear the rrq.
779 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500780 **/
781void
James Smart1151e3e2011-02-16 12:39:35 -0500782lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500783
784{
785 struct lpfc_hba *phba = vport->phba;
786 struct lpfc_node_rrq *rrq;
787 struct lpfc_node_rrq *nextrrq;
788 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500789 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500790
791 if (phba->sli_rev != LPFC_SLI_REV4)
792 return;
James Smart1151e3e2011-02-16 12:39:35 -0500793 if (!ndlp) {
794 lpfc_sli4_vport_delete_els_xri_aborted(vport);
795 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500796 }
James Smart1151e3e2011-02-16 12:39:35 -0500797 spin_lock_irqsave(&phba->hbalock, iflags);
798 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
799 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
800 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500801 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500802
803 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
804 list_del(&rrq->list);
805 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
806 }
James Smart19ca7602010-11-20 23:11:55 -0500807}
808
809/**
James Smart1151e3e2011-02-16 12:39:35 -0500810 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500811 * @phba: Pointer to HBA context object.
812 * @ndlp: Targets nodelist pointer for this exchange.
813 * @xritag the xri in the bitmap to test.
814 *
815 * This function is called with hbalock held. This function
816 * returns 0 = rrq not active for this xri
817 * 1 = rrq is valid for this xri.
818 **/
James Smart1151e3e2011-02-16 12:39:35 -0500819int
820lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500821 uint16_t xritag)
822{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100823 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500824 if (!ndlp)
825 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500826 if (!ndlp->active_rrqs_xri_bitmap)
827 return 0;
828 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500829 return 1;
830 else
831 return 0;
832}
833
834/**
835 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
836 * @phba: Pointer to HBA context object.
837 * @ndlp: nodelist pointer for this target.
838 * @xritag: xri used in this exchange.
839 * @rxid: Remote Exchange ID.
840 * @send_rrq: Flag used to determine if we should send rrq els cmd.
841 *
842 * This function takes the hbalock.
843 * The active bit is always set in the active rrq xri_bitmap even
844 * if there is no slot avaiable for the other rrq information.
845 *
846 * returns 0 rrq actived for this xri
847 * < 0 No memory or invalid ndlp.
848 **/
849int
850lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500851 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500852{
James Smart19ca7602010-11-20 23:11:55 -0500853 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500854 struct lpfc_node_rrq *rrq;
855 int empty;
856
857 if (!ndlp)
858 return -EINVAL;
859
860 if (!phba->cfg_enable_rrq)
861 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500862
863 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500864 if (phba->pport->load_flag & FC_UNLOADING) {
865 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
866 goto out;
867 }
868
869 /*
870 * set the active bit even if there is no mem available.
871 */
872 if (NLP_CHK_FREE_REQ(ndlp))
873 goto out;
874
875 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
876 goto out;
877
James Smartcff261f2013-12-17 20:29:47 -0500878 if (!ndlp->active_rrqs_xri_bitmap)
879 goto out;
880
881 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500882 goto out;
883
James Smart19ca7602010-11-20 23:11:55 -0500884 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500885 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
886 if (!rrq) {
887 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
888 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
889 " DID:0x%x Send:%d\n",
890 xritag, rxid, ndlp->nlp_DID, send_rrq);
891 return -EINVAL;
892 }
James Smarte5771b42013-03-01 16:37:14 -0500893 if (phba->cfg_enable_rrq == 1)
894 rrq->send_rrq = send_rrq;
895 else
896 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500897 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400898 rrq->rrq_stop_time = jiffies +
899 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500900 rrq->ndlp = ndlp;
901 rrq->nlp_DID = ndlp->nlp_DID;
902 rrq->vport = ndlp->vport;
903 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500904 spin_lock_irqsave(&phba->hbalock, iflags);
905 empty = list_empty(&phba->active_rrq_list);
906 list_add_tail(&rrq->list, &phba->active_rrq_list);
907 phba->hba_flag |= HBA_RRQ_ACTIVE;
908 if (empty)
909 lpfc_worker_wake_up(phba);
910 spin_unlock_irqrestore(&phba->hbalock, iflags);
911 return 0;
912out:
913 spin_unlock_irqrestore(&phba->hbalock, iflags);
914 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
915 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
916 " DID:0x%x Send:%d\n",
917 xritag, rxid, ndlp->nlp_DID, send_rrq);
918 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500919}
920
921/**
James Smart895427b2017-02-12 13:52:30 -0800922 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400923 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500924 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400925 *
James Smartdafe8ce2014-09-03 12:56:40 -0400926 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400927 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400928 * list is not empty then it is successful, it returns pointer to the newly
929 * allocated sglq object else it returns NULL.
930 **/
931static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800932__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400933{
James Smart895427b2017-02-12 13:52:30 -0800934 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400935 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500936 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500937 struct lpfc_scsi_buf *lpfc_cmd;
938 struct lpfc_nodelist *ndlp;
939 int found = 0;
940
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100941 lockdep_assert_held(&phba->hbalock);
942
James Smart19ca7602010-11-20 23:11:55 -0500943 if (piocbq->iocb_flag & LPFC_IO_FCP) {
944 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
945 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500946 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400947 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500948 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400949 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
950 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
951 ndlp = NULL;
952 else
953 ndlp = piocbq->context_un.ndlp;
954 } else {
James Smart19ca7602010-11-20 23:11:55 -0500955 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400956 }
James Smart19ca7602010-11-20 23:11:55 -0500957
James Smart895427b2017-02-12 13:52:30 -0800958 spin_lock(&phba->sli4_hba.sgl_list_lock);
959 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500960 start_sglq = sglq;
961 while (!found) {
962 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -0800963 break;
James Smart895427b2017-02-12 13:52:30 -0800964 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
965 test_bit(sglq->sli4_lxritag,
966 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500967 /* This xri has an rrq outstanding for this DID.
968 * put it back in the list and get another xri.
969 */
James Smart895427b2017-02-12 13:52:30 -0800970 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500971 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800972 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500973 struct lpfc_sglq, list);
974 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -0700975 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500976 sglq = NULL;
977 break;
978 } else
979 continue;
980 }
981 sglq->ndlp = ndlp;
982 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400983 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500984 sglq->state = SGL_ALLOCATED;
985 }
James Smart895427b2017-02-12 13:52:30 -0800986 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400987 return sglq;
988}
989
990/**
James Smartf358dd02017-02-12 13:52:34 -0800991 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
992 * @phba: Pointer to HBA context object.
993 * @piocb: Pointer to the iocbq.
994 *
995 * This function is called with the sgl_list lock held. This function
996 * gets a new driver sglq object from the sglq list. If the
997 * list is not empty then it is successful, it returns pointer to the newly
998 * allocated sglq object else it returns NULL.
999 **/
1000struct lpfc_sglq *
1001__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1002{
1003 struct list_head *lpfc_nvmet_sgl_list;
1004 struct lpfc_sglq *sglq = NULL;
1005
1006 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1007
1008 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1009
1010 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1011 if (!sglq)
1012 return NULL;
1013 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1014 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001015 return sglq;
1016}
1017
James Smarte59058c2008-08-24 21:49:00 -04001018/**
James Smart3621a712009-04-06 18:47:14 -04001019 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001020 * @phba: Pointer to HBA context object.
1021 *
1022 * This function is called with no lock held. This function
1023 * allocates a new driver iocb object from the iocb pool. If the
1024 * allocation is successful, it returns pointer to the newly
1025 * allocated iocb object else it returns NULL.
1026 **/
James Smart2e0fef82007-06-17 19:56:36 -05001027struct lpfc_iocbq *
1028lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001029{
James Smart2e0fef82007-06-17 19:56:36 -05001030 struct lpfc_iocbq * iocbq = NULL;
1031 unsigned long iflags;
1032
1033 spin_lock_irqsave(&phba->hbalock, iflags);
1034 iocbq = __lpfc_sli_get_iocbq(phba);
1035 spin_unlock_irqrestore(&phba->hbalock, iflags);
1036 return iocbq;
1037}
1038
James Smarte59058c2008-08-24 21:49:00 -04001039/**
James Smart4f774512009-05-22 14:52:35 -04001040 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1041 * @phba: Pointer to HBA context object.
1042 * @iocbq: Pointer to driver iocb object.
1043 *
1044 * This function is called with hbalock held to release driver
1045 * iocb object to the iocb pool. The iotag in the iocb object
1046 * does not change for each use of the iocb object. This function
1047 * clears all other fields of the iocb object when it is freed.
1048 * The sqlq structure that holds the xritag and phys and virtual
1049 * mappings for the scatter gather list is retrieved from the
1050 * active array of sglq. The get of the sglq pointer also clears
1051 * the entry in the array. If the status of the IO indiactes that
1052 * this IO was aborted then the sglq entry it put on the
1053 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1054 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001055 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001056 **/
1057static void
1058__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1059{
1060 struct lpfc_sglq *sglq;
1061 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001062 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001063 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001064
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001065 lockdep_assert_held(&phba->hbalock);
1066
James Smart4f774512009-05-22 14:52:35 -04001067 if (iocbq->sli4_xritag == NO_XRI)
1068 sglq = NULL;
1069 else
James Smart6d368e52011-05-24 11:44:12 -04001070 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1071
James Smart0e9bb8d2013-03-01 16:35:12 -05001072
James Smart4f774512009-05-22 14:52:35 -04001073 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001074 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1075 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1076 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001077 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001078 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001079 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001080 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1081 spin_unlock_irqrestore(
1082 &phba->sli4_hba.sgl_list_lock, iflag);
1083 goto out;
1084 }
1085
James Smart895427b2017-02-12 13:52:30 -08001086 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001087 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1088 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001089 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1090 iflag);
James Smart341af102010-01-26 23:07:37 -05001091 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001092 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001093 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001094 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001095 } else {
James Smart895427b2017-02-12 13:52:30 -08001096 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1097 iflag);
James Smart4f774512009-05-22 14:52:35 -04001098 sglq->state = SGL_FREED;
1099 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001100 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001101 &phba->sli4_hba.lpfc_els_sgl_list);
1102 spin_unlock_irqrestore(
1103 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001104
1105 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001106 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001107 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001108 }
James Smart4f774512009-05-22 14:52:35 -04001109 }
1110
James Smartf358dd02017-02-12 13:52:34 -08001111out:
James Smart4f774512009-05-22 14:52:35 -04001112 /*
1113 * Clean all volatile data fields, preserve iotag and node struct.
1114 */
1115 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001116 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001117 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001118 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1119 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001120 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1121}
1122
James Smart2a9bf3d2010-06-07 15:24:45 -04001123
James Smart4f774512009-05-22 14:52:35 -04001124/**
James Smart3772a992009-05-22 14:50:54 -04001125 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1126 * @phba: Pointer to HBA context object.
1127 * @iocbq: Pointer to driver iocb object.
1128 *
1129 * This function is called with hbalock held to release driver
1130 * iocb object to the iocb pool. The iotag in the iocb object
1131 * does not change for each use of the iocb object. This function
1132 * clears all other fields of the iocb object when it is freed.
1133 **/
1134static void
1135__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1136{
1137 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1138
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001139 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001140
1141 /*
James Smart3772a992009-05-22 14:50:54 -04001142 * Clean all volatile data fields, preserve iotag and node struct.
1143 */
1144 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1145 iocbq->sli4_xritag = NO_XRI;
1146 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1147}
1148
1149/**
James Smart3621a712009-04-06 18:47:14 -04001150 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001151 * @phba: Pointer to HBA context object.
1152 * @iocbq: Pointer to driver iocb object.
1153 *
1154 * This function is called with hbalock held to release driver
1155 * iocb object to the iocb pool. The iotag in the iocb object
1156 * does not change for each use of the iocb object. This function
1157 * clears all other fields of the iocb object when it is freed.
1158 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001159static void
James Smart2e0fef82007-06-17 19:56:36 -05001160__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1161{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001162 lockdep_assert_held(&phba->hbalock);
1163
James Smart3772a992009-05-22 14:50:54 -04001164 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001165 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001166}
1167
James Smarte59058c2008-08-24 21:49:00 -04001168/**
James Smart3621a712009-04-06 18:47:14 -04001169 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001170 * @phba: Pointer to HBA context object.
1171 * @iocbq: Pointer to driver iocb object.
1172 *
1173 * This function is called with no lock held to release the iocb to
1174 * iocb pool.
1175 **/
James Smart2e0fef82007-06-17 19:56:36 -05001176void
1177lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1178{
1179 unsigned long iflags;
1180
1181 /*
1182 * Clean all volatile data fields, preserve iotag and node struct.
1183 */
1184 spin_lock_irqsave(&phba->hbalock, iflags);
1185 __lpfc_sli_release_iocbq(phba, iocbq);
1186 spin_unlock_irqrestore(&phba->hbalock, iflags);
1187}
1188
James Smarte59058c2008-08-24 21:49:00 -04001189/**
James Smarta257bf92009-04-06 18:48:10 -04001190 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1191 * @phba: Pointer to HBA context object.
1192 * @iocblist: List of IOCBs.
1193 * @ulpstatus: ULP status in IOCB command field.
1194 * @ulpWord4: ULP word-4 in IOCB command field.
1195 *
1196 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1197 * on the list by invoking the complete callback function associated with the
1198 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1199 * fields.
1200 **/
1201void
1202lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1203 uint32_t ulpstatus, uint32_t ulpWord4)
1204{
1205 struct lpfc_iocbq *piocb;
1206
1207 while (!list_empty(iocblist)) {
1208 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001209 if (!piocb->iocb_cmpl)
1210 lpfc_sli_release_iocbq(phba, piocb);
1211 else {
1212 piocb->iocb.ulpStatus = ulpstatus;
1213 piocb->iocb.un.ulpWord[4] = ulpWord4;
1214 (piocb->iocb_cmpl) (phba, piocb, piocb);
1215 }
1216 }
1217 return;
1218}
1219
1220/**
James Smart3621a712009-04-06 18:47:14 -04001221 * lpfc_sli_iocb_cmd_type - Get the iocb type
1222 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001223 *
1224 * This function is called by ring event handler function to get the iocb type.
1225 * This function translates the iocb command to an iocb command type used to
1226 * decide the final disposition of each completed IOCB.
1227 * The function returns
1228 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1229 * LPFC_SOL_IOCB if it is a solicited iocb completion
1230 * LPFC_ABORT_IOCB if it is an abort iocb
1231 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1232 *
1233 * The caller is not required to hold any lock.
1234 **/
dea31012005-04-17 16:05:31 -05001235static lpfc_iocb_type
1236lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1237{
1238 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1239
1240 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1241 return 0;
1242
1243 switch (iocb_cmnd) {
1244 case CMD_XMIT_SEQUENCE_CR:
1245 case CMD_XMIT_SEQUENCE_CX:
1246 case CMD_XMIT_BCAST_CN:
1247 case CMD_XMIT_BCAST_CX:
1248 case CMD_ELS_REQUEST_CR:
1249 case CMD_ELS_REQUEST_CX:
1250 case CMD_CREATE_XRI_CR:
1251 case CMD_CREATE_XRI_CX:
1252 case CMD_GET_RPI_CN:
1253 case CMD_XMIT_ELS_RSP_CX:
1254 case CMD_GET_RPI_CR:
1255 case CMD_FCP_IWRITE_CR:
1256 case CMD_FCP_IWRITE_CX:
1257 case CMD_FCP_IREAD_CR:
1258 case CMD_FCP_IREAD_CX:
1259 case CMD_FCP_ICMND_CR:
1260 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001261 case CMD_FCP_TSEND_CX:
1262 case CMD_FCP_TRSP_CX:
1263 case CMD_FCP_TRECEIVE_CX:
1264 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001265 case CMD_ADAPTER_MSG:
1266 case CMD_ADAPTER_DUMP:
1267 case CMD_XMIT_SEQUENCE64_CR:
1268 case CMD_XMIT_SEQUENCE64_CX:
1269 case CMD_XMIT_BCAST64_CN:
1270 case CMD_XMIT_BCAST64_CX:
1271 case CMD_ELS_REQUEST64_CR:
1272 case CMD_ELS_REQUEST64_CX:
1273 case CMD_FCP_IWRITE64_CR:
1274 case CMD_FCP_IWRITE64_CX:
1275 case CMD_FCP_IREAD64_CR:
1276 case CMD_FCP_IREAD64_CX:
1277 case CMD_FCP_ICMND64_CR:
1278 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001279 case CMD_FCP_TSEND64_CX:
1280 case CMD_FCP_TRSP64_CX:
1281 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001282 case CMD_GEN_REQUEST64_CR:
1283 case CMD_GEN_REQUEST64_CX:
1284 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001285 case DSSCMD_IWRITE64_CR:
1286 case DSSCMD_IWRITE64_CX:
1287 case DSSCMD_IREAD64_CR:
1288 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001289 type = LPFC_SOL_IOCB;
1290 break;
1291 case CMD_ABORT_XRI_CN:
1292 case CMD_ABORT_XRI_CX:
1293 case CMD_CLOSE_XRI_CN:
1294 case CMD_CLOSE_XRI_CX:
1295 case CMD_XRI_ABORTED_CX:
1296 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001297 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001298 type = LPFC_ABORT_IOCB;
1299 break;
1300 case CMD_RCV_SEQUENCE_CX:
1301 case CMD_RCV_ELS_REQ_CX:
1302 case CMD_RCV_SEQUENCE64_CX:
1303 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001304 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001305 case CMD_IOCB_RCV_SEQ64_CX:
1306 case CMD_IOCB_RCV_ELS64_CX:
1307 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001308 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001309 type = LPFC_UNSOL_IOCB;
1310 break;
James Smart3163f722008-02-08 18:50:25 -05001311 case CMD_IOCB_XMIT_MSEQ64_CR:
1312 case CMD_IOCB_XMIT_MSEQ64_CX:
1313 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1314 case CMD_IOCB_RCV_ELS_LIST64_CX:
1315 case CMD_IOCB_CLOSE_EXTENDED_CN:
1316 case CMD_IOCB_ABORT_EXTENDED_CN:
1317 case CMD_IOCB_RET_HBQE64_CN:
1318 case CMD_IOCB_FCP_IBIDIR64_CR:
1319 case CMD_IOCB_FCP_IBIDIR64_CX:
1320 case CMD_IOCB_FCP_ITASKMGT64_CX:
1321 case CMD_IOCB_LOGENTRY_CN:
1322 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1323 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001324 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001325 type = LPFC_UNKNOWN_IOCB;
1326 break;
dea31012005-04-17 16:05:31 -05001327 default:
1328 type = LPFC_UNKNOWN_IOCB;
1329 break;
1330 }
1331
1332 return type;
1333}
1334
James Smarte59058c2008-08-24 21:49:00 -04001335/**
James Smart3621a712009-04-06 18:47:14 -04001336 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001337 * @phba: Pointer to HBA context object.
1338 *
1339 * This function is called from SLI initialization code
1340 * to configure every ring of the HBA's SLI interface. The
1341 * caller is not required to hold any lock. This function issues
1342 * a config_ring mailbox command for each ring.
1343 * This function returns zero if successful else returns a negative
1344 * error code.
1345 **/
dea31012005-04-17 16:05:31 -05001346static int
James Smarted957682007-06-17 19:56:37 -05001347lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001348{
1349 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001350 LPFC_MBOXQ_t *pmb;
1351 MAILBOX_t *pmbox;
1352 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001353
James Smarted957682007-06-17 19:56:37 -05001354 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1355 if (!pmb)
1356 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001357 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001358 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001359 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001360 lpfc_config_ring(phba, i, pmb);
1361 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1362 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001364 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001365 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1366 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001367 rc, pmbox->mbxCommand,
1368 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001369 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001370 ret = -ENXIO;
1371 break;
dea31012005-04-17 16:05:31 -05001372 }
1373 }
James Smarted957682007-06-17 19:56:37 -05001374 mempool_free(pmb, phba->mbox_mem_pool);
1375 return ret;
dea31012005-04-17 16:05:31 -05001376}
1377
James Smarte59058c2008-08-24 21:49:00 -04001378/**
James Smart3621a712009-04-06 18:47:14 -04001379 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001380 * @phba: Pointer to HBA context object.
1381 * @pring: Pointer to driver SLI ring object.
1382 * @piocb: Pointer to the driver iocb object.
1383 *
1384 * This function is called with hbalock held. The function adds the
1385 * new iocb to txcmplq of the given ring. This function always returns
1386 * 0. If this function is called for ELS ring, this function checks if
1387 * there is a vport associated with the ELS command. This function also
1388 * starts els_tmofunc timer if this is an ELS command.
1389 **/
dea31012005-04-17 16:05:31 -05001390static int
James Smart2e0fef82007-06-17 19:56:36 -05001391lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1392 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001393{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001394 lockdep_assert_held(&phba->hbalock);
1395
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001396 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001397
dea31012005-04-17 16:05:31 -05001398 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001399 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001400
James Smart92d7f7b2007-06-17 19:56:38 -05001401 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1402 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001403 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1404 BUG_ON(!piocb->vport);
1405 if (!(piocb->vport->load_flag & FC_UNLOADING))
1406 mod_timer(&piocb->vport->els_tmofunc,
1407 jiffies +
1408 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1409 }
dea31012005-04-17 16:05:31 -05001410
James Smart2e0fef82007-06-17 19:56:36 -05001411 return 0;
dea31012005-04-17 16:05:31 -05001412}
1413
James Smarte59058c2008-08-24 21:49:00 -04001414/**
James Smart3621a712009-04-06 18:47:14 -04001415 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001416 * @phba: Pointer to HBA context object.
1417 * @pring: Pointer to driver SLI ring object.
1418 *
1419 * This function is called with hbalock held to get next
1420 * iocb in txq of the given ring. If there is any iocb in
1421 * the txq, the function returns first iocb in the list after
1422 * removing the iocb from the list, else it returns NULL.
1423 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001424struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001425lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001426{
dea31012005-04-17 16:05:31 -05001427 struct lpfc_iocbq *cmd_iocb;
1428
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001429 lockdep_assert_held(&phba->hbalock);
1430
James Smart858c9f62007-06-17 19:56:39 -05001431 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001432 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001433}
1434
James Smarte59058c2008-08-24 21:49:00 -04001435/**
James Smart3621a712009-04-06 18:47:14 -04001436 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001437 * @phba: Pointer to HBA context object.
1438 * @pring: Pointer to driver SLI ring object.
1439 *
1440 * This function is called with hbalock held and the caller must post the
1441 * iocb without releasing the lock. If the caller releases the lock,
1442 * iocb slot returned by the function is not guaranteed to be available.
1443 * The function returns pointer to the next available iocb slot if there
1444 * is available slot in the ring, else it returns NULL.
1445 * If the get index of the ring is ahead of the put index, the function
1446 * will post an error attention event to the worker thread to take the
1447 * HBA to offline state.
1448 **/
dea31012005-04-17 16:05:31 -05001449static IOCB_t *
1450lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1451{
James Smart34b02dc2008-08-24 21:49:55 -04001452 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001453 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001454
1455 lockdep_assert_held(&phba->hbalock);
1456
James Smart7e56aa22012-08-03 12:35:34 -04001457 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1458 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1459 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001460
James Smart7e56aa22012-08-03 12:35:34 -04001461 if (unlikely(pring->sli.sli3.local_getidx ==
1462 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001463
James Smart7e56aa22012-08-03 12:35:34 -04001464 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001465
James Smart7e56aa22012-08-03 12:35:34 -04001466 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001467 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001468 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001469 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001470 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001471 pring->sli.sli3.local_getidx,
1472 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001473
James Smart2e0fef82007-06-17 19:56:36 -05001474 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001475 /*
1476 * All error attention handlers are posted to
1477 * worker thread
1478 */
1479 phba->work_ha |= HA_ERATT;
1480 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001481
James Smart5e9d9b82008-06-14 22:52:53 -04001482 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001483
1484 return NULL;
1485 }
1486
James Smart7e56aa22012-08-03 12:35:34 -04001487 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001488 return NULL;
1489 }
1490
James Smarted957682007-06-17 19:56:37 -05001491 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001492}
1493
James Smarte59058c2008-08-24 21:49:00 -04001494/**
James Smart3621a712009-04-06 18:47:14 -04001495 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001496 * @phba: Pointer to HBA context object.
1497 * @iocbq: Pointer to driver iocb object.
1498 *
1499 * This function gets an iotag for the iocb. If there is no unused iotag and
1500 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1501 * array and assigns a new iotag.
1502 * The function returns the allocated iotag if successful, else returns zero.
1503 * Zero is not a valid iotag.
1504 * The caller is not required to hold any lock.
1505 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001506uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001507lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001508{
James Smart2e0fef82007-06-17 19:56:36 -05001509 struct lpfc_iocbq **new_arr;
1510 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001511 size_t new_len;
1512 struct lpfc_sli *psli = &phba->sli;
1513 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001514
James Smart2e0fef82007-06-17 19:56:36 -05001515 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001516 iotag = psli->last_iotag;
1517 if(++iotag < psli->iocbq_lookup_len) {
1518 psli->last_iotag = iotag;
1519 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001520 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001521 iocbq->iotag = iotag;
1522 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001523 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001524 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1525 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001526 spin_unlock_irq(&phba->hbalock);
1527 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001528 GFP_KERNEL);
1529 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001530 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001531 old_arr = psli->iocbq_lookup;
1532 if (new_len <= psli->iocbq_lookup_len) {
1533 /* highly unprobable case */
1534 kfree(new_arr);
1535 iotag = psli->last_iotag;
1536 if(++iotag < psli->iocbq_lookup_len) {
1537 psli->last_iotag = iotag;
1538 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001539 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001540 iocbq->iotag = iotag;
1541 return iotag;
1542 }
James Smart2e0fef82007-06-17 19:56:36 -05001543 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001544 return 0;
1545 }
1546 if (psli->iocbq_lookup)
1547 memcpy(new_arr, old_arr,
1548 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001549 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001550 psli->iocbq_lookup = new_arr;
1551 psli->iocbq_lookup_len = new_len;
1552 psli->last_iotag = iotag;
1553 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001554 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001555 iocbq->iotag = iotag;
1556 kfree(old_arr);
1557 return iotag;
1558 }
James Smart8f6d98d2006-08-01 07:34:00 -04001559 } else
James Smart2e0fef82007-06-17 19:56:36 -05001560 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001561
James Smartbc739052010-08-04 16:11:18 -04001562 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001563 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1564 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001565
James Bottomley604a3e32005-10-29 10:28:33 -05001566 return 0;
dea31012005-04-17 16:05:31 -05001567}
1568
James Smarte59058c2008-08-24 21:49:00 -04001569/**
James Smart3621a712009-04-06 18:47:14 -04001570 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001571 * @phba: Pointer to HBA context object.
1572 * @pring: Pointer to driver SLI ring object.
1573 * @iocb: Pointer to iocb slot in the ring.
1574 * @nextiocb: Pointer to driver iocb object which need to be
1575 * posted to firmware.
1576 *
1577 * This function is called with hbalock held to post a new iocb to
1578 * the firmware. This function copies the new iocb to ring iocb slot and
1579 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1580 * a completion call back for this iocb else the function will free the
1581 * iocb object.
1582 **/
dea31012005-04-17 16:05:31 -05001583static void
1584lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1585 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1586{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001587 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001588 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001589 * Set up an iotag
dea31012005-04-17 16:05:31 -05001590 */
James Bottomley604a3e32005-10-29 10:28:33 -05001591 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001592
James Smarte2a0a9d2008-12-04 22:40:02 -05001593
James Smarta58cbd52007-08-02 11:09:43 -04001594 if (pring->ringno == LPFC_ELS_RING) {
1595 lpfc_debugfs_slow_ring_trc(phba,
1596 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1597 *(((uint32_t *) &nextiocb->iocb) + 4),
1598 *(((uint32_t *) &nextiocb->iocb) + 6),
1599 *(((uint32_t *) &nextiocb->iocb) + 7));
1600 }
1601
dea31012005-04-17 16:05:31 -05001602 /*
1603 * Issue iocb command to adapter
1604 */
James Smart92d7f7b2007-06-17 19:56:38 -05001605 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001606 wmb();
1607 pring->stats.iocb_cmd++;
1608
1609 /*
1610 * If there is no completion routine to call, we can release the
1611 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1612 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1613 */
1614 if (nextiocb->iocb_cmpl)
1615 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001616 else
James Smart2e0fef82007-06-17 19:56:36 -05001617 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001618
1619 /*
1620 * Let the HBA know what IOCB slot will be the next one the
1621 * driver will put a command into.
1622 */
James Smart7e56aa22012-08-03 12:35:34 -04001623 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1624 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001625}
1626
James Smarte59058c2008-08-24 21:49:00 -04001627/**
James Smart3621a712009-04-06 18:47:14 -04001628 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001629 * @phba: Pointer to HBA context object.
1630 * @pring: Pointer to driver SLI ring object.
1631 *
1632 * The caller is not required to hold any lock for calling this function.
1633 * This function updates the chip attention bits for the ring to inform firmware
1634 * that there are pending work to be done for this ring and requests an
1635 * interrupt when there is space available in the ring. This function is
1636 * called when the driver is unable to post more iocbs to the ring due
1637 * to unavailability of space in the ring.
1638 **/
dea31012005-04-17 16:05:31 -05001639static void
James Smart2e0fef82007-06-17 19:56:36 -05001640lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001641{
1642 int ringno = pring->ringno;
1643
1644 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1645
1646 wmb();
1647
1648 /*
1649 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1650 * The HBA will tell us when an IOCB entry is available.
1651 */
1652 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1653 readl(phba->CAregaddr); /* flush */
1654
1655 pring->stats.iocb_cmd_full++;
1656}
1657
James Smarte59058c2008-08-24 21:49:00 -04001658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: Pointer to HBA context object.
1661 * @pring: Pointer to driver SLI ring object.
1662 *
1663 * This function updates the chip attention register bit for the
1664 * given ring to inform HBA that there is more work to be done
1665 * in this ring. The caller is not required to hold any lock.
1666 **/
dea31012005-04-17 16:05:31 -05001667static void
James Smart2e0fef82007-06-17 19:56:36 -05001668lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001669{
1670 int ringno = pring->ringno;
1671
1672 /*
1673 * Tell the HBA that there is work to do in this ring.
1674 */
James Smart34b02dc2008-08-24 21:49:55 -04001675 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1676 wmb();
1677 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1678 readl(phba->CAregaddr); /* flush */
1679 }
dea31012005-04-17 16:05:31 -05001680}
1681
James Smarte59058c2008-08-24 21:49:00 -04001682/**
James Smart3621a712009-04-06 18:47:14 -04001683 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001684 * @phba: Pointer to HBA context object.
1685 * @pring: Pointer to driver SLI ring object.
1686 *
1687 * This function is called with hbalock held to post pending iocbs
1688 * in the txq to the firmware. This function is called when driver
1689 * detects space available in the ring.
1690 **/
dea31012005-04-17 16:05:31 -05001691static void
James Smart2e0fef82007-06-17 19:56:36 -05001692lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001693{
1694 IOCB_t *iocb;
1695 struct lpfc_iocbq *nextiocb;
1696
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001697 lockdep_assert_held(&phba->hbalock);
1698
dea31012005-04-17 16:05:31 -05001699 /*
1700 * Check to see if:
1701 * (a) there is anything on the txq to send
1702 * (b) link is up
1703 * (c) link attention events can be processed (fcp ring only)
1704 * (d) IOCB processing is not blocked by the outstanding mbox command.
1705 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001706
1707 if (lpfc_is_link_up(phba) &&
1708 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001709 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001710 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001711
1712 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1713 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1714 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1715
1716 if (iocb)
1717 lpfc_sli_update_ring(phba, pring);
1718 else
1719 lpfc_sli_update_full_ring(phba, pring);
1720 }
1721
1722 return;
1723}
1724
James Smarte59058c2008-08-24 21:49:00 -04001725/**
James Smart3621a712009-04-06 18:47:14 -04001726 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001727 * @phba: Pointer to HBA context object.
1728 * @hbqno: HBQ number.
1729 *
1730 * This function is called with hbalock held to get the next
1731 * available slot for the given HBQ. If there is free slot
1732 * available for the HBQ it will return pointer to the next available
1733 * HBQ entry else it will return NULL.
1734 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001735static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001736lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1737{
1738 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1739
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001740 lockdep_assert_held(&phba->hbalock);
1741
James Smarted957682007-06-17 19:56:37 -05001742 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1743 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1744 hbqp->next_hbqPutIdx = 0;
1745
1746 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001747 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001748 uint32_t getidx = le32_to_cpu(raw_index);
1749
1750 hbqp->local_hbqGetIdx = getidx;
1751
1752 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1753 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001754 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001755 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001756 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001757 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001758 hbqp->entry_count);
1759
1760 phba->link_state = LPFC_HBA_ERROR;
1761 return NULL;
1762 }
1763
1764 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1765 return NULL;
1766 }
1767
James Smart51ef4c22007-08-02 11:10:31 -04001768 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1769 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001770}
1771
James Smarte59058c2008-08-24 21:49:00 -04001772/**
James Smart3621a712009-04-06 18:47:14 -04001773 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001774 * @phba: Pointer to HBA context object.
1775 *
1776 * This function is called with no lock held to free all the
1777 * hbq buffers while uninitializing the SLI interface. It also
1778 * frees the HBQ buffers returned by the firmware but not yet
1779 * processed by the upper layers.
1780 **/
James Smarted957682007-06-17 19:56:37 -05001781void
1782lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1783{
James Smart92d7f7b2007-06-17 19:56:38 -05001784 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1785 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001786 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001787 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001788
James Smart51ef4c22007-08-02 11:10:31 -04001789 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001790 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001791 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001792 for (i = 0; i < hbq_count; ++i) {
1793 list_for_each_entry_safe(dmabuf, next_dmabuf,
1794 &phba->hbqs[i].hbq_buffer_list, list) {
1795 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1796 list_del(&hbq_buf->dbuf.list);
1797 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1798 }
James Smarta8adb832007-10-27 13:37:53 -04001799 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001800 }
James Smart3163f722008-02-08 18:50:25 -05001801
1802 /* Mark the HBQs not in use */
1803 phba->hbq_in_use = 0;
1804 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001805}
1806
James Smarte59058c2008-08-24 21:49:00 -04001807/**
James Smart3621a712009-04-06 18:47:14 -04001808 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001809 * @phba: Pointer to HBA context object.
1810 * @hbqno: HBQ number.
1811 * @hbq_buf: Pointer to HBQ buffer.
1812 *
1813 * This function is called with the hbalock held to post a
1814 * hbq buffer to the firmware. If the function finds an empty
1815 * slot in the HBQ, it will post the buffer. The function will return
1816 * pointer to the hbq entry if it successfully post the buffer
1817 * else it will return NULL.
1818 **/
James Smart3772a992009-05-22 14:50:54 -04001819static int
James Smarted957682007-06-17 19:56:37 -05001820lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001821 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001822{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001823 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001824 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1825}
1826
1827/**
1828 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1829 * @phba: Pointer to HBA context object.
1830 * @hbqno: HBQ number.
1831 * @hbq_buf: Pointer to HBQ buffer.
1832 *
1833 * This function is called with the hbalock held to post a hbq buffer to the
1834 * firmware. If the function finds an empty slot in the HBQ, it will post the
1835 * buffer and place it on the hbq_buffer_list. The function will return zero if
1836 * it successfully post the buffer else it will return an error.
1837 **/
1838static int
1839lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1840 struct hbq_dmabuf *hbq_buf)
1841{
James Smarted957682007-06-17 19:56:37 -05001842 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001843 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001844
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001845 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001846 /* Get next HBQ entry slot to use */
1847 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1848 if (hbqe) {
1849 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1850
James Smart92d7f7b2007-06-17 19:56:38 -05001851 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1852 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001853 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001854 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001855 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1856 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1857 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001858 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1859 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001860 /* flush */
James Smarted957682007-06-17 19:56:37 -05001861 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001862 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001863 return 0;
1864 } else
1865 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001866}
1867
James Smart4f774512009-05-22 14:52:35 -04001868/**
1869 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1870 * @phba: Pointer to HBA context object.
1871 * @hbqno: HBQ number.
1872 * @hbq_buf: Pointer to HBQ buffer.
1873 *
1874 * This function is called with the hbalock held to post an RQE to the SLI4
1875 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1876 * the hbq_buffer_list and return zero, otherwise it will return an error.
1877 **/
1878static int
1879lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1880 struct hbq_dmabuf *hbq_buf)
1881{
1882 int rc;
1883 struct lpfc_rqe hrqe;
1884 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001885 struct lpfc_queue *hrq;
1886 struct lpfc_queue *drq;
1887
1888 if (hbqno != LPFC_ELS_HBQ)
1889 return 1;
1890 hrq = phba->sli4_hba.hdr_rq;
1891 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001892
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001893 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001894 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1895 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1896 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1897 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001898 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001899 if (rc < 0)
1900 return rc;
James Smart895427b2017-02-12 13:52:30 -08001901 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001902 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1903 return 0;
1904}
1905
James Smarte59058c2008-08-24 21:49:00 -04001906/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001907static struct lpfc_hbq_init lpfc_els_hbq = {
1908 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001909 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001910 .mask_count = 0,
1911 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001912 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001913 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001914 .init_count = 40,
1915 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001916};
James Smarted957682007-06-17 19:56:37 -05001917
James Smarte59058c2008-08-24 21:49:00 -04001918/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001919struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001920 &lpfc_els_hbq,
1921};
1922
James Smarte59058c2008-08-24 21:49:00 -04001923/**
James Smart3621a712009-04-06 18:47:14 -04001924 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001925 * @phba: Pointer to HBA context object.
1926 * @hbqno: HBQ number.
1927 * @count: Number of HBQ buffers to be posted.
1928 *
James Smartd7c255b2008-08-24 21:50:00 -04001929 * This function is called with no lock held to post more hbq buffers to the
1930 * given HBQ. The function returns the number of HBQ buffers successfully
1931 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001932 **/
James Smart311464e2007-08-02 11:10:37 -04001933static int
James Smart92d7f7b2007-06-17 19:56:38 -05001934lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1935{
James Smartd7c255b2008-08-24 21:50:00 -04001936 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001937 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001938 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001939 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001940 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001941 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001942
James Smartd7c255b2008-08-24 21:50:00 -04001943 if ((phba->hbqs[hbqno].buffer_count + count) >
1944 lpfc_hbq_defs[hbqno]->entry_count)
1945 count = lpfc_hbq_defs[hbqno]->entry_count -
1946 phba->hbqs[hbqno].buffer_count;
1947 if (!count)
1948 return 0;
1949 /* Allocate HBQ entries */
1950 for (i = 0; i < count; i++) {
1951 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1952 if (!hbq_buffer)
1953 break;
1954 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1955 }
James Smart3163f722008-02-08 18:50:25 -05001956 /* Check whether HBQ is still in use */
1957 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001958 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001959 goto err;
1960 while (!list_empty(&hbq_buf_list)) {
1961 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1962 dbuf.list);
1963 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1964 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001965 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001966 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001967 posted++;
1968 } else
James Smart51ef4c22007-08-02 11:10:31 -04001969 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001970 }
James Smart3163f722008-02-08 18:50:25 -05001971 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001972 return posted;
1973err:
1974 spin_unlock_irqrestore(&phba->hbalock, flags);
1975 while (!list_empty(&hbq_buf_list)) {
1976 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1977 dbuf.list);
1978 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1979 }
James Smart92d7f7b2007-06-17 19:56:38 -05001980 return 0;
James Smarted957682007-06-17 19:56:37 -05001981}
1982
James Smarte59058c2008-08-24 21:49:00 -04001983/**
James Smart3621a712009-04-06 18:47:14 -04001984 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001985 * @phba: Pointer to HBA context object.
1986 * @qno: HBQ number.
1987 *
1988 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001989 * is called with no lock held. The function returns the number of HBQ entries
1990 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001991 **/
James Smarted957682007-06-17 19:56:37 -05001992int
James Smart92d7f7b2007-06-17 19:56:38 -05001993lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001994{
James Smartdef9c7a2009-12-21 17:02:28 -05001995 if (phba->sli_rev == LPFC_SLI_REV4)
1996 return 0;
1997 else
1998 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1999 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002000}
2001
James Smarte59058c2008-08-24 21:49:00 -04002002/**
James Smart3621a712009-04-06 18:47:14 -04002003 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002004 * @phba: Pointer to HBA context object.
2005 * @qno: HBQ queue number.
2006 *
2007 * This function is called from SLI initialization code path with
2008 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002009 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002010 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002011static int
James Smart92d7f7b2007-06-17 19:56:38 -05002012lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002013{
James Smartdef9c7a2009-12-21 17:02:28 -05002014 if (phba->sli_rev == LPFC_SLI_REV4)
2015 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002016 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002017 else
2018 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2019 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002020}
2021
James Smarte59058c2008-08-24 21:49:00 -04002022/**
James Smart3772a992009-05-22 14:50:54 -04002023 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2024 * @phba: Pointer to HBA context object.
2025 * @hbqno: HBQ number.
2026 *
2027 * This function removes the first hbq buffer on an hbq list and returns a
2028 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2029 **/
2030static struct hbq_dmabuf *
2031lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2032{
2033 struct lpfc_dmabuf *d_buf;
2034
2035 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2036 if (!d_buf)
2037 return NULL;
2038 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2039}
2040
2041/**
James Smart2d7dbc42017-02-12 13:52:35 -08002042 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2043 * @phba: Pointer to HBA context object.
2044 * @hbqno: HBQ number.
2045 *
2046 * This function removes the first RQ buffer on an RQ buffer list and returns a
2047 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2048 **/
2049static struct rqb_dmabuf *
2050lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2051{
2052 struct lpfc_dmabuf *h_buf;
2053 struct lpfc_rqb *rqbp;
2054
2055 rqbp = hrq->rqbp;
2056 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2057 struct lpfc_dmabuf, list);
2058 if (!h_buf)
2059 return NULL;
2060 rqbp->buffer_count--;
2061 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2062}
2063
2064/**
James Smart3621a712009-04-06 18:47:14 -04002065 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002066 * @phba: Pointer to HBA context object.
2067 * @tag: Tag of the hbq buffer.
2068 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002069 * This function searches for the hbq buffer associated with the given tag in
2070 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2071 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002072 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002073static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002074lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2075{
James Smart92d7f7b2007-06-17 19:56:38 -05002076 struct lpfc_dmabuf *d_buf;
2077 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002078 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002079
James Smart51ef4c22007-08-02 11:10:31 -04002080 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002081 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002082 return NULL;
2083
James Smart3772a992009-05-22 14:50:54 -04002084 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002085 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002086 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002087 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002088 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002089 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002090 }
2091 }
James Smart3772a992009-05-22 14:50:54 -04002092 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002093 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002094 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002095 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002096 return NULL;
James Smarted957682007-06-17 19:56:37 -05002097}
2098
James Smarte59058c2008-08-24 21:49:00 -04002099/**
James Smart3621a712009-04-06 18:47:14 -04002100 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002101 * @phba: Pointer to HBA context object.
2102 * @hbq_buffer: Pointer to HBQ buffer.
2103 *
2104 * This function is called with hbalock. This function gives back
2105 * the hbq buffer to firmware. If the HBQ does not have space to
2106 * post the buffer, it will free the buffer.
2107 **/
James Smarted957682007-06-17 19:56:37 -05002108void
James Smart51ef4c22007-08-02 11:10:31 -04002109lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002110{
2111 uint32_t hbqno;
2112
James Smart51ef4c22007-08-02 11:10:31 -04002113 if (hbq_buffer) {
2114 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002115 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002116 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002117 }
2118}
2119
James Smarte59058c2008-08-24 21:49:00 -04002120/**
James Smart3621a712009-04-06 18:47:14 -04002121 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002122 * @mbxCommand: mailbox command code.
2123 *
2124 * This function is called by the mailbox event handler function to verify
2125 * that the completed mailbox command is a legitimate mailbox command. If the
2126 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2127 * and the mailbox event handler will take the HBA offline.
2128 **/
dea31012005-04-17 16:05:31 -05002129static int
2130lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2131{
2132 uint8_t ret;
2133
2134 switch (mbxCommand) {
2135 case MBX_LOAD_SM:
2136 case MBX_READ_NV:
2137 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002138 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002139 case MBX_RUN_BIU_DIAG:
2140 case MBX_INIT_LINK:
2141 case MBX_DOWN_LINK:
2142 case MBX_CONFIG_LINK:
2143 case MBX_CONFIG_RING:
2144 case MBX_RESET_RING:
2145 case MBX_READ_CONFIG:
2146 case MBX_READ_RCONFIG:
2147 case MBX_READ_SPARM:
2148 case MBX_READ_STATUS:
2149 case MBX_READ_RPI:
2150 case MBX_READ_XRI:
2151 case MBX_READ_REV:
2152 case MBX_READ_LNK_STAT:
2153 case MBX_REG_LOGIN:
2154 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002155 case MBX_CLEAR_LA:
2156 case MBX_DUMP_MEMORY:
2157 case MBX_DUMP_CONTEXT:
2158 case MBX_RUN_DIAGS:
2159 case MBX_RESTART:
2160 case MBX_UPDATE_CFG:
2161 case MBX_DOWN_LOAD:
2162 case MBX_DEL_LD_ENTRY:
2163 case MBX_RUN_PROGRAM:
2164 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002165 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002166 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002167 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002168 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002169 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002170 case MBX_LOAD_AREA:
2171 case MBX_RUN_BIU_DIAG64:
2172 case MBX_CONFIG_PORT:
2173 case MBX_READ_SPARM64:
2174 case MBX_READ_RPI64:
2175 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002176 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002177 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002178 case MBX_SET_DEBUG:
2179 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002180 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002181 case MBX_REG_VPI:
2182 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002183 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002184 case MBX_PORT_CAPABILITIES:
2185 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002186 case MBX_SLI4_CONFIG:
2187 case MBX_SLI4_REQ_FTRS:
2188 case MBX_REG_FCFI:
2189 case MBX_UNREG_FCFI:
2190 case MBX_REG_VFI:
2191 case MBX_UNREG_VFI:
2192 case MBX_INIT_VPI:
2193 case MBX_INIT_VFI:
2194 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002195 case MBX_READ_EVENT_LOG_STATUS:
2196 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002197 case MBX_SECURITY_MGMT:
2198 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002199 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002200 ret = mbxCommand;
2201 break;
2202 default:
2203 ret = MBX_SHUTDOWN;
2204 break;
2205 }
James Smart2e0fef82007-06-17 19:56:36 -05002206 return ret;
dea31012005-04-17 16:05:31 -05002207}
James Smarte59058c2008-08-24 21:49:00 -04002208
2209/**
James Smart3621a712009-04-06 18:47:14 -04002210 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002211 * @phba: Pointer to HBA context object.
2212 * @pmboxq: Pointer to mailbox command.
2213 *
2214 * This is completion handler function for mailbox commands issued from
2215 * lpfc_sli_issue_mbox_wait function. This function is called by the
2216 * mailbox event handler function with no lock held. This function
2217 * will wake up thread waiting on the wait queue pointed by context1
2218 * of the mailbox.
2219 **/
James Smart04c68492009-05-22 14:52:52 -04002220void
James Smart2e0fef82007-06-17 19:56:36 -05002221lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002222{
2223 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002224 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002225
2226 /*
2227 * If pdone_q is empty, the driver thread gave up waiting and
2228 * continued running.
2229 */
James Smart7054a602007-04-25 09:52:34 -04002230 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002231 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002232 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2233 if (pdone_q)
2234 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002235 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002236 return;
2237}
2238
James Smarte59058c2008-08-24 21:49:00 -04002239
2240/**
James Smart3621a712009-04-06 18:47:14 -04002241 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002242 * @phba: Pointer to HBA context object.
2243 * @pmb: Pointer to mailbox object.
2244 *
2245 * This function is the default mailbox completion handler. It
2246 * frees the memory resources associated with the completed mailbox
2247 * command. If the completed command is a REG_LOGIN mailbox command,
2248 * this function will issue a UREG_LOGIN to re-claim the RPI.
2249 **/
dea31012005-04-17 16:05:31 -05002250void
James Smart2e0fef82007-06-17 19:56:36 -05002251lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002252{
James Smartd439d282010-09-29 11:18:45 -04002253 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002254 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002255 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002256 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002257 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002258 int rc;
2259
dea31012005-04-17 16:05:31 -05002260 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002261
dea31012005-04-17 16:05:31 -05002262 if (mp) {
2263 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2264 kfree(mp);
2265 }
James Smart7054a602007-04-25 09:52:34 -04002266
2267 /*
2268 * If a REG_LOGIN succeeded after node is destroyed or node
2269 * is in re-discovery driver need to cleanup the RPI.
2270 */
James Smart2e0fef82007-06-17 19:56:36 -05002271 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002272 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2273 !pmb->u.mb.mbxStatus) {
2274 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002275 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002276 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002277 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002278 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002279 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2280 if (rc != MBX_NOT_FINISHED)
2281 return;
2282 }
2283
James Smart695a8142010-01-26 23:08:03 -05002284 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2285 !(phba->pport->load_flag & FC_UNLOADING) &&
2286 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002287 shost = lpfc_shost_from_vport(vport);
2288 spin_lock_irq(shost->host_lock);
2289 vport->vpi_state |= LPFC_VPI_REGISTERED;
2290 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2291 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002292 }
2293
James Smartd439d282010-09-29 11:18:45 -04002294 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2295 ndlp = (struct lpfc_nodelist *)pmb->context2;
2296 lpfc_nlp_put(ndlp);
2297 pmb->context2 = NULL;
2298 }
2299
James Smartdcf2a4e2010-09-29 11:18:53 -04002300 /* Check security permission status on INIT_LINK mailbox command */
2301 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2302 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2304 "2860 SLI authentication is required "
2305 "for INIT_LINK but has not done yet\n");
2306
James Smart04c68492009-05-22 14:52:52 -04002307 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2308 lpfc_sli4_mbox_cmd_free(phba, pmb);
2309 else
2310 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002311}
James Smartbe6bb942015-04-07 15:07:22 -04002312 /**
2313 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2314 * @phba: Pointer to HBA context object.
2315 * @pmb: Pointer to mailbox object.
2316 *
2317 * This function is the unreg rpi mailbox completion handler. It
2318 * frees the memory resources associated with the completed mailbox
2319 * command. An additional refrenece is put on the ndlp to prevent
2320 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2321 * the unreg mailbox command completes, this routine puts the
2322 * reference back.
2323 *
2324 **/
2325void
2326lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2327{
2328 struct lpfc_vport *vport = pmb->vport;
2329 struct lpfc_nodelist *ndlp;
2330
2331 ndlp = pmb->context1;
2332 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2333 if (phba->sli_rev == LPFC_SLI_REV4 &&
2334 (bf_get(lpfc_sli_intf_if_type,
2335 &phba->sli4_hba.sli_intf) ==
2336 LPFC_SLI_INTF_IF_TYPE_2)) {
2337 if (ndlp) {
2338 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2339 "0010 UNREG_LOGIN vpi:%x "
2340 "rpi:%x DID:%x map:%x %p\n",
2341 vport->vpi, ndlp->nlp_rpi,
2342 ndlp->nlp_DID,
2343 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002344 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002345 lpfc_nlp_put(ndlp);
2346 }
2347 }
2348 }
2349
2350 mempool_free(pmb, phba->mbox_mem_pool);
2351}
dea31012005-04-17 16:05:31 -05002352
James Smarte59058c2008-08-24 21:49:00 -04002353/**
James Smart3621a712009-04-06 18:47:14 -04002354 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002355 * @phba: Pointer to HBA context object.
2356 *
2357 * This function is called with no lock held. This function processes all
2358 * the completed mailbox commands and gives it to upper layers. The interrupt
2359 * service routine processes mailbox completion interrupt and adds completed
2360 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2361 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2362 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2363 * function returns the mailbox commands to the upper layer by calling the
2364 * completion handler function of each mailbox.
2365 **/
dea31012005-04-17 16:05:31 -05002366int
James Smart2e0fef82007-06-17 19:56:36 -05002367lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002368{
James Smart92d7f7b2007-06-17 19:56:38 -05002369 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002370 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002371 int rc;
2372 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002373
2374 phba->sli.slistat.mbox_event++;
2375
James Smart92d7f7b2007-06-17 19:56:38 -05002376 /* Get all completed mailboxe buffers into the cmplq */
2377 spin_lock_irq(&phba->hbalock);
2378 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2379 spin_unlock_irq(&phba->hbalock);
2380
dea31012005-04-17 16:05:31 -05002381 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002382 do {
2383 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2384 if (pmb == NULL)
2385 break;
2386
James Smart04c68492009-05-22 14:52:52 -04002387 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002388
James Smart858c9f62007-06-17 19:56:39 -05002389 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2390 if (pmb->vport) {
2391 lpfc_debugfs_disc_trc(pmb->vport,
2392 LPFC_DISC_TRC_MBOX_VPORT,
2393 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2394 (uint32_t)pmbox->mbxCommand,
2395 pmbox->un.varWords[0],
2396 pmbox->un.varWords[1]);
2397 }
2398 else {
2399 lpfc_debugfs_disc_trc(phba->pport,
2400 LPFC_DISC_TRC_MBOX,
2401 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2402 (uint32_t)pmbox->mbxCommand,
2403 pmbox->un.varWords[0],
2404 pmbox->un.varWords[1]);
2405 }
2406 }
2407
dea31012005-04-17 16:05:31 -05002408 /*
2409 * It is a fatal error if unknown mbox command completion.
2410 */
2411 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2412 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002413 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002414 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002415 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002416 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002417 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002418 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002419 lpfc_sli_config_mbox_subsys_get(phba,
2420 pmb),
2421 lpfc_sli_config_mbox_opcode_get(phba,
2422 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002423 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002424 phba->work_hs = HS_FFER3;
2425 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002426 continue;
dea31012005-04-17 16:05:31 -05002427 }
2428
dea31012005-04-17 16:05:31 -05002429 if (pmbox->mbxStatus) {
2430 phba->sli.slistat.mbox_stat_err++;
2431 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2432 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002433 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002434 LOG_MBOX | LOG_SLI,
2435 "(%d):0305 Mbox cmd cmpl "
2436 "error - RETRYing Data: x%x "
2437 "(x%x/x%x) x%x x%x x%x\n",
2438 pmb->vport ? pmb->vport->vpi : 0,
2439 pmbox->mbxCommand,
2440 lpfc_sli_config_mbox_subsys_get(phba,
2441 pmb),
2442 lpfc_sli_config_mbox_opcode_get(phba,
2443 pmb),
2444 pmbox->mbxStatus,
2445 pmbox->un.varWords[0],
2446 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002447 pmbox->mbxStatus = 0;
2448 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002449 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002450 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002451 continue;
dea31012005-04-17 16:05:31 -05002452 }
2453 }
2454
2455 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002456 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002457 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002458 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2459 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002460 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002461 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002462 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2463 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002464 pmb->mbox_cmpl,
2465 *((uint32_t *) pmbox),
2466 pmbox->un.varWords[0],
2467 pmbox->un.varWords[1],
2468 pmbox->un.varWords[2],
2469 pmbox->un.varWords[3],
2470 pmbox->un.varWords[4],
2471 pmbox->un.varWords[5],
2472 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002473 pmbox->un.varWords[7],
2474 pmbox->un.varWords[8],
2475 pmbox->un.varWords[9],
2476 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002477
James Smart92d7f7b2007-06-17 19:56:38 -05002478 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002479 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002480 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002481 return 0;
dea31012005-04-17 16:05:31 -05002482}
James Smart92d7f7b2007-06-17 19:56:38 -05002483
James Smarte59058c2008-08-24 21:49:00 -04002484/**
James Smart3621a712009-04-06 18:47:14 -04002485 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002486 * @phba: Pointer to HBA context object.
2487 * @pring: Pointer to driver SLI ring object.
2488 * @tag: buffer tag.
2489 *
2490 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2491 * is set in the tag the buffer is posted for a particular exchange,
2492 * the function will return the buffer without replacing the buffer.
2493 * If the buffer is for unsolicited ELS or CT traffic, this function
2494 * returns the buffer and also posts another buffer to the firmware.
2495 **/
James Smart76bb24e2007-10-27 13:38:00 -04002496static struct lpfc_dmabuf *
2497lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002498 struct lpfc_sli_ring *pring,
2499 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002500{
James Smart9f1e1b52008-12-04 22:39:40 -05002501 struct hbq_dmabuf *hbq_entry;
2502
James Smart76bb24e2007-10-27 13:38:00 -04002503 if (tag & QUE_BUFTAG_BIT)
2504 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002505 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2506 if (!hbq_entry)
2507 return NULL;
2508 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002509}
James Smart57127f12007-10-27 13:37:05 -04002510
James Smart3772a992009-05-22 14:50:54 -04002511/**
2512 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2513 * @phba: Pointer to HBA context object.
2514 * @pring: Pointer to driver SLI ring object.
2515 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2516 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2517 * @fch_type: the type for the first frame of the sequence.
2518 *
2519 * This function is called with no lock held. This function uses the r_ctl and
2520 * type of the received sequence to find the correct callback function to call
2521 * to process the sequence.
2522 **/
2523static int
2524lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2525 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2526 uint32_t fch_type)
2527{
2528 int i;
2529
James Smartf358dd02017-02-12 13:52:34 -08002530 switch (fch_type) {
2531 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002532 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002533 return 1;
2534 default:
2535 break;
2536 }
2537
James Smart3772a992009-05-22 14:50:54 -04002538 /* unSolicited Responses */
2539 if (pring->prt[0].profile) {
2540 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2541 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2542 saveq);
2543 return 1;
2544 }
2545 /* We must search, based on rctl / type
2546 for the right routine */
2547 for (i = 0; i < pring->num_mask; i++) {
2548 if ((pring->prt[i].rctl == fch_r_ctl) &&
2549 (pring->prt[i].type == fch_type)) {
2550 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2551 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2552 (phba, pring, saveq);
2553 return 1;
2554 }
2555 }
2556 return 0;
2557}
James Smarte59058c2008-08-24 21:49:00 -04002558
2559/**
James Smart3621a712009-04-06 18:47:14 -04002560 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002561 * @phba: Pointer to HBA context object.
2562 * @pring: Pointer to driver SLI ring object.
2563 * @saveq: Pointer to the unsolicited iocb.
2564 *
2565 * This function is called with no lock held by the ring event handler
2566 * when there is an unsolicited iocb posted to the response ring by the
2567 * firmware. This function gets the buffer associated with the iocbs
2568 * and calls the event handler for the ring. This function handles both
2569 * qring buffers and hbq buffers.
2570 * When the function returns 1 the caller can free the iocb object otherwise
2571 * upper layer functions will free the iocb objects.
2572 **/
dea31012005-04-17 16:05:31 -05002573static int
2574lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2575 struct lpfc_iocbq *saveq)
2576{
2577 IOCB_t * irsp;
2578 WORD5 * w5p;
2579 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002580 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002581 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002582
dea31012005-04-17 16:05:31 -05002583 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002584
2585 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2586 if (pring->lpfc_sli_rcv_async_status)
2587 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2588 else
2589 lpfc_printf_log(phba,
2590 KERN_WARNING,
2591 LOG_SLI,
2592 "0316 Ring %d handler: unexpected "
2593 "ASYNC_STATUS iocb received evt_code "
2594 "0x%x\n",
2595 pring->ringno,
2596 irsp->un.asyncstat.evt_code);
2597 return 1;
2598 }
2599
James Smart3163f722008-02-08 18:50:25 -05002600 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2601 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2602 if (irsp->ulpBdeCount > 0) {
2603 dmzbuf = lpfc_sli_get_buff(phba, pring,
2604 irsp->un.ulpWord[3]);
2605 lpfc_in_buf_free(phba, dmzbuf);
2606 }
2607
2608 if (irsp->ulpBdeCount > 1) {
2609 dmzbuf = lpfc_sli_get_buff(phba, pring,
2610 irsp->unsli3.sli3Words[3]);
2611 lpfc_in_buf_free(phba, dmzbuf);
2612 }
2613
2614 if (irsp->ulpBdeCount > 2) {
2615 dmzbuf = lpfc_sli_get_buff(phba, pring,
2616 irsp->unsli3.sli3Words[7]);
2617 lpfc_in_buf_free(phba, dmzbuf);
2618 }
2619
2620 return 1;
2621 }
2622
James Smart92d7f7b2007-06-17 19:56:38 -05002623 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002624 if (irsp->ulpBdeCount != 0) {
2625 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002626 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002627 if (!saveq->context2)
2628 lpfc_printf_log(phba,
2629 KERN_ERR,
2630 LOG_SLI,
2631 "0341 Ring %d Cannot find buffer for "
2632 "an unsolicited iocb. tag 0x%x\n",
2633 pring->ringno,
2634 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002635 }
2636 if (irsp->ulpBdeCount == 2) {
2637 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002638 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002639 if (!saveq->context3)
2640 lpfc_printf_log(phba,
2641 KERN_ERR,
2642 LOG_SLI,
2643 "0342 Ring %d Cannot find buffer for an"
2644 " unsolicited iocb. tag 0x%x\n",
2645 pring->ringno,
2646 irsp->unsli3.sli3Words[7]);
2647 }
2648 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002649 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002650 if (irsp->ulpBdeCount != 0) {
2651 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2652 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002653 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002654 lpfc_printf_log(phba,
2655 KERN_ERR,
2656 LOG_SLI,
2657 "0343 Ring %d Cannot find "
2658 "buffer for an unsolicited iocb"
2659 ". tag 0x%x\n", pring->ringno,
2660 irsp->un.ulpWord[3]);
2661 }
2662 if (irsp->ulpBdeCount == 2) {
2663 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2664 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002665 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002666 lpfc_printf_log(phba,
2667 KERN_ERR,
2668 LOG_SLI,
2669 "0344 Ring %d Cannot find "
2670 "buffer for an unsolicited "
2671 "iocb. tag 0x%x\n",
2672 pring->ringno,
2673 irsp->unsli3.sli3Words[7]);
2674 }
2675 }
James Smart92d7f7b2007-06-17 19:56:38 -05002676 }
James Smart9c2face2008-01-11 01:53:18 -05002677 if (irsp->ulpBdeCount != 0 &&
2678 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2679 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2680 int found = 0;
2681
2682 /* search continue save q for same XRI */
2683 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002684 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2685 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002686 list_add_tail(&saveq->list, &iocbq->list);
2687 found = 1;
2688 break;
2689 }
2690 }
2691 if (!found)
2692 list_add_tail(&saveq->clist,
2693 &pring->iocb_continue_saveq);
2694 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2695 list_del_init(&iocbq->clist);
2696 saveq = iocbq;
2697 irsp = &(saveq->iocb);
2698 } else
2699 return 0;
2700 }
2701 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2702 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2703 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002704 Rctl = FC_RCTL_ELS_REQ;
2705 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002706 } else {
2707 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2708 Rctl = w5p->hcsw.Rctl;
2709 Type = w5p->hcsw.Type;
2710
2711 /* Firmware Workaround */
2712 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2713 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2714 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002715 Rctl = FC_RCTL_ELS_REQ;
2716 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002717 w5p->hcsw.Rctl = Rctl;
2718 w5p->hcsw.Type = Type;
2719 }
2720 }
James Smart92d7f7b2007-06-17 19:56:38 -05002721
James Smart3772a992009-05-22 14:50:54 -04002722 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002723 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002724 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002725 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002726 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002727
James Smart92d7f7b2007-06-17 19:56:38 -05002728 return 1;
dea31012005-04-17 16:05:31 -05002729}
2730
James Smarte59058c2008-08-24 21:49:00 -04002731/**
James Smart3621a712009-04-06 18:47:14 -04002732 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002733 * @phba: Pointer to HBA context object.
2734 * @pring: Pointer to driver SLI ring object.
2735 * @prspiocb: Pointer to response iocb object.
2736 *
2737 * This function looks up the iocb_lookup table to get the command iocb
2738 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002739 * response iocb. This function is called with the hbalock held
2740 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002741 * This function returns the command iocb object if it finds the command
2742 * iocb else returns NULL.
2743 **/
dea31012005-04-17 16:05:31 -05002744static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002745lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2746 struct lpfc_sli_ring *pring,
2747 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002748{
dea31012005-04-17 16:05:31 -05002749 struct lpfc_iocbq *cmd_iocb = NULL;
2750 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002751 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002752
James Bottomley604a3e32005-10-29 10:28:33 -05002753 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002754
James Bottomley604a3e32005-10-29 10:28:33 -05002755 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2756 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002757 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002758 /* remove from txcmpl queue list */
2759 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002760 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002761 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002762 }
dea31012005-04-17 16:05:31 -05002763 }
2764
dea31012005-04-17 16:05:31 -05002765 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002766 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002767 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002768 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002769 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002770 return NULL;
2771}
2772
James Smarte59058c2008-08-24 21:49:00 -04002773/**
James Smart3772a992009-05-22 14:50:54 -04002774 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2775 * @phba: Pointer to HBA context object.
2776 * @pring: Pointer to driver SLI ring object.
2777 * @iotag: IOCB tag.
2778 *
2779 * This function looks up the iocb_lookup table to get the command iocb
2780 * corresponding to the given iotag. This function is called with the
2781 * hbalock held.
2782 * This function returns the command iocb object if it finds the command
2783 * iocb else returns NULL.
2784 **/
2785static struct lpfc_iocbq *
2786lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2787 struct lpfc_sli_ring *pring, uint16_t iotag)
2788{
James Smart895427b2017-02-12 13:52:30 -08002789 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002790
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002791 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002792 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2793 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002794 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2795 /* remove from txcmpl queue list */
2796 list_del_init(&cmd_iocb->list);
2797 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002798 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002799 }
James Smart3772a992009-05-22 14:50:54 -04002800 }
James Smart89533e92016-10-13 15:06:15 -07002801
James Smart3772a992009-05-22 14:50:54 -04002802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002803 "0372 iotag x%x lookup error: max iotag (x%x) "
2804 "iocb_flag x%x\n",
2805 iotag, phba->sli.last_iotag,
2806 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002807 return NULL;
2808}
2809
2810/**
James Smart3621a712009-04-06 18:47:14 -04002811 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002812 * @phba: Pointer to HBA context object.
2813 * @pring: Pointer to driver SLI ring object.
2814 * @saveq: Pointer to the response iocb to be processed.
2815 *
2816 * This function is called by the ring event handler for non-fcp
2817 * rings when there is a new response iocb in the response ring.
2818 * The caller is not required to hold any locks. This function
2819 * gets the command iocb associated with the response iocb and
2820 * calls the completion handler for the command iocb. If there
2821 * is no completion handler, the function will free the resources
2822 * associated with command iocb. If the response iocb is for
2823 * an already aborted command iocb, the status of the completion
2824 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2825 * This function always returns 1.
2826 **/
dea31012005-04-17 16:05:31 -05002827static int
James Smart2e0fef82007-06-17 19:56:36 -05002828lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002829 struct lpfc_iocbq *saveq)
2830{
James Smart2e0fef82007-06-17 19:56:36 -05002831 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002832 int rc = 1;
2833 unsigned long iflag;
2834
2835 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002836 if (phba->sli_rev == LPFC_SLI_REV4)
2837 spin_lock_irqsave(&pring->ring_lock, iflag);
2838 else
2839 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002840 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002841 if (phba->sli_rev == LPFC_SLI_REV4)
2842 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2843 else
2844 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05002845
dea31012005-04-17 16:05:31 -05002846 if (cmdiocbp) {
2847 if (cmdiocbp->iocb_cmpl) {
2848 /*
James Smartea2151b2008-09-07 11:52:10 -04002849 * If an ELS command failed send an event to mgmt
2850 * application.
2851 */
2852 if (saveq->iocb.ulpStatus &&
2853 (pring->ringno == LPFC_ELS_RING) &&
2854 (cmdiocbp->iocb.ulpCommand ==
2855 CMD_ELS_REQUEST64_CR))
2856 lpfc_send_els_failure_event(phba,
2857 cmdiocbp, saveq);
2858
2859 /*
dea31012005-04-17 16:05:31 -05002860 * Post all ELS completions to the worker thread.
2861 * All other are passed to the completion callback.
2862 */
2863 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002864 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2865 (cmdiocbp->iocb_flag &
2866 LPFC_DRIVER_ABORTED)) {
2867 spin_lock_irqsave(&phba->hbalock,
2868 iflag);
James Smart07951072007-04-25 09:51:38 -04002869 cmdiocbp->iocb_flag &=
2870 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002871 spin_unlock_irqrestore(&phba->hbalock,
2872 iflag);
James Smart07951072007-04-25 09:51:38 -04002873 saveq->iocb.ulpStatus =
2874 IOSTAT_LOCAL_REJECT;
2875 saveq->iocb.un.ulpWord[4] =
2876 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002877
2878 /* Firmware could still be in progress
2879 * of DMAing payload, so don't free data
2880 * buffer till after a hbeat.
2881 */
James Smart341af102010-01-26 23:07:37 -05002882 spin_lock_irqsave(&phba->hbalock,
2883 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002884 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002885 spin_unlock_irqrestore(&phba->hbalock,
2886 iflag);
2887 }
James Smart0f65ff62010-02-26 14:14:23 -05002888 if (phba->sli_rev == LPFC_SLI_REV4) {
2889 if (saveq->iocb_flag &
2890 LPFC_EXCHANGE_BUSY) {
2891 /* Set cmdiocb flag for the
2892 * exchange busy so sgl (xri)
2893 * will not be released until
2894 * the abort xri is received
2895 * from hba.
2896 */
2897 spin_lock_irqsave(
2898 &phba->hbalock, iflag);
2899 cmdiocbp->iocb_flag |=
2900 LPFC_EXCHANGE_BUSY;
2901 spin_unlock_irqrestore(
2902 &phba->hbalock, iflag);
2903 }
2904 if (cmdiocbp->iocb_flag &
2905 LPFC_DRIVER_ABORTED) {
2906 /*
2907 * Clear LPFC_DRIVER_ABORTED
2908 * bit in case it was driver
2909 * initiated abort.
2910 */
2911 spin_lock_irqsave(
2912 &phba->hbalock, iflag);
2913 cmdiocbp->iocb_flag &=
2914 ~LPFC_DRIVER_ABORTED;
2915 spin_unlock_irqrestore(
2916 &phba->hbalock, iflag);
2917 cmdiocbp->iocb.ulpStatus =
2918 IOSTAT_LOCAL_REJECT;
2919 cmdiocbp->iocb.un.ulpWord[4] =
2920 IOERR_ABORT_REQUESTED;
2921 /*
2922 * For SLI4, irsiocb contains
2923 * NO_XRI in sli_xritag, it
2924 * shall not affect releasing
2925 * sgl (xri) process.
2926 */
2927 saveq->iocb.ulpStatus =
2928 IOSTAT_LOCAL_REJECT;
2929 saveq->iocb.un.ulpWord[4] =
2930 IOERR_SLI_ABORTED;
2931 spin_lock_irqsave(
2932 &phba->hbalock, iflag);
2933 saveq->iocb_flag |=
2934 LPFC_DELAY_MEM_FREE;
2935 spin_unlock_irqrestore(
2936 &phba->hbalock, iflag);
2937 }
James Smart07951072007-04-25 09:51:38 -04002938 }
dea31012005-04-17 16:05:31 -05002939 }
James Smart2e0fef82007-06-17 19:56:36 -05002940 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002941 } else
2942 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002943 } else {
2944 /*
2945 * Unknown initiating command based on the response iotag.
2946 * This could be the case on the ELS ring because of
2947 * lpfc_els_abort().
2948 */
2949 if (pring->ringno != LPFC_ELS_RING) {
2950 /*
2951 * Ring <ringno> handler: unexpected completion IoTag
2952 * <IoTag>
2953 */
James Smarta257bf92009-04-06 18:48:10 -04002954 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002955 "0322 Ring %d handler: "
2956 "unexpected completion IoTag x%x "
2957 "Data: x%x x%x x%x x%x\n",
2958 pring->ringno,
2959 saveq->iocb.ulpIoTag,
2960 saveq->iocb.ulpStatus,
2961 saveq->iocb.un.ulpWord[4],
2962 saveq->iocb.ulpCommand,
2963 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002964 }
2965 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002966
dea31012005-04-17 16:05:31 -05002967 return rc;
2968}
2969
James Smarte59058c2008-08-24 21:49:00 -04002970/**
James Smart3621a712009-04-06 18:47:14 -04002971 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002972 * @phba: Pointer to HBA context object.
2973 * @pring: Pointer to driver SLI ring object.
2974 *
2975 * This function is called from the iocb ring event handlers when
2976 * put pointer is ahead of the get pointer for a ring. This function signal
2977 * an error attention condition to the worker thread and the worker
2978 * thread will transition the HBA to offline state.
2979 **/
James Smart2e0fef82007-06-17 19:56:36 -05002980static void
2981lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002982{
James Smart34b02dc2008-08-24 21:49:55 -04002983 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002984 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002985 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002986 * rsp ring <portRspMax>
2987 */
2988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002989 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002990 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002991 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002992 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002993
James Smart2e0fef82007-06-17 19:56:36 -05002994 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002995
2996 /*
2997 * All error attention handlers are posted to
2998 * worker thread
2999 */
3000 phba->work_ha |= HA_ERATT;
3001 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003002
James Smart5e9d9b82008-06-14 22:52:53 -04003003 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003004
3005 return;
3006}
3007
James Smarte59058c2008-08-24 21:49:00 -04003008/**
James Smart3621a712009-04-06 18:47:14 -04003009 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003010 * @ptr: Pointer to address of HBA context object.
3011 *
3012 * This function is invoked by the Error Attention polling timer when the
3013 * timer times out. It will check the SLI Error Attention register for
3014 * possible attention events. If so, it will post an Error Attention event
3015 * and wake up worker thread to process it. Otherwise, it will set up the
3016 * Error Attention polling timer for the next poll.
3017 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003018void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003019{
3020 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003021 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003022 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003023
Kees Cookf22eb4d2017-09-06 20:24:26 -07003024 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003025
James Smartaa6fbb72012-08-03 12:36:03 -04003026 /* Here we will also keep track of interrupts per sec of the hba */
3027 sli_intr = phba->sli.slistat.sli_intr;
3028
3029 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3030 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3031 sli_intr);
3032 else
3033 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3034
James Smart65791f12016-07-06 12:35:56 -07003035 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3036 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003037 phba->sli.slistat.sli_ips = cnt;
3038
3039 phba->sli.slistat.sli_prev_intr = sli_intr;
3040
James Smart93996272008-08-24 21:50:30 -04003041 /* Check chip HA register for error event */
3042 eratt = lpfc_sli_check_eratt(phba);
3043
3044 if (eratt)
3045 /* Tell the worker thread there is work to do */
3046 lpfc_worker_wake_up(phba);
3047 else
3048 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003049 mod_timer(&phba->eratt_poll,
3050 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003051 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003052 return;
3053}
3054
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003055
James Smarte59058c2008-08-24 21:49:00 -04003056/**
James Smart3621a712009-04-06 18:47:14 -04003057 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003058 * @phba: Pointer to HBA context object.
3059 * @pring: Pointer to driver SLI ring object.
3060 * @mask: Host attention register mask for this ring.
3061 *
3062 * This function is called from the interrupt context when there is a ring
3063 * event for the fcp ring. The caller does not hold any lock.
3064 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003065 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003066 * LE bit set. The function will call the completion handler of the command iocb
3067 * if the response iocb indicates a completion for a command iocb or it is
3068 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3069 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003070 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003071 * to check it explicitly.
3072 */
3073int
James Smart2e0fef82007-06-17 19:56:36 -05003074lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3075 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003076{
James Smart34b02dc2008-08-24 21:49:55 -04003077 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003078 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003079 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003080 struct lpfc_iocbq *cmdiocbq = NULL;
3081 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003082 uint32_t status;
3083 uint32_t portRspPut, portRspMax;
3084 int rc = 1;
3085 lpfc_iocb_type type;
3086 unsigned long iflag;
3087 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003088
James Smart2e0fef82007-06-17 19:56:36 -05003089 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003090 pring->stats.iocb_event++;
3091
dea31012005-04-17 16:05:31 -05003092 /*
3093 * The next available response entry should never exceed the maximum
3094 * entries. If it does, treat it as an adapter hardware error.
3095 */
James Smart7e56aa22012-08-03 12:35:34 -04003096 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003097 portRspPut = le32_to_cpu(pgp->rspPutInx);
3098 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003099 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003100 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003101 return 1;
3102 }
James Smart45ed1192009-10-02 15:17:02 -04003103 if (phba->fcp_ring_in_use) {
3104 spin_unlock_irqrestore(&phba->hbalock, iflag);
3105 return 1;
3106 } else
3107 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003108
3109 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003110 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003111 /*
3112 * Fetch an entry off the ring and copy it into a local data
3113 * structure. The copy involves a byte-swap since the
3114 * network byte order and pci byte orders are different.
3115 */
James Smarted957682007-06-17 19:56:37 -05003116 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003117 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003118
James Smart7e56aa22012-08-03 12:35:34 -04003119 if (++pring->sli.sli3.rspidx >= portRspMax)
3120 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003121
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003122 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3123 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003124 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003125 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003126 irsp = &rspiocbq.iocb;
3127
dea31012005-04-17 16:05:31 -05003128 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3129 pring->stats.iocb_rsp++;
3130 rsp_cmpl++;
3131
3132 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003133 /*
3134 * If resource errors reported from HBA, reduce
3135 * queuedepths of the SCSI device.
3136 */
3137 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003138 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3139 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003140 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003141 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003142 spin_lock_irqsave(&phba->hbalock, iflag);
3143 }
3144
dea31012005-04-17 16:05:31 -05003145 /* Rsp ring <ringno> error: IOCB */
3146 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003147 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003148 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003149 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003150 irsp->un.ulpWord[0],
3151 irsp->un.ulpWord[1],
3152 irsp->un.ulpWord[2],
3153 irsp->un.ulpWord[3],
3154 irsp->un.ulpWord[4],
3155 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003156 *(uint32_t *)&irsp->un1,
3157 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003158 }
3159
3160 switch (type) {
3161 case LPFC_ABORT_IOCB:
3162 case LPFC_SOL_IOCB:
3163 /*
3164 * Idle exchange closed via ABTS from port. No iocb
3165 * resources need to be recovered.
3166 */
3167 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003168 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003169 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003170 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003171 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003172 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003173 break;
3174 }
3175
James Bottomley604a3e32005-10-29 10:28:33 -05003176 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3177 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003178 if (unlikely(!cmdiocbq))
3179 break;
3180 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3181 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3182 if (cmdiocbq->iocb_cmpl) {
3183 spin_unlock_irqrestore(&phba->hbalock, iflag);
3184 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3185 &rspiocbq);
3186 spin_lock_irqsave(&phba->hbalock, iflag);
3187 }
dea31012005-04-17 16:05:31 -05003188 break;
James Smarta4bc3372006-12-02 13:34:16 -05003189 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003190 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003191 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003192 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003193 break;
dea31012005-04-17 16:05:31 -05003194 default:
3195 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3196 char adaptermsg[LPFC_MAX_ADPTMSG];
3197 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3198 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3199 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003200 dev_warn(&((phba->pcidev)->dev),
3201 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003202 phba->brd_no, adaptermsg);
3203 } else {
3204 /* Unknown IOCB command */
3205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003206 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003207 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003208 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003209 irsp->ulpStatus,
3210 irsp->ulpIoTag,
3211 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003212 }
3213 break;
3214 }
3215
3216 /*
3217 * The response IOCB has been processed. Update the ring
3218 * pointer in SLIM. If the port response put pointer has not
3219 * been updated, sync the pgp->rspPutInx and fetch the new port
3220 * response put pointer.
3221 */
James Smart7e56aa22012-08-03 12:35:34 -04003222 writel(pring->sli.sli3.rspidx,
3223 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003224
James Smart7e56aa22012-08-03 12:35:34 -04003225 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003226 portRspPut = le32_to_cpu(pgp->rspPutInx);
3227 }
3228
3229 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3230 pring->stats.iocb_rsp_full++;
3231 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3232 writel(status, phba->CAregaddr);
3233 readl(phba->CAregaddr);
3234 }
3235 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3236 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3237 pring->stats.iocb_cmd_empty++;
3238
3239 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003240 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003241 lpfc_sli_resume_iocb(phba, pring);
3242
3243 if ((pring->lpfc_sli_cmd_available))
3244 (pring->lpfc_sli_cmd_available) (phba, pring);
3245
3246 }
3247
James Smart45ed1192009-10-02 15:17:02 -04003248 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003249 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003250 return rc;
3251}
3252
James Smarte59058c2008-08-24 21:49:00 -04003253/**
James Smart3772a992009-05-22 14:50:54 -04003254 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3255 * @phba: Pointer to HBA context object.
3256 * @pring: Pointer to driver SLI ring object.
3257 * @rspiocbp: Pointer to driver response IOCB object.
3258 *
3259 * This function is called from the worker thread when there is a slow-path
3260 * response IOCB to process. This function chains all the response iocbs until
3261 * seeing the iocb with the LE bit set. The function will call
3262 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3263 * completion of a command iocb. The function will call the
3264 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3265 * The function frees the resources or calls the completion handler if this
3266 * iocb is an abort completion. The function returns NULL when the response
3267 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3268 * this function shall chain the iocb on to the iocb_continueq and return the
3269 * response iocb passed in.
3270 **/
3271static struct lpfc_iocbq *
3272lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3273 struct lpfc_iocbq *rspiocbp)
3274{
3275 struct lpfc_iocbq *saveq;
3276 struct lpfc_iocbq *cmdiocbp;
3277 struct lpfc_iocbq *next_iocb;
3278 IOCB_t *irsp = NULL;
3279 uint32_t free_saveq;
3280 uint8_t iocb_cmd_type;
3281 lpfc_iocb_type type;
3282 unsigned long iflag;
3283 int rc;
3284
3285 spin_lock_irqsave(&phba->hbalock, iflag);
3286 /* First add the response iocb to the countinueq list */
3287 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3288 pring->iocb_continueq_cnt++;
3289
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003290 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003291 irsp = &rspiocbp->iocb;
3292 if (irsp->ulpLe) {
3293 /*
3294 * By default, the driver expects to free all resources
3295 * associated with this iocb completion.
3296 */
3297 free_saveq = 1;
3298 saveq = list_get_first(&pring->iocb_continueq,
3299 struct lpfc_iocbq, list);
3300 irsp = &(saveq->iocb);
3301 list_del_init(&pring->iocb_continueq);
3302 pring->iocb_continueq_cnt = 0;
3303
3304 pring->stats.iocb_rsp++;
3305
3306 /*
3307 * If resource errors reported from HBA, reduce
3308 * queuedepths of the SCSI device.
3309 */
3310 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003311 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3312 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003313 spin_unlock_irqrestore(&phba->hbalock, iflag);
3314 phba->lpfc_rampdown_queue_depth(phba);
3315 spin_lock_irqsave(&phba->hbalock, iflag);
3316 }
3317
3318 if (irsp->ulpStatus) {
3319 /* Rsp ring <ringno> error: IOCB */
3320 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3321 "0328 Rsp Ring %d error: "
3322 "IOCB Data: "
3323 "x%x x%x x%x x%x "
3324 "x%x x%x x%x x%x "
3325 "x%x x%x x%x x%x "
3326 "x%x x%x x%x x%x\n",
3327 pring->ringno,
3328 irsp->un.ulpWord[0],
3329 irsp->un.ulpWord[1],
3330 irsp->un.ulpWord[2],
3331 irsp->un.ulpWord[3],
3332 irsp->un.ulpWord[4],
3333 irsp->un.ulpWord[5],
3334 *(((uint32_t *) irsp) + 6),
3335 *(((uint32_t *) irsp) + 7),
3336 *(((uint32_t *) irsp) + 8),
3337 *(((uint32_t *) irsp) + 9),
3338 *(((uint32_t *) irsp) + 10),
3339 *(((uint32_t *) irsp) + 11),
3340 *(((uint32_t *) irsp) + 12),
3341 *(((uint32_t *) irsp) + 13),
3342 *(((uint32_t *) irsp) + 14),
3343 *(((uint32_t *) irsp) + 15));
3344 }
3345
3346 /*
3347 * Fetch the IOCB command type and call the correct completion
3348 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3349 * get freed back to the lpfc_iocb_list by the discovery
3350 * kernel thread.
3351 */
3352 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3353 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3354 switch (type) {
3355 case LPFC_SOL_IOCB:
3356 spin_unlock_irqrestore(&phba->hbalock, iflag);
3357 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3358 spin_lock_irqsave(&phba->hbalock, iflag);
3359 break;
3360
3361 case LPFC_UNSOL_IOCB:
3362 spin_unlock_irqrestore(&phba->hbalock, iflag);
3363 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3364 spin_lock_irqsave(&phba->hbalock, iflag);
3365 if (!rc)
3366 free_saveq = 0;
3367 break;
3368
3369 case LPFC_ABORT_IOCB:
3370 cmdiocbp = NULL;
3371 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3372 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3373 saveq);
3374 if (cmdiocbp) {
3375 /* Call the specified completion routine */
3376 if (cmdiocbp->iocb_cmpl) {
3377 spin_unlock_irqrestore(&phba->hbalock,
3378 iflag);
3379 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3380 saveq);
3381 spin_lock_irqsave(&phba->hbalock,
3382 iflag);
3383 } else
3384 __lpfc_sli_release_iocbq(phba,
3385 cmdiocbp);
3386 }
3387 break;
3388
3389 case LPFC_UNKNOWN_IOCB:
3390 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3391 char adaptermsg[LPFC_MAX_ADPTMSG];
3392 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3393 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3394 MAX_MSG_DATA);
3395 dev_warn(&((phba->pcidev)->dev),
3396 "lpfc%d: %s\n",
3397 phba->brd_no, adaptermsg);
3398 } else {
3399 /* Unknown IOCB command */
3400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3401 "0335 Unknown IOCB "
3402 "command Data: x%x "
3403 "x%x x%x x%x\n",
3404 irsp->ulpCommand,
3405 irsp->ulpStatus,
3406 irsp->ulpIoTag,
3407 irsp->ulpContext);
3408 }
3409 break;
3410 }
3411
3412 if (free_saveq) {
3413 list_for_each_entry_safe(rspiocbp, next_iocb,
3414 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003415 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003416 __lpfc_sli_release_iocbq(phba, rspiocbp);
3417 }
3418 __lpfc_sli_release_iocbq(phba, saveq);
3419 }
3420 rspiocbp = NULL;
3421 }
3422 spin_unlock_irqrestore(&phba->hbalock, iflag);
3423 return rspiocbp;
3424}
3425
3426/**
3427 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003428 * @phba: Pointer to HBA context object.
3429 * @pring: Pointer to driver SLI ring object.
3430 * @mask: Host attention register mask for this ring.
3431 *
James Smart3772a992009-05-22 14:50:54 -04003432 * This routine wraps the actual slow_ring event process routine from the
3433 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003434 **/
James Smart3772a992009-05-22 14:50:54 -04003435void
James Smart2e0fef82007-06-17 19:56:36 -05003436lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3437 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003438{
James Smart3772a992009-05-22 14:50:54 -04003439 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3440}
3441
3442/**
3443 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3444 * @phba: Pointer to HBA context object.
3445 * @pring: Pointer to driver SLI ring object.
3446 * @mask: Host attention register mask for this ring.
3447 *
3448 * This function is called from the worker thread when there is a ring event
3449 * for non-fcp rings. The caller does not hold any lock. The function will
3450 * remove each response iocb in the response ring and calls the handle
3451 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3452 **/
3453static void
3454lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3455 struct lpfc_sli_ring *pring, uint32_t mask)
3456{
James Smart34b02dc2008-08-24 21:49:55 -04003457 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003458 IOCB_t *entry;
3459 IOCB_t *irsp = NULL;
3460 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003461 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003462 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003463 uint32_t status;
dea31012005-04-17 16:05:31 -05003464
James Smart34b02dc2008-08-24 21:49:55 -04003465 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003466 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003467 pring->stats.iocb_event++;
3468
dea31012005-04-17 16:05:31 -05003469 /*
3470 * The next available response entry should never exceed the maximum
3471 * entries. If it does, treat it as an adapter hardware error.
3472 */
James Smart7e56aa22012-08-03 12:35:34 -04003473 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003474 portRspPut = le32_to_cpu(pgp->rspPutInx);
3475 if (portRspPut >= portRspMax) {
3476 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003477 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003478 * rsp ring <portRspMax>
3479 */
James Smarted957682007-06-17 19:56:37 -05003480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003481 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003482 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003483 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003484
James Smart2e0fef82007-06-17 19:56:36 -05003485 phba->link_state = LPFC_HBA_ERROR;
3486 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003487
3488 phba->work_hs = HS_FFER3;
3489 lpfc_handle_eratt(phba);
3490
James Smart3772a992009-05-22 14:50:54 -04003491 return;
dea31012005-04-17 16:05:31 -05003492 }
3493
3494 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003495 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003496 /*
3497 * Build a completion list and call the appropriate handler.
3498 * The process is to get the next available response iocb, get
3499 * a free iocb from the list, copy the response data into the
3500 * free iocb, insert to the continuation list, and update the
3501 * next response index to slim. This process makes response
3502 * iocb's in the ring available to DMA as fast as possible but
3503 * pays a penalty for a copy operation. Since the iocb is
3504 * only 32 bytes, this penalty is considered small relative to
3505 * the PCI reads for register values and a slim write. When
3506 * the ulpLe field is set, the entire Command has been
3507 * received.
3508 */
James Smarted957682007-06-17 19:56:37 -05003509 entry = lpfc_resp_iocb(phba, pring);
3510
James Smart858c9f62007-06-17 19:56:39 -05003511 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003512 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003513 if (rspiocbp == NULL) {
3514 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003515 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003516 break;
3517 }
3518
James Smarted957682007-06-17 19:56:37 -05003519 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3520 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003521 irsp = &rspiocbp->iocb;
3522
James Smart7e56aa22012-08-03 12:35:34 -04003523 if (++pring->sli.sli3.rspidx >= portRspMax)
3524 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003525
James Smarta58cbd52007-08-02 11:09:43 -04003526 if (pring->ringno == LPFC_ELS_RING) {
3527 lpfc_debugfs_slow_ring_trc(phba,
3528 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3529 *(((uint32_t *) irsp) + 4),
3530 *(((uint32_t *) irsp) + 6),
3531 *(((uint32_t *) irsp) + 7));
3532 }
3533
James Smart7e56aa22012-08-03 12:35:34 -04003534 writel(pring->sli.sli3.rspidx,
3535 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003536
James Smart3772a992009-05-22 14:50:54 -04003537 spin_unlock_irqrestore(&phba->hbalock, iflag);
3538 /* Handle the response IOCB */
3539 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3540 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003541
3542 /*
3543 * If the port response put pointer has not been updated, sync
3544 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3545 * response put pointer.
3546 */
James Smart7e56aa22012-08-03 12:35:34 -04003547 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003548 portRspPut = le32_to_cpu(pgp->rspPutInx);
3549 }
James Smart7e56aa22012-08-03 12:35:34 -04003550 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003551
James Smart92d7f7b2007-06-17 19:56:38 -05003552 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003553 /* At least one response entry has been freed */
3554 pring->stats.iocb_rsp_full++;
3555 /* SET RxRE_RSP in Chip Att register */
3556 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3557 writel(status, phba->CAregaddr);
3558 readl(phba->CAregaddr); /* flush */
3559 }
3560 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3561 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3562 pring->stats.iocb_cmd_empty++;
3563
3564 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003565 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003566 lpfc_sli_resume_iocb(phba, pring);
3567
3568 if ((pring->lpfc_sli_cmd_available))
3569 (pring->lpfc_sli_cmd_available) (phba, pring);
3570
3571 }
3572
James Smart2e0fef82007-06-17 19:56:36 -05003573 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003574 return;
dea31012005-04-17 16:05:31 -05003575}
3576
James Smarte59058c2008-08-24 21:49:00 -04003577/**
James Smart4f774512009-05-22 14:52:35 -04003578 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3579 * @phba: Pointer to HBA context object.
3580 * @pring: Pointer to driver SLI ring object.
3581 * @mask: Host attention register mask for this ring.
3582 *
3583 * This function is called from the worker thread when there is a pending
3584 * ELS response iocb on the driver internal slow-path response iocb worker
3585 * queue. The caller does not hold any lock. The function will remove each
3586 * response iocb from the response worker queue and calls the handle
3587 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3588 **/
3589static void
3590lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3591 struct lpfc_sli_ring *pring, uint32_t mask)
3592{
3593 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003594 struct hbq_dmabuf *dmabuf;
3595 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003596 unsigned long iflag;
3597
James Smart45ed1192009-10-02 15:17:02 -04003598 spin_lock_irqsave(&phba->hbalock, iflag);
3599 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3600 spin_unlock_irqrestore(&phba->hbalock, iflag);
3601 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003602 /* Get the response iocb from the head of work queue */
3603 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003604 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003605 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003606 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003607
3608 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3609 case CQE_CODE_COMPL_WQE:
3610 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3611 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003612 /* Translate ELS WCQE to response IOCBQ */
3613 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3614 irspiocbq);
3615 if (irspiocbq)
3616 lpfc_sli_sp_handle_rspiocb(phba, pring,
3617 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003618 break;
3619 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003620 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003621 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3622 cq_event);
3623 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3624 break;
3625 default:
3626 break;
3627 }
James Smart4f774512009-05-22 14:52:35 -04003628 }
3629}
3630
3631/**
James Smart3621a712009-04-06 18:47:14 -04003632 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003633 * @phba: Pointer to HBA context object.
3634 * @pring: Pointer to driver SLI ring object.
3635 *
3636 * This function aborts all iocbs in the given ring and frees all the iocb
3637 * objects in txq. This function issues an abort iocb for all the iocb commands
3638 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3639 * the return of this function. The caller is not required to hold any locks.
3640 **/
James Smart2e0fef82007-06-17 19:56:36 -05003641void
dea31012005-04-17 16:05:31 -05003642lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3643{
James Smart2534ba72007-04-25 09:52:20 -04003644 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003645 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003646
James Smart92d7f7b2007-06-17 19:56:38 -05003647 if (pring->ringno == LPFC_ELS_RING) {
3648 lpfc_fabric_abort_hba(phba);
3649 }
3650
dea31012005-04-17 16:05:31 -05003651 /* Error everything on txq and txcmplq
3652 * First do the txq.
3653 */
James Smartdb55fba2014-04-04 13:52:02 -04003654 if (phba->sli_rev >= LPFC_SLI_REV4) {
3655 spin_lock_irq(&pring->ring_lock);
3656 list_splice_init(&pring->txq, &completions);
3657 pring->txq_cnt = 0;
3658 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003659
James Smartdb55fba2014-04-04 13:52:02 -04003660 spin_lock_irq(&phba->hbalock);
3661 /* Next issue ABTS for everything on the txcmplq */
3662 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3663 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3664 spin_unlock_irq(&phba->hbalock);
3665 } else {
3666 spin_lock_irq(&phba->hbalock);
3667 list_splice_init(&pring->txq, &completions);
3668 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003669
James Smartdb55fba2014-04-04 13:52:02 -04003670 /* Next issue ABTS for everything on the txcmplq */
3671 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3672 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3673 spin_unlock_irq(&phba->hbalock);
3674 }
James Smart2534ba72007-04-25 09:52:20 -04003675
James Smarta257bf92009-04-06 18:48:10 -04003676 /* Cancel all the IOCBs from the completions list */
3677 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3678 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003679}
3680
James Smarte59058c2008-08-24 21:49:00 -04003681/**
James Smart895427b2017-02-12 13:52:30 -08003682 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3683 * @phba: Pointer to HBA context object.
3684 * @pring: Pointer to driver SLI ring object.
3685 *
3686 * This function aborts all iocbs in the given ring and frees all the iocb
3687 * objects in txq. This function issues an abort iocb for all the iocb commands
3688 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3689 * the return of this function. The caller is not required to hold any locks.
3690 **/
3691void
3692lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3693{
3694 LIST_HEAD(completions);
3695 struct lpfc_iocbq *iocb, *next_iocb;
3696
3697 if (pring->ringno == LPFC_ELS_RING)
3698 lpfc_fabric_abort_hba(phba);
3699
3700 spin_lock_irq(&phba->hbalock);
3701 /* Next issue ABTS for everything on the txcmplq */
3702 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3703 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3704 spin_unlock_irq(&phba->hbalock);
3705}
3706
3707
3708/**
James Smartdb55fba2014-04-04 13:52:02 -04003709 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3710 * @phba: Pointer to HBA context object.
3711 * @pring: Pointer to driver SLI ring object.
3712 *
3713 * This function aborts all iocbs in FCP rings and frees all the iocb
3714 * objects in txq. This function issues an abort iocb for all the iocb commands
3715 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3716 * the return of this function. The caller is not required to hold any locks.
3717 **/
3718void
3719lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3720{
3721 struct lpfc_sli *psli = &phba->sli;
3722 struct lpfc_sli_ring *pring;
3723 uint32_t i;
3724
3725 /* Look on all the FCP Rings for the iotag */
3726 if (phba->sli_rev >= LPFC_SLI_REV4) {
3727 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003728 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003729 lpfc_sli_abort_iocb_ring(phba, pring);
3730 }
3731 } else {
James Smart895427b2017-02-12 13:52:30 -08003732 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003733 lpfc_sli_abort_iocb_ring(phba, pring);
3734 }
3735}
3736
James Smart895427b2017-02-12 13:52:30 -08003737/**
3738 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3739 * @phba: Pointer to HBA context object.
3740 *
3741 * This function aborts all wqes in NVME rings. This function issues an
3742 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3743 * the txcmplq is not guaranteed to complete before the return of this
3744 * function. The caller is not required to hold any locks.
3745 **/
3746void
3747lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3748{
3749 struct lpfc_sli_ring *pring;
3750 uint32_t i;
3751
3752 if (phba->sli_rev < LPFC_SLI_REV4)
3753 return;
3754
3755 /* Abort all IO on each NVME ring. */
3756 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3757 pring = phba->sli4_hba.nvme_wq[i]->pring;
3758 lpfc_sli_abort_wqe_ring(phba, pring);
3759 }
3760}
3761
James Smartdb55fba2014-04-04 13:52:02 -04003762
3763/**
James Smart3621a712009-04-06 18:47:14 -04003764 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003765 * @phba: Pointer to HBA context object.
3766 *
3767 * This function flushes all iocbs in the fcp ring and frees all the iocb
3768 * objects in txq and txcmplq. This function will not issue abort iocbs
3769 * for all the iocb commands in txcmplq, they will just be returned with
3770 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3771 * slot has been permanently disabled.
3772 **/
3773void
3774lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3775{
3776 LIST_HEAD(txq);
3777 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003778 struct lpfc_sli *psli = &phba->sli;
3779 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003780 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003781 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003782
3783 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003784 /* Indicate the I/O queues are flushed */
3785 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003786 spin_unlock_irq(&phba->hbalock);
3787
James Smartdb55fba2014-04-04 13:52:02 -04003788 /* Look on all the FCP Rings for the iotag */
3789 if (phba->sli_rev >= LPFC_SLI_REV4) {
3790 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003791 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003792
James Smartdb55fba2014-04-04 13:52:02 -04003793 spin_lock_irq(&pring->ring_lock);
3794 /* Retrieve everything on txq */
3795 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003796 list_for_each_entry_safe(piocb, next_iocb,
3797 &pring->txcmplq, list)
3798 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003799 /* Retrieve everything on the txcmplq */
3800 list_splice_init(&pring->txcmplq, &txcmplq);
3801 pring->txq_cnt = 0;
3802 pring->txcmplq_cnt = 0;
3803 spin_unlock_irq(&pring->ring_lock);
3804
3805 /* Flush the txq */
3806 lpfc_sli_cancel_iocbs(phba, &txq,
3807 IOSTAT_LOCAL_REJECT,
3808 IOERR_SLI_DOWN);
3809 /* Flush the txcmpq */
3810 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3811 IOSTAT_LOCAL_REJECT,
3812 IOERR_SLI_DOWN);
3813 }
3814 } else {
James Smart895427b2017-02-12 13:52:30 -08003815 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003816
3817 spin_lock_irq(&phba->hbalock);
3818 /* Retrieve everything on txq */
3819 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003820 list_for_each_entry_safe(piocb, next_iocb,
3821 &pring->txcmplq, list)
3822 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003823 /* Retrieve everything on the txcmplq */
3824 list_splice_init(&pring->txcmplq, &txcmplq);
3825 pring->txq_cnt = 0;
3826 pring->txcmplq_cnt = 0;
3827 spin_unlock_irq(&phba->hbalock);
3828
3829 /* Flush the txq */
3830 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3831 IOERR_SLI_DOWN);
3832 /* Flush the txcmpq */
3833 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3834 IOERR_SLI_DOWN);
3835 }
James Smarta8e497d2008-08-24 21:50:11 -04003836}
3837
3838/**
James Smart895427b2017-02-12 13:52:30 -08003839 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3840 * @phba: Pointer to HBA context object.
3841 *
3842 * This function flushes all wqes in the nvme rings and frees all resources
3843 * in the txcmplq. This function does not issue abort wqes for the IO
3844 * commands in txcmplq, they will just be returned with
3845 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3846 * slot has been permanently disabled.
3847 **/
3848void
3849lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3850{
3851 LIST_HEAD(txcmplq);
3852 struct lpfc_sli_ring *pring;
3853 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003854 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08003855
3856 if (phba->sli_rev < LPFC_SLI_REV4)
3857 return;
3858
3859 /* Hint to other driver operations that a flush is in progress. */
3860 spin_lock_irq(&phba->hbalock);
3861 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3862 spin_unlock_irq(&phba->hbalock);
3863
3864 /* Cycle through all NVME rings and complete each IO with
3865 * a local driver reason code. This is a flush so no
3866 * abort exchange to FW.
3867 */
3868 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3869 pring = phba->sli4_hba.nvme_wq[i]->pring;
3870
James Smart895427b2017-02-12 13:52:30 -08003871 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08003872 list_for_each_entry_safe(piocb, next_iocb,
3873 &pring->txcmplq, list)
3874 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3875 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08003876 list_splice_init(&pring->txcmplq, &txcmplq);
3877 pring->txcmplq_cnt = 0;
3878 spin_unlock_irq(&pring->ring_lock);
3879
3880 /* Flush the txcmpq &&&PAE */
3881 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3882 IOSTAT_LOCAL_REJECT,
3883 IOERR_SLI_DOWN);
3884 }
3885}
3886
3887/**
James Smart3772a992009-05-22 14:50:54 -04003888 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003889 * @phba: Pointer to HBA context object.
3890 * @mask: Bit mask to be checked.
3891 *
3892 * This function reads the host status register and compares
3893 * with the provided bit mask to check if HBA completed
3894 * the restart. This function will wait in a loop for the
3895 * HBA to complete restart. If the HBA does not restart within
3896 * 15 iterations, the function will reset the HBA again. The
3897 * function returns 1 when HBA fail to restart otherwise returns
3898 * zero.
3899 **/
James Smart3772a992009-05-22 14:50:54 -04003900static int
3901lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003902{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003903 uint32_t status;
3904 int i = 0;
3905 int retval = 0;
dea31012005-04-17 16:05:31 -05003906
Jamie Wellnitz41415862006-02-28 19:25:27 -05003907 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003908 if (lpfc_readl(phba->HSregaddr, &status))
3909 return 1;
dea31012005-04-17 16:05:31 -05003910
Jamie Wellnitz41415862006-02-28 19:25:27 -05003911 /*
3912 * Check status register every 100ms for 5 retries, then every
3913 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3914 * every 2.5 sec for 4.
3915 * Break our of the loop if errors occurred during init.
3916 */
3917 while (((status & mask) != mask) &&
3918 !(status & HS_FFERM) &&
3919 i++ < 20) {
dea31012005-04-17 16:05:31 -05003920
Jamie Wellnitz41415862006-02-28 19:25:27 -05003921 if (i <= 5)
3922 msleep(10);
3923 else if (i <= 10)
3924 msleep(500);
3925 else
3926 msleep(2500);
dea31012005-04-17 16:05:31 -05003927
Jamie Wellnitz41415862006-02-28 19:25:27 -05003928 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003929 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003930 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003931 lpfc_sli_brdrestart(phba);
3932 }
3933 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003934 if (lpfc_readl(phba->HSregaddr, &status)) {
3935 retval = 1;
3936 break;
3937 }
dea31012005-04-17 16:05:31 -05003938 }
dea31012005-04-17 16:05:31 -05003939
Jamie Wellnitz41415862006-02-28 19:25:27 -05003940 /* Check to see if any errors occurred during init */
3941 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3943 "2751 Adapter failed to restart, "
3944 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3945 status,
3946 readl(phba->MBslimaddr + 0xa8),
3947 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003948 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949 retval = 1;
3950 }
dea31012005-04-17 16:05:31 -05003951
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 return retval;
dea31012005-04-17 16:05:31 -05003953}
3954
James Smartda0436e2009-05-22 14:51:39 -04003955/**
3956 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3957 * @phba: Pointer to HBA context object.
3958 * @mask: Bit mask to be checked.
3959 *
3960 * This function checks the host status register to check if HBA is
3961 * ready. This function will wait in a loop for the HBA to be ready
3962 * If the HBA is not ready , the function will will reset the HBA PCI
3963 * function again. The function returns 1 when HBA fail to be ready
3964 * otherwise returns zero.
3965 **/
3966static int
3967lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3968{
3969 uint32_t status;
3970 int retval = 0;
3971
3972 /* Read the HBA Host Status Register */
3973 status = lpfc_sli4_post_status_check(phba);
3974
3975 if (status) {
3976 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3977 lpfc_sli_brdrestart(phba);
3978 status = lpfc_sli4_post_status_check(phba);
3979 }
3980
3981 /* Check to see if any errors occurred during init */
3982 if (status) {
3983 phba->link_state = LPFC_HBA_ERROR;
3984 retval = 1;
3985 } else
3986 phba->sli4_hba.intr_enable = 0;
3987
3988 return retval;
3989}
3990
3991/**
3992 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3993 * @phba: Pointer to HBA context object.
3994 * @mask: Bit mask to be checked.
3995 *
3996 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3997 * from the API jump table function pointer from the lpfc_hba struct.
3998 **/
3999int
4000lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4001{
4002 return phba->lpfc_sli_brdready(phba, mask);
4003}
4004
James Smart92908312006-03-07 15:04:13 -05004005#define BARRIER_TEST_PATTERN (0xdeadbeef)
4006
James Smarte59058c2008-08-24 21:49:00 -04004007/**
James Smart3621a712009-04-06 18:47:14 -04004008 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004009 * @phba: Pointer to HBA context object.
4010 *
James Smart1b511972011-12-13 13:23:09 -05004011 * This function is called before resetting an HBA. This function is called
4012 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004013 **/
James Smart2e0fef82007-06-17 19:56:36 -05004014void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004015{
James Smart65a29c12006-07-06 15:50:50 -04004016 uint32_t __iomem *resp_buf;
4017 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004018 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004019 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004020 int i;
4021 uint8_t hdrtype;
4022
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004023 lockdep_assert_held(&phba->hbalock);
4024
James Smart92908312006-03-07 15:04:13 -05004025 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4026 if (hdrtype != 0x80 ||
4027 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4028 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4029 return;
4030
4031 /*
4032 * Tell the other part of the chip to suspend temporarily all
4033 * its DMA activity.
4034 */
James Smart65a29c12006-07-06 15:50:50 -04004035 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004036
4037 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004038 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4039 return;
James Smart92908312006-03-07 15:04:13 -05004040 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4041 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004042 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004043
James Smart9940b972011-03-11 16:06:12 -05004044 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4045 return;
4046 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004047 /* Clear Chip error bit */
4048 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004049 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004050 }
4051
4052 mbox = 0;
4053 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4054 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4055
4056 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004057 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004058 writel(mbox, mbox_buf);
4059
James Smart9940b972011-03-11 16:06:12 -05004060 for (i = 0; i < 50; i++) {
4061 if (lpfc_readl((resp_buf + 1), &resp_data))
4062 return;
4063 if (resp_data != ~(BARRIER_TEST_PATTERN))
4064 mdelay(1);
4065 else
4066 break;
4067 }
4068 resp_data = 0;
4069 if (lpfc_readl((resp_buf + 1), &resp_data))
4070 return;
4071 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004072 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004073 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004074 goto restore_hc;
4075 else
4076 goto clear_errat;
4077 }
4078
4079 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004080 resp_data = 0;
4081 for (i = 0; i < 500; i++) {
4082 if (lpfc_readl(resp_buf, &resp_data))
4083 return;
4084 if (resp_data != mbox)
4085 mdelay(1);
4086 else
4087 break;
4088 }
James Smart92908312006-03-07 15:04:13 -05004089
4090clear_errat:
4091
James Smart9940b972011-03-11 16:06:12 -05004092 while (++i < 500) {
4093 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4094 return;
4095 if (!(ha_copy & HA_ERATT))
4096 mdelay(1);
4097 else
4098 break;
4099 }
James Smart92908312006-03-07 15:04:13 -05004100
4101 if (readl(phba->HAregaddr) & HA_ERATT) {
4102 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004103 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004104 }
4105
4106restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004107 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004108 writel(hc_copy, phba->HCregaddr);
4109 readl(phba->HCregaddr); /* flush */
4110}
4111
James Smarte59058c2008-08-24 21:49:00 -04004112/**
James Smart3621a712009-04-06 18:47:14 -04004113 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004114 * @phba: Pointer to HBA context object.
4115 *
4116 * This function issues a kill_board mailbox command and waits for
4117 * the error attention interrupt. This function is called for stopping
4118 * the firmware processing. The caller is not required to hold any
4119 * locks. This function calls lpfc_hba_down_post function to free
4120 * any pending commands after the kill. The function will return 1 when it
4121 * fails to kill the board else will return 0.
4122 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004123int
James Smart2e0fef82007-06-17 19:56:36 -05004124lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004125{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004126 struct lpfc_sli *psli;
4127 LPFC_MBOXQ_t *pmb;
4128 uint32_t status;
4129 uint32_t ha_copy;
4130 int retval;
4131 int i = 0;
4132
4133 psli = &phba->sli;
4134
4135 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004136 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004137 "0329 Kill HBA Data: x%x x%x\n",
4138 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004139
James Smart98c9ea52007-10-27 13:37:33 -04004140 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4141 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004142 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004143
4144 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004145 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004146 if (lpfc_readl(phba->HCregaddr, &status)) {
4147 spin_unlock_irq(&phba->hbalock);
4148 mempool_free(pmb, phba->mbox_mem_pool);
4149 return 1;
4150 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004151 status &= ~HC_ERINT_ENA;
4152 writel(status, phba->HCregaddr);
4153 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004154 phba->link_flag |= LS_IGNORE_ERATT;
4155 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004156
4157 lpfc_kill_board(phba, pmb);
4158 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4159 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4160
4161 if (retval != MBX_SUCCESS) {
4162 if (retval != MBX_BUSY)
4163 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4165 "2752 KILL_BOARD command failed retval %d\n",
4166 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004167 spin_lock_irq(&phba->hbalock);
4168 phba->link_flag &= ~LS_IGNORE_ERATT;
4169 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004170 return 1;
4171 }
4172
James Smartf4b4c682009-05-22 14:53:12 -04004173 spin_lock_irq(&phba->hbalock);
4174 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4175 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004176
Jamie Wellnitz41415862006-02-28 19:25:27 -05004177 mempool_free(pmb, phba->mbox_mem_pool);
4178
4179 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4180 * attention every 100ms for 3 seconds. If we don't get ERATT after
4181 * 3 seconds we still set HBA_ERROR state because the status of the
4182 * board is now undefined.
4183 */
James Smart9940b972011-03-11 16:06:12 -05004184 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4185 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004186 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4187 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004188 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4189 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004190 }
4191
4192 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004193 if (ha_copy & HA_ERATT) {
4194 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004195 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004196 }
James Smart2e0fef82007-06-17 19:56:36 -05004197 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004198 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004199 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004200 phba->link_flag &= ~LS_IGNORE_ERATT;
4201 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004204 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004205
James Smart2e0fef82007-06-17 19:56:36 -05004206 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004207}
4208
James Smarte59058c2008-08-24 21:49:00 -04004209/**
James Smart3772a992009-05-22 14:50:54 -04004210 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004211 * @phba: Pointer to HBA context object.
4212 *
4213 * This function resets the HBA by writing HC_INITFF to the control
4214 * register. After the HBA resets, this function resets all the iocb ring
4215 * indices. This function disables PCI layer parity checking during
4216 * the reset.
4217 * This function returns 0 always.
4218 * The caller is not required to hold any locks.
4219 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004220int
James Smart2e0fef82007-06-17 19:56:36 -05004221lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004222{
4223 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004224 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004225 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004226 int i;
dea31012005-04-17 16:05:31 -05004227
Jamie Wellnitz41415862006-02-28 19:25:27 -05004228 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004229
Jamie Wellnitz41415862006-02-28 19:25:27 -05004230 /* Reset HBA */
4231 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004232 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004233 (phba->pport) ? phba->pport->port_state : 0,
4234 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004235
4236 /* perform board reset */
4237 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004238 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004239 if (phba->pport) {
4240 phba->pport->fc_myDID = 0;
4241 phba->pport->fc_prevDID = 0;
4242 }
dea31012005-04-17 16:05:31 -05004243
Jamie Wellnitz41415862006-02-28 19:25:27 -05004244 /* Turn off parity checking and serr during the physical reset */
4245 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4246 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4247 (cfg_value &
4248 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4249
James Smart3772a992009-05-22 14:50:54 -04004250 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4251
Jamie Wellnitz41415862006-02-28 19:25:27 -05004252 /* Now toggle INITFF bit in the Host Control Register */
4253 writel(HC_INITFF, phba->HCregaddr);
4254 mdelay(1);
4255 readl(phba->HCregaddr); /* flush */
4256 writel(0, phba->HCregaddr);
4257 readl(phba->HCregaddr); /* flush */
4258
4259 /* Restore PCI cmd register */
4260 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004261
4262 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004263 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004264 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004265 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004266 pring->sli.sli3.rspidx = 0;
4267 pring->sli.sli3.next_cmdidx = 0;
4268 pring->sli.sli3.local_getidx = 0;
4269 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004270 pring->missbufcnt = 0;
4271 }
dea31012005-04-17 16:05:31 -05004272
James Smart2e0fef82007-06-17 19:56:36 -05004273 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004274 return 0;
4275}
4276
James Smarte59058c2008-08-24 21:49:00 -04004277/**
James Smartda0436e2009-05-22 14:51:39 -04004278 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4279 * @phba: Pointer to HBA context object.
4280 *
4281 * This function resets a SLI4 HBA. This function disables PCI layer parity
4282 * checking during resets the device. The caller is not required to hold
4283 * any locks.
4284 *
4285 * This function returns 0 always.
4286 **/
4287int
4288lpfc_sli4_brdreset(struct lpfc_hba *phba)
4289{
4290 struct lpfc_sli *psli = &phba->sli;
4291 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004292 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004293
4294 /* Reset HBA */
4295 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004296 "0295 Reset HBA Data: x%x x%x x%x\n",
4297 phba->pport->port_state, psli->sli_flag,
4298 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004299
4300 /* perform board reset */
4301 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004302 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004303 phba->pport->fc_myDID = 0;
4304 phba->pport->fc_prevDID = 0;
4305
James Smartda0436e2009-05-22 14:51:39 -04004306 spin_lock_irq(&phba->hbalock);
4307 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4308 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004309 spin_unlock_irq(&phba->hbalock);
4310
James Smart02936352014-04-04 13:52:12 -04004311 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4312 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4313 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4314 return rc;
4315 }
4316
James Smartda0436e2009-05-22 14:51:39 -04004317 /* Now physically reset the device */
4318 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4319 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004320
4321 /* Turn off parity checking and serr during the physical reset */
4322 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4323 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4324 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4325
James Smart88318812012-09-29 11:29:29 -04004326 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004327 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004328
James Smartbe858b62010-12-15 17:57:20 -05004329 /* Restore PCI cmd register */
4330 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4331
James Smart27b01b82012-05-09 21:19:44 -04004332 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004333}
4334
4335/**
4336 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004337 * @phba: Pointer to HBA context object.
4338 *
4339 * This function is called in the SLI initialization code path to
4340 * restart the HBA. The caller is not required to hold any lock.
4341 * This function writes MBX_RESTART mailbox command to the SLIM and
4342 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4343 * function to free any pending commands. The function enables
4344 * POST only during the first initialization. The function returns zero.
4345 * The function does not guarantee completion of MBX_RESTART mailbox
4346 * command before the return of this function.
4347 **/
James Smartda0436e2009-05-22 14:51:39 -04004348static int
4349lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004350{
4351 MAILBOX_t *mb;
4352 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004353 volatile uint32_t word0;
4354 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004355 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004356
James Smart2e0fef82007-06-17 19:56:36 -05004357 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004358
James Smart0d878412009-10-02 15:16:56 -04004359 /* Take PCIe device Advanced Error Reporting (AER) state */
4360 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4361
Jamie Wellnitz41415862006-02-28 19:25:27 -05004362 psli = &phba->sli;
4363
4364 /* Restart HBA */
4365 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004366 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004367 (phba->pport) ? phba->pport->port_state : 0,
4368 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004369
4370 word0 = 0;
4371 mb = (MAILBOX_t *) &word0;
4372 mb->mbxCommand = MBX_RESTART;
4373 mb->mbxHc = 1;
4374
James Smart92908312006-03-07 15:04:13 -05004375 lpfc_reset_barrier(phba);
4376
Jamie Wellnitz41415862006-02-28 19:25:27 -05004377 to_slim = phba->MBslimaddr;
4378 writel(*(uint32_t *) mb, to_slim);
4379 readl(to_slim); /* flush */
4380
4381 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004382 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004384 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004386 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004387 writel(*(uint32_t *) mb, to_slim);
4388 readl(to_slim); /* flush */
4389
4390 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004391 if (phba->pport)
4392 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004393 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004394 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004396
James Smart64ba8812006-08-02 15:24:34 -04004397 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4398 psli->stats_start = get_seconds();
4399
James Smarteaf15d52008-12-04 22:39:29 -05004400 /* Give the INITFF and Post time to settle. */
4401 mdelay(100);
dea31012005-04-17 16:05:31 -05004402
James Smart0d878412009-10-02 15:16:56 -04004403 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4404 if (hba_aer_enabled)
4405 pci_disable_pcie_error_reporting(phba->pcidev);
4406
Jamie Wellnitz41415862006-02-28 19:25:27 -05004407 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004408
4409 return 0;
4410}
4411
James Smarte59058c2008-08-24 21:49:00 -04004412/**
James Smartda0436e2009-05-22 14:51:39 -04004413 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4414 * @phba: Pointer to HBA context object.
4415 *
4416 * This function is called in the SLI initialization code path to restart
4417 * a SLI4 HBA. The caller is not required to hold any lock.
4418 * At the end of the function, it calls lpfc_hba_down_post function to
4419 * free any pending commands.
4420 **/
4421static int
4422lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4423{
4424 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004425 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004426 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004427
4428 /* Restart HBA */
4429 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4430 "0296 Restart HBA Data: x%x x%x\n",
4431 phba->pport->port_state, psli->sli_flag);
4432
James Smart75baf692010-06-08 18:31:21 -04004433 /* Take PCIe device Advanced Error Reporting (AER) state */
4434 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4435
James Smart27b01b82012-05-09 21:19:44 -04004436 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004437
4438 spin_lock_irq(&phba->hbalock);
4439 phba->pport->stopped = 0;
4440 phba->link_state = LPFC_INIT_START;
4441 phba->hba_flag = 0;
4442 spin_unlock_irq(&phba->hbalock);
4443
4444 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4445 psli->stats_start = get_seconds();
4446
James Smart75baf692010-06-08 18:31:21 -04004447 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4448 if (hba_aer_enabled)
4449 pci_disable_pcie_error_reporting(phba->pcidev);
4450
James Smartda0436e2009-05-22 14:51:39 -04004451 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004452 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004453
James Smart27b01b82012-05-09 21:19:44 -04004454 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004455}
4456
4457/**
4458 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4459 * @phba: Pointer to HBA context object.
4460 *
4461 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4462 * API jump table function pointer from the lpfc_hba struct.
4463**/
4464int
4465lpfc_sli_brdrestart(struct lpfc_hba *phba)
4466{
4467 return phba->lpfc_sli_brdrestart(phba);
4468}
4469
4470/**
James Smart3621a712009-04-06 18:47:14 -04004471 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004472 * @phba: Pointer to HBA context object.
4473 *
4474 * This function is called after a HBA restart to wait for successful
4475 * restart of the HBA. Successful restart of the HBA is indicated by
4476 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4477 * iteration, the function will restart the HBA again. The function returns
4478 * zero if HBA successfully restarted else returns negative error code.
4479 **/
James Smart4492b732017-04-27 15:08:26 -07004480int
dea31012005-04-17 16:05:31 -05004481lpfc_sli_chipset_init(struct lpfc_hba *phba)
4482{
4483 uint32_t status, i = 0;
4484
4485 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004486 if (lpfc_readl(phba->HSregaddr, &status))
4487 return -EIO;
dea31012005-04-17 16:05:31 -05004488
4489 /* Check status register to see what current state is */
4490 i = 0;
4491 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4492
James Smartdcf2a4e2010-09-29 11:18:53 -04004493 /* Check every 10ms for 10 retries, then every 100ms for 90
4494 * retries, then every 1 sec for 50 retires for a total of
4495 * ~60 seconds before reset the board again and check every
4496 * 1 sec for 50 retries. The up to 60 seconds before the
4497 * board ready is required by the Falcon FIPS zeroization
4498 * complete, and any reset the board in between shall cause
4499 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004500 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004501 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004502 /* Adapter failed to init, timeout, status reg
4503 <status> */
James Smarted957682007-06-17 19:56:37 -05004504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004505 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004506 "timeout, status reg x%x, "
4507 "FW Data: A8 x%x AC x%x\n", status,
4508 readl(phba->MBslimaddr + 0xa8),
4509 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004510 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004511 return -ETIMEDOUT;
4512 }
4513
4514 /* Check to see if any errors occurred during init */
4515 if (status & HS_FFERM) {
4516 /* ERROR: During chipset initialization */
4517 /* Adapter failed to init, chipset, status reg
4518 <status> */
James Smarted957682007-06-17 19:56:37 -05004519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004520 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004521 "chipset, status reg x%x, "
4522 "FW Data: A8 x%x AC x%x\n", status,
4523 readl(phba->MBslimaddr + 0xa8),
4524 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004525 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004526 return -EIO;
4527 }
4528
James Smartdcf2a4e2010-09-29 11:18:53 -04004529 if (i <= 10)
dea31012005-04-17 16:05:31 -05004530 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004531 else if (i <= 100)
4532 msleep(100);
4533 else
4534 msleep(1000);
dea31012005-04-17 16:05:31 -05004535
James Smartdcf2a4e2010-09-29 11:18:53 -04004536 if (i == 150) {
4537 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004538 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004539 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004540 }
4541 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004542 if (lpfc_readl(phba->HSregaddr, &status))
4543 return -EIO;
dea31012005-04-17 16:05:31 -05004544 }
4545
4546 /* Check to see if any errors occurred during init */
4547 if (status & HS_FFERM) {
4548 /* ERROR: During chipset initialization */
4549 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004551 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004552 "status reg x%x, "
4553 "FW Data: A8 x%x AC x%x\n", status,
4554 readl(phba->MBslimaddr + 0xa8),
4555 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004556 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004557 return -EIO;
4558 }
4559
4560 /* Clear all interrupt enable conditions */
4561 writel(0, phba->HCregaddr);
4562 readl(phba->HCregaddr); /* flush */
4563
4564 /* setup host attn register */
4565 writel(0xffffffff, phba->HAregaddr);
4566 readl(phba->HAregaddr); /* flush */
4567 return 0;
4568}
4569
James Smarte59058c2008-08-24 21:49:00 -04004570/**
James Smart3621a712009-04-06 18:47:14 -04004571 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004572 *
4573 * This function calculates and returns the number of HBQs required to be
4574 * configured.
4575 **/
James Smart78b2d852007-08-02 11:10:21 -04004576int
James Smarted957682007-06-17 19:56:37 -05004577lpfc_sli_hbq_count(void)
4578{
James Smart92d7f7b2007-06-17 19:56:38 -05004579 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004580}
4581
James Smarte59058c2008-08-24 21:49:00 -04004582/**
James Smart3621a712009-04-06 18:47:14 -04004583 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004584 *
4585 * This function adds the number of hbq entries in every HBQ to get
4586 * the total number of hbq entries required for the HBA and returns
4587 * the total count.
4588 **/
James Smarted957682007-06-17 19:56:37 -05004589static int
4590lpfc_sli_hbq_entry_count(void)
4591{
4592 int hbq_count = lpfc_sli_hbq_count();
4593 int count = 0;
4594 int i;
4595
4596 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004597 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004598 return count;
4599}
4600
James Smarte59058c2008-08-24 21:49:00 -04004601/**
James Smart3621a712009-04-06 18:47:14 -04004602 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004603 *
4604 * This function calculates amount of memory required for all hbq entries
4605 * to be configured and returns the total memory required.
4606 **/
dea31012005-04-17 16:05:31 -05004607int
James Smarted957682007-06-17 19:56:37 -05004608lpfc_sli_hbq_size(void)
4609{
4610 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4611}
4612
James Smarte59058c2008-08-24 21:49:00 -04004613/**
James Smart3621a712009-04-06 18:47:14 -04004614 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004615 * @phba: Pointer to HBA context object.
4616 *
4617 * This function is called during the SLI initialization to configure
4618 * all the HBQs and post buffers to the HBQ. The caller is not
4619 * required to hold any locks. This function will return zero if successful
4620 * else it will return negative error code.
4621 **/
James Smarted957682007-06-17 19:56:37 -05004622static int
4623lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4624{
4625 int hbq_count = lpfc_sli_hbq_count();
4626 LPFC_MBOXQ_t *pmb;
4627 MAILBOX_t *pmbox;
4628 uint32_t hbqno;
4629 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004630
James Smart92d7f7b2007-06-17 19:56:38 -05004631 /* Get a Mailbox buffer to setup mailbox
4632 * commands for HBA initialization
4633 */
James Smarted957682007-06-17 19:56:37 -05004634 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4635
4636 if (!pmb)
4637 return -ENOMEM;
4638
James Smart04c68492009-05-22 14:52:52 -04004639 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004640
4641 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4642 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004643 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004644
4645 hbq_entry_index = 0;
4646 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4647 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4648 phba->hbqs[hbqno].hbqPutIdx = 0;
4649 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4650 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004651 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004652 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4653 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004654 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4655
4656 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4657 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4658 mbxStatus <status>, ring <num> */
4659
4660 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004661 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004662 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004663 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004664 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004665 pmbox->mbxStatus, hbqno);
4666
4667 phba->link_state = LPFC_HBA_ERROR;
4668 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004669 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004670 }
4671 }
4672 phba->hbq_count = hbq_count;
4673
James Smarted957682007-06-17 19:56:37 -05004674 mempool_free(pmb, phba->mbox_mem_pool);
4675
James Smart92d7f7b2007-06-17 19:56:38 -05004676 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004677 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4678 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004679 return 0;
4680}
4681
James Smarte59058c2008-08-24 21:49:00 -04004682/**
James Smart4f774512009-05-22 14:52:35 -04004683 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4684 * @phba: Pointer to HBA context object.
4685 *
4686 * This function is called during the SLI initialization to configure
4687 * all the HBQs and post buffers to the HBQ. The caller is not
4688 * required to hold any locks. This function will return zero if successful
4689 * else it will return negative error code.
4690 **/
4691static int
4692lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4693{
4694 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004695 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4696 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004697 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004698 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004699 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004700 return 0;
4701}
4702
4703/**
James Smart3621a712009-04-06 18:47:14 -04004704 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004705 * @phba: Pointer to HBA context object.
4706 * @sli_mode: sli mode - 2/3
4707 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004708 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004709 * to issue config_port mailbox command. This function restarts the
4710 * HBA firmware and issues a config_port mailbox command to configure
4711 * the SLI interface in the sli mode specified by sli_mode
4712 * variable. The caller is not required to hold any locks.
4713 * The function returns 0 if successful, else returns negative error
4714 * code.
4715 **/
James Smart93996272008-08-24 21:50:30 -04004716int
4717lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004718{
4719 LPFC_MBOXQ_t *pmb;
4720 uint32_t resetcount = 0, rc = 0, done = 0;
4721
4722 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4723 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004724 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004725 return -ENOMEM;
4726 }
4727
James Smarted957682007-06-17 19:56:37 -05004728 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004729 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004730 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004731 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004732 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004733 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004734 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004735 rc = lpfc_sli_chipset_init(phba);
4736 if (rc)
4737 break;
4738
James Smart2e0fef82007-06-17 19:56:36 -05004739 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004740 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004741 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004742 resetcount++;
4743
James Smarted957682007-06-17 19:56:37 -05004744 /* Call pre CONFIG_PORT mailbox command initialization. A
4745 * value of 0 means the call was successful. Any other
4746 * nonzero value is a failure, but if ERESTART is returned,
4747 * the driver may reset the HBA and try again.
4748 */
dea31012005-04-17 16:05:31 -05004749 rc = lpfc_config_port_prep(phba);
4750 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004751 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004752 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004753 } else if (rc)
dea31012005-04-17 16:05:31 -05004754 break;
James Smart6d368e52011-05-24 11:44:12 -04004755
James Smart2e0fef82007-06-17 19:56:36 -05004756 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004757 lpfc_config_port(phba, pmb);
4758 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004759 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4760 LPFC_SLI3_HBQ_ENABLED |
4761 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004762 LPFC_SLI3_BG_ENABLED |
4763 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004764 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004766 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004767 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004768 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004769 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004770 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004771 spin_unlock_irq(&phba->hbalock);
4772 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004773 } else {
4774 /* Allow asynchronous mailbox command to go through */
4775 spin_lock_irq(&phba->hbalock);
4776 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4777 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004778 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004779
4780 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4781 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4782 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4783 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004784 }
dea31012005-04-17 16:05:31 -05004785 }
James Smarted957682007-06-17 19:56:37 -05004786 if (!done) {
4787 rc = -EINVAL;
4788 goto do_prep_failed;
4789 }
James Smart04c68492009-05-22 14:52:52 -04004790 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4791 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004792 rc = -ENXIO;
4793 goto do_prep_failed;
4794 }
James Smart04c68492009-05-22 14:52:52 -04004795 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004796 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004797 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4798 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4799 phba->max_vpi : phba->max_vports;
4800
James Smart34b02dc2008-08-24 21:49:55 -04004801 } else
4802 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004803 phba->fips_level = 0;
4804 phba->fips_spec_rev = 0;
4805 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004806 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004807 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4808 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4810 "2850 Security Crypto Active. FIPS x%d "
4811 "(Spec Rev: x%d)",
4812 phba->fips_level, phba->fips_spec_rev);
4813 }
4814 if (pmb->u.mb.un.varCfgPort.sec_err) {
4815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4816 "2856 Config Port Security Crypto "
4817 "Error: x%x ",
4818 pmb->u.mb.un.varCfgPort.sec_err);
4819 }
James Smart04c68492009-05-22 14:52:52 -04004820 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004821 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004822 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004823 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004824
4825 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4826 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004827
4828 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004829 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004830 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4831 else
4832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4833 "0443 Adapter did not grant "
4834 "BlockGuard\n");
4835 }
James Smart34b02dc2008-08-24 21:49:55 -04004836 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004837 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004838 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004839 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004840 }
James Smart92d7f7b2007-06-17 19:56:38 -05004841do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004842 mempool_free(pmb, phba->mbox_mem_pool);
4843 return rc;
4844}
4845
James Smarte59058c2008-08-24 21:49:00 -04004846
4847/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004848 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004849 * @phba: Pointer to HBA context object.
4850 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004851 * This function is the main SLI initialization function. This function
4852 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004853 * error attention handler code. Caller is not required to hold any
4854 * locks. This function issues config_port mailbox command to configure
4855 * the SLI, setup iocb rings and HBQ rings. In the end the function
4856 * calls the config_port_post function to issue init_link mailbox
4857 * command and to start the discovery. The function will return zero
4858 * if successful, else it will return negative error code.
4859 **/
James Smarted957682007-06-17 19:56:37 -05004860int
4861lpfc_sli_hba_setup(struct lpfc_hba *phba)
4862{
4863 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004864 int mode = 3, i;
4865 int longs;
James Smarted957682007-06-17 19:56:37 -05004866
James Smart12247e82016-07-06 12:36:09 -07004867 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004868 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004869 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004871 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004872 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004873 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004874 break;
4875 }
James Smarted957682007-06-17 19:56:37 -05004876 mode = 2;
4877 break;
4878 case 0:
4879 case 3:
4880 break;
4881 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004883 "1819 Unrecognized sli_mode parameter: %d.\n",
4884 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004885
4886 break;
4887 }
James Smartb5c53952016-03-31 14:12:30 -07004888 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004889
James Smart93996272008-08-24 21:50:30 -04004890 rc = lpfc_sli_config_port(phba, mode);
4891
James Smart12247e82016-07-06 12:36:09 -07004892 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004894 "1820 Unable to select SLI-3. "
4895 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004896 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004897 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004898 else if (rc && mode == 2)
4899 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004900 if (rc)
dea31012005-04-17 16:05:31 -05004901 goto lpfc_sli_hba_setup_error;
4902
James Smart0d878412009-10-02 15:16:56 -04004903 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4904 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4905 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4906 if (!rc) {
4907 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4908 "2709 This device supports "
4909 "Advanced Error Reporting (AER)\n");
4910 spin_lock_irq(&phba->hbalock);
4911 phba->hba_flag |= HBA_AER_ENABLED;
4912 spin_unlock_irq(&phba->hbalock);
4913 } else {
4914 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4915 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004916 "Advanced Error Reporting (AER): %d\n",
4917 rc);
James Smart0d878412009-10-02 15:16:56 -04004918 phba->cfg_aer_support = 0;
4919 }
4920 }
4921
James Smarted957682007-06-17 19:56:37 -05004922 if (phba->sli_rev == 3) {
4923 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4924 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004925 } else {
4926 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4927 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004928 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004929 }
4930
4931 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004932 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4933 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004934 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004935
4936 if (rc)
4937 goto lpfc_sli_hba_setup_error;
4938
James Smart6d368e52011-05-24 11:44:12 -04004939 /* Initialize VPIs. */
4940 if (phba->sli_rev == LPFC_SLI_REV3) {
4941 /*
4942 * The VPI bitmask and physical ID array are allocated
4943 * and initialized once only - at driver load. A port
4944 * reset doesn't need to reinitialize this memory.
4945 */
4946 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4947 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4948 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4949 GFP_KERNEL);
4950 if (!phba->vpi_bmask) {
4951 rc = -ENOMEM;
4952 goto lpfc_sli_hba_setup_error;
4953 }
4954
4955 phba->vpi_ids = kzalloc(
4956 (phba->max_vpi+1) * sizeof(uint16_t),
4957 GFP_KERNEL);
4958 if (!phba->vpi_ids) {
4959 kfree(phba->vpi_bmask);
4960 rc = -ENOMEM;
4961 goto lpfc_sli_hba_setup_error;
4962 }
4963 for (i = 0; i < phba->max_vpi; i++)
4964 phba->vpi_ids[i] = i;
4965 }
4966 }
4967
James Smart93996272008-08-24 21:50:30 -04004968 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004969 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4970 rc = lpfc_sli_hbq_setup(phba);
4971 if (rc)
4972 goto lpfc_sli_hba_setup_error;
4973 }
James Smart04c68492009-05-22 14:52:52 -04004974 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004975 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004976 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004977
4978 rc = lpfc_config_port_post(phba);
4979 if (rc)
4980 goto lpfc_sli_hba_setup_error;
4981
James Smarted957682007-06-17 19:56:37 -05004982 return rc;
4983
James Smart92d7f7b2007-06-17 19:56:38 -05004984lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004985 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004987 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004988 return rc;
4989}
4990
James Smartda0436e2009-05-22 14:51:39 -04004991/**
4992 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4993 * @phba: Pointer to HBA context object.
4994 * @mboxq: mailbox pointer.
4995 * This function issue a dump mailbox command to read config region
4996 * 23 and parse the records in the region and populate driver
4997 * data structure.
4998 **/
4999static int
James Smartff78d8f2011-12-13 13:21:35 -05005000lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005001{
James Smartff78d8f2011-12-13 13:21:35 -05005002 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005003 struct lpfc_dmabuf *mp;
5004 struct lpfc_mqe *mqe;
5005 uint32_t data_length;
5006 int rc;
5007
5008 /* Program the default value of vlan_id and fc_map */
5009 phba->valid_vlan = 0;
5010 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5011 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5012 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5013
James Smartff78d8f2011-12-13 13:21:35 -05005014 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5015 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005016 return -ENOMEM;
5017
James Smartff78d8f2011-12-13 13:21:35 -05005018 mqe = &mboxq->u.mqe;
5019 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5020 rc = -ENOMEM;
5021 goto out_free_mboxq;
5022 }
5023
James Smartda0436e2009-05-22 14:51:39 -04005024 mp = (struct lpfc_dmabuf *) mboxq->context1;
5025 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5026
5027 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5028 "(%d):2571 Mailbox cmd x%x Status x%x "
5029 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5030 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5031 "CQ: x%x x%x x%x x%x\n",
5032 mboxq->vport ? mboxq->vport->vpi : 0,
5033 bf_get(lpfc_mqe_command, mqe),
5034 bf_get(lpfc_mqe_status, mqe),
5035 mqe->un.mb_words[0], mqe->un.mb_words[1],
5036 mqe->un.mb_words[2], mqe->un.mb_words[3],
5037 mqe->un.mb_words[4], mqe->un.mb_words[5],
5038 mqe->un.mb_words[6], mqe->un.mb_words[7],
5039 mqe->un.mb_words[8], mqe->un.mb_words[9],
5040 mqe->un.mb_words[10], mqe->un.mb_words[11],
5041 mqe->un.mb_words[12], mqe->un.mb_words[13],
5042 mqe->un.mb_words[14], mqe->un.mb_words[15],
5043 mqe->un.mb_words[16], mqe->un.mb_words[50],
5044 mboxq->mcqe.word0,
5045 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5046 mboxq->mcqe.trailer);
5047
5048 if (rc) {
5049 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5050 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005051 rc = -EIO;
5052 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005053 }
5054 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005055 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005056 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5057 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005058 rc = -EIO;
5059 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005060 }
James Smartda0436e2009-05-22 14:51:39 -04005061
5062 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5063 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5064 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005065 rc = 0;
5066
5067out_free_mboxq:
5068 mempool_free(mboxq, phba->mbox_mem_pool);
5069 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005070}
5071
5072/**
5073 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5074 * @phba: pointer to lpfc hba data structure.
5075 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5076 * @vpd: pointer to the memory to hold resulting port vpd data.
5077 * @vpd_size: On input, the number of bytes allocated to @vpd.
5078 * On output, the number of data bytes in @vpd.
5079 *
5080 * This routine executes a READ_REV SLI4 mailbox command. In
5081 * addition, this routine gets the port vpd data.
5082 *
5083 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005084 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005085 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005086 **/
5087static int
5088lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5089 uint8_t *vpd, uint32_t *vpd_size)
5090{
5091 int rc = 0;
5092 uint32_t dma_size;
5093 struct lpfc_dmabuf *dmabuf;
5094 struct lpfc_mqe *mqe;
5095
5096 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5097 if (!dmabuf)
5098 return -ENOMEM;
5099
5100 /*
5101 * Get a DMA buffer for the vpd data resulting from the READ_REV
5102 * mailbox command.
5103 */
5104 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005105 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5106 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005107 if (!dmabuf->virt) {
5108 kfree(dmabuf);
5109 return -ENOMEM;
5110 }
James Smartda0436e2009-05-22 14:51:39 -04005111
5112 /*
5113 * The SLI4 implementation of READ_REV conflicts at word1,
5114 * bits 31:16 and SLI4 adds vpd functionality not present
5115 * in SLI3. This code corrects the conflicts.
5116 */
5117 lpfc_read_rev(phba, mboxq);
5118 mqe = &mboxq->u.mqe;
5119 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5120 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5121 mqe->un.read_rev.word1 &= 0x0000FFFF;
5122 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5123 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5124
5125 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5126 if (rc) {
5127 dma_free_coherent(&phba->pcidev->dev, dma_size,
5128 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005129 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005130 return -EIO;
5131 }
5132
James Smartda0436e2009-05-22 14:51:39 -04005133 /*
5134 * The available vpd length cannot be bigger than the
5135 * DMA buffer passed to the port. Catch the less than
5136 * case and update the caller's size.
5137 */
5138 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5139 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5140
James Smartd7c47992010-06-08 18:31:54 -04005141 memcpy(vpd, dmabuf->virt, *vpd_size);
5142
James Smartda0436e2009-05-22 14:51:39 -04005143 dma_free_coherent(&phba->pcidev->dev, dma_size,
5144 dmabuf->virt, dmabuf->phys);
5145 kfree(dmabuf);
5146 return 0;
5147}
5148
5149/**
James Smartcd1c8302011-10-10 21:33:25 -04005150 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5151 * @phba: pointer to lpfc hba data structure.
5152 *
5153 * This routine retrieves SLI4 device physical port name this PCI function
5154 * is attached to.
5155 *
5156 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005157 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005158 * otherwise - failed to retrieve physical port name
5159 **/
5160static int
5161lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5162{
5163 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005164 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5165 struct lpfc_controller_attribute *cntl_attr;
5166 struct lpfc_mbx_get_port_name *get_port_name;
5167 void *virtaddr = NULL;
5168 uint32_t alloclen, reqlen;
5169 uint32_t shdr_status, shdr_add_status;
5170 union lpfc_sli4_cfg_shdr *shdr;
5171 char cport_name = 0;
5172 int rc;
5173
5174 /* We assume nothing at this point */
5175 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5176 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5177
5178 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5179 if (!mboxq)
5180 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005181 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005182 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5183 lpfc_sli4_read_config(phba);
5184 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5185 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005186
5187 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5188 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5189 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5190 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5191 LPFC_SLI4_MBX_NEMBED);
5192 if (alloclen < reqlen) {
5193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5194 "3084 Allocated DMA memory size (%d) is "
5195 "less than the requested DMA memory size "
5196 "(%d)\n", alloclen, reqlen);
5197 rc = -ENOMEM;
5198 goto out_free_mboxq;
5199 }
5200 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5201 virtaddr = mboxq->sge_array->addr[0];
5202 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5203 shdr = &mbx_cntl_attr->cfg_shdr;
5204 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5205 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5206 if (shdr_status || shdr_add_status || rc) {
5207 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5208 "3085 Mailbox x%x (x%x/x%x) failed, "
5209 "rc:x%x, status:x%x, add_status:x%x\n",
5210 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5211 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5212 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5213 rc, shdr_status, shdr_add_status);
5214 rc = -ENXIO;
5215 goto out_free_mboxq;
5216 }
5217 cntl_attr = &mbx_cntl_attr->cntl_attr;
5218 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5219 phba->sli4_hba.lnk_info.lnk_tp =
5220 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5221 phba->sli4_hba.lnk_info.lnk_no =
5222 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5223 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5224 "3086 lnk_type:%d, lnk_numb:%d\n",
5225 phba->sli4_hba.lnk_info.lnk_tp,
5226 phba->sli4_hba.lnk_info.lnk_no);
5227
5228retrieve_ppname:
5229 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5230 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5231 sizeof(struct lpfc_mbx_get_port_name) -
5232 sizeof(struct lpfc_sli4_cfg_mhdr),
5233 LPFC_SLI4_MBX_EMBED);
5234 get_port_name = &mboxq->u.mqe.un.get_port_name;
5235 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5236 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5237 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5238 phba->sli4_hba.lnk_info.lnk_tp);
5239 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5240 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5241 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5242 if (shdr_status || shdr_add_status || rc) {
5243 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5244 "3087 Mailbox x%x (x%x/x%x) failed: "
5245 "rc:x%x, status:x%x, add_status:x%x\n",
5246 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5247 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5248 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5249 rc, shdr_status, shdr_add_status);
5250 rc = -ENXIO;
5251 goto out_free_mboxq;
5252 }
5253 switch (phba->sli4_hba.lnk_info.lnk_no) {
5254 case LPFC_LINK_NUMBER_0:
5255 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5256 &get_port_name->u.response);
5257 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5258 break;
5259 case LPFC_LINK_NUMBER_1:
5260 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5261 &get_port_name->u.response);
5262 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5263 break;
5264 case LPFC_LINK_NUMBER_2:
5265 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5266 &get_port_name->u.response);
5267 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5268 break;
5269 case LPFC_LINK_NUMBER_3:
5270 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5271 &get_port_name->u.response);
5272 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5273 break;
5274 default:
5275 break;
5276 }
5277
5278 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5279 phba->Port[0] = cport_name;
5280 phba->Port[1] = '\0';
5281 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5282 "3091 SLI get port name: %s\n", phba->Port);
5283 }
5284
5285out_free_mboxq:
5286 if (rc != MBX_TIMEOUT) {
5287 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5288 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5289 else
5290 mempool_free(mboxq, phba->mbox_mem_pool);
5291 }
5292 return rc;
5293}
5294
5295/**
James Smartda0436e2009-05-22 14:51:39 -04005296 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5297 * @phba: pointer to lpfc hba data structure.
5298 *
5299 * This routine is called to explicitly arm the SLI4 device's completion and
5300 * event queues
5301 **/
5302static void
5303lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5304{
James Smart895427b2017-02-12 13:52:30 -08005305 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005306
5307 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5308 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005309 if (phba->sli4_hba.nvmels_cq)
5310 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5311 LPFC_QUEUE_REARM);
5312
5313 if (phba->sli4_hba.fcp_cq)
5314 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5315 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5316 LPFC_QUEUE_REARM);
5317
5318 if (phba->sli4_hba.nvme_cq)
5319 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5320 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5321 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005322
James Smartf38fa0b2014-04-04 13:52:21 -04005323 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005324 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5325
James Smart895427b2017-02-12 13:52:30 -08005326 if (phba->sli4_hba.hba_eq)
5327 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5328 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5329 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005330
James Smart2d7dbc42017-02-12 13:52:35 -08005331 if (phba->nvmet_support) {
5332 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5333 lpfc_sli4_cq_release(
5334 phba->sli4_hba.nvmet_cqset[qidx],
5335 LPFC_QUEUE_REARM);
5336 }
James Smart2e90f4b2011-12-13 13:22:37 -05005337 }
James Smart1ba981f2014-02-20 09:56:45 -05005338
5339 if (phba->cfg_fof)
5340 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005341}
5342
5343/**
James Smart6d368e52011-05-24 11:44:12 -04005344 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5345 * @phba: Pointer to HBA context object.
5346 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005347 * @extnt_count: buffer to hold port available extent count.
5348 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005349 *
James Smartb76f2dc2011-07-22 18:37:42 -04005350 * This function calls the port and retrievs the number of available
5351 * extents and their size for a particular extent type.
5352 *
5353 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005354 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005355int
James Smart6d368e52011-05-24 11:44:12 -04005356lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5357 uint16_t *extnt_count, uint16_t *extnt_size)
5358{
5359 int rc = 0;
5360 uint32_t length;
5361 uint32_t mbox_tmo;
5362 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5363 LPFC_MBOXQ_t *mbox;
5364
5365 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5366 if (!mbox)
5367 return -ENOMEM;
5368
5369 /* Find out how many extents are available for this resource type */
5370 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5371 sizeof(struct lpfc_sli4_cfg_mhdr));
5372 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5373 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5374 length, LPFC_SLI4_MBX_EMBED);
5375
5376 /* Send an extents count of 0 - the GET doesn't use it. */
5377 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5378 LPFC_SLI4_MBX_EMBED);
5379 if (unlikely(rc)) {
5380 rc = -EIO;
5381 goto err_exit;
5382 }
5383
5384 if (!phba->sli4_hba.intr_enable)
5385 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5386 else {
James Smarta183a152011-10-10 21:32:43 -04005387 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005388 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5389 }
5390 if (unlikely(rc)) {
5391 rc = -EIO;
5392 goto err_exit;
5393 }
5394
5395 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5396 if (bf_get(lpfc_mbox_hdr_status,
5397 &rsrc_info->header.cfg_shdr.response)) {
5398 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5399 "2930 Failed to get resource extents "
5400 "Status 0x%x Add'l Status 0x%x\n",
5401 bf_get(lpfc_mbox_hdr_status,
5402 &rsrc_info->header.cfg_shdr.response),
5403 bf_get(lpfc_mbox_hdr_add_status,
5404 &rsrc_info->header.cfg_shdr.response));
5405 rc = -EIO;
5406 goto err_exit;
5407 }
5408
5409 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5410 &rsrc_info->u.rsp);
5411 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5412 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005413
5414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5415 "3162 Retrieved extents type-%d from port: count:%d, "
5416 "size:%d\n", type, *extnt_count, *extnt_size);
5417
5418err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005419 mempool_free(mbox, phba->mbox_mem_pool);
5420 return rc;
5421}
5422
5423/**
5424 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5425 * @phba: Pointer to HBA context object.
5426 * @type: The extent type to check.
5427 *
5428 * This function reads the current available extents from the port and checks
5429 * if the extent count or extent size has changed since the last access.
5430 * Callers use this routine post port reset to understand if there is a
5431 * extent reprovisioning requirement.
5432 *
5433 * Returns:
5434 * -Error: error indicates problem.
5435 * 1: Extent count or size has changed.
5436 * 0: No changes.
5437 **/
5438static int
5439lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5440{
5441 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5442 uint16_t size_diff, rsrc_ext_size;
5443 int rc = 0;
5444 struct lpfc_rsrc_blks *rsrc_entry;
5445 struct list_head *rsrc_blk_list = NULL;
5446
5447 size_diff = 0;
5448 curr_ext_cnt = 0;
5449 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5450 &rsrc_ext_cnt,
5451 &rsrc_ext_size);
5452 if (unlikely(rc))
5453 return -EIO;
5454
5455 switch (type) {
5456 case LPFC_RSC_TYPE_FCOE_RPI:
5457 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5458 break;
5459 case LPFC_RSC_TYPE_FCOE_VPI:
5460 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5461 break;
5462 case LPFC_RSC_TYPE_FCOE_XRI:
5463 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5464 break;
5465 case LPFC_RSC_TYPE_FCOE_VFI:
5466 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5467 break;
5468 default:
5469 break;
5470 }
5471
5472 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5473 curr_ext_cnt++;
5474 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5475 size_diff++;
5476 }
5477
5478 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5479 rc = 1;
5480
5481 return rc;
5482}
5483
5484/**
5485 * lpfc_sli4_cfg_post_extnts -
5486 * @phba: Pointer to HBA context object.
5487 * @extnt_cnt - number of available extents.
5488 * @type - the extent type (rpi, xri, vfi, vpi).
5489 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5490 * @mbox - pointer to the caller's allocated mailbox structure.
5491 *
5492 * This function executes the extents allocation request. It also
5493 * takes care of the amount of memory needed to allocate or get the
5494 * allocated extents. It is the caller's responsibility to evaluate
5495 * the response.
5496 *
5497 * Returns:
5498 * -Error: Error value describes the condition found.
5499 * 0: if successful
5500 **/
5501static int
James Smart8a9d2e82012-05-09 21:16:12 -04005502lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005503 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5504{
5505 int rc = 0;
5506 uint32_t req_len;
5507 uint32_t emb_len;
5508 uint32_t alloc_len, mbox_tmo;
5509
5510 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005511 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005512
5513 /*
5514 * Calculate the size of an embedded mailbox. The uint32_t
5515 * accounts for extents-specific word.
5516 */
5517 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5518 sizeof(uint32_t);
5519
5520 /*
5521 * Presume the allocation and response will fit into an embedded
5522 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5523 */
5524 *emb = LPFC_SLI4_MBX_EMBED;
5525 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005526 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005527 sizeof(union lpfc_sli4_cfg_shdr) +
5528 sizeof(uint32_t);
5529 *emb = LPFC_SLI4_MBX_NEMBED;
5530 }
5531
5532 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5533 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5534 req_len, *emb);
5535 if (alloc_len < req_len) {
5536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005537 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005538 "less than the requested DMA memory "
5539 "size (x%x)\n", alloc_len, req_len);
5540 return -ENOMEM;
5541 }
James Smart8a9d2e82012-05-09 21:16:12 -04005542 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005543 if (unlikely(rc))
5544 return -EIO;
5545
5546 if (!phba->sli4_hba.intr_enable)
5547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5548 else {
James Smarta183a152011-10-10 21:32:43 -04005549 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005550 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5551 }
5552
5553 if (unlikely(rc))
5554 rc = -EIO;
5555 return rc;
5556}
5557
5558/**
5559 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5560 * @phba: Pointer to HBA context object.
5561 * @type: The resource extent type to allocate.
5562 *
5563 * This function allocates the number of elements for the specified
5564 * resource type.
5565 **/
5566static int
5567lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5568{
5569 bool emb = false;
5570 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5571 uint16_t rsrc_id, rsrc_start, j, k;
5572 uint16_t *ids;
5573 int i, rc;
5574 unsigned long longs;
5575 unsigned long *bmask;
5576 struct lpfc_rsrc_blks *rsrc_blks;
5577 LPFC_MBOXQ_t *mbox;
5578 uint32_t length;
5579 struct lpfc_id_range *id_array = NULL;
5580 void *virtaddr = NULL;
5581 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5582 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5583 struct list_head *ext_blk_list;
5584
5585 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5586 &rsrc_cnt,
5587 &rsrc_size);
5588 if (unlikely(rc))
5589 return -EIO;
5590
5591 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5592 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5593 "3009 No available Resource Extents "
5594 "for resource type 0x%x: Count: 0x%x, "
5595 "Size 0x%x\n", type, rsrc_cnt,
5596 rsrc_size);
5597 return -ENOMEM;
5598 }
5599
James Smart8a9d2e82012-05-09 21:16:12 -04005600 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5601 "2903 Post resource extents type-0x%x: "
5602 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005603
5604 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5605 if (!mbox)
5606 return -ENOMEM;
5607
James Smart8a9d2e82012-05-09 21:16:12 -04005608 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005609 if (unlikely(rc)) {
5610 rc = -EIO;
5611 goto err_exit;
5612 }
5613
5614 /*
5615 * Figure out where the response is located. Then get local pointers
5616 * to the response data. The port does not guarantee to respond to
5617 * all extents counts request so update the local variable with the
5618 * allocated count from the port.
5619 */
5620 if (emb == LPFC_SLI4_MBX_EMBED) {
5621 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5622 id_array = &rsrc_ext->u.rsp.id[0];
5623 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5624 } else {
5625 virtaddr = mbox->sge_array->addr[0];
5626 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5627 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5628 id_array = &n_rsrc->id;
5629 }
5630
5631 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5632 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5633
5634 /*
5635 * Based on the resource size and count, correct the base and max
5636 * resource values.
5637 */
5638 length = sizeof(struct lpfc_rsrc_blks);
5639 switch (type) {
5640 case LPFC_RSC_TYPE_FCOE_RPI:
5641 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5642 sizeof(unsigned long),
5643 GFP_KERNEL);
5644 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5645 rc = -ENOMEM;
5646 goto err_exit;
5647 }
5648 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5649 sizeof(uint16_t),
5650 GFP_KERNEL);
5651 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5652 kfree(phba->sli4_hba.rpi_bmask);
5653 rc = -ENOMEM;
5654 goto err_exit;
5655 }
5656
5657 /*
5658 * The next_rpi was initialized with the maximum available
5659 * count but the port may allocate a smaller number. Catch
5660 * that case and update the next_rpi.
5661 */
5662 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5663
5664 /* Initialize local ptrs for common extent processing later. */
5665 bmask = phba->sli4_hba.rpi_bmask;
5666 ids = phba->sli4_hba.rpi_ids;
5667 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5668 break;
5669 case LPFC_RSC_TYPE_FCOE_VPI:
5670 phba->vpi_bmask = kzalloc(longs *
5671 sizeof(unsigned long),
5672 GFP_KERNEL);
5673 if (unlikely(!phba->vpi_bmask)) {
5674 rc = -ENOMEM;
5675 goto err_exit;
5676 }
5677 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5678 sizeof(uint16_t),
5679 GFP_KERNEL);
5680 if (unlikely(!phba->vpi_ids)) {
5681 kfree(phba->vpi_bmask);
5682 rc = -ENOMEM;
5683 goto err_exit;
5684 }
5685
5686 /* Initialize local ptrs for common extent processing later. */
5687 bmask = phba->vpi_bmask;
5688 ids = phba->vpi_ids;
5689 ext_blk_list = &phba->lpfc_vpi_blk_list;
5690 break;
5691 case LPFC_RSC_TYPE_FCOE_XRI:
5692 phba->sli4_hba.xri_bmask = kzalloc(longs *
5693 sizeof(unsigned long),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5696 rc = -ENOMEM;
5697 goto err_exit;
5698 }
James Smart8a9d2e82012-05-09 21:16:12 -04005699 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005700 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5701 sizeof(uint16_t),
5702 GFP_KERNEL);
5703 if (unlikely(!phba->sli4_hba.xri_ids)) {
5704 kfree(phba->sli4_hba.xri_bmask);
5705 rc = -ENOMEM;
5706 goto err_exit;
5707 }
5708
5709 /* Initialize local ptrs for common extent processing later. */
5710 bmask = phba->sli4_hba.xri_bmask;
5711 ids = phba->sli4_hba.xri_ids;
5712 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5713 break;
5714 case LPFC_RSC_TYPE_FCOE_VFI:
5715 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5716 sizeof(unsigned long),
5717 GFP_KERNEL);
5718 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5719 rc = -ENOMEM;
5720 goto err_exit;
5721 }
5722 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5723 sizeof(uint16_t),
5724 GFP_KERNEL);
5725 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5726 kfree(phba->sli4_hba.vfi_bmask);
5727 rc = -ENOMEM;
5728 goto err_exit;
5729 }
5730
5731 /* Initialize local ptrs for common extent processing later. */
5732 bmask = phba->sli4_hba.vfi_bmask;
5733 ids = phba->sli4_hba.vfi_ids;
5734 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5735 break;
5736 default:
5737 /* Unsupported Opcode. Fail call. */
5738 id_array = NULL;
5739 bmask = NULL;
5740 ids = NULL;
5741 ext_blk_list = NULL;
5742 goto err_exit;
5743 }
5744
5745 /*
5746 * Complete initializing the extent configuration with the
5747 * allocated ids assigned to this function. The bitmask serves
5748 * as an index into the array and manages the available ids. The
5749 * array just stores the ids communicated to the port via the wqes.
5750 */
5751 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5752 if ((i % 2) == 0)
5753 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5754 &id_array[k]);
5755 else
5756 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5757 &id_array[k]);
5758
5759 rsrc_blks = kzalloc(length, GFP_KERNEL);
5760 if (unlikely(!rsrc_blks)) {
5761 rc = -ENOMEM;
5762 kfree(bmask);
5763 kfree(ids);
5764 goto err_exit;
5765 }
5766 rsrc_blks->rsrc_start = rsrc_id;
5767 rsrc_blks->rsrc_size = rsrc_size;
5768 list_add_tail(&rsrc_blks->list, ext_blk_list);
5769 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005770 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005771 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005772 lpfc_sli4_get_iocb_cnt(phba);
5773 phba->sli4_hba.nvme_xri_start =
5774 phba->sli4_hba.scsi_xri_start +
5775 phba->sli4_hba.scsi_xri_max;
5776 }
James Smart6d368e52011-05-24 11:44:12 -04005777
5778 while (rsrc_id < (rsrc_start + rsrc_size)) {
5779 ids[j] = rsrc_id;
5780 rsrc_id++;
5781 j++;
5782 }
5783 /* Entire word processed. Get next word.*/
5784 if ((i % 2) == 1)
5785 k++;
5786 }
5787 err_exit:
5788 lpfc_sli4_mbox_cmd_free(phba, mbox);
5789 return rc;
5790}
5791
James Smart895427b2017-02-12 13:52:30 -08005792
5793
James Smart6d368e52011-05-24 11:44:12 -04005794/**
5795 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5796 * @phba: Pointer to HBA context object.
5797 * @type: the extent's type.
5798 *
5799 * This function deallocates all extents of a particular resource type.
5800 * SLI4 does not allow for deallocating a particular extent range. It
5801 * is the caller's responsibility to release all kernel memory resources.
5802 **/
5803static int
5804lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5805{
5806 int rc;
5807 uint32_t length, mbox_tmo = 0;
5808 LPFC_MBOXQ_t *mbox;
5809 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5810 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5811
5812 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5813 if (!mbox)
5814 return -ENOMEM;
5815
5816 /*
5817 * This function sends an embedded mailbox because it only sends the
5818 * the resource type. All extents of this type are released by the
5819 * port.
5820 */
5821 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5822 sizeof(struct lpfc_sli4_cfg_mhdr));
5823 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5824 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5825 length, LPFC_SLI4_MBX_EMBED);
5826
5827 /* Send an extents count of 0 - the dealloc doesn't use it. */
5828 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5829 LPFC_SLI4_MBX_EMBED);
5830 if (unlikely(rc)) {
5831 rc = -EIO;
5832 goto out_free_mbox;
5833 }
5834 if (!phba->sli4_hba.intr_enable)
5835 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5836 else {
James Smarta183a152011-10-10 21:32:43 -04005837 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005838 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5839 }
5840 if (unlikely(rc)) {
5841 rc = -EIO;
5842 goto out_free_mbox;
5843 }
5844
5845 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5846 if (bf_get(lpfc_mbox_hdr_status,
5847 &dealloc_rsrc->header.cfg_shdr.response)) {
5848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5849 "2919 Failed to release resource extents "
5850 "for type %d - Status 0x%x Add'l Status 0x%x. "
5851 "Resource memory not released.\n",
5852 type,
5853 bf_get(lpfc_mbox_hdr_status,
5854 &dealloc_rsrc->header.cfg_shdr.response),
5855 bf_get(lpfc_mbox_hdr_add_status,
5856 &dealloc_rsrc->header.cfg_shdr.response));
5857 rc = -EIO;
5858 goto out_free_mbox;
5859 }
5860
5861 /* Release kernel memory resources for the specific type. */
5862 switch (type) {
5863 case LPFC_RSC_TYPE_FCOE_VPI:
5864 kfree(phba->vpi_bmask);
5865 kfree(phba->vpi_ids);
5866 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5867 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5868 &phba->lpfc_vpi_blk_list, list) {
5869 list_del_init(&rsrc_blk->list);
5870 kfree(rsrc_blk);
5871 }
James Smart16a3a202013-04-17 20:14:38 -04005872 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005873 break;
5874 case LPFC_RSC_TYPE_FCOE_XRI:
5875 kfree(phba->sli4_hba.xri_bmask);
5876 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005877 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5878 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5879 list_del_init(&rsrc_blk->list);
5880 kfree(rsrc_blk);
5881 }
5882 break;
5883 case LPFC_RSC_TYPE_FCOE_VFI:
5884 kfree(phba->sli4_hba.vfi_bmask);
5885 kfree(phba->sli4_hba.vfi_ids);
5886 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5887 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5888 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5889 list_del_init(&rsrc_blk->list);
5890 kfree(rsrc_blk);
5891 }
5892 break;
5893 case LPFC_RSC_TYPE_FCOE_RPI:
5894 /* RPI bitmask and physical id array are cleaned up earlier. */
5895 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5896 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5897 list_del_init(&rsrc_blk->list);
5898 kfree(rsrc_blk);
5899 }
5900 break;
5901 default:
5902 break;
5903 }
5904
5905 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5906
5907 out_free_mbox:
5908 mempool_free(mbox, phba->mbox_mem_pool);
5909 return rc;
5910}
5911
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005912static void
James Smart7bdedb32016-07-06 12:36:00 -07005913lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5914 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005915{
James Smart65791f12016-07-06 12:35:56 -07005916 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005917
James Smart65791f12016-07-06 12:35:56 -07005918 len = sizeof(struct lpfc_mbx_set_feature) -
5919 sizeof(struct lpfc_sli4_cfg_mhdr);
5920 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5921 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5922 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005923
James Smart7bdedb32016-07-06 12:36:00 -07005924 switch (feature) {
5925 case LPFC_SET_UE_RECOVERY:
5926 bf_set(lpfc_mbx_set_feature_UER,
5927 &mbox->u.mqe.un.set_feature, 1);
5928 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5929 mbox->u.mqe.un.set_feature.param_len = 8;
5930 break;
5931 case LPFC_SET_MDS_DIAGS:
5932 bf_set(lpfc_mbx_set_feature_mds,
5933 &mbox->u.mqe.un.set_feature, 1);
5934 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07005935 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07005936 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5937 mbox->u.mqe.un.set_feature.param_len = 8;
5938 break;
James Smart65791f12016-07-06 12:35:56 -07005939 }
James Smart7bdedb32016-07-06 12:36:00 -07005940
5941 return;
James Smart65791f12016-07-06 12:35:56 -07005942}
5943
James Smart6d368e52011-05-24 11:44:12 -04005944/**
5945 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5946 * @phba: Pointer to HBA context object.
5947 *
5948 * This function allocates all SLI4 resource identifiers.
5949 **/
5950int
5951lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5952{
5953 int i, rc, error = 0;
5954 uint16_t count, base;
5955 unsigned long longs;
5956
James Smartff78d8f2011-12-13 13:21:35 -05005957 if (!phba->sli4_hba.rpi_hdrs_in_use)
5958 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005959 if (phba->sli4_hba.extents_in_use) {
5960 /*
5961 * The port supports resource extents. The XRI, VPI, VFI, RPI
5962 * resource extent count must be read and allocated before
5963 * provisioning the resource id arrays.
5964 */
5965 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5966 LPFC_IDX_RSRC_RDY) {
5967 /*
5968 * Extent-based resources are set - the driver could
5969 * be in a port reset. Figure out if any corrective
5970 * actions need to be taken.
5971 */
5972 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5973 LPFC_RSC_TYPE_FCOE_VFI);
5974 if (rc != 0)
5975 error++;
5976 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5977 LPFC_RSC_TYPE_FCOE_VPI);
5978 if (rc != 0)
5979 error++;
5980 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5981 LPFC_RSC_TYPE_FCOE_XRI);
5982 if (rc != 0)
5983 error++;
5984 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5985 LPFC_RSC_TYPE_FCOE_RPI);
5986 if (rc != 0)
5987 error++;
5988
5989 /*
5990 * It's possible that the number of resources
5991 * provided to this port instance changed between
5992 * resets. Detect this condition and reallocate
5993 * resources. Otherwise, there is no action.
5994 */
5995 if (error) {
5996 lpfc_printf_log(phba, KERN_INFO,
5997 LOG_MBOX | LOG_INIT,
5998 "2931 Detected extent resource "
5999 "change. Reallocating all "
6000 "extents.\n");
6001 rc = lpfc_sli4_dealloc_extent(phba,
6002 LPFC_RSC_TYPE_FCOE_VFI);
6003 rc = lpfc_sli4_dealloc_extent(phba,
6004 LPFC_RSC_TYPE_FCOE_VPI);
6005 rc = lpfc_sli4_dealloc_extent(phba,
6006 LPFC_RSC_TYPE_FCOE_XRI);
6007 rc = lpfc_sli4_dealloc_extent(phba,
6008 LPFC_RSC_TYPE_FCOE_RPI);
6009 } else
6010 return 0;
6011 }
6012
6013 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6014 if (unlikely(rc))
6015 goto err_exit;
6016
6017 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6018 if (unlikely(rc))
6019 goto err_exit;
6020
6021 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6022 if (unlikely(rc))
6023 goto err_exit;
6024
6025 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6026 if (unlikely(rc))
6027 goto err_exit;
6028 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6029 LPFC_IDX_RSRC_RDY);
6030 return rc;
6031 } else {
6032 /*
6033 * The port does not support resource extents. The XRI, VPI,
6034 * VFI, RPI resource ids were determined from READ_CONFIG.
6035 * Just allocate the bitmasks and provision the resource id
6036 * arrays. If a port reset is active, the resources don't
6037 * need any action - just exit.
6038 */
6039 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006040 LPFC_IDX_RSRC_RDY) {
6041 lpfc_sli4_dealloc_resource_identifiers(phba);
6042 lpfc_sli4_remove_rpis(phba);
6043 }
James Smart6d368e52011-05-24 11:44:12 -04006044 /* RPIs. */
6045 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006046 if (count <= 0) {
6047 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6048 "3279 Invalid provisioning of "
6049 "rpi:%d\n", count);
6050 rc = -EINVAL;
6051 goto err_exit;
6052 }
James Smart6d368e52011-05-24 11:44:12 -04006053 base = phba->sli4_hba.max_cfg_param.rpi_base;
6054 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6055 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6056 sizeof(unsigned long),
6057 GFP_KERNEL);
6058 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6059 rc = -ENOMEM;
6060 goto err_exit;
6061 }
6062 phba->sli4_hba.rpi_ids = kzalloc(count *
6063 sizeof(uint16_t),
6064 GFP_KERNEL);
6065 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6066 rc = -ENOMEM;
6067 goto free_rpi_bmask;
6068 }
6069
6070 for (i = 0; i < count; i++)
6071 phba->sli4_hba.rpi_ids[i] = base + i;
6072
6073 /* VPIs. */
6074 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006075 if (count <= 0) {
6076 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6077 "3280 Invalid provisioning of "
6078 "vpi:%d\n", count);
6079 rc = -EINVAL;
6080 goto free_rpi_ids;
6081 }
James Smart6d368e52011-05-24 11:44:12 -04006082 base = phba->sli4_hba.max_cfg_param.vpi_base;
6083 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6084 phba->vpi_bmask = kzalloc(longs *
6085 sizeof(unsigned long),
6086 GFP_KERNEL);
6087 if (unlikely(!phba->vpi_bmask)) {
6088 rc = -ENOMEM;
6089 goto free_rpi_ids;
6090 }
6091 phba->vpi_ids = kzalloc(count *
6092 sizeof(uint16_t),
6093 GFP_KERNEL);
6094 if (unlikely(!phba->vpi_ids)) {
6095 rc = -ENOMEM;
6096 goto free_vpi_bmask;
6097 }
6098
6099 for (i = 0; i < count; i++)
6100 phba->vpi_ids[i] = base + i;
6101
6102 /* XRIs. */
6103 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006104 if (count <= 0) {
6105 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6106 "3281 Invalid provisioning of "
6107 "xri:%d\n", count);
6108 rc = -EINVAL;
6109 goto free_vpi_ids;
6110 }
James Smart6d368e52011-05-24 11:44:12 -04006111 base = phba->sli4_hba.max_cfg_param.xri_base;
6112 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6113 phba->sli4_hba.xri_bmask = kzalloc(longs *
6114 sizeof(unsigned long),
6115 GFP_KERNEL);
6116 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6117 rc = -ENOMEM;
6118 goto free_vpi_ids;
6119 }
James Smart41899be2012-03-01 22:34:19 -05006120 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006121 phba->sli4_hba.xri_ids = kzalloc(count *
6122 sizeof(uint16_t),
6123 GFP_KERNEL);
6124 if (unlikely(!phba->sli4_hba.xri_ids)) {
6125 rc = -ENOMEM;
6126 goto free_xri_bmask;
6127 }
6128
6129 for (i = 0; i < count; i++)
6130 phba->sli4_hba.xri_ids[i] = base + i;
6131
6132 /* VFIs. */
6133 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006134 if (count <= 0) {
6135 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6136 "3282 Invalid provisioning of "
6137 "vfi:%d\n", count);
6138 rc = -EINVAL;
6139 goto free_xri_ids;
6140 }
James Smart6d368e52011-05-24 11:44:12 -04006141 base = phba->sli4_hba.max_cfg_param.vfi_base;
6142 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6143 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6144 sizeof(unsigned long),
6145 GFP_KERNEL);
6146 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6147 rc = -ENOMEM;
6148 goto free_xri_ids;
6149 }
6150 phba->sli4_hba.vfi_ids = kzalloc(count *
6151 sizeof(uint16_t),
6152 GFP_KERNEL);
6153 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6154 rc = -ENOMEM;
6155 goto free_vfi_bmask;
6156 }
6157
6158 for (i = 0; i < count; i++)
6159 phba->sli4_hba.vfi_ids[i] = base + i;
6160
6161 /*
6162 * Mark all resources ready. An HBA reset doesn't need
6163 * to reset the initialization.
6164 */
6165 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6166 LPFC_IDX_RSRC_RDY);
6167 return 0;
6168 }
6169
6170 free_vfi_bmask:
6171 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006172 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006173 free_xri_ids:
6174 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006175 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006176 free_xri_bmask:
6177 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006178 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006179 free_vpi_ids:
6180 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006181 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006182 free_vpi_bmask:
6183 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006184 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006185 free_rpi_ids:
6186 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006187 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006188 free_rpi_bmask:
6189 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006190 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006191 err_exit:
6192 return rc;
6193}
6194
6195/**
6196 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6197 * @phba: Pointer to HBA context object.
6198 *
6199 * This function allocates the number of elements for the specified
6200 * resource type.
6201 **/
6202int
6203lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6204{
6205 if (phba->sli4_hba.extents_in_use) {
6206 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6207 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6208 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6209 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6210 } else {
6211 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006212 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006213 kfree(phba->vpi_ids);
6214 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6215 kfree(phba->sli4_hba.xri_bmask);
6216 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006217 kfree(phba->sli4_hba.vfi_bmask);
6218 kfree(phba->sli4_hba.vfi_ids);
6219 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6220 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6221 }
6222
6223 return 0;
6224}
6225
6226/**
James Smartb76f2dc2011-07-22 18:37:42 -04006227 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6228 * @phba: Pointer to HBA context object.
6229 * @type: The resource extent type.
6230 * @extnt_count: buffer to hold port extent count response
6231 * @extnt_size: buffer to hold port extent size response.
6232 *
6233 * This function calls the port to read the host allocated extents
6234 * for a particular type.
6235 **/
6236int
6237lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6238 uint16_t *extnt_cnt, uint16_t *extnt_size)
6239{
6240 bool emb;
6241 int rc = 0;
6242 uint16_t curr_blks = 0;
6243 uint32_t req_len, emb_len;
6244 uint32_t alloc_len, mbox_tmo;
6245 struct list_head *blk_list_head;
6246 struct lpfc_rsrc_blks *rsrc_blk;
6247 LPFC_MBOXQ_t *mbox;
6248 void *virtaddr = NULL;
6249 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6250 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6251 union lpfc_sli4_cfg_shdr *shdr;
6252
6253 switch (type) {
6254 case LPFC_RSC_TYPE_FCOE_VPI:
6255 blk_list_head = &phba->lpfc_vpi_blk_list;
6256 break;
6257 case LPFC_RSC_TYPE_FCOE_XRI:
6258 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6259 break;
6260 case LPFC_RSC_TYPE_FCOE_VFI:
6261 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6262 break;
6263 case LPFC_RSC_TYPE_FCOE_RPI:
6264 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6265 break;
6266 default:
6267 return -EIO;
6268 }
6269
6270 /* Count the number of extents currently allocatd for this type. */
6271 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6272 if (curr_blks == 0) {
6273 /*
6274 * The GET_ALLOCATED mailbox does not return the size,
6275 * just the count. The size should be just the size
6276 * stored in the current allocated block and all sizes
6277 * for an extent type are the same so set the return
6278 * value now.
6279 */
6280 *extnt_size = rsrc_blk->rsrc_size;
6281 }
6282 curr_blks++;
6283 }
6284
James Smartb76f2dc2011-07-22 18:37:42 -04006285 /*
6286 * Calculate the size of an embedded mailbox. The uint32_t
6287 * accounts for extents-specific word.
6288 */
6289 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6290 sizeof(uint32_t);
6291
6292 /*
6293 * Presume the allocation and response will fit into an embedded
6294 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6295 */
6296 emb = LPFC_SLI4_MBX_EMBED;
6297 req_len = emb_len;
6298 if (req_len > emb_len) {
6299 req_len = curr_blks * sizeof(uint16_t) +
6300 sizeof(union lpfc_sli4_cfg_shdr) +
6301 sizeof(uint32_t);
6302 emb = LPFC_SLI4_MBX_NEMBED;
6303 }
6304
6305 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6306 if (!mbox)
6307 return -ENOMEM;
6308 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6309
6310 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6311 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6312 req_len, emb);
6313 if (alloc_len < req_len) {
6314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6315 "2983 Allocated DMA memory size (x%x) is "
6316 "less than the requested DMA memory "
6317 "size (x%x)\n", alloc_len, req_len);
6318 rc = -ENOMEM;
6319 goto err_exit;
6320 }
6321 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6322 if (unlikely(rc)) {
6323 rc = -EIO;
6324 goto err_exit;
6325 }
6326
6327 if (!phba->sli4_hba.intr_enable)
6328 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6329 else {
James Smarta183a152011-10-10 21:32:43 -04006330 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006331 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6332 }
6333
6334 if (unlikely(rc)) {
6335 rc = -EIO;
6336 goto err_exit;
6337 }
6338
6339 /*
6340 * Figure out where the response is located. Then get local pointers
6341 * to the response data. The port does not guarantee to respond to
6342 * all extents counts request so update the local variable with the
6343 * allocated count from the port.
6344 */
6345 if (emb == LPFC_SLI4_MBX_EMBED) {
6346 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6347 shdr = &rsrc_ext->header.cfg_shdr;
6348 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6349 } else {
6350 virtaddr = mbox->sge_array->addr[0];
6351 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6352 shdr = &n_rsrc->cfg_shdr;
6353 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6354 }
6355
6356 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6358 "2984 Failed to read allocated resources "
6359 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6360 type,
6361 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6362 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6363 rc = -EIO;
6364 goto err_exit;
6365 }
6366 err_exit:
6367 lpfc_sli4_mbox_cmd_free(phba, mbox);
6368 return rc;
6369}
6370
6371/**
James Smart0ef69962017-04-21 16:04:50 -07006372 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006373 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006374 * @pring: Pointer to driver SLI ring object.
6375 * @sgl_list: linked link of sgl buffers to post
6376 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006377 *
James Smart895427b2017-02-12 13:52:30 -08006378 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006379 * repost them to the port by using SGL block post. This is needed after a
6380 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006381 * of buffer sgls which contains contiguous xris and uses the non-embedded
6382 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006383 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6384 * mailbox command for posting.
6385 *
6386 * Returns: 0 = success, non-zero failure.
6387 **/
6388static int
James Smart895427b2017-02-12 13:52:30 -08006389lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6390 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006391{
6392 struct lpfc_sglq *sglq_entry = NULL;
6393 struct lpfc_sglq *sglq_entry_next = NULL;
6394 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006395 int status, total_cnt;
6396 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006397 int last_xritag = NO_XRI;
6398 LIST_HEAD(prep_sgl_list);
6399 LIST_HEAD(blck_sgl_list);
6400 LIST_HEAD(allc_sgl_list);
6401 LIST_HEAD(post_sgl_list);
6402 LIST_HEAD(free_sgl_list);
6403
James Smart38c20672013-03-01 16:37:44 -05006404 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006405 spin_lock(&phba->sli4_hba.sgl_list_lock);
6406 list_splice_init(sgl_list, &allc_sgl_list);
6407 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006408 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006409
James Smart895427b2017-02-12 13:52:30 -08006410 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006411 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6412 &allc_sgl_list, list) {
6413 list_del_init(&sglq_entry->list);
6414 block_cnt++;
6415 if ((last_xritag != NO_XRI) &&
6416 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6417 /* a hole in xri block, form a sgl posting block */
6418 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6419 post_cnt = block_cnt - 1;
6420 /* prepare list for next posting block */
6421 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6422 block_cnt = 1;
6423 } else {
6424 /* prepare list for next posting block */
6425 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6426 /* enough sgls for non-embed sgl mbox command */
6427 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6428 list_splice_init(&prep_sgl_list,
6429 &blck_sgl_list);
6430 post_cnt = block_cnt;
6431 block_cnt = 0;
6432 }
6433 }
6434 num_posted++;
6435
6436 /* keep track of last sgl's xritag */
6437 last_xritag = sglq_entry->sli4_xritag;
6438
James Smart895427b2017-02-12 13:52:30 -08006439 /* end of repost sgl list condition for buffers */
6440 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006441 if (post_cnt == 0) {
6442 list_splice_init(&prep_sgl_list,
6443 &blck_sgl_list);
6444 post_cnt = block_cnt;
6445 } else if (block_cnt == 1) {
6446 status = lpfc_sli4_post_sgl(phba,
6447 sglq_entry->phys, 0,
6448 sglq_entry->sli4_xritag);
6449 if (!status) {
6450 /* successful, put sgl to posted list */
6451 list_add_tail(&sglq_entry->list,
6452 &post_sgl_list);
6453 } else {
6454 /* Failure, put sgl to free list */
6455 lpfc_printf_log(phba, KERN_WARNING,
6456 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006457 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006458 "sgl, xritag:x%x\n",
6459 sglq_entry->sli4_xritag);
6460 list_add_tail(&sglq_entry->list,
6461 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006462 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006463 }
6464 }
6465 }
6466
6467 /* continue until a nembed page worth of sgls */
6468 if (post_cnt == 0)
6469 continue;
6470
James Smart895427b2017-02-12 13:52:30 -08006471 /* post the buffer list sgls as a block */
6472 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6473 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006474
6475 if (!status) {
6476 /* success, put sgl list to posted sgl list */
6477 list_splice_init(&blck_sgl_list, &post_sgl_list);
6478 } else {
6479 /* Failure, put sgl list to free sgl list */
6480 sglq_entry_first = list_first_entry(&blck_sgl_list,
6481 struct lpfc_sglq,
6482 list);
6483 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006484 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006485 "xritag:x%x-x%x\n",
6486 sglq_entry_first->sli4_xritag,
6487 (sglq_entry_first->sli4_xritag +
6488 post_cnt - 1));
6489 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006490 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006491 }
6492
6493 /* don't reset xirtag due to hole in xri block */
6494 if (block_cnt == 0)
6495 last_xritag = NO_XRI;
6496
James Smart895427b2017-02-12 13:52:30 -08006497 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006498 post_cnt = 0;
6499 }
6500
James Smart895427b2017-02-12 13:52:30 -08006501 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006502 lpfc_free_sgl_list(phba, &free_sgl_list);
6503
James Smart895427b2017-02-12 13:52:30 -08006504 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006505 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006506 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006507 spin_lock(&phba->sli4_hba.sgl_list_lock);
6508 list_splice_init(&post_sgl_list, sgl_list);
6509 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006510 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006511 } else {
6512 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006513 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006514 return -EIO;
6515 }
James Smart895427b2017-02-12 13:52:30 -08006516
6517 /* return the number of XRIs actually posted */
6518 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006519}
6520
James Smart61bda8f2016-10-13 15:06:05 -07006521void
6522lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6523{
6524 uint32_t len;
6525
6526 len = sizeof(struct lpfc_mbx_set_host_data) -
6527 sizeof(struct lpfc_sli4_cfg_mhdr);
6528 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6529 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6530 LPFC_SLI4_MBX_EMBED);
6531
6532 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006533 mbox->u.mqe.un.set_host_data.param_len =
6534 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006535 snprintf(mbox->u.mqe.un.set_host_data.data,
6536 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6537 "Linux %s v"LPFC_DRIVER_VERSION,
6538 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6539}
6540
James Smarta8cf5df2017-05-15 15:20:46 -07006541int
James Smart6c621a22017-05-15 15:20:45 -07006542lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006543 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006544{
6545 int rc, i;
6546 struct lpfc_rqe hrqe;
6547 struct lpfc_rqe drqe;
6548 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006549 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006550 struct rqb_dmabuf *rqb_buffer;
6551 LIST_HEAD(rqb_buf_list);
6552
James Smart411de512018-01-30 15:58:52 -08006553 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006554 rqbp = hrq->rqbp;
6555 for (i = 0; i < count; i++) {
6556 /* IF RQ is already full, don't bother */
6557 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6558 break;
6559 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6560 if (!rqb_buffer)
6561 break;
6562 rqb_buffer->hrq = hrq;
6563 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006564 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006565 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6566 }
6567 while (!list_empty(&rqb_buf_list)) {
6568 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6569 hbuf.list);
6570
6571 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6572 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6573 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6574 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6575 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6576 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6578 "6421 Cannot post to HRQ %d: %x %x %x "
6579 "DRQ %x %x\n",
6580 hrq->queue_id,
6581 hrq->host_index,
6582 hrq->hba_index,
6583 hrq->entry_count,
6584 drq->host_index,
6585 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006586 rqbp->rqb_free_buffer(phba, rqb_buffer);
6587 } else {
6588 list_add_tail(&rqb_buffer->hbuf.list,
6589 &rqbp->rqb_buffer_list);
6590 rqbp->buffer_count++;
6591 }
6592 }
James Smart411de512018-01-30 15:58:52 -08006593 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006594 return 1;
6595}
6596
James Smart8a9d2e82012-05-09 21:16:12 -04006597/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006598 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006599 * @phba: Pointer to HBA context object.
6600 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006601 * This function is the main SLI4 device initialization PCI function. This
6602 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006603 * HBA error attention handler code. Caller is not required to hold any
6604 * locks.
6605 **/
6606int
6607lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6608{
James Smart6c621a22017-05-15 15:20:45 -07006609 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006610 LPFC_MBOXQ_t *mboxq;
6611 struct lpfc_mqe *mqe;
6612 uint8_t *vpd;
6613 uint32_t vpd_size;
6614 uint32_t ftr_rsp = 0;
6615 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6616 struct lpfc_vport *vport = phba->pport;
6617 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006618 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006619
6620 /* Perform a PCI function reset to start from clean */
6621 rc = lpfc_pci_function_reset(phba);
6622 if (unlikely(rc))
6623 return -ENODEV;
6624
6625 /* Check the HBA Host Status Register for readyness */
6626 rc = lpfc_sli4_post_status_check(phba);
6627 if (unlikely(rc))
6628 return -ENODEV;
6629 else {
6630 spin_lock_irq(&phba->hbalock);
6631 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6632 spin_unlock_irq(&phba->hbalock);
6633 }
6634
6635 /*
6636 * Allocate a single mailbox container for initializing the
6637 * port.
6638 */
6639 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6640 if (!mboxq)
6641 return -ENOMEM;
6642
James Smartda0436e2009-05-22 14:51:39 -04006643 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006644 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006645 vpd = kzalloc(vpd_size, GFP_KERNEL);
6646 if (!vpd) {
6647 rc = -ENOMEM;
6648 goto out_free_mbox;
6649 }
6650
6651 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006652 if (unlikely(rc)) {
6653 kfree(vpd);
6654 goto out_free_mbox;
6655 }
James Smart572709e2013-07-15 18:32:43 -04006656
James Smartda0436e2009-05-22 14:51:39 -04006657 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006658 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006659 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006660 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006661 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6662 } else {
James Smart76a95d72010-11-20 23:11:48 -05006663 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006664 }
James Smart45ed1192009-10-02 15:17:02 -04006665
6666 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6667 LPFC_DCBX_CEE_MODE)
6668 phba->hba_flag |= HBA_FIP_SUPPORT;
6669 else
6670 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6671
James Smart4f2e66c2012-05-09 21:17:07 -04006672 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6673
James Smartc31098c2011-04-16 11:03:33 -04006674 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6676 "0376 READ_REV Error. SLI Level %d "
6677 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006678 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006679 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006680 kfree(vpd);
6681 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006682 }
James Smartcd1c8302011-10-10 21:33:25 -04006683
6684 /*
James Smartff78d8f2011-12-13 13:21:35 -05006685 * Continue initialization with default values even if driver failed
6686 * to read FCoE param config regions, only read parameters if the
6687 * board is FCoE
6688 */
6689 if (phba->hba_flag & HBA_FCOE_MODE &&
6690 lpfc_sli4_read_fcoe_params(phba))
6691 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6692 "2570 Failed to read FCoE parameters\n");
6693
6694 /*
James Smartcd1c8302011-10-10 21:33:25 -04006695 * Retrieve sli4 device physical port name, failure of doing it
6696 * is considered as non-fatal.
6697 */
6698 rc = lpfc_sli4_retrieve_pport_name(phba);
6699 if (!rc)
6700 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6701 "3080 Successful retrieving SLI4 device "
6702 "physical port name: %s.\n", phba->Port);
6703
James Smartda0436e2009-05-22 14:51:39 -04006704 /*
6705 * Evaluate the read rev and vpd data. Populate the driver
6706 * state with the results. If this routine fails, the failure
6707 * is not fatal as the driver will use generic values.
6708 */
6709 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6710 if (unlikely(!rc)) {
6711 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6712 "0377 Error %d parsing vpd. "
6713 "Using defaults.\n", rc);
6714 rc = 0;
6715 }
James Smart76a95d72010-11-20 23:11:48 -05006716 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006717
James Smartf1126682009-06-10 17:22:44 -04006718 /* Save information as VPD data */
6719 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6720 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6721 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6722 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6723 &mqe->un.read_rev);
6724 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6725 &mqe->un.read_rev);
6726 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6727 &mqe->un.read_rev);
6728 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6729 &mqe->un.read_rev);
6730 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6731 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6732 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6733 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6734 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6735 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6736 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6737 "(%d):0380 READ_REV Status x%x "
6738 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6739 mboxq->vport ? mboxq->vport->vpi : 0,
6740 bf_get(lpfc_mqe_status, mqe),
6741 phba->vpd.rev.opFwName,
6742 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6743 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006744
James Smart572709e2013-07-15 18:32:43 -04006745 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6746 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6747 if (phba->pport->cfg_lun_queue_depth > rc) {
6748 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6749 "3362 LUN queue depth changed from %d to %d\n",
6750 phba->pport->cfg_lun_queue_depth, rc);
6751 phba->pport->cfg_lun_queue_depth = rc;
6752 }
6753
James Smart65791f12016-07-06 12:35:56 -07006754 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006755 LPFC_SLI_INTF_IF_TYPE_0) {
6756 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6757 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6758 if (rc == MBX_SUCCESS) {
6759 phba->hba_flag |= HBA_RECOVERABLE_UE;
6760 /* Set 1Sec interval to detect UE */
6761 phba->eratt_poll_interval = 1;
6762 phba->sli4_hba.ue_to_sr = bf_get(
6763 lpfc_mbx_set_feature_UESR,
6764 &mboxq->u.mqe.un.set_feature);
6765 phba->sli4_hba.ue_to_rp = bf_get(
6766 lpfc_mbx_set_feature_UERP,
6767 &mboxq->u.mqe.un.set_feature);
6768 }
6769 }
6770
6771 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6772 /* Enable MDS Diagnostics only if the SLI Port supports it */
6773 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6774 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6775 if (rc != MBX_SUCCESS)
6776 phba->mds_diags_support = 0;
6777 }
James Smart572709e2013-07-15 18:32:43 -04006778
James Smartda0436e2009-05-22 14:51:39 -04006779 /*
6780 * Discover the port's supported feature set and match it against the
6781 * hosts requests.
6782 */
6783 lpfc_request_features(phba, mboxq);
6784 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6785 if (unlikely(rc)) {
6786 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006787 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006788 }
6789
6790 /*
6791 * The port must support FCP initiator mode as this is the
6792 * only mode running in the host.
6793 */
6794 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6795 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6796 "0378 No support for fcpi mode.\n");
6797 ftr_rsp++;
6798 }
James Smartfedd3b72011-02-16 12:39:24 -05006799 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6800 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6801 else
6802 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006803 /*
6804 * If the port cannot support the host's requested features
6805 * then turn off the global config parameters to disable the
6806 * feature in the driver. This is not a fatal error.
6807 */
James Smartbf086112011-08-21 21:48:13 -04006808 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6809 if (phba->cfg_enable_bg) {
6810 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6811 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6812 else
6813 ftr_rsp++;
6814 }
James Smartda0436e2009-05-22 14:51:39 -04006815
6816 if (phba->max_vpi && phba->cfg_enable_npiv &&
6817 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6818 ftr_rsp++;
6819
6820 if (ftr_rsp) {
6821 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6822 "0379 Feature Mismatch Data: x%08x %08x "
6823 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6824 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6825 phba->cfg_enable_npiv, phba->max_vpi);
6826 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6827 phba->cfg_enable_bg = 0;
6828 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6829 phba->cfg_enable_npiv = 0;
6830 }
6831
6832 /* These SLI3 features are assumed in SLI4 */
6833 spin_lock_irq(&phba->hbalock);
6834 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6835 spin_unlock_irq(&phba->hbalock);
6836
James Smart6d368e52011-05-24 11:44:12 -04006837 /*
6838 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6839 * calls depends on these resources to complete port setup.
6840 */
6841 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6842 if (rc) {
6843 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6844 "2920 Failed to alloc Resource IDs "
6845 "rc = x%x\n", rc);
6846 goto out_free_mbox;
6847 }
6848
James Smart61bda8f2016-10-13 15:06:05 -07006849 lpfc_set_host_data(phba, mboxq);
6850
6851 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6852 if (rc) {
6853 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6854 "2134 Failed to set host os driver version %x",
6855 rc);
6856 }
6857
James Smartda0436e2009-05-22 14:51:39 -04006858 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006859 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6860 if (rc) {
6861 phba->link_state = LPFC_HBA_ERROR;
6862 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006863 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006864 }
6865
James Smartda0436e2009-05-22 14:51:39 -04006866 mboxq->vport = vport;
6867 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6868 mp = (struct lpfc_dmabuf *) mboxq->context1;
6869 if (rc == MBX_SUCCESS) {
6870 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6871 rc = 0;
6872 }
6873
6874 /*
6875 * This memory was allocated by the lpfc_read_sparam routine. Release
6876 * it to the mbuf pool.
6877 */
6878 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6879 kfree(mp);
6880 mboxq->context1 = NULL;
6881 if (unlikely(rc)) {
6882 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6883 "0382 READ_SPARAM command failed "
6884 "status %d, mbxStatus x%x\n",
6885 rc, bf_get(lpfc_mqe_status, mqe));
6886 phba->link_state = LPFC_HBA_ERROR;
6887 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006888 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006889 }
6890
James Smart05580562011-05-24 11:40:48 -04006891 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006892
6893 /* Update the fc_host data structures with new wwn. */
6894 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6895 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6896
James Smart895427b2017-02-12 13:52:30 -08006897 /* Create all the SLI4 queues */
6898 rc = lpfc_sli4_queue_create(phba);
6899 if (rc) {
6900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6901 "3089 Failed to allocate queues\n");
6902 rc = -ENODEV;
6903 goto out_free_mbox;
6904 }
6905 /* Set up all the queues to the device */
6906 rc = lpfc_sli4_queue_setup(phba);
6907 if (unlikely(rc)) {
6908 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6909 "0381 Error %d during queue setup.\n ", rc);
6910 goto out_stop_timers;
6911 }
6912 /* Initialize the driver internal SLI layer lists. */
6913 lpfc_sli4_setup(phba);
6914 lpfc_sli4_queue_init(phba);
6915
6916 /* update host els xri-sgl sizes and mappings */
6917 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006918 if (unlikely(rc)) {
6919 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6920 "1400 Failed to update xri-sgl size and "
6921 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006922 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006923 }
6924
James Smart8a9d2e82012-05-09 21:16:12 -04006925 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006926 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6927 phba->sli4_hba.els_xri_cnt);
6928 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006929 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6930 "0582 Error %d during els sgl post "
6931 "operation\n", rc);
6932 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006933 goto out_destroy_queue;
6934 }
6935 phba->sli4_hba.els_xri_cnt = rc;
6936
James Smartf358dd02017-02-12 13:52:34 -08006937 if (phba->nvmet_support) {
6938 /* update host nvmet xri-sgl sizes and mappings */
6939 rc = lpfc_sli4_nvmet_sgl_update(phba);
6940 if (unlikely(rc)) {
6941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6942 "6308 Failed to update nvmet-sgl size "
6943 "and mapping: %d\n", rc);
6944 goto out_destroy_queue;
6945 }
6946
6947 /* register the nvmet sgl pool to the port */
6948 rc = lpfc_sli4_repost_sgl_list(
6949 phba,
6950 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6951 phba->sli4_hba.nvmet_xri_cnt);
6952 if (unlikely(rc < 0)) {
6953 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6954 "3117 Error %d during nvmet "
6955 "sgl post\n", rc);
6956 rc = -ENODEV;
6957 goto out_destroy_queue;
6958 }
6959 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006960
6961 cnt = phba->cfg_iocb_cnt * 1024;
6962 /* We need 1 iocbq for every SGL, for IO processing */
6963 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08006964 } else {
James Smart895427b2017-02-12 13:52:30 -08006965 /* update host scsi xri-sgl sizes and mappings */
6966 rc = lpfc_sli4_scsi_sgl_update(phba);
6967 if (unlikely(rc)) {
6968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6969 "6309 Failed to update scsi-sgl size "
6970 "and mapping: %d\n", rc);
6971 goto out_destroy_queue;
6972 }
6973
6974 /* update host nvme xri-sgl sizes and mappings */
6975 rc = lpfc_sli4_nvme_sgl_update(phba);
6976 if (unlikely(rc)) {
6977 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6978 "6082 Failed to update nvme-sgl size "
6979 "and mapping: %d\n", rc);
6980 goto out_destroy_queue;
6981 }
James Smart6c621a22017-05-15 15:20:45 -07006982
6983 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07006984 }
6985
6986 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07006987 /* Initialize and populate the iocb list per host */
6988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006989 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07006990 phba->cfg_iocb_cnt, cnt);
6991 rc = lpfc_init_iocb_list(phba, cnt);
6992 if (rc) {
6993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006994 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07006995 goto out_destroy_queue;
6996 }
James Smart8a9d2e82012-05-09 21:16:12 -04006997 }
6998
James Smart11e644e2017-06-15 22:56:48 -07006999 if (phba->nvmet_support)
7000 lpfc_nvmet_create_targetport(phba);
7001
James Smart2d7dbc42017-02-12 13:52:35 -08007002 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007003 /* Post initial buffers to all RQs created */
7004 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7005 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7006 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7007 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7008 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007009 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007010 rqbp->buffer_count = 0;
7011
James Smart2d7dbc42017-02-12 13:52:35 -08007012 lpfc_post_rq_buffer(
7013 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7014 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07007015 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007016 }
7017 }
7018
James Smart895427b2017-02-12 13:52:30 -08007019 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7020 /* register the allocated scsi sgl pool to the port */
7021 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7022 if (unlikely(rc)) {
7023 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7024 "0383 Error %d during scsi sgl post "
7025 "operation\n", rc);
7026 /* Some Scsi buffers were moved to abort scsi list */
7027 /* A pci function reset will repost them */
7028 rc = -ENODEV;
7029 goto out_destroy_queue;
7030 }
James Smartda0436e2009-05-22 14:51:39 -04007031 }
7032
James Smart01649562017-02-12 13:52:32 -08007033 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7034 (phba->nvmet_support == 0)) {
7035
7036 /* register the allocated nvme sgl pool to the port */
7037 rc = lpfc_repost_nvme_sgl_list(phba);
7038 if (unlikely(rc)) {
7039 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7040 "6116 Error %d during nvme sgl post "
7041 "operation\n", rc);
7042 /* Some NVME buffers were moved to abort nvme list */
7043 /* A pci function reset will repost them */
7044 rc = -ENODEV;
7045 goto out_destroy_queue;
7046 }
James Smartda0436e2009-05-22 14:51:39 -04007047 }
7048
7049 /* Post the rpi header region to the device. */
7050 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7051 if (unlikely(rc)) {
7052 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7053 "0393 Error %d during rpi post operation\n",
7054 rc);
7055 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007056 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007057 }
James Smart97f2ecf2012-03-01 22:35:23 -05007058 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007059
James Smart895427b2017-02-12 13:52:30 -08007060 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007061 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007062 /*
7063 * The FC Port needs to register FCFI (index 0)
7064 */
7065 lpfc_reg_fcfi(phba, mboxq);
7066 mboxq->vport = phba->pport;
7067 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7068 if (rc != MBX_SUCCESS)
7069 goto out_unset_queue;
7070 rc = 0;
7071 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7072 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007073 } else {
7074 /* We are a NVME Target mode with MRQ > 1 */
7075
7076 /* First register the FCFI */
7077 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7078 mboxq->vport = phba->pport;
7079 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7080 if (rc != MBX_SUCCESS)
7081 goto out_unset_queue;
7082 rc = 0;
7083 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7084 &mboxq->u.mqe.un.reg_fcfi_mrq);
7085
7086 /* Next register the MRQs */
7087 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7088 mboxq->vport = phba->pport;
7089 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7090 if (rc != MBX_SUCCESS)
7091 goto out_unset_queue;
7092 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007093 }
7094 /* Check if the port is configured to be disabled */
7095 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007096 }
7097
7098 /* Arm the CQs and then EQs on device */
7099 lpfc_sli4_arm_cqeq_intr(phba);
7100
7101 /* Indicate device interrupt mode */
7102 phba->sli4_hba.intr_enable = 1;
7103
7104 /* Allow asynchronous mailbox command to go through */
7105 spin_lock_irq(&phba->hbalock);
7106 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7107 spin_unlock_irq(&phba->hbalock);
7108
7109 /* Post receive buffers to the device */
7110 lpfc_sli4_rb_setup(phba);
7111
James Smartfc2b9892010-02-26 14:15:29 -05007112 /* Reset HBA FCF states after HBA reset */
7113 phba->fcf.fcf_flag = 0;
7114 phba->fcf.current_rec.flag = 0;
7115
James Smartda0436e2009-05-22 14:51:39 -04007116 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007117 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007118 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007119
7120 /* Start heart beat timer */
7121 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007122 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007123 phba->hb_outstanding = 0;
7124 phba->last_completion_time = jiffies;
7125
7126 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007127 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007128 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007129
James Smart75baf692010-06-08 18:31:21 -04007130 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7131 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7132 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7133 if (!rc) {
7134 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7135 "2829 This device supports "
7136 "Advanced Error Reporting (AER)\n");
7137 spin_lock_irq(&phba->hbalock);
7138 phba->hba_flag |= HBA_AER_ENABLED;
7139 spin_unlock_irq(&phba->hbalock);
7140 } else {
7141 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7142 "2830 This device does not support "
7143 "Advanced Error Reporting (AER)\n");
7144 phba->cfg_aer_support = 0;
7145 }
James Smart0a96e972011-07-22 18:37:28 -04007146 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007147 }
7148
James Smartda0436e2009-05-22 14:51:39 -04007149 /*
7150 * The port is ready, set the host's link state to LINK_DOWN
7151 * in preparation for link interrupts.
7152 */
James Smartda0436e2009-05-22 14:51:39 -04007153 spin_lock_irq(&phba->hbalock);
7154 phba->link_state = LPFC_LINK_DOWN;
7155 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007156 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7157 (phba->hba_flag & LINK_DISABLED)) {
7158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7159 "3103 Adapter Link is disabled.\n");
7160 lpfc_down_link(phba, mboxq);
7161 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7162 if (rc != MBX_SUCCESS) {
7163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7164 "3104 Adapter failed to issue "
7165 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7166 goto out_unset_queue;
7167 }
7168 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007169 /* don't perform init_link on SLI4 FC port loopback test */
7170 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7171 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7172 if (rc)
7173 goto out_unset_queue;
7174 }
James Smart5350d872011-10-10 21:33:49 -04007175 }
7176 mempool_free(mboxq, phba->mbox_mem_pool);
7177 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007178out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007179 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007180 lpfc_sli4_queue_unset(phba);
7181out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007182 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007183 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007184out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007185 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007186out_free_mbox:
7187 mempool_free(mboxq, phba->mbox_mem_pool);
7188 return rc;
7189}
James Smarte59058c2008-08-24 21:49:00 -04007190
7191/**
James Smart3621a712009-04-06 18:47:14 -04007192 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007193 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007194 *
James Smarte59058c2008-08-24 21:49:00 -04007195 * This is the callback function for mailbox timer. The mailbox
7196 * timer is armed when a new mailbox command is issued and the timer
7197 * is deleted when the mailbox complete. The function is called by
7198 * the kernel timer code when a mailbox does not complete within
7199 * expected time. This function wakes up the worker thread to
7200 * process the mailbox timeout and returns. All the processing is
7201 * done by the worker thread function lpfc_mbox_timeout_handler.
7202 **/
dea31012005-04-17 16:05:31 -05007203void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007204lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007205{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007206 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007207 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007208 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007209
James Smart2e0fef82007-06-17 19:56:36 -05007210 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007211 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007212 if (!tmo_posted)
7213 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7214 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7215
James Smart5e9d9b82008-06-14 22:52:53 -04007216 if (!tmo_posted)
7217 lpfc_worker_wake_up(phba);
7218 return;
dea31012005-04-17 16:05:31 -05007219}
7220
James Smarte8d3c3b2013-10-10 12:21:30 -04007221/**
7222 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7223 * are pending
7224 * @phba: Pointer to HBA context object.
7225 *
7226 * This function checks if any mailbox completions are present on the mailbox
7227 * completion queue.
7228 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007229static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007230lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7231{
7232
7233 uint32_t idx;
7234 struct lpfc_queue *mcq;
7235 struct lpfc_mcqe *mcqe;
7236 bool pending_completions = false;
7237
7238 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7239 return false;
7240
7241 /* Check for completions on mailbox completion queue */
7242
7243 mcq = phba->sli4_hba.mbx_cq;
7244 idx = mcq->hba_index;
7245 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7246 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7247 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7248 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7249 pending_completions = true;
7250 break;
7251 }
7252 idx = (idx + 1) % mcq->entry_count;
7253 if (mcq->hba_index == idx)
7254 break;
7255 }
7256 return pending_completions;
7257
7258}
7259
7260/**
7261 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7262 * that were missed.
7263 * @phba: Pointer to HBA context object.
7264 *
7265 * For sli4, it is possible to miss an interrupt. As such mbox completions
7266 * maybe missed causing erroneous mailbox timeouts to occur. This function
7267 * checks to see if mbox completions are on the mailbox completion queue
7268 * and will process all the completions associated with the eq for the
7269 * mailbox completion queue.
7270 **/
7271bool
7272lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7273{
7274
7275 uint32_t eqidx;
7276 struct lpfc_queue *fpeq = NULL;
7277 struct lpfc_eqe *eqe;
7278 bool mbox_pending;
7279
7280 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7281 return false;
7282
7283 /* Find the eq associated with the mcq */
7284
7285 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007286 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007287 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7288 phba->sli4_hba.mbx_cq->assoc_qid) {
7289 fpeq = phba->sli4_hba.hba_eq[eqidx];
7290 break;
7291 }
7292 if (!fpeq)
7293 return false;
7294
7295 /* Turn off interrupts from this EQ */
7296
7297 lpfc_sli4_eq_clr_intr(fpeq);
7298
7299 /* Check to see if a mbox completion is pending */
7300
7301 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7302
7303 /*
7304 * If a mbox completion is pending, process all the events on EQ
7305 * associated with the mbox completion queue (this could include
7306 * mailbox commands, async events, els commands, receive queue data
7307 * and fcp commands)
7308 */
7309
7310 if (mbox_pending)
7311 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7312 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7313 fpeq->EQ_processed++;
7314 }
7315
7316 /* Always clear and re-arm the EQ */
7317
7318 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7319
7320 return mbox_pending;
7321
7322}
James Smarte59058c2008-08-24 21:49:00 -04007323
7324/**
James Smart3621a712009-04-06 18:47:14 -04007325 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007326 * @phba: Pointer to HBA context object.
7327 *
7328 * This function is called from worker thread when a mailbox command times out.
7329 * The caller is not required to hold any locks. This function will reset the
7330 * HBA and recover all the pending commands.
7331 **/
dea31012005-04-17 16:05:31 -05007332void
7333lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7334{
James Smart2e0fef82007-06-17 19:56:36 -05007335 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007336 MAILBOX_t *mb = NULL;
7337
James Smart1dcb58e2007-04-25 09:51:30 -04007338 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007339
James Smarte8d3c3b2013-10-10 12:21:30 -04007340 /* If the mailbox completed, process the completion and return */
7341 if (lpfc_sli4_process_missed_mbox_completions(phba))
7342 return;
7343
James Smarteb016562014-09-03 12:58:06 -04007344 if (pmbox != NULL)
7345 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007346 /* Check the pmbox pointer first. There is a race condition
7347 * between the mbox timeout handler getting executed in the
7348 * worklist and the mailbox actually completing. When this
7349 * race condition occurs, the mbox_active will be NULL.
7350 */
7351 spin_lock_irq(&phba->hbalock);
7352 if (pmbox == NULL) {
7353 lpfc_printf_log(phba, KERN_WARNING,
7354 LOG_MBOX | LOG_SLI,
7355 "0353 Active Mailbox cleared - mailbox timeout "
7356 "exiting\n");
7357 spin_unlock_irq(&phba->hbalock);
7358 return;
7359 }
7360
dea31012005-04-17 16:05:31 -05007361 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007362 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007363 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007364 mb->mbxCommand,
7365 phba->pport->port_state,
7366 phba->sli.sli_flag,
7367 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007368 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007369
James Smart1dcb58e2007-04-25 09:51:30 -04007370 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7371 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007372 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007373 */
James Smart2e0fef82007-06-17 19:56:36 -05007374 spin_lock_irq(&phba->pport->work_port_lock);
7375 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7376 spin_unlock_irq(&phba->pport->work_port_lock);
7377 spin_lock_irq(&phba->hbalock);
7378 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007379 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007380 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007381
James Smartdb55fba2014-04-04 13:52:02 -04007382 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007383
7384 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007385 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007386
7387 /* Reset the HBA device */
7388 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007389}
7390
James Smarte59058c2008-08-24 21:49:00 -04007391/**
James Smart3772a992009-05-22 14:50:54 -04007392 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007393 * @phba: Pointer to HBA context object.
7394 * @pmbox: Pointer to mailbox object.
7395 * @flag: Flag indicating how the mailbox need to be processed.
7396 *
7397 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007398 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7399 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007400 * The mailbox command can be submitted in polling mode, in which case
7401 * this function will wait in a polling loop for the completion of the
7402 * mailbox.
7403 * If the mailbox is submitted in no_wait mode (not polling) the
7404 * function will submit the command and returns immediately without waiting
7405 * for the mailbox completion. The no_wait is supported only when HBA
7406 * is in SLI2/SLI3 mode - interrupts are enabled.
7407 * The SLI interface allows only one mailbox pending at a time. If the
7408 * mailbox is issued in polling mode and there is already a mailbox
7409 * pending, then the function will return an error. If the mailbox is issued
7410 * in NO_WAIT mode and there is a mailbox pending already, the function
7411 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7412 * The sli layer owns the mailbox object until the completion of mailbox
7413 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7414 * return codes the caller owns the mailbox command after the return of
7415 * the function.
7416 **/
James Smart3772a992009-05-22 14:50:54 -04007417static int
7418lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7419 uint32_t flag)
dea31012005-04-17 16:05:31 -05007420{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007421 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007422 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007423 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007424 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007425 int i;
James Smart09372822008-01-11 01:52:54 -05007426 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007427 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007428 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007429 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007430 int processing_queue = 0;
7431
7432 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7433 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007434 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007435 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007436 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7437 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7438 return MBX_SUCCESS;
7439 }
James Smart58da1ff2008-04-07 10:15:56 -04007440 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007441 pmbox = lpfc_mbox_get(phba);
7442 if (!pmbox) {
7443 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7444 return MBX_SUCCESS;
7445 }
7446 }
dea31012005-04-17 16:05:31 -05007447
James Smarted957682007-06-17 19:56:37 -05007448 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007449 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007450 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007451 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007452 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007453 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007454 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007455 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007456 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007457 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007458 }
7459 }
7460
Linas Vepstas8d63f372007-02-14 14:28:36 -06007461 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007462 if (unlikely(pci_channel_offline(phba->pcidev))) {
7463 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7464 goto out_not_finished;
7465 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007466
James Smarta257bf92009-04-06 18:48:10 -04007467 /* If HBA has a deferred error attention, fail the iocb. */
7468 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7469 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7470 goto out_not_finished;
7471 }
7472
dea31012005-04-17 16:05:31 -05007473 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007474
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007475 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007476 status = MBX_SUCCESS;
7477
James Smart2e0fef82007-06-17 19:56:36 -05007478 if (phba->link_state == LPFC_HBA_ERROR) {
7479 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007480
7481 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007482 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7483 "(%d):0311 Mailbox command x%x cannot "
7484 "issue Data: x%x x%x\n",
7485 pmbox->vport ? pmbox->vport->vpi : 0,
7486 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007487 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007488 }
7489
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007490 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007491 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7492 !(hc_copy & HC_MBINT_ENA)) {
7493 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007495 "(%d):2528 Mailbox command x%x cannot "
7496 "issue Data: x%x x%x\n",
7497 pmbox->vport ? pmbox->vport->vpi : 0,
7498 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007499 goto out_not_finished;
7500 }
James Smart92908312006-03-07 15:04:13 -05007501 }
7502
dea31012005-04-17 16:05:31 -05007503 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7504 /* Polling for a mbox command when another one is already active
7505 * is not allowed in SLI. Also, the driver must have established
7506 * SLI2 mode to queue and process multiple mbox commands.
7507 */
7508
7509 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007510 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007511
7512 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7514 "(%d):2529 Mailbox command x%x "
7515 "cannot issue Data: x%x x%x\n",
7516 pmbox->vport ? pmbox->vport->vpi : 0,
7517 pmbox->u.mb.mbxCommand,
7518 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007519 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007520 }
7521
James Smart3772a992009-05-22 14:50:54 -04007522 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007523 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007524 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7526 "(%d):2530 Mailbox command x%x "
7527 "cannot issue Data: x%x x%x\n",
7528 pmbox->vport ? pmbox->vport->vpi : 0,
7529 pmbox->u.mb.mbxCommand,
7530 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007531 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007532 }
7533
dea31012005-04-17 16:05:31 -05007534 /* Another mailbox command is still being processed, queue this
7535 * command to be processed later.
7536 */
7537 lpfc_mbox_put(phba, pmbox);
7538
7539 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007540 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007541 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007542 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007543 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007544 mbx->mbxCommand,
7545 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007546 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007547
7548 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007549 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007550
James Smart858c9f62007-06-17 19:56:39 -05007551 if (pmbox->vport) {
7552 lpfc_debugfs_disc_trc(pmbox->vport,
7553 LPFC_DISC_TRC_MBOX_VPORT,
7554 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007555 (uint32_t)mbx->mbxCommand,
7556 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007557 }
7558 else {
7559 lpfc_debugfs_disc_trc(phba->pport,
7560 LPFC_DISC_TRC_MBOX,
7561 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007562 (uint32_t)mbx->mbxCommand,
7563 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007564 }
7565
James Smart2e0fef82007-06-17 19:56:36 -05007566 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007567 }
7568
dea31012005-04-17 16:05:31 -05007569 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7570
7571 /* If we are not polling, we MUST be in SLI2 mode */
7572 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007573 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007574 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007575 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007576 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007577 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007578 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7579 "(%d):2531 Mailbox command x%x "
7580 "cannot issue Data: x%x x%x\n",
7581 pmbox->vport ? pmbox->vport->vpi : 0,
7582 pmbox->u.mb.mbxCommand,
7583 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007584 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007585 }
7586 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007587 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7588 1000);
7589 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007590 }
7591
7592 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007593 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007594 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007595 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007596 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007597 mbx->mbxCommand,
7598 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007599 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007600
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007601 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007602 if (pmbox->vport) {
7603 lpfc_debugfs_disc_trc(pmbox->vport,
7604 LPFC_DISC_TRC_MBOX_VPORT,
7605 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007606 (uint32_t)mbx->mbxCommand,
7607 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007608 }
7609 else {
7610 lpfc_debugfs_disc_trc(phba->pport,
7611 LPFC_DISC_TRC_MBOX,
7612 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007613 (uint32_t)mbx->mbxCommand,
7614 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007615 }
7616 }
7617
dea31012005-04-17 16:05:31 -05007618 psli->slistat.mbox_cmd++;
7619 evtctr = psli->slistat.mbox_event;
7620
7621 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007622 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007623
James Smart3772a992009-05-22 14:50:54 -04007624 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007625 /* Populate mbox extension offset word. */
7626 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007627 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007628 = (uint8_t *)phba->mbox_ext
7629 - (uint8_t *)phba->mbox;
7630 }
7631
7632 /* Copy the mailbox extension data */
7633 if (pmbox->in_ext_byte_len && pmbox->context2) {
7634 lpfc_sli_pcimem_bcopy(pmbox->context2,
7635 (uint8_t *)phba->mbox_ext,
7636 pmbox->in_ext_byte_len);
7637 }
7638 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007639 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007640 } else {
James Smart7a470272010-03-15 11:25:20 -04007641 /* Populate mbox extension offset word. */
7642 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007643 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007644 = MAILBOX_HBA_EXT_OFFSET;
7645
7646 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007647 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007648 lpfc_memcpy_to_slim(phba->MBslimaddr +
7649 MAILBOX_HBA_EXT_OFFSET,
7650 pmbox->context2, pmbox->in_ext_byte_len);
7651
James Smart895427b2017-02-12 13:52:30 -08007652 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007653 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007654 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7655 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007656
7657 /* First copy mbox command data to HBA SLIM, skip past first
7658 word */
7659 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007660 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007661 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7662
7663 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007664 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007665 to_slim = phba->MBslimaddr;
7666 writel(ldata, to_slim);
7667 readl(to_slim); /* flush */
7668
James Smart895427b2017-02-12 13:52:30 -08007669 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007670 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007671 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007672 }
7673
7674 wmb();
dea31012005-04-17 16:05:31 -05007675
7676 switch (flag) {
7677 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007678 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007679 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007680 /* Interrupt board to do it */
7681 writel(CA_MBATT, phba->CAregaddr);
7682 readl(phba->CAregaddr); /* flush */
7683 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007684 break;
7685
7686 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007687 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007688 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007689 /* Interrupt board to do it */
7690 writel(CA_MBATT, phba->CAregaddr);
7691 readl(phba->CAregaddr); /* flush */
7692
James Smart3772a992009-05-22 14:50:54 -04007693 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007694 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007695 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007696 word0 = le32_to_cpu(word0);
7697 } else {
7698 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007699 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7700 spin_unlock_irqrestore(&phba->hbalock,
7701 drvr_flag);
7702 goto out_not_finished;
7703 }
dea31012005-04-17 16:05:31 -05007704 }
7705
7706 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007707 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7708 spin_unlock_irqrestore(&phba->hbalock,
7709 drvr_flag);
7710 goto out_not_finished;
7711 }
James Smarta183a152011-10-10 21:32:43 -04007712 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7713 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007714 i = 0;
dea31012005-04-17 16:05:31 -05007715 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007716 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7717 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007718 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007719 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007720 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007721 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007722 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007723 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007724 }
7725
7726 /* Check if we took a mbox interrupt while we were
7727 polling */
7728 if (((word0 & OWN_CHIP) != OWN_CHIP)
7729 && (evtctr != psli->slistat.mbox_event))
7730 break;
7731
James Smart09372822008-01-11 01:52:54 -05007732 if (i++ > 10) {
7733 spin_unlock_irqrestore(&phba->hbalock,
7734 drvr_flag);
7735 msleep(1);
7736 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7737 }
dea31012005-04-17 16:05:31 -05007738
James Smart3772a992009-05-22 14:50:54 -04007739 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007740 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007741 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007742 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007743 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007744 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007745 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007746 /* Check real SLIM for any errors */
7747 slimword0 = readl(phba->MBslimaddr);
7748 slimmb = (MAILBOX_t *) & slimword0;
7749 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7750 && slimmb->mbxStatus) {
7751 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007752 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007753 word0 = slimword0;
7754 }
7755 }
7756 } else {
7757 /* First copy command data */
7758 word0 = readl(phba->MBslimaddr);
7759 }
7760 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007761 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7762 spin_unlock_irqrestore(&phba->hbalock,
7763 drvr_flag);
7764 goto out_not_finished;
7765 }
dea31012005-04-17 16:05:31 -05007766 }
7767
James Smart3772a992009-05-22 14:50:54 -04007768 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007769 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007770 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7771 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007772 /* Copy the mailbox extension data */
7773 if (pmbox->out_ext_byte_len && pmbox->context2) {
7774 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7775 pmbox->context2,
7776 pmbox->out_ext_byte_len);
7777 }
dea31012005-04-17 16:05:31 -05007778 } else {
7779 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007780 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007781 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007782 /* Copy the mailbox extension data */
7783 if (pmbox->out_ext_byte_len && pmbox->context2) {
7784 lpfc_memcpy_from_slim(pmbox->context2,
7785 phba->MBslimaddr +
7786 MAILBOX_HBA_EXT_OFFSET,
7787 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007788 }
7789 }
7790
7791 writel(HA_MBATT, phba->HAregaddr);
7792 readl(phba->HAregaddr); /* flush */
7793
7794 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007795 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007796 }
7797
James Smart2e0fef82007-06-17 19:56:36 -05007798 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7799 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007800
7801out_not_finished:
7802 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007803 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007804 lpfc_mbox_cmpl_put(phba, pmbox);
7805 }
7806 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007807}
7808
James Smarte59058c2008-08-24 21:49:00 -04007809/**
James Smartf1126682009-06-10 17:22:44 -04007810 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7811 * @phba: Pointer to HBA context object.
7812 *
7813 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7814 * the driver internal pending mailbox queue. It will then try to wait out the
7815 * possible outstanding mailbox command before return.
7816 *
7817 * Returns:
7818 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7819 * the outstanding mailbox command timed out.
7820 **/
7821static int
7822lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7823{
7824 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007825 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007826 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007827
7828 /* Mark the asynchronous mailbox command posting as blocked */
7829 spin_lock_irq(&phba->hbalock);
7830 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007831 /* Determine how long we might wait for the active mailbox
7832 * command to be gracefully completed by firmware.
7833 */
James Smarta183a152011-10-10 21:32:43 -04007834 if (phba->sli.mbox_active)
7835 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7836 phba->sli.mbox_active) *
7837 1000) + jiffies;
7838 spin_unlock_irq(&phba->hbalock);
7839
James Smarte8d3c3b2013-10-10 12:21:30 -04007840 /* Make sure the mailbox is really active */
7841 if (timeout)
7842 lpfc_sli4_process_missed_mbox_completions(phba);
7843
James Smartf1126682009-06-10 17:22:44 -04007844 /* Wait for the outstnading mailbox command to complete */
7845 while (phba->sli.mbox_active) {
7846 /* Check active mailbox complete status every 2ms */
7847 msleep(2);
7848 if (time_after(jiffies, timeout)) {
7849 /* Timeout, marked the outstanding cmd not complete */
7850 rc = 1;
7851 break;
7852 }
7853 }
7854
7855 /* Can not cleanly block async mailbox command, fails it */
7856 if (rc) {
7857 spin_lock_irq(&phba->hbalock);
7858 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7859 spin_unlock_irq(&phba->hbalock);
7860 }
7861 return rc;
7862}
7863
7864/**
7865 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7866 * @phba: Pointer to HBA context object.
7867 *
7868 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7869 * commands from the driver internal pending mailbox queue. It makes sure
7870 * that there is no outstanding mailbox command before resuming posting
7871 * asynchronous mailbox commands. If, for any reason, there is outstanding
7872 * mailbox command, it will try to wait it out before resuming asynchronous
7873 * mailbox command posting.
7874 **/
7875static void
7876lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7877{
7878 struct lpfc_sli *psli = &phba->sli;
7879
7880 spin_lock_irq(&phba->hbalock);
7881 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7882 /* Asynchronous mailbox posting is not blocked, do nothing */
7883 spin_unlock_irq(&phba->hbalock);
7884 return;
7885 }
7886
7887 /* Outstanding synchronous mailbox command is guaranteed to be done,
7888 * successful or timeout, after timing-out the outstanding mailbox
7889 * command shall always be removed, so just unblock posting async
7890 * mailbox command and resume
7891 */
7892 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7893 spin_unlock_irq(&phba->hbalock);
7894
7895 /* wake up worker thread to post asynchronlous mailbox command */
7896 lpfc_worker_wake_up(phba);
7897}
7898
7899/**
James Smart2d843ed2012-09-29 11:29:06 -04007900 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7901 * @phba: Pointer to HBA context object.
7902 * @mboxq: Pointer to mailbox object.
7903 *
7904 * The function waits for the bootstrap mailbox register ready bit from
7905 * port for twice the regular mailbox command timeout value.
7906 *
7907 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7908 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7909 **/
7910static int
7911lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7912{
7913 uint32_t db_ready;
7914 unsigned long timeout;
7915 struct lpfc_register bmbx_reg;
7916
7917 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7918 * 1000) + jiffies;
7919
7920 do {
7921 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7922 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7923 if (!db_ready)
7924 msleep(2);
7925
7926 if (time_after(jiffies, timeout))
7927 return MBXERR_ERROR;
7928 } while (!db_ready);
7929
7930 return 0;
7931}
7932
7933/**
James Smartda0436e2009-05-22 14:51:39 -04007934 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7935 * @phba: Pointer to HBA context object.
7936 * @mboxq: Pointer to mailbox object.
7937 *
7938 * The function posts a mailbox to the port. The mailbox is expected
7939 * to be comletely filled in and ready for the port to operate on it.
7940 * This routine executes a synchronous completion operation on the
7941 * mailbox by polling for its completion.
7942 *
7943 * The caller must not be holding any locks when calling this routine.
7944 *
7945 * Returns:
7946 * MBX_SUCCESS - mailbox posted successfully
7947 * Any of the MBX error values.
7948 **/
7949static int
7950lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7951{
7952 int rc = MBX_SUCCESS;
7953 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007954 uint32_t mcqe_status;
7955 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007956 struct lpfc_sli *psli = &phba->sli;
7957 struct lpfc_mqe *mb = &mboxq->u.mqe;
7958 struct lpfc_bmbx_create *mbox_rgn;
7959 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007960
7961 /*
7962 * Only one mailbox can be active to the bootstrap mailbox region
7963 * at a time and there is no queueing provided.
7964 */
7965 spin_lock_irqsave(&phba->hbalock, iflag);
7966 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7967 spin_unlock_irqrestore(&phba->hbalock, iflag);
7968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007969 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007970 "cannot issue Data: x%x x%x\n",
7971 mboxq->vport ? mboxq->vport->vpi : 0,
7972 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007973 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7974 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007975 psli->sli_flag, MBX_POLL);
7976 return MBXERR_ERROR;
7977 }
7978 /* The server grabs the token and owns it until release */
7979 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7980 phba->sli.mbox_active = mboxq;
7981 spin_unlock_irqrestore(&phba->hbalock, iflag);
7982
James Smart2d843ed2012-09-29 11:29:06 -04007983 /* wait for bootstrap mbox register for readyness */
7984 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7985 if (rc)
7986 goto exit;
7987
James Smartda0436e2009-05-22 14:51:39 -04007988 /*
7989 * Initialize the bootstrap memory region to avoid stale data areas
7990 * in the mailbox post. Then copy the caller's mailbox contents to
7991 * the bmbx mailbox region.
7992 */
7993 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7994 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7995 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7996 sizeof(struct lpfc_mqe));
7997
7998 /* Post the high mailbox dma address to the port and wait for ready. */
7999 dma_address = &phba->sli4_hba.bmbx.dma_address;
8000 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8001
James Smart2d843ed2012-09-29 11:29:06 -04008002 /* wait for bootstrap mbox register for hi-address write done */
8003 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8004 if (rc)
8005 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008006
8007 /* Post the low mailbox dma address to the port. */
8008 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008009
James Smart2d843ed2012-09-29 11:29:06 -04008010 /* wait for bootstrap mbox register for low address write done */
8011 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8012 if (rc)
8013 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008014
8015 /*
8016 * Read the CQ to ensure the mailbox has completed.
8017 * If so, update the mailbox status so that the upper layers
8018 * can complete the request normally.
8019 */
8020 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8021 sizeof(struct lpfc_mqe));
8022 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8023 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8024 sizeof(struct lpfc_mcqe));
8025 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008026 /*
8027 * When the CQE status indicates a failure and the mailbox status
8028 * indicates success then copy the CQE status into the mailbox status
8029 * (and prefix it with x4000).
8030 */
James Smartda0436e2009-05-22 14:51:39 -04008031 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008032 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8033 bf_set(lpfc_mqe_status, mb,
8034 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008035 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008036 } else
8037 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008038
8039 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008040 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008041 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8042 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008043 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8044 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8045 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008046 bf_get(lpfc_mqe_status, mb),
8047 mb->un.mb_words[0], mb->un.mb_words[1],
8048 mb->un.mb_words[2], mb->un.mb_words[3],
8049 mb->un.mb_words[4], mb->un.mb_words[5],
8050 mb->un.mb_words[6], mb->un.mb_words[7],
8051 mb->un.mb_words[8], mb->un.mb_words[9],
8052 mb->un.mb_words[10], mb->un.mb_words[11],
8053 mb->un.mb_words[12], mboxq->mcqe.word0,
8054 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8055 mboxq->mcqe.trailer);
8056exit:
8057 /* We are holding the token, no needed for lock when release */
8058 spin_lock_irqsave(&phba->hbalock, iflag);
8059 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8060 phba->sli.mbox_active = NULL;
8061 spin_unlock_irqrestore(&phba->hbalock, iflag);
8062 return rc;
8063}
8064
8065/**
8066 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8067 * @phba: Pointer to HBA context object.
8068 * @pmbox: Pointer to mailbox object.
8069 * @flag: Flag indicating how the mailbox need to be processed.
8070 *
8071 * This function is called by discovery code and HBA management code to submit
8072 * a mailbox command to firmware with SLI-4 interface spec.
8073 *
8074 * Return codes the caller owns the mailbox command after the return of the
8075 * function.
8076 **/
8077static int
8078lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8079 uint32_t flag)
8080{
8081 struct lpfc_sli *psli = &phba->sli;
8082 unsigned long iflags;
8083 int rc;
8084
James Smartb76f2dc2011-07-22 18:37:42 -04008085 /* dump from issue mailbox command if setup */
8086 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8087
James Smart8fa38512009-07-19 10:01:03 -04008088 rc = lpfc_mbox_dev_check(phba);
8089 if (unlikely(rc)) {
8090 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008091 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008092 "cannot issue Data: x%x x%x\n",
8093 mboxq->vport ? mboxq->vport->vpi : 0,
8094 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008095 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8096 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008097 psli->sli_flag, flag);
8098 goto out_not_finished;
8099 }
8100
James Smartda0436e2009-05-22 14:51:39 -04008101 /* Detect polling mode and jump to a handler */
8102 if (!phba->sli4_hba.intr_enable) {
8103 if (flag == MBX_POLL)
8104 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8105 else
8106 rc = -EIO;
8107 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008108 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008109 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008110 "(x%x/x%x) failure: "
8111 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8112 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008113 mboxq->vport ? mboxq->vport->vpi : 0,
8114 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008115 lpfc_sli_config_mbox_subsys_get(phba,
8116 mboxq),
8117 lpfc_sli_config_mbox_opcode_get(phba,
8118 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008119 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8120 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8121 bf_get(lpfc_mcqe_ext_status,
8122 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008123 psli->sli_flag, flag);
8124 return rc;
8125 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008126 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8127 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008128 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008129 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008130 mboxq->vport ? mboxq->vport->vpi : 0,
8131 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008132 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8133 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008134 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008135 /* Try to block the asynchronous mailbox posting */
8136 rc = lpfc_sli4_async_mbox_block(phba);
8137 if (!rc) {
8138 /* Successfully blocked, now issue sync mbox cmd */
8139 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8140 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008141 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008142 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008143 "(%d):2597 Sync Mailbox command "
8144 "x%x (x%x/x%x) failure: "
8145 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8146 "Data: x%x x%x\n,",
8147 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008148 mboxq->u.mb.mbxCommand,
8149 lpfc_sli_config_mbox_subsys_get(phba,
8150 mboxq),
8151 lpfc_sli_config_mbox_opcode_get(phba,
8152 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008153 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8154 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8155 bf_get(lpfc_mcqe_ext_status,
8156 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008157 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008158 /* Unblock the async mailbox posting afterward */
8159 lpfc_sli4_async_mbox_unblock(phba);
8160 }
8161 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008162 }
8163
8164 /* Now, interrupt mode asynchrous mailbox command */
8165 rc = lpfc_mbox_cmd_check(phba, mboxq);
8166 if (rc) {
8167 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008168 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008169 "cannot issue Data: x%x x%x\n",
8170 mboxq->vport ? mboxq->vport->vpi : 0,
8171 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008172 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8173 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008174 psli->sli_flag, flag);
8175 goto out_not_finished;
8176 }
James Smartda0436e2009-05-22 14:51:39 -04008177
8178 /* Put the mailbox command to the driver internal FIFO */
8179 psli->slistat.mbox_busy++;
8180 spin_lock_irqsave(&phba->hbalock, iflags);
8181 lpfc_mbox_put(phba, mboxq);
8182 spin_unlock_irqrestore(&phba->hbalock, iflags);
8183 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8184 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008185 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008186 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8187 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008188 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8189 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008190 phba->pport->port_state,
8191 psli->sli_flag, MBX_NOWAIT);
8192 /* Wake up worker thread to transport mailbox command from head */
8193 lpfc_worker_wake_up(phba);
8194
8195 return MBX_BUSY;
8196
8197out_not_finished:
8198 return MBX_NOT_FINISHED;
8199}
8200
8201/**
8202 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8203 * @phba: Pointer to HBA context object.
8204 *
8205 * This function is called by worker thread to send a mailbox command to
8206 * SLI4 HBA firmware.
8207 *
8208 **/
8209int
8210lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8211{
8212 struct lpfc_sli *psli = &phba->sli;
8213 LPFC_MBOXQ_t *mboxq;
8214 int rc = MBX_SUCCESS;
8215 unsigned long iflags;
8216 struct lpfc_mqe *mqe;
8217 uint32_t mbx_cmnd;
8218
8219 /* Check interrupt mode before post async mailbox command */
8220 if (unlikely(!phba->sli4_hba.intr_enable))
8221 return MBX_NOT_FINISHED;
8222
8223 /* Check for mailbox command service token */
8224 spin_lock_irqsave(&phba->hbalock, iflags);
8225 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8226 spin_unlock_irqrestore(&phba->hbalock, iflags);
8227 return MBX_NOT_FINISHED;
8228 }
8229 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8230 spin_unlock_irqrestore(&phba->hbalock, iflags);
8231 return MBX_NOT_FINISHED;
8232 }
8233 if (unlikely(phba->sli.mbox_active)) {
8234 spin_unlock_irqrestore(&phba->hbalock, iflags);
8235 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8236 "0384 There is pending active mailbox cmd\n");
8237 return MBX_NOT_FINISHED;
8238 }
8239 /* Take the mailbox command service token */
8240 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8241
8242 /* Get the next mailbox command from head of queue */
8243 mboxq = lpfc_mbox_get(phba);
8244
8245 /* If no more mailbox command waiting for post, we're done */
8246 if (!mboxq) {
8247 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8248 spin_unlock_irqrestore(&phba->hbalock, iflags);
8249 return MBX_SUCCESS;
8250 }
8251 phba->sli.mbox_active = mboxq;
8252 spin_unlock_irqrestore(&phba->hbalock, iflags);
8253
8254 /* Check device readiness for posting mailbox command */
8255 rc = lpfc_mbox_dev_check(phba);
8256 if (unlikely(rc))
8257 /* Driver clean routine will clean up pending mailbox */
8258 goto out_not_finished;
8259
8260 /* Prepare the mbox command to be posted */
8261 mqe = &mboxq->u.mqe;
8262 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8263
8264 /* Start timer for the mbox_tmo and log some mailbox post messages */
8265 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008266 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008267
8268 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008269 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008270 "x%x x%x\n",
8271 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008272 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8273 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008274 phba->pport->port_state, psli->sli_flag);
8275
8276 if (mbx_cmnd != MBX_HEARTBEAT) {
8277 if (mboxq->vport) {
8278 lpfc_debugfs_disc_trc(mboxq->vport,
8279 LPFC_DISC_TRC_MBOX_VPORT,
8280 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8281 mbx_cmnd, mqe->un.mb_words[0],
8282 mqe->un.mb_words[1]);
8283 } else {
8284 lpfc_debugfs_disc_trc(phba->pport,
8285 LPFC_DISC_TRC_MBOX,
8286 "MBOX Send: cmd:x%x mb:x%x x%x",
8287 mbx_cmnd, mqe->un.mb_words[0],
8288 mqe->un.mb_words[1]);
8289 }
8290 }
8291 psli->slistat.mbox_cmd++;
8292
8293 /* Post the mailbox command to the port */
8294 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8295 if (rc != MBX_SUCCESS) {
8296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008297 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008298 "cannot issue Data: x%x x%x\n",
8299 mboxq->vport ? mboxq->vport->vpi : 0,
8300 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008301 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8302 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008303 psli->sli_flag, MBX_NOWAIT);
8304 goto out_not_finished;
8305 }
8306
8307 return rc;
8308
8309out_not_finished:
8310 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008311 if (phba->sli.mbox_active) {
8312 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8313 __lpfc_mbox_cmpl_put(phba, mboxq);
8314 /* Release the token */
8315 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8316 phba->sli.mbox_active = NULL;
8317 }
James Smartda0436e2009-05-22 14:51:39 -04008318 spin_unlock_irqrestore(&phba->hbalock, iflags);
8319
8320 return MBX_NOT_FINISHED;
8321}
8322
8323/**
8324 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8325 * @phba: Pointer to HBA context object.
8326 * @pmbox: Pointer to mailbox object.
8327 * @flag: Flag indicating how the mailbox need to be processed.
8328 *
8329 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8330 * the API jump table function pointer from the lpfc_hba struct.
8331 *
8332 * Return codes the caller owns the mailbox command after the return of the
8333 * function.
8334 **/
8335int
8336lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8337{
8338 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8339}
8340
8341/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008342 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008343 * @phba: The hba struct for which this call is being executed.
8344 * @dev_grp: The HBA PCI-Device group number.
8345 *
8346 * This routine sets up the mbox interface API function jump table in @phba
8347 * struct.
8348 * Returns: 0 - success, -ENODEV - failure.
8349 **/
8350int
8351lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8352{
8353
8354 switch (dev_grp) {
8355 case LPFC_PCI_DEV_LP:
8356 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8357 phba->lpfc_sli_handle_slow_ring_event =
8358 lpfc_sli_handle_slow_ring_event_s3;
8359 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8360 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8361 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8362 break;
8363 case LPFC_PCI_DEV_OC:
8364 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8365 phba->lpfc_sli_handle_slow_ring_event =
8366 lpfc_sli_handle_slow_ring_event_s4;
8367 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8368 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8369 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8370 break;
8371 default:
8372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8373 "1420 Invalid HBA PCI-device group: 0x%x\n",
8374 dev_grp);
8375 return -ENODEV;
8376 break;
8377 }
8378 return 0;
8379}
8380
8381/**
James Smart3621a712009-04-06 18:47:14 -04008382 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008383 * @phba: Pointer to HBA context object.
8384 * @pring: Pointer to driver SLI ring object.
8385 * @piocb: Pointer to address of newly added command iocb.
8386 *
8387 * This function is called with hbalock held to add a command
8388 * iocb to the txq when SLI layer cannot submit the command iocb
8389 * to the ring.
8390 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008391void
James Smart92d7f7b2007-06-17 19:56:38 -05008392__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008393 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008394{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008395 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008396 /* Insert the caller's iocb in the txq tail for later processing. */
8397 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008398}
8399
James Smarte59058c2008-08-24 21:49:00 -04008400/**
James Smart3621a712009-04-06 18:47:14 -04008401 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008402 * @phba: Pointer to HBA context object.
8403 * @pring: Pointer to driver SLI ring object.
8404 * @piocb: Pointer to address of newly added command iocb.
8405 *
8406 * This function is called with hbalock held before a new
8407 * iocb is submitted to the firmware. This function checks
8408 * txq to flush the iocbs in txq to Firmware before
8409 * submitting new iocbs to the Firmware.
8410 * If there are iocbs in the txq which need to be submitted
8411 * to firmware, lpfc_sli_next_iocb returns the first element
8412 * of the txq after dequeuing it from txq.
8413 * If there is no iocb in the txq then the function will return
8414 * *piocb and *piocb is set to NULL. Caller needs to check
8415 * *piocb to find if there are more commands in the txq.
8416 **/
dea31012005-04-17 16:05:31 -05008417static struct lpfc_iocbq *
8418lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008419 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008420{
8421 struct lpfc_iocbq * nextiocb;
8422
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008423 lockdep_assert_held(&phba->hbalock);
8424
dea31012005-04-17 16:05:31 -05008425 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8426 if (!nextiocb) {
8427 nextiocb = *piocb;
8428 *piocb = NULL;
8429 }
8430
8431 return nextiocb;
8432}
8433
James Smarte59058c2008-08-24 21:49:00 -04008434/**
James Smart3772a992009-05-22 14:50:54 -04008435 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008436 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008437 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008438 * @piocb: Pointer to command iocb.
8439 * @flag: Flag indicating if this command can be put into txq.
8440 *
James Smart3772a992009-05-22 14:50:54 -04008441 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8442 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8443 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8444 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8445 * this function allows only iocbs for posting buffers. This function finds
8446 * next available slot in the command ring and posts the command to the
8447 * available slot and writes the port attention register to request HBA start
8448 * processing new iocb. If there is no slot available in the ring and
8449 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8450 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008451 *
James Smart3772a992009-05-22 14:50:54 -04008452 * This function is called with hbalock held. The function will return success
8453 * after it successfully submit the iocb to firmware or after adding to the
8454 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008455 **/
James Smart98c9ea52007-10-27 13:37:33 -04008456static int
James Smart3772a992009-05-22 14:50:54 -04008457__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008458 struct lpfc_iocbq *piocb, uint32_t flag)
8459{
8460 struct lpfc_iocbq *nextiocb;
8461 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008462 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008463
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008464 lockdep_assert_held(&phba->hbalock);
8465
James Smart92d7f7b2007-06-17 19:56:38 -05008466 if (piocb->iocb_cmpl && (!piocb->vport) &&
8467 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8468 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8469 lpfc_printf_log(phba, KERN_ERR,
8470 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008471 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008472 piocb->iocb.ulpCommand);
8473 dump_stack();
8474 return IOCB_ERROR;
8475 }
8476
8477
Linas Vepstas8d63f372007-02-14 14:28:36 -06008478 /* If the PCI channel is in offline state, do not post iocbs. */
8479 if (unlikely(pci_channel_offline(phba->pcidev)))
8480 return IOCB_ERROR;
8481
James Smarta257bf92009-04-06 18:48:10 -04008482 /* If HBA has a deferred error attention, fail the iocb. */
8483 if (unlikely(phba->hba_flag & DEFER_ERATT))
8484 return IOCB_ERROR;
8485
dea31012005-04-17 16:05:31 -05008486 /*
8487 * We should never get an IOCB if we are in a < LINK_DOWN state
8488 */
James Smart2e0fef82007-06-17 19:56:36 -05008489 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008490 return IOCB_ERROR;
8491
8492 /*
8493 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008494 * outstanding event.
dea31012005-04-17 16:05:31 -05008495 */
James Smart0b727fe2007-10-27 13:37:25 -04008496 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008497 goto iocb_busy;
8498
James Smart2e0fef82007-06-17 19:56:36 -05008499 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008500 /*
James Smart2680eea2007-04-25 09:52:55 -04008501 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008502 * can be issued if the link is not up.
8503 */
8504 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008505 case CMD_GEN_REQUEST64_CR:
8506 case CMD_GEN_REQUEST64_CX:
8507 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8508 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008509 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008510 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8511 MENLO_TRANSPORT_TYPE))
8512
8513 goto iocb_busy;
8514 break;
dea31012005-04-17 16:05:31 -05008515 case CMD_QUE_RING_BUF_CN:
8516 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008517 /*
8518 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8519 * completion, iocb_cmpl MUST be 0.
8520 */
8521 if (piocb->iocb_cmpl)
8522 piocb->iocb_cmpl = NULL;
8523 /*FALLTHROUGH*/
8524 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008525 case CMD_CLOSE_XRI_CN:
8526 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008527 break;
8528 default:
8529 goto iocb_busy;
8530 }
8531
8532 /*
8533 * For FCP commands, we must be in a state where we can process link
8534 * attention events.
8535 */
James Smart895427b2017-02-12 13:52:30 -08008536 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008537 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008538 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008539 }
dea31012005-04-17 16:05:31 -05008540
dea31012005-04-17 16:05:31 -05008541 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8542 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8543 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8544
8545 if (iocb)
8546 lpfc_sli_update_ring(phba, pring);
8547 else
8548 lpfc_sli_update_full_ring(phba, pring);
8549
8550 if (!piocb)
8551 return IOCB_SUCCESS;
8552
8553 goto out_busy;
8554
8555 iocb_busy:
8556 pring->stats.iocb_cmd_delay++;
8557
8558 out_busy:
8559
8560 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008561 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008562 return IOCB_SUCCESS;
8563 }
8564
8565 return IOCB_BUSY;
8566}
8567
James Smart3772a992009-05-22 14:50:54 -04008568/**
James Smart4f774512009-05-22 14:52:35 -04008569 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8570 * @phba: Pointer to HBA context object.
8571 * @piocb: Pointer to command iocb.
8572 * @sglq: Pointer to the scatter gather queue object.
8573 *
8574 * This routine converts the bpl or bde that is in the IOCB
8575 * to a sgl list for the sli4 hardware. The physical address
8576 * of the bpl/bde is converted back to a virtual address.
8577 * If the IOCB contains a BPL then the list of BDE's is
8578 * converted to sli4_sge's. If the IOCB contains a single
8579 * BDE then it is converted to a single sli_sge.
8580 * The IOCB is still in cpu endianess so the contents of
8581 * the bpl can be used without byte swapping.
8582 *
8583 * Returns valid XRI = Success, NO_XRI = Failure.
8584**/
8585static uint16_t
8586lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8587 struct lpfc_sglq *sglq)
8588{
8589 uint16_t xritag = NO_XRI;
8590 struct ulp_bde64 *bpl = NULL;
8591 struct ulp_bde64 bde;
8592 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008593 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008594 IOCB_t *icmd;
8595 int numBdes = 0;
8596 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008597 uint32_t offset = 0; /* accumulated offset in the sg request list */
8598 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008599
8600 if (!piocbq || !sglq)
8601 return xritag;
8602
8603 sgl = (struct sli4_sge *)sglq->sgl;
8604 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008605 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8606 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008607 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8608 numBdes = icmd->un.genreq64.bdl.bdeSize /
8609 sizeof(struct ulp_bde64);
8610 /* The addrHigh and addrLow fields within the IOCB
8611 * have not been byteswapped yet so there is no
8612 * need to swap them back.
8613 */
James Smart1b511972011-12-13 13:23:09 -05008614 if (piocbq->context3)
8615 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8616 else
8617 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008618
James Smart1b511972011-12-13 13:23:09 -05008619 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008620 if (!bpl)
8621 return xritag;
8622
8623 for (i = 0; i < numBdes; i++) {
8624 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008625 sgl->addr_hi = bpl->addrHigh;
8626 sgl->addr_lo = bpl->addrLow;
8627
James Smart05580562011-05-24 11:40:48 -04008628 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008629 if ((i+1) == numBdes)
8630 bf_set(lpfc_sli4_sge_last, sgl, 1);
8631 else
8632 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008633 /* swap the size field back to the cpu so we
8634 * can assign it to the sgl.
8635 */
8636 bde.tus.w = le32_to_cpu(bpl->tus.w);
8637 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008638 /* The offsets in the sgl need to be accumulated
8639 * separately for the request and reply lists.
8640 * The request is always first, the reply follows.
8641 */
8642 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8643 /* add up the reply sg entries */
8644 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8645 inbound++;
8646 /* first inbound? reset the offset */
8647 if (inbound == 1)
8648 offset = 0;
8649 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008650 bf_set(lpfc_sli4_sge_type, sgl,
8651 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008652 offset += bde.tus.f.bdeSize;
8653 }
James Smart546fc852011-03-11 16:06:29 -05008654 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008655 bpl++;
8656 sgl++;
8657 }
8658 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8659 /* The addrHigh and addrLow fields of the BDE have not
8660 * been byteswapped yet so they need to be swapped
8661 * before putting them in the sgl.
8662 */
8663 sgl->addr_hi =
8664 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8665 sgl->addr_lo =
8666 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008667 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008668 bf_set(lpfc_sli4_sge_last, sgl, 1);
8669 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008670 sgl->sge_len =
8671 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008672 }
8673 return sglq->sli4_xritag;
8674}
8675
8676/**
James Smart4f774512009-05-22 14:52:35 -04008677 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8678 * @phba: Pointer to HBA context object.
8679 * @piocb: Pointer to command iocb.
8680 * @wqe: Pointer to the work queue entry.
8681 *
8682 * This routine converts the iocb command to its Work Queue Entry
8683 * equivalent. The wqe pointer should not have any fields set when
8684 * this routine is called because it will memcpy over them.
8685 * This routine does not set the CQ_ID or the WQEC bits in the
8686 * wqe.
8687 *
8688 * Returns: 0 = Success, IOCB_ERROR = Failure.
8689 **/
8690static int
8691lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8692 union lpfc_wqe *wqe)
8693{
James Smart5ffc2662009-11-18 15:39:44 -05008694 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008695 uint8_t ct = 0;
8696 uint32_t fip;
8697 uint32_t abort_tag;
8698 uint8_t command_type = ELS_COMMAND_NON_FIP;
8699 uint8_t cmnd;
8700 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008701 uint16_t abrt_iotag;
8702 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008703 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008704 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008705 int numBdes, i;
8706 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008707 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008708 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008709 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008710
James Smart45ed1192009-10-02 15:17:02 -04008711 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008712 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008713 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008714 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008715 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008716 command_type = ELS_COMMAND_FIP;
8717 else
8718 command_type = ELS_COMMAND_NON_FIP;
8719
James Smartb5c53952016-03-31 14:12:30 -07008720 if (phba->fcp_embed_io)
8721 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008722 /* Some of the fields are in the right position already */
8723 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008724 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8725 /* The ct field has moved so reset */
8726 wqe->generic.wqe_com.word7 = 0;
8727 wqe->generic.wqe_com.word10 = 0;
8728 }
James Smartb5c53952016-03-31 14:12:30 -07008729
8730 abort_tag = (uint32_t) iocbq->iotag;
8731 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008732 /* words0-2 bpl convert bde */
8733 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008734 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8735 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008736 bpl = (struct ulp_bde64 *)
8737 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8738 if (!bpl)
8739 return IOCB_ERROR;
8740
8741 /* Should already be byte swapped. */
8742 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8743 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8744 /* swap the size field back to the cpu so we
8745 * can assign it to the sgl.
8746 */
8747 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008748 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8749 total_len = 0;
8750 for (i = 0; i < numBdes; i++) {
8751 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8752 total_len += bde.tus.f.bdeSize;
8753 }
James Smart4f774512009-05-22 14:52:35 -04008754 } else
James Smart5ffc2662009-11-18 15:39:44 -05008755 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008756
8757 iocbq->iocb.ulpIoTag = iocbq->iotag;
8758 cmnd = iocbq->iocb.ulpCommand;
8759
8760 switch (iocbq->iocb.ulpCommand) {
8761 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008762 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8763 ndlp = iocbq->context_un.ndlp;
8764 else
8765 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008766 if (!iocbq->iocb.ulpLe) {
8767 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8768 "2007 Only Limited Edition cmd Format"
8769 " supported 0x%x\n",
8770 iocbq->iocb.ulpCommand);
8771 return IOCB_ERROR;
8772 }
James Smartff78d8f2011-12-13 13:21:35 -05008773
James Smart5ffc2662009-11-18 15:39:44 -05008774 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008775 /* Els_reguest64 has a TMO */
8776 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8777 iocbq->iocb.ulpTimeout);
8778 /* Need a VF for word 4 set the vf bit*/
8779 bf_set(els_req64_vf, &wqe->els_req, 0);
8780 /* And a VFID for word 12 */
8781 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008782 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008783 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8784 iocbq->iocb.ulpContext);
8785 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8786 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008787 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008788 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008789 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8790 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008791 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8792 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008793 if_type = bf_get(lpfc_sli_intf_if_type,
8794 &phba->sli4_hba.sli_intf);
8795 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008796 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008797 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008798 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008799 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008800 *pcmd == ELS_CMD_PLOGI)) {
8801 bf_set(els_req64_sp, &wqe->els_req, 1);
8802 bf_set(els_req64_sid, &wqe->els_req,
8803 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008804 if ((*pcmd == ELS_CMD_FLOGI) &&
8805 !(phba->fc_topology ==
8806 LPFC_TOPOLOGY_LOOP))
8807 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008808 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8809 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008810 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008811 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008812 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8813 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8814 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8815 }
James Smartc8685952009-11-18 15:39:16 -05008816 }
James Smart6d368e52011-05-24 11:44:12 -04008817 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8818 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008819 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8820 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8821 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8822 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8823 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8824 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008825 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008826 break;
James Smart5ffc2662009-11-18 15:39:44 -05008827 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008828 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8829 iocbq->iocb.un.ulpWord[3]);
8830 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008831 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008832 /* The entire sequence is transmitted for this IOCB */
8833 xmit_len = total_len;
8834 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008835 if (phba->link_flag & LS_LOOPBACK_MODE)
8836 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008837 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008838 /* word3 iocb=io_tag32 wqe=reserved */
8839 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008840 /* word4 relative_offset memcpy */
8841 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008842 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8843 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8844 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8845 LPFC_WQE_IOD_WRITE);
8846 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8847 LPFC_WQE_LENLOC_WORD12);
8848 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008849 wqe->xmit_sequence.xmit_len = xmit_len;
8850 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008851 break;
James Smart4f774512009-05-22 14:52:35 -04008852 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008853 /* word3 iocb=iotag32 wqe=seq_payload_len */
8854 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008855 /* word4 iocb=rsvd wqe=rsvd */
8856 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8857 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008858 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008859 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008860 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8861 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8862 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8863 LPFC_WQE_LENLOC_WORD3);
8864 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008865 break;
James Smart4f774512009-05-22 14:52:35 -04008866 case CMD_FCP_IWRITE64_CR:
8867 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008868 /* word3 iocb=iotag wqe=payload_offset_len */
8869 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008870 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8871 xmit_len + sizeof(struct fcp_rsp));
8872 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8873 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008874 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8875 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8876 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8877 iocbq->iocb.ulpFCP2Rcvy);
8878 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8879 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008880 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8881 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8882 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008883 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008884 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008885 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8886 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008887 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8888 if (iocbq->priority) {
8889 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8890 (iocbq->priority << 1));
8891 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008892 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8893 (phba->cfg_XLanePriority << 1));
8894 }
8895 }
James Smartb5c53952016-03-31 14:12:30 -07008896 /* Note, word 10 is already initialized to 0 */
8897
8898 if (phba->fcp_embed_io) {
8899 struct lpfc_scsi_buf *lpfc_cmd;
8900 struct sli4_sge *sgl;
8901 union lpfc_wqe128 *wqe128;
8902 struct fcp_cmnd *fcp_cmnd;
8903 uint32_t *ptr;
8904
8905 /* 128 byte wqe support here */
8906 wqe128 = (union lpfc_wqe128 *)wqe;
8907
8908 lpfc_cmd = iocbq->context1;
8909 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8910 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8911
8912 /* Word 0-2 - FCP_CMND */
8913 wqe128->generic.bde.tus.f.bdeFlags =
8914 BUFF_TYPE_BDE_IMMED;
8915 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8916 wqe128->generic.bde.addrHigh = 0;
8917 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8918
8919 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8920
8921 /* Word 22-29 FCP CMND Payload */
8922 ptr = &wqe128->words[22];
8923 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8924 }
James Smart7851fe22011-07-22 18:36:52 -04008925 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008926 case CMD_FCP_IREAD64_CR:
8927 /* word3 iocb=iotag wqe=payload_offset_len */
8928 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008929 bf_set(payload_offset_len, &wqe->fcp_iread,
8930 xmit_len + sizeof(struct fcp_rsp));
8931 bf_set(cmd_buff_len, &wqe->fcp_iread,
8932 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008933 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8934 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8935 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8936 iocbq->iocb.ulpFCP2Rcvy);
8937 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008938 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008939 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8940 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8941 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008942 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008943 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008944 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8945 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008946 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8947 if (iocbq->priority) {
8948 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8949 (iocbq->priority << 1));
8950 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008951 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8952 (phba->cfg_XLanePriority << 1));
8953 }
8954 }
James Smartb5c53952016-03-31 14:12:30 -07008955 /* Note, word 10 is already initialized to 0 */
8956
8957 if (phba->fcp_embed_io) {
8958 struct lpfc_scsi_buf *lpfc_cmd;
8959 struct sli4_sge *sgl;
8960 union lpfc_wqe128 *wqe128;
8961 struct fcp_cmnd *fcp_cmnd;
8962 uint32_t *ptr;
8963
8964 /* 128 byte wqe support here */
8965 wqe128 = (union lpfc_wqe128 *)wqe;
8966
8967 lpfc_cmd = iocbq->context1;
8968 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8969 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8970
8971 /* Word 0-2 - FCP_CMND */
8972 wqe128->generic.bde.tus.f.bdeFlags =
8973 BUFF_TYPE_BDE_IMMED;
8974 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8975 wqe128->generic.bde.addrHigh = 0;
8976 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8977
8978 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8979
8980 /* Word 22-29 FCP CMND Payload */
8981 ptr = &wqe128->words[22];
8982 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8983 }
James Smart7851fe22011-07-22 18:36:52 -04008984 break;
James Smartf1126682009-06-10 17:22:44 -04008985 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008986 /* word3 iocb=iotag wqe=payload_offset_len */
8987 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8988 bf_set(payload_offset_len, &wqe->fcp_icmd,
8989 xmit_len + sizeof(struct fcp_rsp));
8990 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8991 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008992 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008993 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008994 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008995 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8996 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8997 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8998 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8999 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009000 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9001 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009002 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9003 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009004 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9005 if (iocbq->priority) {
9006 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9007 (iocbq->priority << 1));
9008 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009009 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9010 (phba->cfg_XLanePriority << 1));
9011 }
9012 }
James Smartb5c53952016-03-31 14:12:30 -07009013 /* Note, word 10 is already initialized to 0 */
9014
9015 if (phba->fcp_embed_io) {
9016 struct lpfc_scsi_buf *lpfc_cmd;
9017 struct sli4_sge *sgl;
9018 union lpfc_wqe128 *wqe128;
9019 struct fcp_cmnd *fcp_cmnd;
9020 uint32_t *ptr;
9021
9022 /* 128 byte wqe support here */
9023 wqe128 = (union lpfc_wqe128 *)wqe;
9024
9025 lpfc_cmd = iocbq->context1;
9026 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9027 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9028
9029 /* Word 0-2 - FCP_CMND */
9030 wqe128->generic.bde.tus.f.bdeFlags =
9031 BUFF_TYPE_BDE_IMMED;
9032 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9033 wqe128->generic.bde.addrHigh = 0;
9034 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9035
9036 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9037
9038 /* Word 22-29 FCP CMND Payload */
9039 ptr = &wqe128->words[22];
9040 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9041 }
James Smart7851fe22011-07-22 18:36:52 -04009042 break;
James Smart4f774512009-05-22 14:52:35 -04009043 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009044 /* For this command calculate the xmit length of the
9045 * request bde.
9046 */
9047 xmit_len = 0;
9048 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9049 sizeof(struct ulp_bde64);
9050 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009051 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009052 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9053 break;
James Smart63e801c2010-11-20 23:14:19 -05009054 xmit_len += bde.tus.f.bdeSize;
9055 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009056 /* word3 iocb=IO_TAG wqe=request_payload_len */
9057 wqe->gen_req.request_payload_len = xmit_len;
9058 /* word4 iocb=parameter wqe=relative_offset memcpy */
9059 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009060 /* word6 context tag copied in memcpy */
9061 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9062 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9064 "2015 Invalid CT %x command 0x%x\n",
9065 ct, iocbq->iocb.ulpCommand);
9066 return IOCB_ERROR;
9067 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009068 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9069 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9070 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9071 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9072 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9073 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9074 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9075 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009076 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009077 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009078 break;
James Smart4f774512009-05-22 14:52:35 -04009079 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009080 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009081 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009082 /* word3 iocb=iotag32 wqe=response_payload_len */
9083 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009084 /* word4 */
9085 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009086 /* word5 iocb=rsvd wge=did */
9087 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009088 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9089
9090 if_type = bf_get(lpfc_sli_intf_if_type,
9091 &phba->sli4_hba.sli_intf);
9092 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9093 if (iocbq->vport->fc_flag & FC_PT2PT) {
9094 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9095 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9096 iocbq->vport->fc_myDID);
9097 if (iocbq->vport->fc_myDID == Fabric_DID) {
9098 bf_set(wqe_els_did,
9099 &wqe->xmit_els_rsp.wqe_dest, 0);
9100 }
9101 }
9102 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009103 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9104 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9105 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9106 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009107 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009108 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009109 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009110 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009111 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9112 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9113 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9114 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9115 LPFC_WQE_LENLOC_WORD3);
9116 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009117 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9118 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009119 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9120 iocbq->context2)->virt);
9121 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009122 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9123 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009124 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009125 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9126 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009127 phba->vpi_ids[phba->pport->vpi]);
9128 }
James Smart4f774512009-05-22 14:52:35 -04009129 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009130 break;
James Smart4f774512009-05-22 14:52:35 -04009131 case CMD_CLOSE_XRI_CN:
9132 case CMD_ABORT_XRI_CN:
9133 case CMD_ABORT_XRI_CX:
9134 /* words 0-2 memcpy should be 0 rserved */
9135 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009136 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9137 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9138 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9139 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9140 } else
9141 fip = 0;
9142
9143 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009144 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009145 * The link is down, or the command was ELS_FIP
9146 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009147 * on the wire.
9148 */
9149 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9150 else
9151 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9152 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009153 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9154 wqe->abort_cmd.rsrvd5 = 0;
9155 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009156 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9157 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009158 /*
9159 * The abort handler will send us CMD_ABORT_XRI_CN or
9160 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9161 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009162 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9163 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9164 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9165 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009166 cmnd = CMD_ABORT_XRI_CX;
9167 command_type = OTHER_COMMAND;
9168 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009169 break;
James Smart6669f9b2009-10-02 15:16:45 -04009170 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009171 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009172 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009173 * we re-construct this WQE here based on information in
9174 * iocbq from scratch.
9175 */
9176 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009177 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009178 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009179 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9180 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009181 LPFC_ABTS_UNSOL_INT) {
9182 /* ABTS sent by initiator to CT exchange, the
9183 * RX_ID field will be filled with the newly
9184 * allocated responder XRI.
9185 */
9186 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9187 iocbq->sli4_xritag);
9188 } else {
9189 /* ABTS sent by responder to CT exchange, the
9190 * RX_ID field will be filled with the responder
9191 * RX_ID from ABTS.
9192 */
9193 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009194 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009195 }
James Smart6669f9b2009-10-02 15:16:45 -04009196 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9197 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009198
9199 /* Use CT=VPI */
9200 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9201 ndlp->nlp_DID);
9202 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9203 iocbq->iocb.ulpContext);
9204 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009205 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009206 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009207 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9208 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9209 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009210 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9211 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009212 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9213 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9214 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9215 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9216 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9217 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9218 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9219 }
9220
James Smart7851fe22011-07-22 18:36:52 -04009221 break;
James Smartae9e28f2017-05-15 15:20:51 -07009222 case CMD_SEND_FRAME:
9223 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9224 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9225 return 0;
James Smart4f774512009-05-22 14:52:35 -04009226 case CMD_XRI_ABORTED_CX:
9227 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009228 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9229 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9230 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9231 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9232 default:
9233 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9234 "2014 Invalid command 0x%x\n",
9235 iocbq->iocb.ulpCommand);
9236 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009237 break;
James Smart4f774512009-05-22 14:52:35 -04009238 }
James Smart6d368e52011-05-24 11:44:12 -04009239
James Smart8012cc32012-10-31 14:44:49 -04009240 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9241 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9242 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9243 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9244 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9245 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9246 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9247 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009248 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9249 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9250 wqe->generic.wqe_com.abort_tag = abort_tag;
9251 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9252 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9253 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9254 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009255 return 0;
9256}
9257
9258/**
9259 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9260 * @phba: Pointer to HBA context object.
9261 * @ring_number: SLI ring number to issue iocb on.
9262 * @piocb: Pointer to command iocb.
9263 * @flag: Flag indicating if this command can be put into txq.
9264 *
9265 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9266 * an iocb command to an HBA with SLI-4 interface spec.
9267 *
9268 * This function is called with hbalock held. The function will return success
9269 * after it successfully submit the iocb to firmware or after adding to the
9270 * txq.
9271 **/
9272static int
9273__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9274 struct lpfc_iocbq *piocb, uint32_t flag)
9275{
9276 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009277 union lpfc_wqe *wqe;
9278 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009279 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009280 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009281
James Smart895427b2017-02-12 13:52:30 -08009282 /* Get the WQ */
9283 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9284 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9285 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9286 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9287 else
9288 wq = phba->sli4_hba.oas_wq;
9289 } else {
9290 wq = phba->sli4_hba.els_wq;
9291 }
9292
9293 /* Get corresponding ring */
9294 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009295
James Smartb5c53952016-03-31 14:12:30 -07009296 /*
9297 * The WQE can be either 64 or 128 bytes,
9298 * so allocate space on the stack assuming the largest.
9299 */
9300 wqe = (union lpfc_wqe *)&wqe128;
9301
James Smart895427b2017-02-12 13:52:30 -08009302 lockdep_assert_held(&phba->hbalock);
9303
James Smart4f774512009-05-22 14:52:35 -04009304 if (piocb->sli4_xritag == NO_XRI) {
9305 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009306 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009307 sglq = NULL;
9308 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009309 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009310 if (!(flag & SLI_IOCB_RET_IOCB)) {
9311 __lpfc_sli_ringtx_put(phba,
9312 pring, piocb);
9313 return IOCB_SUCCESS;
9314 } else {
9315 return IOCB_BUSY;
9316 }
9317 } else {
James Smart895427b2017-02-12 13:52:30 -08009318 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009319 if (!sglq) {
9320 if (!(flag & SLI_IOCB_RET_IOCB)) {
9321 __lpfc_sli_ringtx_put(phba,
9322 pring,
9323 piocb);
9324 return IOCB_SUCCESS;
9325 } else
9326 return IOCB_BUSY;
9327 }
9328 }
James Smart4f774512009-05-22 14:52:35 -04009329 }
James Smart2ea259e2017-02-12 13:52:27 -08009330 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009331 /* These IO's already have an XRI and a mapped sgl. */
9332 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009333 else {
James Smart6d368e52011-05-24 11:44:12 -04009334 /*
9335 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009336 * sglq is on the active list
9337 */
James Smartedccdc12013-01-03 15:43:45 -05009338 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009339 if (!sglq)
9340 return IOCB_ERROR;
9341 }
9342
9343 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009344 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009345 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009346 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009347 return IOCB_ERROR;
9348 }
9349
James Smartb5c53952016-03-31 14:12:30 -07009350 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009351 return IOCB_ERROR;
9352
James Smart895427b2017-02-12 13:52:30 -08009353 if (lpfc_sli4_wq_put(wq, wqe))
9354 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009355 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9356
9357 return 0;
9358}
9359
9360/**
James Smart3772a992009-05-22 14:50:54 -04009361 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9362 *
9363 * This routine wraps the actual lockless version for issusing IOCB function
9364 * pointer from the lpfc_hba struct.
9365 *
9366 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009367 * IOCB_ERROR - Error
9368 * IOCB_SUCCESS - Success
9369 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009370 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009371int
James Smart3772a992009-05-22 14:50:54 -04009372__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9373 struct lpfc_iocbq *piocb, uint32_t flag)
9374{
9375 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9376}
9377
9378/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009379 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009380 * @phba: The hba struct for which this call is being executed.
9381 * @dev_grp: The HBA PCI-Device group number.
9382 *
9383 * This routine sets up the SLI interface API function jump table in @phba
9384 * struct.
9385 * Returns: 0 - success, -ENODEV - failure.
9386 **/
9387int
9388lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9389{
9390
9391 switch (dev_grp) {
9392 case LPFC_PCI_DEV_LP:
9393 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9394 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9395 break;
James Smart4f774512009-05-22 14:52:35 -04009396 case LPFC_PCI_DEV_OC:
9397 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9398 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9399 break;
James Smart3772a992009-05-22 14:50:54 -04009400 default:
9401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9402 "1419 Invalid HBA PCI-device group: 0x%x\n",
9403 dev_grp);
9404 return -ENODEV;
9405 break;
9406 }
9407 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9408 return 0;
9409}
James Smart92d7f7b2007-06-17 19:56:38 -05009410
James Smarta1efe162015-05-21 13:55:20 -04009411/**
James Smart895427b2017-02-12 13:52:30 -08009412 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009413 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009414 * @piocb: Pointer to command iocb.
9415 *
James Smart895427b2017-02-12 13:52:30 -08009416 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9417 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009418 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009419 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009420 */
James Smart895427b2017-02-12 13:52:30 -08009421struct lpfc_sli_ring *
9422lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009423{
James Smart895427b2017-02-12 13:52:30 -08009424 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009425 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009426 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009427 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009428 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009429 /*
James Smart895427b2017-02-12 13:52:30 -08009430 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009431 * be setup based on what work queue we used.
9432 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009433 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009434 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009435 lpfc_sli4_scmd_to_wqidx_distr(phba,
9436 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009437 piocb->hba_wqidx = piocb->hba_wqidx %
9438 phba->cfg_fcp_io_channel;
9439 }
James Smart895427b2017-02-12 13:52:30 -08009440 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009441 } else {
9442 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009443 return NULL;
9444 piocb->hba_wqidx = 0;
9445 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009446 }
James Smart895427b2017-02-12 13:52:30 -08009447 } else {
9448 if (unlikely(!phba->sli4_hba.els_wq))
9449 return NULL;
9450 piocb->hba_wqidx = 0;
9451 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009452 }
James Smart9bd2bff52014-09-03 12:57:30 -04009453}
9454
James Smarte59058c2008-08-24 21:49:00 -04009455/**
James Smart3621a712009-04-06 18:47:14 -04009456 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009457 * @phba: Pointer to HBA context object.
9458 * @pring: Pointer to driver SLI ring object.
9459 * @piocb: Pointer to command iocb.
9460 * @flag: Flag indicating if this command can be put into txq.
9461 *
9462 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9463 * function. This function gets the hbalock and calls
9464 * __lpfc_sli_issue_iocb function and will return the error returned
9465 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9466 * functions which do not hold hbalock.
9467 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009468int
James Smart3772a992009-05-22 14:50:54 -04009469lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009470 struct lpfc_iocbq *piocb, uint32_t flag)
9471{
James Smart895427b2017-02-12 13:52:30 -08009472 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009473 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009474 struct lpfc_queue *fpeq;
9475 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009476 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009477 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009478
James Smart7e56aa22012-08-03 12:35:34 -04009479 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009480 pring = lpfc_sli4_calc_ring(phba, piocb);
9481 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009482 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009483
James Smart9bd2bff52014-09-03 12:57:30 -04009484 spin_lock_irqsave(&pring->ring_lock, iflags);
9485 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9486 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009487
James Smart9bd2bff52014-09-03 12:57:30 -04009488 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009489 idx = piocb->hba_wqidx;
9490 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009491
James Smart895427b2017-02-12 13:52:30 -08009492 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009493
James Smart9bd2bff52014-09-03 12:57:30 -04009494 /* Get associated EQ with this index */
9495 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009496
James Smart9bd2bff52014-09-03 12:57:30 -04009497 /* Turn off interrupts from this EQ */
9498 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009499
James Smart9bd2bff52014-09-03 12:57:30 -04009500 /*
9501 * Process all the events on FCP EQ
9502 */
9503 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9504 lpfc_sli4_hba_handle_eqe(phba,
9505 eqe, idx);
9506 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009507 }
James Smartba20c852012-08-03 12:36:52 -04009508
James Smart9bd2bff52014-09-03 12:57:30 -04009509 /* Always clear and re-arm the EQ */
9510 lpfc_sli4_eq_release(fpeq,
9511 LPFC_QUEUE_REARM);
9512 }
James Smart895427b2017-02-12 13:52:30 -08009513 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009514 }
James Smart7e56aa22012-08-03 12:35:34 -04009515 } else {
9516 /* For now, SLI2/3 will still use hbalock */
9517 spin_lock_irqsave(&phba->hbalock, iflags);
9518 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9519 spin_unlock_irqrestore(&phba->hbalock, iflags);
9520 }
James Smart92d7f7b2007-06-17 19:56:38 -05009521 return rc;
9522}
9523
James Smarte59058c2008-08-24 21:49:00 -04009524/**
James Smart3621a712009-04-06 18:47:14 -04009525 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009526 * @phba: Pointer to HBA context object.
9527 *
9528 * This function is called while driver attaches with the
9529 * HBA to setup the extra ring. The extra ring is used
9530 * only when driver needs to support target mode functionality
9531 * or IP over FC functionalities.
9532 *
James Smart895427b2017-02-12 13:52:30 -08009533 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009534 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009535static int
9536lpfc_extra_ring_setup( struct lpfc_hba *phba)
9537{
9538 struct lpfc_sli *psli;
9539 struct lpfc_sli_ring *pring;
9540
9541 psli = &phba->sli;
9542
9543 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009544
9545 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009546 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009547 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9548 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9549 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9550 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009551
James Smarta4bc3372006-12-02 13:34:16 -05009552 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009553 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009554
James Smart7e56aa22012-08-03 12:35:34 -04009555 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9556 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9557 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9558 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009559
9560 /* Setup default profile for this ring */
9561 pring->iotag_max = 4096;
9562 pring->num_mask = 1;
9563 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009564 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9565 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009566 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9567 return 0;
9568}
9569
James Smartcb69f7d2011-12-13 13:21:57 -05009570/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9571 * @phba: Pointer to HBA context object.
9572 * @iocbq: Pointer to iocb object.
9573 *
9574 * The async_event handler calls this routine when it receives
9575 * an ASYNC_STATUS_CN event from the port. The port generates
9576 * this event when an Abort Sequence request to an rport fails
9577 * twice in succession. The abort could be originated by the
9578 * driver or by the port. The ABTS could have been for an ELS
9579 * or FCP IO. The port only generates this event when an ABTS
9580 * fails to complete after one retry.
9581 */
9582static void
9583lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9584 struct lpfc_iocbq *iocbq)
9585{
9586 struct lpfc_nodelist *ndlp = NULL;
9587 uint16_t rpi = 0, vpi = 0;
9588 struct lpfc_vport *vport = NULL;
9589
9590 /* The rpi in the ulpContext is vport-sensitive. */
9591 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9592 rpi = iocbq->iocb.ulpContext;
9593
9594 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9595 "3092 Port generated ABTS async event "
9596 "on vpi %d rpi %d status 0x%x\n",
9597 vpi, rpi, iocbq->iocb.ulpStatus);
9598
9599 vport = lpfc_find_vport_by_vpid(phba, vpi);
9600 if (!vport)
9601 goto err_exit;
9602 ndlp = lpfc_findnode_rpi(vport, rpi);
9603 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9604 goto err_exit;
9605
9606 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9607 lpfc_sli_abts_recover_port(vport, ndlp);
9608 return;
9609
9610 err_exit:
9611 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9612 "3095 Event Context not found, no "
9613 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9614 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9615 vpi, rpi);
9616}
9617
9618/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9619 * @phba: pointer to HBA context object.
9620 * @ndlp: nodelist pointer for the impacted rport.
9621 * @axri: pointer to the wcqe containing the failed exchange.
9622 *
9623 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9624 * port. The port generates this event when an abort exchange request to an
9625 * rport fails twice in succession with no reply. The abort could be originated
9626 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9627 */
9628void
9629lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9630 struct lpfc_nodelist *ndlp,
9631 struct sli4_wcqe_xri_aborted *axri)
9632{
9633 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009634 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009635
James Smart6b5151f2012-01-18 16:24:06 -05009636 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009637 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9638 "3115 Node Context not found, driver "
9639 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009640 return;
9641 }
9642
James Smartcb69f7d2011-12-13 13:21:57 -05009643 vport = ndlp->vport;
9644 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9645 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009646 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009647 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009648 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009649 bf_get(lpfc_wcqe_xa_status, axri),
9650 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009651
James Smart5c1db2a2012-03-01 22:34:36 -05009652 /*
9653 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9654 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9655 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9656 */
James Smarte3d2b802012-08-14 14:25:43 -04009657 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009658 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9659 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009660 lpfc_sli_abts_recover_port(vport, ndlp);
9661}
9662
James Smarte59058c2008-08-24 21:49:00 -04009663/**
James Smart3621a712009-04-06 18:47:14 -04009664 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009665 * @phba: Pointer to HBA context object.
9666 * @pring: Pointer to driver SLI ring object.
9667 * @iocbq: Pointer to iocb object.
9668 *
9669 * This function is called by the slow ring event handler
9670 * function when there is an ASYNC event iocb in the ring.
9671 * This function is called with no lock held.
9672 * Currently this function handles only temperature related
9673 * ASYNC events. The function decodes the temperature sensor
9674 * event message and posts events for the management applications.
9675 **/
James Smart98c9ea52007-10-27 13:37:33 -04009676static void
James Smart57127f12007-10-27 13:37:05 -04009677lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9678 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9679{
9680 IOCB_t *icmd;
9681 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009682 struct temp_event temp_event_data;
9683 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009684 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009685
9686 icmd = &iocbq->iocb;
9687 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009688
James Smartcb69f7d2011-12-13 13:21:57 -05009689 switch (evt_code) {
9690 case ASYNC_TEMP_WARN:
9691 case ASYNC_TEMP_SAFE:
9692 temp_event_data.data = (uint32_t) icmd->ulpContext;
9693 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9694 if (evt_code == ASYNC_TEMP_WARN) {
9695 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9696 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9697 "0347 Adapter is very hot, please take "
9698 "corrective action. temperature : %d Celsius\n",
9699 (uint32_t) icmd->ulpContext);
9700 } else {
9701 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9702 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9703 "0340 Adapter temperature is OK now. "
9704 "temperature : %d Celsius\n",
9705 (uint32_t) icmd->ulpContext);
9706 }
9707
9708 /* Send temperature change event to applications */
9709 shost = lpfc_shost_from_vport(phba->pport);
9710 fc_host_post_vendor_event(shost, fc_get_event_number(),
9711 sizeof(temp_event_data), (char *) &temp_event_data,
9712 LPFC_NL_VENDOR_ID);
9713 break;
9714 case ASYNC_STATUS_CN:
9715 lpfc_sli_abts_err_handler(phba, iocbq);
9716 break;
9717 default:
James Smarta257bf92009-04-06 18:48:10 -04009718 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009720 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009721 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009722 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9723 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9724 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9725 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009726 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009727 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9728 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9729 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9730 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9731
James Smartcb69f7d2011-12-13 13:21:57 -05009732 break;
James Smart57127f12007-10-27 13:37:05 -04009733 }
James Smart57127f12007-10-27 13:37:05 -04009734}
9735
9736
James Smarte59058c2008-08-24 21:49:00 -04009737/**
James Smart895427b2017-02-12 13:52:30 -08009738 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009739 * @phba: Pointer to HBA context object.
9740 *
9741 * lpfc_sli_setup sets up rings of the SLI interface with
9742 * number of iocbs per ring and iotags. This function is
9743 * called while driver attach to the HBA and before the
9744 * interrupts are enabled. So there is no need for locking.
9745 *
9746 * This function always returns 0.
9747 **/
dea31012005-04-17 16:05:31 -05009748int
James Smart895427b2017-02-12 13:52:30 -08009749lpfc_sli4_setup(struct lpfc_hba *phba)
9750{
9751 struct lpfc_sli_ring *pring;
9752
9753 pring = phba->sli4_hba.els_wq->pring;
9754 pring->num_mask = LPFC_MAX_RING_MASK;
9755 pring->prt[0].profile = 0; /* Mask 0 */
9756 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9757 pring->prt[0].type = FC_TYPE_ELS;
9758 pring->prt[0].lpfc_sli_rcv_unsol_event =
9759 lpfc_els_unsol_event;
9760 pring->prt[1].profile = 0; /* Mask 1 */
9761 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9762 pring->prt[1].type = FC_TYPE_ELS;
9763 pring->prt[1].lpfc_sli_rcv_unsol_event =
9764 lpfc_els_unsol_event;
9765 pring->prt[2].profile = 0; /* Mask 2 */
9766 /* NameServer Inquiry */
9767 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9768 /* NameServer */
9769 pring->prt[2].type = FC_TYPE_CT;
9770 pring->prt[2].lpfc_sli_rcv_unsol_event =
9771 lpfc_ct_unsol_event;
9772 pring->prt[3].profile = 0; /* Mask 3 */
9773 /* NameServer response */
9774 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9775 /* NameServer */
9776 pring->prt[3].type = FC_TYPE_CT;
9777 pring->prt[3].lpfc_sli_rcv_unsol_event =
9778 lpfc_ct_unsol_event;
9779 return 0;
9780}
9781
9782/**
9783 * lpfc_sli_setup - SLI ring setup function
9784 * @phba: Pointer to HBA context object.
9785 *
9786 * lpfc_sli_setup sets up rings of the SLI interface with
9787 * number of iocbs per ring and iotags. This function is
9788 * called while driver attach to the HBA and before the
9789 * interrupts are enabled. So there is no need for locking.
9790 *
9791 * This function always returns 0. SLI3 only.
9792 **/
9793int
dea31012005-04-17 16:05:31 -05009794lpfc_sli_setup(struct lpfc_hba *phba)
9795{
James Smarted957682007-06-17 19:56:37 -05009796 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009797 struct lpfc_sli *psli = &phba->sli;
9798 struct lpfc_sli_ring *pring;
9799
James Smart2a76a282012-08-03 12:35:54 -04009800 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009801 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009802
James Bottomley604a3e32005-10-29 10:28:33 -05009803 psli->iocbq_lookup = NULL;
9804 psli->iocbq_lookup_len = 0;
9805 psli->last_iotag = 0;
9806
dea31012005-04-17 16:05:31 -05009807 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009808 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009809 switch (i) {
9810 case LPFC_FCP_RING: /* ring 0 - FCP */
9811 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009812 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9813 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9814 pring->sli.sli3.numCiocb +=
9815 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9816 pring->sli.sli3.numRiocb +=
9817 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9818 pring->sli.sli3.numCiocb +=
9819 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9820 pring->sli.sli3.numRiocb +=
9821 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9822 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009823 SLI3_IOCB_CMD_SIZE :
9824 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009825 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009826 SLI3_IOCB_RSP_SIZE :
9827 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009828 pring->iotag_ctr = 0;
9829 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009830 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009831 pring->fast_iotag = pring->iotag_max;
9832 pring->num_mask = 0;
9833 break;
James Smarta4bc3372006-12-02 13:34:16 -05009834 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009835 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009836 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9837 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9838 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009839 SLI3_IOCB_CMD_SIZE :
9840 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009841 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009842 SLI3_IOCB_RSP_SIZE :
9843 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009844 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009845 pring->num_mask = 0;
9846 break;
9847 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9848 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009849 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9850 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9851 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009852 SLI3_IOCB_CMD_SIZE :
9853 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009854 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009855 SLI3_IOCB_RSP_SIZE :
9856 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009857 pring->fast_iotag = 0;
9858 pring->iotag_ctr = 0;
9859 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009860 pring->lpfc_sli_rcv_async_status =
9861 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009862 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009863 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009864 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9865 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009866 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009867 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009868 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009869 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9870 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009871 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009872 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009873 pring->prt[2].profile = 0; /* Mask 2 */
9874 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009875 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009876 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009877 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009878 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009879 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009880 pring->prt[3].profile = 0; /* Mask 3 */
9881 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009882 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009883 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009884 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009885 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009886 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009887 break;
9888 }
James Smart7e56aa22012-08-03 12:35:34 -04009889 totiocbsize += (pring->sli.sli3.numCiocb *
9890 pring->sli.sli3.sizeCiocb) +
9891 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009892 }
James Smarted957682007-06-17 19:56:37 -05009893 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009894 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009895 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9896 "SLI2 SLIM Data: x%x x%lx\n",
9897 phba->brd_no, totiocbsize,
9898 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009899 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009900 if (phba->cfg_multi_ring_support == 2)
9901 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009902
9903 return 0;
9904}
9905
James Smarte59058c2008-08-24 21:49:00 -04009906/**
James Smart895427b2017-02-12 13:52:30 -08009907 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009908 * @phba: Pointer to HBA context object.
9909 *
James Smart895427b2017-02-12 13:52:30 -08009910 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009911 * ring. This function also initializes ring indices of each ring.
9912 * This function is called during the initialization of the SLI
9913 * interface of an HBA.
9914 * This function is called with no lock held and always returns
9915 * 1.
9916 **/
James Smart895427b2017-02-12 13:52:30 -08009917void
9918lpfc_sli4_queue_init(struct lpfc_hba *phba)
9919{
9920 struct lpfc_sli *psli;
9921 struct lpfc_sli_ring *pring;
9922 int i;
9923
9924 psli = &phba->sli;
9925 spin_lock_irq(&phba->hbalock);
9926 INIT_LIST_HEAD(&psli->mboxq);
9927 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9928 /* Initialize list headers for txq and txcmplq as double linked lists */
9929 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9930 pring = phba->sli4_hba.fcp_wq[i]->pring;
9931 pring->flag = 0;
9932 pring->ringno = LPFC_FCP_RING;
9933 INIT_LIST_HEAD(&pring->txq);
9934 INIT_LIST_HEAD(&pring->txcmplq);
9935 INIT_LIST_HEAD(&pring->iocb_continueq);
9936 spin_lock_init(&pring->ring_lock);
9937 }
9938 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9939 pring = phba->sli4_hba.nvme_wq[i]->pring;
9940 pring->flag = 0;
9941 pring->ringno = LPFC_FCP_RING;
9942 INIT_LIST_HEAD(&pring->txq);
9943 INIT_LIST_HEAD(&pring->txcmplq);
9944 INIT_LIST_HEAD(&pring->iocb_continueq);
9945 spin_lock_init(&pring->ring_lock);
9946 }
9947 pring = phba->sli4_hba.els_wq->pring;
9948 pring->flag = 0;
9949 pring->ringno = LPFC_ELS_RING;
9950 INIT_LIST_HEAD(&pring->txq);
9951 INIT_LIST_HEAD(&pring->txcmplq);
9952 INIT_LIST_HEAD(&pring->iocb_continueq);
9953 spin_lock_init(&pring->ring_lock);
9954
9955 if (phba->cfg_nvme_io_channel) {
9956 pring = phba->sli4_hba.nvmels_wq->pring;
9957 pring->flag = 0;
9958 pring->ringno = LPFC_ELS_RING;
9959 INIT_LIST_HEAD(&pring->txq);
9960 INIT_LIST_HEAD(&pring->txcmplq);
9961 INIT_LIST_HEAD(&pring->iocb_continueq);
9962 spin_lock_init(&pring->ring_lock);
9963 }
9964
9965 if (phba->cfg_fof) {
9966 pring = phba->sli4_hba.oas_wq->pring;
9967 pring->flag = 0;
9968 pring->ringno = LPFC_FCP_RING;
9969 INIT_LIST_HEAD(&pring->txq);
9970 INIT_LIST_HEAD(&pring->txcmplq);
9971 INIT_LIST_HEAD(&pring->iocb_continueq);
9972 spin_lock_init(&pring->ring_lock);
9973 }
9974
9975 spin_unlock_irq(&phba->hbalock);
9976}
9977
9978/**
9979 * lpfc_sli_queue_init - Queue initialization function
9980 * @phba: Pointer to HBA context object.
9981 *
9982 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9983 * ring. This function also initializes ring indices of each ring.
9984 * This function is called during the initialization of the SLI
9985 * interface of an HBA.
9986 * This function is called with no lock held and always returns
9987 * 1.
9988 **/
9989void
9990lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009991{
9992 struct lpfc_sli *psli;
9993 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009994 int i;
dea31012005-04-17 16:05:31 -05009995
9996 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009997 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009998 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009999 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010000 /* Initialize list headers for txq and txcmplq as double linked lists */
10001 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010002 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010003 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010004 pring->sli.sli3.next_cmdidx = 0;
10005 pring->sli.sli3.local_getidx = 0;
10006 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010007 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010008 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010009 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010010 pring->flag = 0;
10011 INIT_LIST_HEAD(&pring->txq);
10012 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010013 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010014 }
James Smart2e0fef82007-06-17 19:56:36 -050010015 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010016}
10017
James Smarte59058c2008-08-24 21:49:00 -040010018/**
James Smart04c68492009-05-22 14:52:52 -040010019 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10020 * @phba: Pointer to HBA context object.
10021 *
10022 * This routine flushes the mailbox command subsystem. It will unconditionally
10023 * flush all the mailbox commands in the three possible stages in the mailbox
10024 * command sub-system: pending mailbox command queue; the outstanding mailbox
10025 * command; and completed mailbox command queue. It is caller's responsibility
10026 * to make sure that the driver is in the proper state to flush the mailbox
10027 * command sub-system. Namely, the posting of mailbox commands into the
10028 * pending mailbox command queue from the various clients must be stopped;
10029 * either the HBA is in a state that it will never works on the outstanding
10030 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10031 * mailbox command has been completed.
10032 **/
10033static void
10034lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10035{
10036 LIST_HEAD(completions);
10037 struct lpfc_sli *psli = &phba->sli;
10038 LPFC_MBOXQ_t *pmb;
10039 unsigned long iflag;
10040
10041 /* Flush all the mailbox commands in the mbox system */
10042 spin_lock_irqsave(&phba->hbalock, iflag);
10043 /* The pending mailbox command queue */
10044 list_splice_init(&phba->sli.mboxq, &completions);
10045 /* The outstanding active mailbox command */
10046 if (psli->mbox_active) {
10047 list_add_tail(&psli->mbox_active->list, &completions);
10048 psli->mbox_active = NULL;
10049 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10050 }
10051 /* The completed mailbox command queue */
10052 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10053 spin_unlock_irqrestore(&phba->hbalock, iflag);
10054
10055 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10056 while (!list_empty(&completions)) {
10057 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10058 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10059 if (pmb->mbox_cmpl)
10060 pmb->mbox_cmpl(phba, pmb);
10061 }
10062}
10063
10064/**
James Smart3621a712009-04-06 18:47:14 -040010065 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010066 * @vport: Pointer to virtual port object.
10067 *
10068 * lpfc_sli_host_down is called to clean up the resources
10069 * associated with a vport before destroying virtual
10070 * port data structures.
10071 * This function does following operations:
10072 * - Free discovery resources associated with this virtual
10073 * port.
10074 * - Free iocbs associated with this virtual port in
10075 * the txq.
10076 * - Send abort for all iocb commands associated with this
10077 * vport in txcmplq.
10078 *
10079 * This function is called with no lock held and always returns 1.
10080 **/
dea31012005-04-17 16:05:31 -050010081int
James Smart92d7f7b2007-06-17 19:56:38 -050010082lpfc_sli_host_down(struct lpfc_vport *vport)
10083{
James Smart858c9f62007-06-17 19:56:39 -050010084 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010085 struct lpfc_hba *phba = vport->phba;
10086 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010087 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010088 struct lpfc_sli_ring *pring;
10089 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010090 int i;
10091 unsigned long flags = 0;
10092 uint16_t prev_pring_flag;
10093
10094 lpfc_cleanup_discovery_resources(vport);
10095
10096 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010097
James Smart895427b2017-02-12 13:52:30 -080010098 /*
10099 * Error everything on the txq since these iocbs
10100 * have not been given to the FW yet.
10101 * Also issue ABTS for everything on the txcmplq
10102 */
10103 if (phba->sli_rev != LPFC_SLI_REV4) {
10104 for (i = 0; i < psli->num_rings; i++) {
10105 pring = &psli->sli3_ring[i];
10106 prev_pring_flag = pring->flag;
10107 /* Only slow rings */
10108 if (pring->ringno == LPFC_ELS_RING) {
10109 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10110 /* Set the lpfc data pending flag */
10111 set_bit(LPFC_DATA_READY, &phba->data_flags);
10112 }
10113 list_for_each_entry_safe(iocb, next_iocb,
10114 &pring->txq, list) {
10115 if (iocb->vport != vport)
10116 continue;
10117 list_move_tail(&iocb->list, &completions);
10118 }
10119 list_for_each_entry_safe(iocb, next_iocb,
10120 &pring->txcmplq, list) {
10121 if (iocb->vport != vport)
10122 continue;
10123 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10124 }
10125 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010126 }
James Smart895427b2017-02-12 13:52:30 -080010127 } else {
10128 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10129 pring = qp->pring;
10130 if (!pring)
10131 continue;
10132 if (pring == phba->sli4_hba.els_wq->pring) {
10133 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10134 /* Set the lpfc data pending flag */
10135 set_bit(LPFC_DATA_READY, &phba->data_flags);
10136 }
10137 prev_pring_flag = pring->flag;
10138 spin_lock_irq(&pring->ring_lock);
10139 list_for_each_entry_safe(iocb, next_iocb,
10140 &pring->txq, list) {
10141 if (iocb->vport != vport)
10142 continue;
10143 list_move_tail(&iocb->list, &completions);
10144 }
10145 spin_unlock_irq(&pring->ring_lock);
10146 list_for_each_entry_safe(iocb, next_iocb,
10147 &pring->txcmplq, list) {
10148 if (iocb->vport != vport)
10149 continue;
10150 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10151 }
10152 pring->flag = prev_pring_flag;
10153 }
James Smart92d7f7b2007-06-17 19:56:38 -050010154 }
James Smart92d7f7b2007-06-17 19:56:38 -050010155 spin_unlock_irqrestore(&phba->hbalock, flags);
10156
James Smarta257bf92009-04-06 18:48:10 -040010157 /* Cancel all the IOCBs from the completions list */
10158 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10159 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010160 return 1;
10161}
10162
James Smarte59058c2008-08-24 21:49:00 -040010163/**
James Smart3621a712009-04-06 18:47:14 -040010164 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010165 * @phba: Pointer to HBA context object.
10166 *
10167 * This function cleans up all iocb, buffers, mailbox commands
10168 * while shutting down the HBA. This function is called with no
10169 * lock held and always returns 1.
10170 * This function does the following to cleanup driver resources:
10171 * - Free discovery resources for each virtual port
10172 * - Cleanup any pending fabric iocbs
10173 * - Iterate through the iocb txq and free each entry
10174 * in the list.
10175 * - Free up any buffer posted to the HBA
10176 * - Free mailbox commands in the mailbox queue.
10177 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010178int
James Smart2e0fef82007-06-17 19:56:36 -050010179lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010180{
James Smart2534ba72007-04-25 09:52:20 -040010181 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010182 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010183 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010184 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010185 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010186 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010187 int i;
10188
10189 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010190 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010191
dea31012005-04-17 16:05:31 -050010192 lpfc_hba_down_prep(phba);
10193
James Smart92d7f7b2007-06-17 19:56:38 -050010194 lpfc_fabric_abort_hba(phba);
10195
James Smart2e0fef82007-06-17 19:56:36 -050010196 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010197
James Smart895427b2017-02-12 13:52:30 -080010198 /*
10199 * Error everything on the txq since these iocbs
10200 * have not been given to the FW yet.
10201 */
10202 if (phba->sli_rev != LPFC_SLI_REV4) {
10203 for (i = 0; i < psli->num_rings; i++) {
10204 pring = &psli->sli3_ring[i];
10205 /* Only slow rings */
10206 if (pring->ringno == LPFC_ELS_RING) {
10207 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10208 /* Set the lpfc data pending flag */
10209 set_bit(LPFC_DATA_READY, &phba->data_flags);
10210 }
10211 list_splice_init(&pring->txq, &completions);
10212 }
10213 } else {
10214 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10215 pring = qp->pring;
10216 if (!pring)
10217 continue;
10218 spin_lock_irq(&pring->ring_lock);
10219 list_splice_init(&pring->txq, &completions);
10220 spin_unlock_irq(&pring->ring_lock);
10221 if (pring == phba->sli4_hba.els_wq->pring) {
10222 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10223 /* Set the lpfc data pending flag */
10224 set_bit(LPFC_DATA_READY, &phba->data_flags);
10225 }
10226 }
dea31012005-04-17 16:05:31 -050010227 }
James Smart2e0fef82007-06-17 19:56:36 -050010228 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010229
James Smarta257bf92009-04-06 18:48:10 -040010230 /* Cancel all the IOCBs from the completions list */
10231 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10232 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010233
James Smart0ff10d42008-01-11 01:52:36 -050010234 spin_lock_irqsave(&phba->hbalock, flags);
10235 list_splice_init(&phba->elsbuf, &completions);
10236 phba->elsbuf_cnt = 0;
10237 phba->elsbuf_prev_cnt = 0;
10238 spin_unlock_irqrestore(&phba->hbalock, flags);
10239
10240 while (!list_empty(&completions)) {
10241 list_remove_head(&completions, buf_ptr,
10242 struct lpfc_dmabuf, list);
10243 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10244 kfree(buf_ptr);
10245 }
10246
dea31012005-04-17 16:05:31 -050010247 /* Return any active mbox cmds */
10248 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010249
James Smartda0436e2009-05-22 14:51:39 -040010250 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010251 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010252 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010253
James Smartda0436e2009-05-22 14:51:39 -040010254 return 1;
10255}
James Smart92d7f7b2007-06-17 19:56:38 -050010256
James Smartda0436e2009-05-22 14:51:39 -040010257/**
James Smart3621a712009-04-06 18:47:14 -040010258 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010259 * @srcp: Source memory pointer.
10260 * @destp: Destination memory pointer.
10261 * @cnt: Number of words required to be copied.
10262 *
10263 * This function is used for copying data between driver memory
10264 * and the SLI memory. This function also changes the endianness
10265 * of each word if native endianness is different from SLI
10266 * endianness. This function can be called with or without
10267 * lock.
10268 **/
dea31012005-04-17 16:05:31 -050010269void
10270lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10271{
10272 uint32_t *src = srcp;
10273 uint32_t *dest = destp;
10274 uint32_t ldata;
10275 int i;
10276
10277 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10278 ldata = *src;
10279 ldata = le32_to_cpu(ldata);
10280 *dest = ldata;
10281 src++;
10282 dest++;
10283 }
10284}
10285
James Smarte59058c2008-08-24 21:49:00 -040010286
10287/**
James Smarta0c87cb2009-07-19 10:01:10 -040010288 * lpfc_sli_bemem_bcopy - SLI memory copy function
10289 * @srcp: Source memory pointer.
10290 * @destp: Destination memory pointer.
10291 * @cnt: Number of words required to be copied.
10292 *
10293 * This function is used for copying data between a data structure
10294 * with big endian representation to local endianness.
10295 * This function can be called with or without lock.
10296 **/
10297void
10298lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10299{
10300 uint32_t *src = srcp;
10301 uint32_t *dest = destp;
10302 uint32_t ldata;
10303 int i;
10304
10305 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10306 ldata = *src;
10307 ldata = be32_to_cpu(ldata);
10308 *dest = ldata;
10309 src++;
10310 dest++;
10311 }
10312}
10313
10314/**
James Smart3621a712009-04-06 18:47:14 -040010315 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010316 * @phba: Pointer to HBA context object.
10317 * @pring: Pointer to driver SLI ring object.
10318 * @mp: Pointer to driver buffer object.
10319 *
10320 * This function is called with no lock held.
10321 * It always return zero after adding the buffer to the postbufq
10322 * buffer list.
10323 **/
dea31012005-04-17 16:05:31 -050010324int
James Smart2e0fef82007-06-17 19:56:36 -050010325lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10326 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010327{
10328 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10329 later */
James Smart2e0fef82007-06-17 19:56:36 -050010330 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010331 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010332 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010333 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010334 return 0;
10335}
10336
James Smarte59058c2008-08-24 21:49:00 -040010337/**
James Smart3621a712009-04-06 18:47:14 -040010338 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010339 * @phba: Pointer to HBA context object.
10340 *
10341 * When HBQ is enabled, buffers are searched based on tags. This function
10342 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10343 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10344 * does not conflict with tags of buffer posted for unsolicited events.
10345 * The function returns the allocated tag. The function is called with
10346 * no locks held.
10347 **/
James Smart76bb24e2007-10-27 13:38:00 -040010348uint32_t
10349lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10350{
10351 spin_lock_irq(&phba->hbalock);
10352 phba->buffer_tag_count++;
10353 /*
10354 * Always set the QUE_BUFTAG_BIT to distiguish between
10355 * a tag assigned by HBQ.
10356 */
10357 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10358 spin_unlock_irq(&phba->hbalock);
10359 return phba->buffer_tag_count;
10360}
10361
James Smarte59058c2008-08-24 21:49:00 -040010362/**
James Smart3621a712009-04-06 18:47:14 -040010363 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010364 * @phba: Pointer to HBA context object.
10365 * @pring: Pointer to driver SLI ring object.
10366 * @tag: Buffer tag.
10367 *
10368 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10369 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10370 * iocb is posted to the response ring with the tag of the buffer.
10371 * This function searches the pring->postbufq list using the tag
10372 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10373 * iocb. If the buffer is found then lpfc_dmabuf object of the
10374 * buffer is returned to the caller else NULL is returned.
10375 * This function is called with no lock held.
10376 **/
James Smart76bb24e2007-10-27 13:38:00 -040010377struct lpfc_dmabuf *
10378lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10379 uint32_t tag)
10380{
10381 struct lpfc_dmabuf *mp, *next_mp;
10382 struct list_head *slp = &pring->postbufq;
10383
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010384 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010385 spin_lock_irq(&phba->hbalock);
10386 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10387 if (mp->buffer_tag == tag) {
10388 list_del_init(&mp->list);
10389 pring->postbufq_cnt--;
10390 spin_unlock_irq(&phba->hbalock);
10391 return mp;
10392 }
10393 }
10394
10395 spin_unlock_irq(&phba->hbalock);
10396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010397 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010398 "ring %d Data x%lx x%p x%p x%x\n",
10399 pring->ringno, (unsigned long) tag,
10400 slp->next, slp->prev, pring->postbufq_cnt);
10401
10402 return NULL;
10403}
dea31012005-04-17 16:05:31 -050010404
James Smarte59058c2008-08-24 21:49:00 -040010405/**
James Smart3621a712009-04-06 18:47:14 -040010406 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010407 * @phba: Pointer to HBA context object.
10408 * @pring: Pointer to driver SLI ring object.
10409 * @phys: DMA address of the buffer.
10410 *
10411 * This function searches the buffer list using the dma_address
10412 * of unsolicited event to find the driver's lpfc_dmabuf object
10413 * corresponding to the dma_address. The function returns the
10414 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10415 * This function is called by the ct and els unsolicited event
10416 * handlers to get the buffer associated with the unsolicited
10417 * event.
10418 *
10419 * This function is called with no lock held.
10420 **/
dea31012005-04-17 16:05:31 -050010421struct lpfc_dmabuf *
10422lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10423 dma_addr_t phys)
10424{
10425 struct lpfc_dmabuf *mp, *next_mp;
10426 struct list_head *slp = &pring->postbufq;
10427
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010428 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010429 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010430 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10431 if (mp->phys == phys) {
10432 list_del_init(&mp->list);
10433 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010434 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010435 return mp;
10436 }
10437 }
10438
James Smart2e0fef82007-06-17 19:56:36 -050010439 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010441 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010442 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010443 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010444 slp->next, slp->prev, pring->postbufq_cnt);
10445 return NULL;
10446}
10447
James Smarte59058c2008-08-24 21:49:00 -040010448/**
James Smart3621a712009-04-06 18:47:14 -040010449 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010450 * @phba: Pointer to HBA context object.
10451 * @cmdiocb: Pointer to driver command iocb object.
10452 * @rspiocb: Pointer to driver response iocb object.
10453 *
10454 * This function is the completion handler for the abort iocbs for
10455 * ELS commands. This function is called from the ELS ring event
10456 * handler with no lock held. This function frees memory resources
10457 * associated with the abort iocb.
10458 **/
dea31012005-04-17 16:05:31 -050010459static void
James Smart2e0fef82007-06-17 19:56:36 -050010460lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10461 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010462{
James Smart2e0fef82007-06-17 19:56:36 -050010463 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010464 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010465 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010466
10467 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010468
10469 /*
10470 * Assume that the port already completed and returned, or
10471 * will return the iocb. Just Log the message.
10472 */
James Smart2680eea2007-04-25 09:52:55 -040010473 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10474 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10475
James Smart2e0fef82007-06-17 19:56:36 -050010476 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010477 if (phba->sli_rev < LPFC_SLI_REV4) {
10478 if (abort_iotag != 0 &&
10479 abort_iotag <= phba->sli.last_iotag)
10480 abort_iocb =
10481 phba->sli.iocbq_lookup[abort_iotag];
10482 } else
10483 /* For sli4 the abort_tag is the XRI,
10484 * so the abort routine puts the iotag of the iocb
10485 * being aborted in the context field of the abort
10486 * IOCB.
10487 */
10488 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010489
James Smart2a9bf3d2010-06-07 15:24:45 -040010490 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10491 "0327 Cannot abort els iocb %p "
10492 "with tag %x context %x, abort status %x, "
10493 "abort code %x\n",
10494 abort_iocb, abort_iotag, abort_context,
10495 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010496
James Smartff78d8f2011-12-13 13:21:35 -050010497 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010498 }
James Bottomley604a3e32005-10-29 10:28:33 -050010499 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010500 return;
10501}
10502
James Smarte59058c2008-08-24 21:49:00 -040010503/**
James Smart3621a712009-04-06 18:47:14 -040010504 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010505 * @phba: Pointer to HBA context object.
10506 * @cmdiocb: Pointer to driver command iocb object.
10507 * @rspiocb: Pointer to driver response iocb object.
10508 *
10509 * The function is called from SLI ring event handler with no
10510 * lock held. This function is the completion handler for ELS commands
10511 * which are aborted. The function frees memory resources used for
10512 * the aborted ELS commands.
10513 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010514static void
10515lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10516 struct lpfc_iocbq *rspiocb)
10517{
10518 IOCB_t *irsp = &rspiocb->iocb;
10519
10520 /* ELS cmd tag <ulpIoTag> completes */
10521 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010522 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010523 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010524 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010525 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010526 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10527 lpfc_ct_free_iocb(phba, cmdiocb);
10528 else
10529 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010530 return;
10531}
10532
James Smarte59058c2008-08-24 21:49:00 -040010533/**
James Smart5af5eee2010-10-22 11:06:38 -040010534 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010535 * @phba: Pointer to HBA context object.
10536 * @pring: Pointer to driver SLI ring object.
10537 * @cmdiocb: Pointer to driver command iocb object.
10538 *
James Smart5af5eee2010-10-22 11:06:38 -040010539 * This function issues an abort iocb for the provided command iocb down to
10540 * the port. Other than the case the outstanding command iocb is an abort
10541 * request, this function issues abort out unconditionally. This function is
10542 * called with hbalock held. The function returns 0 when it fails due to
10543 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010544 **/
James Smart5af5eee2010-10-22 11:06:38 -040010545static int
10546lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010547 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010548{
James Smart2e0fef82007-06-17 19:56:36 -050010549 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010550 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010551 IOCB_t *icmd = NULL;
10552 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010553 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010554 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010555
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010556 lockdep_assert_held(&phba->hbalock);
10557
James Smart92d7f7b2007-06-17 19:56:38 -050010558 /*
10559 * There are certain command types we don't want to abort. And we
10560 * don't want to abort commands that are already in the process of
10561 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010562 */
10563 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010564 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010565 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10566 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010567 return 0;
10568
dea31012005-04-17 16:05:31 -050010569 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010570 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010571 if (abtsiocbp == NULL)
10572 return 0;
dea31012005-04-17 16:05:31 -050010573
James Smart07951072007-04-25 09:51:38 -040010574 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010575 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010576 */
10577 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10578
dea31012005-04-17 16:05:31 -050010579 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010580 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10581 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010582 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010583 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010584 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10585 }
James Smartda0436e2009-05-22 14:51:39 -040010586 else
10587 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010588 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010589 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010590
James Smart5ffc2662009-11-18 15:39:44 -050010591 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010592 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010593 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10594 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010595 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10596 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010597
James Smart2e0fef82007-06-17 19:56:36 -050010598 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010599 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10600 else
10601 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10602
10603 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010604 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010605
James Smarte8b62012007-08-02 11:10:09 -040010606 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10607 "0339 Abort xri x%x, original iotag x%x, "
10608 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010609 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010610 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010611 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010612
10613 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010614 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10615 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010616 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010617 /* Note: both hbalock and ring_lock need to be set here */
10618 spin_lock_irqsave(&pring->ring_lock, iflags);
10619 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10620 abtsiocbp, 0);
10621 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10622 } else {
10623 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10624 abtsiocbp, 0);
10625 }
James Smart07951072007-04-25 09:51:38 -040010626
James Smartd7c255b2008-08-24 21:50:00 -040010627 if (retval)
10628 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010629
10630 /*
10631 * Caller to this routine should check for IOCB_ERROR
10632 * and handle it properly. This routine no longer removes
10633 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10634 */
10635 return retval;
10636}
10637
10638/**
10639 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10640 * @phba: Pointer to HBA context object.
10641 * @pring: Pointer to driver SLI ring object.
10642 * @cmdiocb: Pointer to driver command iocb object.
10643 *
10644 * This function issues an abort iocb for the provided command iocb. In case
10645 * of unloading, the abort iocb will not be issued to commands on the ELS
10646 * ring. Instead, the callback function shall be changed to those commands
10647 * so that nothing happens when them finishes. This function is called with
10648 * hbalock held. The function returns 0 when the command iocb is an abort
10649 * request.
10650 **/
10651int
10652lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10653 struct lpfc_iocbq *cmdiocb)
10654{
10655 struct lpfc_vport *vport = cmdiocb->vport;
10656 int retval = IOCB_ERROR;
10657 IOCB_t *icmd = NULL;
10658
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010659 lockdep_assert_held(&phba->hbalock);
10660
James Smart5af5eee2010-10-22 11:06:38 -040010661 /*
10662 * There are certain command types we don't want to abort. And we
10663 * don't want to abort commands that are already in the process of
10664 * being aborted.
10665 */
10666 icmd = &cmdiocb->iocb;
10667 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10668 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10669 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10670 return 0;
10671
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010672 if (!pring) {
10673 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10674 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10675 else
10676 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10677 goto abort_iotag_exit;
10678 }
10679
James Smart5af5eee2010-10-22 11:06:38 -040010680 /*
10681 * If we're unloading, don't abort iocb on the ELS ring, but change
10682 * the callback so that nothing happens when it finishes.
10683 */
10684 if ((vport->load_flag & FC_UNLOADING) &&
10685 (pring->ringno == LPFC_ELS_RING)) {
10686 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10687 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10688 else
10689 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10690 goto abort_iotag_exit;
10691 }
10692
10693 /* Now, we try to issue the abort to the cmdiocb out */
10694 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10695
James Smart07951072007-04-25 09:51:38 -040010696abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010697 /*
10698 * Caller to this routine should check for IOCB_ERROR
10699 * and handle it properly. This routine no longer removes
10700 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010701 */
James Smart2e0fef82007-06-17 19:56:36 -050010702 return retval;
dea31012005-04-17 16:05:31 -050010703}
10704
James Smarte59058c2008-08-24 21:49:00 -040010705/**
James Smart895427b2017-02-12 13:52:30 -080010706 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10707 * @phba: Pointer to HBA context object.
10708 * @pring: Pointer to driver SLI ring object.
10709 * @cmdiocb: Pointer to driver command iocb object.
10710 *
10711 * This function issues an abort iocb for the provided command iocb down to
10712 * the port. Other than the case the outstanding command iocb is an abort
10713 * request, this function issues abort out unconditionally. This function is
10714 * called with hbalock held. The function returns 0 when it fails due to
10715 * memory allocation failure or when the command iocb is an abort request.
10716 **/
10717static int
10718lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10719 struct lpfc_iocbq *cmdiocb)
10720{
10721 struct lpfc_vport *vport = cmdiocb->vport;
10722 struct lpfc_iocbq *abtsiocbp;
10723 union lpfc_wqe *abts_wqe;
10724 int retval;
10725
10726 /*
10727 * There are certain command types we don't want to abort. And we
10728 * don't want to abort commands that are already in the process of
10729 * being aborted.
10730 */
10731 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10732 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10733 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10734 return 0;
10735
10736 /* issue ABTS for this io based on iotag */
10737 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10738 if (abtsiocbp == NULL)
10739 return 0;
10740
10741 /* This signals the response to set the correct status
10742 * before calling the completion handler
10743 */
10744 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10745
10746 /* Complete prepping the abort wqe and issue to the FW. */
10747 abts_wqe = &abtsiocbp->wqe;
10748 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10749 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10750
10751 /* Explicitly set reserved fields to zero.*/
10752 abts_wqe->abort_cmd.rsrvd4 = 0;
10753 abts_wqe->abort_cmd.rsrvd5 = 0;
10754
10755 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10756 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10757 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10758
10759 /* word 7 */
10760 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10761 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10762 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10763 cmdiocb->iocb.ulpClass);
10764
10765 /* word 8 - tell the FW to abort the IO associated with this
10766 * outstanding exchange ID.
10767 */
10768 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10769
10770 /* word 9 - this is the iotag for the abts_wqe completion. */
10771 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10772 abtsiocbp->iotag);
10773
10774 /* word 10 */
10775 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10776 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10777 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10778
10779 /* word 11 */
10780 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10781 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10782 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10783
10784 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10785 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10786 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010787 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010788 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010789 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010790 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10791 "6147 Failed abts issue_wqe with status x%x "
10792 "for oxid x%x\n",
10793 retval, cmdiocb->sli4_xritag);
10794 lpfc_sli_release_iocbq(phba, abtsiocbp);
10795 return retval;
10796 }
10797
10798 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10799 "6148 Drv Abort NVME Request Issued for "
10800 "ox_id x%x on reqtag x%x\n",
10801 cmdiocb->sli4_xritag,
10802 abtsiocbp->iotag);
10803
10804 return retval;
10805}
10806
10807/**
James Smart5af5eee2010-10-22 11:06:38 -040010808 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10809 * @phba: pointer to lpfc HBA data structure.
10810 *
10811 * This routine will abort all pending and outstanding iocbs to an HBA.
10812 **/
10813void
10814lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10815{
10816 struct lpfc_sli *psli = &phba->sli;
10817 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010818 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010819 int i;
10820
James Smart895427b2017-02-12 13:52:30 -080010821 if (phba->sli_rev != LPFC_SLI_REV4) {
10822 for (i = 0; i < psli->num_rings; i++) {
10823 pring = &psli->sli3_ring[i];
10824 lpfc_sli_abort_iocb_ring(phba, pring);
10825 }
10826 return;
10827 }
10828 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10829 pring = qp->pring;
10830 if (!pring)
10831 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010832 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010833 }
10834}
10835
10836/**
James Smart3621a712009-04-06 18:47:14 -040010837 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010838 * @iocbq: Pointer to driver iocb object.
10839 * @vport: Pointer to driver virtual port object.
10840 * @tgt_id: SCSI ID of the target.
10841 * @lun_id: LUN ID of the scsi device.
10842 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10843 *
James Smart3621a712009-04-06 18:47:14 -040010844 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010845 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10846 * 0 if the filtering criteria is met for the given iocb and will return
10847 * 1 if the filtering criteria is not met.
10848 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10849 * given iocb is for the SCSI device specified by vport, tgt_id and
10850 * lun_id parameter.
10851 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10852 * given iocb is for the SCSI target specified by vport and tgt_id
10853 * parameters.
10854 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10855 * given iocb is for the SCSI host associated with the given vport.
10856 * This function is called with no locks held.
10857 **/
dea31012005-04-17 16:05:31 -050010858static int
James Smart51ef4c22007-08-02 11:10:31 -040010859lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10860 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010861 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010862{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010863 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010864 int rc = 1;
10865
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010866 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10867 return rc;
10868
James Smart51ef4c22007-08-02 11:10:31 -040010869 if (iocbq->vport != vport)
10870 return rc;
10871
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010872 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010873
James Smart495a7142008-06-14 22:52:59 -040010874 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010875 return rc;
10876
10877 switch (ctx_cmd) {
10878 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010879 if ((lpfc_cmd->rdata->pnode) &&
10880 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10881 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010882 rc = 0;
10883 break;
10884 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010885 if ((lpfc_cmd->rdata->pnode) &&
10886 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010887 rc = 0;
10888 break;
dea31012005-04-17 16:05:31 -050010889 case LPFC_CTX_HOST:
10890 rc = 0;
10891 break;
10892 default:
10893 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010894 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010895 break;
10896 }
10897
10898 return rc;
10899}
10900
James Smarte59058c2008-08-24 21:49:00 -040010901/**
James Smart3621a712009-04-06 18:47:14 -040010902 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010903 * @vport: Pointer to virtual port.
10904 * @tgt_id: SCSI ID of the target.
10905 * @lun_id: LUN ID of the scsi device.
10906 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10907 *
10908 * This function returns number of FCP commands pending for the vport.
10909 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10910 * commands pending on the vport associated with SCSI device specified
10911 * by tgt_id and lun_id parameters.
10912 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10913 * commands pending on the vport associated with SCSI target specified
10914 * by tgt_id parameter.
10915 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10916 * commands pending on the vport.
10917 * This function returns the number of iocbs which satisfy the filter.
10918 * This function is called without any lock held.
10919 **/
dea31012005-04-17 16:05:31 -050010920int
James Smart51ef4c22007-08-02 11:10:31 -040010921lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10922 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010923{
James Smart51ef4c22007-08-02 11:10:31 -040010924 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010925 struct lpfc_iocbq *iocbq;
10926 int sum, i;
dea31012005-04-17 16:05:31 -050010927
Johannes Thumshirn31979002016-07-18 16:06:03 +020010928 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010929 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10930 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010931
James Smart51ef4c22007-08-02 11:10:31 -040010932 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10933 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010934 sum++;
dea31012005-04-17 16:05:31 -050010935 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010936 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010937
dea31012005-04-17 16:05:31 -050010938 return sum;
10939}
10940
James Smarte59058c2008-08-24 21:49:00 -040010941/**
James Smart3621a712009-04-06 18:47:14 -040010942 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010943 * @phba: Pointer to HBA context object
10944 * @cmdiocb: Pointer to command iocb object.
10945 * @rspiocb: Pointer to response iocb object.
10946 *
10947 * This function is called when an aborted FCP iocb completes. This
10948 * function is called by the ring event handler with no lock held.
10949 * This function frees the iocb.
10950 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010951void
James Smart2e0fef82007-06-17 19:56:36 -050010952lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10953 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010954{
James Smartcb69f7d2011-12-13 13:21:57 -050010955 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010956 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010957 "original iotag x%x, abort cmd iotag x%x "
10958 "status 0x%x, reason 0x%x\n",
10959 cmdiocb->iocb.un.acxri.abortContextTag,
10960 cmdiocb->iocb.un.acxri.abortIoTag,
10961 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10962 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010963 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010964 return;
10965}
10966
James Smarte59058c2008-08-24 21:49:00 -040010967/**
James Smart3621a712009-04-06 18:47:14 -040010968 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010969 * @vport: Pointer to virtual port.
10970 * @pring: Pointer to driver SLI ring object.
10971 * @tgt_id: SCSI ID of the target.
10972 * @lun_id: LUN ID of the scsi device.
10973 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10974 *
10975 * This function sends an abort command for every SCSI command
10976 * associated with the given virtual port pending on the ring
10977 * filtered by lpfc_sli_validate_fcp_iocb function.
10978 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10979 * FCP iocbs associated with lun specified by tgt_id and lun_id
10980 * parameters
10981 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10982 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10983 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10984 * FCP iocbs associated with virtual port.
10985 * This function returns number of iocbs it failed to abort.
10986 * This function is called with no locks held.
10987 **/
dea31012005-04-17 16:05:31 -050010988int
James Smart51ef4c22007-08-02 11:10:31 -040010989lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10990 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010991{
James Smart51ef4c22007-08-02 11:10:31 -040010992 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010993 struct lpfc_iocbq *iocbq;
10994 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070010995 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050010996 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010997 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010998 int i;
dea31012005-04-17 16:05:31 -050010999
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011000 for (i = 1; i <= phba->sli.last_iotag; i++) {
11001 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011002
James Smart51ef4c22007-08-02 11:10:31 -040011003 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011004 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011005 continue;
11006
James Smartafbd8d82013-09-06 12:22:13 -040011007 /*
11008 * If the iocbq is already being aborted, don't take a second
11009 * action, but do count it.
11010 */
11011 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11012 continue;
11013
dea31012005-04-17 16:05:31 -050011014 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011015 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011016 if (abtsiocb == NULL) {
11017 errcnt++;
11018 continue;
11019 }
dea31012005-04-17 16:05:31 -050011020
James Smartafbd8d82013-09-06 12:22:13 -040011021 /* indicate the IO is being aborted by the driver. */
11022 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11023
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011024 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011025 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11026 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011027 if (phba->sli_rev == LPFC_SLI_REV4)
11028 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11029 else
11030 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011031 abtsiocb->iocb.ulpLe = 1;
11032 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011033 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011034
James Smart5ffc2662009-11-18 15:39:44 -050011035 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011036 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011037 if (iocbq->iocb_flag & LPFC_IO_FCP)
11038 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011039 if (iocbq->iocb_flag & LPFC_IO_FOF)
11040 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011041
James Smart2e0fef82007-06-17 19:56:36 -050011042 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011043 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11044 else
11045 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11046
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011047 /* Setup callback routine and issue the command. */
11048 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011049 if (phba->sli_rev == LPFC_SLI_REV4) {
11050 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11051 if (!pring_s4)
11052 continue;
11053 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11054 abtsiocb, 0);
11055 } else
11056 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11057 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011058 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011059 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011060 errcnt++;
11061 continue;
11062 }
11063 }
11064
11065 return errcnt;
11066}
11067
James Smarte59058c2008-08-24 21:49:00 -040011068/**
James Smart98912dda2014-04-04 13:52:31 -040011069 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11070 * @vport: Pointer to virtual port.
11071 * @pring: Pointer to driver SLI ring object.
11072 * @tgt_id: SCSI ID of the target.
11073 * @lun_id: LUN ID of the scsi device.
11074 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11075 *
11076 * This function sends an abort command for every SCSI command
11077 * associated with the given virtual port pending on the ring
11078 * filtered by lpfc_sli_validate_fcp_iocb function.
11079 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11080 * FCP iocbs associated with lun specified by tgt_id and lun_id
11081 * parameters
11082 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11083 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11084 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11085 * FCP iocbs associated with virtual port.
11086 * This function returns number of iocbs it aborted .
11087 * This function is called with no locks held right after a taskmgmt
11088 * command is sent.
11089 **/
11090int
11091lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11092 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11093{
11094 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011095 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011096 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011097 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011098 struct lpfc_iocbq *iocbq;
11099 IOCB_t *icmd;
11100 int sum, i, ret_val;
11101 unsigned long iflags;
11102 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011103
11104 spin_lock_irq(&phba->hbalock);
11105
11106 /* all I/Os are in process of being flushed */
11107 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11108 spin_unlock_irq(&phba->hbalock);
11109 return 0;
11110 }
11111 sum = 0;
11112
11113 for (i = 1; i <= phba->sli.last_iotag; i++) {
11114 iocbq = phba->sli.iocbq_lookup[i];
11115
11116 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11117 cmd) != 0)
11118 continue;
11119
11120 /*
11121 * If the iocbq is already being aborted, don't take a second
11122 * action, but do count it.
11123 */
11124 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11125 continue;
11126
11127 /* issue ABTS for this IOCB based on iotag */
11128 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11129 if (abtsiocbq == NULL)
11130 continue;
11131
11132 icmd = &iocbq->iocb;
11133 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11134 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11135 if (phba->sli_rev == LPFC_SLI_REV4)
11136 abtsiocbq->iocb.un.acxri.abortIoTag =
11137 iocbq->sli4_xritag;
11138 else
11139 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11140 abtsiocbq->iocb.ulpLe = 1;
11141 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11142 abtsiocbq->vport = vport;
11143
11144 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011145 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011146 if (iocbq->iocb_flag & LPFC_IO_FCP)
11147 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011148 if (iocbq->iocb_flag & LPFC_IO_FOF)
11149 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011150
James Smart8c50d252014-09-03 12:58:16 -040011151 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11152 ndlp = lpfc_cmd->rdata->pnode;
11153
11154 if (lpfc_is_link_up(phba) &&
11155 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011156 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11157 else
11158 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11159
11160 /* Setup callback routine and issue the command. */
11161 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11162
11163 /*
11164 * Indicate the IO is being aborted by the driver and set
11165 * the caller's flag into the aborted IO.
11166 */
11167 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11168
11169 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011170 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11171 if (pring_s4 == NULL)
11172 continue;
James Smart98912dda2014-04-04 13:52:31 -040011173 /* Note: both hbalock and ring_lock must be set here */
11174 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11175 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11176 abtsiocbq, 0);
11177 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11178 } else {
11179 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11180 abtsiocbq, 0);
11181 }
11182
11183
11184 if (ret_val == IOCB_ERROR)
11185 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11186 else
11187 sum++;
11188 }
11189 spin_unlock_irq(&phba->hbalock);
11190 return sum;
11191}
11192
11193/**
James Smart3621a712009-04-06 18:47:14 -040011194 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011195 * @phba: Pointer to HBA context object.
11196 * @cmdiocbq: Pointer to command iocb.
11197 * @rspiocbq: Pointer to response iocb.
11198 *
11199 * This function is the completion handler for iocbs issued using
11200 * lpfc_sli_issue_iocb_wait function. This function is called by the
11201 * ring event handler function without any lock held. This function
11202 * can be called from both worker thread context and interrupt
11203 * context. This function also can be called from other thread which
11204 * cleans up the SLI layer objects.
11205 * This function copy the contents of the response iocb to the
11206 * response iocb memory object provided by the caller of
11207 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11208 * sleeps for the iocb completion.
11209 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011210static void
11211lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11212 struct lpfc_iocbq *cmdiocbq,
11213 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011214{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011215 wait_queue_head_t *pdone_q;
11216 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011217 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011218
James Smart2e0fef82007-06-17 19:56:36 -050011219 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011220 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11221
11222 /*
11223 * A time out has occurred for the iocb. If a time out
11224 * completion handler has been supplied, call it. Otherwise,
11225 * just free the iocbq.
11226 */
11227
11228 spin_unlock_irqrestore(&phba->hbalock, iflags);
11229 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11230 cmdiocbq->wait_iocb_cmpl = NULL;
11231 if (cmdiocbq->iocb_cmpl)
11232 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11233 else
11234 lpfc_sli_release_iocbq(phba, cmdiocbq);
11235 return;
11236 }
11237
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011238 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11239 if (cmdiocbq->context2 && rspiocbq)
11240 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11241 &rspiocbq->iocb, sizeof(IOCB_t));
11242
James Smart0f65ff62010-02-26 14:14:23 -050011243 /* Set the exchange busy flag for task management commands */
11244 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11245 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11246 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11247 cur_iocbq);
11248 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11249 }
11250
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011251 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011252 if (pdone_q)
11253 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011254 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011255 return;
11256}
11257
James Smarte59058c2008-08-24 21:49:00 -040011258/**
James Smartd11e31d2009-06-10 17:23:06 -040011259 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11260 * @phba: Pointer to HBA context object..
11261 * @piocbq: Pointer to command iocb.
11262 * @flag: Flag to test.
11263 *
11264 * This routine grabs the hbalock and then test the iocb_flag to
11265 * see if the passed in flag is set.
11266 * Returns:
11267 * 1 if flag is set.
11268 * 0 if flag is not set.
11269 **/
11270static int
11271lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11272 struct lpfc_iocbq *piocbq, uint32_t flag)
11273{
11274 unsigned long iflags;
11275 int ret;
11276
11277 spin_lock_irqsave(&phba->hbalock, iflags);
11278 ret = piocbq->iocb_flag & flag;
11279 spin_unlock_irqrestore(&phba->hbalock, iflags);
11280 return ret;
11281
11282}
11283
11284/**
James Smart3621a712009-04-06 18:47:14 -040011285 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011286 * @phba: Pointer to HBA context object..
11287 * @pring: Pointer to sli ring.
11288 * @piocb: Pointer to command iocb.
11289 * @prspiocbq: Pointer to response iocb.
11290 * @timeout: Timeout in number of seconds.
11291 *
11292 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011293 * iocb to complete. The iocb_cmpl field of the shall be used
11294 * to handle iocbs which time out. If the field is NULL, the
11295 * function shall free the iocbq structure. If more clean up is
11296 * needed, the caller is expected to provide a completion function
11297 * that will provide the needed clean up. If the iocb command is
11298 * not completed within timeout seconds, the function will either
11299 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11300 * completion function set in the iocb_cmpl field and then return
11301 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11302 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011303 * The function waits for the iocb completion using an
11304 * non-interruptible wait.
11305 * This function will sleep while waiting for iocb completion.
11306 * So, this function should not be called from any context which
11307 * does not allow sleeping. Due to the same reason, this function
11308 * cannot be called with interrupt disabled.
11309 * This function assumes that the iocb completions occur while
11310 * this function sleep. So, this function cannot be called from
11311 * the thread which process iocb completion for this ring.
11312 * This function clears the iocb_flag of the iocb object before
11313 * issuing the iocb and the iocb completion handler sets this
11314 * flag and wakes this thread when the iocb completes.
11315 * The contents of the response iocb will be copied to prspiocbq
11316 * by the completion handler when the command completes.
11317 * This function returns IOCB_SUCCESS when success.
11318 * This function is called with no lock held.
11319 **/
dea31012005-04-17 16:05:31 -050011320int
James Smart2e0fef82007-06-17 19:56:36 -050011321lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011322 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011323 struct lpfc_iocbq *piocb,
11324 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011325 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011326{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011327 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011328 long timeleft, timeout_req = 0;
11329 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011330 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011331 struct lpfc_iocbq *iocb;
11332 int txq_cnt = 0;
11333 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011334 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011335 unsigned long iflags;
11336 bool iocb_completed = true;
11337
James Smart895427b2017-02-12 13:52:30 -080011338 if (phba->sli_rev >= LPFC_SLI_REV4)
11339 pring = lpfc_sli4_calc_ring(phba, piocb);
11340 else
11341 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011342 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011343 * If the caller has provided a response iocbq buffer, then context2
11344 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011345 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011346 if (prspiocbq) {
11347 if (piocb->context2)
11348 return IOCB_ERROR;
11349 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011350 }
11351
James Smart5a0916b2013-07-15 18:31:42 -040011352 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011353 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11354 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011355 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011356
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011357 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011358 if (lpfc_readl(phba->HCregaddr, &creg_val))
11359 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011360 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11361 writel(creg_val, phba->HCregaddr);
11362 readl(phba->HCregaddr); /* flush */
11363 }
11364
James Smart2a9bf3d2010-06-07 15:24:45 -040011365 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11366 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011367 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011368 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011369 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011370 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011371 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011372 spin_lock_irqsave(&phba->hbalock, iflags);
11373 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011374
James Smart5a0916b2013-07-15 18:31:42 -040011375 /*
11376 * IOCB timed out. Inform the wake iocb wait
11377 * completion function and set local status
11378 */
11379
11380 iocb_completed = false;
11381 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11382 }
11383 spin_unlock_irqrestore(&phba->hbalock, iflags);
11384 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011385 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011386 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011387 /* Note: we are not indicating if the IOCB has a success
11388 * status or not - that's for the caller to check.
11389 * IOCB_SUCCESS means just that the command was sent and
11390 * completed. Not that it completed successfully.
11391 * */
James Smart7054a602007-04-25 09:52:34 -040011392 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011394 "0338 IOCB wait timeout error - no "
11395 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011396 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011397 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011399 "0330 IOCB wake NOT set, "
11400 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011401 timeout, (timeleft / jiffies));
11402 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011403 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011404 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011405 if (phba->cfg_log_verbose & LOG_SLI) {
11406 list_for_each_entry(iocb, &pring->txq, list) {
11407 txq_cnt++;
11408 }
11409 list_for_each_entry(iocb, &pring->txcmplq, list) {
11410 txcmplq_cnt++;
11411 }
11412 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11413 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11414 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11415 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011416 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011417 } else {
11418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011419 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011420 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011421 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011422 }
11423
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011424 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011425 if (lpfc_readl(phba->HCregaddr, &creg_val))
11426 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011427 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11428 writel(creg_val, phba->HCregaddr);
11429 readl(phba->HCregaddr); /* flush */
11430 }
11431
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011432 if (prspiocbq)
11433 piocb->context2 = NULL;
11434
11435 piocb->context_un.wait_queue = NULL;
11436 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011437 return retval;
11438}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011439
James Smarte59058c2008-08-24 21:49:00 -040011440/**
James Smart3621a712009-04-06 18:47:14 -040011441 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011442 * @phba: Pointer to HBA context object.
11443 * @pmboxq: Pointer to driver mailbox object.
11444 * @timeout: Timeout in number of seconds.
11445 *
11446 * This function issues the mailbox to firmware and waits for the
11447 * mailbox command to complete. If the mailbox command is not
11448 * completed within timeout seconds, it returns MBX_TIMEOUT.
11449 * The function waits for the mailbox completion using an
11450 * interruptible wait. If the thread is woken up due to a
11451 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11452 * should not free the mailbox resources, if this function returns
11453 * MBX_TIMEOUT.
11454 * This function will sleep while waiting for mailbox completion.
11455 * So, this function should not be called from any context which
11456 * does not allow sleeping. Due to the same reason, this function
11457 * cannot be called with interrupt disabled.
11458 * This function assumes that the mailbox completion occurs while
11459 * this function sleep. So, this function cannot be called from
11460 * the worker thread which processes mailbox completion.
11461 * This function is called in the context of HBA management
11462 * applications.
11463 * This function returns MBX_SUCCESS when successful.
11464 * This function is called with no lock held.
11465 **/
dea31012005-04-17 16:05:31 -050011466int
James Smart2e0fef82007-06-17 19:56:36 -050011467lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011468 uint32_t timeout)
11469{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011470 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011471 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011472 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011473 unsigned long flag;
dea31012005-04-17 16:05:31 -050011474
James Smartb230b8a2013-05-31 17:05:27 -040011475 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011476 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011477 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011478
James Smart495a7142008-06-14 22:52:59 -040011479 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011480 /* setup wake call as IOCB callback */
11481 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11482 /* setup context field to pass wait_queue pointer to wake function */
11483 pmboxq->context1 = &done_q;
11484
dea31012005-04-17 16:05:31 -050011485 /* now issue the command */
11486 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011487 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011488 wait_event_interruptible_timeout(done_q,
11489 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011490 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011491
James Smart858c9f62007-06-17 19:56:39 -050011492 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011493 /* restore the possible extended buffer for free resource */
11494 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011495 /*
11496 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11497 * else do not free the resources.
11498 */
James Smartd7c47992010-06-08 18:31:54 -040011499 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011500 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011501 } else {
James Smart7054a602007-04-25 09:52:34 -040011502 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011503 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11504 }
11505 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011506 } else {
11507 /* restore the possible extended buffer for free resource */
11508 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011509 }
11510
dea31012005-04-17 16:05:31 -050011511 return retval;
11512}
11513
James Smarte59058c2008-08-24 21:49:00 -040011514/**
James Smart3772a992009-05-22 14:50:54 -040011515 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011516 * @phba: Pointer to HBA context.
11517 *
James Smart3772a992009-05-22 14:50:54 -040011518 * This function is called to shutdown the driver's mailbox sub-system.
11519 * It first marks the mailbox sub-system is in a block state to prevent
11520 * the asynchronous mailbox command from issued off the pending mailbox
11521 * command queue. If the mailbox command sub-system shutdown is due to
11522 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11523 * the mailbox sub-system flush routine to forcefully bring down the
11524 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11525 * as with offline or HBA function reset), this routine will wait for the
11526 * outstanding mailbox command to complete before invoking the mailbox
11527 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011528 **/
James Smart3772a992009-05-22 14:50:54 -040011529void
James Smart618a5232012-06-12 13:54:36 -040011530lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011531{
James Smart3772a992009-05-22 14:50:54 -040011532 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011533 unsigned long timeout;
11534
James Smart618a5232012-06-12 13:54:36 -040011535 if (mbx_action == LPFC_MBX_NO_WAIT) {
11536 /* delay 100ms for port state */
11537 msleep(100);
11538 lpfc_sli_mbox_sys_flush(phba);
11539 return;
11540 }
James Smarta183a152011-10-10 21:32:43 -040011541 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011542
James Smart3772a992009-05-22 14:50:54 -040011543 spin_lock_irq(&phba->hbalock);
11544 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011545
11546 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011547 /* Determine how long we might wait for the active mailbox
11548 * command to be gracefully completed by firmware.
11549 */
James Smarta183a152011-10-10 21:32:43 -040011550 if (phba->sli.mbox_active)
11551 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11552 phba->sli.mbox_active) *
11553 1000) + jiffies;
11554 spin_unlock_irq(&phba->hbalock);
11555
James Smart3772a992009-05-22 14:50:54 -040011556 while (phba->sli.mbox_active) {
11557 /* Check active mailbox complete status every 2ms */
11558 msleep(2);
11559 if (time_after(jiffies, timeout))
11560 /* Timeout, let the mailbox flush routine to
11561 * forcefully release active mailbox command
11562 */
11563 break;
11564 }
James Smartd7069f02012-03-01 22:36:29 -050011565 } else
11566 spin_unlock_irq(&phba->hbalock);
11567
James Smart3772a992009-05-22 14:50:54 -040011568 lpfc_sli_mbox_sys_flush(phba);
11569}
11570
11571/**
11572 * lpfc_sli_eratt_read - read sli-3 error attention events
11573 * @phba: Pointer to HBA context.
11574 *
11575 * This function is called to read the SLI3 device error attention registers
11576 * for possible error attention events. The caller must hold the hostlock
11577 * with spin_lock_irq().
11578 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011579 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011580 * Register and returns 0 otherwise.
11581 **/
11582static int
11583lpfc_sli_eratt_read(struct lpfc_hba *phba)
11584{
James Smarted957682007-06-17 19:56:37 -050011585 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011586
James Smart3772a992009-05-22 14:50:54 -040011587 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011588 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11589 goto unplug_err;
11590
James Smart3772a992009-05-22 14:50:54 -040011591 if (ha_copy & HA_ERATT) {
11592 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011593 if (lpfc_sli_read_hs(phba))
11594 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011595
James Smart3772a992009-05-22 14:50:54 -040011596 /* Check if there is a deferred error condition is active */
11597 if ((HS_FFER1 & phba->work_hs) &&
11598 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011599 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011600 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011601 /* Clear all interrupt enable conditions */
11602 writel(0, phba->HCregaddr);
11603 readl(phba->HCregaddr);
11604 }
11605
11606 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011607 phba->work_ha |= HA_ERATT;
11608 /* Indicate polling handles this ERATT */
11609 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011610 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011611 }
James Smart3772a992009-05-22 14:50:54 -040011612 return 0;
James Smart9940b972011-03-11 16:06:12 -050011613
11614unplug_err:
11615 /* Set the driver HS work bitmap */
11616 phba->work_hs |= UNPLUG_ERR;
11617 /* Set the driver HA work bitmap */
11618 phba->work_ha |= HA_ERATT;
11619 /* Indicate polling handles this ERATT */
11620 phba->hba_flag |= HBA_ERATT_HANDLED;
11621 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011622}
11623
James Smarte59058c2008-08-24 21:49:00 -040011624/**
James Smartda0436e2009-05-22 14:51:39 -040011625 * lpfc_sli4_eratt_read - read sli-4 error attention events
11626 * @phba: Pointer to HBA context.
11627 *
11628 * This function is called to read the SLI4 device error attention registers
11629 * for possible error attention events. The caller must hold the hostlock
11630 * with spin_lock_irq().
11631 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011632 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011633 * Register and returns 0 otherwise.
11634 **/
11635static int
11636lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11637{
11638 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011639 uint32_t if_type, portsmphr;
11640 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011641
James Smart2fcee4b2010-12-15 17:57:46 -050011642 /*
11643 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011644 * registers for error attention. This can be changed later.
11645 */
James Smart2fcee4b2010-12-15 17:57:46 -050011646 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11647 switch (if_type) {
11648 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011649 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11650 &uerr_sta_lo) ||
11651 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11652 &uerr_sta_hi)) {
11653 phba->work_hs |= UNPLUG_ERR;
11654 phba->work_ha |= HA_ERATT;
11655 phba->hba_flag |= HBA_ERATT_HANDLED;
11656 return 1;
11657 }
James Smart2fcee4b2010-12-15 17:57:46 -050011658 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11659 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11661 "1423 HBA Unrecoverable error: "
11662 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11663 "ue_mask_lo_reg=0x%x, "
11664 "ue_mask_hi_reg=0x%x\n",
11665 uerr_sta_lo, uerr_sta_hi,
11666 phba->sli4_hba.ue_mask_lo,
11667 phba->sli4_hba.ue_mask_hi);
11668 phba->work_status[0] = uerr_sta_lo;
11669 phba->work_status[1] = uerr_sta_hi;
11670 phba->work_ha |= HA_ERATT;
11671 phba->hba_flag |= HBA_ERATT_HANDLED;
11672 return 1;
11673 }
11674 break;
11675 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011676 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11677 &portstat_reg.word0) ||
11678 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11679 &portsmphr)){
11680 phba->work_hs |= UNPLUG_ERR;
11681 phba->work_ha |= HA_ERATT;
11682 phba->hba_flag |= HBA_ERATT_HANDLED;
11683 return 1;
11684 }
James Smart2fcee4b2010-12-15 17:57:46 -050011685 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11686 phba->work_status[0] =
11687 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11688 phba->work_status[1] =
11689 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011691 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011692 "port status reg 0x%x, "
11693 "port smphr reg 0x%x, "
11694 "error 1=0x%x, error 2=0x%x\n",
11695 portstat_reg.word0,
11696 portsmphr,
11697 phba->work_status[0],
11698 phba->work_status[1]);
11699 phba->work_ha |= HA_ERATT;
11700 phba->hba_flag |= HBA_ERATT_HANDLED;
11701 return 1;
11702 }
11703 break;
11704 case LPFC_SLI_INTF_IF_TYPE_1:
11705 default:
James Smarta747c9c2009-11-18 15:41:10 -050011706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011707 "2886 HBA Error Attention on unsupported "
11708 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011709 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011710 }
James Smart2fcee4b2010-12-15 17:57:46 -050011711
James Smartda0436e2009-05-22 14:51:39 -040011712 return 0;
11713}
11714
11715/**
James Smart3621a712009-04-06 18:47:14 -040011716 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011717 * @phba: Pointer to HBA context.
11718 *
James Smart3772a992009-05-22 14:50:54 -040011719 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011720 * error attention register bit for error attention events.
11721 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011722 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011723 * Register and returns 0 otherwise.
11724 **/
11725int
11726lpfc_sli_check_eratt(struct lpfc_hba *phba)
11727{
11728 uint32_t ha_copy;
11729
11730 /* If somebody is waiting to handle an eratt, don't process it
11731 * here. The brdkill function will do this.
11732 */
11733 if (phba->link_flag & LS_IGNORE_ERATT)
11734 return 0;
11735
11736 /* Check if interrupt handler handles this ERATT */
11737 spin_lock_irq(&phba->hbalock);
11738 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11739 /* Interrupt handler has handled ERATT */
11740 spin_unlock_irq(&phba->hbalock);
11741 return 0;
11742 }
11743
James Smarta257bf92009-04-06 18:48:10 -040011744 /*
11745 * If there is deferred error attention, do not check for error
11746 * attention
11747 */
11748 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11749 spin_unlock_irq(&phba->hbalock);
11750 return 0;
11751 }
11752
James Smart3772a992009-05-22 14:50:54 -040011753 /* If PCI channel is offline, don't process it */
11754 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011755 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011756 return 0;
11757 }
11758
11759 switch (phba->sli_rev) {
11760 case LPFC_SLI_REV2:
11761 case LPFC_SLI_REV3:
11762 /* Read chip Host Attention (HA) register */
11763 ha_copy = lpfc_sli_eratt_read(phba);
11764 break;
James Smartda0436e2009-05-22 14:51:39 -040011765 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011766 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011767 ha_copy = lpfc_sli4_eratt_read(phba);
11768 break;
James Smart3772a992009-05-22 14:50:54 -040011769 default:
11770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11771 "0299 Invalid SLI revision (%d)\n",
11772 phba->sli_rev);
11773 ha_copy = 0;
11774 break;
James Smart93996272008-08-24 21:50:30 -040011775 }
11776 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011777
11778 return ha_copy;
11779}
11780
11781/**
11782 * lpfc_intr_state_check - Check device state for interrupt handling
11783 * @phba: Pointer to HBA context.
11784 *
11785 * This inline routine checks whether a device or its PCI slot is in a state
11786 * that the interrupt should be handled.
11787 *
11788 * This function returns 0 if the device or the PCI slot is in a state that
11789 * interrupt should be handled, otherwise -EIO.
11790 */
11791static inline int
11792lpfc_intr_state_check(struct lpfc_hba *phba)
11793{
11794 /* If the pci channel is offline, ignore all the interrupts */
11795 if (unlikely(pci_channel_offline(phba->pcidev)))
11796 return -EIO;
11797
11798 /* Update device level interrupt statistics */
11799 phba->sli.slistat.sli_intr++;
11800
11801 /* Ignore all interrupts during initialization. */
11802 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11803 return -EIO;
11804
James Smart93996272008-08-24 21:50:30 -040011805 return 0;
11806}
11807
11808/**
James Smart3772a992009-05-22 14:50:54 -040011809 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011810 * @irq: Interrupt number.
11811 * @dev_id: The device context pointer.
11812 *
James Smart93996272008-08-24 21:50:30 -040011813 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011814 * service routine when device with SLI-3 interface spec is enabled with
11815 * MSI-X multi-message interrupt mode and there are slow-path events in
11816 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11817 * interrupt mode, this function is called as part of the device-level
11818 * interrupt handler. When the PCI slot is in error recovery or the HBA
11819 * is undergoing initialization, the interrupt handler will not process
11820 * the interrupt. The link attention and ELS ring attention events are
11821 * handled by the worker thread. The interrupt handler signals the worker
11822 * thread and returns for these events. This function is called without
11823 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011824 * structures.
11825 *
11826 * This function returns IRQ_HANDLED when interrupt is handled else it
11827 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011828 **/
dea31012005-04-17 16:05:31 -050011829irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011830lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011831{
James Smart2e0fef82007-06-17 19:56:36 -050011832 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011833 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011834 uint32_t work_ha_copy;
11835 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011836 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011837 uint32_t control;
11838
James Smart92d7f7b2007-06-17 19:56:38 -050011839 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011840 struct lpfc_vport *vport;
11841 struct lpfc_nodelist *ndlp;
11842 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011843 LPFC_MBOXQ_t *pmb;
11844 int rc;
11845
dea31012005-04-17 16:05:31 -050011846 /*
11847 * Get the driver's phba structure from the dev_id and
11848 * assume the HBA is not interrupting.
11849 */
James Smart93996272008-08-24 21:50:30 -040011850 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011851
11852 if (unlikely(!phba))
11853 return IRQ_NONE;
11854
dea31012005-04-17 16:05:31 -050011855 /*
James Smart93996272008-08-24 21:50:30 -040011856 * Stuff needs to be attented to when this function is invoked as an
11857 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011858 */
James Smart93996272008-08-24 21:50:30 -040011859 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011860 /* Check device state for handling interrupt */
11861 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011862 return IRQ_NONE;
11863 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011864 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011865 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11866 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011867 /* If somebody is waiting to handle an eratt don't process it
11868 * here. The brdkill function will do this.
11869 */
11870 if (phba->link_flag & LS_IGNORE_ERATT)
11871 ha_copy &= ~HA_ERATT;
11872 /* Check the need for handling ERATT in interrupt handler */
11873 if (ha_copy & HA_ERATT) {
11874 if (phba->hba_flag & HBA_ERATT_HANDLED)
11875 /* ERATT polling has handled ERATT */
11876 ha_copy &= ~HA_ERATT;
11877 else
11878 /* Indicate interrupt handler handles ERATT */
11879 phba->hba_flag |= HBA_ERATT_HANDLED;
11880 }
James Smarta257bf92009-04-06 18:48:10 -040011881
11882 /*
11883 * If there is deferred error attention, do not check for any
11884 * interrupt.
11885 */
11886 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011887 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011888 return IRQ_NONE;
11889 }
11890
James Smart93996272008-08-24 21:50:30 -040011891 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011892 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11893 goto unplug_error;
11894
James Smarta747c9c2009-11-18 15:41:10 -050011895 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11896 HC_LAINT_ENA | HC_ERINT_ENA),
11897 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011898 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11899 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011900 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011901 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011902 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011903 } else
11904 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011905
dea31012005-04-17 16:05:31 -050011906 work_ha_copy = ha_copy & phba->work_ha_mask;
11907
James Smart93996272008-08-24 21:50:30 -040011908 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011909 if (work_ha_copy & HA_LATT) {
11910 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11911 /*
11912 * Turn off Link Attention interrupts
11913 * until CLEAR_LA done
11914 */
James Smart5b75da22008-12-04 22:39:35 -050011915 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011916 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011917 if (lpfc_readl(phba->HCregaddr, &control))
11918 goto unplug_error;
dea31012005-04-17 16:05:31 -050011919 control &= ~HC_LAINT_ENA;
11920 writel(control, phba->HCregaddr);
11921 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011922 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011923 }
11924 else
11925 work_ha_copy &= ~HA_LATT;
11926 }
11927
James Smart93996272008-08-24 21:50:30 -040011928 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011929 /*
11930 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11931 * the only slow ring.
11932 */
11933 status = (work_ha_copy &
11934 (HA_RXMASK << (4*LPFC_ELS_RING)));
11935 status >>= (4*LPFC_ELS_RING);
11936 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011937 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011938 if (lpfc_readl(phba->HCregaddr, &control))
11939 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011940
11941 lpfc_debugfs_slow_ring_trc(phba,
11942 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11943 control, status,
11944 (uint32_t)phba->sli.slistat.sli_intr);
11945
James Smart858c9f62007-06-17 19:56:39 -050011946 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011947 lpfc_debugfs_slow_ring_trc(phba,
11948 "ISR Disable ring:"
11949 "pwork:x%x hawork:x%x wait:x%x",
11950 phba->work_ha, work_ha_copy,
11951 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011952 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011953
James Smart858c9f62007-06-17 19:56:39 -050011954 control &=
11955 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011956 writel(control, phba->HCregaddr);
11957 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011958 }
James Smarta58cbd52007-08-02 11:09:43 -040011959 else {
11960 lpfc_debugfs_slow_ring_trc(phba,
11961 "ISR slow ring: pwork:"
11962 "x%x hawork:x%x wait:x%x",
11963 phba->work_ha, work_ha_copy,
11964 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011965 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011966 }
James Smart5b75da22008-12-04 22:39:35 -050011967 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011968 }
11969 }
James Smart5b75da22008-12-04 22:39:35 -050011970 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011971 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011972 if (lpfc_sli_read_hs(phba))
11973 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011974 /*
11975 * Check if there is a deferred error condition
11976 * is active
11977 */
11978 if ((HS_FFER1 & phba->work_hs) &&
11979 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011980 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11981 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011982 phba->hba_flag |= DEFER_ERATT;
11983 /* Clear all interrupt enable conditions */
11984 writel(0, phba->HCregaddr);
11985 readl(phba->HCregaddr);
11986 }
11987 }
11988
James Smart93996272008-08-24 21:50:30 -040011989 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011990 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011991 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011992 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011993 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011994
11995 /* First check out the status word */
11996 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11997 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011998 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011999 /*
12000 * Stray Mailbox Interrupt, mbxCommand <cmd>
12001 * mbxStatus <status>
12002 */
James Smart09372822008-01-11 01:52:54 -050012003 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012004 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012005 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012006 "Interrupt mbxCommand x%x "
12007 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012008 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012009 pmbox->mbxCommand,
12010 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012011 /* clear mailbox attention bit */
12012 work_ha_copy &= ~HA_MBATT;
12013 } else {
James Smart97eab632008-04-07 10:16:05 -040012014 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012015 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012016 phba->last_completion_time = jiffies;
12017 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012018 if (pmb->mbox_cmpl) {
12019 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12020 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012021 if (pmb->out_ext_byte_len &&
12022 pmb->context2)
12023 lpfc_sli_pcimem_bcopy(
12024 phba->mbox_ext,
12025 pmb->context2,
12026 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012027 }
James Smart09372822008-01-11 01:52:54 -050012028 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12029 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12030
12031 lpfc_debugfs_disc_trc(vport,
12032 LPFC_DISC_TRC_MBOX_VPORT,
12033 "MBOX dflt rpi: : "
12034 "status:x%x rpi:x%x",
12035 (uint32_t)pmbox->mbxStatus,
12036 pmbox->un.varWords[0], 0);
12037
12038 if (!pmbox->mbxStatus) {
12039 mp = (struct lpfc_dmabuf *)
12040 (pmb->context1);
12041 ndlp = (struct lpfc_nodelist *)
12042 pmb->context2;
12043
12044 /* Reg_LOGIN of dflt RPI was
12045 * successful. new lets get
12046 * rid of the RPI using the
12047 * same mbox buffer.
12048 */
12049 lpfc_unreg_login(phba,
12050 vport->vpi,
12051 pmbox->un.varWords[0],
12052 pmb);
12053 pmb->mbox_cmpl =
12054 lpfc_mbx_cmpl_dflt_rpi;
12055 pmb->context1 = mp;
12056 pmb->context2 = ndlp;
12057 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012058 rc = lpfc_sli_issue_mbox(phba,
12059 pmb,
12060 MBX_NOWAIT);
12061 if (rc != MBX_BUSY)
12062 lpfc_printf_log(phba,
12063 KERN_ERR,
12064 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012065 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012066 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012067 if (rc != MBX_NOT_FINISHED)
12068 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012069 }
12070 }
James Smart5b75da22008-12-04 22:39:35 -050012071 spin_lock_irqsave(
12072 &phba->pport->work_port_lock,
12073 iflag);
James Smart09372822008-01-11 01:52:54 -050012074 phba->pport->work_port_events &=
12075 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012076 spin_unlock_irqrestore(
12077 &phba->pport->work_port_lock,
12078 iflag);
James Smart09372822008-01-11 01:52:54 -050012079 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012080 }
James Smart97eab632008-04-07 10:16:05 -040012081 } else
James Smart5b75da22008-12-04 22:39:35 -050012082 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012083
James Smart92d7f7b2007-06-17 19:56:38 -050012084 if ((work_ha_copy & HA_MBATT) &&
12085 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012086send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012087 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012088 do {
12089 rc = lpfc_sli_issue_mbox(phba, NULL,
12090 MBX_NOWAIT);
12091 } while (rc == MBX_NOT_FINISHED);
12092 if (rc != MBX_SUCCESS)
12093 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12094 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012095 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012096 }
12097
James Smart5b75da22008-12-04 22:39:35 -050012098 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012099 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012100 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012101 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012102 }
James Smart93996272008-08-24 21:50:30 -040012103 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012104unplug_error:
12105 spin_unlock_irqrestore(&phba->hbalock, iflag);
12106 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012107
James Smart3772a992009-05-22 14:50:54 -040012108} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012109
12110/**
James Smart3772a992009-05-22 14:50:54 -040012111 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012112 * @irq: Interrupt number.
12113 * @dev_id: The device context pointer.
12114 *
12115 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012116 * service routine when device with SLI-3 interface spec is enabled with
12117 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12118 * ring event in the HBA. However, when the device is enabled with either
12119 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12120 * device-level interrupt handler. When the PCI slot is in error recovery
12121 * or the HBA is undergoing initialization, the interrupt handler will not
12122 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12123 * the intrrupt context. This function is called without any lock held.
12124 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012125 *
12126 * This function returns IRQ_HANDLED when interrupt is handled else it
12127 * returns IRQ_NONE.
12128 **/
12129irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012130lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012131{
12132 struct lpfc_hba *phba;
12133 uint32_t ha_copy;
12134 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012135 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012136 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012137
12138 /* Get the driver's phba structure from the dev_id and
12139 * assume the HBA is not interrupting.
12140 */
12141 phba = (struct lpfc_hba *) dev_id;
12142
12143 if (unlikely(!phba))
12144 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012145
12146 /*
James Smart93996272008-08-24 21:50:30 -040012147 * Stuff needs to be attented to when this function is invoked as an
12148 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012149 */
James Smart93996272008-08-24 21:50:30 -040012150 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012151 /* Check device state for handling interrupt */
12152 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012153 return IRQ_NONE;
12154 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012155 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12156 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012157 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012158 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012159 /*
12160 * If there is deferred error attention, do not check for
12161 * any interrupt.
12162 */
12163 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012164 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012165 return IRQ_NONE;
12166 }
James Smart93996272008-08-24 21:50:30 -040012167 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12168 phba->HAregaddr);
12169 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012170 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012171 } else
12172 ha_copy = phba->ha_copy;
12173
12174 /*
12175 * Process all events on FCP ring. Take the optimized path for FCP IO.
12176 */
12177 ha_copy &= ~(phba->work_ha_mask);
12178
12179 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012180 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012181 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012182 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012183 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012184
12185 if (phba->cfg_multi_ring_support == 2) {
12186 /*
James Smart93996272008-08-24 21:50:30 -040012187 * Process all events on extra ring. Take the optimized path
12188 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012189 */
James Smart93996272008-08-24 21:50:30 -040012190 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012191 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012192 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012193 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012194 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012195 status);
12196 }
12197 }
dea31012005-04-17 16:05:31 -050012198 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012199} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012200
James Smart93996272008-08-24 21:50:30 -040012201/**
James Smart3772a992009-05-22 14:50:54 -040012202 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012203 * @irq: Interrupt number.
12204 * @dev_id: The device context pointer.
12205 *
James Smart3772a992009-05-22 14:50:54 -040012206 * This function is the HBA device-level interrupt handler to device with
12207 * SLI-3 interface spec, called from the PCI layer when either MSI or
12208 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12209 * requires driver attention. This function invokes the slow-path interrupt
12210 * attention handling function and fast-path interrupt attention handling
12211 * function in turn to process the relevant HBA attention events. This
12212 * function is called without any lock held. It gets the hbalock to access
12213 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012214 *
12215 * This function returns IRQ_HANDLED when interrupt is handled, else it
12216 * returns IRQ_NONE.
12217 **/
12218irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012219lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012220{
12221 struct lpfc_hba *phba;
12222 irqreturn_t sp_irq_rc, fp_irq_rc;
12223 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012224 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012225
12226 /*
12227 * Get the driver's phba structure from the dev_id and
12228 * assume the HBA is not interrupting.
12229 */
12230 phba = (struct lpfc_hba *) dev_id;
12231
12232 if (unlikely(!phba))
12233 return IRQ_NONE;
12234
James Smart3772a992009-05-22 14:50:54 -040012235 /* Check device state for handling interrupt */
12236 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012237 return IRQ_NONE;
12238
12239 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012240 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12241 spin_unlock(&phba->hbalock);
12242 return IRQ_HANDLED;
12243 }
12244
James Smart93996272008-08-24 21:50:30 -040012245 if (unlikely(!phba->ha_copy)) {
12246 spin_unlock(&phba->hbalock);
12247 return IRQ_NONE;
12248 } else if (phba->ha_copy & HA_ERATT) {
12249 if (phba->hba_flag & HBA_ERATT_HANDLED)
12250 /* ERATT polling has handled ERATT */
12251 phba->ha_copy &= ~HA_ERATT;
12252 else
12253 /* Indicate interrupt handler handles ERATT */
12254 phba->hba_flag |= HBA_ERATT_HANDLED;
12255 }
12256
James Smarta257bf92009-04-06 18:48:10 -040012257 /*
12258 * If there is deferred error attention, do not check for any interrupt.
12259 */
12260 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012261 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012262 return IRQ_NONE;
12263 }
12264
James Smart93996272008-08-24 21:50:30 -040012265 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012266 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12267 spin_unlock(&phba->hbalock);
12268 return IRQ_HANDLED;
12269 }
James Smarta747c9c2009-11-18 15:41:10 -050012270 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12271 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12272 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012273 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012274 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012275 readl(phba->HAregaddr); /* flush */
12276 spin_unlock(&phba->hbalock);
12277
12278 /*
12279 * Invokes slow-path host attention interrupt handling as appropriate.
12280 */
12281
12282 /* status of events with mailbox and link attention */
12283 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12284
12285 /* status of events with ELS ring */
12286 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12287 status2 >>= (4*LPFC_ELS_RING);
12288
12289 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012290 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012291 else
12292 sp_irq_rc = IRQ_NONE;
12293
12294 /*
12295 * Invoke fast-path host attention interrupt handling as appropriate.
12296 */
12297
12298 /* status of events with FCP ring */
12299 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12300 status1 >>= (4*LPFC_FCP_RING);
12301
12302 /* status of events with extra ring */
12303 if (phba->cfg_multi_ring_support == 2) {
12304 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12305 status2 >>= (4*LPFC_EXTRA_RING);
12306 } else
12307 status2 = 0;
12308
12309 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012310 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012311 else
12312 fp_irq_rc = IRQ_NONE;
12313
12314 /* Return device-level interrupt handling status */
12315 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012316} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012317
12318/**
12319 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12320 * @phba: pointer to lpfc hba data structure.
12321 *
12322 * This routine is invoked by the worker thread to process all the pending
12323 * SLI4 FCP abort XRI events.
12324 **/
12325void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12326{
12327 struct lpfc_cq_event *cq_event;
12328
12329 /* First, declare the fcp xri abort event has been handled */
12330 spin_lock_irq(&phba->hbalock);
12331 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12332 spin_unlock_irq(&phba->hbalock);
12333 /* Now, handle all the fcp xri abort events */
12334 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12335 /* Get the first event from the head of the event queue */
12336 spin_lock_irq(&phba->hbalock);
12337 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12338 cq_event, struct lpfc_cq_event, list);
12339 spin_unlock_irq(&phba->hbalock);
12340 /* Notify aborted XRI for FCP work queue */
12341 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12342 /* Free the event processed back to the free pool */
12343 lpfc_sli4_cq_event_release(phba, cq_event);
12344 }
12345}
12346
12347/**
12348 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12349 * @phba: pointer to lpfc hba data structure.
12350 *
12351 * This routine is invoked by the worker thread to process all the pending
12352 * SLI4 els abort xri events.
12353 **/
12354void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12355{
12356 struct lpfc_cq_event *cq_event;
12357
12358 /* First, declare the els xri abort event has been handled */
12359 spin_lock_irq(&phba->hbalock);
12360 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12361 spin_unlock_irq(&phba->hbalock);
12362 /* Now, handle all the els xri abort events */
12363 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12364 /* Get the first event from the head of the event queue */
12365 spin_lock_irq(&phba->hbalock);
12366 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12367 cq_event, struct lpfc_cq_event, list);
12368 spin_unlock_irq(&phba->hbalock);
12369 /* Notify aborted XRI for ELS work queue */
12370 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12371 /* Free the event processed back to the free pool */
12372 lpfc_sli4_cq_event_release(phba, cq_event);
12373 }
12374}
12375
James Smart341af102010-01-26 23:07:37 -050012376/**
12377 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12378 * @phba: pointer to lpfc hba data structure
12379 * @pIocbIn: pointer to the rspiocbq
12380 * @pIocbOut: pointer to the cmdiocbq
12381 * @wcqe: pointer to the complete wcqe
12382 *
12383 * This routine transfers the fields of a command iocbq to a response iocbq
12384 * by copying all the IOCB fields from command iocbq and transferring the
12385 * completion status information from the complete wcqe.
12386 **/
James Smart4f774512009-05-22 14:52:35 -040012387static void
James Smart341af102010-01-26 23:07:37 -050012388lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12389 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012390 struct lpfc_iocbq *pIocbOut,
12391 struct lpfc_wcqe_complete *wcqe)
12392{
James Smartaf227412013-10-10 12:23:10 -040012393 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012394 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012395 uint32_t status, max_response;
12396 struct lpfc_dmabuf *dmabuf;
12397 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012398 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12399
12400 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12401 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012402 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012403 status = bf_get(lpfc_wcqe_c_status, wcqe);
12404 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012405 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12406 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12407 pIocbIn->iocb.un.fcpi.fcpi_parm =
12408 pIocbOut->iocb.un.fcpi.fcpi_parm -
12409 wcqe->total_data_placed;
12410 else
12411 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012412 else {
James Smart4f774512009-05-22 14:52:35 -040012413 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012414 switch (pIocbOut->iocb.ulpCommand) {
12415 case CMD_ELS_REQUEST64_CR:
12416 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12417 bpl = (struct ulp_bde64 *)dmabuf->virt;
12418 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12419 max_response = bde.tus.f.bdeSize;
12420 break;
12421 case CMD_GEN_REQUEST64_CR:
12422 max_response = 0;
12423 if (!pIocbOut->context3)
12424 break;
12425 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12426 sizeof(struct ulp_bde64);
12427 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12428 bpl = (struct ulp_bde64 *)dmabuf->virt;
12429 for (i = 0; i < numBdes; i++) {
12430 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12431 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12432 max_response += bde.tus.f.bdeSize;
12433 }
12434 break;
12435 default:
12436 max_response = wcqe->total_data_placed;
12437 break;
12438 }
12439 if (max_response < wcqe->total_data_placed)
12440 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12441 else
12442 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12443 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012444 }
James Smart341af102010-01-26 23:07:37 -050012445
James Smartacd68592012-01-18 16:25:09 -050012446 /* Convert BG errors for completion status */
12447 if (status == CQE_STATUS_DI_ERROR) {
12448 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12449
12450 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12451 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12452 else
12453 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12454
12455 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12456 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12457 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12458 BGS_GUARD_ERR_MASK;
12459 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12460 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12461 BGS_APPTAG_ERR_MASK;
12462 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12463 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12464 BGS_REFTAG_ERR_MASK;
12465
12466 /* Check to see if there was any good data before the error */
12467 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12468 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12469 BGS_HI_WATER_MARK_PRESENT_MASK;
12470 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12471 wcqe->total_data_placed;
12472 }
12473
12474 /*
12475 * Set ALL the error bits to indicate we don't know what
12476 * type of error it is.
12477 */
12478 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12479 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12480 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12481 BGS_GUARD_ERR_MASK);
12482 }
12483
James Smart341af102010-01-26 23:07:37 -050012484 /* Pick up HBA exchange busy condition */
12485 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12486 spin_lock_irqsave(&phba->hbalock, iflags);
12487 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12488 spin_unlock_irqrestore(&phba->hbalock, iflags);
12489 }
James Smart4f774512009-05-22 14:52:35 -040012490}
12491
12492/**
James Smart45ed1192009-10-02 15:17:02 -040012493 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12494 * @phba: Pointer to HBA context object.
12495 * @wcqe: Pointer to work-queue completion queue entry.
12496 *
12497 * This routine handles an ELS work-queue completion event and construct
12498 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12499 * discovery engine to handle.
12500 *
12501 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12502 **/
12503static struct lpfc_iocbq *
12504lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12505 struct lpfc_iocbq *irspiocbq)
12506{
James Smart895427b2017-02-12 13:52:30 -080012507 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012508 struct lpfc_iocbq *cmdiocbq;
12509 struct lpfc_wcqe_complete *wcqe;
12510 unsigned long iflags;
12511
James Smart895427b2017-02-12 13:52:30 -080012512 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012513 if (unlikely(!pring))
12514 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012515
James Smart45ed1192009-10-02 15:17:02 -040012516 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012517 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012518 pring->stats.iocb_event++;
12519 /* Look up the ELS command IOCB and create pseudo response IOCB */
12520 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12521 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012522 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012523 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012524 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12525 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012526 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12527 wcqe->word0, wcqe->total_data_placed,
12528 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012529 lpfc_sli_release_iocbq(phba, irspiocbq);
12530 return NULL;
12531 }
12532
Dick Kennedy401bb412017-09-29 17:34:28 -070012533 /* Put the iocb back on the txcmplq */
12534 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12535 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12536
James Smart45ed1192009-10-02 15:17:02 -040012537 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012538 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012539
12540 return irspiocbq;
12541}
12542
James Smart8a5ca102017-11-20 16:00:30 -080012543inline struct lpfc_cq_event *
12544lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12545{
12546 struct lpfc_cq_event *cq_event;
12547
12548 /* Allocate a new internal CQ_EVENT entry */
12549 cq_event = lpfc_sli4_cq_event_alloc(phba);
12550 if (!cq_event) {
12551 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12552 "0602 Failed to alloc CQ_EVENT entry\n");
12553 return NULL;
12554 }
12555
12556 /* Move the CQE into the event */
12557 memcpy(&cq_event->cqe, entry, size);
12558 return cq_event;
12559}
12560
James Smart45ed1192009-10-02 15:17:02 -040012561/**
James Smart04c68492009-05-22 14:52:52 -040012562 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12563 * @phba: Pointer to HBA context object.
12564 * @cqe: Pointer to mailbox completion queue entry.
12565 *
12566 * This routine process a mailbox completion queue entry with asynchrous
12567 * event.
12568 *
12569 * Return: true if work posted to worker thread, otherwise false.
12570 **/
12571static bool
12572lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12573{
12574 struct lpfc_cq_event *cq_event;
12575 unsigned long iflags;
12576
12577 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12578 "0392 Async Event: word0:x%x, word1:x%x, "
12579 "word2:x%x, word3:x%x\n", mcqe->word0,
12580 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12581
James Smart8a5ca102017-11-20 16:00:30 -080012582 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12583 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012584 return false;
James Smart04c68492009-05-22 14:52:52 -040012585 spin_lock_irqsave(&phba->hbalock, iflags);
12586 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12587 /* Set the async event flag */
12588 phba->hba_flag |= ASYNC_EVENT;
12589 spin_unlock_irqrestore(&phba->hbalock, iflags);
12590
12591 return true;
12592}
12593
12594/**
12595 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12596 * @phba: Pointer to HBA context object.
12597 * @cqe: Pointer to mailbox completion queue entry.
12598 *
12599 * This routine process a mailbox completion queue entry with mailbox
12600 * completion event.
12601 *
12602 * Return: true if work posted to worker thread, otherwise false.
12603 **/
12604static bool
12605lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12606{
12607 uint32_t mcqe_status;
12608 MAILBOX_t *mbox, *pmbox;
12609 struct lpfc_mqe *mqe;
12610 struct lpfc_vport *vport;
12611 struct lpfc_nodelist *ndlp;
12612 struct lpfc_dmabuf *mp;
12613 unsigned long iflags;
12614 LPFC_MBOXQ_t *pmb;
12615 bool workposted = false;
12616 int rc;
12617
12618 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12619 if (!bf_get(lpfc_trailer_completed, mcqe))
12620 goto out_no_mqe_complete;
12621
12622 /* Get the reference to the active mbox command */
12623 spin_lock_irqsave(&phba->hbalock, iflags);
12624 pmb = phba->sli.mbox_active;
12625 if (unlikely(!pmb)) {
12626 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12627 "1832 No pending MBOX command to handle\n");
12628 spin_unlock_irqrestore(&phba->hbalock, iflags);
12629 goto out_no_mqe_complete;
12630 }
12631 spin_unlock_irqrestore(&phba->hbalock, iflags);
12632 mqe = &pmb->u.mqe;
12633 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12634 mbox = phba->mbox;
12635 vport = pmb->vport;
12636
12637 /* Reset heartbeat timer */
12638 phba->last_completion_time = jiffies;
12639 del_timer(&phba->sli.mbox_tmo);
12640
12641 /* Move mbox data to caller's mailbox region, do endian swapping */
12642 if (pmb->mbox_cmpl && mbox)
12643 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012644
James Smart73d91e52011-10-10 21:32:10 -040012645 /*
12646 * For mcqe errors, conditionally move a modified error code to
12647 * the mbox so that the error will not be missed.
12648 */
12649 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12650 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12651 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12652 bf_set(lpfc_mqe_status, mqe,
12653 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12654 }
James Smart04c68492009-05-22 14:52:52 -040012655 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12656 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12657 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12658 "MBOX dflt rpi: status:x%x rpi:x%x",
12659 mcqe_status,
12660 pmbox->un.varWords[0], 0);
12661 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12662 mp = (struct lpfc_dmabuf *)(pmb->context1);
12663 ndlp = (struct lpfc_nodelist *)pmb->context2;
12664 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12665 * RID of the PPI using the same mbox buffer.
12666 */
12667 lpfc_unreg_login(phba, vport->vpi,
12668 pmbox->un.varWords[0], pmb);
12669 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12670 pmb->context1 = mp;
12671 pmb->context2 = ndlp;
12672 pmb->vport = vport;
12673 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12674 if (rc != MBX_BUSY)
12675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12676 LOG_SLI, "0385 rc should "
12677 "have been MBX_BUSY\n");
12678 if (rc != MBX_NOT_FINISHED)
12679 goto send_current_mbox;
12680 }
12681 }
12682 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12683 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12684 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12685
12686 /* There is mailbox completion work to do */
12687 spin_lock_irqsave(&phba->hbalock, iflags);
12688 __lpfc_mbox_cmpl_put(phba, pmb);
12689 phba->work_ha |= HA_MBATT;
12690 spin_unlock_irqrestore(&phba->hbalock, iflags);
12691 workposted = true;
12692
12693send_current_mbox:
12694 spin_lock_irqsave(&phba->hbalock, iflags);
12695 /* Release the mailbox command posting token */
12696 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12697 /* Setting active mailbox pointer need to be in sync to flag clear */
12698 phba->sli.mbox_active = NULL;
12699 spin_unlock_irqrestore(&phba->hbalock, iflags);
12700 /* Wake up worker thread to post the next pending mailbox command */
12701 lpfc_worker_wake_up(phba);
12702out_no_mqe_complete:
12703 if (bf_get(lpfc_trailer_consumed, mcqe))
12704 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12705 return workposted;
12706}
12707
12708/**
12709 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12710 * @phba: Pointer to HBA context object.
12711 * @cqe: Pointer to mailbox completion queue entry.
12712 *
12713 * This routine process a mailbox completion queue entry, it invokes the
12714 * proper mailbox complete handling or asynchrous event handling routine
12715 * according to the MCQE's async bit.
12716 *
12717 * Return: true if work posted to worker thread, otherwise false.
12718 **/
12719static bool
12720lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12721{
12722 struct lpfc_mcqe mcqe;
12723 bool workposted;
12724
12725 /* Copy the mailbox MCQE and convert endian order as needed */
12726 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12727
12728 /* Invoke the proper event handling routine */
12729 if (!bf_get(lpfc_trailer_async, &mcqe))
12730 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12731 else
12732 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12733 return workposted;
12734}
12735
12736/**
James Smart4f774512009-05-22 14:52:35 -040012737 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12738 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012739 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012740 * @wcqe: Pointer to work-queue completion queue entry.
12741 *
12742 * This routine handles an ELS work-queue completion event.
12743 *
12744 * Return: true if work posted to worker thread, otherwise false.
12745 **/
12746static bool
James Smart2a76a282012-08-03 12:35:54 -040012747lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012748 struct lpfc_wcqe_complete *wcqe)
12749{
James Smart4f774512009-05-22 14:52:35 -040012750 struct lpfc_iocbq *irspiocbq;
12751 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012752 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012753 int txq_cnt = 0;
12754 int txcmplq_cnt = 0;
12755 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012756
James Smart45ed1192009-10-02 15:17:02 -040012757 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012758 irspiocbq = lpfc_sli_get_iocbq(phba);
12759 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012760 if (!list_empty(&pring->txq))
12761 txq_cnt++;
12762 if (!list_empty(&pring->txcmplq))
12763 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012765 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12766 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012767 txq_cnt, phba->iocb_cnt,
12768 fcp_txcmplq_cnt,
12769 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012770 return false;
James Smart4f774512009-05-22 14:52:35 -040012771 }
James Smart4f774512009-05-22 14:52:35 -040012772
James Smart45ed1192009-10-02 15:17:02 -040012773 /* Save off the slow-path queue event for work thread to process */
12774 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012775 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012776 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012777 &phba->sli4_hba.sp_queue_event);
12778 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012779 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012780
James Smart45ed1192009-10-02 15:17:02 -040012781 return true;
James Smart4f774512009-05-22 14:52:35 -040012782}
12783
12784/**
12785 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12786 * @phba: Pointer to HBA context object.
12787 * @wcqe: Pointer to work-queue completion queue entry.
12788 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012789 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012790 * proper WQ release routine to the slow-path WQ.
12791 **/
12792static void
12793lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12794 struct lpfc_wcqe_release *wcqe)
12795{
James Smart2e90f4b2011-12-13 13:22:37 -050012796 /* sanity check on queue memory */
12797 if (unlikely(!phba->sli4_hba.els_wq))
12798 return;
James Smart4f774512009-05-22 14:52:35 -040012799 /* Check for the slow-path ELS work queue */
12800 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12801 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12802 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12803 else
12804 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12805 "2579 Slow-path wqe consume event carries "
12806 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12807 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12808 phba->sli4_hba.els_wq->queue_id);
12809}
12810
12811/**
12812 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12813 * @phba: Pointer to HBA context object.
12814 * @cq: Pointer to a WQ completion queue.
12815 * @wcqe: Pointer to work-queue completion queue entry.
12816 *
12817 * This routine handles an XRI abort event.
12818 *
12819 * Return: true if work posted to worker thread, otherwise false.
12820 **/
12821static bool
12822lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12823 struct lpfc_queue *cq,
12824 struct sli4_wcqe_xri_aborted *wcqe)
12825{
12826 bool workposted = false;
12827 struct lpfc_cq_event *cq_event;
12828 unsigned long iflags;
12829
James Smart4f774512009-05-22 14:52:35 -040012830 switch (cq->subtype) {
12831 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080012832 cq_event = lpfc_cq_event_setup(
12833 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12834 if (!cq_event)
12835 return false;
James Smart4f774512009-05-22 14:52:35 -040012836 spin_lock_irqsave(&phba->hbalock, iflags);
12837 list_add_tail(&cq_event->list,
12838 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12839 /* Set the fcp xri abort event flag */
12840 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12841 spin_unlock_irqrestore(&phba->hbalock, iflags);
12842 workposted = true;
12843 break;
James Smart422c4cb2017-11-20 16:00:32 -080012844 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040012845 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080012846 cq_event = lpfc_cq_event_setup(
12847 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12848 if (!cq_event)
12849 return false;
James Smart4f774512009-05-22 14:52:35 -040012850 spin_lock_irqsave(&phba->hbalock, iflags);
12851 list_add_tail(&cq_event->list,
12852 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12853 /* Set the els xri abort event flag */
12854 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12855 spin_unlock_irqrestore(&phba->hbalock, iflags);
12856 workposted = true;
12857 break;
James Smart318083a2017-03-04 09:30:30 -080012858 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080012859 /* Notify aborted XRI for NVME work queue */
12860 if (phba->nvmet_support)
12861 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12862 else
12863 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12864
12865 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080012866 break;
James Smart4f774512009-05-22 14:52:35 -040012867 default:
12868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012869 "0603 Invalid CQ subtype %d: "
12870 "%08x %08x %08x %08x\n",
12871 cq->subtype, wcqe->word0, wcqe->parameter,
12872 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040012873 workposted = false;
12874 break;
12875 }
12876 return workposted;
12877}
12878
12879/**
James Smart4d9ab992009-10-02 15:16:39 -040012880 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012881 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012882 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012883 *
James Smart4d9ab992009-10-02 15:16:39 -040012884 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012885 *
12886 * Return: true if work posted to worker thread, otherwise false.
12887 **/
12888static bool
James Smart4d9ab992009-10-02 15:16:39 -040012889lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12890{
12891 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012892 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012893 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12894 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012895 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012896 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012897 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012898 unsigned long iflags;
12899
James Smart2e90f4b2011-12-13 13:22:37 -050012900 /* sanity check on queue memory */
12901 if (unlikely(!hrq) || unlikely(!drq))
12902 return workposted;
12903
James Smart7851fe22011-07-22 18:36:52 -040012904 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12905 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12906 else
12907 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12908 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012909 goto out;
12910
12911 status = bf_get(lpfc_rcqe_status, rcqe);
12912 switch (status) {
12913 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12914 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12915 "2537 Receive Frame Truncated!!\n");
12916 case FC_STATUS_RQ_SUCCESS:
12917 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080012918 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012919 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12920 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012921 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012922 spin_unlock_irqrestore(&phba->hbalock, iflags);
12923 goto out;
12924 }
James Smartb84daac2012-08-03 12:35:13 -040012925 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012926 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012927 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012928
12929 /* If a NVME LS event (type 0x28), treat it as Fast path */
12930 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12931
James Smart4d9ab992009-10-02 15:16:39 -040012932 /* save off the frame for the word thread to process */
12933 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012934 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012935 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012936 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012937 spin_unlock_irqrestore(&phba->hbalock, iflags);
12938 workposted = true;
12939 break;
James Smart4d9ab992009-10-02 15:16:39 -040012940 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012941 if (phba->nvmet_support) {
12942 tgtp = phba->targetport->private;
12943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12944 "6402 RQE Error x%x, posted %d err_cnt "
12945 "%d: %x %x %x\n",
12946 status, hrq->RQ_buf_posted,
12947 hrq->RQ_no_posted_buf,
12948 atomic_read(&tgtp->rcv_fcp_cmd_in),
12949 atomic_read(&tgtp->rcv_fcp_cmd_out),
12950 atomic_read(&tgtp->xmt_fcp_release));
12951 }
12952 /* fallthrough */
12953
12954 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012955 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012956 /* Post more buffers if possible */
12957 spin_lock_irqsave(&phba->hbalock, iflags);
12958 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12959 spin_unlock_irqrestore(&phba->hbalock, iflags);
12960 workposted = true;
12961 break;
12962 }
12963out:
12964 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012965}
12966
12967/**
12968 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12969 * @phba: Pointer to HBA context object.
12970 * @cq: Pointer to the completion queue.
12971 * @wcqe: Pointer to a completion queue entry.
12972 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012973 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012974 * entry.
12975 *
12976 * Return: true if work posted to worker thread, otherwise false.
12977 **/
12978static bool
12979lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012980 struct lpfc_cqe *cqe)
12981{
James Smart45ed1192009-10-02 15:17:02 -040012982 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012983 bool workposted = false;
12984
12985 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012986 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012987
12988 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012989 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012990 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012991 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012992 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012993 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012994 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012995 break;
12996 case CQE_CODE_RELEASE_WQE:
12997 /* Process the WQ release event */
12998 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012999 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013000 break;
13001 case CQE_CODE_XRI_ABORTED:
13002 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013003 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013004 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013005 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013006 break;
James Smart4d9ab992009-10-02 15:16:39 -040013007 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013008 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013009 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013010 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013011 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013012 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013013 break;
James Smart4f774512009-05-22 14:52:35 -040013014 default:
13015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13016 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013017 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013018 break;
13019 }
13020 return workposted;
13021}
13022
13023/**
James Smart4f774512009-05-22 14:52:35 -040013024 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13025 * @phba: Pointer to HBA context object.
13026 * @eqe: Pointer to fast-path event queue entry.
13027 *
13028 * This routine process a event queue entry from the slow-path event queue.
13029 * It will check the MajorCode and MinorCode to determine this is for a
13030 * completion event on a completion queue, if not, an error shall be logged
13031 * and just return. Otherwise, it will get to the corresponding completion
13032 * queue and process all the entries on that completion queue, rearm the
13033 * completion queue, and then return.
13034 *
13035 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013036static void
James Smart67d12732012-08-03 12:36:13 -040013037lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13038 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013039{
James Smart67d12732012-08-03 12:36:13 -040013040 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013041 uint16_t cqid;
13042
James Smart4f774512009-05-22 14:52:35 -040013043 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013044 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013045
James Smart4f774512009-05-22 14:52:35 -040013046 list_for_each_entry(childq, &speq->child_list, list) {
13047 if (childq->queue_id == cqid) {
13048 cq = childq;
13049 break;
13050 }
13051 }
13052 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013053 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13055 "0365 Slow-path CQ identifier "
13056 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013057 return;
James Smart4f774512009-05-22 14:52:35 -040013058 }
13059
James Smart895427b2017-02-12 13:52:30 -080013060 /* Save EQ associated with this CQ */
13061 cq->assoc_qp = speq;
13062
Dick Kennedyf485c182017-09-29 17:34:34 -070013063 if (!queue_work(phba->wq, &cq->spwork))
13064 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13065 "0390 Cannot schedule soft IRQ "
13066 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13067 cqid, cq->queue_id, smp_processor_id());
13068}
13069
13070/**
13071 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13072 * @phba: Pointer to HBA context object.
13073 *
13074 * This routine process a event queue entry from the slow-path event queue.
13075 * It will check the MajorCode and MinorCode to determine this is for a
13076 * completion event on a completion queue, if not, an error shall be logged
13077 * and just return. Otherwise, it will get to the corresponding completion
13078 * queue and process all the entries on that completion queue, rearm the
13079 * completion queue, and then return.
13080 *
13081 **/
13082static void
13083lpfc_sli4_sp_process_cq(struct work_struct *work)
13084{
13085 struct lpfc_queue *cq =
13086 container_of(work, struct lpfc_queue, spwork);
13087 struct lpfc_hba *phba = cq->phba;
13088 struct lpfc_cqe *cqe;
13089 bool workposted = false;
13090 int ccount = 0;
13091
James Smart4f774512009-05-22 14:52:35 -040013092 /* Process all the entries to the CQ */
13093 switch (cq->type) {
13094 case LPFC_MCQ:
13095 while ((cqe = lpfc_sli4_cq_get(cq))) {
13096 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013097 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013098 break;
James Smartb84daac2012-08-03 12:35:13 -040013099 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013100 }
13101 break;
13102 case LPFC_WCQ:
13103 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013104 if (cq->subtype == LPFC_FCP ||
13105 cq->subtype == LPFC_NVME) {
13106#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13107 if (phba->ktime_on)
13108 cq->isr_timestamp = ktime_get_ns();
13109 else
13110 cq->isr_timestamp = 0;
13111#endif
James Smart895427b2017-02-12 13:52:30 -080013112 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013113 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013114 } else {
James Smart05580562011-05-24 11:40:48 -040013115 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13116 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013117 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013118 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013119 break;
James Smart4f774512009-05-22 14:52:35 -040013120 }
James Smartb84daac2012-08-03 12:35:13 -040013121
13122 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013123 if (ccount > cq->CQ_max_cqe)
13124 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013125 break;
13126 default:
13127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13128 "0370 Invalid completion queue type (%d)\n",
13129 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013130 return;
James Smart4f774512009-05-22 14:52:35 -040013131 }
13132
13133 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013134 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013135 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13136 "0371 No entry from the CQ: identifier "
13137 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13138
13139 /* In any case, flash and re-arm the RCQ */
13140 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13141
13142 /* wake up worker thread if there are works to be done */
13143 if (workposted)
13144 lpfc_worker_wake_up(phba);
13145}
13146
13147/**
13148 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013149 * @phba: Pointer to HBA context object.
13150 * @cq: Pointer to associated CQ
13151 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013152 *
13153 * This routine process a fast-path work queue completion entry from fast-path
13154 * event queue for FCP command response completion.
13155 **/
13156static void
James Smart2a76a282012-08-03 12:35:54 -040013157lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013158 struct lpfc_wcqe_complete *wcqe)
13159{
James Smart2a76a282012-08-03 12:35:54 -040013160 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013161 struct lpfc_iocbq *cmdiocbq;
13162 struct lpfc_iocbq irspiocbq;
13163 unsigned long iflags;
13164
James Smart4f774512009-05-22 14:52:35 -040013165 /* Check for response status */
13166 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13167 /* If resource errors reported from HBA, reduce queue
13168 * depth of the SCSI device.
13169 */
James Smarte3d2b802012-08-14 14:25:43 -040013170 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13171 IOSTAT_LOCAL_REJECT)) &&
13172 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13173 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013174 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013175
James Smart4f774512009-05-22 14:52:35 -040013176 /* Log the error status */
13177 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13178 "0373 FCP complete error: status=x%x, "
13179 "hw_status=x%x, total_data_specified=%d, "
13180 "parameter=x%x, word3=x%x\n",
13181 bf_get(lpfc_wcqe_c_status, wcqe),
13182 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13183 wcqe->total_data_placed, wcqe->parameter,
13184 wcqe->word3);
13185 }
13186
13187 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013188 spin_lock_irqsave(&pring->ring_lock, iflags);
13189 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013190 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13191 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013192 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013193 if (unlikely(!cmdiocbq)) {
13194 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13195 "0374 FCP complete with no corresponding "
13196 "cmdiocb: iotag (%d)\n",
13197 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13198 return;
13199 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013200#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13201 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13202#endif
James Smart895427b2017-02-12 13:52:30 -080013203 if (cmdiocbq->iocb_cmpl == NULL) {
13204 if (cmdiocbq->wqe_cmpl) {
13205 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13206 spin_lock_irqsave(&phba->hbalock, iflags);
13207 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13208 spin_unlock_irqrestore(&phba->hbalock, iflags);
13209 }
13210
13211 /* Pass the cmd_iocb and the wcqe to the upper layer */
13212 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13213 return;
13214 }
James Smart4f774512009-05-22 14:52:35 -040013215 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13216 "0375 FCP cmdiocb not callback function "
13217 "iotag: (%d)\n",
13218 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13219 return;
13220 }
13221
13222 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013223 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013224
James Smart0f65ff62010-02-26 14:14:23 -050013225 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13226 spin_lock_irqsave(&phba->hbalock, iflags);
13227 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13228 spin_unlock_irqrestore(&phba->hbalock, iflags);
13229 }
13230
James Smart4f774512009-05-22 14:52:35 -040013231 /* Pass the cmd_iocb and the rsp state to the upper layer */
13232 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13233}
13234
13235/**
13236 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13237 * @phba: Pointer to HBA context object.
13238 * @cq: Pointer to completion queue.
13239 * @wcqe: Pointer to work-queue completion queue entry.
13240 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013241 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013242 * proper WQ release routine to the slow-path WQ.
13243 **/
13244static void
13245lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13246 struct lpfc_wcqe_release *wcqe)
13247{
13248 struct lpfc_queue *childwq;
13249 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013250 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013251
13252 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013253 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013254 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013255 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013256 lpfc_sli4_wq_release(childwq,
13257 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013258 if (childwq->q_flag & HBA_NVMET_WQFULL)
13259 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013260 wqid_matched = true;
13261 break;
13262 }
13263 }
13264 /* Report warning log message if no match found */
13265 if (wqid_matched != true)
13266 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13267 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013268 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013269}
13270
13271/**
James Smart2d7dbc42017-02-12 13:52:35 -080013272 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13273 * @phba: Pointer to HBA context object.
13274 * @rcqe: Pointer to receive-queue completion queue entry.
13275 *
13276 * This routine process a receive-queue completion queue entry.
13277 *
13278 * Return: true if work posted to worker thread, otherwise false.
13279 **/
13280static bool
13281lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13282 struct lpfc_rcqe *rcqe)
13283{
13284 bool workposted = false;
13285 struct lpfc_queue *hrq;
13286 struct lpfc_queue *drq;
13287 struct rqb_dmabuf *dma_buf;
13288 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013289 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013290 uint32_t status, rq_id;
13291 unsigned long iflags;
13292 uint32_t fctl, idx;
13293
13294 if ((phba->nvmet_support == 0) ||
13295 (phba->sli4_hba.nvmet_cqset == NULL))
13296 return workposted;
13297
13298 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13299 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13300 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13301
13302 /* sanity check on queue memory */
13303 if (unlikely(!hrq) || unlikely(!drq))
13304 return workposted;
13305
13306 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13307 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13308 else
13309 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13310
13311 if ((phba->nvmet_support == 0) ||
13312 (rq_id != hrq->queue_id))
13313 return workposted;
13314
13315 status = bf_get(lpfc_rcqe_status, rcqe);
13316 switch (status) {
13317 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13319 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013320 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013321 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013322 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013323 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013324 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13325 if (!dma_buf) {
13326 hrq->RQ_no_buf_found++;
13327 spin_unlock_irqrestore(&phba->hbalock, iflags);
13328 goto out;
13329 }
13330 spin_unlock_irqrestore(&phba->hbalock, iflags);
13331 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013332 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013333 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13334
13335 /* Just some basic sanity checks on FCP Command frame */
13336 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13337 fc_hdr->fh_f_ctl[1] << 8 |
13338 fc_hdr->fh_f_ctl[2]);
13339 if (((fctl &
13340 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13341 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13342 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13343 goto drop;
13344
13345 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13346 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013347 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013348 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013349 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013350 return false;
13351 }
13352drop:
13353 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13354 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013355 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013356 if (phba->nvmet_support) {
13357 tgtp = phba->targetport->private;
13358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13359 "6401 RQE Error x%x, posted %d err_cnt "
13360 "%d: %x %x %x\n",
13361 status, hrq->RQ_buf_posted,
13362 hrq->RQ_no_posted_buf,
13363 atomic_read(&tgtp->rcv_fcp_cmd_in),
13364 atomic_read(&tgtp->rcv_fcp_cmd_out),
13365 atomic_read(&tgtp->xmt_fcp_release));
13366 }
13367 /* fallthrough */
13368
13369 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013370 hrq->RQ_no_posted_buf++;
13371 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013372 break;
13373 }
13374out:
13375 return workposted;
13376}
13377
13378/**
James Smart895427b2017-02-12 13:52:30 -080013379 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013380 * @cq: Pointer to the completion queue.
13381 * @eqe: Pointer to fast-path completion queue entry.
13382 *
13383 * This routine process a fast-path work queue completion entry from fast-path
13384 * event queue for FCP command response completion.
13385 **/
13386static int
James Smart895427b2017-02-12 13:52:30 -080013387lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013388 struct lpfc_cqe *cqe)
13389{
13390 struct lpfc_wcqe_release wcqe;
13391 bool workposted = false;
13392
13393 /* Copy the work queue CQE and convert endian order if needed */
13394 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13395
13396 /* Check and process for different type of WCQE and dispatch */
13397 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13398 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013399 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013400 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013401 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013402 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013403 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13404 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13405 (struct lpfc_wcqe_complete *)&wcqe);
13406 if (cq->subtype == LPFC_NVME_LS)
13407 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013408 (struct lpfc_wcqe_complete *)&wcqe);
13409 break;
13410 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013411 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013412 /* Process the WQ release event */
13413 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13414 (struct lpfc_wcqe_release *)&wcqe);
13415 break;
13416 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013417 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013418 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013419 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013420 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13421 (struct sli4_wcqe_xri_aborted *)&wcqe);
13422 break;
James Smart895427b2017-02-12 13:52:30 -080013423 case CQE_CODE_RECEIVE_V1:
13424 case CQE_CODE_RECEIVE:
13425 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013426 if (cq->subtype == LPFC_NVMET) {
13427 workposted = lpfc_sli4_nvmet_handle_rcqe(
13428 phba, cq, (struct lpfc_rcqe *)&wcqe);
13429 }
James Smart895427b2017-02-12 13:52:30 -080013430 break;
James Smart4f774512009-05-22 14:52:35 -040013431 default:
13432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013433 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013434 bf_get(lpfc_wcqe_c_code, &wcqe));
13435 break;
13436 }
13437 return workposted;
13438}
13439
13440/**
James Smart67d12732012-08-03 12:36:13 -040013441 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013442 * @phba: Pointer to HBA context object.
13443 * @eqe: Pointer to fast-path event queue entry.
13444 *
13445 * This routine process a event queue entry from the fast-path event queue.
13446 * It will check the MajorCode and MinorCode to determine this is for a
13447 * completion event on a completion queue, if not, an error shall be logged
13448 * and just return. Otherwise, it will get to the corresponding completion
13449 * queue and process all the entries on the completion queue, rearm the
13450 * completion queue, and then return.
13451 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013452static void
James Smart67d12732012-08-03 12:36:13 -040013453lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13454 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013455{
James Smart895427b2017-02-12 13:52:30 -080013456 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013457 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013458
James Smartcb5172e2010-03-15 11:25:07 -040013459 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013461 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013462 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013463 bf_get_le32(lpfc_eqe_major_code, eqe),
13464 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013465 return;
James Smart4f774512009-05-22 14:52:35 -040013466 }
13467
James Smart67d12732012-08-03 12:36:13 -040013468 /* Get the reference to the corresponding CQ */
13469 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13470
James Smart2d7dbc42017-02-12 13:52:35 -080013471 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13472 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13473 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13474 /* Process NVMET unsol rcv */
13475 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13476 goto process_cq;
13477 }
13478 }
13479
James Smart895427b2017-02-12 13:52:30 -080013480 if (phba->sli4_hba.nvme_cq_map &&
13481 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013482 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013483 cq = phba->sli4_hba.nvme_cq[qidx];
13484 goto process_cq;
13485 }
13486
13487 if (phba->sli4_hba.fcp_cq_map &&
13488 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13489 /* Process FCP command completion */
13490 cq = phba->sli4_hba.fcp_cq[qidx];
13491 goto process_cq;
13492 }
13493
13494 if (phba->sli4_hba.nvmels_cq &&
13495 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13496 /* Process NVME unsol rcv */
13497 cq = phba->sli4_hba.nvmels_cq;
13498 }
13499
13500 /* Otherwise this is a Slow path event */
13501 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013502 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13503 return;
James Smart67d12732012-08-03 12:36:13 -040013504 }
13505
James Smart895427b2017-02-12 13:52:30 -080013506process_cq:
James Smart4f774512009-05-22 14:52:35 -040013507 if (unlikely(cqid != cq->queue_id)) {
13508 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13509 "0368 Miss-matched fast-path completion "
13510 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13511 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013512 return;
James Smart4f774512009-05-22 14:52:35 -040013513 }
13514
James Smart895427b2017-02-12 13:52:30 -080013515 /* Save EQ associated with this CQ */
13516 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13517
Dick Kennedyf485c182017-09-29 17:34:34 -070013518 if (!queue_work(phba->wq, &cq->irqwork))
13519 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13520 "0363 Cannot schedule soft IRQ "
13521 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13522 cqid, cq->queue_id, smp_processor_id());
13523}
13524
13525/**
13526 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13527 * @phba: Pointer to HBA context object.
13528 * @eqe: Pointer to fast-path event queue entry.
13529 *
13530 * This routine process a event queue entry from the fast-path event queue.
13531 * It will check the MajorCode and MinorCode to determine this is for a
13532 * completion event on a completion queue, if not, an error shall be logged
13533 * and just return. Otherwise, it will get to the corresponding completion
13534 * queue and process all the entries on the completion queue, rearm the
13535 * completion queue, and then return.
13536 **/
13537static void
13538lpfc_sli4_hba_process_cq(struct work_struct *work)
13539{
13540 struct lpfc_queue *cq =
13541 container_of(work, struct lpfc_queue, irqwork);
13542 struct lpfc_hba *phba = cq->phba;
13543 struct lpfc_cqe *cqe;
13544 bool workposted = false;
13545 int ccount = 0;
13546
James Smart4f774512009-05-22 14:52:35 -040013547 /* Process all the entries to the CQ */
13548 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013549#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13550 if (phba->ktime_on)
13551 cq->isr_timestamp = ktime_get_ns();
13552 else
13553 cq->isr_timestamp = 0;
13554#endif
James Smart895427b2017-02-12 13:52:30 -080013555 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013556 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013557 break;
James Smart4f774512009-05-22 14:52:35 -040013558 }
13559
James Smartb84daac2012-08-03 12:35:13 -040013560 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013561 if (ccount > cq->CQ_max_cqe)
13562 cq->CQ_max_cqe = ccount;
13563 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013564
James Smart4f774512009-05-22 14:52:35 -040013565 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013566 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013567 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13568 "0369 No entry from fast-path completion "
13569 "queue fcpcqid=%d\n", cq->queue_id);
13570
13571 /* In any case, flash and re-arm the CQ */
13572 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13573
13574 /* wake up worker thread if there are works to be done */
13575 if (workposted)
13576 lpfc_worker_wake_up(phba);
13577}
13578
13579static void
13580lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13581{
13582 struct lpfc_eqe *eqe;
13583
13584 /* walk all the EQ entries and drop on the floor */
13585 while ((eqe = lpfc_sli4_eq_get(eq)))
13586 ;
13587
13588 /* Clear and re-arm the EQ */
13589 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13590}
13591
James Smart1ba981f2014-02-20 09:56:45 -050013592
13593/**
13594 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13595 * entry
13596 * @phba: Pointer to HBA context object.
13597 * @eqe: Pointer to fast-path event queue entry.
13598 *
13599 * This routine process a event queue entry from the Flash Optimized Fabric
13600 * event queue. It will check the MajorCode and MinorCode to determine this
13601 * is for a completion event on a completion queue, if not, an error shall be
13602 * logged and just return. Otherwise, it will get to the corresponding
13603 * completion queue and process all the entries on the completion queue, rearm
13604 * the completion queue, and then return.
13605 **/
13606static void
13607lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13608{
13609 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013610 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013611
13612 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13614 "9147 Not a valid completion "
13615 "event: majorcode=x%x, minorcode=x%x\n",
13616 bf_get_le32(lpfc_eqe_major_code, eqe),
13617 bf_get_le32(lpfc_eqe_minor_code, eqe));
13618 return;
13619 }
13620
13621 /* Get the reference to the corresponding CQ */
13622 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13623
13624 /* Next check for OAS */
13625 cq = phba->sli4_hba.oas_cq;
13626 if (unlikely(!cq)) {
13627 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13629 "9148 OAS completion queue "
13630 "does not exist\n");
13631 return;
13632 }
13633
13634 if (unlikely(cqid != cq->queue_id)) {
13635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13636 "9149 Miss-matched fast-path compl "
13637 "queue id: eqcqid=%d, fcpcqid=%d\n",
13638 cqid, cq->queue_id);
13639 return;
13640 }
13641
James Smartd41b65b2017-06-15 22:56:41 -070013642 /* Save EQ associated with this CQ */
13643 cq->assoc_qp = phba->sli4_hba.fof_eq;
13644
Dick Kennedyf485c182017-09-29 17:34:34 -070013645 /* CQ work will be processed on CPU affinitized to this IRQ */
13646 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013648 "0367 Cannot schedule soft IRQ "
13649 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13650 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013651}
13652
13653/**
13654 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13655 * @irq: Interrupt number.
13656 * @dev_id: The device context pointer.
13657 *
13658 * This function is directly called from the PCI layer as an interrupt
13659 * service routine when device with SLI-4 interface spec is enabled with
13660 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13661 * IOCB ring event in the HBA. However, when the device is enabled with either
13662 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13663 * device-level interrupt handler. When the PCI slot is in error recovery
13664 * or the HBA is undergoing initialization, the interrupt handler will not
13665 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13666 * the intrrupt context. This function is called without any lock held.
13667 * It gets the hbalock to access and update SLI data structures. Note that,
13668 * the EQ to CQ are one-to-one map such that the EQ index is
13669 * equal to that of CQ index.
13670 *
13671 * This function returns IRQ_HANDLED when interrupt is handled else it
13672 * returns IRQ_NONE.
13673 **/
13674irqreturn_t
13675lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13676{
13677 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013678 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013679 struct lpfc_queue *eq;
13680 struct lpfc_eqe *eqe;
13681 unsigned long iflag;
13682 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013683
13684 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013685 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13686 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013687
13688 if (unlikely(!phba))
13689 return IRQ_NONE;
13690
13691 /* Get to the EQ struct associated with this vector */
13692 eq = phba->sli4_hba.fof_eq;
13693 if (unlikely(!eq))
13694 return IRQ_NONE;
13695
13696 /* Check device state for handling interrupt */
13697 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013698 /* Check again for link_state with lock held */
13699 spin_lock_irqsave(&phba->hbalock, iflag);
13700 if (phba->link_state < LPFC_LINK_DOWN)
13701 /* Flush, clear interrupt, and rearm the EQ */
13702 lpfc_sli4_eq_flush(phba, eq);
13703 spin_unlock_irqrestore(&phba->hbalock, iflag);
13704 return IRQ_NONE;
13705 }
13706
13707 /*
13708 * Process all the event on FCP fast-path EQ
13709 */
13710 while ((eqe = lpfc_sli4_eq_get(eq))) {
13711 lpfc_sli4_fof_handle_eqe(phba, eqe);
13712 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013713 break;
James Smart1ba981f2014-02-20 09:56:45 -050013714 eq->EQ_processed++;
13715 }
13716
13717 /* Track the max number of EQEs processed in 1 intr */
13718 if (ecount > eq->EQ_max_eqe)
13719 eq->EQ_max_eqe = ecount;
13720
13721
13722 if (unlikely(ecount == 0)) {
13723 eq->EQ_no_entry++;
13724
13725 if (phba->intr_type == MSIX)
13726 /* MSI-X treated interrupt served as no EQ share INT */
13727 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13728 "9145 MSI-X interrupt with no EQE\n");
13729 else {
13730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13731 "9146 ISR interrupt with no EQE\n");
13732 /* Non MSI-X treated on interrupt as EQ share INT */
13733 return IRQ_NONE;
13734 }
13735 }
13736 /* Always clear and re-arm the fast-path EQ */
13737 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13738 return IRQ_HANDLED;
13739}
13740
James Smart4f774512009-05-22 14:52:35 -040013741/**
James Smart67d12732012-08-03 12:36:13 -040013742 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013743 * @irq: Interrupt number.
13744 * @dev_id: The device context pointer.
13745 *
13746 * This function is directly called from the PCI layer as an interrupt
13747 * service routine when device with SLI-4 interface spec is enabled with
13748 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13749 * ring event in the HBA. However, when the device is enabled with either
13750 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13751 * device-level interrupt handler. When the PCI slot is in error recovery
13752 * or the HBA is undergoing initialization, the interrupt handler will not
13753 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13754 * the intrrupt context. This function is called without any lock held.
13755 * It gets the hbalock to access and update SLI data structures. Note that,
13756 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13757 * equal to that of FCP CQ index.
13758 *
James Smart67d12732012-08-03 12:36:13 -040013759 * The link attention and ELS ring attention events are handled
13760 * by the worker thread. The interrupt handler signals the worker thread
13761 * and returns for these events. This function is called without any lock
13762 * held. It gets the hbalock to access and update SLI data structures.
13763 *
James Smart4f774512009-05-22 14:52:35 -040013764 * This function returns IRQ_HANDLED when interrupt is handled else it
13765 * returns IRQ_NONE.
13766 **/
13767irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013768lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013769{
13770 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013771 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013772 struct lpfc_queue *fpeq;
13773 struct lpfc_eqe *eqe;
13774 unsigned long iflag;
13775 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013776 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013777
13778 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013779 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13780 phba = hba_eq_hdl->phba;
13781 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013782
13783 if (unlikely(!phba))
13784 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013785 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013786 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013787
13788 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013789 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013790 if (unlikely(!fpeq))
13791 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013792
James Smartba20c852012-08-03 12:36:52 -040013793 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013794 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013795 lpfc_sli4_eq_clr_intr(fpeq);
13796 else {
James Smart895427b2017-02-12 13:52:30 -080013797 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013798 return IRQ_NONE;
13799 }
13800 }
13801
James Smart4f774512009-05-22 14:52:35 -040013802 /* Check device state for handling interrupt */
13803 if (unlikely(lpfc_intr_state_check(phba))) {
13804 /* Check again for link_state with lock held */
13805 spin_lock_irqsave(&phba->hbalock, iflag);
13806 if (phba->link_state < LPFC_LINK_DOWN)
13807 /* Flush, clear interrupt, and rearm the EQ */
13808 lpfc_sli4_eq_flush(phba, fpeq);
13809 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013810 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013811 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013812 return IRQ_NONE;
13813 }
13814
13815 /*
13816 * Process all the event on FCP fast-path EQ
13817 */
13818 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013819 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13820 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013821 break;
James Smartb84daac2012-08-03 12:35:13 -040013822 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013823 }
13824
James Smartb84daac2012-08-03 12:35:13 -040013825 /* Track the max number of EQEs processed in 1 intr */
13826 if (ecount > fpeq->EQ_max_eqe)
13827 fpeq->EQ_max_eqe = ecount;
13828
James Smart4f774512009-05-22 14:52:35 -040013829 /* Always clear and re-arm the fast-path EQ */
13830 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13831
13832 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013833 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013834
13835 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013836 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013837 return IRQ_NONE;
13838 }
13839
James Smart4f774512009-05-22 14:52:35 -040013840 if (phba->intr_type == MSIX)
13841 /* MSI-X treated interrupt served as no EQ share INT */
13842 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13843 "0358 MSI-X interrupt with no EQE\n");
13844 else
13845 /* Non MSI-X treated on interrupt as EQ share INT */
13846 return IRQ_NONE;
13847 }
13848
James Smartba20c852012-08-03 12:36:52 -040013849 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013850 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13851
James Smart4f774512009-05-22 14:52:35 -040013852 return IRQ_HANDLED;
13853} /* lpfc_sli4_fp_intr_handler */
13854
13855/**
13856 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13857 * @irq: Interrupt number.
13858 * @dev_id: The device context pointer.
13859 *
13860 * This function is the device-level interrupt handler to device with SLI-4
13861 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13862 * interrupt mode is enabled and there is an event in the HBA which requires
13863 * driver attention. This function invokes the slow-path interrupt attention
13864 * handling function and fast-path interrupt attention handling function in
13865 * turn to process the relevant HBA attention events. This function is called
13866 * without any lock held. It gets the hbalock to access and update SLI data
13867 * structures.
13868 *
13869 * This function returns IRQ_HANDLED when interrupt is handled, else it
13870 * returns IRQ_NONE.
13871 **/
13872irqreturn_t
13873lpfc_sli4_intr_handler(int irq, void *dev_id)
13874{
13875 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013876 irqreturn_t hba_irq_rc;
13877 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013878 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013879
13880 /* Get the driver's phba structure from the dev_id */
13881 phba = (struct lpfc_hba *)dev_id;
13882
13883 if (unlikely(!phba))
13884 return IRQ_NONE;
13885
13886 /*
James Smart4f774512009-05-22 14:52:35 -040013887 * Invoke fast-path host attention interrupt handling as appropriate.
13888 */
James Smart895427b2017-02-12 13:52:30 -080013889 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013890 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013891 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013892 if (hba_irq_rc == IRQ_HANDLED)
13893 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013894 }
13895
James Smart1ba981f2014-02-20 09:56:45 -050013896 if (phba->cfg_fof) {
13897 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013898 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013899 if (hba_irq_rc == IRQ_HANDLED)
13900 hba_handled |= true;
13901 }
13902
James Smart67d12732012-08-03 12:36:13 -040013903 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013904} /* lpfc_sli4_intr_handler */
13905
13906/**
13907 * lpfc_sli4_queue_free - free a queue structure and associated memory
13908 * @queue: The queue structure to free.
13909 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013910 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013911 * the host resident queue. This function must be called after destroying the
13912 * queue on the HBA.
13913 **/
13914void
13915lpfc_sli4_queue_free(struct lpfc_queue *queue)
13916{
13917 struct lpfc_dmabuf *dmabuf;
13918
13919 if (!queue)
13920 return;
13921
13922 while (!list_empty(&queue->page_list)) {
13923 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13924 list);
James Smart81b96ed2017-11-20 16:00:29 -080013925 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040013926 dmabuf->virt, dmabuf->phys);
13927 kfree(dmabuf);
13928 }
James Smart895427b2017-02-12 13:52:30 -080013929 if (queue->rqbp) {
13930 lpfc_free_rq_buffer(queue->phba, queue);
13931 kfree(queue->rqbp);
13932 }
James Smartd1f525a2017-04-21 16:04:55 -070013933
13934 if (!list_empty(&queue->wq_list))
13935 list_del(&queue->wq_list);
13936
James Smart4f774512009-05-22 14:52:35 -040013937 kfree(queue);
13938 return;
13939}
13940
13941/**
13942 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13943 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080013944 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040013945 * @entry_size: The size of each queue entry for this queue.
13946 * @entry count: The number of entries that this queue will handle.
13947 *
13948 * This function allocates a queue structure and the DMAable memory used for
13949 * the host resident queue. This function must be called before creating the
13950 * queue on the HBA.
13951 **/
13952struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080013953lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13954 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040013955{
13956 struct lpfc_queue *queue;
13957 struct lpfc_dmabuf *dmabuf;
13958 int x, total_qe_count;
13959 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013960 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013961
James Smartcb5172e2010-03-15 11:25:07 -040013962 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080013963 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040013964
James Smart4f774512009-05-22 14:52:35 -040013965 queue = kzalloc(sizeof(struct lpfc_queue) +
13966 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13967 if (!queue)
13968 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013969 queue->page_count = (ALIGN(entry_size * entry_count,
13970 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013971
13972 /* If needed, Adjust page count to match the max the adapter supports */
13973 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13974 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13975
James Smart4f774512009-05-22 14:52:35 -040013976 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013977 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080013978 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040013979 INIT_LIST_HEAD(&queue->page_list);
13980 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080013981
13982 /* Set queue parameters now. If the system cannot provide memory
13983 * resources, the free routine needs to know what was allocated.
13984 */
13985 queue->entry_size = entry_size;
13986 queue->entry_count = entry_count;
13987 queue->page_size = hw_page_size;
13988 queue->phba = phba;
13989
James Smart4f774512009-05-22 14:52:35 -040013990 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13991 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13992 if (!dmabuf)
13993 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013994 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13995 hw_page_size, &dmabuf->phys,
13996 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013997 if (!dmabuf->virt) {
13998 kfree(dmabuf);
13999 goto out_fail;
14000 }
14001 dmabuf->buffer_tag = x;
14002 list_add_tail(&dmabuf->list, &queue->page_list);
14003 /* initialize queue's entry array */
14004 dma_pointer = dmabuf->virt;
14005 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014006 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014007 total_qe_count++, dma_pointer += entry_size) {
14008 queue->qe[total_qe_count].address = dma_pointer;
14009 }
14010 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014011 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14012 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014013
James Smart64eb4dc2017-05-15 15:20:49 -070014014 /* entry_repost will be set during q creation */
14015
James Smart4f774512009-05-22 14:52:35 -040014016 return queue;
14017out_fail:
14018 lpfc_sli4_queue_free(queue);
14019 return NULL;
14020}
14021
14022/**
James Smart962bc512013-01-03 15:44:00 -050014023 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14024 * @phba: HBA structure that indicates port to create a queue on.
14025 * @pci_barset: PCI BAR set flag.
14026 *
14027 * This function shall perform iomap of the specified PCI BAR address to host
14028 * memory address if not already done so and return it. The returned host
14029 * memory address can be NULL.
14030 */
14031static void __iomem *
14032lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14033{
James Smart962bc512013-01-03 15:44:00 -050014034 if (!phba->pcidev)
14035 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014036
14037 switch (pci_barset) {
14038 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014039 return phba->pci_bar0_memmap_p;
14040 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014041 return phba->pci_bar2_memmap_p;
14042 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014043 return phba->pci_bar4_memmap_p;
14044 default:
14045 break;
14046 }
14047 return NULL;
14048}
14049
14050/**
James Smart895427b2017-02-12 13:52:30 -080014051 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014052 * @phba: HBA structure that indicates port to create a queue on.
14053 * @startq: The starting FCP EQ to modify
14054 *
14055 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014056 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14057 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014058 *
14059 * The @phba struct is used to send mailbox command to HBA. The @startq
14060 * is used to get the starting FCP EQ to change.
14061 * This function is asynchronous and will wait for the mailbox
14062 * command to finish before continuing.
14063 *
14064 * On success this function will return a zero. If unable to allocate enough
14065 * memory this function will return -ENOMEM. If the queue create mailbox command
14066 * fails this function will return -ENXIO.
14067 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014068int
James Smart0cf07f842017-06-01 21:07:10 -070014069lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14070 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014071{
14072 struct lpfc_mbx_modify_eq_delay *eq_delay;
14073 LPFC_MBOXQ_t *mbox;
14074 struct lpfc_queue *eq;
14075 int cnt, rc, length, status = 0;
14076 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014077 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014078 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014079 union lpfc_sli4_cfg_shdr *shdr;
14080 uint16_t dmult;
14081
James Smart895427b2017-02-12 13:52:30 -080014082 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014083 return 0;
14084
14085 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14086 if (!mbox)
14087 return -ENOMEM;
14088 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14089 sizeof(struct lpfc_sli4_cfg_mhdr));
14090 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14091 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14092 length, LPFC_SLI4_MBX_EMBED);
14093 eq_delay = &mbox->u.mqe.un.eq_delay;
14094
14095 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014096 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014097 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014098 dmult = 0;
14099 else
14100 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014101 if (dmult > LPFC_DMULT_MAX)
14102 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014103
14104 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014105 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14106 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014107 if (!eq)
14108 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014109 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014110 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14111 eq_delay->u.request.eq[cnt].phase = 0;
14112 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14113 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014114
14115 /* q_mode is only used for auto_imax */
14116 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14117 /* Use EQ Delay Register method for q_mode */
14118
14119 /* Convert for EQ Delay register */
14120 val = phba->cfg_fcp_imax;
14121 if (val) {
14122 /* First, interrupts per sec per EQ */
14123 val = phba->cfg_fcp_imax /
14124 phba->io_channel_irqs;
14125
14126 /* us delay between each interrupt */
14127 val = LPFC_SEC_TO_USEC / val;
14128 }
14129 eq->q_mode = val;
14130 } else {
14131 eq->q_mode = imax;
14132 }
14133
14134 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014135 break;
14136 }
14137 eq_delay->u.request.num_eq = cnt;
14138
14139 mbox->vport = phba->pport;
14140 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14141 mbox->context1 = NULL;
14142 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14143 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14144 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14145 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14146 if (shdr_status || shdr_add_status || rc) {
14147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14148 "2512 MODIFY_EQ_DELAY mailbox failed with "
14149 "status x%x add_status x%x, mbx status x%x\n",
14150 shdr_status, shdr_add_status, rc);
14151 status = -ENXIO;
14152 }
14153 mempool_free(mbox, phba->mbox_mem_pool);
14154 return status;
14155}
14156
14157/**
James Smart4f774512009-05-22 14:52:35 -040014158 * lpfc_eq_create - Create an Event Queue on the HBA
14159 * @phba: HBA structure that indicates port to create a queue on.
14160 * @eq: The queue structure to use to create the event queue.
14161 * @imax: The maximum interrupt per second limit.
14162 *
14163 * This function creates an event queue, as detailed in @eq, on a port,
14164 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14165 *
14166 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14167 * is used to get the entry count and entry size that are necessary to
14168 * determine the number of pages to allocate and use for this queue. This
14169 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14170 * event queue. This function is asynchronous and will wait for the mailbox
14171 * command to finish before continuing.
14172 *
14173 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014174 * memory this function will return -ENOMEM. If the queue create mailbox command
14175 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014176 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014177int
James Smartee020062012-09-29 11:28:52 -040014178lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014179{
14180 struct lpfc_mbx_eq_create *eq_create;
14181 LPFC_MBOXQ_t *mbox;
14182 int rc, length, status = 0;
14183 struct lpfc_dmabuf *dmabuf;
14184 uint32_t shdr_status, shdr_add_status;
14185 union lpfc_sli4_cfg_shdr *shdr;
14186 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014187 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14188
James Smart2e90f4b2011-12-13 13:22:37 -050014189 /* sanity check on queue memory */
14190 if (!eq)
14191 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014192 if (!phba->sli4_hba.pc_sli4_params.supported)
14193 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014194
14195 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14196 if (!mbox)
14197 return -ENOMEM;
14198 length = (sizeof(struct lpfc_mbx_eq_create) -
14199 sizeof(struct lpfc_sli4_cfg_mhdr));
14200 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14201 LPFC_MBOX_OPCODE_EQ_CREATE,
14202 length, LPFC_SLI4_MBX_EMBED);
14203 eq_create = &mbox->u.mqe.un.eq_create;
14204 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14205 eq->page_count);
14206 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14207 LPFC_EQE_SIZE);
14208 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014209 /* don't setup delay multiplier using EQ_CREATE */
14210 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014211 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14212 dmult);
14213 switch (eq->entry_count) {
14214 default:
14215 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14216 "0360 Unsupported EQ count. (%d)\n",
14217 eq->entry_count);
14218 if (eq->entry_count < 256)
14219 return -EINVAL;
14220 /* otherwise default to smallest count (drop through) */
14221 case 256:
14222 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14223 LPFC_EQ_CNT_256);
14224 break;
14225 case 512:
14226 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14227 LPFC_EQ_CNT_512);
14228 break;
14229 case 1024:
14230 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14231 LPFC_EQ_CNT_1024);
14232 break;
14233 case 2048:
14234 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14235 LPFC_EQ_CNT_2048);
14236 break;
14237 case 4096:
14238 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14239 LPFC_EQ_CNT_4096);
14240 break;
14241 }
14242 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014243 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014244 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14245 putPaddrLow(dmabuf->phys);
14246 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14247 putPaddrHigh(dmabuf->phys);
14248 }
14249 mbox->vport = phba->pport;
14250 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14251 mbox->context1 = NULL;
14252 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14253 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14254 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14255 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14256 if (shdr_status || shdr_add_status || rc) {
14257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14258 "2500 EQ_CREATE mailbox failed with "
14259 "status x%x add_status x%x, mbx status x%x\n",
14260 shdr_status, shdr_add_status, rc);
14261 status = -ENXIO;
14262 }
14263 eq->type = LPFC_EQ;
14264 eq->subtype = LPFC_NONE;
14265 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14266 if (eq->queue_id == 0xFFFF)
14267 status = -ENXIO;
14268 eq->host_index = 0;
14269 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014270 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014271
James Smart8fa38512009-07-19 10:01:03 -040014272 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014273 return status;
14274}
14275
14276/**
14277 * lpfc_cq_create - Create a Completion Queue on the HBA
14278 * @phba: HBA structure that indicates port to create a queue on.
14279 * @cq: The queue structure to use to create the completion queue.
14280 * @eq: The event queue to bind this completion queue to.
14281 *
14282 * This function creates a completion queue, as detailed in @wq, on a port,
14283 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14284 *
14285 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14286 * is used to get the entry count and entry size that are necessary to
14287 * determine the number of pages to allocate and use for this queue. The @eq
14288 * is used to indicate which event queue to bind this completion queue to. This
14289 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14290 * completion queue. This function is asynchronous and will wait for the mailbox
14291 * command to finish before continuing.
14292 *
14293 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014294 * memory this function will return -ENOMEM. If the queue create mailbox command
14295 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014296 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014297int
James Smart4f774512009-05-22 14:52:35 -040014298lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14299 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14300{
14301 struct lpfc_mbx_cq_create *cq_create;
14302 struct lpfc_dmabuf *dmabuf;
14303 LPFC_MBOXQ_t *mbox;
14304 int rc, length, status = 0;
14305 uint32_t shdr_status, shdr_add_status;
14306 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014307 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14308
James Smart2e90f4b2011-12-13 13:22:37 -050014309 /* sanity check on queue memory */
14310 if (!cq || !eq)
14311 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014312 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014313 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014314
James Smart4f774512009-05-22 14:52:35 -040014315 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14316 if (!mbox)
14317 return -ENOMEM;
14318 length = (sizeof(struct lpfc_mbx_cq_create) -
14319 sizeof(struct lpfc_sli4_cfg_mhdr));
14320 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14321 LPFC_MBOX_OPCODE_CQ_CREATE,
14322 length, LPFC_SLI4_MBX_EMBED);
14323 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014324 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014325 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14326 cq->page_count);
14327 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14328 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014329 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14330 phba->sli4_hba.pc_sli4_params.cqv);
14331 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014332 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14333 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014334 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14335 eq->queue_id);
14336 } else {
14337 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14338 eq->queue_id);
14339 }
James Smart4f774512009-05-22 14:52:35 -040014340 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014341 case 2048:
14342 case 4096:
14343 if (phba->sli4_hba.pc_sli4_params.cqv ==
14344 LPFC_Q_CREATE_VERSION_2) {
14345 cq_create->u.request.context.lpfc_cq_context_count =
14346 cq->entry_count;
14347 bf_set(lpfc_cq_context_count,
14348 &cq_create->u.request.context,
14349 LPFC_CQ_CNT_WORD7);
14350 break;
14351 }
14352 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014353 default:
14354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014355 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014356 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014357 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014358 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014359 if (cq->entry_count < 256) {
14360 status = -EINVAL;
14361 goto out;
14362 }
James Smart4f774512009-05-22 14:52:35 -040014363 /* otherwise default to smallest count (drop through) */
14364 case 256:
14365 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14366 LPFC_CQ_CNT_256);
14367 break;
14368 case 512:
14369 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14370 LPFC_CQ_CNT_512);
14371 break;
14372 case 1024:
14373 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14374 LPFC_CQ_CNT_1024);
14375 break;
14376 }
14377 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014378 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014379 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14380 putPaddrLow(dmabuf->phys);
14381 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14382 putPaddrHigh(dmabuf->phys);
14383 }
14384 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14385
14386 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014387 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14388 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14389 if (shdr_status || shdr_add_status || rc) {
14390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14391 "2501 CQ_CREATE mailbox failed with "
14392 "status x%x add_status x%x, mbx status x%x\n",
14393 shdr_status, shdr_add_status, rc);
14394 status = -ENXIO;
14395 goto out;
14396 }
14397 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14398 if (cq->queue_id == 0xFFFF) {
14399 status = -ENXIO;
14400 goto out;
14401 }
14402 /* link the cq onto the parent eq child list */
14403 list_add_tail(&cq->list, &eq->child_list);
14404 /* Set up completion queue's type and subtype */
14405 cq->type = type;
14406 cq->subtype = subtype;
14407 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014408 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014409 cq->host_index = 0;
14410 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014411 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014412
James Smart8fa38512009-07-19 10:01:03 -040014413out:
14414 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014415 return status;
14416}
14417
14418/**
James Smart2d7dbc42017-02-12 13:52:35 -080014419 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14420 * @phba: HBA structure that indicates port to create a queue on.
14421 * @cqp: The queue structure array to use to create the completion queues.
14422 * @eqp: The event queue array to bind these completion queues to.
14423 *
14424 * This function creates a set of completion queue, s to support MRQ
14425 * as detailed in @cqp, on a port,
14426 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14427 *
14428 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14429 * is used to get the entry count and entry size that are necessary to
14430 * determine the number of pages to allocate and use for this queue. The @eq
14431 * is used to indicate which event queue to bind this completion queue to. This
14432 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14433 * completion queue. This function is asynchronous and will wait for the mailbox
14434 * command to finish before continuing.
14435 *
14436 * On success this function will return a zero. If unable to allocate enough
14437 * memory this function will return -ENOMEM. If the queue create mailbox command
14438 * fails this function will return -ENXIO.
14439 **/
14440int
14441lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14442 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14443{
14444 struct lpfc_queue *cq;
14445 struct lpfc_queue *eq;
14446 struct lpfc_mbx_cq_create_set *cq_set;
14447 struct lpfc_dmabuf *dmabuf;
14448 LPFC_MBOXQ_t *mbox;
14449 int rc, length, alloclen, status = 0;
14450 int cnt, idx, numcq, page_idx = 0;
14451 uint32_t shdr_status, shdr_add_status;
14452 union lpfc_sli4_cfg_shdr *shdr;
14453 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14454
14455 /* sanity check on queue memory */
14456 numcq = phba->cfg_nvmet_mrq;
14457 if (!cqp || !eqp || !numcq)
14458 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014459
14460 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14461 if (!mbox)
14462 return -ENOMEM;
14463
14464 length = sizeof(struct lpfc_mbx_cq_create_set);
14465 length += ((numcq * cqp[0]->page_count) *
14466 sizeof(struct dma_address));
14467 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14468 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14469 LPFC_SLI4_MBX_NEMBED);
14470 if (alloclen < length) {
14471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14472 "3098 Allocated DMA memory size (%d) is "
14473 "less than the requested DMA memory size "
14474 "(%d)\n", alloclen, length);
14475 status = -ENOMEM;
14476 goto out;
14477 }
14478 cq_set = mbox->sge_array->addr[0];
14479 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14480 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14481
14482 for (idx = 0; idx < numcq; idx++) {
14483 cq = cqp[idx];
14484 eq = eqp[idx];
14485 if (!cq || !eq) {
14486 status = -ENOMEM;
14487 goto out;
14488 }
James Smart81b96ed2017-11-20 16:00:29 -080014489 if (!phba->sli4_hba.pc_sli4_params.supported)
14490 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014491
14492 switch (idx) {
14493 case 0:
14494 bf_set(lpfc_mbx_cq_create_set_page_size,
14495 &cq_set->u.request,
14496 (hw_page_size / SLI4_PAGE_SIZE));
14497 bf_set(lpfc_mbx_cq_create_set_num_pages,
14498 &cq_set->u.request, cq->page_count);
14499 bf_set(lpfc_mbx_cq_create_set_evt,
14500 &cq_set->u.request, 1);
14501 bf_set(lpfc_mbx_cq_create_set_valid,
14502 &cq_set->u.request, 1);
14503 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14504 &cq_set->u.request, 0);
14505 bf_set(lpfc_mbx_cq_create_set_num_cq,
14506 &cq_set->u.request, numcq);
14507 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014508 case 2048:
14509 case 4096:
14510 if (phba->sli4_hba.pc_sli4_params.cqv ==
14511 LPFC_Q_CREATE_VERSION_2) {
14512 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14513 &cq_set->u.request,
14514 cq->entry_count);
14515 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14516 &cq_set->u.request,
14517 LPFC_CQ_CNT_WORD7);
14518 break;
14519 }
14520 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014521 default:
14522 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14523 "3118 Bad CQ count. (%d)\n",
14524 cq->entry_count);
14525 if (cq->entry_count < 256) {
14526 status = -EINVAL;
14527 goto out;
14528 }
14529 /* otherwise default to smallest (drop thru) */
14530 case 256:
14531 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14532 &cq_set->u.request, LPFC_CQ_CNT_256);
14533 break;
14534 case 512:
14535 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14536 &cq_set->u.request, LPFC_CQ_CNT_512);
14537 break;
14538 case 1024:
14539 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14540 &cq_set->u.request, LPFC_CQ_CNT_1024);
14541 break;
14542 }
14543 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14544 &cq_set->u.request, eq->queue_id);
14545 break;
14546 case 1:
14547 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14548 &cq_set->u.request, eq->queue_id);
14549 break;
14550 case 2:
14551 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14552 &cq_set->u.request, eq->queue_id);
14553 break;
14554 case 3:
14555 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14556 &cq_set->u.request, eq->queue_id);
14557 break;
14558 case 4:
14559 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14560 &cq_set->u.request, eq->queue_id);
14561 break;
14562 case 5:
14563 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14564 &cq_set->u.request, eq->queue_id);
14565 break;
14566 case 6:
14567 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14568 &cq_set->u.request, eq->queue_id);
14569 break;
14570 case 7:
14571 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14572 &cq_set->u.request, eq->queue_id);
14573 break;
14574 case 8:
14575 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14576 &cq_set->u.request, eq->queue_id);
14577 break;
14578 case 9:
14579 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14580 &cq_set->u.request, eq->queue_id);
14581 break;
14582 case 10:
14583 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14584 &cq_set->u.request, eq->queue_id);
14585 break;
14586 case 11:
14587 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14588 &cq_set->u.request, eq->queue_id);
14589 break;
14590 case 12:
14591 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14592 &cq_set->u.request, eq->queue_id);
14593 break;
14594 case 13:
14595 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14596 &cq_set->u.request, eq->queue_id);
14597 break;
14598 case 14:
14599 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14600 &cq_set->u.request, eq->queue_id);
14601 break;
14602 case 15:
14603 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14604 &cq_set->u.request, eq->queue_id);
14605 break;
14606 }
14607
14608 /* link the cq onto the parent eq child list */
14609 list_add_tail(&cq->list, &eq->child_list);
14610 /* Set up completion queue's type and subtype */
14611 cq->type = type;
14612 cq->subtype = subtype;
14613 cq->assoc_qid = eq->queue_id;
14614 cq->host_index = 0;
14615 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014616 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014617 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014618
14619 rc = 0;
14620 list_for_each_entry(dmabuf, &cq->page_list, list) {
14621 memset(dmabuf->virt, 0, hw_page_size);
14622 cnt = page_idx + dmabuf->buffer_tag;
14623 cq_set->u.request.page[cnt].addr_lo =
14624 putPaddrLow(dmabuf->phys);
14625 cq_set->u.request.page[cnt].addr_hi =
14626 putPaddrHigh(dmabuf->phys);
14627 rc++;
14628 }
14629 page_idx += rc;
14630 }
14631
14632 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14633
14634 /* The IOCTL status is embedded in the mailbox subheader. */
14635 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14636 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14637 if (shdr_status || shdr_add_status || rc) {
14638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14639 "3119 CQ_CREATE_SET mailbox failed with "
14640 "status x%x add_status x%x, mbx status x%x\n",
14641 shdr_status, shdr_add_status, rc);
14642 status = -ENXIO;
14643 goto out;
14644 }
14645 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14646 if (rc == 0xFFFF) {
14647 status = -ENXIO;
14648 goto out;
14649 }
14650
14651 for (idx = 0; idx < numcq; idx++) {
14652 cq = cqp[idx];
14653 cq->queue_id = rc + idx;
14654 }
14655
14656out:
14657 lpfc_sli4_mbox_cmd_free(phba, mbox);
14658 return status;
14659}
14660
14661/**
James Smartb19a0612010-04-06 14:48:51 -040014662 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014663 * @phba: HBA structure that indicates port to create a queue on.
14664 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014665 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14666 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014667 *
James Smartb19a0612010-04-06 14:48:51 -040014668 * This function provides failback (fb) functionality when the
14669 * mq_create_ext fails on older FW generations. It's purpose is identical
14670 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014671 *
James Smartb19a0612010-04-06 14:48:51 -040014672 * This routine cannot fail as all attributes were previously accessed and
14673 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014674 **/
James Smartb19a0612010-04-06 14:48:51 -040014675static void
14676lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14677 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014678{
14679 struct lpfc_mbx_mq_create *mq_create;
14680 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014681 int length;
James Smart04c68492009-05-22 14:52:52 -040014682
James Smart04c68492009-05-22 14:52:52 -040014683 length = (sizeof(struct lpfc_mbx_mq_create) -
14684 sizeof(struct lpfc_sli4_cfg_mhdr));
14685 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14686 LPFC_MBOX_OPCODE_MQ_CREATE,
14687 length, LPFC_SLI4_MBX_EMBED);
14688 mq_create = &mbox->u.mqe.un.mq_create;
14689 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014690 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014691 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014692 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014693 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14694 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014695 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014696 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14697 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014698 break;
14699 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014700 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14701 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014702 break;
14703 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014704 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14705 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014706 break;
14707 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014708 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14709 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014710 break;
14711 }
14712 list_for_each_entry(dmabuf, &mq->page_list, list) {
14713 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014714 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014715 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014716 putPaddrHigh(dmabuf->phys);
14717 }
14718}
14719
14720/**
14721 * lpfc_mq_create - Create a mailbox Queue on the HBA
14722 * @phba: HBA structure that indicates port to create a queue on.
14723 * @mq: The queue structure to use to create the mailbox queue.
14724 * @cq: The completion queue to associate with this cq.
14725 * @subtype: The queue's subtype.
14726 *
14727 * This function creates a mailbox queue, as detailed in @mq, on a port,
14728 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14729 *
14730 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14731 * is used to get the entry count and entry size that are necessary to
14732 * determine the number of pages to allocate and use for this queue. This
14733 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14734 * mailbox queue. This function is asynchronous and will wait for the mailbox
14735 * command to finish before continuing.
14736 *
14737 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014738 * memory this function will return -ENOMEM. If the queue create mailbox command
14739 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014740 **/
14741int32_t
14742lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14743 struct lpfc_queue *cq, uint32_t subtype)
14744{
14745 struct lpfc_mbx_mq_create *mq_create;
14746 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14747 struct lpfc_dmabuf *dmabuf;
14748 LPFC_MBOXQ_t *mbox;
14749 int rc, length, status = 0;
14750 uint32_t shdr_status, shdr_add_status;
14751 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014752 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014753
James Smart2e90f4b2011-12-13 13:22:37 -050014754 /* sanity check on queue memory */
14755 if (!mq || !cq)
14756 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014757 if (!phba->sli4_hba.pc_sli4_params.supported)
14758 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014759
14760 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14761 if (!mbox)
14762 return -ENOMEM;
14763 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14764 sizeof(struct lpfc_sli4_cfg_mhdr));
14765 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14766 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14767 length, LPFC_SLI4_MBX_EMBED);
14768
14769 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014770 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014771 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14772 &mq_create_ext->u.request, mq->page_count);
14773 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14774 &mq_create_ext->u.request, 1);
14775 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014776 &mq_create_ext->u.request, 1);
14777 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14778 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014779 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14780 &mq_create_ext->u.request, 1);
14781 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14782 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014783 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014784 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14785 phba->sli4_hba.pc_sli4_params.mqv);
14786 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14787 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14788 cq->queue_id);
14789 else
14790 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14791 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014792 switch (mq->entry_count) {
14793 default:
14794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14795 "0362 Unsupported MQ count. (%d)\n",
14796 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014797 if (mq->entry_count < 16) {
14798 status = -EINVAL;
14799 goto out;
14800 }
James Smartb19a0612010-04-06 14:48:51 -040014801 /* otherwise default to smallest count (drop through) */
14802 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014803 bf_set(lpfc_mq_context_ring_size,
14804 &mq_create_ext->u.request.context,
14805 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014806 break;
14807 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014808 bf_set(lpfc_mq_context_ring_size,
14809 &mq_create_ext->u.request.context,
14810 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014811 break;
14812 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014813 bf_set(lpfc_mq_context_ring_size,
14814 &mq_create_ext->u.request.context,
14815 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014816 break;
14817 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014818 bf_set(lpfc_mq_context_ring_size,
14819 &mq_create_ext->u.request.context,
14820 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014821 break;
14822 }
14823 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014824 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014825 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14826 putPaddrLow(dmabuf->phys);
14827 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014828 putPaddrHigh(dmabuf->phys);
14829 }
14830 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014831 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14832 &mq_create_ext->u.response);
14833 if (rc != MBX_SUCCESS) {
14834 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14835 "2795 MQ_CREATE_EXT failed with "
14836 "status x%x. Failback to MQ_CREATE.\n",
14837 rc);
14838 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14839 mq_create = &mbox->u.mqe.un.mq_create;
14840 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14841 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14842 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14843 &mq_create->u.response);
14844 }
14845
James Smart04c68492009-05-22 14:52:52 -040014846 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014847 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14848 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14849 if (shdr_status || shdr_add_status || rc) {
14850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14851 "2502 MQ_CREATE mailbox failed with "
14852 "status x%x add_status x%x, mbx status x%x\n",
14853 shdr_status, shdr_add_status, rc);
14854 status = -ENXIO;
14855 goto out;
14856 }
James Smart04c68492009-05-22 14:52:52 -040014857 if (mq->queue_id == 0xFFFF) {
14858 status = -ENXIO;
14859 goto out;
14860 }
14861 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014862 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014863 mq->subtype = subtype;
14864 mq->host_index = 0;
14865 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014866 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014867
14868 /* link the mq onto the parent cq child list */
14869 list_add_tail(&mq->list, &cq->child_list);
14870out:
James Smart8fa38512009-07-19 10:01:03 -040014871 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014872 return status;
14873}
14874
14875/**
James Smart4f774512009-05-22 14:52:35 -040014876 * lpfc_wq_create - Create a Work Queue on the HBA
14877 * @phba: HBA structure that indicates port to create a queue on.
14878 * @wq: The queue structure to use to create the work queue.
14879 * @cq: The completion queue to bind this work queue to.
14880 * @subtype: The subtype of the work queue indicating its functionality.
14881 *
14882 * This function creates a work queue, as detailed in @wq, on a port, described
14883 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14884 *
14885 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14886 * is used to get the entry count and entry size that are necessary to
14887 * determine the number of pages to allocate and use for this queue. The @cq
14888 * is used to indicate which completion queue to bind this work queue to. This
14889 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14890 * work queue. This function is asynchronous and will wait for the mailbox
14891 * command to finish before continuing.
14892 *
14893 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014894 * memory this function will return -ENOMEM. If the queue create mailbox command
14895 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014896 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014897int
James Smart4f774512009-05-22 14:52:35 -040014898lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14899 struct lpfc_queue *cq, uint32_t subtype)
14900{
14901 struct lpfc_mbx_wq_create *wq_create;
14902 struct lpfc_dmabuf *dmabuf;
14903 LPFC_MBOXQ_t *mbox;
14904 int rc, length, status = 0;
14905 uint32_t shdr_status, shdr_add_status;
14906 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014907 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014908 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014909 void __iomem *bar_memmap_p;
14910 uint32_t db_offset;
14911 uint16_t pci_barset;
James Smart81b96ed2017-11-20 16:00:29 -080014912 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040014913
James Smart2e90f4b2011-12-13 13:22:37 -050014914 /* sanity check on queue memory */
14915 if (!wq || !cq)
14916 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014917 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014918 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040014919
14920 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14921 if (!mbox)
14922 return -ENOMEM;
14923 length = (sizeof(struct lpfc_mbx_wq_create) -
14924 sizeof(struct lpfc_sli4_cfg_mhdr));
14925 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14926 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14927 length, LPFC_SLI4_MBX_EMBED);
14928 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014929 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014930 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14931 wq->page_count);
14932 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14933 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014934
14935 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014936 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14937 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014938
James Smartc176ffa2018-01-30 15:58:46 -080014939 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
14940 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080014941 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14942 else
14943 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14944
14945 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040014946 case LPFC_Q_CREATE_VERSION_0:
14947 switch (wq->entry_size) {
14948 default:
14949 case 64:
14950 /* Nothing to do, version 0 ONLY supports 64 byte */
14951 page = wq_create->u.request.page;
14952 break;
14953 case 128:
14954 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14955 LPFC_WQ_SZ128_SUPPORT)) {
14956 status = -ERANGE;
14957 goto out;
14958 }
14959 /* If we get here the HBA MUST also support V1 and
14960 * we MUST use it
14961 */
14962 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14963 LPFC_Q_CREATE_VERSION_1);
14964
14965 bf_set(lpfc_mbx_wq_create_wqe_count,
14966 &wq_create->u.request_1, wq->entry_count);
14967 bf_set(lpfc_mbx_wq_create_wqe_size,
14968 &wq_create->u.request_1,
14969 LPFC_WQ_WQE_SIZE_128);
14970 bf_set(lpfc_mbx_wq_create_page_size,
14971 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014972 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014973 page = wq_create->u.request_1.page;
14974 break;
14975 }
14976 break;
14977 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014978 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14979 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014980 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14981 LPFC_Q_CREATE_VERSION_1);
14982
James Smart5a6f1332011-03-11 16:05:35 -050014983 switch (wq->entry_size) {
14984 default:
14985 case 64:
14986 bf_set(lpfc_mbx_wq_create_wqe_size,
14987 &wq_create->u.request_1,
14988 LPFC_WQ_WQE_SIZE_64);
14989 break;
14990 case 128:
James Smart0c651872013-07-15 18:33:23 -040014991 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14992 LPFC_WQ_SZ128_SUPPORT)) {
14993 status = -ERANGE;
14994 goto out;
14995 }
James Smart5a6f1332011-03-11 16:05:35 -050014996 bf_set(lpfc_mbx_wq_create_wqe_size,
14997 &wq_create->u.request_1,
14998 LPFC_WQ_WQE_SIZE_128);
14999 break;
15000 }
James Smart8ea73db2017-02-12 13:52:25 -080015001 bf_set(lpfc_mbx_wq_create_page_size,
15002 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015003 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015004 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015005 break;
15006 default:
15007 status = -ERANGE;
15008 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050015009 }
James Smart0c651872013-07-15 18:33:23 -040015010
James Smart4f774512009-05-22 14:52:35 -040015011 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015012 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015013 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15014 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015015 }
James Smart962bc512013-01-03 15:44:00 -050015016
15017 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15018 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15019
James Smart4f774512009-05-22 14:52:35 -040015020 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15021 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015022 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15023 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15024 if (shdr_status || shdr_add_status || rc) {
15025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15026 "2503 WQ_CREATE mailbox failed with "
15027 "status x%x add_status x%x, mbx status x%x\n",
15028 shdr_status, shdr_add_status, rc);
15029 status = -ENXIO;
15030 goto out;
15031 }
15032 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15033 if (wq->queue_id == 0xFFFF) {
15034 status = -ENXIO;
15035 goto out;
15036 }
James Smart962bc512013-01-03 15:44:00 -050015037 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15038 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15039 &wq_create->u.response);
15040 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15041 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15043 "3265 WQ[%d] doorbell format not "
15044 "supported: x%x\n", wq->queue_id,
15045 wq->db_format);
15046 status = -EINVAL;
15047 goto out;
15048 }
15049 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15050 &wq_create->u.response);
15051 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15052 if (!bar_memmap_p) {
15053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15054 "3263 WQ[%d] failed to memmap pci "
15055 "barset:x%x\n", wq->queue_id,
15056 pci_barset);
15057 status = -ENOMEM;
15058 goto out;
15059 }
15060 db_offset = wq_create->u.response.doorbell_offset;
15061 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15062 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15064 "3252 WQ[%d] doorbell offset not "
15065 "supported: x%x\n", wq->queue_id,
15066 db_offset);
15067 status = -EINVAL;
15068 goto out;
15069 }
15070 wq->db_regaddr = bar_memmap_p + db_offset;
15071 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015072 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15073 "format:x%x\n", wq->queue_id, pci_barset,
15074 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015075 } else {
15076 wq->db_format = LPFC_DB_LIST_FORMAT;
15077 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15078 }
James Smart895427b2017-02-12 13:52:30 -080015079 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15080 if (wq->pring == NULL) {
15081 status = -ENOMEM;
15082 goto out;
15083 }
James Smart4f774512009-05-22 14:52:35 -040015084 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015085 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015086 wq->subtype = subtype;
15087 wq->host_index = 0;
15088 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015089 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015090
15091 /* link the wq onto the parent cq child list */
15092 list_add_tail(&wq->list, &cq->child_list);
15093out:
James Smart8fa38512009-07-19 10:01:03 -040015094 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015095 return status;
15096}
15097
15098/**
15099 * lpfc_rq_create - Create a Receive Queue on the HBA
15100 * @phba: HBA structure that indicates port to create a queue on.
15101 * @hrq: The queue structure to use to create the header receive queue.
15102 * @drq: The queue structure to use to create the data receive queue.
15103 * @cq: The completion queue to bind this work queue to.
15104 *
15105 * This function creates a receive buffer queue pair , as detailed in @hrq and
15106 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15107 * to the HBA.
15108 *
15109 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15110 * struct is used to get the entry count that is necessary to determine the
15111 * number of pages to use for this queue. The @cq is used to indicate which
15112 * completion queue to bind received buffers that are posted to these queues to.
15113 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15114 * receive queue pair. This function is asynchronous and will wait for the
15115 * mailbox command to finish before continuing.
15116 *
15117 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015118 * memory this function will return -ENOMEM. If the queue create mailbox command
15119 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015120 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015121int
James Smart4f774512009-05-22 14:52:35 -040015122lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15123 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15124{
15125 struct lpfc_mbx_rq_create *rq_create;
15126 struct lpfc_dmabuf *dmabuf;
15127 LPFC_MBOXQ_t *mbox;
15128 int rc, length, status = 0;
15129 uint32_t shdr_status, shdr_add_status;
15130 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015131 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015132 void __iomem *bar_memmap_p;
15133 uint32_t db_offset;
15134 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015135
James Smart2e90f4b2011-12-13 13:22:37 -050015136 /* sanity check on queue memory */
15137 if (!hrq || !drq || !cq)
15138 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015139 if (!phba->sli4_hba.pc_sli4_params.supported)
15140 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015141
15142 if (hrq->entry_count != drq->entry_count)
15143 return -EINVAL;
15144 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15145 if (!mbox)
15146 return -ENOMEM;
15147 length = (sizeof(struct lpfc_mbx_rq_create) -
15148 sizeof(struct lpfc_sli4_cfg_mhdr));
15149 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15150 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15151 length, LPFC_SLI4_MBX_EMBED);
15152 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015153 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15154 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15155 phba->sli4_hba.pc_sli4_params.rqv);
15156 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15157 bf_set(lpfc_rq_context_rqe_count_1,
15158 &rq_create->u.request.context,
15159 hrq->entry_count);
15160 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015161 bf_set(lpfc_rq_context_rqe_size,
15162 &rq_create->u.request.context,
15163 LPFC_RQE_SIZE_8);
15164 bf_set(lpfc_rq_context_page_size,
15165 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015166 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015167 } else {
15168 switch (hrq->entry_count) {
15169 default:
15170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15171 "2535 Unsupported RQ count. (%d)\n",
15172 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015173 if (hrq->entry_count < 512) {
15174 status = -EINVAL;
15175 goto out;
15176 }
James Smart5a6f1332011-03-11 16:05:35 -050015177 /* otherwise default to smallest count (drop through) */
15178 case 512:
15179 bf_set(lpfc_rq_context_rqe_count,
15180 &rq_create->u.request.context,
15181 LPFC_RQ_RING_SIZE_512);
15182 break;
15183 case 1024:
15184 bf_set(lpfc_rq_context_rqe_count,
15185 &rq_create->u.request.context,
15186 LPFC_RQ_RING_SIZE_1024);
15187 break;
15188 case 2048:
15189 bf_set(lpfc_rq_context_rqe_count,
15190 &rq_create->u.request.context,
15191 LPFC_RQ_RING_SIZE_2048);
15192 break;
15193 case 4096:
15194 bf_set(lpfc_rq_context_rqe_count,
15195 &rq_create->u.request.context,
15196 LPFC_RQ_RING_SIZE_4096);
15197 break;
15198 }
15199 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15200 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015201 }
15202 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15203 cq->queue_id);
15204 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15205 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015206 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015207 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015208 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15209 putPaddrLow(dmabuf->phys);
15210 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15211 putPaddrHigh(dmabuf->phys);
15212 }
James Smart962bc512013-01-03 15:44:00 -050015213 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15214 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15215
James Smart4f774512009-05-22 14:52:35 -040015216 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15217 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15220 if (shdr_status || shdr_add_status || rc) {
15221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15222 "2504 RQ_CREATE mailbox failed with "
15223 "status x%x add_status x%x, mbx status x%x\n",
15224 shdr_status, shdr_add_status, rc);
15225 status = -ENXIO;
15226 goto out;
15227 }
15228 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15229 if (hrq->queue_id == 0xFFFF) {
15230 status = -ENXIO;
15231 goto out;
15232 }
James Smart962bc512013-01-03 15:44:00 -050015233
15234 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15235 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15236 &rq_create->u.response);
15237 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15238 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15240 "3262 RQ [%d] doorbell format not "
15241 "supported: x%x\n", hrq->queue_id,
15242 hrq->db_format);
15243 status = -EINVAL;
15244 goto out;
15245 }
15246
15247 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15248 &rq_create->u.response);
15249 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15250 if (!bar_memmap_p) {
15251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15252 "3269 RQ[%d] failed to memmap pci "
15253 "barset:x%x\n", hrq->queue_id,
15254 pci_barset);
15255 status = -ENOMEM;
15256 goto out;
15257 }
15258
15259 db_offset = rq_create->u.response.doorbell_offset;
15260 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15261 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15263 "3270 RQ[%d] doorbell offset not "
15264 "supported: x%x\n", hrq->queue_id,
15265 db_offset);
15266 status = -EINVAL;
15267 goto out;
15268 }
15269 hrq->db_regaddr = bar_memmap_p + db_offset;
15270 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015271 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15272 "format:x%x\n", hrq->queue_id, pci_barset,
15273 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015274 } else {
15275 hrq->db_format = LPFC_DB_RING_FORMAT;
15276 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15277 }
James Smart4f774512009-05-22 14:52:35 -040015278 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015279 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015280 hrq->subtype = subtype;
15281 hrq->host_index = 0;
15282 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015283 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015284
15285 /* now create the data queue */
15286 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15287 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15288 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015289 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15290 phba->sli4_hba.pc_sli4_params.rqv);
15291 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15292 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015293 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015294 if (subtype == LPFC_NVMET)
15295 rq_create->u.request.context.buffer_size =
15296 LPFC_NVMET_DATA_BUF_SIZE;
15297 else
15298 rq_create->u.request.context.buffer_size =
15299 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015300 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15301 LPFC_RQE_SIZE_8);
15302 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15303 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015304 } else {
15305 switch (drq->entry_count) {
15306 default:
15307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15308 "2536 Unsupported RQ count. (%d)\n",
15309 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015310 if (drq->entry_count < 512) {
15311 status = -EINVAL;
15312 goto out;
15313 }
James Smart5a6f1332011-03-11 16:05:35 -050015314 /* otherwise default to smallest count (drop through) */
15315 case 512:
15316 bf_set(lpfc_rq_context_rqe_count,
15317 &rq_create->u.request.context,
15318 LPFC_RQ_RING_SIZE_512);
15319 break;
15320 case 1024:
15321 bf_set(lpfc_rq_context_rqe_count,
15322 &rq_create->u.request.context,
15323 LPFC_RQ_RING_SIZE_1024);
15324 break;
15325 case 2048:
15326 bf_set(lpfc_rq_context_rqe_count,
15327 &rq_create->u.request.context,
15328 LPFC_RQ_RING_SIZE_2048);
15329 break;
15330 case 4096:
15331 bf_set(lpfc_rq_context_rqe_count,
15332 &rq_create->u.request.context,
15333 LPFC_RQ_RING_SIZE_4096);
15334 break;
15335 }
James Smart3c603be2017-05-15 15:20:44 -070015336 if (subtype == LPFC_NVMET)
15337 bf_set(lpfc_rq_context_buf_size,
15338 &rq_create->u.request.context,
15339 LPFC_NVMET_DATA_BUF_SIZE);
15340 else
15341 bf_set(lpfc_rq_context_buf_size,
15342 &rq_create->u.request.context,
15343 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015344 }
15345 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15346 cq->queue_id);
15347 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15348 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015349 list_for_each_entry(dmabuf, &drq->page_list, list) {
15350 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15351 putPaddrLow(dmabuf->phys);
15352 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15353 putPaddrHigh(dmabuf->phys);
15354 }
James Smart962bc512013-01-03 15:44:00 -050015355 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15356 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015357 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15358 /* The IOCTL status is embedded in the mailbox subheader. */
15359 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15360 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15361 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15362 if (shdr_status || shdr_add_status || rc) {
15363 status = -ENXIO;
15364 goto out;
15365 }
15366 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15367 if (drq->queue_id == 0xFFFF) {
15368 status = -ENXIO;
15369 goto out;
15370 }
15371 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015372 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015373 drq->subtype = subtype;
15374 drq->host_index = 0;
15375 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015376 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015377
15378 /* link the header and data RQs onto the parent cq child list */
15379 list_add_tail(&hrq->list, &cq->child_list);
15380 list_add_tail(&drq->list, &cq->child_list);
15381
15382out:
James Smart8fa38512009-07-19 10:01:03 -040015383 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015384 return status;
15385}
15386
15387/**
James Smart2d7dbc42017-02-12 13:52:35 -080015388 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15389 * @phba: HBA structure that indicates port to create a queue on.
15390 * @hrqp: The queue structure array to use to create the header receive queues.
15391 * @drqp: The queue structure array to use to create the data receive queues.
15392 * @cqp: The completion queue array to bind these receive queues to.
15393 *
15394 * This function creates a receive buffer queue pair , as detailed in @hrq and
15395 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15396 * to the HBA.
15397 *
15398 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15399 * struct is used to get the entry count that is necessary to determine the
15400 * number of pages to use for this queue. The @cq is used to indicate which
15401 * completion queue to bind received buffers that are posted to these queues to.
15402 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15403 * receive queue pair. This function is asynchronous and will wait for the
15404 * mailbox command to finish before continuing.
15405 *
15406 * On success this function will return a zero. If unable to allocate enough
15407 * memory this function will return -ENOMEM. If the queue create mailbox command
15408 * fails this function will return -ENXIO.
15409 **/
15410int
15411lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15412 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15413 uint32_t subtype)
15414{
15415 struct lpfc_queue *hrq, *drq, *cq;
15416 struct lpfc_mbx_rq_create_v2 *rq_create;
15417 struct lpfc_dmabuf *dmabuf;
15418 LPFC_MBOXQ_t *mbox;
15419 int rc, length, alloclen, status = 0;
15420 int cnt, idx, numrq, page_idx = 0;
15421 uint32_t shdr_status, shdr_add_status;
15422 union lpfc_sli4_cfg_shdr *shdr;
15423 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15424
15425 numrq = phba->cfg_nvmet_mrq;
15426 /* sanity check on array memory */
15427 if (!hrqp || !drqp || !cqp || !numrq)
15428 return -ENODEV;
15429 if (!phba->sli4_hba.pc_sli4_params.supported)
15430 hw_page_size = SLI4_PAGE_SIZE;
15431
15432 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15433 if (!mbox)
15434 return -ENOMEM;
15435
15436 length = sizeof(struct lpfc_mbx_rq_create_v2);
15437 length += ((2 * numrq * hrqp[0]->page_count) *
15438 sizeof(struct dma_address));
15439
15440 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15441 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15442 LPFC_SLI4_MBX_NEMBED);
15443 if (alloclen < length) {
15444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15445 "3099 Allocated DMA memory size (%d) is "
15446 "less than the requested DMA memory size "
15447 "(%d)\n", alloclen, length);
15448 status = -ENOMEM;
15449 goto out;
15450 }
15451
15452
15453
15454 rq_create = mbox->sge_array->addr[0];
15455 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15456
15457 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15458 cnt = 0;
15459
15460 for (idx = 0; idx < numrq; idx++) {
15461 hrq = hrqp[idx];
15462 drq = drqp[idx];
15463 cq = cqp[idx];
15464
James Smart2d7dbc42017-02-12 13:52:35 -080015465 /* sanity check on queue memory */
15466 if (!hrq || !drq || !cq) {
15467 status = -ENODEV;
15468 goto out;
15469 }
15470
James Smart7aabe842017-03-04 09:30:22 -080015471 if (hrq->entry_count != drq->entry_count) {
15472 status = -EINVAL;
15473 goto out;
15474 }
15475
James Smart2d7dbc42017-02-12 13:52:35 -080015476 if (idx == 0) {
15477 bf_set(lpfc_mbx_rq_create_num_pages,
15478 &rq_create->u.request,
15479 hrq->page_count);
15480 bf_set(lpfc_mbx_rq_create_rq_cnt,
15481 &rq_create->u.request, (numrq * 2));
15482 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15483 1);
15484 bf_set(lpfc_rq_context_base_cq,
15485 &rq_create->u.request.context,
15486 cq->queue_id);
15487 bf_set(lpfc_rq_context_data_size,
15488 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015489 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015490 bf_set(lpfc_rq_context_hdr_size,
15491 &rq_create->u.request.context,
15492 LPFC_HDR_BUF_SIZE);
15493 bf_set(lpfc_rq_context_rqe_count_1,
15494 &rq_create->u.request.context,
15495 hrq->entry_count);
15496 bf_set(lpfc_rq_context_rqe_size,
15497 &rq_create->u.request.context,
15498 LPFC_RQE_SIZE_8);
15499 bf_set(lpfc_rq_context_page_size,
15500 &rq_create->u.request.context,
15501 (PAGE_SIZE/SLI4_PAGE_SIZE));
15502 }
15503 rc = 0;
15504 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15505 memset(dmabuf->virt, 0, hw_page_size);
15506 cnt = page_idx + dmabuf->buffer_tag;
15507 rq_create->u.request.page[cnt].addr_lo =
15508 putPaddrLow(dmabuf->phys);
15509 rq_create->u.request.page[cnt].addr_hi =
15510 putPaddrHigh(dmabuf->phys);
15511 rc++;
15512 }
15513 page_idx += rc;
15514
15515 rc = 0;
15516 list_for_each_entry(dmabuf, &drq->page_list, list) {
15517 memset(dmabuf->virt, 0, hw_page_size);
15518 cnt = page_idx + dmabuf->buffer_tag;
15519 rq_create->u.request.page[cnt].addr_lo =
15520 putPaddrLow(dmabuf->phys);
15521 rq_create->u.request.page[cnt].addr_hi =
15522 putPaddrHigh(dmabuf->phys);
15523 rc++;
15524 }
15525 page_idx += rc;
15526
15527 hrq->db_format = LPFC_DB_RING_FORMAT;
15528 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15529 hrq->type = LPFC_HRQ;
15530 hrq->assoc_qid = cq->queue_id;
15531 hrq->subtype = subtype;
15532 hrq->host_index = 0;
15533 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015534 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015535
15536 drq->db_format = LPFC_DB_RING_FORMAT;
15537 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15538 drq->type = LPFC_DRQ;
15539 drq->assoc_qid = cq->queue_id;
15540 drq->subtype = subtype;
15541 drq->host_index = 0;
15542 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015543 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015544
15545 list_add_tail(&hrq->list, &cq->child_list);
15546 list_add_tail(&drq->list, &cq->child_list);
15547 }
15548
15549 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15550 /* The IOCTL status is embedded in the mailbox subheader. */
15551 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15552 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15553 if (shdr_status || shdr_add_status || rc) {
15554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15555 "3120 RQ_CREATE mailbox failed with "
15556 "status x%x add_status x%x, mbx status x%x\n",
15557 shdr_status, shdr_add_status, rc);
15558 status = -ENXIO;
15559 goto out;
15560 }
15561 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15562 if (rc == 0xFFFF) {
15563 status = -ENXIO;
15564 goto out;
15565 }
15566
15567 /* Initialize all RQs with associated queue id */
15568 for (idx = 0; idx < numrq; idx++) {
15569 hrq = hrqp[idx];
15570 hrq->queue_id = rc + (2 * idx);
15571 drq = drqp[idx];
15572 drq->queue_id = rc + (2 * idx) + 1;
15573 }
15574
15575out:
15576 lpfc_sli4_mbox_cmd_free(phba, mbox);
15577 return status;
15578}
15579
15580/**
James Smart4f774512009-05-22 14:52:35 -040015581 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15582 * @eq: The queue structure associated with the queue to destroy.
15583 *
15584 * This function destroys a queue, as detailed in @eq by sending an mailbox
15585 * command, specific to the type of queue, to the HBA.
15586 *
15587 * The @eq struct is used to get the queue ID of the queue to destroy.
15588 *
15589 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015590 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015591 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015592int
James Smart4f774512009-05-22 14:52:35 -040015593lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15594{
15595 LPFC_MBOXQ_t *mbox;
15596 int rc, length, status = 0;
15597 uint32_t shdr_status, shdr_add_status;
15598 union lpfc_sli4_cfg_shdr *shdr;
15599
James Smart2e90f4b2011-12-13 13:22:37 -050015600 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015601 if (!eq)
15602 return -ENODEV;
15603 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15604 if (!mbox)
15605 return -ENOMEM;
15606 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15607 sizeof(struct lpfc_sli4_cfg_mhdr));
15608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15609 LPFC_MBOX_OPCODE_EQ_DESTROY,
15610 length, LPFC_SLI4_MBX_EMBED);
15611 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15612 eq->queue_id);
15613 mbox->vport = eq->phba->pport;
15614 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15615
15616 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15617 /* The IOCTL status is embedded in the mailbox subheader. */
15618 shdr = (union lpfc_sli4_cfg_shdr *)
15619 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15620 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15621 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15622 if (shdr_status || shdr_add_status || rc) {
15623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15624 "2505 EQ_DESTROY mailbox failed with "
15625 "status x%x add_status x%x, mbx status x%x\n",
15626 shdr_status, shdr_add_status, rc);
15627 status = -ENXIO;
15628 }
15629
15630 /* Remove eq from any list */
15631 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015632 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015633 return status;
15634}
15635
15636/**
15637 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15638 * @cq: The queue structure associated with the queue to destroy.
15639 *
15640 * This function destroys a queue, as detailed in @cq by sending an mailbox
15641 * command, specific to the type of queue, to the HBA.
15642 *
15643 * The @cq struct is used to get the queue ID of the queue to destroy.
15644 *
15645 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015646 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015647 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015648int
James Smart4f774512009-05-22 14:52:35 -040015649lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15650{
15651 LPFC_MBOXQ_t *mbox;
15652 int rc, length, status = 0;
15653 uint32_t shdr_status, shdr_add_status;
15654 union lpfc_sli4_cfg_shdr *shdr;
15655
James Smart2e90f4b2011-12-13 13:22:37 -050015656 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015657 if (!cq)
15658 return -ENODEV;
15659 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15660 if (!mbox)
15661 return -ENOMEM;
15662 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15663 sizeof(struct lpfc_sli4_cfg_mhdr));
15664 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15665 LPFC_MBOX_OPCODE_CQ_DESTROY,
15666 length, LPFC_SLI4_MBX_EMBED);
15667 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15668 cq->queue_id);
15669 mbox->vport = cq->phba->pport;
15670 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15671 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15672 /* The IOCTL status is embedded in the mailbox subheader. */
15673 shdr = (union lpfc_sli4_cfg_shdr *)
15674 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15675 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15676 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15677 if (shdr_status || shdr_add_status || rc) {
15678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15679 "2506 CQ_DESTROY mailbox failed with "
15680 "status x%x add_status x%x, mbx status x%x\n",
15681 shdr_status, shdr_add_status, rc);
15682 status = -ENXIO;
15683 }
15684 /* Remove cq from any list */
15685 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015686 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015687 return status;
15688}
15689
15690/**
James Smart04c68492009-05-22 14:52:52 -040015691 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15692 * @qm: The queue structure associated with the queue to destroy.
15693 *
15694 * This function destroys a queue, as detailed in @mq by sending an mailbox
15695 * command, specific to the type of queue, to the HBA.
15696 *
15697 * The @mq struct is used to get the queue ID of the queue to destroy.
15698 *
15699 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015700 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015701 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015702int
James Smart04c68492009-05-22 14:52:52 -040015703lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15704{
15705 LPFC_MBOXQ_t *mbox;
15706 int rc, length, status = 0;
15707 uint32_t shdr_status, shdr_add_status;
15708 union lpfc_sli4_cfg_shdr *shdr;
15709
James Smart2e90f4b2011-12-13 13:22:37 -050015710 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015711 if (!mq)
15712 return -ENODEV;
15713 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15714 if (!mbox)
15715 return -ENOMEM;
15716 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15717 sizeof(struct lpfc_sli4_cfg_mhdr));
15718 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15719 LPFC_MBOX_OPCODE_MQ_DESTROY,
15720 length, LPFC_SLI4_MBX_EMBED);
15721 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15722 mq->queue_id);
15723 mbox->vport = mq->phba->pport;
15724 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15725 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15726 /* The IOCTL status is embedded in the mailbox subheader. */
15727 shdr = (union lpfc_sli4_cfg_shdr *)
15728 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15729 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15730 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15731 if (shdr_status || shdr_add_status || rc) {
15732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15733 "2507 MQ_DESTROY mailbox failed with "
15734 "status x%x add_status x%x, mbx status x%x\n",
15735 shdr_status, shdr_add_status, rc);
15736 status = -ENXIO;
15737 }
15738 /* Remove mq from any list */
15739 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015740 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015741 return status;
15742}
15743
15744/**
James Smart4f774512009-05-22 14:52:35 -040015745 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15746 * @wq: The queue structure associated with the queue to destroy.
15747 *
15748 * This function destroys a queue, as detailed in @wq by sending an mailbox
15749 * command, specific to the type of queue, to the HBA.
15750 *
15751 * The @wq struct is used to get the queue ID of the queue to destroy.
15752 *
15753 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015754 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015755 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015756int
James Smart4f774512009-05-22 14:52:35 -040015757lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15758{
15759 LPFC_MBOXQ_t *mbox;
15760 int rc, length, status = 0;
15761 uint32_t shdr_status, shdr_add_status;
15762 union lpfc_sli4_cfg_shdr *shdr;
15763
James Smart2e90f4b2011-12-13 13:22:37 -050015764 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015765 if (!wq)
15766 return -ENODEV;
15767 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15768 if (!mbox)
15769 return -ENOMEM;
15770 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15771 sizeof(struct lpfc_sli4_cfg_mhdr));
15772 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15773 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15774 length, LPFC_SLI4_MBX_EMBED);
15775 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15776 wq->queue_id);
15777 mbox->vport = wq->phba->pport;
15778 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15779 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15780 shdr = (union lpfc_sli4_cfg_shdr *)
15781 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15782 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15783 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15784 if (shdr_status || shdr_add_status || rc) {
15785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15786 "2508 WQ_DESTROY mailbox failed with "
15787 "status x%x add_status x%x, mbx status x%x\n",
15788 shdr_status, shdr_add_status, rc);
15789 status = -ENXIO;
15790 }
15791 /* Remove wq from any list */
15792 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015793 kfree(wq->pring);
15794 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015795 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015796 return status;
15797}
15798
15799/**
15800 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15801 * @rq: The queue structure associated with the queue to destroy.
15802 *
15803 * This function destroys a queue, as detailed in @rq by sending an mailbox
15804 * command, specific to the type of queue, to the HBA.
15805 *
15806 * The @rq struct is used to get the queue ID of the queue to destroy.
15807 *
15808 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015809 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015810 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015811int
James Smart4f774512009-05-22 14:52:35 -040015812lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15813 struct lpfc_queue *drq)
15814{
15815 LPFC_MBOXQ_t *mbox;
15816 int rc, length, status = 0;
15817 uint32_t shdr_status, shdr_add_status;
15818 union lpfc_sli4_cfg_shdr *shdr;
15819
James Smart2e90f4b2011-12-13 13:22:37 -050015820 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015821 if (!hrq || !drq)
15822 return -ENODEV;
15823 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15824 if (!mbox)
15825 return -ENOMEM;
15826 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015827 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015828 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15829 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15830 length, LPFC_SLI4_MBX_EMBED);
15831 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15832 hrq->queue_id);
15833 mbox->vport = hrq->phba->pport;
15834 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15835 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15836 /* The IOCTL status is embedded in the mailbox subheader. */
15837 shdr = (union lpfc_sli4_cfg_shdr *)
15838 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15839 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15840 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15841 if (shdr_status || shdr_add_status || rc) {
15842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15843 "2509 RQ_DESTROY mailbox failed with "
15844 "status x%x add_status x%x, mbx status x%x\n",
15845 shdr_status, shdr_add_status, rc);
15846 if (rc != MBX_TIMEOUT)
15847 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15848 return -ENXIO;
15849 }
15850 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15851 drq->queue_id);
15852 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15853 shdr = (union lpfc_sli4_cfg_shdr *)
15854 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15855 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15856 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15857 if (shdr_status || shdr_add_status || rc) {
15858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15859 "2510 RQ_DESTROY mailbox failed with "
15860 "status x%x add_status x%x, mbx status x%x\n",
15861 shdr_status, shdr_add_status, rc);
15862 status = -ENXIO;
15863 }
15864 list_del_init(&hrq->list);
15865 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015866 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015867 return status;
15868}
15869
15870/**
15871 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15872 * @phba: The virtual port for which this call being executed.
15873 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15874 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15875 * @xritag: the xritag that ties this io to the SGL pages.
15876 *
15877 * This routine will post the sgl pages for the IO that has the xritag
15878 * that is in the iocbq structure. The xritag is assigned during iocbq
15879 * creation and persists for as long as the driver is loaded.
15880 * if the caller has fewer than 256 scatter gather segments to map then
15881 * pdma_phys_addr1 should be 0.
15882 * If the caller needs to map more than 256 scatter gather segment then
15883 * pdma_phys_addr1 should be a valid physical address.
15884 * physical address for SGLs must be 64 byte aligned.
15885 * If you are going to map 2 SGL's then the first one must have 256 entries
15886 * the second sgl can have between 1 and 256 entries.
15887 *
15888 * Return codes:
15889 * 0 - Success
15890 * -ENXIO, -ENOMEM - Failure
15891 **/
15892int
15893lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15894 dma_addr_t pdma_phys_addr0,
15895 dma_addr_t pdma_phys_addr1,
15896 uint16_t xritag)
15897{
15898 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15899 LPFC_MBOXQ_t *mbox;
15900 int rc;
15901 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015902 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015903 union lpfc_sli4_cfg_shdr *shdr;
15904
15905 if (xritag == NO_XRI) {
15906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15907 "0364 Invalid param:\n");
15908 return -EINVAL;
15909 }
15910
15911 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15912 if (!mbox)
15913 return -ENOMEM;
15914
15915 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15916 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15917 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015918 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015919
15920 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15921 &mbox->u.mqe.un.post_sgl_pages;
15922 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15923 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15924
15925 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15926 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15927 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15928 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15929
15930 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15931 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15932 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15933 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15934 if (!phba->sli4_hba.intr_enable)
15935 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015936 else {
James Smarta183a152011-10-10 21:32:43 -040015937 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015938 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15939 }
James Smart4f774512009-05-22 14:52:35 -040015940 /* The IOCTL status is embedded in the mailbox subheader. */
15941 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15942 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15943 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15944 if (rc != MBX_TIMEOUT)
15945 mempool_free(mbox, phba->mbox_mem_pool);
15946 if (shdr_status || shdr_add_status || rc) {
15947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15948 "2511 POST_SGL mailbox failed with "
15949 "status x%x add_status x%x, mbx status x%x\n",
15950 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015951 }
15952 return 0;
15953}
James Smart4f774512009-05-22 14:52:35 -040015954
15955/**
James Smart88a2cfb2011-07-22 18:36:33 -040015956 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015957 * @phba: pointer to lpfc hba data structure.
15958 *
15959 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015960 * HBA consistent with the SLI-4 interface spec. This routine
15961 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15962 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015963 *
James Smart88a2cfb2011-07-22 18:36:33 -040015964 * Returns
15965 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15966 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15967 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015968static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015969lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15970{
15971 unsigned long xri;
15972
15973 /*
15974 * Fetch the next logical xri. Because this index is logical,
15975 * the driver starts at 0 each time.
15976 */
15977 spin_lock_irq(&phba->hbalock);
15978 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15979 phba->sli4_hba.max_cfg_param.max_xri, 0);
15980 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15981 spin_unlock_irq(&phba->hbalock);
15982 return NO_XRI;
15983 } else {
15984 set_bit(xri, phba->sli4_hba.xri_bmask);
15985 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015986 }
James Smart6d368e52011-05-24 11:44:12 -040015987 spin_unlock_irq(&phba->hbalock);
15988 return xri;
15989}
15990
15991/**
15992 * lpfc_sli4_free_xri - Release an xri for reuse.
15993 * @phba: pointer to lpfc hba data structure.
15994 *
15995 * This routine is invoked to release an xri to the pool of
15996 * available rpis maintained by the driver.
15997 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015998static void
James Smart6d368e52011-05-24 11:44:12 -040015999__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16000{
16001 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016002 phba->sli4_hba.max_cfg_param.xri_used--;
16003 }
16004}
16005
16006/**
16007 * lpfc_sli4_free_xri - Release an xri for reuse.
16008 * @phba: pointer to lpfc hba data structure.
16009 *
16010 * This routine is invoked to release an xri to the pool of
16011 * available rpis maintained by the driver.
16012 **/
16013void
16014lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16015{
16016 spin_lock_irq(&phba->hbalock);
16017 __lpfc_sli4_free_xri(phba, xri);
16018 spin_unlock_irq(&phba->hbalock);
16019}
16020
16021/**
James Smart4f774512009-05-22 14:52:35 -040016022 * lpfc_sli4_next_xritag - Get an xritag for the io
16023 * @phba: Pointer to HBA context object.
16024 *
16025 * This function gets an xritag for the iocb. If there is no unused xritag
16026 * it will return 0xffff.
16027 * The function returns the allocated xritag if successful, else returns zero.
16028 * Zero is not a valid xritag.
16029 * The caller is not required to hold any lock.
16030 **/
16031uint16_t
16032lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16033{
James Smart6d368e52011-05-24 11:44:12 -040016034 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016035
James Smart6d368e52011-05-24 11:44:12 -040016036 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016037 if (xri_index == NO_XRI)
16038 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16039 "2004 Failed to allocate XRI.last XRITAG is %d"
16040 " Max XRI is %d, Used XRI is %d\n",
16041 xri_index,
16042 phba->sli4_hba.max_cfg_param.max_xri,
16043 phba->sli4_hba.max_cfg_param.xri_used);
16044 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016045}
16046
16047/**
James Smart895427b2017-02-12 13:52:30 -080016048 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016049 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016050 * @post_sgl_list: pointer to els sgl entry list.
16051 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016052 *
16053 * This routine is invoked to post a block of driver's sgl pages to the
16054 * HBA using non-embedded mailbox command. No Lock is held. This routine
16055 * is only called when the driver is loading and after all IO has been
16056 * stopped.
16057 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016058static int
James Smart895427b2017-02-12 13:52:30 -080016059lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016060 struct list_head *post_sgl_list,
16061 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016062{
James Smart8a9d2e82012-05-09 21:16:12 -040016063 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016064 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16065 struct sgl_page_pairs *sgl_pg_pairs;
16066 void *viraddr;
16067 LPFC_MBOXQ_t *mbox;
16068 uint32_t reqlen, alloclen, pg_pairs;
16069 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016070 uint16_t xritag_start = 0;
16071 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016072 uint32_t shdr_status, shdr_add_status;
16073 union lpfc_sli4_cfg_shdr *shdr;
16074
James Smart895427b2017-02-12 13:52:30 -080016075 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016076 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016077 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016079 "2559 Block sgl registration required DMA "
16080 "size (%d) great than a page\n", reqlen);
16081 return -ENOMEM;
16082 }
James Smart895427b2017-02-12 13:52:30 -080016083
James Smart4f774512009-05-22 14:52:35 -040016084 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016085 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016086 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016087
16088 /* Allocate DMA memory and set up the non-embedded mailbox command */
16089 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16090 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16091 LPFC_SLI4_MBX_NEMBED);
16092
16093 if (alloclen < reqlen) {
16094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16095 "0285 Allocated DMA memory size (%d) is "
16096 "less than the requested DMA memory "
16097 "size (%d)\n", alloclen, reqlen);
16098 lpfc_sli4_mbox_cmd_free(phba, mbox);
16099 return -ENOMEM;
16100 }
James Smart4f774512009-05-22 14:52:35 -040016101 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016102 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016103 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16104 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16105
James Smart8a9d2e82012-05-09 21:16:12 -040016106 pg_pairs = 0;
16107 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016108 /* Set up the sge entry */
16109 sgl_pg_pairs->sgl_pg0_addr_lo =
16110 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16111 sgl_pg_pairs->sgl_pg0_addr_hi =
16112 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16113 sgl_pg_pairs->sgl_pg1_addr_lo =
16114 cpu_to_le32(putPaddrLow(0));
16115 sgl_pg_pairs->sgl_pg1_addr_hi =
16116 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016117
James Smart4f774512009-05-22 14:52:35 -040016118 /* Keep the first xritag on the list */
16119 if (pg_pairs == 0)
16120 xritag_start = sglq_entry->sli4_xritag;
16121 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016122 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016123 }
James Smart6d368e52011-05-24 11:44:12 -040016124
16125 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016126 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016127 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016128 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016129
James Smart4f774512009-05-22 14:52:35 -040016130 if (!phba->sli4_hba.intr_enable)
16131 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16132 else {
James Smarta183a152011-10-10 21:32:43 -040016133 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016134 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16135 }
16136 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16137 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16138 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16139 if (rc != MBX_TIMEOUT)
16140 lpfc_sli4_mbox_cmd_free(phba, mbox);
16141 if (shdr_status || shdr_add_status || rc) {
16142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16143 "2513 POST_SGL_BLOCK mailbox command failed "
16144 "status x%x add_status x%x mbx status x%x\n",
16145 shdr_status, shdr_add_status, rc);
16146 rc = -ENXIO;
16147 }
16148 return rc;
16149}
16150
16151/**
16152 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16153 * @phba: pointer to lpfc hba data structure.
16154 * @sblist: pointer to scsi buffer list.
16155 * @count: number of scsi buffers on the list.
16156 *
16157 * This routine is invoked to post a block of @count scsi sgl pages from a
16158 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16159 * No Lock is held.
16160 *
16161 **/
16162int
James Smart8a9d2e82012-05-09 21:16:12 -040016163lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16164 struct list_head *sblist,
16165 int count)
James Smart4f774512009-05-22 14:52:35 -040016166{
16167 struct lpfc_scsi_buf *psb;
16168 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16169 struct sgl_page_pairs *sgl_pg_pairs;
16170 void *viraddr;
16171 LPFC_MBOXQ_t *mbox;
16172 uint32_t reqlen, alloclen, pg_pairs;
16173 uint32_t mbox_tmo;
16174 uint16_t xritag_start = 0;
16175 int rc = 0;
16176 uint32_t shdr_status, shdr_add_status;
16177 dma_addr_t pdma_phys_bpl1;
16178 union lpfc_sli4_cfg_shdr *shdr;
16179
16180 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016181 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016182 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016183 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016184 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16185 "0217 Block sgl registration required DMA "
16186 "size (%d) great than a page\n", reqlen);
16187 return -ENOMEM;
16188 }
16189 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16190 if (!mbox) {
16191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16192 "0283 Failed to allocate mbox cmd memory\n");
16193 return -ENOMEM;
16194 }
16195
16196 /* Allocate DMA memory and set up the non-embedded mailbox command */
16197 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16198 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16199 LPFC_SLI4_MBX_NEMBED);
16200
16201 if (alloclen < reqlen) {
16202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16203 "2561 Allocated DMA memory size (%d) is "
16204 "less than the requested DMA memory "
16205 "size (%d)\n", alloclen, reqlen);
16206 lpfc_sli4_mbox_cmd_free(phba, mbox);
16207 return -ENOMEM;
16208 }
James Smart6d368e52011-05-24 11:44:12 -040016209
James Smart4f774512009-05-22 14:52:35 -040016210 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016211 viraddr = mbox->sge_array->addr[0];
16212
16213 /* Set up the SGL pages in the non-embedded DMA pages */
16214 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16215 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16216
16217 pg_pairs = 0;
16218 list_for_each_entry(psb, sblist, list) {
16219 /* Set up the sge entry */
16220 sgl_pg_pairs->sgl_pg0_addr_lo =
16221 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16222 sgl_pg_pairs->sgl_pg0_addr_hi =
16223 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16224 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16225 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16226 else
16227 pdma_phys_bpl1 = 0;
16228 sgl_pg_pairs->sgl_pg1_addr_lo =
16229 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16230 sgl_pg_pairs->sgl_pg1_addr_hi =
16231 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16232 /* Keep the first xritag on the list */
16233 if (pg_pairs == 0)
16234 xritag_start = psb->cur_iocbq.sli4_xritag;
16235 sgl_pg_pairs++;
16236 pg_pairs++;
16237 }
16238 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16239 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16240 /* Perform endian conversion if necessary */
16241 sgl->word0 = cpu_to_le32(sgl->word0);
16242
16243 if (!phba->sli4_hba.intr_enable)
16244 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16245 else {
James Smarta183a152011-10-10 21:32:43 -040016246 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016247 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16248 }
16249 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16250 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16251 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16252 if (rc != MBX_TIMEOUT)
16253 lpfc_sli4_mbox_cmd_free(phba, mbox);
16254 if (shdr_status || shdr_add_status || rc) {
16255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16256 "2564 POST_SGL_BLOCK mailbox command failed "
16257 "status x%x add_status x%x mbx status x%x\n",
16258 shdr_status, shdr_add_status, rc);
16259 rc = -ENXIO;
16260 }
16261 return rc;
16262}
16263
16264/**
16265 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16266 * @phba: pointer to lpfc_hba struct that the frame was received on
16267 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16268 *
16269 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16270 * valid type of frame that the LPFC driver will handle. This function will
16271 * return a zero if the frame is a valid frame or a non zero value when the
16272 * frame does not pass the check.
16273 **/
16274static int
16275lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16276{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016277 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016278 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016279 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016280
James Smartae9e28f2017-05-15 15:20:51 -070016281#define FC_RCTL_MDS_DIAGS 0xF4
16282
James Smart4f774512009-05-22 14:52:35 -040016283 switch (fc_hdr->fh_r_ctl) {
16284 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16285 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16286 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16287 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16288 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16289 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16290 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16291 case FC_RCTL_DD_CMD_STATUS: /* command status */
16292 case FC_RCTL_ELS_REQ: /* extended link services request */
16293 case FC_RCTL_ELS_REP: /* extended link services reply */
16294 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16295 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16296 case FC_RCTL_BA_NOP: /* basic link service NOP */
16297 case FC_RCTL_BA_ABTS: /* basic link service abort */
16298 case FC_RCTL_BA_RMC: /* remove connection */
16299 case FC_RCTL_BA_ACC: /* basic accept */
16300 case FC_RCTL_BA_RJT: /* basic reject */
16301 case FC_RCTL_BA_PRMT:
16302 case FC_RCTL_ACK_1: /* acknowledge_1 */
16303 case FC_RCTL_ACK_0: /* acknowledge_0 */
16304 case FC_RCTL_P_RJT: /* port reject */
16305 case FC_RCTL_F_RJT: /* fabric reject */
16306 case FC_RCTL_P_BSY: /* port busy */
16307 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16308 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16309 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016310 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016311 case FC_RCTL_END: /* end */
16312 break;
16313 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16314 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16315 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16316 return lpfc_fc_frame_check(phba, fc_hdr);
16317 default:
16318 goto drop;
16319 }
James Smartae9e28f2017-05-15 15:20:51 -070016320
16321#define FC_TYPE_VENDOR_UNIQUE 0xFF
16322
James Smart4f774512009-05-22 14:52:35 -040016323 switch (fc_hdr->fh_type) {
16324 case FC_TYPE_BLS:
16325 case FC_TYPE_ELS:
16326 case FC_TYPE_FCP:
16327 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016328 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016329 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016330 break;
16331 case FC_TYPE_IP:
16332 case FC_TYPE_ILS:
16333 default:
16334 goto drop;
16335 }
James Smart546fc852011-03-11 16:06:29 -050016336
James Smart4f774512009-05-22 14:52:35 -040016337 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016338 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016339 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016340 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16341 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16342 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16343 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16344 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016345 return 0;
16346drop:
16347 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016348 "2539 Dropped frame rctl:x%x type:x%x\n",
16349 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016350 return 1;
16351}
16352
16353/**
16354 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16355 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16356 *
16357 * This function processes the FC header to retrieve the VFI from the VF
16358 * header, if one exists. This function will return the VFI if one exists
16359 * or 0 if no VSAN Header exists.
16360 **/
16361static uint32_t
16362lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16363{
16364 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16365
16366 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16367 return 0;
16368 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16369}
16370
16371/**
16372 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16373 * @phba: Pointer to the HBA structure to search for the vport on
16374 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16375 * @fcfi: The FC Fabric ID that the frame came from
16376 *
16377 * This function searches the @phba for a vport that matches the content of the
16378 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16379 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16380 * returns the matching vport pointer or NULL if unable to match frame to a
16381 * vport.
16382 **/
16383static struct lpfc_vport *
16384lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016385 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016386{
16387 struct lpfc_vport **vports;
16388 struct lpfc_vport *vport = NULL;
16389 int i;
James Smart939723a2012-05-09 21:19:03 -040016390
James Smartbf086112011-08-21 21:48:13 -040016391 if (did == Fabric_DID)
16392 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016393 if ((phba->pport->fc_flag & FC_PT2PT) &&
16394 !(phba->link_state == LPFC_HBA_READY))
16395 return phba->pport;
16396
James Smart4f774512009-05-22 14:52:35 -040016397 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016398 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016399 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16400 if (phba->fcf.fcfi == fcfi &&
16401 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16402 vports[i]->fc_myDID == did) {
16403 vport = vports[i];
16404 break;
16405 }
16406 }
James Smart895427b2017-02-12 13:52:30 -080016407 }
James Smart4f774512009-05-22 14:52:35 -040016408 lpfc_destroy_vport_work_array(phba, vports);
16409 return vport;
16410}
16411
16412/**
James Smart45ed1192009-10-02 15:17:02 -040016413 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16414 * @vport: The vport to work on.
16415 *
16416 * This function updates the receive sequence time stamp for this vport. The
16417 * receive sequence time stamp indicates the time that the last frame of the
16418 * the sequence that has been idle for the longest amount of time was received.
16419 * the driver uses this time stamp to indicate if any received sequences have
16420 * timed out.
16421 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016422static void
James Smart45ed1192009-10-02 15:17:02 -040016423lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16424{
16425 struct lpfc_dmabuf *h_buf;
16426 struct hbq_dmabuf *dmabuf = NULL;
16427
16428 /* get the oldest sequence on the rcv list */
16429 h_buf = list_get_first(&vport->rcv_buffer_list,
16430 struct lpfc_dmabuf, list);
16431 if (!h_buf)
16432 return;
16433 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16434 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16435}
16436
16437/**
16438 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16439 * @vport: The vport that the received sequences were sent to.
16440 *
16441 * This function cleans up all outstanding received sequences. This is called
16442 * by the driver when a link event or user action invalidates all the received
16443 * sequences.
16444 **/
16445void
16446lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16447{
16448 struct lpfc_dmabuf *h_buf, *hnext;
16449 struct lpfc_dmabuf *d_buf, *dnext;
16450 struct hbq_dmabuf *dmabuf = NULL;
16451
16452 /* start with the oldest sequence on the rcv list */
16453 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16454 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16455 list_del_init(&dmabuf->hbuf.list);
16456 list_for_each_entry_safe(d_buf, dnext,
16457 &dmabuf->dbuf.list, list) {
16458 list_del_init(&d_buf->list);
16459 lpfc_in_buf_free(vport->phba, d_buf);
16460 }
16461 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16462 }
16463}
16464
16465/**
16466 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16467 * @vport: The vport that the received sequences were sent to.
16468 *
16469 * This function determines whether any received sequences have timed out by
16470 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16471 * indicates that there is at least one timed out sequence this routine will
16472 * go through the received sequences one at a time from most inactive to most
16473 * active to determine which ones need to be cleaned up. Once it has determined
16474 * that a sequence needs to be cleaned up it will simply free up the resources
16475 * without sending an abort.
16476 **/
16477void
16478lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16479{
16480 struct lpfc_dmabuf *h_buf, *hnext;
16481 struct lpfc_dmabuf *d_buf, *dnext;
16482 struct hbq_dmabuf *dmabuf = NULL;
16483 unsigned long timeout;
16484 int abort_count = 0;
16485
16486 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16487 vport->rcv_buffer_time_stamp);
16488 if (list_empty(&vport->rcv_buffer_list) ||
16489 time_before(jiffies, timeout))
16490 return;
16491 /* start with the oldest sequence on the rcv list */
16492 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16493 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16494 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16495 dmabuf->time_stamp);
16496 if (time_before(jiffies, timeout))
16497 break;
16498 abort_count++;
16499 list_del_init(&dmabuf->hbuf.list);
16500 list_for_each_entry_safe(d_buf, dnext,
16501 &dmabuf->dbuf.list, list) {
16502 list_del_init(&d_buf->list);
16503 lpfc_in_buf_free(vport->phba, d_buf);
16504 }
16505 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16506 }
16507 if (abort_count)
16508 lpfc_update_rcv_time_stamp(vport);
16509}
16510
16511/**
James Smart4f774512009-05-22 14:52:35 -040016512 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16513 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16514 *
16515 * This function searches through the existing incomplete sequences that have
16516 * been sent to this @vport. If the frame matches one of the incomplete
16517 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16518 * make up that sequence. If no sequence is found that matches this frame then
16519 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16520 * This function returns a pointer to the first dmabuf in the sequence list that
16521 * the frame was linked to.
16522 **/
16523static struct hbq_dmabuf *
16524lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16525{
16526 struct fc_frame_header *new_hdr;
16527 struct fc_frame_header *temp_hdr;
16528 struct lpfc_dmabuf *d_buf;
16529 struct lpfc_dmabuf *h_buf;
16530 struct hbq_dmabuf *seq_dmabuf = NULL;
16531 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016532 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016533
James Smart4d9ab992009-10-02 15:16:39 -040016534 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016535 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016536 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016537
James Smart4f774512009-05-22 14:52:35 -040016538 /* Use the hdr_buf to find the sequence that this frame belongs to */
16539 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16540 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16541 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16542 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16543 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16544 continue;
16545 /* found a pending sequence that matches this frame */
16546 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16547 break;
16548 }
16549 if (!seq_dmabuf) {
16550 /*
16551 * This indicates first frame received for this sequence.
16552 * Queue the buffer on the vport's rcv_buffer_list.
16553 */
16554 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016555 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016556 return dmabuf;
16557 }
16558 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016559 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16560 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016561 list_del_init(&seq_dmabuf->hbuf.list);
16562 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16563 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016564 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016565 return dmabuf;
16566 }
James Smart45ed1192009-10-02 15:17:02 -040016567 /* move this sequence to the tail to indicate a young sequence */
16568 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16569 seq_dmabuf->time_stamp = jiffies;
16570 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016571 if (list_empty(&seq_dmabuf->dbuf.list)) {
16572 temp_hdr = dmabuf->hbuf.virt;
16573 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16574 return seq_dmabuf;
16575 }
James Smart4f774512009-05-22 14:52:35 -040016576 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016577 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16578 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016579 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16580 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16581 /*
16582 * If the frame's sequence count is greater than the frame on
16583 * the list then insert the frame right after this frame
16584 */
James Smarteeead812009-12-21 17:01:23 -050016585 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16586 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016587 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016588 found = 1;
16589 break;
James Smart4f774512009-05-22 14:52:35 -040016590 }
James Smart4360ca92015-12-16 18:12:04 -050016591
16592 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16593 break;
16594 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016595 }
James Smart4360ca92015-12-16 18:12:04 -050016596
16597 if (found)
16598 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016599 return NULL;
16600}
16601
16602/**
James Smart6669f9b2009-10-02 15:16:45 -040016603 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16604 * @vport: pointer to a vitural port
16605 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16606 *
16607 * This function tries to abort from the partially assembed sequence, described
16608 * by the information from basic abbort @dmabuf. It checks to see whether such
16609 * partially assembled sequence held by the driver. If so, it shall free up all
16610 * the frames from the partially assembled sequence.
16611 *
16612 * Return
16613 * true -- if there is matching partially assembled sequence present and all
16614 * the frames freed with the sequence;
16615 * false -- if there is no matching partially assembled sequence present so
16616 * nothing got aborted in the lower layer driver
16617 **/
16618static bool
16619lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16620 struct hbq_dmabuf *dmabuf)
16621{
16622 struct fc_frame_header *new_hdr;
16623 struct fc_frame_header *temp_hdr;
16624 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16625 struct hbq_dmabuf *seq_dmabuf = NULL;
16626
16627 /* Use the hdr_buf to find the sequence that matches this frame */
16628 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16629 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16630 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16631 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16632 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16633 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16634 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16635 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16636 continue;
16637 /* found a pending sequence that matches this frame */
16638 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16639 break;
16640 }
16641
16642 /* Free up all the frames from the partially assembled sequence */
16643 if (seq_dmabuf) {
16644 list_for_each_entry_safe(d_buf, n_buf,
16645 &seq_dmabuf->dbuf.list, list) {
16646 list_del_init(&d_buf->list);
16647 lpfc_in_buf_free(vport->phba, d_buf);
16648 }
16649 return true;
16650 }
16651 return false;
16652}
16653
16654/**
James Smart6dd9e312013-01-03 15:43:37 -050016655 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16656 * @vport: pointer to a vitural port
16657 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16658 *
16659 * This function tries to abort from the assembed sequence from upper level
16660 * protocol, described by the information from basic abbort @dmabuf. It
16661 * checks to see whether such pending context exists at upper level protocol.
16662 * If so, it shall clean up the pending context.
16663 *
16664 * Return
16665 * true -- if there is matching pending context of the sequence cleaned
16666 * at ulp;
16667 * false -- if there is no matching pending context of the sequence present
16668 * at ulp.
16669 **/
16670static bool
16671lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16672{
16673 struct lpfc_hba *phba = vport->phba;
16674 int handled;
16675
16676 /* Accepting abort at ulp with SLI4 only */
16677 if (phba->sli_rev < LPFC_SLI_REV4)
16678 return false;
16679
16680 /* Register all caring upper level protocols to attend abort */
16681 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16682 if (handled)
16683 return true;
16684
16685 return false;
16686}
16687
16688/**
James Smart546fc852011-03-11 16:06:29 -050016689 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016690 * @phba: Pointer to HBA context object.
16691 * @cmd_iocbq: pointer to the command iocbq structure.
16692 * @rsp_iocbq: pointer to the response iocbq structure.
16693 *
James Smart546fc852011-03-11 16:06:29 -050016694 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016695 * event. It properly releases the memory allocated to the sequence abort
16696 * accept iocb.
16697 **/
16698static void
James Smart546fc852011-03-11 16:06:29 -050016699lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016700 struct lpfc_iocbq *cmd_iocbq,
16701 struct lpfc_iocbq *rsp_iocbq)
16702{
James Smart6dd9e312013-01-03 15:43:37 -050016703 struct lpfc_nodelist *ndlp;
16704
16705 if (cmd_iocbq) {
16706 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16707 lpfc_nlp_put(ndlp);
16708 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016709 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016710 }
James Smart6b5151f2012-01-18 16:24:06 -050016711
16712 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16713 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16715 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16716 rsp_iocbq->iocb.ulpStatus,
16717 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016718}
16719
16720/**
James Smart6d368e52011-05-24 11:44:12 -040016721 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16722 * @phba: Pointer to HBA context object.
16723 * @xri: xri id in transaction.
16724 *
16725 * This function validates the xri maps to the known range of XRIs allocated an
16726 * used by the driver.
16727 **/
James Smart7851fe22011-07-22 18:36:52 -040016728uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016729lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16730 uint16_t xri)
16731{
James Smarta2fc4aef2014-09-03 12:57:55 -040016732 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016733
16734 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16735 if (xri == phba->sli4_hba.xri_ids[i])
16736 return i;
16737 }
16738 return NO_XRI;
16739}
16740
James Smart6d368e52011-05-24 11:44:12 -040016741/**
James Smart546fc852011-03-11 16:06:29 -050016742 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016743 * @phba: Pointer to HBA context object.
16744 * @fc_hdr: pointer to a FC frame header.
16745 *
James Smart546fc852011-03-11 16:06:29 -050016746 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016747 * event after aborting the sequence handling.
16748 **/
James Smart86c67372017-04-21 16:05:04 -070016749void
James Smart6dd9e312013-01-03 15:43:37 -050016750lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16751 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016752{
James Smart6dd9e312013-01-03 15:43:37 -050016753 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016754 struct lpfc_iocbq *ctiocb = NULL;
16755 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016756 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016757 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016758 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016759 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016760
16761 if (!lpfc_is_link_up(phba))
16762 return;
16763
16764 sid = sli4_sid_from_fc_hdr(fc_hdr);
16765 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016766 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016767
James Smart6dd9e312013-01-03 15:43:37 -050016768 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016769 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016770 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016771 if (!ndlp) {
16772 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16773 "1268 Failed to allocate ndlp for "
16774 "oxid:x%x SID:x%x\n", oxid, sid);
16775 return;
16776 }
James Smart6dd9e312013-01-03 15:43:37 -050016777 /* Put ndlp onto pport node list */
16778 lpfc_enqueue_node(vport, ndlp);
16779 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16780 /* re-setup ndlp without removing from node list */
16781 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16782 if (!ndlp) {
16783 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16784 "3275 Failed to active ndlp found "
16785 "for oxid:x%x SID:x%x\n", oxid, sid);
16786 return;
16787 }
James Smart6669f9b2009-10-02 15:16:45 -040016788 }
16789
James Smart546fc852011-03-11 16:06:29 -050016790 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016791 ctiocb = lpfc_sli_get_iocbq(phba);
16792 if (!ctiocb)
16793 return;
16794
James Smart5ffc2662009-11-18 15:39:44 -050016795 /* Extract the F_CTL field from FC_HDR */
16796 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16797
James Smart6669f9b2009-10-02 15:16:45 -040016798 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016799 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016800 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016801 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16802 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16803 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16804
16805 /* Fill in the rest of iocb fields */
16806 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16807 icmd->ulpBdeCount = 0;
16808 icmd->ulpLe = 1;
16809 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016810 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016811 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016812
James Smart6669f9b2009-10-02 15:16:45 -040016813 ctiocb->iocb_cmpl = NULL;
16814 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016815 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016816 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016817 ctiocb->sli4_xritag = NO_XRI;
16818
James Smartee0f4fe2012-05-09 21:19:14 -040016819 if (fctl & FC_FC_EX_CTX)
16820 /* Exchange responder sent the abort so we
16821 * own the oxid.
16822 */
16823 xri = oxid;
16824 else
16825 xri = rxid;
16826 lxri = lpfc_sli4_xri_inrange(phba, xri);
16827 if (lxri != NO_XRI)
16828 lpfc_set_rrq_active(phba, ndlp, lxri,
16829 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016830 /* For BA_ABTS from exchange responder, if the logical xri with
16831 * the oxid maps to the FCP XRI range, the port no longer has
16832 * that exchange context, send a BLS_RJT. Override the IOCB for
16833 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016834 */
James Smart6dd9e312013-01-03 15:43:37 -050016835 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016836 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016837 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16838 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16839 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16840 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16841 }
16842
16843 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16844 * the driver no longer has that exchange, send a BLS_RJT. Override
16845 * the IOCB for a BA_RJT.
16846 */
16847 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016848 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16849 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16850 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16851 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16852 }
James Smart6669f9b2009-10-02 15:16:45 -040016853
James Smart5ffc2662009-11-18 15:39:44 -050016854 if (fctl & FC_FC_EX_CTX) {
16855 /* ABTS sent by responder to CT exchange, construction
16856 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16857 * field and RX_ID from ABTS for RX_ID field.
16858 */
James Smart546fc852011-03-11 16:06:29 -050016859 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016860 } else {
16861 /* ABTS sent by initiator to CT exchange, construction
16862 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016863 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016864 */
James Smart546fc852011-03-11 16:06:29 -050016865 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016866 }
James Smartf09c3ac2012-03-01 22:33:29 -050016867 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016868 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016869
James Smart546fc852011-03-11 16:06:29 -050016870 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016871 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16872 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16873 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016874
16875 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16876 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016877 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16878 "2925 Failed to issue CT ABTS RSP x%x on "
16879 "xri x%x, Data x%x\n",
16880 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16881 phba->link_state);
16882 lpfc_nlp_put(ndlp);
16883 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016884 lpfc_sli_release_iocbq(phba, ctiocb);
16885 }
James Smart6669f9b2009-10-02 15:16:45 -040016886}
16887
16888/**
16889 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16890 * @vport: Pointer to the vport on which this sequence was received
16891 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16892 *
16893 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16894 * receive sequence is only partially assembed by the driver, it shall abort
16895 * the partially assembled frames for the sequence. Otherwise, if the
16896 * unsolicited receive sequence has been completely assembled and passed to
16897 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16898 * unsolicited sequence has been aborted. After that, it will issue a basic
16899 * accept to accept the abort.
16900 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016901static void
James Smart6669f9b2009-10-02 15:16:45 -040016902lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16903 struct hbq_dmabuf *dmabuf)
16904{
16905 struct lpfc_hba *phba = vport->phba;
16906 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016907 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016908 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016909
James Smart6669f9b2009-10-02 15:16:45 -040016910 /* Make a copy of fc_hdr before the dmabuf being released */
16911 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016912 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016913
James Smart5ffc2662009-11-18 15:39:44 -050016914 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016915 /* ABTS by responder to exchange, no cleanup needed */
16916 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016917 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016918 /* ABTS by initiator to exchange, need to do cleanup */
16919 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16920 if (aborted == false)
16921 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016922 }
James Smart6dd9e312013-01-03 15:43:37 -050016923 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16924
James Smart86c67372017-04-21 16:05:04 -070016925 if (phba->nvmet_support) {
16926 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16927 return;
16928 }
16929
James Smart6dd9e312013-01-03 15:43:37 -050016930 /* Respond with BA_ACC or BA_RJT accordingly */
16931 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016932}
16933
16934/**
James Smart4f774512009-05-22 14:52:35 -040016935 * lpfc_seq_complete - Indicates if a sequence is complete
16936 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16937 *
16938 * This function checks the sequence, starting with the frame described by
16939 * @dmabuf, to see if all the frames associated with this sequence are present.
16940 * the frames associated with this sequence are linked to the @dmabuf using the
16941 * dbuf list. This function looks for two major things. 1) That the first frame
16942 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16943 * set. 3) That there are no holes in the sequence count. The function will
16944 * return 1 when the sequence is complete, otherwise it will return 0.
16945 **/
16946static int
16947lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16948{
16949 struct fc_frame_header *hdr;
16950 struct lpfc_dmabuf *d_buf;
16951 struct hbq_dmabuf *seq_dmabuf;
16952 uint32_t fctl;
16953 int seq_count = 0;
16954
16955 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16956 /* make sure first fame of sequence has a sequence count of zero */
16957 if (hdr->fh_seq_cnt != seq_count)
16958 return 0;
16959 fctl = (hdr->fh_f_ctl[0] << 16 |
16960 hdr->fh_f_ctl[1] << 8 |
16961 hdr->fh_f_ctl[2]);
16962 /* If last frame of sequence we can return success. */
16963 if (fctl & FC_FC_END_SEQ)
16964 return 1;
16965 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16966 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16967 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16968 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016969 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016970 return 0;
16971 fctl = (hdr->fh_f_ctl[0] << 16 |
16972 hdr->fh_f_ctl[1] << 8 |
16973 hdr->fh_f_ctl[2]);
16974 /* If last frame of sequence we can return success. */
16975 if (fctl & FC_FC_END_SEQ)
16976 return 1;
16977 }
16978 return 0;
16979}
16980
16981/**
16982 * lpfc_prep_seq - Prep sequence for ULP processing
16983 * @vport: Pointer to the vport on which this sequence was received
16984 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16985 *
16986 * This function takes a sequence, described by a list of frames, and creates
16987 * a list of iocbq structures to describe the sequence. This iocbq list will be
16988 * used to issue to the generic unsolicited sequence handler. This routine
16989 * returns a pointer to the first iocbq in the list. If the function is unable
16990 * to allocate an iocbq then it throw out the received frames that were not
16991 * able to be described and return a pointer to the first iocbq. If unable to
16992 * allocate any iocbqs (including the first) this function will return NULL.
16993 **/
16994static struct lpfc_iocbq *
16995lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16996{
James Smart7851fe22011-07-22 18:36:52 -040016997 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016998 struct lpfc_dmabuf *d_buf, *n_buf;
16999 struct lpfc_iocbq *first_iocbq, *iocbq;
17000 struct fc_frame_header *fc_hdr;
17001 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017002 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017003 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017004
17005 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17006 /* remove from receive buffer list */
17007 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017008 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017009 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017010 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017011 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017012 /* Get an iocbq struct to fill in. */
17013 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17014 if (first_iocbq) {
17015 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017016 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017017 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017018 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017019
17020 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17021 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17022 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17023 first_iocbq->iocb.un.rcvels.parmRo =
17024 sli4_did_from_fc_hdr(fc_hdr);
17025 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17026 } else
17027 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017028 first_iocbq->iocb.ulpContext = NO_XRI;
17029 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17030 be16_to_cpu(fc_hdr->fh_ox_id);
17031 /* iocbq is prepped for internal consumption. Physical vpi. */
17032 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17033 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017034 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017035 tot_len = bf_get(lpfc_rcqe_length,
17036 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17037
James Smart4f774512009-05-22 14:52:35 -040017038 first_iocbq->context2 = &seq_dmabuf->dbuf;
17039 first_iocbq->context3 = NULL;
17040 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017041 if (tot_len > LPFC_DATA_BUF_SIZE)
17042 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017043 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017044 else
17045 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17046
James Smart4f774512009-05-22 14:52:35 -040017047 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017048
James Smart7851fe22011-07-22 18:36:52 -040017049 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017050 }
17051 iocbq = first_iocbq;
17052 /*
17053 * Each IOCBq can have two Buffers assigned, so go through the list
17054 * of buffers for this sequence and save two buffers in each IOCBq
17055 */
17056 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17057 if (!iocbq) {
17058 lpfc_in_buf_free(vport->phba, d_buf);
17059 continue;
17060 }
17061 if (!iocbq->context3) {
17062 iocbq->context3 = d_buf;
17063 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017064 /* We need to get the size out of the right CQE */
17065 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17066 len = bf_get(lpfc_rcqe_length,
17067 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017068 pbde = (struct ulp_bde64 *)
17069 &iocbq->iocb.unsli3.sli3Words[4];
17070 if (len > LPFC_DATA_BUF_SIZE)
17071 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17072 else
17073 pbde->tus.f.bdeSize = len;
17074
James Smart7851fe22011-07-22 18:36:52 -040017075 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17076 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017077 } else {
17078 iocbq = lpfc_sli_get_iocbq(vport->phba);
17079 if (!iocbq) {
17080 if (first_iocbq) {
17081 first_iocbq->iocb.ulpStatus =
17082 IOSTAT_FCP_RSP_ERROR;
17083 first_iocbq->iocb.un.ulpWord[4] =
17084 IOERR_NO_RESOURCES;
17085 }
17086 lpfc_in_buf_free(vport->phba, d_buf);
17087 continue;
17088 }
James Smart7851fe22011-07-22 18:36:52 -040017089 /* We need to get the size out of the right CQE */
17090 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17091 len = bf_get(lpfc_rcqe_length,
17092 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017093 iocbq->context2 = d_buf;
17094 iocbq->context3 = NULL;
17095 iocbq->iocb.ulpBdeCount = 1;
17096 if (len > LPFC_DATA_BUF_SIZE)
17097 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17098 LPFC_DATA_BUF_SIZE;
17099 else
17100 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17101
James Smart7851fe22011-07-22 18:36:52 -040017102 tot_len += len;
17103 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17104
James Smart4f774512009-05-22 14:52:35 -040017105 iocbq->iocb.un.rcvels.remoteID = sid;
17106 list_add_tail(&iocbq->list, &first_iocbq->list);
17107 }
17108 }
17109 return first_iocbq;
17110}
17111
James Smart6669f9b2009-10-02 15:16:45 -040017112static void
17113lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17114 struct hbq_dmabuf *seq_dmabuf)
17115{
17116 struct fc_frame_header *fc_hdr;
17117 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17118 struct lpfc_hba *phba = vport->phba;
17119
17120 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17121 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17122 if (!iocbq) {
17123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17124 "2707 Ring %d handler: Failed to allocate "
17125 "iocb Rctl x%x Type x%x received\n",
17126 LPFC_ELS_RING,
17127 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17128 return;
17129 }
17130 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017131 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017132 iocbq, fc_hdr->fh_r_ctl,
17133 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017135 "2540 Ring %d handler: unexpected Rctl "
17136 "x%x Type x%x received\n",
17137 LPFC_ELS_RING,
17138 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17139
17140 /* Free iocb created in lpfc_prep_seq */
17141 list_for_each_entry_safe(curr_iocb, next_iocb,
17142 &iocbq->list, list) {
17143 list_del_init(&curr_iocb->list);
17144 lpfc_sli_release_iocbq(phba, curr_iocb);
17145 }
17146 lpfc_sli_release_iocbq(phba, iocbq);
17147}
17148
James Smartae9e28f2017-05-15 15:20:51 -070017149static void
17150lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17151 struct lpfc_iocbq *rspiocb)
17152{
17153 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17154
17155 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017156 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017157 kfree(pcmd);
17158 lpfc_sli_release_iocbq(phba, cmdiocb);
17159}
17160
17161static void
17162lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17163 struct hbq_dmabuf *dmabuf)
17164{
17165 struct fc_frame_header *fc_hdr;
17166 struct lpfc_hba *phba = vport->phba;
17167 struct lpfc_iocbq *iocbq = NULL;
17168 union lpfc_wqe *wqe;
17169 struct lpfc_dmabuf *pcmd = NULL;
17170 uint32_t frame_len;
17171 int rc;
17172
17173 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17174 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17175
17176 /* Send the received frame back */
17177 iocbq = lpfc_sli_get_iocbq(phba);
17178 if (!iocbq)
17179 goto exit;
17180
17181 /* Allocate buffer for command payload */
17182 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17183 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017184 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017185 &pcmd->phys);
17186 if (!pcmd || !pcmd->virt)
17187 goto exit;
17188
17189 INIT_LIST_HEAD(&pcmd->list);
17190
17191 /* copyin the payload */
17192 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17193
17194 /* fill in BDE's for command */
17195 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17196 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17197 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17198 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17199
17200 iocbq->context2 = pcmd;
17201 iocbq->vport = vport;
17202 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17203 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17204
17205 /*
17206 * Setup rest of the iocb as though it were a WQE
17207 * Build the SEND_FRAME WQE
17208 */
17209 wqe = (union lpfc_wqe *)&iocbq->iocb;
17210
17211 wqe->send_frame.frame_len = frame_len;
17212 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17213 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17214 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17215 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17216 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17217 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17218
17219 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17220 iocbq->iocb.ulpLe = 1;
17221 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17222 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17223 if (rc == IOCB_ERROR)
17224 goto exit;
17225
17226 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17227 return;
17228
17229exit:
17230 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17231 "2023 Unable to process MDS loopback frame\n");
17232 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017233 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017234 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017235 if (iocbq)
17236 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017237 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17238}
17239
James Smart4f774512009-05-22 14:52:35 -040017240/**
17241 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17242 * @phba: Pointer to HBA context object.
17243 *
17244 * This function is called with no lock held. This function processes all
17245 * the received buffers and gives it to upper layers when a received buffer
17246 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017247 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017248 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17249 * appropriate receive function when the final frame in a sequence is received.
17250 **/
James Smart4d9ab992009-10-02 15:16:39 -040017251void
17252lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17253 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017254{
James Smart4d9ab992009-10-02 15:16:39 -040017255 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017256 struct fc_frame_header *fc_hdr;
17257 struct lpfc_vport *vport;
17258 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017259 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017260
James Smart4f774512009-05-22 14:52:35 -040017261 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017262 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017263
James Smart4d9ab992009-10-02 15:16:39 -040017264 /* check to see if this a valid type of frame */
17265 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17266 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17267 return;
17268 }
James Smart2ea259e2017-02-12 13:52:27 -080017269
James Smart7851fe22011-07-22 18:36:52 -040017270 if ((bf_get(lpfc_cqe_code,
17271 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17272 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17273 &dmabuf->cq_event.cqe.rcqe_cmpl);
17274 else
17275 fcfi = bf_get(lpfc_rcqe_fcf_id,
17276 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017277
James Smartae9e28f2017-05-15 15:20:51 -070017278 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17279 vport = phba->pport;
17280 /* Handle MDS Loopback frames */
17281 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17282 return;
17283 }
17284
James Smart895427b2017-02-12 13:52:30 -080017285 /* d_id this frame is directed to */
17286 did = sli4_did_from_fc_hdr(fc_hdr);
17287
17288 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017289 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017290 /* throw out the frame */
17291 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17292 return;
17293 }
James Smart939723a2012-05-09 21:19:03 -040017294
James Smart939723a2012-05-09 21:19:03 -040017295 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17296 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17297 (did != Fabric_DID)) {
17298 /*
17299 * Throw out the frame if we are not pt2pt.
17300 * The pt2pt protocol allows for discovery frames
17301 * to be received without a registered VPI.
17302 */
17303 if (!(vport->fc_flag & FC_PT2PT) ||
17304 (phba->link_state == LPFC_HBA_READY)) {
17305 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17306 return;
17307 }
17308 }
17309
James Smart6669f9b2009-10-02 15:16:45 -040017310 /* Handle the basic abort sequence (BA_ABTS) event */
17311 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17312 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17313 return;
17314 }
17315
James Smart4d9ab992009-10-02 15:16:39 -040017316 /* Link this frame */
17317 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17318 if (!seq_dmabuf) {
17319 /* unable to add frame to vport - throw it out */
17320 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17321 return;
17322 }
17323 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017324 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017325 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017326
James Smart6669f9b2009-10-02 15:16:45 -040017327 /* Send the complete sequence to the upper layer protocol */
17328 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017329}
James Smart6fb120a2009-05-22 14:52:59 -040017330
17331/**
17332 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17333 * @phba: pointer to lpfc hba data structure.
17334 *
17335 * This routine is invoked to post rpi header templates to the
17336 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017337 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17338 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017339 *
17340 * This routine does not require any locks. It's usage is expected
17341 * to be driver load or reset recovery when the driver is
17342 * sequential.
17343 *
17344 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017345 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017346 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017347 * When this error occurs, the driver is not guaranteed
17348 * to have any rpi regions posted to the device and
17349 * must either attempt to repost the regions or take a
17350 * fatal error.
17351 **/
17352int
17353lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17354{
17355 struct lpfc_rpi_hdr *rpi_page;
17356 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017357 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017358
James Smart6d368e52011-05-24 11:44:12 -040017359 /* SLI4 ports that support extents do not require RPI headers. */
17360 if (!phba->sli4_hba.rpi_hdrs_in_use)
17361 goto exit;
17362 if (phba->sli4_hba.extents_in_use)
17363 return -EIO;
17364
James Smart6fb120a2009-05-22 14:52:59 -040017365 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017366 /*
17367 * Assign the rpi headers a physical rpi only if the driver
17368 * has not initialized those resources. A port reset only
17369 * needs the headers posted.
17370 */
17371 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17372 LPFC_RPI_RSRC_RDY)
17373 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17374
James Smart6fb120a2009-05-22 14:52:59 -040017375 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17376 if (rc != MBX_SUCCESS) {
17377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17378 "2008 Error %d posting all rpi "
17379 "headers\n", rc);
17380 rc = -EIO;
17381 break;
17382 }
17383 }
17384
James Smart6d368e52011-05-24 11:44:12 -040017385 exit:
17386 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17387 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017388 return rc;
17389}
17390
17391/**
17392 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17393 * @phba: pointer to lpfc hba data structure.
17394 * @rpi_page: pointer to the rpi memory region.
17395 *
17396 * This routine is invoked to post a single rpi header to the
17397 * HBA consistent with the SLI-4 interface spec. This memory region
17398 * maps up to 64 rpi context regions.
17399 *
17400 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017401 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017402 * -ENOMEM - No available memory
17403 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017404 **/
17405int
17406lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17407{
17408 LPFC_MBOXQ_t *mboxq;
17409 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17410 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017411 uint32_t shdr_status, shdr_add_status;
17412 union lpfc_sli4_cfg_shdr *shdr;
17413
James Smart6d368e52011-05-24 11:44:12 -040017414 /* SLI4 ports that support extents do not require RPI headers. */
17415 if (!phba->sli4_hba.rpi_hdrs_in_use)
17416 return rc;
17417 if (phba->sli4_hba.extents_in_use)
17418 return -EIO;
17419
James Smart6fb120a2009-05-22 14:52:59 -040017420 /* The port is notified of the header region via a mailbox command. */
17421 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17422 if (!mboxq) {
17423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17424 "2001 Unable to allocate memory for issuing "
17425 "SLI_CONFIG_SPECIAL mailbox command\n");
17426 return -ENOMEM;
17427 }
17428
17429 /* Post all rpi memory regions to the port. */
17430 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017431 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17432 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17433 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017434 sizeof(struct lpfc_sli4_cfg_mhdr),
17435 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017436
17437
17438 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017439 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17440 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017441 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17442 hdr_tmpl, rpi_page->page_count);
17443
James Smart6fb120a2009-05-22 14:52:59 -040017444 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17445 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017446 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017447 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17448 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17449 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17450 if (rc != MBX_TIMEOUT)
17451 mempool_free(mboxq, phba->mbox_mem_pool);
17452 if (shdr_status || shdr_add_status || rc) {
17453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17454 "2514 POST_RPI_HDR mailbox failed with "
17455 "status x%x add_status x%x, mbx status x%x\n",
17456 shdr_status, shdr_add_status, rc);
17457 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017458 } else {
17459 /*
17460 * The next_rpi stores the next logical module-64 rpi value used
17461 * to post physical rpis in subsequent rpi postings.
17462 */
17463 spin_lock_irq(&phba->hbalock);
17464 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17465 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017466 }
17467 return rc;
17468}
17469
17470/**
17471 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17472 * @phba: pointer to lpfc hba data structure.
17473 *
17474 * This routine is invoked to post rpi header templates to the
17475 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017476 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17477 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017478 *
17479 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017480 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017481 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17482 **/
17483int
17484lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17485{
James Smart6d368e52011-05-24 11:44:12 -040017486 unsigned long rpi;
17487 uint16_t max_rpi, rpi_limit;
17488 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017489 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017490 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017491
James Smart6fb120a2009-05-22 14:52:59 -040017492 /*
James Smart6d368e52011-05-24 11:44:12 -040017493 * Fetch the next logical rpi. Because this index is logical,
17494 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017495 */
James Smart4902b382013-10-10 12:20:35 -040017496 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017497 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17498 rpi_limit = phba->sli4_hba.next_rpi;
17499
James Smart6d368e52011-05-24 11:44:12 -040017500 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17501 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017502 rpi = LPFC_RPI_ALLOC_ERROR;
17503 else {
17504 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17505 phba->sli4_hba.max_cfg_param.rpi_used++;
17506 phba->sli4_hba.rpi_count++;
17507 }
James Smartbe6bb942015-04-07 15:07:22 -040017508 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17509 "0001 rpi:%x max:%x lim:%x\n",
17510 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017511
17512 /*
17513 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017514 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017515 */
17516 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17517 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017518 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017519 return rpi;
17520 }
17521
17522 /*
James Smart6d368e52011-05-24 11:44:12 -040017523 * RPI header postings are not required for SLI4 ports capable of
17524 * extents.
17525 */
17526 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017527 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017528 return rpi;
17529 }
17530
17531 /*
James Smart6fb120a2009-05-22 14:52:59 -040017532 * If the driver is running low on rpi resources, allocate another
17533 * page now. Note that the next_rpi value is used because
17534 * it represents how many are actually in use whereas max_rpi notes
17535 * how many are supported max by the device.
17536 */
James Smart6d368e52011-05-24 11:44:12 -040017537 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017538 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017539 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17540 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17541 if (!rpi_hdr) {
17542 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17543 "2002 Error Could not grow rpi "
17544 "count\n");
17545 } else {
James Smart6d368e52011-05-24 11:44:12 -040017546 lrpi = rpi_hdr->start_rpi;
17547 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017548 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17549 }
17550 }
17551
17552 return rpi;
17553}
17554
17555/**
17556 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17557 * @phba: pointer to lpfc hba data structure.
17558 *
17559 * This routine is invoked to release an rpi to the pool of
17560 * available rpis maintained by the driver.
17561 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017562static void
James Smartd7c47992010-06-08 18:31:54 -040017563__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17564{
17565 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17566 phba->sli4_hba.rpi_count--;
17567 phba->sli4_hba.max_cfg_param.rpi_used--;
17568 }
17569}
17570
17571/**
17572 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17573 * @phba: pointer to lpfc hba data structure.
17574 *
17575 * This routine is invoked to release an rpi to the pool of
17576 * available rpis maintained by the driver.
17577 **/
17578void
James Smart6fb120a2009-05-22 14:52:59 -040017579lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17580{
17581 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017582 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017583 spin_unlock_irq(&phba->hbalock);
17584}
17585
17586/**
17587 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17588 * @phba: pointer to lpfc hba data structure.
17589 *
17590 * This routine is invoked to remove the memory region that
17591 * provided rpi via a bitmask.
17592 **/
17593void
17594lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17595{
17596 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017597 kfree(phba->sli4_hba.rpi_ids);
17598 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017599}
17600
17601/**
17602 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17603 * @phba: pointer to lpfc hba data structure.
17604 *
17605 * This routine is invoked to remove the memory region that
17606 * provided rpi via a bitmask.
17607 **/
17608int
James Smart6b5151f2012-01-18 16:24:06 -050017609lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17610 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017611{
17612 LPFC_MBOXQ_t *mboxq;
17613 struct lpfc_hba *phba = ndlp->phba;
17614 int rc;
17615
17616 /* The port is notified of the header region via a mailbox command. */
17617 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17618 if (!mboxq)
17619 return -ENOMEM;
17620
17621 /* Post all rpi memory regions to the port. */
17622 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017623 if (cmpl) {
17624 mboxq->mbox_cmpl = cmpl;
17625 mboxq->context1 = arg;
17626 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017627 } else
17628 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017629 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017630 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17631 if (rc == MBX_NOT_FINISHED) {
17632 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17633 "2010 Resume RPI Mailbox failed "
17634 "status %d, mbxStatus x%x\n", rc,
17635 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17636 mempool_free(mboxq, phba->mbox_mem_pool);
17637 return -EIO;
17638 }
17639 return 0;
17640}
17641
17642/**
17643 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017644 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017645 *
James Smart76a95d72010-11-20 23:11:48 -050017646 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017647 *
17648 * Returns:
17649 * 0 success
17650 * -Evalue otherwise
17651 **/
17652int
James Smart76a95d72010-11-20 23:11:48 -050017653lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017654{
17655 LPFC_MBOXQ_t *mboxq;
17656 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017657 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017658 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017659 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017660 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17661 if (!mboxq)
17662 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017663 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017664 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017665 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017666 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017667 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017668 "2022 INIT VPI Mailbox failed "
17669 "status %d, mbxStatus x%x\n", rc,
17670 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017671 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017672 }
James Smart6a9c52c2009-10-02 15:16:51 -040017673 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017674 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017675
17676 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017677}
17678
17679/**
17680 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17681 * @phba: pointer to lpfc hba data structure.
17682 * @mboxq: Pointer to mailbox object.
17683 *
17684 * This routine is invoked to manually add a single FCF record. The caller
17685 * must pass a completely initialized FCF_Record. This routine takes
17686 * care of the nonembedded mailbox operations.
17687 **/
17688static void
17689lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17690{
17691 void *virt_addr;
17692 union lpfc_sli4_cfg_shdr *shdr;
17693 uint32_t shdr_status, shdr_add_status;
17694
17695 virt_addr = mboxq->sge_array->addr[0];
17696 /* The IOCTL status is embedded in the mailbox subheader. */
17697 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17698 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17699 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17700
17701 if ((shdr_status || shdr_add_status) &&
17702 (shdr_status != STATUS_FCF_IN_USE))
17703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17704 "2558 ADD_FCF_RECORD mailbox failed with "
17705 "status x%x add_status x%x\n",
17706 shdr_status, shdr_add_status);
17707
17708 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17709}
17710
17711/**
17712 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17713 * @phba: pointer to lpfc hba data structure.
17714 * @fcf_record: pointer to the initialized fcf record to add.
17715 *
17716 * This routine is invoked to manually add a single FCF record. The caller
17717 * must pass a completely initialized FCF_Record. This routine takes
17718 * care of the nonembedded mailbox operations.
17719 **/
17720int
17721lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17722{
17723 int rc = 0;
17724 LPFC_MBOXQ_t *mboxq;
17725 uint8_t *bytep;
17726 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017727 struct lpfc_mbx_sge sge;
17728 uint32_t alloc_len, req_len;
17729 uint32_t fcfindex;
17730
17731 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17732 if (!mboxq) {
17733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17734 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17735 return -ENOMEM;
17736 }
17737
17738 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17739 sizeof(uint32_t);
17740
17741 /* Allocate DMA memory and set up the non-embedded mailbox command */
17742 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17743 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17744 req_len, LPFC_SLI4_MBX_NEMBED);
17745 if (alloc_len < req_len) {
17746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17747 "2523 Allocated DMA memory size (x%x) is "
17748 "less than the requested DMA memory "
17749 "size (x%x)\n", alloc_len, req_len);
17750 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17751 return -ENOMEM;
17752 }
17753
17754 /*
17755 * Get the first SGE entry from the non-embedded DMA memory. This
17756 * routine only uses a single SGE.
17757 */
17758 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017759 virt_addr = mboxq->sge_array->addr[0];
17760 /*
17761 * Configure the FCF record for FCFI 0. This is the driver's
17762 * hardcoded default and gets used in nonFIP mode.
17763 */
17764 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17765 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17766 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17767
17768 /*
17769 * Copy the fcf_index and the FCF Record Data. The data starts after
17770 * the FCoE header plus word10. The data copy needs to be endian
17771 * correct.
17772 */
17773 bytep += sizeof(uint32_t);
17774 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17775 mboxq->vport = phba->pport;
17776 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17777 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17778 if (rc == MBX_NOT_FINISHED) {
17779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17780 "2515 ADD_FCF_RECORD mailbox failed with "
17781 "status 0x%x\n", rc);
17782 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17783 rc = -EIO;
17784 } else
17785 rc = 0;
17786
17787 return rc;
17788}
17789
17790/**
17791 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17792 * @phba: pointer to lpfc hba data structure.
17793 * @fcf_record: pointer to the fcf record to write the default data.
17794 * @fcf_index: FCF table entry index.
17795 *
17796 * This routine is invoked to build the driver's default FCF record. The
17797 * values used are hardcoded. This routine handles memory initialization.
17798 *
17799 **/
17800void
17801lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17802 struct fcf_record *fcf_record,
17803 uint16_t fcf_index)
17804{
17805 memset(fcf_record, 0, sizeof(struct fcf_record));
17806 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17807 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17808 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17809 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17810 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17811 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17812 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17813 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17814 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17815 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17816 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17817 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17818 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017819 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017820 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17821 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17822 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17823 /* Set the VLAN bit map */
17824 if (phba->valid_vlan) {
17825 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17826 = 1 << (phba->vlan_id % 8);
17827 }
17828}
17829
17830/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017831 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017832 * @phba: pointer to lpfc hba data structure.
17833 * @fcf_index: FCF table entry offset.
17834 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017835 * This routine is invoked to scan the entire FCF table by reading FCF
17836 * record and processing it one at a time starting from the @fcf_index
17837 * for initial FCF discovery or fast FCF failover rediscovery.
17838 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017839 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017840 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017841 **/
17842int
James Smart0c9ab6f2010-02-26 14:15:57 -050017843lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017844{
17845 int rc = 0, error;
17846 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017847
James Smart32b97932009-07-19 10:01:21 -040017848 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017849 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017850 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17851 if (!mboxq) {
17852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17853 "2000 Failed to allocate mbox for "
17854 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017855 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017856 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017857 }
James Smartecfd03c2010-02-12 14:41:27 -050017858 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017859 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017860 if (rc) {
17861 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017862 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017863 }
James Smartecfd03c2010-02-12 14:41:27 -050017864 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017865 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017866 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017867
17868 spin_lock_irq(&phba->hbalock);
17869 phba->hba_flag |= FCF_TS_INPROG;
17870 spin_unlock_irq(&phba->hbalock);
17871
James Smart6fb120a2009-05-22 14:52:59 -040017872 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017873 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017874 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017875 else {
James Smart38b92ef2010-08-04 16:11:39 -040017876 /* Reset eligible FCF count for new scan */
17877 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017878 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017879 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017880 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017881fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017882 if (error) {
17883 if (mboxq)
17884 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017885 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017886 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017887 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017888 spin_unlock_irq(&phba->hbalock);
17889 }
James Smart6fb120a2009-05-22 14:52:59 -040017890 return error;
17891}
James Smarta0c87cb2009-07-19 10:01:10 -040017892
17893/**
James Smarta93ff372010-10-22 11:06:08 -040017894 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017895 * @phba: pointer to lpfc hba data structure.
17896 * @fcf_index: FCF table entry offset.
17897 *
17898 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017899 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017900 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017901 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017902 * otherwise.
17903 **/
17904int
17905lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17906{
17907 int rc = 0, error;
17908 LPFC_MBOXQ_t *mboxq;
17909
17910 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17911 if (!mboxq) {
17912 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17913 "2763 Failed to allocate mbox for "
17914 "READ_FCF cmd\n");
17915 error = -ENOMEM;
17916 goto fail_fcf_read;
17917 }
17918 /* Construct the read FCF record mailbox command */
17919 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17920 if (rc) {
17921 error = -EINVAL;
17922 goto fail_fcf_read;
17923 }
17924 /* Issue the mailbox command asynchronously */
17925 mboxq->vport = phba->pport;
17926 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17927 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17928 if (rc == MBX_NOT_FINISHED)
17929 error = -EIO;
17930 else
17931 error = 0;
17932
17933fail_fcf_read:
17934 if (error && mboxq)
17935 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17936 return error;
17937}
17938
17939/**
17940 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17941 * @phba: pointer to lpfc hba data structure.
17942 * @fcf_index: FCF table entry offset.
17943 *
17944 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017945 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017946 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017947 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017948 * otherwise.
17949 **/
17950int
17951lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17952{
17953 int rc = 0, error;
17954 LPFC_MBOXQ_t *mboxq;
17955
17956 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17957 if (!mboxq) {
17958 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17959 "2758 Failed to allocate mbox for "
17960 "READ_FCF cmd\n");
17961 error = -ENOMEM;
17962 goto fail_fcf_read;
17963 }
17964 /* Construct the read FCF record mailbox command */
17965 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17966 if (rc) {
17967 error = -EINVAL;
17968 goto fail_fcf_read;
17969 }
17970 /* Issue the mailbox command asynchronously */
17971 mboxq->vport = phba->pport;
17972 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17973 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17974 if (rc == MBX_NOT_FINISHED)
17975 error = -EIO;
17976 else
17977 error = 0;
17978
17979fail_fcf_read:
17980 if (error && mboxq)
17981 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17982 return error;
17983}
17984
17985/**
James Smartf5cb5302015-12-16 18:11:52 -050017986 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017987 * phba pointer to the lpfc_hba struct for this port.
17988 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17989 * routine when the rr_bmask is empty. The FCF indecies are put into the
17990 * rr_bmask based on their priority level. Starting from the highest priority
17991 * to the lowest. The most likely FCF candidate will be in the highest
17992 * priority group. When this routine is called it searches the fcf_pri list for
17993 * next lowest priority group and repopulates the rr_bmask with only those
17994 * fcf_indexes.
17995 * returns:
17996 * 1=success 0=failure
17997 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017998static int
James Smart7d791df2011-07-22 18:37:52 -040017999lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18000{
18001 uint16_t next_fcf_pri;
18002 uint16_t last_index;
18003 struct lpfc_fcf_pri *fcf_pri;
18004 int rc;
18005 int ret = 0;
18006
18007 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18008 LPFC_SLI4_FCF_TBL_INDX_MAX);
18009 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18010 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018011
18012 /* Verify the priority list has 2 or more entries */
18013 spin_lock_irq(&phba->hbalock);
18014 if (list_empty(&phba->fcf.fcf_pri_list) ||
18015 list_is_singular(&phba->fcf.fcf_pri_list)) {
18016 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018017 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18018 "3061 Last IDX %d\n", last_index);
18019 return 0; /* Empty rr list */
18020 }
James Smart25626692013-03-01 16:36:54 -050018021 spin_unlock_irq(&phba->hbalock);
18022
James Smart7d791df2011-07-22 18:37:52 -040018023 next_fcf_pri = 0;
18024 /*
18025 * Clear the rr_bmask and set all of the bits that are at this
18026 * priority.
18027 */
18028 memset(phba->fcf.fcf_rr_bmask, 0,
18029 sizeof(*phba->fcf.fcf_rr_bmask));
18030 spin_lock_irq(&phba->hbalock);
18031 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18032 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18033 continue;
18034 /*
18035 * the 1st priority that has not FLOGI failed
18036 * will be the highest.
18037 */
18038 if (!next_fcf_pri)
18039 next_fcf_pri = fcf_pri->fcf_rec.priority;
18040 spin_unlock_irq(&phba->hbalock);
18041 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18042 rc = lpfc_sli4_fcf_rr_index_set(phba,
18043 fcf_pri->fcf_rec.fcf_index);
18044 if (rc)
18045 return 0;
18046 }
18047 spin_lock_irq(&phba->hbalock);
18048 }
18049 /*
18050 * if next_fcf_pri was not set above and the list is not empty then
18051 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018052 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018053 */
18054 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18055 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18056 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18057 /*
18058 * the 1st priority that has not FLOGI failed
18059 * will be the highest.
18060 */
18061 if (!next_fcf_pri)
18062 next_fcf_pri = fcf_pri->fcf_rec.priority;
18063 spin_unlock_irq(&phba->hbalock);
18064 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18065 rc = lpfc_sli4_fcf_rr_index_set(phba,
18066 fcf_pri->fcf_rec.fcf_index);
18067 if (rc)
18068 return 0;
18069 }
18070 spin_lock_irq(&phba->hbalock);
18071 }
18072 } else
18073 ret = 1;
18074 spin_unlock_irq(&phba->hbalock);
18075
18076 return ret;
18077}
18078/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018079 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18080 * @phba: pointer to lpfc hba data structure.
18081 *
18082 * This routine is to get the next eligible FCF record index in a round
18083 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018084 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018085 * shall be returned, otherwise, the next eligible FCF record's index
18086 * shall be returned.
18087 **/
18088uint16_t
18089lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18090{
18091 uint16_t next_fcf_index;
18092
James Smart421c6622013-01-03 15:44:16 -050018093initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018094 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018095 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18096
James Smart7d791df2011-07-22 18:37:52 -040018097next_priority:
James Smart421c6622013-01-03 15:44:16 -050018098 /* Determine the next fcf index to check */
18099 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018100 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18101 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018102 next_fcf_index);
18103
James Smart0c9ab6f2010-02-26 14:15:57 -050018104 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018105 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18106 /*
18107 * If we have wrapped then we need to clear the bits that
18108 * have been tested so that we can detect when we should
18109 * change the priority level.
18110 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018111 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18112 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018113 }
18114
James Smart0c9ab6f2010-02-26 14:15:57 -050018115
James Smart3804dc82010-07-14 15:31:37 -040018116 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018117 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18118 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18119 /*
18120 * If next fcf index is not found check if there are lower
18121 * Priority level fcf's in the fcf_priority list.
18122 * Set up the rr_bmask with all of the avaiable fcf bits
18123 * at that level and continue the selection process.
18124 */
18125 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018126 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018127 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18128 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018129 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18130 return LPFC_FCOE_FCF_NEXT_NONE;
18131 else {
18132 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18133 "3063 Only FCF available idx %d, flag %x\n",
18134 next_fcf_index,
18135 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18136 return next_fcf_index;
18137 }
James Smart3804dc82010-07-14 15:31:37 -040018138 }
18139
James Smart7d791df2011-07-22 18:37:52 -040018140 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18141 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018142 LPFC_FCF_FLOGI_FAILED) {
18143 if (list_is_singular(&phba->fcf.fcf_pri_list))
18144 return LPFC_FCOE_FCF_NEXT_NONE;
18145
James Smart7d791df2011-07-22 18:37:52 -040018146 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018147 }
James Smart7d791df2011-07-22 18:37:52 -040018148
James Smart3804dc82010-07-14 15:31:37 -040018149 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018150 "2845 Get next roundrobin failover FCF (x%x)\n",
18151 next_fcf_index);
18152
James Smart0c9ab6f2010-02-26 14:15:57 -050018153 return next_fcf_index;
18154}
18155
18156/**
18157 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18158 * @phba: pointer to lpfc hba data structure.
18159 *
18160 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018161 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018162 * does not go beyond the range of the driver allocated bmask dimension
18163 * before setting the bit.
18164 *
18165 * Returns 0 if the index bit successfully set, otherwise, it returns
18166 * -EINVAL.
18167 **/
18168int
18169lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18170{
18171 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18172 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018173 "2610 FCF (x%x) reached driver's book "
18174 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018175 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18176 return -EINVAL;
18177 }
18178 /* Set the eligible FCF record index bmask */
18179 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18180
James Smart3804dc82010-07-14 15:31:37 -040018181 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018182 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018183 "bmask\n", fcf_index);
18184
James Smart0c9ab6f2010-02-26 14:15:57 -050018185 return 0;
18186}
18187
18188/**
James Smart3804dc82010-07-14 15:31:37 -040018189 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018190 * @phba: pointer to lpfc hba data structure.
18191 *
18192 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018193 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018194 * does not go beyond the range of the driver allocated bmask dimension
18195 * before clearing the bit.
18196 **/
18197void
18198lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18199{
James Smart9a803a72013-09-06 12:17:56 -040018200 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018201 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18202 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018203 "2762 FCF (x%x) reached driver's book "
18204 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018205 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18206 return;
18207 }
18208 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018209 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018210 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18211 list) {
James Smart7d791df2011-07-22 18:37:52 -040018212 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18213 list_del_init(&fcf_pri->list);
18214 break;
18215 }
18216 }
18217 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018218 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018219
18220 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018221 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018222 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018223}
18224
18225/**
James Smartecfd03c2010-02-12 14:41:27 -050018226 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18227 * @phba: pointer to lpfc hba data structure.
18228 *
18229 * This routine is the completion routine for the rediscover FCF table mailbox
18230 * command. If the mailbox command returned failure, it will try to stop the
18231 * FCF rediscover wait timer.
18232 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018233static void
James Smartecfd03c2010-02-12 14:41:27 -050018234lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18235{
18236 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18237 uint32_t shdr_status, shdr_add_status;
18238
18239 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18240
18241 shdr_status = bf_get(lpfc_mbox_hdr_status,
18242 &redisc_fcf->header.cfg_shdr.response);
18243 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18244 &redisc_fcf->header.cfg_shdr.response);
18245 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018246 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018247 "2746 Requesting for FCF rediscovery failed "
18248 "status x%x add_status x%x\n",
18249 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018250 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018251 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018252 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018253 spin_unlock_irq(&phba->hbalock);
18254 /*
18255 * CVL event triggered FCF rediscover request failed,
18256 * last resort to re-try current registered FCF entry.
18257 */
18258 lpfc_retry_pport_discovery(phba);
18259 } else {
18260 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018261 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018262 spin_unlock_irq(&phba->hbalock);
18263 /*
18264 * DEAD FCF event triggered FCF rediscover request
18265 * failed, last resort to fail over as a link down
18266 * to FCF registration.
18267 */
18268 lpfc_sli4_fcf_dead_failthrough(phba);
18269 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018270 } else {
18271 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018272 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018273 /*
18274 * Start FCF rediscovery wait timer for pending FCF
18275 * before rescan FCF record table.
18276 */
18277 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018278 }
James Smartecfd03c2010-02-12 14:41:27 -050018279
18280 mempool_free(mbox, phba->mbox_mem_pool);
18281}
18282
18283/**
James Smart3804dc82010-07-14 15:31:37 -040018284 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018285 * @phba: pointer to lpfc hba data structure.
18286 *
18287 * This routine is invoked to request for rediscovery of the entire FCF table
18288 * by the port.
18289 **/
18290int
18291lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18292{
18293 LPFC_MBOXQ_t *mbox;
18294 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18295 int rc, length;
18296
James Smart0c9ab6f2010-02-26 14:15:57 -050018297 /* Cancel retry delay timers to all vports before FCF rediscover */
18298 lpfc_cancel_all_vport_retry_delay_timer(phba);
18299
James Smartecfd03c2010-02-12 14:41:27 -050018300 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18301 if (!mbox) {
18302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18303 "2745 Failed to allocate mbox for "
18304 "requesting FCF rediscover.\n");
18305 return -ENOMEM;
18306 }
18307
18308 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18309 sizeof(struct lpfc_sli4_cfg_mhdr));
18310 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18311 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18312 length, LPFC_SLI4_MBX_EMBED);
18313
18314 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18315 /* Set count to 0 for invalidating the entire FCF database */
18316 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18317
18318 /* Issue the mailbox command asynchronously */
18319 mbox->vport = phba->pport;
18320 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18321 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18322
18323 if (rc == MBX_NOT_FINISHED) {
18324 mempool_free(mbox, phba->mbox_mem_pool);
18325 return -EIO;
18326 }
18327 return 0;
18328}
18329
18330/**
James Smartfc2b9892010-02-26 14:15:29 -050018331 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18332 * @phba: pointer to lpfc hba data structure.
18333 *
18334 * This function is the failover routine as a last resort to the FCF DEAD
18335 * event when driver failed to perform fast FCF failover.
18336 **/
18337void
18338lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18339{
18340 uint32_t link_state;
18341
18342 /*
18343 * Last resort as FCF DEAD event failover will treat this as
18344 * a link down, but save the link state because we don't want
18345 * it to be changed to Link Down unless it is already down.
18346 */
18347 link_state = phba->link_state;
18348 lpfc_linkdown(phba);
18349 phba->link_state = link_state;
18350
18351 /* Unregister FCF if no devices connected to it */
18352 lpfc_unregister_unused_fcf(phba);
18353}
18354
18355/**
James Smart026abb82011-12-13 13:20:45 -050018356 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018357 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018358 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018359 *
James Smart026abb82011-12-13 13:20:45 -050018360 * This function gets SLI3 port configure region 23 data through memory dump
18361 * mailbox command. When it successfully retrieves data, the size of the data
18362 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018363 **/
James Smart026abb82011-12-13 13:20:45 -050018364static uint32_t
18365lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018366{
18367 LPFC_MBOXQ_t *pmb = NULL;
18368 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018369 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018370 int rc;
18371
James Smart026abb82011-12-13 13:20:45 -050018372 if (!rgn23_data)
18373 return 0;
18374
James Smarta0c87cb2009-07-19 10:01:10 -040018375 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18376 if (!pmb) {
18377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018378 "2600 failed to allocate mailbox memory\n");
18379 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018380 }
18381 mb = &pmb->u.mb;
18382
James Smarta0c87cb2009-07-19 10:01:10 -040018383 do {
18384 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18385 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18386
18387 if (rc != MBX_SUCCESS) {
18388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018389 "2601 failed to read config "
18390 "region 23, rc 0x%x Status 0x%x\n",
18391 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018392 mb->un.varDmp.word_cnt = 0;
18393 }
18394 /*
18395 * dump mem may return a zero when finished or we got a
18396 * mailbox error, either way we are done.
18397 */
18398 if (mb->un.varDmp.word_cnt == 0)
18399 break;
18400 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18401 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18402
18403 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018404 rgn23_data + offset,
18405 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018406 offset += mb->un.varDmp.word_cnt;
18407 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18408
James Smart026abb82011-12-13 13:20:45 -050018409 mempool_free(pmb, phba->mbox_mem_pool);
18410 return offset;
18411}
18412
18413/**
18414 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18415 * @phba: pointer to lpfc hba data structure.
18416 * @rgn23_data: pointer to configure region 23 data.
18417 *
18418 * This function gets SLI4 port configure region 23 data through memory dump
18419 * mailbox command. When it successfully retrieves data, the size of the data
18420 * will be returned, otherwise, 0 will be returned.
18421 **/
18422static uint32_t
18423lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18424{
18425 LPFC_MBOXQ_t *mboxq = NULL;
18426 struct lpfc_dmabuf *mp = NULL;
18427 struct lpfc_mqe *mqe;
18428 uint32_t data_length = 0;
18429 int rc;
18430
18431 if (!rgn23_data)
18432 return 0;
18433
18434 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18435 if (!mboxq) {
18436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18437 "3105 failed to allocate mailbox memory\n");
18438 return 0;
18439 }
18440
18441 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18442 goto out;
18443 mqe = &mboxq->u.mqe;
18444 mp = (struct lpfc_dmabuf *) mboxq->context1;
18445 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18446 if (rc)
18447 goto out;
18448 data_length = mqe->un.mb_words[5];
18449 if (data_length == 0)
18450 goto out;
18451 if (data_length > DMP_RGN23_SIZE) {
18452 data_length = 0;
18453 goto out;
18454 }
18455 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18456out:
18457 mempool_free(mboxq, phba->mbox_mem_pool);
18458 if (mp) {
18459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18460 kfree(mp);
18461 }
18462 return data_length;
18463}
18464
18465/**
18466 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18467 * @phba: pointer to lpfc hba data structure.
18468 *
18469 * This function read region 23 and parse TLV for port status to
18470 * decide if the user disaled the port. If the TLV indicates the
18471 * port is disabled, the hba_flag is set accordingly.
18472 **/
18473void
18474lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18475{
18476 uint8_t *rgn23_data = NULL;
18477 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18478 uint32_t offset = 0;
18479
18480 /* Get adapter Region 23 data */
18481 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18482 if (!rgn23_data)
18483 goto out;
18484
18485 if (phba->sli_rev < LPFC_SLI_REV4)
18486 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18487 else {
18488 if_type = bf_get(lpfc_sli_intf_if_type,
18489 &phba->sli4_hba.sli_intf);
18490 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18491 goto out;
18492 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18493 }
James Smarta0c87cb2009-07-19 10:01:10 -040018494
18495 if (!data_size)
18496 goto out;
18497
18498 /* Check the region signature first */
18499 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18501 "2619 Config region 23 has bad signature\n");
18502 goto out;
18503 }
18504 offset += 4;
18505
18506 /* Check the data structure version */
18507 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18509 "2620 Config region 23 has bad version\n");
18510 goto out;
18511 }
18512 offset += 4;
18513
18514 /* Parse TLV entries in the region */
18515 while (offset < data_size) {
18516 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18517 break;
18518 /*
18519 * If the TLV is not driver specific TLV or driver id is
18520 * not linux driver id, skip the record.
18521 */
18522 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18523 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18524 (rgn23_data[offset + 3] != 0)) {
18525 offset += rgn23_data[offset + 1] * 4 + 4;
18526 continue;
18527 }
18528
18529 /* Driver found a driver specific TLV in the config region */
18530 sub_tlv_len = rgn23_data[offset + 1] * 4;
18531 offset += 4;
18532 tlv_offset = 0;
18533
18534 /*
18535 * Search for configured port state sub-TLV.
18536 */
18537 while ((offset < data_size) &&
18538 (tlv_offset < sub_tlv_len)) {
18539 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18540 offset += 4;
18541 tlv_offset += 4;
18542 break;
18543 }
18544 if (rgn23_data[offset] != PORT_STE_TYPE) {
18545 offset += rgn23_data[offset + 1] * 4 + 4;
18546 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18547 continue;
18548 }
18549
18550 /* This HBA contains PORT_STE configured */
18551 if (!rgn23_data[offset + 2])
18552 phba->hba_flag |= LINK_DISABLED;
18553
18554 goto out;
18555 }
18556 }
James Smart026abb82011-12-13 13:20:45 -050018557
James Smarta0c87cb2009-07-19 10:01:10 -040018558out:
James Smarta0c87cb2009-07-19 10:01:10 -040018559 kfree(rgn23_data);
18560 return;
18561}
James Smart695a8142010-01-26 23:08:03 -050018562
18563/**
James Smart52d52442011-05-24 11:42:45 -040018564 * lpfc_wr_object - write an object to the firmware
18565 * @phba: HBA structure that indicates port to create a queue on.
18566 * @dmabuf_list: list of dmabufs to write to the port.
18567 * @size: the total byte value of the objects to write to the port.
18568 * @offset: the current offset to be used to start the transfer.
18569 *
18570 * This routine will create a wr_object mailbox command to send to the port.
18571 * the mailbox command will be constructed using the dma buffers described in
18572 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18573 * BDEs that the imbedded mailbox can support. The @offset variable will be
18574 * used to indicate the starting offset of the transfer and will also return
18575 * the offset after the write object mailbox has completed. @size is used to
18576 * determine the end of the object and whether the eof bit should be set.
18577 *
18578 * Return 0 is successful and offset will contain the the new offset to use
18579 * for the next write.
18580 * Return negative value for error cases.
18581 **/
18582int
18583lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18584 uint32_t size, uint32_t *offset)
18585{
18586 struct lpfc_mbx_wr_object *wr_object;
18587 LPFC_MBOXQ_t *mbox;
18588 int rc = 0, i = 0;
18589 uint32_t shdr_status, shdr_add_status;
18590 uint32_t mbox_tmo;
18591 union lpfc_sli4_cfg_shdr *shdr;
18592 struct lpfc_dmabuf *dmabuf;
18593 uint32_t written = 0;
18594
18595 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18596 if (!mbox)
18597 return -ENOMEM;
18598
18599 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18600 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18601 sizeof(struct lpfc_mbx_wr_object) -
18602 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18603
18604 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18605 wr_object->u.request.write_offset = *offset;
18606 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18607 wr_object->u.request.object_name[0] =
18608 cpu_to_le32(wr_object->u.request.object_name[0]);
18609 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18610 list_for_each_entry(dmabuf, dmabuf_list, list) {
18611 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18612 break;
18613 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18614 wr_object->u.request.bde[i].addrHigh =
18615 putPaddrHigh(dmabuf->phys);
18616 if (written + SLI4_PAGE_SIZE >= size) {
18617 wr_object->u.request.bde[i].tus.f.bdeSize =
18618 (size - written);
18619 written += (size - written);
18620 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18621 } else {
18622 wr_object->u.request.bde[i].tus.f.bdeSize =
18623 SLI4_PAGE_SIZE;
18624 written += SLI4_PAGE_SIZE;
18625 }
18626 i++;
18627 }
18628 wr_object->u.request.bde_count = i;
18629 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18630 if (!phba->sli4_hba.intr_enable)
18631 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18632 else {
James Smarta183a152011-10-10 21:32:43 -040018633 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018634 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18635 }
18636 /* The IOCTL status is embedded in the mailbox subheader. */
18637 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18638 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18639 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18640 if (rc != MBX_TIMEOUT)
18641 mempool_free(mbox, phba->mbox_mem_pool);
18642 if (shdr_status || shdr_add_status || rc) {
18643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18644 "3025 Write Object mailbox failed with "
18645 "status x%x add_status x%x, mbx status x%x\n",
18646 shdr_status, shdr_add_status, rc);
18647 rc = -ENXIO;
18648 } else
18649 *offset += wr_object->u.response.actual_write_length;
18650 return rc;
18651}
18652
18653/**
James Smart695a8142010-01-26 23:08:03 -050018654 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18655 * @vport: pointer to vport data structure.
18656 *
18657 * This function iterate through the mailboxq and clean up all REG_LOGIN
18658 * and REG_VPI mailbox commands associated with the vport. This function
18659 * is called when driver want to restart discovery of the vport due to
18660 * a Clear Virtual Link event.
18661 **/
18662void
18663lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18664{
18665 struct lpfc_hba *phba = vport->phba;
18666 LPFC_MBOXQ_t *mb, *nextmb;
18667 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018668 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018669 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018670 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018671 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018672 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018673
James Smartd439d282010-09-29 11:18:45 -040018674 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018675 spin_lock_irq(&phba->hbalock);
18676 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18677 if (mb->vport != vport)
18678 continue;
18679
18680 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18681 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18682 continue;
18683
James Smartd439d282010-09-29 11:18:45 -040018684 list_del(&mb->list);
18685 list_add_tail(&mb->list, &mbox_cmd_list);
18686 }
18687 /* Clean up active mailbox command with the vport */
18688 mb = phba->sli.mbox_active;
18689 if (mb && (mb->vport == vport)) {
18690 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18691 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18692 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18693 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18694 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18695 /* Put reference count for delayed processing */
18696 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18697 /* Unregister the RPI when mailbox complete */
18698 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18699 }
18700 }
James Smart63e801c2010-11-20 23:14:19 -050018701 /* Cleanup any mailbox completions which are not yet processed */
18702 do {
18703 restart_loop = 0;
18704 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18705 /*
18706 * If this mailox is already processed or it is
18707 * for another vport ignore it.
18708 */
18709 if ((mb->vport != vport) ||
18710 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18711 continue;
18712
18713 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18714 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18715 continue;
18716
18717 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18718 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18719 ndlp = (struct lpfc_nodelist *)mb->context2;
18720 /* Unregister the RPI when mailbox complete */
18721 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18722 restart_loop = 1;
18723 spin_unlock_irq(&phba->hbalock);
18724 spin_lock(shost->host_lock);
18725 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18726 spin_unlock(shost->host_lock);
18727 spin_lock_irq(&phba->hbalock);
18728 break;
18729 }
18730 }
18731 } while (restart_loop);
18732
James Smartd439d282010-09-29 11:18:45 -040018733 spin_unlock_irq(&phba->hbalock);
18734
18735 /* Release the cleaned-up mailbox commands */
18736 while (!list_empty(&mbox_cmd_list)) {
18737 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018738 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18739 mp = (struct lpfc_dmabuf *) (mb->context1);
18740 if (mp) {
18741 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18742 kfree(mp);
18743 }
James Smart78730cf2010-04-06 15:06:30 -040018744 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018745 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018746 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018747 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018748 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018749 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018750 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018751 }
James Smart695a8142010-01-26 23:08:03 -050018752 }
James Smart695a8142010-01-26 23:08:03 -050018753 mempool_free(mb, phba->mbox_mem_pool);
18754 }
James Smartd439d282010-09-29 11:18:45 -040018755
18756 /* Release the ndlp with the cleaned-up active mailbox command */
18757 if (act_mbx_ndlp) {
18758 spin_lock(shost->host_lock);
18759 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18760 spin_unlock(shost->host_lock);
18761 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018762 }
James Smart695a8142010-01-26 23:08:03 -050018763}
18764
James Smart2a9bf3d2010-06-07 15:24:45 -040018765/**
18766 * lpfc_drain_txq - Drain the txq
18767 * @phba: Pointer to HBA context object.
18768 *
18769 * This function attempt to submit IOCBs on the txq
18770 * to the adapter. For SLI4 adapters, the txq contains
18771 * ELS IOCBs that have been deferred because the there
18772 * are no SGLs. This congestion can occur with large
18773 * vport counts during node discovery.
18774 **/
18775
18776uint32_t
18777lpfc_drain_txq(struct lpfc_hba *phba)
18778{
18779 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018780 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018781 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018782 unsigned long iflags = 0;
18783 char *fail_msg = NULL;
18784 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018785 union lpfc_wqe128 wqe128;
18786 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018787 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018788
James Smart895427b2017-02-12 13:52:30 -080018789 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070018790 if (unlikely(!pring))
18791 return 0;
James Smart895427b2017-02-12 13:52:30 -080018792
James Smart398d81c2013-05-31 17:04:19 -040018793 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018794 list_for_each_entry(piocbq, &pring->txq, list) {
18795 txq_cnt++;
18796 }
18797
18798 if (txq_cnt > pring->txq_max)
18799 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018800
James Smart398d81c2013-05-31 17:04:19 -040018801 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018802
James Smart0e9bb8d2013-03-01 16:35:12 -050018803 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018804 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018805
James Smart19ca7602010-11-20 23:11:55 -050018806 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018807 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018808 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18810 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018811 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018812 break;
18813 }
James Smart895427b2017-02-12 13:52:30 -080018814 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018815 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018816 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018817 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018818 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018819 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018820 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018821
18822 /* The xri and iocb resources secured,
18823 * attempt to issue request
18824 */
James Smart6d368e52011-05-24 11:44:12 -040018825 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018826 piocbq->sli4_xritag = sglq->sli4_xritag;
18827 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18828 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018829 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018830 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018831 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018832 fail_msg = " - Wq is full";
18833 else
18834 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18835
18836 if (fail_msg) {
18837 /* Failed means we can't issue and need to cancel */
18838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18839 "2822 IOCB failed %s iotag 0x%x "
18840 "xri 0x%x\n",
18841 fail_msg,
18842 piocbq->iotag, piocbq->sli4_xritag);
18843 list_add_tail(&piocbq->list, &completions);
18844 }
James Smart398d81c2013-05-31 17:04:19 -040018845 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018846 }
18847
James Smart2a9bf3d2010-06-07 15:24:45 -040018848 /* Cancel all the IOCBs that cannot be issued */
18849 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18850 IOERR_SLI_ABORTED);
18851
James Smart0e9bb8d2013-03-01 16:35:12 -050018852 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018853}
James Smart895427b2017-02-12 13:52:30 -080018854
18855/**
18856 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18857 * @phba: Pointer to HBA context object.
18858 * @pwqe: Pointer to command WQE.
18859 * @sglq: Pointer to the scatter gather queue object.
18860 *
18861 * This routine converts the bpl or bde that is in the WQE
18862 * to a sgl list for the sli4 hardware. The physical address
18863 * of the bpl/bde is converted back to a virtual address.
18864 * If the WQE contains a BPL then the list of BDE's is
18865 * converted to sli4_sge's. If the WQE contains a single
18866 * BDE then it is converted to a single sli_sge.
18867 * The WQE is still in cpu endianness so the contents of
18868 * the bpl can be used without byte swapping.
18869 *
18870 * Returns valid XRI = Success, NO_XRI = Failure.
18871 */
18872static uint16_t
18873lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18874 struct lpfc_sglq *sglq)
18875{
18876 uint16_t xritag = NO_XRI;
18877 struct ulp_bde64 *bpl = NULL;
18878 struct ulp_bde64 bde;
18879 struct sli4_sge *sgl = NULL;
18880 struct lpfc_dmabuf *dmabuf;
18881 union lpfc_wqe *wqe;
18882 int numBdes = 0;
18883 int i = 0;
18884 uint32_t offset = 0; /* accumulated offset in the sg request list */
18885 int inbound = 0; /* number of sg reply entries inbound from firmware */
18886 uint32_t cmd;
18887
18888 if (!pwqeq || !sglq)
18889 return xritag;
18890
18891 sgl = (struct sli4_sge *)sglq->sgl;
18892 wqe = &pwqeq->wqe;
18893 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18894
18895 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18896 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18897 return sglq->sli4_xritag;
18898 numBdes = pwqeq->rsvd2;
18899 if (numBdes) {
18900 /* The addrHigh and addrLow fields within the WQE
18901 * have not been byteswapped yet so there is no
18902 * need to swap them back.
18903 */
18904 if (pwqeq->context3)
18905 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18906 else
18907 return xritag;
18908
18909 bpl = (struct ulp_bde64 *)dmabuf->virt;
18910 if (!bpl)
18911 return xritag;
18912
18913 for (i = 0; i < numBdes; i++) {
18914 /* Should already be byte swapped. */
18915 sgl->addr_hi = bpl->addrHigh;
18916 sgl->addr_lo = bpl->addrLow;
18917
18918 sgl->word2 = le32_to_cpu(sgl->word2);
18919 if ((i+1) == numBdes)
18920 bf_set(lpfc_sli4_sge_last, sgl, 1);
18921 else
18922 bf_set(lpfc_sli4_sge_last, sgl, 0);
18923 /* swap the size field back to the cpu so we
18924 * can assign it to the sgl.
18925 */
18926 bde.tus.w = le32_to_cpu(bpl->tus.w);
18927 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18928 /* The offsets in the sgl need to be accumulated
18929 * separately for the request and reply lists.
18930 * The request is always first, the reply follows.
18931 */
18932 switch (cmd) {
18933 case CMD_GEN_REQUEST64_WQE:
18934 /* add up the reply sg entries */
18935 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18936 inbound++;
18937 /* first inbound? reset the offset */
18938 if (inbound == 1)
18939 offset = 0;
18940 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18941 bf_set(lpfc_sli4_sge_type, sgl,
18942 LPFC_SGE_TYPE_DATA);
18943 offset += bde.tus.f.bdeSize;
18944 break;
18945 case CMD_FCP_TRSP64_WQE:
18946 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18947 bf_set(lpfc_sli4_sge_type, sgl,
18948 LPFC_SGE_TYPE_DATA);
18949 break;
18950 case CMD_FCP_TSEND64_WQE:
18951 case CMD_FCP_TRECEIVE64_WQE:
18952 bf_set(lpfc_sli4_sge_type, sgl,
18953 bpl->tus.f.bdeFlags);
18954 if (i < 3)
18955 offset = 0;
18956 else
18957 offset += bde.tus.f.bdeSize;
18958 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18959 break;
18960 }
18961 sgl->word2 = cpu_to_le32(sgl->word2);
18962 bpl++;
18963 sgl++;
18964 }
18965 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18966 /* The addrHigh and addrLow fields of the BDE have not
18967 * been byteswapped yet so they need to be swapped
18968 * before putting them in the sgl.
18969 */
18970 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18971 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18972 sgl->word2 = le32_to_cpu(sgl->word2);
18973 bf_set(lpfc_sli4_sge_last, sgl, 1);
18974 sgl->word2 = cpu_to_le32(sgl->word2);
18975 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18976 }
18977 return sglq->sli4_xritag;
18978}
18979
18980/**
18981 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18982 * @phba: Pointer to HBA context object.
18983 * @ring_number: Base sli ring number
18984 * @pwqe: Pointer to command WQE.
18985 **/
18986int
18987lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18988 struct lpfc_iocbq *pwqe)
18989{
18990 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018991 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018992 struct lpfc_queue *wq;
18993 struct lpfc_sglq *sglq;
18994 struct lpfc_sli_ring *pring;
18995 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070018996 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080018997
18998 /* NVME_LS and NVME_LS ABTS requests. */
18999 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19000 pring = phba->sli4_hba.nvmels_wq->pring;
19001 spin_lock_irqsave(&pring->ring_lock, iflags);
19002 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19003 if (!sglq) {
19004 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19005 return WQE_BUSY;
19006 }
19007 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19008 pwqe->sli4_xritag = sglq->sli4_xritag;
19009 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19010 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19011 return WQE_ERROR;
19012 }
19013 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19014 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019015 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19016 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019017 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019018 return ret;
James Smart895427b2017-02-12 13:52:30 -080019019 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019020
James Smart895427b2017-02-12 13:52:30 -080019021 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19022 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19023 return 0;
19024 }
19025
19026 /* NVME_FCREQ and NVME_ABTS requests */
19027 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19028 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19029 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19030
19031 spin_lock_irqsave(&pring->ring_lock, iflags);
19032 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19033 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19034 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019035 ret = lpfc_sli4_wq_put(wq, wqe);
19036 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019037 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019038 return ret;
James Smart895427b2017-02-12 13:52:30 -080019039 }
19040 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19041 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19042 return 0;
19043 }
19044
James Smartf358dd02017-02-12 13:52:34 -080019045 /* NVMET requests */
19046 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19047 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19048 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19049
19050 spin_lock_irqsave(&pring->ring_lock, iflags);
19051 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019052 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019053 if (pwqe->sli4_xritag == NO_XRI) {
19054 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19055 pwqe->sli4_xritag = sglq->sli4_xritag;
19056 }
19057 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19058 pwqe->sli4_xritag);
19059 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19060 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19061 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019062 ret = lpfc_sli4_wq_put(wq, wqe);
19063 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019064 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019065 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019066 }
19067 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19068 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19069 return 0;
19070 }
James Smart895427b2017-02-12 13:52:30 -080019071 return WQE_ERROR;
19072}