blob: 8b2919a553d60c4e596e6a942aefca60d2743acc [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 Smarta8e497d2008-08-24 21:50:11 -04003781
3782 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003783 /* Indicate the I/O queues are flushed */
3784 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003785 spin_unlock_irq(&phba->hbalock);
3786
James Smartdb55fba2014-04-04 13:52:02 -04003787 /* Look on all the FCP Rings for the iotag */
3788 if (phba->sli_rev >= LPFC_SLI_REV4) {
3789 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003790 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003791
James Smartdb55fba2014-04-04 13:52:02 -04003792 spin_lock_irq(&pring->ring_lock);
3793 /* Retrieve everything on txq */
3794 list_splice_init(&pring->txq, &txq);
3795 /* Retrieve everything on the txcmplq */
3796 list_splice_init(&pring->txcmplq, &txcmplq);
3797 pring->txq_cnt = 0;
3798 pring->txcmplq_cnt = 0;
3799 spin_unlock_irq(&pring->ring_lock);
3800
3801 /* Flush the txq */
3802 lpfc_sli_cancel_iocbs(phba, &txq,
3803 IOSTAT_LOCAL_REJECT,
3804 IOERR_SLI_DOWN);
3805 /* Flush the txcmpq */
3806 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3807 IOSTAT_LOCAL_REJECT,
3808 IOERR_SLI_DOWN);
3809 }
3810 } else {
James Smart895427b2017-02-12 13:52:30 -08003811 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003812
3813 spin_lock_irq(&phba->hbalock);
3814 /* Retrieve everything on txq */
3815 list_splice_init(&pring->txq, &txq);
3816 /* Retrieve everything on the txcmplq */
3817 list_splice_init(&pring->txcmplq, &txcmplq);
3818 pring->txq_cnt = 0;
3819 pring->txcmplq_cnt = 0;
3820 spin_unlock_irq(&phba->hbalock);
3821
3822 /* Flush the txq */
3823 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3824 IOERR_SLI_DOWN);
3825 /* Flush the txcmpq */
3826 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3827 IOERR_SLI_DOWN);
3828 }
James Smarta8e497d2008-08-24 21:50:11 -04003829}
3830
3831/**
James Smart895427b2017-02-12 13:52:30 -08003832 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3833 * @phba: Pointer to HBA context object.
3834 *
3835 * This function flushes all wqes in the nvme rings and frees all resources
3836 * in the txcmplq. This function does not issue abort wqes for the IO
3837 * commands in txcmplq, they will just be returned with
3838 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3839 * slot has been permanently disabled.
3840 **/
3841void
3842lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3843{
3844 LIST_HEAD(txcmplq);
3845 struct lpfc_sli_ring *pring;
3846 uint32_t i;
3847
3848 if (phba->sli_rev < LPFC_SLI_REV4)
3849 return;
3850
3851 /* Hint to other driver operations that a flush is in progress. */
3852 spin_lock_irq(&phba->hbalock);
3853 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3854 spin_unlock_irq(&phba->hbalock);
3855
3856 /* Cycle through all NVME rings and complete each IO with
3857 * a local driver reason code. This is a flush so no
3858 * abort exchange to FW.
3859 */
3860 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3861 pring = phba->sli4_hba.nvme_wq[i]->pring;
3862
3863 /* Retrieve everything on the txcmplq */
3864 spin_lock_irq(&pring->ring_lock);
3865 list_splice_init(&pring->txcmplq, &txcmplq);
3866 pring->txcmplq_cnt = 0;
3867 spin_unlock_irq(&pring->ring_lock);
3868
3869 /* Flush the txcmpq &&&PAE */
3870 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3871 IOSTAT_LOCAL_REJECT,
3872 IOERR_SLI_DOWN);
3873 }
3874}
3875
3876/**
James Smart3772a992009-05-22 14:50:54 -04003877 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003878 * @phba: Pointer to HBA context object.
3879 * @mask: Bit mask to be checked.
3880 *
3881 * This function reads the host status register and compares
3882 * with the provided bit mask to check if HBA completed
3883 * the restart. This function will wait in a loop for the
3884 * HBA to complete restart. If the HBA does not restart within
3885 * 15 iterations, the function will reset the HBA again. The
3886 * function returns 1 when HBA fail to restart otherwise returns
3887 * zero.
3888 **/
James Smart3772a992009-05-22 14:50:54 -04003889static int
3890lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003891{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003892 uint32_t status;
3893 int i = 0;
3894 int retval = 0;
dea31012005-04-17 16:05:31 -05003895
Jamie Wellnitz41415862006-02-28 19:25:27 -05003896 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003897 if (lpfc_readl(phba->HSregaddr, &status))
3898 return 1;
dea31012005-04-17 16:05:31 -05003899
Jamie Wellnitz41415862006-02-28 19:25:27 -05003900 /*
3901 * Check status register every 100ms for 5 retries, then every
3902 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3903 * every 2.5 sec for 4.
3904 * Break our of the loop if errors occurred during init.
3905 */
3906 while (((status & mask) != mask) &&
3907 !(status & HS_FFERM) &&
3908 i++ < 20) {
dea31012005-04-17 16:05:31 -05003909
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910 if (i <= 5)
3911 msleep(10);
3912 else if (i <= 10)
3913 msleep(500);
3914 else
3915 msleep(2500);
dea31012005-04-17 16:05:31 -05003916
Jamie Wellnitz41415862006-02-28 19:25:27 -05003917 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003918 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003919 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920 lpfc_sli_brdrestart(phba);
3921 }
3922 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003923 if (lpfc_readl(phba->HSregaddr, &status)) {
3924 retval = 1;
3925 break;
3926 }
dea31012005-04-17 16:05:31 -05003927 }
dea31012005-04-17 16:05:31 -05003928
Jamie Wellnitz41415862006-02-28 19:25:27 -05003929 /* Check to see if any errors occurred during init */
3930 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3932 "2751 Adapter failed to restart, "
3933 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3934 status,
3935 readl(phba->MBslimaddr + 0xa8),
3936 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003937 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003938 retval = 1;
3939 }
dea31012005-04-17 16:05:31 -05003940
Jamie Wellnitz41415862006-02-28 19:25:27 -05003941 return retval;
dea31012005-04-17 16:05:31 -05003942}
3943
James Smartda0436e2009-05-22 14:51:39 -04003944/**
3945 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3946 * @phba: Pointer to HBA context object.
3947 * @mask: Bit mask to be checked.
3948 *
3949 * This function checks the host status register to check if HBA is
3950 * ready. This function will wait in a loop for the HBA to be ready
3951 * If the HBA is not ready , the function will will reset the HBA PCI
3952 * function again. The function returns 1 when HBA fail to be ready
3953 * otherwise returns zero.
3954 **/
3955static int
3956lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3957{
3958 uint32_t status;
3959 int retval = 0;
3960
3961 /* Read the HBA Host Status Register */
3962 status = lpfc_sli4_post_status_check(phba);
3963
3964 if (status) {
3965 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3966 lpfc_sli_brdrestart(phba);
3967 status = lpfc_sli4_post_status_check(phba);
3968 }
3969
3970 /* Check to see if any errors occurred during init */
3971 if (status) {
3972 phba->link_state = LPFC_HBA_ERROR;
3973 retval = 1;
3974 } else
3975 phba->sli4_hba.intr_enable = 0;
3976
3977 return retval;
3978}
3979
3980/**
3981 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3982 * @phba: Pointer to HBA context object.
3983 * @mask: Bit mask to be checked.
3984 *
3985 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3986 * from the API jump table function pointer from the lpfc_hba struct.
3987 **/
3988int
3989lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3990{
3991 return phba->lpfc_sli_brdready(phba, mask);
3992}
3993
James Smart92908312006-03-07 15:04:13 -05003994#define BARRIER_TEST_PATTERN (0xdeadbeef)
3995
James Smarte59058c2008-08-24 21:49:00 -04003996/**
James Smart3621a712009-04-06 18:47:14 -04003997 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003998 * @phba: Pointer to HBA context object.
3999 *
James Smart1b511972011-12-13 13:23:09 -05004000 * This function is called before resetting an HBA. This function is called
4001 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004002 **/
James Smart2e0fef82007-06-17 19:56:36 -05004003void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004004{
James Smart65a29c12006-07-06 15:50:50 -04004005 uint32_t __iomem *resp_buf;
4006 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004007 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004008 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004009 int i;
4010 uint8_t hdrtype;
4011
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004012 lockdep_assert_held(&phba->hbalock);
4013
James Smart92908312006-03-07 15:04:13 -05004014 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4015 if (hdrtype != 0x80 ||
4016 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4017 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4018 return;
4019
4020 /*
4021 * Tell the other part of the chip to suspend temporarily all
4022 * its DMA activity.
4023 */
James Smart65a29c12006-07-06 15:50:50 -04004024 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004025
4026 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004027 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4028 return;
James Smart92908312006-03-07 15:04:13 -05004029 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4030 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004031 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004032
James Smart9940b972011-03-11 16:06:12 -05004033 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4034 return;
4035 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004036 /* Clear Chip error bit */
4037 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004038 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004039 }
4040
4041 mbox = 0;
4042 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4043 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4044
4045 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004046 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004047 writel(mbox, mbox_buf);
4048
James Smart9940b972011-03-11 16:06:12 -05004049 for (i = 0; i < 50; i++) {
4050 if (lpfc_readl((resp_buf + 1), &resp_data))
4051 return;
4052 if (resp_data != ~(BARRIER_TEST_PATTERN))
4053 mdelay(1);
4054 else
4055 break;
4056 }
4057 resp_data = 0;
4058 if (lpfc_readl((resp_buf + 1), &resp_data))
4059 return;
4060 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004061 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004062 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004063 goto restore_hc;
4064 else
4065 goto clear_errat;
4066 }
4067
4068 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004069 resp_data = 0;
4070 for (i = 0; i < 500; i++) {
4071 if (lpfc_readl(resp_buf, &resp_data))
4072 return;
4073 if (resp_data != mbox)
4074 mdelay(1);
4075 else
4076 break;
4077 }
James Smart92908312006-03-07 15:04:13 -05004078
4079clear_errat:
4080
James Smart9940b972011-03-11 16:06:12 -05004081 while (++i < 500) {
4082 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4083 return;
4084 if (!(ha_copy & HA_ERATT))
4085 mdelay(1);
4086 else
4087 break;
4088 }
James Smart92908312006-03-07 15:04:13 -05004089
4090 if (readl(phba->HAregaddr) & HA_ERATT) {
4091 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004092 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004093 }
4094
4095restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004096 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004097 writel(hc_copy, phba->HCregaddr);
4098 readl(phba->HCregaddr); /* flush */
4099}
4100
James Smarte59058c2008-08-24 21:49:00 -04004101/**
James Smart3621a712009-04-06 18:47:14 -04004102 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004103 * @phba: Pointer to HBA context object.
4104 *
4105 * This function issues a kill_board mailbox command and waits for
4106 * the error attention interrupt. This function is called for stopping
4107 * the firmware processing. The caller is not required to hold any
4108 * locks. This function calls lpfc_hba_down_post function to free
4109 * any pending commands after the kill. The function will return 1 when it
4110 * fails to kill the board else will return 0.
4111 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004112int
James Smart2e0fef82007-06-17 19:56:36 -05004113lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004114{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004115 struct lpfc_sli *psli;
4116 LPFC_MBOXQ_t *pmb;
4117 uint32_t status;
4118 uint32_t ha_copy;
4119 int retval;
4120 int i = 0;
4121
4122 psli = &phba->sli;
4123
4124 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004125 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004126 "0329 Kill HBA Data: x%x x%x\n",
4127 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004128
James Smart98c9ea52007-10-27 13:37:33 -04004129 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4130 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004131 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004132
4133 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004134 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004135 if (lpfc_readl(phba->HCregaddr, &status)) {
4136 spin_unlock_irq(&phba->hbalock);
4137 mempool_free(pmb, phba->mbox_mem_pool);
4138 return 1;
4139 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004140 status &= ~HC_ERINT_ENA;
4141 writel(status, phba->HCregaddr);
4142 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004143 phba->link_flag |= LS_IGNORE_ERATT;
4144 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004145
4146 lpfc_kill_board(phba, pmb);
4147 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4148 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4149
4150 if (retval != MBX_SUCCESS) {
4151 if (retval != MBX_BUSY)
4152 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4154 "2752 KILL_BOARD command failed retval %d\n",
4155 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004156 spin_lock_irq(&phba->hbalock);
4157 phba->link_flag &= ~LS_IGNORE_ERATT;
4158 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004159 return 1;
4160 }
4161
James Smartf4b4c682009-05-22 14:53:12 -04004162 spin_lock_irq(&phba->hbalock);
4163 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4164 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004165
Jamie Wellnitz41415862006-02-28 19:25:27 -05004166 mempool_free(pmb, phba->mbox_mem_pool);
4167
4168 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4169 * attention every 100ms for 3 seconds. If we don't get ERATT after
4170 * 3 seconds we still set HBA_ERROR state because the status of the
4171 * board is now undefined.
4172 */
James Smart9940b972011-03-11 16:06:12 -05004173 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4174 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004175 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4176 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004177 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4178 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004179 }
4180
4181 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004182 if (ha_copy & HA_ERATT) {
4183 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004184 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004185 }
James Smart2e0fef82007-06-17 19:56:36 -05004186 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004187 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004188 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004189 phba->link_flag &= ~LS_IGNORE_ERATT;
4190 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004191
Jamie Wellnitz41415862006-02-28 19:25:27 -05004192 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004193 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004194
James Smart2e0fef82007-06-17 19:56:36 -05004195 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004196}
4197
James Smarte59058c2008-08-24 21:49:00 -04004198/**
James Smart3772a992009-05-22 14:50:54 -04004199 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004200 * @phba: Pointer to HBA context object.
4201 *
4202 * This function resets the HBA by writing HC_INITFF to the control
4203 * register. After the HBA resets, this function resets all the iocb ring
4204 * indices. This function disables PCI layer parity checking during
4205 * the reset.
4206 * This function returns 0 always.
4207 * The caller is not required to hold any locks.
4208 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004209int
James Smart2e0fef82007-06-17 19:56:36 -05004210lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004211{
4212 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004213 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004214 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004215 int i;
dea31012005-04-17 16:05:31 -05004216
Jamie Wellnitz41415862006-02-28 19:25:27 -05004217 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004218
Jamie Wellnitz41415862006-02-28 19:25:27 -05004219 /* Reset HBA */
4220 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004221 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004222 (phba->pport) ? phba->pport->port_state : 0,
4223 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004224
4225 /* perform board reset */
4226 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004227 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004228 if (phba->pport) {
4229 phba->pport->fc_myDID = 0;
4230 phba->pport->fc_prevDID = 0;
4231 }
dea31012005-04-17 16:05:31 -05004232
Jamie Wellnitz41415862006-02-28 19:25:27 -05004233 /* Turn off parity checking and serr during the physical reset */
4234 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4235 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4236 (cfg_value &
4237 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4238
James Smart3772a992009-05-22 14:50:54 -04004239 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4240
Jamie Wellnitz41415862006-02-28 19:25:27 -05004241 /* Now toggle INITFF bit in the Host Control Register */
4242 writel(HC_INITFF, phba->HCregaddr);
4243 mdelay(1);
4244 readl(phba->HCregaddr); /* flush */
4245 writel(0, phba->HCregaddr);
4246 readl(phba->HCregaddr); /* flush */
4247
4248 /* Restore PCI cmd register */
4249 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004250
4251 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004252 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004253 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004254 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004255 pring->sli.sli3.rspidx = 0;
4256 pring->sli.sli3.next_cmdidx = 0;
4257 pring->sli.sli3.local_getidx = 0;
4258 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004259 pring->missbufcnt = 0;
4260 }
dea31012005-04-17 16:05:31 -05004261
James Smart2e0fef82007-06-17 19:56:36 -05004262 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004263 return 0;
4264}
4265
James Smarte59058c2008-08-24 21:49:00 -04004266/**
James Smartda0436e2009-05-22 14:51:39 -04004267 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4268 * @phba: Pointer to HBA context object.
4269 *
4270 * This function resets a SLI4 HBA. This function disables PCI layer parity
4271 * checking during resets the device. The caller is not required to hold
4272 * any locks.
4273 *
4274 * This function returns 0 always.
4275 **/
4276int
4277lpfc_sli4_brdreset(struct lpfc_hba *phba)
4278{
4279 struct lpfc_sli *psli = &phba->sli;
4280 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004281 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004282
4283 /* Reset HBA */
4284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004285 "0295 Reset HBA Data: x%x x%x x%x\n",
4286 phba->pport->port_state, psli->sli_flag,
4287 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004288
4289 /* perform board reset */
4290 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004291 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004292 phba->pport->fc_myDID = 0;
4293 phba->pport->fc_prevDID = 0;
4294
James Smartda0436e2009-05-22 14:51:39 -04004295 spin_lock_irq(&phba->hbalock);
4296 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4297 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004298 spin_unlock_irq(&phba->hbalock);
4299
James Smart02936352014-04-04 13:52:12 -04004300 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4301 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4302 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4303 return rc;
4304 }
4305
James Smartda0436e2009-05-22 14:51:39 -04004306 /* Now physically reset the device */
4307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4308 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004309
4310 /* Turn off parity checking and serr during the physical reset */
4311 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4312 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4313 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4314
James Smart88318812012-09-29 11:29:29 -04004315 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004316 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004317
James Smartbe858b62010-12-15 17:57:20 -05004318 /* Restore PCI cmd register */
4319 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4320
James Smart27b01b82012-05-09 21:19:44 -04004321 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004322}
4323
4324/**
4325 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004326 * @phba: Pointer to HBA context object.
4327 *
4328 * This function is called in the SLI initialization code path to
4329 * restart the HBA. The caller is not required to hold any lock.
4330 * This function writes MBX_RESTART mailbox command to the SLIM and
4331 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4332 * function to free any pending commands. The function enables
4333 * POST only during the first initialization. The function returns zero.
4334 * The function does not guarantee completion of MBX_RESTART mailbox
4335 * command before the return of this function.
4336 **/
James Smartda0436e2009-05-22 14:51:39 -04004337static int
4338lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004339{
4340 MAILBOX_t *mb;
4341 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004342 volatile uint32_t word0;
4343 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004344 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345
James Smart2e0fef82007-06-17 19:56:36 -05004346 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004347
James Smart0d878412009-10-02 15:16:56 -04004348 /* Take PCIe device Advanced Error Reporting (AER) state */
4349 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4350
Jamie Wellnitz41415862006-02-28 19:25:27 -05004351 psli = &phba->sli;
4352
4353 /* Restart HBA */
4354 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004355 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004356 (phba->pport) ? phba->pport->port_state : 0,
4357 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004358
4359 word0 = 0;
4360 mb = (MAILBOX_t *) &word0;
4361 mb->mbxCommand = MBX_RESTART;
4362 mb->mbxHc = 1;
4363
James Smart92908312006-03-07 15:04:13 -05004364 lpfc_reset_barrier(phba);
4365
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366 to_slim = phba->MBslimaddr;
4367 writel(*(uint32_t *) mb, to_slim);
4368 readl(to_slim); /* flush */
4369
4370 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004371 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004372 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004373 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004374 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004375 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004376 writel(*(uint32_t *) mb, to_slim);
4377 readl(to_slim); /* flush */
4378
4379 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004380 if (phba->pport)
4381 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004382 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004383 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004384 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385
James Smart64ba8812006-08-02 15:24:34 -04004386 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4387 psli->stats_start = get_seconds();
4388
James Smarteaf15d52008-12-04 22:39:29 -05004389 /* Give the INITFF and Post time to settle. */
4390 mdelay(100);
dea31012005-04-17 16:05:31 -05004391
James Smart0d878412009-10-02 15:16:56 -04004392 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4393 if (hba_aer_enabled)
4394 pci_disable_pcie_error_reporting(phba->pcidev);
4395
Jamie Wellnitz41415862006-02-28 19:25:27 -05004396 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004397
4398 return 0;
4399}
4400
James Smarte59058c2008-08-24 21:49:00 -04004401/**
James Smartda0436e2009-05-22 14:51:39 -04004402 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4403 * @phba: Pointer to HBA context object.
4404 *
4405 * This function is called in the SLI initialization code path to restart
4406 * a SLI4 HBA. The caller is not required to hold any lock.
4407 * At the end of the function, it calls lpfc_hba_down_post function to
4408 * free any pending commands.
4409 **/
4410static int
4411lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4412{
4413 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004414 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004415 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004416
4417 /* Restart HBA */
4418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4419 "0296 Restart HBA Data: x%x x%x\n",
4420 phba->pport->port_state, psli->sli_flag);
4421
James Smart75baf692010-06-08 18:31:21 -04004422 /* Take PCIe device Advanced Error Reporting (AER) state */
4423 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4424
James Smart27b01b82012-05-09 21:19:44 -04004425 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004426
4427 spin_lock_irq(&phba->hbalock);
4428 phba->pport->stopped = 0;
4429 phba->link_state = LPFC_INIT_START;
4430 phba->hba_flag = 0;
4431 spin_unlock_irq(&phba->hbalock);
4432
4433 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4434 psli->stats_start = get_seconds();
4435
James Smart75baf692010-06-08 18:31:21 -04004436 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4437 if (hba_aer_enabled)
4438 pci_disable_pcie_error_reporting(phba->pcidev);
4439
James Smartda0436e2009-05-22 14:51:39 -04004440 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004441 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004442
James Smart27b01b82012-05-09 21:19:44 -04004443 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004444}
4445
4446/**
4447 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4448 * @phba: Pointer to HBA context object.
4449 *
4450 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4451 * API jump table function pointer from the lpfc_hba struct.
4452**/
4453int
4454lpfc_sli_brdrestart(struct lpfc_hba *phba)
4455{
4456 return phba->lpfc_sli_brdrestart(phba);
4457}
4458
4459/**
James Smart3621a712009-04-06 18:47:14 -04004460 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004461 * @phba: Pointer to HBA context object.
4462 *
4463 * This function is called after a HBA restart to wait for successful
4464 * restart of the HBA. Successful restart of the HBA is indicated by
4465 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4466 * iteration, the function will restart the HBA again. The function returns
4467 * zero if HBA successfully restarted else returns negative error code.
4468 **/
James Smart4492b732017-04-27 15:08:26 -07004469int
dea31012005-04-17 16:05:31 -05004470lpfc_sli_chipset_init(struct lpfc_hba *phba)
4471{
4472 uint32_t status, i = 0;
4473
4474 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004475 if (lpfc_readl(phba->HSregaddr, &status))
4476 return -EIO;
dea31012005-04-17 16:05:31 -05004477
4478 /* Check status register to see what current state is */
4479 i = 0;
4480 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4481
James Smartdcf2a4e2010-09-29 11:18:53 -04004482 /* Check every 10ms for 10 retries, then every 100ms for 90
4483 * retries, then every 1 sec for 50 retires for a total of
4484 * ~60 seconds before reset the board again and check every
4485 * 1 sec for 50 retries. The up to 60 seconds before the
4486 * board ready is required by the Falcon FIPS zeroization
4487 * complete, and any reset the board in between shall cause
4488 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004489 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004490 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004491 /* Adapter failed to init, timeout, status reg
4492 <status> */
James Smarted957682007-06-17 19:56:37 -05004493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004494 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004495 "timeout, status reg x%x, "
4496 "FW Data: A8 x%x AC x%x\n", status,
4497 readl(phba->MBslimaddr + 0xa8),
4498 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004499 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004500 return -ETIMEDOUT;
4501 }
4502
4503 /* Check to see if any errors occurred during init */
4504 if (status & HS_FFERM) {
4505 /* ERROR: During chipset initialization */
4506 /* Adapter failed to init, chipset, status reg
4507 <status> */
James Smarted957682007-06-17 19:56:37 -05004508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004509 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004510 "chipset, status reg x%x, "
4511 "FW Data: A8 x%x AC x%x\n", status,
4512 readl(phba->MBslimaddr + 0xa8),
4513 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004514 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004515 return -EIO;
4516 }
4517
James Smartdcf2a4e2010-09-29 11:18:53 -04004518 if (i <= 10)
dea31012005-04-17 16:05:31 -05004519 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004520 else if (i <= 100)
4521 msleep(100);
4522 else
4523 msleep(1000);
dea31012005-04-17 16:05:31 -05004524
James Smartdcf2a4e2010-09-29 11:18:53 -04004525 if (i == 150) {
4526 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004527 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004528 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004529 }
4530 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004531 if (lpfc_readl(phba->HSregaddr, &status))
4532 return -EIO;
dea31012005-04-17 16:05:31 -05004533 }
4534
4535 /* Check to see if any errors occurred during init */
4536 if (status & HS_FFERM) {
4537 /* ERROR: During chipset initialization */
4538 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004540 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004541 "status reg x%x, "
4542 "FW Data: A8 x%x AC x%x\n", status,
4543 readl(phba->MBslimaddr + 0xa8),
4544 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004545 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004546 return -EIO;
4547 }
4548
4549 /* Clear all interrupt enable conditions */
4550 writel(0, phba->HCregaddr);
4551 readl(phba->HCregaddr); /* flush */
4552
4553 /* setup host attn register */
4554 writel(0xffffffff, phba->HAregaddr);
4555 readl(phba->HAregaddr); /* flush */
4556 return 0;
4557}
4558
James Smarte59058c2008-08-24 21:49:00 -04004559/**
James Smart3621a712009-04-06 18:47:14 -04004560 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004561 *
4562 * This function calculates and returns the number of HBQs required to be
4563 * configured.
4564 **/
James Smart78b2d852007-08-02 11:10:21 -04004565int
James Smarted957682007-06-17 19:56:37 -05004566lpfc_sli_hbq_count(void)
4567{
James Smart92d7f7b2007-06-17 19:56:38 -05004568 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004569}
4570
James Smarte59058c2008-08-24 21:49:00 -04004571/**
James Smart3621a712009-04-06 18:47:14 -04004572 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004573 *
4574 * This function adds the number of hbq entries in every HBQ to get
4575 * the total number of hbq entries required for the HBA and returns
4576 * the total count.
4577 **/
James Smarted957682007-06-17 19:56:37 -05004578static int
4579lpfc_sli_hbq_entry_count(void)
4580{
4581 int hbq_count = lpfc_sli_hbq_count();
4582 int count = 0;
4583 int i;
4584
4585 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004586 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004587 return count;
4588}
4589
James Smarte59058c2008-08-24 21:49:00 -04004590/**
James Smart3621a712009-04-06 18:47:14 -04004591 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004592 *
4593 * This function calculates amount of memory required for all hbq entries
4594 * to be configured and returns the total memory required.
4595 **/
dea31012005-04-17 16:05:31 -05004596int
James Smarted957682007-06-17 19:56:37 -05004597lpfc_sli_hbq_size(void)
4598{
4599 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4600}
4601
James Smarte59058c2008-08-24 21:49:00 -04004602/**
James Smart3621a712009-04-06 18:47:14 -04004603 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004604 * @phba: Pointer to HBA context object.
4605 *
4606 * This function is called during the SLI initialization to configure
4607 * all the HBQs and post buffers to the HBQ. The caller is not
4608 * required to hold any locks. This function will return zero if successful
4609 * else it will return negative error code.
4610 **/
James Smarted957682007-06-17 19:56:37 -05004611static int
4612lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4613{
4614 int hbq_count = lpfc_sli_hbq_count();
4615 LPFC_MBOXQ_t *pmb;
4616 MAILBOX_t *pmbox;
4617 uint32_t hbqno;
4618 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004619
James Smart92d7f7b2007-06-17 19:56:38 -05004620 /* Get a Mailbox buffer to setup mailbox
4621 * commands for HBA initialization
4622 */
James Smarted957682007-06-17 19:56:37 -05004623 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4624
4625 if (!pmb)
4626 return -ENOMEM;
4627
James Smart04c68492009-05-22 14:52:52 -04004628 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004629
4630 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4631 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004632 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004633
4634 hbq_entry_index = 0;
4635 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4636 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4637 phba->hbqs[hbqno].hbqPutIdx = 0;
4638 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4639 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004640 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004641 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4642 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004643 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4644
4645 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4646 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4647 mbxStatus <status>, ring <num> */
4648
4649 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004650 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004651 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004652 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004653 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004654 pmbox->mbxStatus, hbqno);
4655
4656 phba->link_state = LPFC_HBA_ERROR;
4657 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004658 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004659 }
4660 }
4661 phba->hbq_count = hbq_count;
4662
James Smarted957682007-06-17 19:56:37 -05004663 mempool_free(pmb, phba->mbox_mem_pool);
4664
James Smart92d7f7b2007-06-17 19:56:38 -05004665 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004666 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4667 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004668 return 0;
4669}
4670
James Smarte59058c2008-08-24 21:49:00 -04004671/**
James Smart4f774512009-05-22 14:52:35 -04004672 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4673 * @phba: Pointer to HBA context object.
4674 *
4675 * This function is called during the SLI initialization to configure
4676 * all the HBQs and post buffers to the HBQ. The caller is not
4677 * required to hold any locks. This function will return zero if successful
4678 * else it will return negative error code.
4679 **/
4680static int
4681lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4682{
4683 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004684 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4685 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004686 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004687 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004688 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004689 return 0;
4690}
4691
4692/**
James Smart3621a712009-04-06 18:47:14 -04004693 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004694 * @phba: Pointer to HBA context object.
4695 * @sli_mode: sli mode - 2/3
4696 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004697 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004698 * to issue config_port mailbox command. This function restarts the
4699 * HBA firmware and issues a config_port mailbox command to configure
4700 * the SLI interface in the sli mode specified by sli_mode
4701 * variable. The caller is not required to hold any locks.
4702 * The function returns 0 if successful, else returns negative error
4703 * code.
4704 **/
James Smart93996272008-08-24 21:50:30 -04004705int
4706lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004707{
4708 LPFC_MBOXQ_t *pmb;
4709 uint32_t resetcount = 0, rc = 0, done = 0;
4710
4711 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4712 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004713 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004714 return -ENOMEM;
4715 }
4716
James Smarted957682007-06-17 19:56:37 -05004717 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004718 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004719 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004720 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004721 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004722 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004723 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004724 rc = lpfc_sli_chipset_init(phba);
4725 if (rc)
4726 break;
4727
James Smart2e0fef82007-06-17 19:56:36 -05004728 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004729 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004730 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004731 resetcount++;
4732
James Smarted957682007-06-17 19:56:37 -05004733 /* Call pre CONFIG_PORT mailbox command initialization. A
4734 * value of 0 means the call was successful. Any other
4735 * nonzero value is a failure, but if ERESTART is returned,
4736 * the driver may reset the HBA and try again.
4737 */
dea31012005-04-17 16:05:31 -05004738 rc = lpfc_config_port_prep(phba);
4739 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004740 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004741 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004742 } else if (rc)
dea31012005-04-17 16:05:31 -05004743 break;
James Smart6d368e52011-05-24 11:44:12 -04004744
James Smart2e0fef82007-06-17 19:56:36 -05004745 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004746 lpfc_config_port(phba, pmb);
4747 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004748 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4749 LPFC_SLI3_HBQ_ENABLED |
4750 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004751 LPFC_SLI3_BG_ENABLED |
4752 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004753 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004755 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004756 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004757 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004758 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004759 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004760 spin_unlock_irq(&phba->hbalock);
4761 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004762 } else {
4763 /* Allow asynchronous mailbox command to go through */
4764 spin_lock_irq(&phba->hbalock);
4765 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4766 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004767 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004768
4769 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4770 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4771 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4772 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004773 }
dea31012005-04-17 16:05:31 -05004774 }
James Smarted957682007-06-17 19:56:37 -05004775 if (!done) {
4776 rc = -EINVAL;
4777 goto do_prep_failed;
4778 }
James Smart04c68492009-05-22 14:52:52 -04004779 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4780 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004781 rc = -ENXIO;
4782 goto do_prep_failed;
4783 }
James Smart04c68492009-05-22 14:52:52 -04004784 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004785 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004786 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4787 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4788 phba->max_vpi : phba->max_vports;
4789
James Smart34b02dc2008-08-24 21:49:55 -04004790 } else
4791 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004792 phba->fips_level = 0;
4793 phba->fips_spec_rev = 0;
4794 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004795 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004796 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4797 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4798 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4799 "2850 Security Crypto Active. FIPS x%d "
4800 "(Spec Rev: x%d)",
4801 phba->fips_level, phba->fips_spec_rev);
4802 }
4803 if (pmb->u.mb.un.varCfgPort.sec_err) {
4804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4805 "2856 Config Port Security Crypto "
4806 "Error: x%x ",
4807 pmb->u.mb.un.varCfgPort.sec_err);
4808 }
James Smart04c68492009-05-22 14:52:52 -04004809 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004810 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004811 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004812 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004813
4814 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4815 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004816
4817 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004818 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004819 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4820 else
4821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4822 "0443 Adapter did not grant "
4823 "BlockGuard\n");
4824 }
James Smart34b02dc2008-08-24 21:49:55 -04004825 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004826 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004827 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004828 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004829 }
James Smart92d7f7b2007-06-17 19:56:38 -05004830do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004831 mempool_free(pmb, phba->mbox_mem_pool);
4832 return rc;
4833}
4834
James Smarte59058c2008-08-24 21:49:00 -04004835
4836/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004837 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004838 * @phba: Pointer to HBA context object.
4839 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004840 * This function is the main SLI initialization function. This function
4841 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004842 * error attention handler code. Caller is not required to hold any
4843 * locks. This function issues config_port mailbox command to configure
4844 * the SLI, setup iocb rings and HBQ rings. In the end the function
4845 * calls the config_port_post function to issue init_link mailbox
4846 * command and to start the discovery. The function will return zero
4847 * if successful, else it will return negative error code.
4848 **/
James Smarted957682007-06-17 19:56:37 -05004849int
4850lpfc_sli_hba_setup(struct lpfc_hba *phba)
4851{
4852 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004853 int mode = 3, i;
4854 int longs;
James Smarted957682007-06-17 19:56:37 -05004855
James Smart12247e82016-07-06 12:36:09 -07004856 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004857 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004858 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004860 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004861 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004862 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004863 break;
4864 }
James Smarted957682007-06-17 19:56:37 -05004865 mode = 2;
4866 break;
4867 case 0:
4868 case 3:
4869 break;
4870 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004872 "1819 Unrecognized sli_mode parameter: %d.\n",
4873 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004874
4875 break;
4876 }
James Smartb5c53952016-03-31 14:12:30 -07004877 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004878
James Smart93996272008-08-24 21:50:30 -04004879 rc = lpfc_sli_config_port(phba, mode);
4880
James Smart12247e82016-07-06 12:36:09 -07004881 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004883 "1820 Unable to select SLI-3. "
4884 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004885 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004886 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004887 else if (rc && mode == 2)
4888 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004889 if (rc)
dea31012005-04-17 16:05:31 -05004890 goto lpfc_sli_hba_setup_error;
4891
James Smart0d878412009-10-02 15:16:56 -04004892 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4893 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4894 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4895 if (!rc) {
4896 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4897 "2709 This device supports "
4898 "Advanced Error Reporting (AER)\n");
4899 spin_lock_irq(&phba->hbalock);
4900 phba->hba_flag |= HBA_AER_ENABLED;
4901 spin_unlock_irq(&phba->hbalock);
4902 } else {
4903 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4904 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004905 "Advanced Error Reporting (AER): %d\n",
4906 rc);
James Smart0d878412009-10-02 15:16:56 -04004907 phba->cfg_aer_support = 0;
4908 }
4909 }
4910
James Smarted957682007-06-17 19:56:37 -05004911 if (phba->sli_rev == 3) {
4912 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4913 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004914 } else {
4915 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4916 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004917 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004918 }
4919
4920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004921 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4922 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004923 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004924
4925 if (rc)
4926 goto lpfc_sli_hba_setup_error;
4927
James Smart6d368e52011-05-24 11:44:12 -04004928 /* Initialize VPIs. */
4929 if (phba->sli_rev == LPFC_SLI_REV3) {
4930 /*
4931 * The VPI bitmask and physical ID array are allocated
4932 * and initialized once only - at driver load. A port
4933 * reset doesn't need to reinitialize this memory.
4934 */
4935 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4936 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4937 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4938 GFP_KERNEL);
4939 if (!phba->vpi_bmask) {
4940 rc = -ENOMEM;
4941 goto lpfc_sli_hba_setup_error;
4942 }
4943
4944 phba->vpi_ids = kzalloc(
4945 (phba->max_vpi+1) * sizeof(uint16_t),
4946 GFP_KERNEL);
4947 if (!phba->vpi_ids) {
4948 kfree(phba->vpi_bmask);
4949 rc = -ENOMEM;
4950 goto lpfc_sli_hba_setup_error;
4951 }
4952 for (i = 0; i < phba->max_vpi; i++)
4953 phba->vpi_ids[i] = i;
4954 }
4955 }
4956
James Smart93996272008-08-24 21:50:30 -04004957 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004958 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4959 rc = lpfc_sli_hbq_setup(phba);
4960 if (rc)
4961 goto lpfc_sli_hba_setup_error;
4962 }
James Smart04c68492009-05-22 14:52:52 -04004963 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004964 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004965 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004966
4967 rc = lpfc_config_port_post(phba);
4968 if (rc)
4969 goto lpfc_sli_hba_setup_error;
4970
James Smarted957682007-06-17 19:56:37 -05004971 return rc;
4972
James Smart92d7f7b2007-06-17 19:56:38 -05004973lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004974 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004976 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004977 return rc;
4978}
4979
James Smartda0436e2009-05-22 14:51:39 -04004980/**
4981 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4982 * @phba: Pointer to HBA context object.
4983 * @mboxq: mailbox pointer.
4984 * This function issue a dump mailbox command to read config region
4985 * 23 and parse the records in the region and populate driver
4986 * data structure.
4987 **/
4988static int
James Smartff78d8f2011-12-13 13:21:35 -05004989lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004990{
James Smartff78d8f2011-12-13 13:21:35 -05004991 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004992 struct lpfc_dmabuf *mp;
4993 struct lpfc_mqe *mqe;
4994 uint32_t data_length;
4995 int rc;
4996
4997 /* Program the default value of vlan_id and fc_map */
4998 phba->valid_vlan = 0;
4999 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5000 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5001 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5002
James Smartff78d8f2011-12-13 13:21:35 -05005003 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5004 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005005 return -ENOMEM;
5006
James Smartff78d8f2011-12-13 13:21:35 -05005007 mqe = &mboxq->u.mqe;
5008 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5009 rc = -ENOMEM;
5010 goto out_free_mboxq;
5011 }
5012
James Smartda0436e2009-05-22 14:51:39 -04005013 mp = (struct lpfc_dmabuf *) mboxq->context1;
5014 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5015
5016 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5017 "(%d):2571 Mailbox cmd x%x Status x%x "
5018 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5019 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5020 "CQ: x%x x%x x%x x%x\n",
5021 mboxq->vport ? mboxq->vport->vpi : 0,
5022 bf_get(lpfc_mqe_command, mqe),
5023 bf_get(lpfc_mqe_status, mqe),
5024 mqe->un.mb_words[0], mqe->un.mb_words[1],
5025 mqe->un.mb_words[2], mqe->un.mb_words[3],
5026 mqe->un.mb_words[4], mqe->un.mb_words[5],
5027 mqe->un.mb_words[6], mqe->un.mb_words[7],
5028 mqe->un.mb_words[8], mqe->un.mb_words[9],
5029 mqe->un.mb_words[10], mqe->un.mb_words[11],
5030 mqe->un.mb_words[12], mqe->un.mb_words[13],
5031 mqe->un.mb_words[14], mqe->un.mb_words[15],
5032 mqe->un.mb_words[16], mqe->un.mb_words[50],
5033 mboxq->mcqe.word0,
5034 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5035 mboxq->mcqe.trailer);
5036
5037 if (rc) {
5038 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5039 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005040 rc = -EIO;
5041 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005042 }
5043 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005044 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005045 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5046 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005047 rc = -EIO;
5048 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005049 }
James Smartda0436e2009-05-22 14:51:39 -04005050
5051 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5052 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5053 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005054 rc = 0;
5055
5056out_free_mboxq:
5057 mempool_free(mboxq, phba->mbox_mem_pool);
5058 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005059}
5060
5061/**
5062 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5063 * @phba: pointer to lpfc hba data structure.
5064 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5065 * @vpd: pointer to the memory to hold resulting port vpd data.
5066 * @vpd_size: On input, the number of bytes allocated to @vpd.
5067 * On output, the number of data bytes in @vpd.
5068 *
5069 * This routine executes a READ_REV SLI4 mailbox command. In
5070 * addition, this routine gets the port vpd data.
5071 *
5072 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005073 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005074 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005075 **/
5076static int
5077lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5078 uint8_t *vpd, uint32_t *vpd_size)
5079{
5080 int rc = 0;
5081 uint32_t dma_size;
5082 struct lpfc_dmabuf *dmabuf;
5083 struct lpfc_mqe *mqe;
5084
5085 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5086 if (!dmabuf)
5087 return -ENOMEM;
5088
5089 /*
5090 * Get a DMA buffer for the vpd data resulting from the READ_REV
5091 * mailbox command.
5092 */
5093 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005094 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5095 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005096 if (!dmabuf->virt) {
5097 kfree(dmabuf);
5098 return -ENOMEM;
5099 }
James Smartda0436e2009-05-22 14:51:39 -04005100
5101 /*
5102 * The SLI4 implementation of READ_REV conflicts at word1,
5103 * bits 31:16 and SLI4 adds vpd functionality not present
5104 * in SLI3. This code corrects the conflicts.
5105 */
5106 lpfc_read_rev(phba, mboxq);
5107 mqe = &mboxq->u.mqe;
5108 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5109 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5110 mqe->un.read_rev.word1 &= 0x0000FFFF;
5111 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5112 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5113
5114 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5115 if (rc) {
5116 dma_free_coherent(&phba->pcidev->dev, dma_size,
5117 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005118 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005119 return -EIO;
5120 }
5121
James Smartda0436e2009-05-22 14:51:39 -04005122 /*
5123 * The available vpd length cannot be bigger than the
5124 * DMA buffer passed to the port. Catch the less than
5125 * case and update the caller's size.
5126 */
5127 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5128 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5129
James Smartd7c47992010-06-08 18:31:54 -04005130 memcpy(vpd, dmabuf->virt, *vpd_size);
5131
James Smartda0436e2009-05-22 14:51:39 -04005132 dma_free_coherent(&phba->pcidev->dev, dma_size,
5133 dmabuf->virt, dmabuf->phys);
5134 kfree(dmabuf);
5135 return 0;
5136}
5137
5138/**
James Smartcd1c8302011-10-10 21:33:25 -04005139 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5140 * @phba: pointer to lpfc hba data structure.
5141 *
5142 * This routine retrieves SLI4 device physical port name this PCI function
5143 * is attached to.
5144 *
5145 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005146 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005147 * otherwise - failed to retrieve physical port name
5148 **/
5149static int
5150lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5151{
5152 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005153 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5154 struct lpfc_controller_attribute *cntl_attr;
5155 struct lpfc_mbx_get_port_name *get_port_name;
5156 void *virtaddr = NULL;
5157 uint32_t alloclen, reqlen;
5158 uint32_t shdr_status, shdr_add_status;
5159 union lpfc_sli4_cfg_shdr *shdr;
5160 char cport_name = 0;
5161 int rc;
5162
5163 /* We assume nothing at this point */
5164 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5165 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5166
5167 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5168 if (!mboxq)
5169 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005170 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005171 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5172 lpfc_sli4_read_config(phba);
5173 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5174 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005175
5176 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5177 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5178 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5179 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5180 LPFC_SLI4_MBX_NEMBED);
5181 if (alloclen < reqlen) {
5182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5183 "3084 Allocated DMA memory size (%d) is "
5184 "less than the requested DMA memory size "
5185 "(%d)\n", alloclen, reqlen);
5186 rc = -ENOMEM;
5187 goto out_free_mboxq;
5188 }
5189 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5190 virtaddr = mboxq->sge_array->addr[0];
5191 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5192 shdr = &mbx_cntl_attr->cfg_shdr;
5193 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5194 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5195 if (shdr_status || shdr_add_status || rc) {
5196 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5197 "3085 Mailbox x%x (x%x/x%x) failed, "
5198 "rc:x%x, status:x%x, add_status:x%x\n",
5199 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5200 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5201 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5202 rc, shdr_status, shdr_add_status);
5203 rc = -ENXIO;
5204 goto out_free_mboxq;
5205 }
5206 cntl_attr = &mbx_cntl_attr->cntl_attr;
5207 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5208 phba->sli4_hba.lnk_info.lnk_tp =
5209 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5210 phba->sli4_hba.lnk_info.lnk_no =
5211 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5212 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5213 "3086 lnk_type:%d, lnk_numb:%d\n",
5214 phba->sli4_hba.lnk_info.lnk_tp,
5215 phba->sli4_hba.lnk_info.lnk_no);
5216
5217retrieve_ppname:
5218 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5219 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5220 sizeof(struct lpfc_mbx_get_port_name) -
5221 sizeof(struct lpfc_sli4_cfg_mhdr),
5222 LPFC_SLI4_MBX_EMBED);
5223 get_port_name = &mboxq->u.mqe.un.get_port_name;
5224 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5225 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5226 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5227 phba->sli4_hba.lnk_info.lnk_tp);
5228 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5229 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5231 if (shdr_status || shdr_add_status || rc) {
5232 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5233 "3087 Mailbox x%x (x%x/x%x) failed: "
5234 "rc:x%x, status:x%x, add_status:x%x\n",
5235 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5236 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5237 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5238 rc, shdr_status, shdr_add_status);
5239 rc = -ENXIO;
5240 goto out_free_mboxq;
5241 }
5242 switch (phba->sli4_hba.lnk_info.lnk_no) {
5243 case LPFC_LINK_NUMBER_0:
5244 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5245 &get_port_name->u.response);
5246 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5247 break;
5248 case LPFC_LINK_NUMBER_1:
5249 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5250 &get_port_name->u.response);
5251 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5252 break;
5253 case LPFC_LINK_NUMBER_2:
5254 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5255 &get_port_name->u.response);
5256 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5257 break;
5258 case LPFC_LINK_NUMBER_3:
5259 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5260 &get_port_name->u.response);
5261 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5262 break;
5263 default:
5264 break;
5265 }
5266
5267 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5268 phba->Port[0] = cport_name;
5269 phba->Port[1] = '\0';
5270 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5271 "3091 SLI get port name: %s\n", phba->Port);
5272 }
5273
5274out_free_mboxq:
5275 if (rc != MBX_TIMEOUT) {
5276 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5277 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5278 else
5279 mempool_free(mboxq, phba->mbox_mem_pool);
5280 }
5281 return rc;
5282}
5283
5284/**
James Smartda0436e2009-05-22 14:51:39 -04005285 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5286 * @phba: pointer to lpfc hba data structure.
5287 *
5288 * This routine is called to explicitly arm the SLI4 device's completion and
5289 * event queues
5290 **/
5291static void
5292lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5293{
James Smart895427b2017-02-12 13:52:30 -08005294 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005295
5296 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5297 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005298 if (phba->sli4_hba.nvmels_cq)
5299 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5300 LPFC_QUEUE_REARM);
5301
5302 if (phba->sli4_hba.fcp_cq)
5303 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5304 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5305 LPFC_QUEUE_REARM);
5306
5307 if (phba->sli4_hba.nvme_cq)
5308 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5309 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5310 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005311
James Smartf38fa0b2014-04-04 13:52:21 -04005312 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005313 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5314
James Smart895427b2017-02-12 13:52:30 -08005315 if (phba->sli4_hba.hba_eq)
5316 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5317 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5318 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005319
James Smart2d7dbc42017-02-12 13:52:35 -08005320 if (phba->nvmet_support) {
5321 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5322 lpfc_sli4_cq_release(
5323 phba->sli4_hba.nvmet_cqset[qidx],
5324 LPFC_QUEUE_REARM);
5325 }
James Smart2e90f4b2011-12-13 13:22:37 -05005326 }
James Smart1ba981f2014-02-20 09:56:45 -05005327
5328 if (phba->cfg_fof)
5329 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005330}
5331
5332/**
James Smart6d368e52011-05-24 11:44:12 -04005333 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5334 * @phba: Pointer to HBA context object.
5335 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005336 * @extnt_count: buffer to hold port available extent count.
5337 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005338 *
James Smartb76f2dc2011-07-22 18:37:42 -04005339 * This function calls the port and retrievs the number of available
5340 * extents and their size for a particular extent type.
5341 *
5342 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005343 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005344int
James Smart6d368e52011-05-24 11:44:12 -04005345lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5346 uint16_t *extnt_count, uint16_t *extnt_size)
5347{
5348 int rc = 0;
5349 uint32_t length;
5350 uint32_t mbox_tmo;
5351 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5352 LPFC_MBOXQ_t *mbox;
5353
5354 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5355 if (!mbox)
5356 return -ENOMEM;
5357
5358 /* Find out how many extents are available for this resource type */
5359 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5360 sizeof(struct lpfc_sli4_cfg_mhdr));
5361 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5362 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5363 length, LPFC_SLI4_MBX_EMBED);
5364
5365 /* Send an extents count of 0 - the GET doesn't use it. */
5366 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5367 LPFC_SLI4_MBX_EMBED);
5368 if (unlikely(rc)) {
5369 rc = -EIO;
5370 goto err_exit;
5371 }
5372
5373 if (!phba->sli4_hba.intr_enable)
5374 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5375 else {
James Smarta183a152011-10-10 21:32:43 -04005376 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005377 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5378 }
5379 if (unlikely(rc)) {
5380 rc = -EIO;
5381 goto err_exit;
5382 }
5383
5384 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5385 if (bf_get(lpfc_mbox_hdr_status,
5386 &rsrc_info->header.cfg_shdr.response)) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5388 "2930 Failed to get resource extents "
5389 "Status 0x%x Add'l Status 0x%x\n",
5390 bf_get(lpfc_mbox_hdr_status,
5391 &rsrc_info->header.cfg_shdr.response),
5392 bf_get(lpfc_mbox_hdr_add_status,
5393 &rsrc_info->header.cfg_shdr.response));
5394 rc = -EIO;
5395 goto err_exit;
5396 }
5397
5398 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5399 &rsrc_info->u.rsp);
5400 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5401 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005402
5403 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5404 "3162 Retrieved extents type-%d from port: count:%d, "
5405 "size:%d\n", type, *extnt_count, *extnt_size);
5406
5407err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005408 mempool_free(mbox, phba->mbox_mem_pool);
5409 return rc;
5410}
5411
5412/**
5413 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5414 * @phba: Pointer to HBA context object.
5415 * @type: The extent type to check.
5416 *
5417 * This function reads the current available extents from the port and checks
5418 * if the extent count or extent size has changed since the last access.
5419 * Callers use this routine post port reset to understand if there is a
5420 * extent reprovisioning requirement.
5421 *
5422 * Returns:
5423 * -Error: error indicates problem.
5424 * 1: Extent count or size has changed.
5425 * 0: No changes.
5426 **/
5427static int
5428lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5429{
5430 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5431 uint16_t size_diff, rsrc_ext_size;
5432 int rc = 0;
5433 struct lpfc_rsrc_blks *rsrc_entry;
5434 struct list_head *rsrc_blk_list = NULL;
5435
5436 size_diff = 0;
5437 curr_ext_cnt = 0;
5438 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5439 &rsrc_ext_cnt,
5440 &rsrc_ext_size);
5441 if (unlikely(rc))
5442 return -EIO;
5443
5444 switch (type) {
5445 case LPFC_RSC_TYPE_FCOE_RPI:
5446 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5447 break;
5448 case LPFC_RSC_TYPE_FCOE_VPI:
5449 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5450 break;
5451 case LPFC_RSC_TYPE_FCOE_XRI:
5452 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5453 break;
5454 case LPFC_RSC_TYPE_FCOE_VFI:
5455 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5456 break;
5457 default:
5458 break;
5459 }
5460
5461 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5462 curr_ext_cnt++;
5463 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5464 size_diff++;
5465 }
5466
5467 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5468 rc = 1;
5469
5470 return rc;
5471}
5472
5473/**
5474 * lpfc_sli4_cfg_post_extnts -
5475 * @phba: Pointer to HBA context object.
5476 * @extnt_cnt - number of available extents.
5477 * @type - the extent type (rpi, xri, vfi, vpi).
5478 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5479 * @mbox - pointer to the caller's allocated mailbox structure.
5480 *
5481 * This function executes the extents allocation request. It also
5482 * takes care of the amount of memory needed to allocate or get the
5483 * allocated extents. It is the caller's responsibility to evaluate
5484 * the response.
5485 *
5486 * Returns:
5487 * -Error: Error value describes the condition found.
5488 * 0: if successful
5489 **/
5490static int
James Smart8a9d2e82012-05-09 21:16:12 -04005491lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005492 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5493{
5494 int rc = 0;
5495 uint32_t req_len;
5496 uint32_t emb_len;
5497 uint32_t alloc_len, mbox_tmo;
5498
5499 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005500 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005501
5502 /*
5503 * Calculate the size of an embedded mailbox. The uint32_t
5504 * accounts for extents-specific word.
5505 */
5506 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5507 sizeof(uint32_t);
5508
5509 /*
5510 * Presume the allocation and response will fit into an embedded
5511 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5512 */
5513 *emb = LPFC_SLI4_MBX_EMBED;
5514 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005515 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005516 sizeof(union lpfc_sli4_cfg_shdr) +
5517 sizeof(uint32_t);
5518 *emb = LPFC_SLI4_MBX_NEMBED;
5519 }
5520
5521 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5522 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5523 req_len, *emb);
5524 if (alloc_len < req_len) {
5525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005526 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005527 "less than the requested DMA memory "
5528 "size (x%x)\n", alloc_len, req_len);
5529 return -ENOMEM;
5530 }
James Smart8a9d2e82012-05-09 21:16:12 -04005531 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005532 if (unlikely(rc))
5533 return -EIO;
5534
5535 if (!phba->sli4_hba.intr_enable)
5536 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5537 else {
James Smarta183a152011-10-10 21:32:43 -04005538 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005539 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5540 }
5541
5542 if (unlikely(rc))
5543 rc = -EIO;
5544 return rc;
5545}
5546
5547/**
5548 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5549 * @phba: Pointer to HBA context object.
5550 * @type: The resource extent type to allocate.
5551 *
5552 * This function allocates the number of elements for the specified
5553 * resource type.
5554 **/
5555static int
5556lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5557{
5558 bool emb = false;
5559 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5560 uint16_t rsrc_id, rsrc_start, j, k;
5561 uint16_t *ids;
5562 int i, rc;
5563 unsigned long longs;
5564 unsigned long *bmask;
5565 struct lpfc_rsrc_blks *rsrc_blks;
5566 LPFC_MBOXQ_t *mbox;
5567 uint32_t length;
5568 struct lpfc_id_range *id_array = NULL;
5569 void *virtaddr = NULL;
5570 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5571 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5572 struct list_head *ext_blk_list;
5573
5574 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5575 &rsrc_cnt,
5576 &rsrc_size);
5577 if (unlikely(rc))
5578 return -EIO;
5579
5580 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5581 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5582 "3009 No available Resource Extents "
5583 "for resource type 0x%x: Count: 0x%x, "
5584 "Size 0x%x\n", type, rsrc_cnt,
5585 rsrc_size);
5586 return -ENOMEM;
5587 }
5588
James Smart8a9d2e82012-05-09 21:16:12 -04005589 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5590 "2903 Post resource extents type-0x%x: "
5591 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005592
5593 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5594 if (!mbox)
5595 return -ENOMEM;
5596
James Smart8a9d2e82012-05-09 21:16:12 -04005597 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005598 if (unlikely(rc)) {
5599 rc = -EIO;
5600 goto err_exit;
5601 }
5602
5603 /*
5604 * Figure out where the response is located. Then get local pointers
5605 * to the response data. The port does not guarantee to respond to
5606 * all extents counts request so update the local variable with the
5607 * allocated count from the port.
5608 */
5609 if (emb == LPFC_SLI4_MBX_EMBED) {
5610 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5611 id_array = &rsrc_ext->u.rsp.id[0];
5612 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5613 } else {
5614 virtaddr = mbox->sge_array->addr[0];
5615 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5616 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5617 id_array = &n_rsrc->id;
5618 }
5619
5620 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5621 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5622
5623 /*
5624 * Based on the resource size and count, correct the base and max
5625 * resource values.
5626 */
5627 length = sizeof(struct lpfc_rsrc_blks);
5628 switch (type) {
5629 case LPFC_RSC_TYPE_FCOE_RPI:
5630 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5631 sizeof(unsigned long),
5632 GFP_KERNEL);
5633 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5634 rc = -ENOMEM;
5635 goto err_exit;
5636 }
5637 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5638 sizeof(uint16_t),
5639 GFP_KERNEL);
5640 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5641 kfree(phba->sli4_hba.rpi_bmask);
5642 rc = -ENOMEM;
5643 goto err_exit;
5644 }
5645
5646 /*
5647 * The next_rpi was initialized with the maximum available
5648 * count but the port may allocate a smaller number. Catch
5649 * that case and update the next_rpi.
5650 */
5651 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5652
5653 /* Initialize local ptrs for common extent processing later. */
5654 bmask = phba->sli4_hba.rpi_bmask;
5655 ids = phba->sli4_hba.rpi_ids;
5656 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5657 break;
5658 case LPFC_RSC_TYPE_FCOE_VPI:
5659 phba->vpi_bmask = kzalloc(longs *
5660 sizeof(unsigned long),
5661 GFP_KERNEL);
5662 if (unlikely(!phba->vpi_bmask)) {
5663 rc = -ENOMEM;
5664 goto err_exit;
5665 }
5666 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5667 sizeof(uint16_t),
5668 GFP_KERNEL);
5669 if (unlikely(!phba->vpi_ids)) {
5670 kfree(phba->vpi_bmask);
5671 rc = -ENOMEM;
5672 goto err_exit;
5673 }
5674
5675 /* Initialize local ptrs for common extent processing later. */
5676 bmask = phba->vpi_bmask;
5677 ids = phba->vpi_ids;
5678 ext_blk_list = &phba->lpfc_vpi_blk_list;
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_XRI:
5681 phba->sli4_hba.xri_bmask = kzalloc(longs *
5682 sizeof(unsigned long),
5683 GFP_KERNEL);
5684 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5685 rc = -ENOMEM;
5686 goto err_exit;
5687 }
James Smart8a9d2e82012-05-09 21:16:12 -04005688 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005689 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5690 sizeof(uint16_t),
5691 GFP_KERNEL);
5692 if (unlikely(!phba->sli4_hba.xri_ids)) {
5693 kfree(phba->sli4_hba.xri_bmask);
5694 rc = -ENOMEM;
5695 goto err_exit;
5696 }
5697
5698 /* Initialize local ptrs for common extent processing later. */
5699 bmask = phba->sli4_hba.xri_bmask;
5700 ids = phba->sli4_hba.xri_ids;
5701 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5702 break;
5703 case LPFC_RSC_TYPE_FCOE_VFI:
5704 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5705 sizeof(unsigned long),
5706 GFP_KERNEL);
5707 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5708 rc = -ENOMEM;
5709 goto err_exit;
5710 }
5711 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5712 sizeof(uint16_t),
5713 GFP_KERNEL);
5714 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5715 kfree(phba->sli4_hba.vfi_bmask);
5716 rc = -ENOMEM;
5717 goto err_exit;
5718 }
5719
5720 /* Initialize local ptrs for common extent processing later. */
5721 bmask = phba->sli4_hba.vfi_bmask;
5722 ids = phba->sli4_hba.vfi_ids;
5723 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5724 break;
5725 default:
5726 /* Unsupported Opcode. Fail call. */
5727 id_array = NULL;
5728 bmask = NULL;
5729 ids = NULL;
5730 ext_blk_list = NULL;
5731 goto err_exit;
5732 }
5733
5734 /*
5735 * Complete initializing the extent configuration with the
5736 * allocated ids assigned to this function. The bitmask serves
5737 * as an index into the array and manages the available ids. The
5738 * array just stores the ids communicated to the port via the wqes.
5739 */
5740 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5741 if ((i % 2) == 0)
5742 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5743 &id_array[k]);
5744 else
5745 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5746 &id_array[k]);
5747
5748 rsrc_blks = kzalloc(length, GFP_KERNEL);
5749 if (unlikely(!rsrc_blks)) {
5750 rc = -ENOMEM;
5751 kfree(bmask);
5752 kfree(ids);
5753 goto err_exit;
5754 }
5755 rsrc_blks->rsrc_start = rsrc_id;
5756 rsrc_blks->rsrc_size = rsrc_size;
5757 list_add_tail(&rsrc_blks->list, ext_blk_list);
5758 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005759 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005760 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005761 lpfc_sli4_get_iocb_cnt(phba);
5762 phba->sli4_hba.nvme_xri_start =
5763 phba->sli4_hba.scsi_xri_start +
5764 phba->sli4_hba.scsi_xri_max;
5765 }
James Smart6d368e52011-05-24 11:44:12 -04005766
5767 while (rsrc_id < (rsrc_start + rsrc_size)) {
5768 ids[j] = rsrc_id;
5769 rsrc_id++;
5770 j++;
5771 }
5772 /* Entire word processed. Get next word.*/
5773 if ((i % 2) == 1)
5774 k++;
5775 }
5776 err_exit:
5777 lpfc_sli4_mbox_cmd_free(phba, mbox);
5778 return rc;
5779}
5780
James Smart895427b2017-02-12 13:52:30 -08005781
5782
James Smart6d368e52011-05-24 11:44:12 -04005783/**
5784 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5785 * @phba: Pointer to HBA context object.
5786 * @type: the extent's type.
5787 *
5788 * This function deallocates all extents of a particular resource type.
5789 * SLI4 does not allow for deallocating a particular extent range. It
5790 * is the caller's responsibility to release all kernel memory resources.
5791 **/
5792static int
5793lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5794{
5795 int rc;
5796 uint32_t length, mbox_tmo = 0;
5797 LPFC_MBOXQ_t *mbox;
5798 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5799 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5800
5801 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5802 if (!mbox)
5803 return -ENOMEM;
5804
5805 /*
5806 * This function sends an embedded mailbox because it only sends the
5807 * the resource type. All extents of this type are released by the
5808 * port.
5809 */
5810 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5811 sizeof(struct lpfc_sli4_cfg_mhdr));
5812 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5813 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5814 length, LPFC_SLI4_MBX_EMBED);
5815
5816 /* Send an extents count of 0 - the dealloc doesn't use it. */
5817 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5818 LPFC_SLI4_MBX_EMBED);
5819 if (unlikely(rc)) {
5820 rc = -EIO;
5821 goto out_free_mbox;
5822 }
5823 if (!phba->sli4_hba.intr_enable)
5824 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5825 else {
James Smarta183a152011-10-10 21:32:43 -04005826 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005827 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5828 }
5829 if (unlikely(rc)) {
5830 rc = -EIO;
5831 goto out_free_mbox;
5832 }
5833
5834 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5835 if (bf_get(lpfc_mbox_hdr_status,
5836 &dealloc_rsrc->header.cfg_shdr.response)) {
5837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5838 "2919 Failed to release resource extents "
5839 "for type %d - Status 0x%x Add'l Status 0x%x. "
5840 "Resource memory not released.\n",
5841 type,
5842 bf_get(lpfc_mbox_hdr_status,
5843 &dealloc_rsrc->header.cfg_shdr.response),
5844 bf_get(lpfc_mbox_hdr_add_status,
5845 &dealloc_rsrc->header.cfg_shdr.response));
5846 rc = -EIO;
5847 goto out_free_mbox;
5848 }
5849
5850 /* Release kernel memory resources for the specific type. */
5851 switch (type) {
5852 case LPFC_RSC_TYPE_FCOE_VPI:
5853 kfree(phba->vpi_bmask);
5854 kfree(phba->vpi_ids);
5855 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5856 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5857 &phba->lpfc_vpi_blk_list, list) {
5858 list_del_init(&rsrc_blk->list);
5859 kfree(rsrc_blk);
5860 }
James Smart16a3a202013-04-17 20:14:38 -04005861 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005862 break;
5863 case LPFC_RSC_TYPE_FCOE_XRI:
5864 kfree(phba->sli4_hba.xri_bmask);
5865 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005866 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5867 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5868 list_del_init(&rsrc_blk->list);
5869 kfree(rsrc_blk);
5870 }
5871 break;
5872 case LPFC_RSC_TYPE_FCOE_VFI:
5873 kfree(phba->sli4_hba.vfi_bmask);
5874 kfree(phba->sli4_hba.vfi_ids);
5875 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5876 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5877 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5878 list_del_init(&rsrc_blk->list);
5879 kfree(rsrc_blk);
5880 }
5881 break;
5882 case LPFC_RSC_TYPE_FCOE_RPI:
5883 /* RPI bitmask and physical id array are cleaned up earlier. */
5884 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5885 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5886 list_del_init(&rsrc_blk->list);
5887 kfree(rsrc_blk);
5888 }
5889 break;
5890 default:
5891 break;
5892 }
5893
5894 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5895
5896 out_free_mbox:
5897 mempool_free(mbox, phba->mbox_mem_pool);
5898 return rc;
5899}
5900
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005901static void
James Smart7bdedb32016-07-06 12:36:00 -07005902lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5903 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005904{
James Smart65791f12016-07-06 12:35:56 -07005905 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005906
James Smart65791f12016-07-06 12:35:56 -07005907 len = sizeof(struct lpfc_mbx_set_feature) -
5908 sizeof(struct lpfc_sli4_cfg_mhdr);
5909 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5910 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5911 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005912
James Smart7bdedb32016-07-06 12:36:00 -07005913 switch (feature) {
5914 case LPFC_SET_UE_RECOVERY:
5915 bf_set(lpfc_mbx_set_feature_UER,
5916 &mbox->u.mqe.un.set_feature, 1);
5917 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5918 mbox->u.mqe.un.set_feature.param_len = 8;
5919 break;
5920 case LPFC_SET_MDS_DIAGS:
5921 bf_set(lpfc_mbx_set_feature_mds,
5922 &mbox->u.mqe.un.set_feature, 1);
5923 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07005924 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07005925 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5926 mbox->u.mqe.un.set_feature.param_len = 8;
5927 break;
James Smart65791f12016-07-06 12:35:56 -07005928 }
James Smart7bdedb32016-07-06 12:36:00 -07005929
5930 return;
James Smart65791f12016-07-06 12:35:56 -07005931}
5932
James Smart6d368e52011-05-24 11:44:12 -04005933/**
5934 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5935 * @phba: Pointer to HBA context object.
5936 *
5937 * This function allocates all SLI4 resource identifiers.
5938 **/
5939int
5940lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5941{
5942 int i, rc, error = 0;
5943 uint16_t count, base;
5944 unsigned long longs;
5945
James Smartff78d8f2011-12-13 13:21:35 -05005946 if (!phba->sli4_hba.rpi_hdrs_in_use)
5947 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005948 if (phba->sli4_hba.extents_in_use) {
5949 /*
5950 * The port supports resource extents. The XRI, VPI, VFI, RPI
5951 * resource extent count must be read and allocated before
5952 * provisioning the resource id arrays.
5953 */
5954 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5955 LPFC_IDX_RSRC_RDY) {
5956 /*
5957 * Extent-based resources are set - the driver could
5958 * be in a port reset. Figure out if any corrective
5959 * actions need to be taken.
5960 */
5961 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5962 LPFC_RSC_TYPE_FCOE_VFI);
5963 if (rc != 0)
5964 error++;
5965 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5966 LPFC_RSC_TYPE_FCOE_VPI);
5967 if (rc != 0)
5968 error++;
5969 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5970 LPFC_RSC_TYPE_FCOE_XRI);
5971 if (rc != 0)
5972 error++;
5973 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5974 LPFC_RSC_TYPE_FCOE_RPI);
5975 if (rc != 0)
5976 error++;
5977
5978 /*
5979 * It's possible that the number of resources
5980 * provided to this port instance changed between
5981 * resets. Detect this condition and reallocate
5982 * resources. Otherwise, there is no action.
5983 */
5984 if (error) {
5985 lpfc_printf_log(phba, KERN_INFO,
5986 LOG_MBOX | LOG_INIT,
5987 "2931 Detected extent resource "
5988 "change. Reallocating all "
5989 "extents.\n");
5990 rc = lpfc_sli4_dealloc_extent(phba,
5991 LPFC_RSC_TYPE_FCOE_VFI);
5992 rc = lpfc_sli4_dealloc_extent(phba,
5993 LPFC_RSC_TYPE_FCOE_VPI);
5994 rc = lpfc_sli4_dealloc_extent(phba,
5995 LPFC_RSC_TYPE_FCOE_XRI);
5996 rc = lpfc_sli4_dealloc_extent(phba,
5997 LPFC_RSC_TYPE_FCOE_RPI);
5998 } else
5999 return 0;
6000 }
6001
6002 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6003 if (unlikely(rc))
6004 goto err_exit;
6005
6006 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6007 if (unlikely(rc))
6008 goto err_exit;
6009
6010 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6011 if (unlikely(rc))
6012 goto err_exit;
6013
6014 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6015 if (unlikely(rc))
6016 goto err_exit;
6017 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6018 LPFC_IDX_RSRC_RDY);
6019 return rc;
6020 } else {
6021 /*
6022 * The port does not support resource extents. The XRI, VPI,
6023 * VFI, RPI resource ids were determined from READ_CONFIG.
6024 * Just allocate the bitmasks and provision the resource id
6025 * arrays. If a port reset is active, the resources don't
6026 * need any action - just exit.
6027 */
6028 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006029 LPFC_IDX_RSRC_RDY) {
6030 lpfc_sli4_dealloc_resource_identifiers(phba);
6031 lpfc_sli4_remove_rpis(phba);
6032 }
James Smart6d368e52011-05-24 11:44:12 -04006033 /* RPIs. */
6034 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006035 if (count <= 0) {
6036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6037 "3279 Invalid provisioning of "
6038 "rpi:%d\n", count);
6039 rc = -EINVAL;
6040 goto err_exit;
6041 }
James Smart6d368e52011-05-24 11:44:12 -04006042 base = phba->sli4_hba.max_cfg_param.rpi_base;
6043 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6044 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6045 sizeof(unsigned long),
6046 GFP_KERNEL);
6047 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6048 rc = -ENOMEM;
6049 goto err_exit;
6050 }
6051 phba->sli4_hba.rpi_ids = kzalloc(count *
6052 sizeof(uint16_t),
6053 GFP_KERNEL);
6054 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6055 rc = -ENOMEM;
6056 goto free_rpi_bmask;
6057 }
6058
6059 for (i = 0; i < count; i++)
6060 phba->sli4_hba.rpi_ids[i] = base + i;
6061
6062 /* VPIs. */
6063 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006064 if (count <= 0) {
6065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6066 "3280 Invalid provisioning of "
6067 "vpi:%d\n", count);
6068 rc = -EINVAL;
6069 goto free_rpi_ids;
6070 }
James Smart6d368e52011-05-24 11:44:12 -04006071 base = phba->sli4_hba.max_cfg_param.vpi_base;
6072 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6073 phba->vpi_bmask = kzalloc(longs *
6074 sizeof(unsigned long),
6075 GFP_KERNEL);
6076 if (unlikely(!phba->vpi_bmask)) {
6077 rc = -ENOMEM;
6078 goto free_rpi_ids;
6079 }
6080 phba->vpi_ids = kzalloc(count *
6081 sizeof(uint16_t),
6082 GFP_KERNEL);
6083 if (unlikely(!phba->vpi_ids)) {
6084 rc = -ENOMEM;
6085 goto free_vpi_bmask;
6086 }
6087
6088 for (i = 0; i < count; i++)
6089 phba->vpi_ids[i] = base + i;
6090
6091 /* XRIs. */
6092 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006093 if (count <= 0) {
6094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6095 "3281 Invalid provisioning of "
6096 "xri:%d\n", count);
6097 rc = -EINVAL;
6098 goto free_vpi_ids;
6099 }
James Smart6d368e52011-05-24 11:44:12 -04006100 base = phba->sli4_hba.max_cfg_param.xri_base;
6101 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6102 phba->sli4_hba.xri_bmask = kzalloc(longs *
6103 sizeof(unsigned long),
6104 GFP_KERNEL);
6105 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6106 rc = -ENOMEM;
6107 goto free_vpi_ids;
6108 }
James Smart41899be2012-03-01 22:34:19 -05006109 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006110 phba->sli4_hba.xri_ids = kzalloc(count *
6111 sizeof(uint16_t),
6112 GFP_KERNEL);
6113 if (unlikely(!phba->sli4_hba.xri_ids)) {
6114 rc = -ENOMEM;
6115 goto free_xri_bmask;
6116 }
6117
6118 for (i = 0; i < count; i++)
6119 phba->sli4_hba.xri_ids[i] = base + i;
6120
6121 /* VFIs. */
6122 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006123 if (count <= 0) {
6124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6125 "3282 Invalid provisioning of "
6126 "vfi:%d\n", count);
6127 rc = -EINVAL;
6128 goto free_xri_ids;
6129 }
James Smart6d368e52011-05-24 11:44:12 -04006130 base = phba->sli4_hba.max_cfg_param.vfi_base;
6131 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6132 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6133 sizeof(unsigned long),
6134 GFP_KERNEL);
6135 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6136 rc = -ENOMEM;
6137 goto free_xri_ids;
6138 }
6139 phba->sli4_hba.vfi_ids = kzalloc(count *
6140 sizeof(uint16_t),
6141 GFP_KERNEL);
6142 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6143 rc = -ENOMEM;
6144 goto free_vfi_bmask;
6145 }
6146
6147 for (i = 0; i < count; i++)
6148 phba->sli4_hba.vfi_ids[i] = base + i;
6149
6150 /*
6151 * Mark all resources ready. An HBA reset doesn't need
6152 * to reset the initialization.
6153 */
6154 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6155 LPFC_IDX_RSRC_RDY);
6156 return 0;
6157 }
6158
6159 free_vfi_bmask:
6160 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006161 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006162 free_xri_ids:
6163 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006164 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006165 free_xri_bmask:
6166 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006167 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006168 free_vpi_ids:
6169 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006170 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006171 free_vpi_bmask:
6172 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006173 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006174 free_rpi_ids:
6175 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006176 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006177 free_rpi_bmask:
6178 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006179 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006180 err_exit:
6181 return rc;
6182}
6183
6184/**
6185 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6186 * @phba: Pointer to HBA context object.
6187 *
6188 * This function allocates the number of elements for the specified
6189 * resource type.
6190 **/
6191int
6192lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6193{
6194 if (phba->sli4_hba.extents_in_use) {
6195 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6196 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6197 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6198 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6199 } else {
6200 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006201 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006202 kfree(phba->vpi_ids);
6203 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6204 kfree(phba->sli4_hba.xri_bmask);
6205 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006206 kfree(phba->sli4_hba.vfi_bmask);
6207 kfree(phba->sli4_hba.vfi_ids);
6208 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6209 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6210 }
6211
6212 return 0;
6213}
6214
6215/**
James Smartb76f2dc2011-07-22 18:37:42 -04006216 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6217 * @phba: Pointer to HBA context object.
6218 * @type: The resource extent type.
6219 * @extnt_count: buffer to hold port extent count response
6220 * @extnt_size: buffer to hold port extent size response.
6221 *
6222 * This function calls the port to read the host allocated extents
6223 * for a particular type.
6224 **/
6225int
6226lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6227 uint16_t *extnt_cnt, uint16_t *extnt_size)
6228{
6229 bool emb;
6230 int rc = 0;
6231 uint16_t curr_blks = 0;
6232 uint32_t req_len, emb_len;
6233 uint32_t alloc_len, mbox_tmo;
6234 struct list_head *blk_list_head;
6235 struct lpfc_rsrc_blks *rsrc_blk;
6236 LPFC_MBOXQ_t *mbox;
6237 void *virtaddr = NULL;
6238 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6239 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6240 union lpfc_sli4_cfg_shdr *shdr;
6241
6242 switch (type) {
6243 case LPFC_RSC_TYPE_FCOE_VPI:
6244 blk_list_head = &phba->lpfc_vpi_blk_list;
6245 break;
6246 case LPFC_RSC_TYPE_FCOE_XRI:
6247 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6248 break;
6249 case LPFC_RSC_TYPE_FCOE_VFI:
6250 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6251 break;
6252 case LPFC_RSC_TYPE_FCOE_RPI:
6253 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6254 break;
6255 default:
6256 return -EIO;
6257 }
6258
6259 /* Count the number of extents currently allocatd for this type. */
6260 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6261 if (curr_blks == 0) {
6262 /*
6263 * The GET_ALLOCATED mailbox does not return the size,
6264 * just the count. The size should be just the size
6265 * stored in the current allocated block and all sizes
6266 * for an extent type are the same so set the return
6267 * value now.
6268 */
6269 *extnt_size = rsrc_blk->rsrc_size;
6270 }
6271 curr_blks++;
6272 }
6273
James Smartb76f2dc2011-07-22 18:37:42 -04006274 /*
6275 * Calculate the size of an embedded mailbox. The uint32_t
6276 * accounts for extents-specific word.
6277 */
6278 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6279 sizeof(uint32_t);
6280
6281 /*
6282 * Presume the allocation and response will fit into an embedded
6283 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6284 */
6285 emb = LPFC_SLI4_MBX_EMBED;
6286 req_len = emb_len;
6287 if (req_len > emb_len) {
6288 req_len = curr_blks * sizeof(uint16_t) +
6289 sizeof(union lpfc_sli4_cfg_shdr) +
6290 sizeof(uint32_t);
6291 emb = LPFC_SLI4_MBX_NEMBED;
6292 }
6293
6294 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6295 if (!mbox)
6296 return -ENOMEM;
6297 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6298
6299 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6300 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6301 req_len, emb);
6302 if (alloc_len < req_len) {
6303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6304 "2983 Allocated DMA memory size (x%x) is "
6305 "less than the requested DMA memory "
6306 "size (x%x)\n", alloc_len, req_len);
6307 rc = -ENOMEM;
6308 goto err_exit;
6309 }
6310 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6311 if (unlikely(rc)) {
6312 rc = -EIO;
6313 goto err_exit;
6314 }
6315
6316 if (!phba->sli4_hba.intr_enable)
6317 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6318 else {
James Smarta183a152011-10-10 21:32:43 -04006319 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006320 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6321 }
6322
6323 if (unlikely(rc)) {
6324 rc = -EIO;
6325 goto err_exit;
6326 }
6327
6328 /*
6329 * Figure out where the response is located. Then get local pointers
6330 * to the response data. The port does not guarantee to respond to
6331 * all extents counts request so update the local variable with the
6332 * allocated count from the port.
6333 */
6334 if (emb == LPFC_SLI4_MBX_EMBED) {
6335 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6336 shdr = &rsrc_ext->header.cfg_shdr;
6337 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6338 } else {
6339 virtaddr = mbox->sge_array->addr[0];
6340 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6341 shdr = &n_rsrc->cfg_shdr;
6342 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6343 }
6344
6345 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6346 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6347 "2984 Failed to read allocated resources "
6348 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6349 type,
6350 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6351 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6352 rc = -EIO;
6353 goto err_exit;
6354 }
6355 err_exit:
6356 lpfc_sli4_mbox_cmd_free(phba, mbox);
6357 return rc;
6358}
6359
6360/**
James Smart0ef69962017-04-21 16:04:50 -07006361 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006362 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006363 * @pring: Pointer to driver SLI ring object.
6364 * @sgl_list: linked link of sgl buffers to post
6365 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006366 *
James Smart895427b2017-02-12 13:52:30 -08006367 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006368 * repost them to the port by using SGL block post. This is needed after a
6369 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006370 * of buffer sgls which contains contiguous xris and uses the non-embedded
6371 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006372 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6373 * mailbox command for posting.
6374 *
6375 * Returns: 0 = success, non-zero failure.
6376 **/
6377static int
James Smart895427b2017-02-12 13:52:30 -08006378lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6379 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006380{
6381 struct lpfc_sglq *sglq_entry = NULL;
6382 struct lpfc_sglq *sglq_entry_next = NULL;
6383 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006384 int status, total_cnt;
6385 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006386 int last_xritag = NO_XRI;
6387 LIST_HEAD(prep_sgl_list);
6388 LIST_HEAD(blck_sgl_list);
6389 LIST_HEAD(allc_sgl_list);
6390 LIST_HEAD(post_sgl_list);
6391 LIST_HEAD(free_sgl_list);
6392
James Smart38c20672013-03-01 16:37:44 -05006393 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006394 spin_lock(&phba->sli4_hba.sgl_list_lock);
6395 list_splice_init(sgl_list, &allc_sgl_list);
6396 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006397 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006398
James Smart895427b2017-02-12 13:52:30 -08006399 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006400 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6401 &allc_sgl_list, list) {
6402 list_del_init(&sglq_entry->list);
6403 block_cnt++;
6404 if ((last_xritag != NO_XRI) &&
6405 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6406 /* a hole in xri block, form a sgl posting block */
6407 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6408 post_cnt = block_cnt - 1;
6409 /* prepare list for next posting block */
6410 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6411 block_cnt = 1;
6412 } else {
6413 /* prepare list for next posting block */
6414 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6415 /* enough sgls for non-embed sgl mbox command */
6416 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6417 list_splice_init(&prep_sgl_list,
6418 &blck_sgl_list);
6419 post_cnt = block_cnt;
6420 block_cnt = 0;
6421 }
6422 }
6423 num_posted++;
6424
6425 /* keep track of last sgl's xritag */
6426 last_xritag = sglq_entry->sli4_xritag;
6427
James Smart895427b2017-02-12 13:52:30 -08006428 /* end of repost sgl list condition for buffers */
6429 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006430 if (post_cnt == 0) {
6431 list_splice_init(&prep_sgl_list,
6432 &blck_sgl_list);
6433 post_cnt = block_cnt;
6434 } else if (block_cnt == 1) {
6435 status = lpfc_sli4_post_sgl(phba,
6436 sglq_entry->phys, 0,
6437 sglq_entry->sli4_xritag);
6438 if (!status) {
6439 /* successful, put sgl to posted list */
6440 list_add_tail(&sglq_entry->list,
6441 &post_sgl_list);
6442 } else {
6443 /* Failure, put sgl to free list */
6444 lpfc_printf_log(phba, KERN_WARNING,
6445 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006446 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006447 "sgl, xritag:x%x\n",
6448 sglq_entry->sli4_xritag);
6449 list_add_tail(&sglq_entry->list,
6450 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006451 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006452 }
6453 }
6454 }
6455
6456 /* continue until a nembed page worth of sgls */
6457 if (post_cnt == 0)
6458 continue;
6459
James Smart895427b2017-02-12 13:52:30 -08006460 /* post the buffer list sgls as a block */
6461 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6462 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006463
6464 if (!status) {
6465 /* success, put sgl list to posted sgl list */
6466 list_splice_init(&blck_sgl_list, &post_sgl_list);
6467 } else {
6468 /* Failure, put sgl list to free sgl list */
6469 sglq_entry_first = list_first_entry(&blck_sgl_list,
6470 struct lpfc_sglq,
6471 list);
6472 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006473 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006474 "xritag:x%x-x%x\n",
6475 sglq_entry_first->sli4_xritag,
6476 (sglq_entry_first->sli4_xritag +
6477 post_cnt - 1));
6478 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006479 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006480 }
6481
6482 /* don't reset xirtag due to hole in xri block */
6483 if (block_cnt == 0)
6484 last_xritag = NO_XRI;
6485
James Smart895427b2017-02-12 13:52:30 -08006486 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006487 post_cnt = 0;
6488 }
6489
James Smart895427b2017-02-12 13:52:30 -08006490 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006491 lpfc_free_sgl_list(phba, &free_sgl_list);
6492
James Smart895427b2017-02-12 13:52:30 -08006493 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006494 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006495 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006496 spin_lock(&phba->sli4_hba.sgl_list_lock);
6497 list_splice_init(&post_sgl_list, sgl_list);
6498 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006499 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006500 } else {
6501 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006502 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006503 return -EIO;
6504 }
James Smart895427b2017-02-12 13:52:30 -08006505
6506 /* return the number of XRIs actually posted */
6507 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006508}
6509
James Smart61bda8f2016-10-13 15:06:05 -07006510void
6511lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6512{
6513 uint32_t len;
6514
6515 len = sizeof(struct lpfc_mbx_set_host_data) -
6516 sizeof(struct lpfc_sli4_cfg_mhdr);
6517 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6518 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6519 LPFC_SLI4_MBX_EMBED);
6520
6521 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006522 mbox->u.mqe.un.set_host_data.param_len =
6523 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006524 snprintf(mbox->u.mqe.un.set_host_data.data,
6525 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6526 "Linux %s v"LPFC_DRIVER_VERSION,
6527 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6528}
6529
James Smarta8cf5df2017-05-15 15:20:46 -07006530int
James Smart6c621a22017-05-15 15:20:45 -07006531lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006532 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006533{
6534 int rc, i;
6535 struct lpfc_rqe hrqe;
6536 struct lpfc_rqe drqe;
6537 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006538 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006539 struct rqb_dmabuf *rqb_buffer;
6540 LIST_HEAD(rqb_buf_list);
6541
James Smart411de512018-01-30 15:58:52 -08006542 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006543 rqbp = hrq->rqbp;
6544 for (i = 0; i < count; i++) {
6545 /* IF RQ is already full, don't bother */
6546 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6547 break;
6548 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6549 if (!rqb_buffer)
6550 break;
6551 rqb_buffer->hrq = hrq;
6552 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006553 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006554 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6555 }
6556 while (!list_empty(&rqb_buf_list)) {
6557 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6558 hbuf.list);
6559
6560 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6561 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6562 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6563 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6564 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6565 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6567 "6421 Cannot post to HRQ %d: %x %x %x "
6568 "DRQ %x %x\n",
6569 hrq->queue_id,
6570 hrq->host_index,
6571 hrq->hba_index,
6572 hrq->entry_count,
6573 drq->host_index,
6574 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006575 rqbp->rqb_free_buffer(phba, rqb_buffer);
6576 } else {
6577 list_add_tail(&rqb_buffer->hbuf.list,
6578 &rqbp->rqb_buffer_list);
6579 rqbp->buffer_count++;
6580 }
6581 }
James Smart411de512018-01-30 15:58:52 -08006582 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006583 return 1;
6584}
6585
James Smart8a9d2e82012-05-09 21:16:12 -04006586/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006587 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006588 * @phba: Pointer to HBA context object.
6589 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006590 * This function is the main SLI4 device initialization PCI function. This
6591 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006592 * HBA error attention handler code. Caller is not required to hold any
6593 * locks.
6594 **/
6595int
6596lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6597{
James Smart6c621a22017-05-15 15:20:45 -07006598 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006599 LPFC_MBOXQ_t *mboxq;
6600 struct lpfc_mqe *mqe;
6601 uint8_t *vpd;
6602 uint32_t vpd_size;
6603 uint32_t ftr_rsp = 0;
6604 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6605 struct lpfc_vport *vport = phba->pport;
6606 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006607 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006608
6609 /* Perform a PCI function reset to start from clean */
6610 rc = lpfc_pci_function_reset(phba);
6611 if (unlikely(rc))
6612 return -ENODEV;
6613
6614 /* Check the HBA Host Status Register for readyness */
6615 rc = lpfc_sli4_post_status_check(phba);
6616 if (unlikely(rc))
6617 return -ENODEV;
6618 else {
6619 spin_lock_irq(&phba->hbalock);
6620 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6621 spin_unlock_irq(&phba->hbalock);
6622 }
6623
6624 /*
6625 * Allocate a single mailbox container for initializing the
6626 * port.
6627 */
6628 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6629 if (!mboxq)
6630 return -ENOMEM;
6631
James Smartda0436e2009-05-22 14:51:39 -04006632 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006633 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006634 vpd = kzalloc(vpd_size, GFP_KERNEL);
6635 if (!vpd) {
6636 rc = -ENOMEM;
6637 goto out_free_mbox;
6638 }
6639
6640 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006641 if (unlikely(rc)) {
6642 kfree(vpd);
6643 goto out_free_mbox;
6644 }
James Smart572709e2013-07-15 18:32:43 -04006645
James Smartda0436e2009-05-22 14:51:39 -04006646 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006647 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006648 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006649 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006650 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6651 } else {
James Smart76a95d72010-11-20 23:11:48 -05006652 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006653 }
James Smart45ed1192009-10-02 15:17:02 -04006654
6655 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6656 LPFC_DCBX_CEE_MODE)
6657 phba->hba_flag |= HBA_FIP_SUPPORT;
6658 else
6659 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6660
James Smart4f2e66c2012-05-09 21:17:07 -04006661 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6662
James Smartc31098c2011-04-16 11:03:33 -04006663 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006664 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6665 "0376 READ_REV Error. SLI Level %d "
6666 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006667 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006668 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006669 kfree(vpd);
6670 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006671 }
James Smartcd1c8302011-10-10 21:33:25 -04006672
6673 /*
James Smartff78d8f2011-12-13 13:21:35 -05006674 * Continue initialization with default values even if driver failed
6675 * to read FCoE param config regions, only read parameters if the
6676 * board is FCoE
6677 */
6678 if (phba->hba_flag & HBA_FCOE_MODE &&
6679 lpfc_sli4_read_fcoe_params(phba))
6680 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6681 "2570 Failed to read FCoE parameters\n");
6682
6683 /*
James Smartcd1c8302011-10-10 21:33:25 -04006684 * Retrieve sli4 device physical port name, failure of doing it
6685 * is considered as non-fatal.
6686 */
6687 rc = lpfc_sli4_retrieve_pport_name(phba);
6688 if (!rc)
6689 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6690 "3080 Successful retrieving SLI4 device "
6691 "physical port name: %s.\n", phba->Port);
6692
James Smartda0436e2009-05-22 14:51:39 -04006693 /*
6694 * Evaluate the read rev and vpd data. Populate the driver
6695 * state with the results. If this routine fails, the failure
6696 * is not fatal as the driver will use generic values.
6697 */
6698 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6699 if (unlikely(!rc)) {
6700 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6701 "0377 Error %d parsing vpd. "
6702 "Using defaults.\n", rc);
6703 rc = 0;
6704 }
James Smart76a95d72010-11-20 23:11:48 -05006705 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006706
James Smartf1126682009-06-10 17:22:44 -04006707 /* Save information as VPD data */
6708 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6709 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6710 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6711 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6712 &mqe->un.read_rev);
6713 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6714 &mqe->un.read_rev);
6715 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6716 &mqe->un.read_rev);
6717 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6718 &mqe->un.read_rev);
6719 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6720 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6721 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6722 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6723 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6724 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6725 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6726 "(%d):0380 READ_REV Status x%x "
6727 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6728 mboxq->vport ? mboxq->vport->vpi : 0,
6729 bf_get(lpfc_mqe_status, mqe),
6730 phba->vpd.rev.opFwName,
6731 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6732 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006733
James Smart572709e2013-07-15 18:32:43 -04006734 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6735 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6736 if (phba->pport->cfg_lun_queue_depth > rc) {
6737 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6738 "3362 LUN queue depth changed from %d to %d\n",
6739 phba->pport->cfg_lun_queue_depth, rc);
6740 phba->pport->cfg_lun_queue_depth = rc;
6741 }
6742
James Smart65791f12016-07-06 12:35:56 -07006743 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006744 LPFC_SLI_INTF_IF_TYPE_0) {
6745 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6746 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6747 if (rc == MBX_SUCCESS) {
6748 phba->hba_flag |= HBA_RECOVERABLE_UE;
6749 /* Set 1Sec interval to detect UE */
6750 phba->eratt_poll_interval = 1;
6751 phba->sli4_hba.ue_to_sr = bf_get(
6752 lpfc_mbx_set_feature_UESR,
6753 &mboxq->u.mqe.un.set_feature);
6754 phba->sli4_hba.ue_to_rp = bf_get(
6755 lpfc_mbx_set_feature_UERP,
6756 &mboxq->u.mqe.un.set_feature);
6757 }
6758 }
6759
6760 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6761 /* Enable MDS Diagnostics only if the SLI Port supports it */
6762 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6763 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6764 if (rc != MBX_SUCCESS)
6765 phba->mds_diags_support = 0;
6766 }
James Smart572709e2013-07-15 18:32:43 -04006767
James Smartda0436e2009-05-22 14:51:39 -04006768 /*
6769 * Discover the port's supported feature set and match it against the
6770 * hosts requests.
6771 */
6772 lpfc_request_features(phba, mboxq);
6773 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6774 if (unlikely(rc)) {
6775 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006776 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006777 }
6778
6779 /*
6780 * The port must support FCP initiator mode as this is the
6781 * only mode running in the host.
6782 */
6783 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6784 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6785 "0378 No support for fcpi mode.\n");
6786 ftr_rsp++;
6787 }
James Smartfedd3b72011-02-16 12:39:24 -05006788 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6789 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6790 else
6791 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006792 /*
6793 * If the port cannot support the host's requested features
6794 * then turn off the global config parameters to disable the
6795 * feature in the driver. This is not a fatal error.
6796 */
James Smartbf086112011-08-21 21:48:13 -04006797 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6798 if (phba->cfg_enable_bg) {
6799 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6800 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6801 else
6802 ftr_rsp++;
6803 }
James Smartda0436e2009-05-22 14:51:39 -04006804
6805 if (phba->max_vpi && phba->cfg_enable_npiv &&
6806 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6807 ftr_rsp++;
6808
6809 if (ftr_rsp) {
6810 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6811 "0379 Feature Mismatch Data: x%08x %08x "
6812 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6813 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6814 phba->cfg_enable_npiv, phba->max_vpi);
6815 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6816 phba->cfg_enable_bg = 0;
6817 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6818 phba->cfg_enable_npiv = 0;
6819 }
6820
6821 /* These SLI3 features are assumed in SLI4 */
6822 spin_lock_irq(&phba->hbalock);
6823 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6824 spin_unlock_irq(&phba->hbalock);
6825
James Smart6d368e52011-05-24 11:44:12 -04006826 /*
6827 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6828 * calls depends on these resources to complete port setup.
6829 */
6830 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6831 if (rc) {
6832 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6833 "2920 Failed to alloc Resource IDs "
6834 "rc = x%x\n", rc);
6835 goto out_free_mbox;
6836 }
6837
James Smart61bda8f2016-10-13 15:06:05 -07006838 lpfc_set_host_data(phba, mboxq);
6839
6840 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6841 if (rc) {
6842 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6843 "2134 Failed to set host os driver version %x",
6844 rc);
6845 }
6846
James Smartda0436e2009-05-22 14:51:39 -04006847 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006848 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6849 if (rc) {
6850 phba->link_state = LPFC_HBA_ERROR;
6851 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006852 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006853 }
6854
James Smartda0436e2009-05-22 14:51:39 -04006855 mboxq->vport = vport;
6856 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6857 mp = (struct lpfc_dmabuf *) mboxq->context1;
6858 if (rc == MBX_SUCCESS) {
6859 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6860 rc = 0;
6861 }
6862
6863 /*
6864 * This memory was allocated by the lpfc_read_sparam routine. Release
6865 * it to the mbuf pool.
6866 */
6867 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6868 kfree(mp);
6869 mboxq->context1 = NULL;
6870 if (unlikely(rc)) {
6871 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6872 "0382 READ_SPARAM command failed "
6873 "status %d, mbxStatus x%x\n",
6874 rc, bf_get(lpfc_mqe_status, mqe));
6875 phba->link_state = LPFC_HBA_ERROR;
6876 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006877 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006878 }
6879
James Smart05580562011-05-24 11:40:48 -04006880 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006881
6882 /* Update the fc_host data structures with new wwn. */
6883 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6884 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6885
James Smart895427b2017-02-12 13:52:30 -08006886 /* Create all the SLI4 queues */
6887 rc = lpfc_sli4_queue_create(phba);
6888 if (rc) {
6889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6890 "3089 Failed to allocate queues\n");
6891 rc = -ENODEV;
6892 goto out_free_mbox;
6893 }
6894 /* Set up all the queues to the device */
6895 rc = lpfc_sli4_queue_setup(phba);
6896 if (unlikely(rc)) {
6897 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6898 "0381 Error %d during queue setup.\n ", rc);
6899 goto out_stop_timers;
6900 }
6901 /* Initialize the driver internal SLI layer lists. */
6902 lpfc_sli4_setup(phba);
6903 lpfc_sli4_queue_init(phba);
6904
6905 /* update host els xri-sgl sizes and mappings */
6906 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006907 if (unlikely(rc)) {
6908 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6909 "1400 Failed to update xri-sgl size and "
6910 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006911 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006912 }
6913
James Smart8a9d2e82012-05-09 21:16:12 -04006914 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006915 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6916 phba->sli4_hba.els_xri_cnt);
6917 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "0582 Error %d during els sgl post "
6920 "operation\n", rc);
6921 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006922 goto out_destroy_queue;
6923 }
6924 phba->sli4_hba.els_xri_cnt = rc;
6925
James Smartf358dd02017-02-12 13:52:34 -08006926 if (phba->nvmet_support) {
6927 /* update host nvmet xri-sgl sizes and mappings */
6928 rc = lpfc_sli4_nvmet_sgl_update(phba);
6929 if (unlikely(rc)) {
6930 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6931 "6308 Failed to update nvmet-sgl size "
6932 "and mapping: %d\n", rc);
6933 goto out_destroy_queue;
6934 }
6935
6936 /* register the nvmet sgl pool to the port */
6937 rc = lpfc_sli4_repost_sgl_list(
6938 phba,
6939 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6940 phba->sli4_hba.nvmet_xri_cnt);
6941 if (unlikely(rc < 0)) {
6942 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6943 "3117 Error %d during nvmet "
6944 "sgl post\n", rc);
6945 rc = -ENODEV;
6946 goto out_destroy_queue;
6947 }
6948 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006949
6950 cnt = phba->cfg_iocb_cnt * 1024;
6951 /* We need 1 iocbq for every SGL, for IO processing */
6952 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08006953 } else {
James Smart895427b2017-02-12 13:52:30 -08006954 /* update host scsi xri-sgl sizes and mappings */
6955 rc = lpfc_sli4_scsi_sgl_update(phba);
6956 if (unlikely(rc)) {
6957 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6958 "6309 Failed to update scsi-sgl size "
6959 "and mapping: %d\n", rc);
6960 goto out_destroy_queue;
6961 }
6962
6963 /* update host nvme xri-sgl sizes and mappings */
6964 rc = lpfc_sli4_nvme_sgl_update(phba);
6965 if (unlikely(rc)) {
6966 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6967 "6082 Failed to update nvme-sgl size "
6968 "and mapping: %d\n", rc);
6969 goto out_destroy_queue;
6970 }
James Smart6c621a22017-05-15 15:20:45 -07006971
6972 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07006973 }
6974
6975 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07006976 /* Initialize and populate the iocb list per host */
6977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006978 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07006979 phba->cfg_iocb_cnt, cnt);
6980 rc = lpfc_init_iocb_list(phba, cnt);
6981 if (rc) {
6982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006983 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07006984 goto out_destroy_queue;
6985 }
James Smart8a9d2e82012-05-09 21:16:12 -04006986 }
6987
James Smart11e644e2017-06-15 22:56:48 -07006988 if (phba->nvmet_support)
6989 lpfc_nvmet_create_targetport(phba);
6990
James Smart2d7dbc42017-02-12 13:52:35 -08006991 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08006992 /* Post initial buffers to all RQs created */
6993 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6994 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6995 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6996 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6997 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07006998 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08006999 rqbp->buffer_count = 0;
7000
James Smart2d7dbc42017-02-12 13:52:35 -08007001 lpfc_post_rq_buffer(
7002 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7003 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07007004 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007005 }
7006 }
7007
James Smart895427b2017-02-12 13:52:30 -08007008 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7009 /* register the allocated scsi sgl pool to the port */
7010 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7011 if (unlikely(rc)) {
7012 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7013 "0383 Error %d during scsi sgl post "
7014 "operation\n", rc);
7015 /* Some Scsi buffers were moved to abort scsi list */
7016 /* A pci function reset will repost them */
7017 rc = -ENODEV;
7018 goto out_destroy_queue;
7019 }
James Smartda0436e2009-05-22 14:51:39 -04007020 }
7021
James Smart01649562017-02-12 13:52:32 -08007022 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7023 (phba->nvmet_support == 0)) {
7024
7025 /* register the allocated nvme sgl pool to the port */
7026 rc = lpfc_repost_nvme_sgl_list(phba);
7027 if (unlikely(rc)) {
7028 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7029 "6116 Error %d during nvme sgl post "
7030 "operation\n", rc);
7031 /* Some NVME buffers were moved to abort nvme list */
7032 /* A pci function reset will repost them */
7033 rc = -ENODEV;
7034 goto out_destroy_queue;
7035 }
James Smartda0436e2009-05-22 14:51:39 -04007036 }
7037
7038 /* Post the rpi header region to the device. */
7039 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7040 if (unlikely(rc)) {
7041 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7042 "0393 Error %d during rpi post operation\n",
7043 rc);
7044 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007045 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007046 }
James Smart97f2ecf2012-03-01 22:35:23 -05007047 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007048
James Smart895427b2017-02-12 13:52:30 -08007049 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007050 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007051 /*
7052 * The FC Port needs to register FCFI (index 0)
7053 */
7054 lpfc_reg_fcfi(phba, mboxq);
7055 mboxq->vport = phba->pport;
7056 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7057 if (rc != MBX_SUCCESS)
7058 goto out_unset_queue;
7059 rc = 0;
7060 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7061 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007062 } else {
7063 /* We are a NVME Target mode with MRQ > 1 */
7064
7065 /* First register the FCFI */
7066 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7067 mboxq->vport = phba->pport;
7068 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7069 if (rc != MBX_SUCCESS)
7070 goto out_unset_queue;
7071 rc = 0;
7072 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7073 &mboxq->u.mqe.un.reg_fcfi_mrq);
7074
7075 /* Next register the MRQs */
7076 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7077 mboxq->vport = phba->pport;
7078 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7079 if (rc != MBX_SUCCESS)
7080 goto out_unset_queue;
7081 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007082 }
7083 /* Check if the port is configured to be disabled */
7084 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007085 }
7086
7087 /* Arm the CQs and then EQs on device */
7088 lpfc_sli4_arm_cqeq_intr(phba);
7089
7090 /* Indicate device interrupt mode */
7091 phba->sli4_hba.intr_enable = 1;
7092
7093 /* Allow asynchronous mailbox command to go through */
7094 spin_lock_irq(&phba->hbalock);
7095 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7096 spin_unlock_irq(&phba->hbalock);
7097
7098 /* Post receive buffers to the device */
7099 lpfc_sli4_rb_setup(phba);
7100
James Smartfc2b9892010-02-26 14:15:29 -05007101 /* Reset HBA FCF states after HBA reset */
7102 phba->fcf.fcf_flag = 0;
7103 phba->fcf.current_rec.flag = 0;
7104
James Smartda0436e2009-05-22 14:51:39 -04007105 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007106 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007107 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007108
7109 /* Start heart beat timer */
7110 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007111 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007112 phba->hb_outstanding = 0;
7113 phba->last_completion_time = jiffies;
7114
7115 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007116 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007117 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007118
James Smart75baf692010-06-08 18:31:21 -04007119 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7120 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7121 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7122 if (!rc) {
7123 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7124 "2829 This device supports "
7125 "Advanced Error Reporting (AER)\n");
7126 spin_lock_irq(&phba->hbalock);
7127 phba->hba_flag |= HBA_AER_ENABLED;
7128 spin_unlock_irq(&phba->hbalock);
7129 } else {
7130 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7131 "2830 This device does not support "
7132 "Advanced Error Reporting (AER)\n");
7133 phba->cfg_aer_support = 0;
7134 }
James Smart0a96e972011-07-22 18:37:28 -04007135 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007136 }
7137
James Smartda0436e2009-05-22 14:51:39 -04007138 /*
7139 * The port is ready, set the host's link state to LINK_DOWN
7140 * in preparation for link interrupts.
7141 */
James Smartda0436e2009-05-22 14:51:39 -04007142 spin_lock_irq(&phba->hbalock);
7143 phba->link_state = LPFC_LINK_DOWN;
7144 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007145 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7146 (phba->hba_flag & LINK_DISABLED)) {
7147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7148 "3103 Adapter Link is disabled.\n");
7149 lpfc_down_link(phba, mboxq);
7150 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7151 if (rc != MBX_SUCCESS) {
7152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7153 "3104 Adapter failed to issue "
7154 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7155 goto out_unset_queue;
7156 }
7157 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007158 /* don't perform init_link on SLI4 FC port loopback test */
7159 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7160 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7161 if (rc)
7162 goto out_unset_queue;
7163 }
James Smart5350d872011-10-10 21:33:49 -04007164 }
7165 mempool_free(mboxq, phba->mbox_mem_pool);
7166 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007167out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007168 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007169 lpfc_sli4_queue_unset(phba);
7170out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007171 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007172 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007173out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007174 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007175out_free_mbox:
7176 mempool_free(mboxq, phba->mbox_mem_pool);
7177 return rc;
7178}
James Smarte59058c2008-08-24 21:49:00 -04007179
7180/**
James Smart3621a712009-04-06 18:47:14 -04007181 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007182 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007183 *
James Smarte59058c2008-08-24 21:49:00 -04007184 * This is the callback function for mailbox timer. The mailbox
7185 * timer is armed when a new mailbox command is issued and the timer
7186 * is deleted when the mailbox complete. The function is called by
7187 * the kernel timer code when a mailbox does not complete within
7188 * expected time. This function wakes up the worker thread to
7189 * process the mailbox timeout and returns. All the processing is
7190 * done by the worker thread function lpfc_mbox_timeout_handler.
7191 **/
dea31012005-04-17 16:05:31 -05007192void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007193lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007194{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007195 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007196 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007197 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007198
James Smart2e0fef82007-06-17 19:56:36 -05007199 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007200 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007201 if (!tmo_posted)
7202 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7203 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7204
James Smart5e9d9b82008-06-14 22:52:53 -04007205 if (!tmo_posted)
7206 lpfc_worker_wake_up(phba);
7207 return;
dea31012005-04-17 16:05:31 -05007208}
7209
James Smarte8d3c3b2013-10-10 12:21:30 -04007210/**
7211 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7212 * are pending
7213 * @phba: Pointer to HBA context object.
7214 *
7215 * This function checks if any mailbox completions are present on the mailbox
7216 * completion queue.
7217 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007218static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007219lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7220{
7221
7222 uint32_t idx;
7223 struct lpfc_queue *mcq;
7224 struct lpfc_mcqe *mcqe;
7225 bool pending_completions = false;
7226
7227 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7228 return false;
7229
7230 /* Check for completions on mailbox completion queue */
7231
7232 mcq = phba->sli4_hba.mbx_cq;
7233 idx = mcq->hba_index;
7234 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7235 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7236 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7237 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7238 pending_completions = true;
7239 break;
7240 }
7241 idx = (idx + 1) % mcq->entry_count;
7242 if (mcq->hba_index == idx)
7243 break;
7244 }
7245 return pending_completions;
7246
7247}
7248
7249/**
7250 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7251 * that were missed.
7252 * @phba: Pointer to HBA context object.
7253 *
7254 * For sli4, it is possible to miss an interrupt. As such mbox completions
7255 * maybe missed causing erroneous mailbox timeouts to occur. This function
7256 * checks to see if mbox completions are on the mailbox completion queue
7257 * and will process all the completions associated with the eq for the
7258 * mailbox completion queue.
7259 **/
7260bool
7261lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7262{
7263
7264 uint32_t eqidx;
7265 struct lpfc_queue *fpeq = NULL;
7266 struct lpfc_eqe *eqe;
7267 bool mbox_pending;
7268
7269 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7270 return false;
7271
7272 /* Find the eq associated with the mcq */
7273
7274 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007275 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007276 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7277 phba->sli4_hba.mbx_cq->assoc_qid) {
7278 fpeq = phba->sli4_hba.hba_eq[eqidx];
7279 break;
7280 }
7281 if (!fpeq)
7282 return false;
7283
7284 /* Turn off interrupts from this EQ */
7285
7286 lpfc_sli4_eq_clr_intr(fpeq);
7287
7288 /* Check to see if a mbox completion is pending */
7289
7290 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7291
7292 /*
7293 * If a mbox completion is pending, process all the events on EQ
7294 * associated with the mbox completion queue (this could include
7295 * mailbox commands, async events, els commands, receive queue data
7296 * and fcp commands)
7297 */
7298
7299 if (mbox_pending)
7300 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7301 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7302 fpeq->EQ_processed++;
7303 }
7304
7305 /* Always clear and re-arm the EQ */
7306
7307 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7308
7309 return mbox_pending;
7310
7311}
James Smarte59058c2008-08-24 21:49:00 -04007312
7313/**
James Smart3621a712009-04-06 18:47:14 -04007314 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007315 * @phba: Pointer to HBA context object.
7316 *
7317 * This function is called from worker thread when a mailbox command times out.
7318 * The caller is not required to hold any locks. This function will reset the
7319 * HBA and recover all the pending commands.
7320 **/
dea31012005-04-17 16:05:31 -05007321void
7322lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7323{
James Smart2e0fef82007-06-17 19:56:36 -05007324 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007325 MAILBOX_t *mb = NULL;
7326
James Smart1dcb58e2007-04-25 09:51:30 -04007327 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007328
James Smarte8d3c3b2013-10-10 12:21:30 -04007329 /* If the mailbox completed, process the completion and return */
7330 if (lpfc_sli4_process_missed_mbox_completions(phba))
7331 return;
7332
James Smarteb016562014-09-03 12:58:06 -04007333 if (pmbox != NULL)
7334 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007335 /* Check the pmbox pointer first. There is a race condition
7336 * between the mbox timeout handler getting executed in the
7337 * worklist and the mailbox actually completing. When this
7338 * race condition occurs, the mbox_active will be NULL.
7339 */
7340 spin_lock_irq(&phba->hbalock);
7341 if (pmbox == NULL) {
7342 lpfc_printf_log(phba, KERN_WARNING,
7343 LOG_MBOX | LOG_SLI,
7344 "0353 Active Mailbox cleared - mailbox timeout "
7345 "exiting\n");
7346 spin_unlock_irq(&phba->hbalock);
7347 return;
7348 }
7349
dea31012005-04-17 16:05:31 -05007350 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007351 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007352 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007353 mb->mbxCommand,
7354 phba->pport->port_state,
7355 phba->sli.sli_flag,
7356 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007357 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007358
James Smart1dcb58e2007-04-25 09:51:30 -04007359 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7360 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007361 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007362 */
James Smart2e0fef82007-06-17 19:56:36 -05007363 spin_lock_irq(&phba->pport->work_port_lock);
7364 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7365 spin_unlock_irq(&phba->pport->work_port_lock);
7366 spin_lock_irq(&phba->hbalock);
7367 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007368 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007369 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007370
James Smartdb55fba2014-04-04 13:52:02 -04007371 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007372
7373 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007374 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007375
7376 /* Reset the HBA device */
7377 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007378}
7379
James Smarte59058c2008-08-24 21:49:00 -04007380/**
James Smart3772a992009-05-22 14:50:54 -04007381 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007382 * @phba: Pointer to HBA context object.
7383 * @pmbox: Pointer to mailbox object.
7384 * @flag: Flag indicating how the mailbox need to be processed.
7385 *
7386 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007387 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7388 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007389 * The mailbox command can be submitted in polling mode, in which case
7390 * this function will wait in a polling loop for the completion of the
7391 * mailbox.
7392 * If the mailbox is submitted in no_wait mode (not polling) the
7393 * function will submit the command and returns immediately without waiting
7394 * for the mailbox completion. The no_wait is supported only when HBA
7395 * is in SLI2/SLI3 mode - interrupts are enabled.
7396 * The SLI interface allows only one mailbox pending at a time. If the
7397 * mailbox is issued in polling mode and there is already a mailbox
7398 * pending, then the function will return an error. If the mailbox is issued
7399 * in NO_WAIT mode and there is a mailbox pending already, the function
7400 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7401 * The sli layer owns the mailbox object until the completion of mailbox
7402 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7403 * return codes the caller owns the mailbox command after the return of
7404 * the function.
7405 **/
James Smart3772a992009-05-22 14:50:54 -04007406static int
7407lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7408 uint32_t flag)
dea31012005-04-17 16:05:31 -05007409{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007410 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007411 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007412 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007413 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007414 int i;
James Smart09372822008-01-11 01:52:54 -05007415 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007416 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007417 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007418 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007419 int processing_queue = 0;
7420
7421 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7422 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007423 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007424 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007425 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7426 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7427 return MBX_SUCCESS;
7428 }
James Smart58da1ff2008-04-07 10:15:56 -04007429 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007430 pmbox = lpfc_mbox_get(phba);
7431 if (!pmbox) {
7432 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7433 return MBX_SUCCESS;
7434 }
7435 }
dea31012005-04-17 16:05:31 -05007436
James Smarted957682007-06-17 19:56:37 -05007437 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007438 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007439 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007440 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007441 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007442 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007443 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007444 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007445 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007446 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007447 }
7448 }
7449
Linas Vepstas8d63f372007-02-14 14:28:36 -06007450 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007451 if (unlikely(pci_channel_offline(phba->pcidev))) {
7452 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7453 goto out_not_finished;
7454 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007455
James Smarta257bf92009-04-06 18:48:10 -04007456 /* If HBA has a deferred error attention, fail the iocb. */
7457 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7458 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7459 goto out_not_finished;
7460 }
7461
dea31012005-04-17 16:05:31 -05007462 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007463
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007464 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007465 status = MBX_SUCCESS;
7466
James Smart2e0fef82007-06-17 19:56:36 -05007467 if (phba->link_state == LPFC_HBA_ERROR) {
7468 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007469
7470 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7472 "(%d):0311 Mailbox command x%x cannot "
7473 "issue Data: x%x x%x\n",
7474 pmbox->vport ? pmbox->vport->vpi : 0,
7475 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007476 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007477 }
7478
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007479 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007480 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7481 !(hc_copy & HC_MBINT_ENA)) {
7482 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7483 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007484 "(%d):2528 Mailbox command x%x cannot "
7485 "issue Data: x%x x%x\n",
7486 pmbox->vport ? pmbox->vport->vpi : 0,
7487 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007488 goto out_not_finished;
7489 }
James Smart92908312006-03-07 15:04:13 -05007490 }
7491
dea31012005-04-17 16:05:31 -05007492 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7493 /* Polling for a mbox command when another one is already active
7494 * is not allowed in SLI. Also, the driver must have established
7495 * SLI2 mode to queue and process multiple mbox commands.
7496 */
7497
7498 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007499 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007500
7501 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7503 "(%d):2529 Mailbox command x%x "
7504 "cannot issue Data: x%x x%x\n",
7505 pmbox->vport ? pmbox->vport->vpi : 0,
7506 pmbox->u.mb.mbxCommand,
7507 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007508 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007509 }
7510
James Smart3772a992009-05-22 14:50:54 -04007511 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007512 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007513 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007514 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7515 "(%d):2530 Mailbox command x%x "
7516 "cannot issue Data: x%x x%x\n",
7517 pmbox->vport ? pmbox->vport->vpi : 0,
7518 pmbox->u.mb.mbxCommand,
7519 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007520 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007521 }
7522
dea31012005-04-17 16:05:31 -05007523 /* Another mailbox command is still being processed, queue this
7524 * command to be processed later.
7525 */
7526 lpfc_mbox_put(phba, pmbox);
7527
7528 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007529 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007530 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007531 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007532 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007533 mbx->mbxCommand,
7534 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007535 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007536
7537 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007538 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007539
James Smart858c9f62007-06-17 19:56:39 -05007540 if (pmbox->vport) {
7541 lpfc_debugfs_disc_trc(pmbox->vport,
7542 LPFC_DISC_TRC_MBOX_VPORT,
7543 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007544 (uint32_t)mbx->mbxCommand,
7545 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007546 }
7547 else {
7548 lpfc_debugfs_disc_trc(phba->pport,
7549 LPFC_DISC_TRC_MBOX,
7550 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007551 (uint32_t)mbx->mbxCommand,
7552 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007553 }
7554
James Smart2e0fef82007-06-17 19:56:36 -05007555 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007556 }
7557
dea31012005-04-17 16:05:31 -05007558 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7559
7560 /* If we are not polling, we MUST be in SLI2 mode */
7561 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007562 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007563 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007564 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007565 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007566 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7568 "(%d):2531 Mailbox command x%x "
7569 "cannot issue Data: x%x x%x\n",
7570 pmbox->vport ? pmbox->vport->vpi : 0,
7571 pmbox->u.mb.mbxCommand,
7572 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007573 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007574 }
7575 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007576 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7577 1000);
7578 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007579 }
7580
7581 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007582 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007583 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007584 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007585 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007586 mbx->mbxCommand,
7587 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007588 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007589
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007590 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007591 if (pmbox->vport) {
7592 lpfc_debugfs_disc_trc(pmbox->vport,
7593 LPFC_DISC_TRC_MBOX_VPORT,
7594 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007595 (uint32_t)mbx->mbxCommand,
7596 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007597 }
7598 else {
7599 lpfc_debugfs_disc_trc(phba->pport,
7600 LPFC_DISC_TRC_MBOX,
7601 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007602 (uint32_t)mbx->mbxCommand,
7603 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007604 }
7605 }
7606
dea31012005-04-17 16:05:31 -05007607 psli->slistat.mbox_cmd++;
7608 evtctr = psli->slistat.mbox_event;
7609
7610 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007611 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007612
James Smart3772a992009-05-22 14:50:54 -04007613 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007614 /* Populate mbox extension offset word. */
7615 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007616 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007617 = (uint8_t *)phba->mbox_ext
7618 - (uint8_t *)phba->mbox;
7619 }
7620
7621 /* Copy the mailbox extension data */
7622 if (pmbox->in_ext_byte_len && pmbox->context2) {
7623 lpfc_sli_pcimem_bcopy(pmbox->context2,
7624 (uint8_t *)phba->mbox_ext,
7625 pmbox->in_ext_byte_len);
7626 }
7627 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007628 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007629 } else {
James Smart7a470272010-03-15 11:25:20 -04007630 /* Populate mbox extension offset word. */
7631 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007632 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007633 = MAILBOX_HBA_EXT_OFFSET;
7634
7635 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007636 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007637 lpfc_memcpy_to_slim(phba->MBslimaddr +
7638 MAILBOX_HBA_EXT_OFFSET,
7639 pmbox->context2, pmbox->in_ext_byte_len);
7640
James Smart895427b2017-02-12 13:52:30 -08007641 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007642 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007643 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7644 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007645
7646 /* First copy mbox command data to HBA SLIM, skip past first
7647 word */
7648 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007649 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007650 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7651
7652 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007653 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007654 to_slim = phba->MBslimaddr;
7655 writel(ldata, to_slim);
7656 readl(to_slim); /* flush */
7657
James Smart895427b2017-02-12 13:52:30 -08007658 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007659 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007660 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007661 }
7662
7663 wmb();
dea31012005-04-17 16:05:31 -05007664
7665 switch (flag) {
7666 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007667 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007668 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007669 /* Interrupt board to do it */
7670 writel(CA_MBATT, phba->CAregaddr);
7671 readl(phba->CAregaddr); /* flush */
7672 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007673 break;
7674
7675 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007676 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007677 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007678 /* Interrupt board to do it */
7679 writel(CA_MBATT, phba->CAregaddr);
7680 readl(phba->CAregaddr); /* flush */
7681
James Smart3772a992009-05-22 14:50:54 -04007682 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007683 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007684 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007685 word0 = le32_to_cpu(word0);
7686 } else {
7687 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007688 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7689 spin_unlock_irqrestore(&phba->hbalock,
7690 drvr_flag);
7691 goto out_not_finished;
7692 }
dea31012005-04-17 16:05:31 -05007693 }
7694
7695 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007696 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7697 spin_unlock_irqrestore(&phba->hbalock,
7698 drvr_flag);
7699 goto out_not_finished;
7700 }
James Smarta183a152011-10-10 21:32:43 -04007701 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7702 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007703 i = 0;
dea31012005-04-17 16:05:31 -05007704 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007705 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7706 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007707 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007708 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007709 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007710 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007711 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007712 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007713 }
7714
7715 /* Check if we took a mbox interrupt while we were
7716 polling */
7717 if (((word0 & OWN_CHIP) != OWN_CHIP)
7718 && (evtctr != psli->slistat.mbox_event))
7719 break;
7720
James Smart09372822008-01-11 01:52:54 -05007721 if (i++ > 10) {
7722 spin_unlock_irqrestore(&phba->hbalock,
7723 drvr_flag);
7724 msleep(1);
7725 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7726 }
dea31012005-04-17 16:05:31 -05007727
James Smart3772a992009-05-22 14:50:54 -04007728 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007729 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007730 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007731 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007732 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007733 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007734 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007735 /* Check real SLIM for any errors */
7736 slimword0 = readl(phba->MBslimaddr);
7737 slimmb = (MAILBOX_t *) & slimword0;
7738 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7739 && slimmb->mbxStatus) {
7740 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007741 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007742 word0 = slimword0;
7743 }
7744 }
7745 } else {
7746 /* First copy command data */
7747 word0 = readl(phba->MBslimaddr);
7748 }
7749 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007750 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7751 spin_unlock_irqrestore(&phba->hbalock,
7752 drvr_flag);
7753 goto out_not_finished;
7754 }
dea31012005-04-17 16:05:31 -05007755 }
7756
James Smart3772a992009-05-22 14:50:54 -04007757 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007758 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007759 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7760 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007761 /* Copy the mailbox extension data */
7762 if (pmbox->out_ext_byte_len && pmbox->context2) {
7763 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7764 pmbox->context2,
7765 pmbox->out_ext_byte_len);
7766 }
dea31012005-04-17 16:05:31 -05007767 } else {
7768 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007769 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007770 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007771 /* Copy the mailbox extension data */
7772 if (pmbox->out_ext_byte_len && pmbox->context2) {
7773 lpfc_memcpy_from_slim(pmbox->context2,
7774 phba->MBslimaddr +
7775 MAILBOX_HBA_EXT_OFFSET,
7776 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007777 }
7778 }
7779
7780 writel(HA_MBATT, phba->HAregaddr);
7781 readl(phba->HAregaddr); /* flush */
7782
7783 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007784 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007785 }
7786
James Smart2e0fef82007-06-17 19:56:36 -05007787 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7788 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007789
7790out_not_finished:
7791 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007792 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007793 lpfc_mbox_cmpl_put(phba, pmbox);
7794 }
7795 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007796}
7797
James Smarte59058c2008-08-24 21:49:00 -04007798/**
James Smartf1126682009-06-10 17:22:44 -04007799 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7800 * @phba: Pointer to HBA context object.
7801 *
7802 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7803 * the driver internal pending mailbox queue. It will then try to wait out the
7804 * possible outstanding mailbox command before return.
7805 *
7806 * Returns:
7807 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7808 * the outstanding mailbox command timed out.
7809 **/
7810static int
7811lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7812{
7813 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007814 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007815 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007816
7817 /* Mark the asynchronous mailbox command posting as blocked */
7818 spin_lock_irq(&phba->hbalock);
7819 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007820 /* Determine how long we might wait for the active mailbox
7821 * command to be gracefully completed by firmware.
7822 */
James Smarta183a152011-10-10 21:32:43 -04007823 if (phba->sli.mbox_active)
7824 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7825 phba->sli.mbox_active) *
7826 1000) + jiffies;
7827 spin_unlock_irq(&phba->hbalock);
7828
James Smarte8d3c3b2013-10-10 12:21:30 -04007829 /* Make sure the mailbox is really active */
7830 if (timeout)
7831 lpfc_sli4_process_missed_mbox_completions(phba);
7832
James Smartf1126682009-06-10 17:22:44 -04007833 /* Wait for the outstnading mailbox command to complete */
7834 while (phba->sli.mbox_active) {
7835 /* Check active mailbox complete status every 2ms */
7836 msleep(2);
7837 if (time_after(jiffies, timeout)) {
7838 /* Timeout, marked the outstanding cmd not complete */
7839 rc = 1;
7840 break;
7841 }
7842 }
7843
7844 /* Can not cleanly block async mailbox command, fails it */
7845 if (rc) {
7846 spin_lock_irq(&phba->hbalock);
7847 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7848 spin_unlock_irq(&phba->hbalock);
7849 }
7850 return rc;
7851}
7852
7853/**
7854 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7855 * @phba: Pointer to HBA context object.
7856 *
7857 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7858 * commands from the driver internal pending mailbox queue. It makes sure
7859 * that there is no outstanding mailbox command before resuming posting
7860 * asynchronous mailbox commands. If, for any reason, there is outstanding
7861 * mailbox command, it will try to wait it out before resuming asynchronous
7862 * mailbox command posting.
7863 **/
7864static void
7865lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7866{
7867 struct lpfc_sli *psli = &phba->sli;
7868
7869 spin_lock_irq(&phba->hbalock);
7870 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7871 /* Asynchronous mailbox posting is not blocked, do nothing */
7872 spin_unlock_irq(&phba->hbalock);
7873 return;
7874 }
7875
7876 /* Outstanding synchronous mailbox command is guaranteed to be done,
7877 * successful or timeout, after timing-out the outstanding mailbox
7878 * command shall always be removed, so just unblock posting async
7879 * mailbox command and resume
7880 */
7881 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7882 spin_unlock_irq(&phba->hbalock);
7883
7884 /* wake up worker thread to post asynchronlous mailbox command */
7885 lpfc_worker_wake_up(phba);
7886}
7887
7888/**
James Smart2d843ed2012-09-29 11:29:06 -04007889 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7890 * @phba: Pointer to HBA context object.
7891 * @mboxq: Pointer to mailbox object.
7892 *
7893 * The function waits for the bootstrap mailbox register ready bit from
7894 * port for twice the regular mailbox command timeout value.
7895 *
7896 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7897 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7898 **/
7899static int
7900lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7901{
7902 uint32_t db_ready;
7903 unsigned long timeout;
7904 struct lpfc_register bmbx_reg;
7905
7906 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7907 * 1000) + jiffies;
7908
7909 do {
7910 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7911 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7912 if (!db_ready)
7913 msleep(2);
7914
7915 if (time_after(jiffies, timeout))
7916 return MBXERR_ERROR;
7917 } while (!db_ready);
7918
7919 return 0;
7920}
7921
7922/**
James Smartda0436e2009-05-22 14:51:39 -04007923 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7924 * @phba: Pointer to HBA context object.
7925 * @mboxq: Pointer to mailbox object.
7926 *
7927 * The function posts a mailbox to the port. The mailbox is expected
7928 * to be comletely filled in and ready for the port to operate on it.
7929 * This routine executes a synchronous completion operation on the
7930 * mailbox by polling for its completion.
7931 *
7932 * The caller must not be holding any locks when calling this routine.
7933 *
7934 * Returns:
7935 * MBX_SUCCESS - mailbox posted successfully
7936 * Any of the MBX error values.
7937 **/
7938static int
7939lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7940{
7941 int rc = MBX_SUCCESS;
7942 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007943 uint32_t mcqe_status;
7944 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007945 struct lpfc_sli *psli = &phba->sli;
7946 struct lpfc_mqe *mb = &mboxq->u.mqe;
7947 struct lpfc_bmbx_create *mbox_rgn;
7948 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007949
7950 /*
7951 * Only one mailbox can be active to the bootstrap mailbox region
7952 * at a time and there is no queueing provided.
7953 */
7954 spin_lock_irqsave(&phba->hbalock, iflag);
7955 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7956 spin_unlock_irqrestore(&phba->hbalock, iflag);
7957 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007958 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007959 "cannot issue Data: x%x x%x\n",
7960 mboxq->vport ? mboxq->vport->vpi : 0,
7961 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007962 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7963 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007964 psli->sli_flag, MBX_POLL);
7965 return MBXERR_ERROR;
7966 }
7967 /* The server grabs the token and owns it until release */
7968 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7969 phba->sli.mbox_active = mboxq;
7970 spin_unlock_irqrestore(&phba->hbalock, iflag);
7971
James Smart2d843ed2012-09-29 11:29:06 -04007972 /* wait for bootstrap mbox register for readyness */
7973 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7974 if (rc)
7975 goto exit;
7976
James Smartda0436e2009-05-22 14:51:39 -04007977 /*
7978 * Initialize the bootstrap memory region to avoid stale data areas
7979 * in the mailbox post. Then copy the caller's mailbox contents to
7980 * the bmbx mailbox region.
7981 */
7982 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7983 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7984 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7985 sizeof(struct lpfc_mqe));
7986
7987 /* Post the high mailbox dma address to the port and wait for ready. */
7988 dma_address = &phba->sli4_hba.bmbx.dma_address;
7989 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7990
James Smart2d843ed2012-09-29 11:29:06 -04007991 /* wait for bootstrap mbox register for hi-address write done */
7992 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7993 if (rc)
7994 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007995
7996 /* Post the low mailbox dma address to the port. */
7997 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007998
James Smart2d843ed2012-09-29 11:29:06 -04007999 /* wait for bootstrap mbox register for low address write done */
8000 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8001 if (rc)
8002 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008003
8004 /*
8005 * Read the CQ to ensure the mailbox has completed.
8006 * If so, update the mailbox status so that the upper layers
8007 * can complete the request normally.
8008 */
8009 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8010 sizeof(struct lpfc_mqe));
8011 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8012 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8013 sizeof(struct lpfc_mcqe));
8014 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008015 /*
8016 * When the CQE status indicates a failure and the mailbox status
8017 * indicates success then copy the CQE status into the mailbox status
8018 * (and prefix it with x4000).
8019 */
James Smartda0436e2009-05-22 14:51:39 -04008020 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008021 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8022 bf_set(lpfc_mqe_status, mb,
8023 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008024 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008025 } else
8026 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008027
8028 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008029 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008030 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8031 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008032 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8033 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8034 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008035 bf_get(lpfc_mqe_status, mb),
8036 mb->un.mb_words[0], mb->un.mb_words[1],
8037 mb->un.mb_words[2], mb->un.mb_words[3],
8038 mb->un.mb_words[4], mb->un.mb_words[5],
8039 mb->un.mb_words[6], mb->un.mb_words[7],
8040 mb->un.mb_words[8], mb->un.mb_words[9],
8041 mb->un.mb_words[10], mb->un.mb_words[11],
8042 mb->un.mb_words[12], mboxq->mcqe.word0,
8043 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8044 mboxq->mcqe.trailer);
8045exit:
8046 /* We are holding the token, no needed for lock when release */
8047 spin_lock_irqsave(&phba->hbalock, iflag);
8048 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8049 phba->sli.mbox_active = NULL;
8050 spin_unlock_irqrestore(&phba->hbalock, iflag);
8051 return rc;
8052}
8053
8054/**
8055 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8056 * @phba: Pointer to HBA context object.
8057 * @pmbox: Pointer to mailbox object.
8058 * @flag: Flag indicating how the mailbox need to be processed.
8059 *
8060 * This function is called by discovery code and HBA management code to submit
8061 * a mailbox command to firmware with SLI-4 interface spec.
8062 *
8063 * Return codes the caller owns the mailbox command after the return of the
8064 * function.
8065 **/
8066static int
8067lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8068 uint32_t flag)
8069{
8070 struct lpfc_sli *psli = &phba->sli;
8071 unsigned long iflags;
8072 int rc;
8073
James Smartb76f2dc2011-07-22 18:37:42 -04008074 /* dump from issue mailbox command if setup */
8075 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8076
James Smart8fa38512009-07-19 10:01:03 -04008077 rc = lpfc_mbox_dev_check(phba);
8078 if (unlikely(rc)) {
8079 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008080 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008081 "cannot issue Data: x%x x%x\n",
8082 mboxq->vport ? mboxq->vport->vpi : 0,
8083 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008084 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8085 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008086 psli->sli_flag, flag);
8087 goto out_not_finished;
8088 }
8089
James Smartda0436e2009-05-22 14:51:39 -04008090 /* Detect polling mode and jump to a handler */
8091 if (!phba->sli4_hba.intr_enable) {
8092 if (flag == MBX_POLL)
8093 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8094 else
8095 rc = -EIO;
8096 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008097 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008098 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008099 "(x%x/x%x) failure: "
8100 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8101 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008102 mboxq->vport ? mboxq->vport->vpi : 0,
8103 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008104 lpfc_sli_config_mbox_subsys_get(phba,
8105 mboxq),
8106 lpfc_sli_config_mbox_opcode_get(phba,
8107 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008108 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8109 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8110 bf_get(lpfc_mcqe_ext_status,
8111 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008112 psli->sli_flag, flag);
8113 return rc;
8114 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008115 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8116 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008117 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008118 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008119 mboxq->vport ? mboxq->vport->vpi : 0,
8120 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008121 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8122 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008123 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008124 /* Try to block the asynchronous mailbox posting */
8125 rc = lpfc_sli4_async_mbox_block(phba);
8126 if (!rc) {
8127 /* Successfully blocked, now issue sync mbox cmd */
8128 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8129 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008130 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008131 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008132 "(%d):2597 Sync Mailbox command "
8133 "x%x (x%x/x%x) failure: "
8134 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8135 "Data: x%x x%x\n,",
8136 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008137 mboxq->u.mb.mbxCommand,
8138 lpfc_sli_config_mbox_subsys_get(phba,
8139 mboxq),
8140 lpfc_sli_config_mbox_opcode_get(phba,
8141 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008142 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8143 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8144 bf_get(lpfc_mcqe_ext_status,
8145 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008146 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008147 /* Unblock the async mailbox posting afterward */
8148 lpfc_sli4_async_mbox_unblock(phba);
8149 }
8150 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008151 }
8152
8153 /* Now, interrupt mode asynchrous mailbox command */
8154 rc = lpfc_mbox_cmd_check(phba, mboxq);
8155 if (rc) {
8156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008157 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008158 "cannot issue Data: x%x x%x\n",
8159 mboxq->vport ? mboxq->vport->vpi : 0,
8160 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008161 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8162 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008163 psli->sli_flag, flag);
8164 goto out_not_finished;
8165 }
James Smartda0436e2009-05-22 14:51:39 -04008166
8167 /* Put the mailbox command to the driver internal FIFO */
8168 psli->slistat.mbox_busy++;
8169 spin_lock_irqsave(&phba->hbalock, iflags);
8170 lpfc_mbox_put(phba, mboxq);
8171 spin_unlock_irqrestore(&phba->hbalock, iflags);
8172 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8173 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008174 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008175 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8176 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008177 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8178 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008179 phba->pport->port_state,
8180 psli->sli_flag, MBX_NOWAIT);
8181 /* Wake up worker thread to transport mailbox command from head */
8182 lpfc_worker_wake_up(phba);
8183
8184 return MBX_BUSY;
8185
8186out_not_finished:
8187 return MBX_NOT_FINISHED;
8188}
8189
8190/**
8191 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8192 * @phba: Pointer to HBA context object.
8193 *
8194 * This function is called by worker thread to send a mailbox command to
8195 * SLI4 HBA firmware.
8196 *
8197 **/
8198int
8199lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8200{
8201 struct lpfc_sli *psli = &phba->sli;
8202 LPFC_MBOXQ_t *mboxq;
8203 int rc = MBX_SUCCESS;
8204 unsigned long iflags;
8205 struct lpfc_mqe *mqe;
8206 uint32_t mbx_cmnd;
8207
8208 /* Check interrupt mode before post async mailbox command */
8209 if (unlikely(!phba->sli4_hba.intr_enable))
8210 return MBX_NOT_FINISHED;
8211
8212 /* Check for mailbox command service token */
8213 spin_lock_irqsave(&phba->hbalock, iflags);
8214 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8215 spin_unlock_irqrestore(&phba->hbalock, iflags);
8216 return MBX_NOT_FINISHED;
8217 }
8218 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8219 spin_unlock_irqrestore(&phba->hbalock, iflags);
8220 return MBX_NOT_FINISHED;
8221 }
8222 if (unlikely(phba->sli.mbox_active)) {
8223 spin_unlock_irqrestore(&phba->hbalock, iflags);
8224 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8225 "0384 There is pending active mailbox cmd\n");
8226 return MBX_NOT_FINISHED;
8227 }
8228 /* Take the mailbox command service token */
8229 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8230
8231 /* Get the next mailbox command from head of queue */
8232 mboxq = lpfc_mbox_get(phba);
8233
8234 /* If no more mailbox command waiting for post, we're done */
8235 if (!mboxq) {
8236 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8237 spin_unlock_irqrestore(&phba->hbalock, iflags);
8238 return MBX_SUCCESS;
8239 }
8240 phba->sli.mbox_active = mboxq;
8241 spin_unlock_irqrestore(&phba->hbalock, iflags);
8242
8243 /* Check device readiness for posting mailbox command */
8244 rc = lpfc_mbox_dev_check(phba);
8245 if (unlikely(rc))
8246 /* Driver clean routine will clean up pending mailbox */
8247 goto out_not_finished;
8248
8249 /* Prepare the mbox command to be posted */
8250 mqe = &mboxq->u.mqe;
8251 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8252
8253 /* Start timer for the mbox_tmo and log some mailbox post messages */
8254 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008255 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008256
8257 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008258 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008259 "x%x x%x\n",
8260 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008261 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8262 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008263 phba->pport->port_state, psli->sli_flag);
8264
8265 if (mbx_cmnd != MBX_HEARTBEAT) {
8266 if (mboxq->vport) {
8267 lpfc_debugfs_disc_trc(mboxq->vport,
8268 LPFC_DISC_TRC_MBOX_VPORT,
8269 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8270 mbx_cmnd, mqe->un.mb_words[0],
8271 mqe->un.mb_words[1]);
8272 } else {
8273 lpfc_debugfs_disc_trc(phba->pport,
8274 LPFC_DISC_TRC_MBOX,
8275 "MBOX Send: cmd:x%x mb:x%x x%x",
8276 mbx_cmnd, mqe->un.mb_words[0],
8277 mqe->un.mb_words[1]);
8278 }
8279 }
8280 psli->slistat.mbox_cmd++;
8281
8282 /* Post the mailbox command to the port */
8283 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8284 if (rc != MBX_SUCCESS) {
8285 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008286 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008287 "cannot issue Data: x%x x%x\n",
8288 mboxq->vport ? mboxq->vport->vpi : 0,
8289 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008290 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8291 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008292 psli->sli_flag, MBX_NOWAIT);
8293 goto out_not_finished;
8294 }
8295
8296 return rc;
8297
8298out_not_finished:
8299 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008300 if (phba->sli.mbox_active) {
8301 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8302 __lpfc_mbox_cmpl_put(phba, mboxq);
8303 /* Release the token */
8304 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8305 phba->sli.mbox_active = NULL;
8306 }
James Smartda0436e2009-05-22 14:51:39 -04008307 spin_unlock_irqrestore(&phba->hbalock, iflags);
8308
8309 return MBX_NOT_FINISHED;
8310}
8311
8312/**
8313 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8314 * @phba: Pointer to HBA context object.
8315 * @pmbox: Pointer to mailbox object.
8316 * @flag: Flag indicating how the mailbox need to be processed.
8317 *
8318 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8319 * the API jump table function pointer from the lpfc_hba struct.
8320 *
8321 * Return codes the caller owns the mailbox command after the return of the
8322 * function.
8323 **/
8324int
8325lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8326{
8327 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8328}
8329
8330/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008331 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008332 * @phba: The hba struct for which this call is being executed.
8333 * @dev_grp: The HBA PCI-Device group number.
8334 *
8335 * This routine sets up the mbox interface API function jump table in @phba
8336 * struct.
8337 * Returns: 0 - success, -ENODEV - failure.
8338 **/
8339int
8340lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8341{
8342
8343 switch (dev_grp) {
8344 case LPFC_PCI_DEV_LP:
8345 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8346 phba->lpfc_sli_handle_slow_ring_event =
8347 lpfc_sli_handle_slow_ring_event_s3;
8348 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8349 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8350 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8351 break;
8352 case LPFC_PCI_DEV_OC:
8353 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8354 phba->lpfc_sli_handle_slow_ring_event =
8355 lpfc_sli_handle_slow_ring_event_s4;
8356 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8357 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8358 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8359 break;
8360 default:
8361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8362 "1420 Invalid HBA PCI-device group: 0x%x\n",
8363 dev_grp);
8364 return -ENODEV;
8365 break;
8366 }
8367 return 0;
8368}
8369
8370/**
James Smart3621a712009-04-06 18:47:14 -04008371 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008372 * @phba: Pointer to HBA context object.
8373 * @pring: Pointer to driver SLI ring object.
8374 * @piocb: Pointer to address of newly added command iocb.
8375 *
8376 * This function is called with hbalock held to add a command
8377 * iocb to the txq when SLI layer cannot submit the command iocb
8378 * to the ring.
8379 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008380void
James Smart92d7f7b2007-06-17 19:56:38 -05008381__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008382 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008383{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008384 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008385 /* Insert the caller's iocb in the txq tail for later processing. */
8386 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008387}
8388
James Smarte59058c2008-08-24 21:49:00 -04008389/**
James Smart3621a712009-04-06 18:47:14 -04008390 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008391 * @phba: Pointer to HBA context object.
8392 * @pring: Pointer to driver SLI ring object.
8393 * @piocb: Pointer to address of newly added command iocb.
8394 *
8395 * This function is called with hbalock held before a new
8396 * iocb is submitted to the firmware. This function checks
8397 * txq to flush the iocbs in txq to Firmware before
8398 * submitting new iocbs to the Firmware.
8399 * If there are iocbs in the txq which need to be submitted
8400 * to firmware, lpfc_sli_next_iocb returns the first element
8401 * of the txq after dequeuing it from txq.
8402 * If there is no iocb in the txq then the function will return
8403 * *piocb and *piocb is set to NULL. Caller needs to check
8404 * *piocb to find if there are more commands in the txq.
8405 **/
dea31012005-04-17 16:05:31 -05008406static struct lpfc_iocbq *
8407lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008408 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008409{
8410 struct lpfc_iocbq * nextiocb;
8411
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008412 lockdep_assert_held(&phba->hbalock);
8413
dea31012005-04-17 16:05:31 -05008414 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8415 if (!nextiocb) {
8416 nextiocb = *piocb;
8417 *piocb = NULL;
8418 }
8419
8420 return nextiocb;
8421}
8422
James Smarte59058c2008-08-24 21:49:00 -04008423/**
James Smart3772a992009-05-22 14:50:54 -04008424 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008425 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008426 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008427 * @piocb: Pointer to command iocb.
8428 * @flag: Flag indicating if this command can be put into txq.
8429 *
James Smart3772a992009-05-22 14:50:54 -04008430 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8431 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8432 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8433 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8434 * this function allows only iocbs for posting buffers. This function finds
8435 * next available slot in the command ring and posts the command to the
8436 * available slot and writes the port attention register to request HBA start
8437 * processing new iocb. If there is no slot available in the ring and
8438 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8439 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008440 *
James Smart3772a992009-05-22 14:50:54 -04008441 * This function is called with hbalock held. The function will return success
8442 * after it successfully submit the iocb to firmware or after adding to the
8443 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008444 **/
James Smart98c9ea52007-10-27 13:37:33 -04008445static int
James Smart3772a992009-05-22 14:50:54 -04008446__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008447 struct lpfc_iocbq *piocb, uint32_t flag)
8448{
8449 struct lpfc_iocbq *nextiocb;
8450 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008451 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008452
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008453 lockdep_assert_held(&phba->hbalock);
8454
James Smart92d7f7b2007-06-17 19:56:38 -05008455 if (piocb->iocb_cmpl && (!piocb->vport) &&
8456 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8457 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8458 lpfc_printf_log(phba, KERN_ERR,
8459 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008460 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008461 piocb->iocb.ulpCommand);
8462 dump_stack();
8463 return IOCB_ERROR;
8464 }
8465
8466
Linas Vepstas8d63f372007-02-14 14:28:36 -06008467 /* If the PCI channel is in offline state, do not post iocbs. */
8468 if (unlikely(pci_channel_offline(phba->pcidev)))
8469 return IOCB_ERROR;
8470
James Smarta257bf92009-04-06 18:48:10 -04008471 /* If HBA has a deferred error attention, fail the iocb. */
8472 if (unlikely(phba->hba_flag & DEFER_ERATT))
8473 return IOCB_ERROR;
8474
dea31012005-04-17 16:05:31 -05008475 /*
8476 * We should never get an IOCB if we are in a < LINK_DOWN state
8477 */
James Smart2e0fef82007-06-17 19:56:36 -05008478 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008479 return IOCB_ERROR;
8480
8481 /*
8482 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008483 * outstanding event.
dea31012005-04-17 16:05:31 -05008484 */
James Smart0b727fe2007-10-27 13:37:25 -04008485 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008486 goto iocb_busy;
8487
James Smart2e0fef82007-06-17 19:56:36 -05008488 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008489 /*
James Smart2680eea2007-04-25 09:52:55 -04008490 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008491 * can be issued if the link is not up.
8492 */
8493 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008494 case CMD_GEN_REQUEST64_CR:
8495 case CMD_GEN_REQUEST64_CX:
8496 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8497 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008498 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008499 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8500 MENLO_TRANSPORT_TYPE))
8501
8502 goto iocb_busy;
8503 break;
dea31012005-04-17 16:05:31 -05008504 case CMD_QUE_RING_BUF_CN:
8505 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008506 /*
8507 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8508 * completion, iocb_cmpl MUST be 0.
8509 */
8510 if (piocb->iocb_cmpl)
8511 piocb->iocb_cmpl = NULL;
8512 /*FALLTHROUGH*/
8513 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008514 case CMD_CLOSE_XRI_CN:
8515 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008516 break;
8517 default:
8518 goto iocb_busy;
8519 }
8520
8521 /*
8522 * For FCP commands, we must be in a state where we can process link
8523 * attention events.
8524 */
James Smart895427b2017-02-12 13:52:30 -08008525 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008526 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008527 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008528 }
dea31012005-04-17 16:05:31 -05008529
dea31012005-04-17 16:05:31 -05008530 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8531 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8532 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8533
8534 if (iocb)
8535 lpfc_sli_update_ring(phba, pring);
8536 else
8537 lpfc_sli_update_full_ring(phba, pring);
8538
8539 if (!piocb)
8540 return IOCB_SUCCESS;
8541
8542 goto out_busy;
8543
8544 iocb_busy:
8545 pring->stats.iocb_cmd_delay++;
8546
8547 out_busy:
8548
8549 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008550 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008551 return IOCB_SUCCESS;
8552 }
8553
8554 return IOCB_BUSY;
8555}
8556
James Smart3772a992009-05-22 14:50:54 -04008557/**
James Smart4f774512009-05-22 14:52:35 -04008558 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8559 * @phba: Pointer to HBA context object.
8560 * @piocb: Pointer to command iocb.
8561 * @sglq: Pointer to the scatter gather queue object.
8562 *
8563 * This routine converts the bpl or bde that is in the IOCB
8564 * to a sgl list for the sli4 hardware. The physical address
8565 * of the bpl/bde is converted back to a virtual address.
8566 * If the IOCB contains a BPL then the list of BDE's is
8567 * converted to sli4_sge's. If the IOCB contains a single
8568 * BDE then it is converted to a single sli_sge.
8569 * The IOCB is still in cpu endianess so the contents of
8570 * the bpl can be used without byte swapping.
8571 *
8572 * Returns valid XRI = Success, NO_XRI = Failure.
8573**/
8574static uint16_t
8575lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8576 struct lpfc_sglq *sglq)
8577{
8578 uint16_t xritag = NO_XRI;
8579 struct ulp_bde64 *bpl = NULL;
8580 struct ulp_bde64 bde;
8581 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008582 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008583 IOCB_t *icmd;
8584 int numBdes = 0;
8585 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008586 uint32_t offset = 0; /* accumulated offset in the sg request list */
8587 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008588
8589 if (!piocbq || !sglq)
8590 return xritag;
8591
8592 sgl = (struct sli4_sge *)sglq->sgl;
8593 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008594 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8595 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008596 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8597 numBdes = icmd->un.genreq64.bdl.bdeSize /
8598 sizeof(struct ulp_bde64);
8599 /* The addrHigh and addrLow fields within the IOCB
8600 * have not been byteswapped yet so there is no
8601 * need to swap them back.
8602 */
James Smart1b511972011-12-13 13:23:09 -05008603 if (piocbq->context3)
8604 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8605 else
8606 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008607
James Smart1b511972011-12-13 13:23:09 -05008608 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008609 if (!bpl)
8610 return xritag;
8611
8612 for (i = 0; i < numBdes; i++) {
8613 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008614 sgl->addr_hi = bpl->addrHigh;
8615 sgl->addr_lo = bpl->addrLow;
8616
James Smart05580562011-05-24 11:40:48 -04008617 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008618 if ((i+1) == numBdes)
8619 bf_set(lpfc_sli4_sge_last, sgl, 1);
8620 else
8621 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008622 /* swap the size field back to the cpu so we
8623 * can assign it to the sgl.
8624 */
8625 bde.tus.w = le32_to_cpu(bpl->tus.w);
8626 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008627 /* The offsets in the sgl need to be accumulated
8628 * separately for the request and reply lists.
8629 * The request is always first, the reply follows.
8630 */
8631 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8632 /* add up the reply sg entries */
8633 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8634 inbound++;
8635 /* first inbound? reset the offset */
8636 if (inbound == 1)
8637 offset = 0;
8638 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008639 bf_set(lpfc_sli4_sge_type, sgl,
8640 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008641 offset += bde.tus.f.bdeSize;
8642 }
James Smart546fc852011-03-11 16:06:29 -05008643 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008644 bpl++;
8645 sgl++;
8646 }
8647 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8648 /* The addrHigh and addrLow fields of the BDE have not
8649 * been byteswapped yet so they need to be swapped
8650 * before putting them in the sgl.
8651 */
8652 sgl->addr_hi =
8653 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8654 sgl->addr_lo =
8655 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008656 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008657 bf_set(lpfc_sli4_sge_last, sgl, 1);
8658 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008659 sgl->sge_len =
8660 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008661 }
8662 return sglq->sli4_xritag;
8663}
8664
8665/**
James Smart4f774512009-05-22 14:52:35 -04008666 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8667 * @phba: Pointer to HBA context object.
8668 * @piocb: Pointer to command iocb.
8669 * @wqe: Pointer to the work queue entry.
8670 *
8671 * This routine converts the iocb command to its Work Queue Entry
8672 * equivalent. The wqe pointer should not have any fields set when
8673 * this routine is called because it will memcpy over them.
8674 * This routine does not set the CQ_ID or the WQEC bits in the
8675 * wqe.
8676 *
8677 * Returns: 0 = Success, IOCB_ERROR = Failure.
8678 **/
8679static int
8680lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8681 union lpfc_wqe *wqe)
8682{
James Smart5ffc2662009-11-18 15:39:44 -05008683 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008684 uint8_t ct = 0;
8685 uint32_t fip;
8686 uint32_t abort_tag;
8687 uint8_t command_type = ELS_COMMAND_NON_FIP;
8688 uint8_t cmnd;
8689 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008690 uint16_t abrt_iotag;
8691 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008692 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008693 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008694 int numBdes, i;
8695 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008696 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008697 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008698 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008699
James Smart45ed1192009-10-02 15:17:02 -04008700 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008701 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008702 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008703 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008704 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008705 command_type = ELS_COMMAND_FIP;
8706 else
8707 command_type = ELS_COMMAND_NON_FIP;
8708
James Smartb5c53952016-03-31 14:12:30 -07008709 if (phba->fcp_embed_io)
8710 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008711 /* Some of the fields are in the right position already */
8712 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008713 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8714 /* The ct field has moved so reset */
8715 wqe->generic.wqe_com.word7 = 0;
8716 wqe->generic.wqe_com.word10 = 0;
8717 }
James Smartb5c53952016-03-31 14:12:30 -07008718
8719 abort_tag = (uint32_t) iocbq->iotag;
8720 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008721 /* words0-2 bpl convert bde */
8722 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008723 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8724 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008725 bpl = (struct ulp_bde64 *)
8726 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8727 if (!bpl)
8728 return IOCB_ERROR;
8729
8730 /* Should already be byte swapped. */
8731 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8732 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8733 /* swap the size field back to the cpu so we
8734 * can assign it to the sgl.
8735 */
8736 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008737 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8738 total_len = 0;
8739 for (i = 0; i < numBdes; i++) {
8740 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8741 total_len += bde.tus.f.bdeSize;
8742 }
James Smart4f774512009-05-22 14:52:35 -04008743 } else
James Smart5ffc2662009-11-18 15:39:44 -05008744 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008745
8746 iocbq->iocb.ulpIoTag = iocbq->iotag;
8747 cmnd = iocbq->iocb.ulpCommand;
8748
8749 switch (iocbq->iocb.ulpCommand) {
8750 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008751 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8752 ndlp = iocbq->context_un.ndlp;
8753 else
8754 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008755 if (!iocbq->iocb.ulpLe) {
8756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8757 "2007 Only Limited Edition cmd Format"
8758 " supported 0x%x\n",
8759 iocbq->iocb.ulpCommand);
8760 return IOCB_ERROR;
8761 }
James Smartff78d8f2011-12-13 13:21:35 -05008762
James Smart5ffc2662009-11-18 15:39:44 -05008763 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008764 /* Els_reguest64 has a TMO */
8765 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8766 iocbq->iocb.ulpTimeout);
8767 /* Need a VF for word 4 set the vf bit*/
8768 bf_set(els_req64_vf, &wqe->els_req, 0);
8769 /* And a VFID for word 12 */
8770 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008771 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008772 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8773 iocbq->iocb.ulpContext);
8774 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8775 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008776 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008777 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008778 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8779 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008780 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8781 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008782 if_type = bf_get(lpfc_sli_intf_if_type,
8783 &phba->sli4_hba.sli_intf);
8784 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008785 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008786 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008787 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008788 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008789 *pcmd == ELS_CMD_PLOGI)) {
8790 bf_set(els_req64_sp, &wqe->els_req, 1);
8791 bf_set(els_req64_sid, &wqe->els_req,
8792 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008793 if ((*pcmd == ELS_CMD_FLOGI) &&
8794 !(phba->fc_topology ==
8795 LPFC_TOPOLOGY_LOOP))
8796 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008797 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8798 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008799 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008800 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008801 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8802 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8803 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8804 }
James Smartc8685952009-11-18 15:39:16 -05008805 }
James Smart6d368e52011-05-24 11:44:12 -04008806 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8807 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008808 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8809 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8810 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8811 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8812 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8813 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008814 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008815 break;
James Smart5ffc2662009-11-18 15:39:44 -05008816 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008817 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8818 iocbq->iocb.un.ulpWord[3]);
8819 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008820 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008821 /* The entire sequence is transmitted for this IOCB */
8822 xmit_len = total_len;
8823 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008824 if (phba->link_flag & LS_LOOPBACK_MODE)
8825 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008826 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008827 /* word3 iocb=io_tag32 wqe=reserved */
8828 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008829 /* word4 relative_offset memcpy */
8830 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008831 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8832 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8833 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8834 LPFC_WQE_IOD_WRITE);
8835 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8836 LPFC_WQE_LENLOC_WORD12);
8837 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008838 wqe->xmit_sequence.xmit_len = xmit_len;
8839 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008840 break;
James Smart4f774512009-05-22 14:52:35 -04008841 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008842 /* word3 iocb=iotag32 wqe=seq_payload_len */
8843 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008844 /* word4 iocb=rsvd wqe=rsvd */
8845 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8846 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008847 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008848 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008849 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8850 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8851 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8852 LPFC_WQE_LENLOC_WORD3);
8853 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008854 break;
James Smart4f774512009-05-22 14:52:35 -04008855 case CMD_FCP_IWRITE64_CR:
8856 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008857 /* word3 iocb=iotag wqe=payload_offset_len */
8858 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008859 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8860 xmit_len + sizeof(struct fcp_rsp));
8861 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8862 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008863 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8864 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8865 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8866 iocbq->iocb.ulpFCP2Rcvy);
8867 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8868 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008869 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8870 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8871 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008872 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008873 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008874 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8875 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008876 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8877 if (iocbq->priority) {
8878 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8879 (iocbq->priority << 1));
8880 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008881 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8882 (phba->cfg_XLanePriority << 1));
8883 }
8884 }
James Smartb5c53952016-03-31 14:12:30 -07008885 /* Note, word 10 is already initialized to 0 */
8886
8887 if (phba->fcp_embed_io) {
8888 struct lpfc_scsi_buf *lpfc_cmd;
8889 struct sli4_sge *sgl;
8890 union lpfc_wqe128 *wqe128;
8891 struct fcp_cmnd *fcp_cmnd;
8892 uint32_t *ptr;
8893
8894 /* 128 byte wqe support here */
8895 wqe128 = (union lpfc_wqe128 *)wqe;
8896
8897 lpfc_cmd = iocbq->context1;
8898 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8899 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8900
8901 /* Word 0-2 - FCP_CMND */
8902 wqe128->generic.bde.tus.f.bdeFlags =
8903 BUFF_TYPE_BDE_IMMED;
8904 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8905 wqe128->generic.bde.addrHigh = 0;
8906 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8907
8908 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8909
8910 /* Word 22-29 FCP CMND Payload */
8911 ptr = &wqe128->words[22];
8912 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8913 }
James Smart7851fe22011-07-22 18:36:52 -04008914 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008915 case CMD_FCP_IREAD64_CR:
8916 /* word3 iocb=iotag wqe=payload_offset_len */
8917 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008918 bf_set(payload_offset_len, &wqe->fcp_iread,
8919 xmit_len + sizeof(struct fcp_rsp));
8920 bf_set(cmd_buff_len, &wqe->fcp_iread,
8921 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008922 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8923 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8924 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8925 iocbq->iocb.ulpFCP2Rcvy);
8926 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008927 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008928 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8929 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8930 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008931 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008932 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008933 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8934 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008935 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8936 if (iocbq->priority) {
8937 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8938 (iocbq->priority << 1));
8939 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008940 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8941 (phba->cfg_XLanePriority << 1));
8942 }
8943 }
James Smartb5c53952016-03-31 14:12:30 -07008944 /* Note, word 10 is already initialized to 0 */
8945
8946 if (phba->fcp_embed_io) {
8947 struct lpfc_scsi_buf *lpfc_cmd;
8948 struct sli4_sge *sgl;
8949 union lpfc_wqe128 *wqe128;
8950 struct fcp_cmnd *fcp_cmnd;
8951 uint32_t *ptr;
8952
8953 /* 128 byte wqe support here */
8954 wqe128 = (union lpfc_wqe128 *)wqe;
8955
8956 lpfc_cmd = iocbq->context1;
8957 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8958 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8959
8960 /* Word 0-2 - FCP_CMND */
8961 wqe128->generic.bde.tus.f.bdeFlags =
8962 BUFF_TYPE_BDE_IMMED;
8963 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8964 wqe128->generic.bde.addrHigh = 0;
8965 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8966
8967 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8968
8969 /* Word 22-29 FCP CMND Payload */
8970 ptr = &wqe128->words[22];
8971 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8972 }
James Smart7851fe22011-07-22 18:36:52 -04008973 break;
James Smartf1126682009-06-10 17:22:44 -04008974 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008975 /* word3 iocb=iotag wqe=payload_offset_len */
8976 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8977 bf_set(payload_offset_len, &wqe->fcp_icmd,
8978 xmit_len + sizeof(struct fcp_rsp));
8979 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8980 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008981 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008982 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008983 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008984 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8985 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8986 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8987 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8988 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008989 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8990 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008991 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8992 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008993 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8994 if (iocbq->priority) {
8995 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8996 (iocbq->priority << 1));
8997 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008998 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8999 (phba->cfg_XLanePriority << 1));
9000 }
9001 }
James Smartb5c53952016-03-31 14:12:30 -07009002 /* Note, word 10 is already initialized to 0 */
9003
9004 if (phba->fcp_embed_io) {
9005 struct lpfc_scsi_buf *lpfc_cmd;
9006 struct sli4_sge *sgl;
9007 union lpfc_wqe128 *wqe128;
9008 struct fcp_cmnd *fcp_cmnd;
9009 uint32_t *ptr;
9010
9011 /* 128 byte wqe support here */
9012 wqe128 = (union lpfc_wqe128 *)wqe;
9013
9014 lpfc_cmd = iocbq->context1;
9015 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9016 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9017
9018 /* Word 0-2 - FCP_CMND */
9019 wqe128->generic.bde.tus.f.bdeFlags =
9020 BUFF_TYPE_BDE_IMMED;
9021 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9022 wqe128->generic.bde.addrHigh = 0;
9023 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9024
9025 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9026
9027 /* Word 22-29 FCP CMND Payload */
9028 ptr = &wqe128->words[22];
9029 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9030 }
James Smart7851fe22011-07-22 18:36:52 -04009031 break;
James Smart4f774512009-05-22 14:52:35 -04009032 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009033 /* For this command calculate the xmit length of the
9034 * request bde.
9035 */
9036 xmit_len = 0;
9037 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9038 sizeof(struct ulp_bde64);
9039 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009040 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009041 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9042 break;
James Smart63e801c2010-11-20 23:14:19 -05009043 xmit_len += bde.tus.f.bdeSize;
9044 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009045 /* word3 iocb=IO_TAG wqe=request_payload_len */
9046 wqe->gen_req.request_payload_len = xmit_len;
9047 /* word4 iocb=parameter wqe=relative_offset memcpy */
9048 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009049 /* word6 context tag copied in memcpy */
9050 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9051 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9053 "2015 Invalid CT %x command 0x%x\n",
9054 ct, iocbq->iocb.ulpCommand);
9055 return IOCB_ERROR;
9056 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009057 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9058 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9059 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9060 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9061 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9062 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9063 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9064 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009065 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009066 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009067 break;
James Smart4f774512009-05-22 14:52:35 -04009068 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009069 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009070 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009071 /* word3 iocb=iotag32 wqe=response_payload_len */
9072 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009073 /* word4 */
9074 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009075 /* word5 iocb=rsvd wge=did */
9076 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009077 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9078
9079 if_type = bf_get(lpfc_sli_intf_if_type,
9080 &phba->sli4_hba.sli_intf);
9081 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9082 if (iocbq->vport->fc_flag & FC_PT2PT) {
9083 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9084 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9085 iocbq->vport->fc_myDID);
9086 if (iocbq->vport->fc_myDID == Fabric_DID) {
9087 bf_set(wqe_els_did,
9088 &wqe->xmit_els_rsp.wqe_dest, 0);
9089 }
9090 }
9091 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009092 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9093 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9094 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9095 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009096 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009097 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009098 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009099 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009100 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9101 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9102 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9103 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9104 LPFC_WQE_LENLOC_WORD3);
9105 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009106 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9107 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009108 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9109 iocbq->context2)->virt);
9110 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009111 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9112 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009113 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009114 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9115 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009116 phba->vpi_ids[phba->pport->vpi]);
9117 }
James Smart4f774512009-05-22 14:52:35 -04009118 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009119 break;
James Smart4f774512009-05-22 14:52:35 -04009120 case CMD_CLOSE_XRI_CN:
9121 case CMD_ABORT_XRI_CN:
9122 case CMD_ABORT_XRI_CX:
9123 /* words 0-2 memcpy should be 0 rserved */
9124 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009125 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9126 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9127 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9128 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9129 } else
9130 fip = 0;
9131
9132 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009133 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009134 * The link is down, or the command was ELS_FIP
9135 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009136 * on the wire.
9137 */
9138 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9139 else
9140 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9141 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009142 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9143 wqe->abort_cmd.rsrvd5 = 0;
9144 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009145 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9146 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009147 /*
9148 * The abort handler will send us CMD_ABORT_XRI_CN or
9149 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9150 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009151 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9152 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9153 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9154 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009155 cmnd = CMD_ABORT_XRI_CX;
9156 command_type = OTHER_COMMAND;
9157 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009158 break;
James Smart6669f9b2009-10-02 15:16:45 -04009159 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009160 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009161 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009162 * we re-construct this WQE here based on information in
9163 * iocbq from scratch.
9164 */
9165 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009166 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009167 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009168 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9169 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009170 LPFC_ABTS_UNSOL_INT) {
9171 /* ABTS sent by initiator to CT exchange, the
9172 * RX_ID field will be filled with the newly
9173 * allocated responder XRI.
9174 */
9175 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9176 iocbq->sli4_xritag);
9177 } else {
9178 /* ABTS sent by responder to CT exchange, the
9179 * RX_ID field will be filled with the responder
9180 * RX_ID from ABTS.
9181 */
9182 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009183 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009184 }
James Smart6669f9b2009-10-02 15:16:45 -04009185 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9186 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009187
9188 /* Use CT=VPI */
9189 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9190 ndlp->nlp_DID);
9191 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9192 iocbq->iocb.ulpContext);
9193 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009194 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009195 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009196 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9197 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9198 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009199 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9200 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009201 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9202 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9203 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9204 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9205 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9206 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9207 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9208 }
9209
James Smart7851fe22011-07-22 18:36:52 -04009210 break;
James Smartae9e28f2017-05-15 15:20:51 -07009211 case CMD_SEND_FRAME:
9212 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9213 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9214 return 0;
James Smart4f774512009-05-22 14:52:35 -04009215 case CMD_XRI_ABORTED_CX:
9216 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009217 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9218 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9219 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9220 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9221 default:
9222 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9223 "2014 Invalid command 0x%x\n",
9224 iocbq->iocb.ulpCommand);
9225 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009226 break;
James Smart4f774512009-05-22 14:52:35 -04009227 }
James Smart6d368e52011-05-24 11:44:12 -04009228
James Smart8012cc32012-10-31 14:44:49 -04009229 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9230 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9231 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9232 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9233 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9234 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9235 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9236 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009237 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9238 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9239 wqe->generic.wqe_com.abort_tag = abort_tag;
9240 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9241 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9242 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9243 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009244 return 0;
9245}
9246
9247/**
9248 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9249 * @phba: Pointer to HBA context object.
9250 * @ring_number: SLI ring number to issue iocb on.
9251 * @piocb: Pointer to command iocb.
9252 * @flag: Flag indicating if this command can be put into txq.
9253 *
9254 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9255 * an iocb command to an HBA with SLI-4 interface spec.
9256 *
9257 * This function is called with hbalock held. The function will return success
9258 * after it successfully submit the iocb to firmware or after adding to the
9259 * txq.
9260 **/
9261static int
9262__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9263 struct lpfc_iocbq *piocb, uint32_t flag)
9264{
9265 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009266 union lpfc_wqe *wqe;
9267 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009268 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009269 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009270
James Smart895427b2017-02-12 13:52:30 -08009271 /* Get the WQ */
9272 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9273 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9274 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9275 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9276 else
9277 wq = phba->sli4_hba.oas_wq;
9278 } else {
9279 wq = phba->sli4_hba.els_wq;
9280 }
9281
9282 /* Get corresponding ring */
9283 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009284
James Smartb5c53952016-03-31 14:12:30 -07009285 /*
9286 * The WQE can be either 64 or 128 bytes,
9287 * so allocate space on the stack assuming the largest.
9288 */
9289 wqe = (union lpfc_wqe *)&wqe128;
9290
James Smart895427b2017-02-12 13:52:30 -08009291 lockdep_assert_held(&phba->hbalock);
9292
James Smart4f774512009-05-22 14:52:35 -04009293 if (piocb->sli4_xritag == NO_XRI) {
9294 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009295 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009296 sglq = NULL;
9297 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009298 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009299 if (!(flag & SLI_IOCB_RET_IOCB)) {
9300 __lpfc_sli_ringtx_put(phba,
9301 pring, piocb);
9302 return IOCB_SUCCESS;
9303 } else {
9304 return IOCB_BUSY;
9305 }
9306 } else {
James Smart895427b2017-02-12 13:52:30 -08009307 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009308 if (!sglq) {
9309 if (!(flag & SLI_IOCB_RET_IOCB)) {
9310 __lpfc_sli_ringtx_put(phba,
9311 pring,
9312 piocb);
9313 return IOCB_SUCCESS;
9314 } else
9315 return IOCB_BUSY;
9316 }
9317 }
James Smart4f774512009-05-22 14:52:35 -04009318 }
James Smart2ea259e2017-02-12 13:52:27 -08009319 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009320 /* These IO's already have an XRI and a mapped sgl. */
9321 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009322 else {
James Smart6d368e52011-05-24 11:44:12 -04009323 /*
9324 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009325 * sglq is on the active list
9326 */
James Smartedccdc12013-01-03 15:43:45 -05009327 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009328 if (!sglq)
9329 return IOCB_ERROR;
9330 }
9331
9332 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009333 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009334 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009335 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009336 return IOCB_ERROR;
9337 }
9338
James Smartb5c53952016-03-31 14:12:30 -07009339 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009340 return IOCB_ERROR;
9341
James Smart895427b2017-02-12 13:52:30 -08009342 if (lpfc_sli4_wq_put(wq, wqe))
9343 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009344 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9345
9346 return 0;
9347}
9348
9349/**
James Smart3772a992009-05-22 14:50:54 -04009350 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9351 *
9352 * This routine wraps the actual lockless version for issusing IOCB function
9353 * pointer from the lpfc_hba struct.
9354 *
9355 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009356 * IOCB_ERROR - Error
9357 * IOCB_SUCCESS - Success
9358 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009359 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009360int
James Smart3772a992009-05-22 14:50:54 -04009361__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9362 struct lpfc_iocbq *piocb, uint32_t flag)
9363{
9364 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9365}
9366
9367/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009368 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009369 * @phba: The hba struct for which this call is being executed.
9370 * @dev_grp: The HBA PCI-Device group number.
9371 *
9372 * This routine sets up the SLI interface API function jump table in @phba
9373 * struct.
9374 * Returns: 0 - success, -ENODEV - failure.
9375 **/
9376int
9377lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9378{
9379
9380 switch (dev_grp) {
9381 case LPFC_PCI_DEV_LP:
9382 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9383 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9384 break;
James Smart4f774512009-05-22 14:52:35 -04009385 case LPFC_PCI_DEV_OC:
9386 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9387 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9388 break;
James Smart3772a992009-05-22 14:50:54 -04009389 default:
9390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9391 "1419 Invalid HBA PCI-device group: 0x%x\n",
9392 dev_grp);
9393 return -ENODEV;
9394 break;
9395 }
9396 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9397 return 0;
9398}
James Smart92d7f7b2007-06-17 19:56:38 -05009399
James Smarta1efe162015-05-21 13:55:20 -04009400/**
James Smart895427b2017-02-12 13:52:30 -08009401 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009402 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009403 * @piocb: Pointer to command iocb.
9404 *
James Smart895427b2017-02-12 13:52:30 -08009405 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9406 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009407 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009408 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009409 */
James Smart895427b2017-02-12 13:52:30 -08009410struct lpfc_sli_ring *
9411lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009412{
James Smart895427b2017-02-12 13:52:30 -08009413 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009414 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009415 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009416 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009417 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009418 /*
James Smart895427b2017-02-12 13:52:30 -08009419 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009420 * be setup based on what work queue we used.
9421 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009422 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009423 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009424 lpfc_sli4_scmd_to_wqidx_distr(phba,
9425 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009426 piocb->hba_wqidx = piocb->hba_wqidx %
9427 phba->cfg_fcp_io_channel;
9428 }
James Smart895427b2017-02-12 13:52:30 -08009429 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009430 } else {
9431 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009432 return NULL;
9433 piocb->hba_wqidx = 0;
9434 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009435 }
James Smart895427b2017-02-12 13:52:30 -08009436 } else {
9437 if (unlikely(!phba->sli4_hba.els_wq))
9438 return NULL;
9439 piocb->hba_wqidx = 0;
9440 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009441 }
James Smart9bd2bff52014-09-03 12:57:30 -04009442}
9443
James Smarte59058c2008-08-24 21:49:00 -04009444/**
James Smart3621a712009-04-06 18:47:14 -04009445 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009446 * @phba: Pointer to HBA context object.
9447 * @pring: Pointer to driver SLI ring object.
9448 * @piocb: Pointer to command iocb.
9449 * @flag: Flag indicating if this command can be put into txq.
9450 *
9451 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9452 * function. This function gets the hbalock and calls
9453 * __lpfc_sli_issue_iocb function and will return the error returned
9454 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9455 * functions which do not hold hbalock.
9456 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009457int
James Smart3772a992009-05-22 14:50:54 -04009458lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009459 struct lpfc_iocbq *piocb, uint32_t flag)
9460{
James Smart895427b2017-02-12 13:52:30 -08009461 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009462 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009463 struct lpfc_queue *fpeq;
9464 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009465 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009466 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009467
James Smart7e56aa22012-08-03 12:35:34 -04009468 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009469 pring = lpfc_sli4_calc_ring(phba, piocb);
9470 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009471 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009472
James Smart9bd2bff52014-09-03 12:57:30 -04009473 spin_lock_irqsave(&pring->ring_lock, iflags);
9474 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9475 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009476
James Smart9bd2bff52014-09-03 12:57:30 -04009477 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009478 idx = piocb->hba_wqidx;
9479 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009480
James Smart895427b2017-02-12 13:52:30 -08009481 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009482
James Smart9bd2bff52014-09-03 12:57:30 -04009483 /* Get associated EQ with this index */
9484 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009485
James Smart9bd2bff52014-09-03 12:57:30 -04009486 /* Turn off interrupts from this EQ */
9487 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009488
James Smart9bd2bff52014-09-03 12:57:30 -04009489 /*
9490 * Process all the events on FCP EQ
9491 */
9492 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9493 lpfc_sli4_hba_handle_eqe(phba,
9494 eqe, idx);
9495 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009496 }
James Smartba20c852012-08-03 12:36:52 -04009497
James Smart9bd2bff52014-09-03 12:57:30 -04009498 /* Always clear and re-arm the EQ */
9499 lpfc_sli4_eq_release(fpeq,
9500 LPFC_QUEUE_REARM);
9501 }
James Smart895427b2017-02-12 13:52:30 -08009502 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009503 }
James Smart7e56aa22012-08-03 12:35:34 -04009504 } else {
9505 /* For now, SLI2/3 will still use hbalock */
9506 spin_lock_irqsave(&phba->hbalock, iflags);
9507 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9508 spin_unlock_irqrestore(&phba->hbalock, iflags);
9509 }
James Smart92d7f7b2007-06-17 19:56:38 -05009510 return rc;
9511}
9512
James Smarte59058c2008-08-24 21:49:00 -04009513/**
James Smart3621a712009-04-06 18:47:14 -04009514 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009515 * @phba: Pointer to HBA context object.
9516 *
9517 * This function is called while driver attaches with the
9518 * HBA to setup the extra ring. The extra ring is used
9519 * only when driver needs to support target mode functionality
9520 * or IP over FC functionalities.
9521 *
James Smart895427b2017-02-12 13:52:30 -08009522 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009523 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009524static int
9525lpfc_extra_ring_setup( struct lpfc_hba *phba)
9526{
9527 struct lpfc_sli *psli;
9528 struct lpfc_sli_ring *pring;
9529
9530 psli = &phba->sli;
9531
9532 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009533
9534 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009535 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009536 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9537 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9538 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9539 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009540
James Smarta4bc3372006-12-02 13:34:16 -05009541 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009542 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009543
James Smart7e56aa22012-08-03 12:35:34 -04009544 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9545 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9546 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9547 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009548
9549 /* Setup default profile for this ring */
9550 pring->iotag_max = 4096;
9551 pring->num_mask = 1;
9552 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009553 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9554 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009555 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9556 return 0;
9557}
9558
James Smartcb69f7d2011-12-13 13:21:57 -05009559/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9560 * @phba: Pointer to HBA context object.
9561 * @iocbq: Pointer to iocb object.
9562 *
9563 * The async_event handler calls this routine when it receives
9564 * an ASYNC_STATUS_CN event from the port. The port generates
9565 * this event when an Abort Sequence request to an rport fails
9566 * twice in succession. The abort could be originated by the
9567 * driver or by the port. The ABTS could have been for an ELS
9568 * or FCP IO. The port only generates this event when an ABTS
9569 * fails to complete after one retry.
9570 */
9571static void
9572lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9573 struct lpfc_iocbq *iocbq)
9574{
9575 struct lpfc_nodelist *ndlp = NULL;
9576 uint16_t rpi = 0, vpi = 0;
9577 struct lpfc_vport *vport = NULL;
9578
9579 /* The rpi in the ulpContext is vport-sensitive. */
9580 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9581 rpi = iocbq->iocb.ulpContext;
9582
9583 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9584 "3092 Port generated ABTS async event "
9585 "on vpi %d rpi %d status 0x%x\n",
9586 vpi, rpi, iocbq->iocb.ulpStatus);
9587
9588 vport = lpfc_find_vport_by_vpid(phba, vpi);
9589 if (!vport)
9590 goto err_exit;
9591 ndlp = lpfc_findnode_rpi(vport, rpi);
9592 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9593 goto err_exit;
9594
9595 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9596 lpfc_sli_abts_recover_port(vport, ndlp);
9597 return;
9598
9599 err_exit:
9600 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9601 "3095 Event Context not found, no "
9602 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9603 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9604 vpi, rpi);
9605}
9606
9607/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9608 * @phba: pointer to HBA context object.
9609 * @ndlp: nodelist pointer for the impacted rport.
9610 * @axri: pointer to the wcqe containing the failed exchange.
9611 *
9612 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9613 * port. The port generates this event when an abort exchange request to an
9614 * rport fails twice in succession with no reply. The abort could be originated
9615 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9616 */
9617void
9618lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9619 struct lpfc_nodelist *ndlp,
9620 struct sli4_wcqe_xri_aborted *axri)
9621{
9622 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009623 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009624
James Smart6b5151f2012-01-18 16:24:06 -05009625 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9627 "3115 Node Context not found, driver "
9628 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009629 return;
9630 }
9631
James Smartcb69f7d2011-12-13 13:21:57 -05009632 vport = ndlp->vport;
9633 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9634 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009635 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009636 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009637 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009638 bf_get(lpfc_wcqe_xa_status, axri),
9639 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009640
James Smart5c1db2a2012-03-01 22:34:36 -05009641 /*
9642 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9643 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9644 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9645 */
James Smarte3d2b802012-08-14 14:25:43 -04009646 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009647 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9648 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009649 lpfc_sli_abts_recover_port(vport, ndlp);
9650}
9651
James Smarte59058c2008-08-24 21:49:00 -04009652/**
James Smart3621a712009-04-06 18:47:14 -04009653 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009654 * @phba: Pointer to HBA context object.
9655 * @pring: Pointer to driver SLI ring object.
9656 * @iocbq: Pointer to iocb object.
9657 *
9658 * This function is called by the slow ring event handler
9659 * function when there is an ASYNC event iocb in the ring.
9660 * This function is called with no lock held.
9661 * Currently this function handles only temperature related
9662 * ASYNC events. The function decodes the temperature sensor
9663 * event message and posts events for the management applications.
9664 **/
James Smart98c9ea52007-10-27 13:37:33 -04009665static void
James Smart57127f12007-10-27 13:37:05 -04009666lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9667 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9668{
9669 IOCB_t *icmd;
9670 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009671 struct temp_event temp_event_data;
9672 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009673 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009674
9675 icmd = &iocbq->iocb;
9676 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009677
James Smartcb69f7d2011-12-13 13:21:57 -05009678 switch (evt_code) {
9679 case ASYNC_TEMP_WARN:
9680 case ASYNC_TEMP_SAFE:
9681 temp_event_data.data = (uint32_t) icmd->ulpContext;
9682 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9683 if (evt_code == ASYNC_TEMP_WARN) {
9684 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9685 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9686 "0347 Adapter is very hot, please take "
9687 "corrective action. temperature : %d Celsius\n",
9688 (uint32_t) icmd->ulpContext);
9689 } else {
9690 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9691 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9692 "0340 Adapter temperature is OK now. "
9693 "temperature : %d Celsius\n",
9694 (uint32_t) icmd->ulpContext);
9695 }
9696
9697 /* Send temperature change event to applications */
9698 shost = lpfc_shost_from_vport(phba->pport);
9699 fc_host_post_vendor_event(shost, fc_get_event_number(),
9700 sizeof(temp_event_data), (char *) &temp_event_data,
9701 LPFC_NL_VENDOR_ID);
9702 break;
9703 case ASYNC_STATUS_CN:
9704 lpfc_sli_abts_err_handler(phba, iocbq);
9705 break;
9706 default:
James Smarta257bf92009-04-06 18:48:10 -04009707 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009709 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009710 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009711 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9712 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9713 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9714 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009715 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009716 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9717 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9718 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9719 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9720
James Smartcb69f7d2011-12-13 13:21:57 -05009721 break;
James Smart57127f12007-10-27 13:37:05 -04009722 }
James Smart57127f12007-10-27 13:37:05 -04009723}
9724
9725
James Smarte59058c2008-08-24 21:49:00 -04009726/**
James Smart895427b2017-02-12 13:52:30 -08009727 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009728 * @phba: Pointer to HBA context object.
9729 *
9730 * lpfc_sli_setup sets up rings of the SLI interface with
9731 * number of iocbs per ring and iotags. This function is
9732 * called while driver attach to the HBA and before the
9733 * interrupts are enabled. So there is no need for locking.
9734 *
9735 * This function always returns 0.
9736 **/
dea31012005-04-17 16:05:31 -05009737int
James Smart895427b2017-02-12 13:52:30 -08009738lpfc_sli4_setup(struct lpfc_hba *phba)
9739{
9740 struct lpfc_sli_ring *pring;
9741
9742 pring = phba->sli4_hba.els_wq->pring;
9743 pring->num_mask = LPFC_MAX_RING_MASK;
9744 pring->prt[0].profile = 0; /* Mask 0 */
9745 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9746 pring->prt[0].type = FC_TYPE_ELS;
9747 pring->prt[0].lpfc_sli_rcv_unsol_event =
9748 lpfc_els_unsol_event;
9749 pring->prt[1].profile = 0; /* Mask 1 */
9750 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9751 pring->prt[1].type = FC_TYPE_ELS;
9752 pring->prt[1].lpfc_sli_rcv_unsol_event =
9753 lpfc_els_unsol_event;
9754 pring->prt[2].profile = 0; /* Mask 2 */
9755 /* NameServer Inquiry */
9756 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9757 /* NameServer */
9758 pring->prt[2].type = FC_TYPE_CT;
9759 pring->prt[2].lpfc_sli_rcv_unsol_event =
9760 lpfc_ct_unsol_event;
9761 pring->prt[3].profile = 0; /* Mask 3 */
9762 /* NameServer response */
9763 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9764 /* NameServer */
9765 pring->prt[3].type = FC_TYPE_CT;
9766 pring->prt[3].lpfc_sli_rcv_unsol_event =
9767 lpfc_ct_unsol_event;
9768 return 0;
9769}
9770
9771/**
9772 * lpfc_sli_setup - SLI ring setup function
9773 * @phba: Pointer to HBA context object.
9774 *
9775 * lpfc_sli_setup sets up rings of the SLI interface with
9776 * number of iocbs per ring and iotags. This function is
9777 * called while driver attach to the HBA and before the
9778 * interrupts are enabled. So there is no need for locking.
9779 *
9780 * This function always returns 0. SLI3 only.
9781 **/
9782int
dea31012005-04-17 16:05:31 -05009783lpfc_sli_setup(struct lpfc_hba *phba)
9784{
James Smarted957682007-06-17 19:56:37 -05009785 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009786 struct lpfc_sli *psli = &phba->sli;
9787 struct lpfc_sli_ring *pring;
9788
James Smart2a76a282012-08-03 12:35:54 -04009789 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009790 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009791
James Bottomley604a3e32005-10-29 10:28:33 -05009792 psli->iocbq_lookup = NULL;
9793 psli->iocbq_lookup_len = 0;
9794 psli->last_iotag = 0;
9795
dea31012005-04-17 16:05:31 -05009796 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009797 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009798 switch (i) {
9799 case LPFC_FCP_RING: /* ring 0 - FCP */
9800 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009801 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9802 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9803 pring->sli.sli3.numCiocb +=
9804 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9805 pring->sli.sli3.numRiocb +=
9806 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9807 pring->sli.sli3.numCiocb +=
9808 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9809 pring->sli.sli3.numRiocb +=
9810 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9811 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009812 SLI3_IOCB_CMD_SIZE :
9813 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009814 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009815 SLI3_IOCB_RSP_SIZE :
9816 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009817 pring->iotag_ctr = 0;
9818 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009819 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009820 pring->fast_iotag = pring->iotag_max;
9821 pring->num_mask = 0;
9822 break;
James Smarta4bc3372006-12-02 13:34:16 -05009823 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009824 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009825 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9826 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9827 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009828 SLI3_IOCB_CMD_SIZE :
9829 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009830 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009831 SLI3_IOCB_RSP_SIZE :
9832 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009833 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009834 pring->num_mask = 0;
9835 break;
9836 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9837 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009838 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9839 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9840 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009841 SLI3_IOCB_CMD_SIZE :
9842 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009843 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009844 SLI3_IOCB_RSP_SIZE :
9845 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009846 pring->fast_iotag = 0;
9847 pring->iotag_ctr = 0;
9848 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009849 pring->lpfc_sli_rcv_async_status =
9850 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009851 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009852 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009853 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9854 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009855 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009856 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009857 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009858 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9859 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009860 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009861 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009862 pring->prt[2].profile = 0; /* Mask 2 */
9863 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009864 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009865 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009866 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009867 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009868 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009869 pring->prt[3].profile = 0; /* Mask 3 */
9870 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009871 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009872 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009873 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009874 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009875 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009876 break;
9877 }
James Smart7e56aa22012-08-03 12:35:34 -04009878 totiocbsize += (pring->sli.sli3.numCiocb *
9879 pring->sli.sli3.sizeCiocb) +
9880 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009881 }
James Smarted957682007-06-17 19:56:37 -05009882 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009883 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009884 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9885 "SLI2 SLIM Data: x%x x%lx\n",
9886 phba->brd_no, totiocbsize,
9887 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009888 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009889 if (phba->cfg_multi_ring_support == 2)
9890 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009891
9892 return 0;
9893}
9894
James Smarte59058c2008-08-24 21:49:00 -04009895/**
James Smart895427b2017-02-12 13:52:30 -08009896 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009897 * @phba: Pointer to HBA context object.
9898 *
James Smart895427b2017-02-12 13:52:30 -08009899 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009900 * ring. This function also initializes ring indices of each ring.
9901 * This function is called during the initialization of the SLI
9902 * interface of an HBA.
9903 * This function is called with no lock held and always returns
9904 * 1.
9905 **/
James Smart895427b2017-02-12 13:52:30 -08009906void
9907lpfc_sli4_queue_init(struct lpfc_hba *phba)
9908{
9909 struct lpfc_sli *psli;
9910 struct lpfc_sli_ring *pring;
9911 int i;
9912
9913 psli = &phba->sli;
9914 spin_lock_irq(&phba->hbalock);
9915 INIT_LIST_HEAD(&psli->mboxq);
9916 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9917 /* Initialize list headers for txq and txcmplq as double linked lists */
9918 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9919 pring = phba->sli4_hba.fcp_wq[i]->pring;
9920 pring->flag = 0;
9921 pring->ringno = LPFC_FCP_RING;
9922 INIT_LIST_HEAD(&pring->txq);
9923 INIT_LIST_HEAD(&pring->txcmplq);
9924 INIT_LIST_HEAD(&pring->iocb_continueq);
9925 spin_lock_init(&pring->ring_lock);
9926 }
9927 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9928 pring = phba->sli4_hba.nvme_wq[i]->pring;
9929 pring->flag = 0;
9930 pring->ringno = LPFC_FCP_RING;
9931 INIT_LIST_HEAD(&pring->txq);
9932 INIT_LIST_HEAD(&pring->txcmplq);
9933 INIT_LIST_HEAD(&pring->iocb_continueq);
9934 spin_lock_init(&pring->ring_lock);
9935 }
9936 pring = phba->sli4_hba.els_wq->pring;
9937 pring->flag = 0;
9938 pring->ringno = LPFC_ELS_RING;
9939 INIT_LIST_HEAD(&pring->txq);
9940 INIT_LIST_HEAD(&pring->txcmplq);
9941 INIT_LIST_HEAD(&pring->iocb_continueq);
9942 spin_lock_init(&pring->ring_lock);
9943
9944 if (phba->cfg_nvme_io_channel) {
9945 pring = phba->sli4_hba.nvmels_wq->pring;
9946 pring->flag = 0;
9947 pring->ringno = LPFC_ELS_RING;
9948 INIT_LIST_HEAD(&pring->txq);
9949 INIT_LIST_HEAD(&pring->txcmplq);
9950 INIT_LIST_HEAD(&pring->iocb_continueq);
9951 spin_lock_init(&pring->ring_lock);
9952 }
9953
9954 if (phba->cfg_fof) {
9955 pring = phba->sli4_hba.oas_wq->pring;
9956 pring->flag = 0;
9957 pring->ringno = LPFC_FCP_RING;
9958 INIT_LIST_HEAD(&pring->txq);
9959 INIT_LIST_HEAD(&pring->txcmplq);
9960 INIT_LIST_HEAD(&pring->iocb_continueq);
9961 spin_lock_init(&pring->ring_lock);
9962 }
9963
9964 spin_unlock_irq(&phba->hbalock);
9965}
9966
9967/**
9968 * lpfc_sli_queue_init - Queue initialization function
9969 * @phba: Pointer to HBA context object.
9970 *
9971 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9972 * ring. This function also initializes ring indices of each ring.
9973 * This function is called during the initialization of the SLI
9974 * interface of an HBA.
9975 * This function is called with no lock held and always returns
9976 * 1.
9977 **/
9978void
9979lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009980{
9981 struct lpfc_sli *psli;
9982 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009983 int i;
dea31012005-04-17 16:05:31 -05009984
9985 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009986 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009987 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009988 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009989 /* Initialize list headers for txq and txcmplq as double linked lists */
9990 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009991 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009992 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009993 pring->sli.sli3.next_cmdidx = 0;
9994 pring->sli.sli3.local_getidx = 0;
9995 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009996 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009997 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009998 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009999 pring->flag = 0;
10000 INIT_LIST_HEAD(&pring->txq);
10001 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010002 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010003 }
James Smart2e0fef82007-06-17 19:56:36 -050010004 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010005}
10006
James Smarte59058c2008-08-24 21:49:00 -040010007/**
James Smart04c68492009-05-22 14:52:52 -040010008 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10009 * @phba: Pointer to HBA context object.
10010 *
10011 * This routine flushes the mailbox command subsystem. It will unconditionally
10012 * flush all the mailbox commands in the three possible stages in the mailbox
10013 * command sub-system: pending mailbox command queue; the outstanding mailbox
10014 * command; and completed mailbox command queue. It is caller's responsibility
10015 * to make sure that the driver is in the proper state to flush the mailbox
10016 * command sub-system. Namely, the posting of mailbox commands into the
10017 * pending mailbox command queue from the various clients must be stopped;
10018 * either the HBA is in a state that it will never works on the outstanding
10019 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10020 * mailbox command has been completed.
10021 **/
10022static void
10023lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10024{
10025 LIST_HEAD(completions);
10026 struct lpfc_sli *psli = &phba->sli;
10027 LPFC_MBOXQ_t *pmb;
10028 unsigned long iflag;
10029
10030 /* Flush all the mailbox commands in the mbox system */
10031 spin_lock_irqsave(&phba->hbalock, iflag);
10032 /* The pending mailbox command queue */
10033 list_splice_init(&phba->sli.mboxq, &completions);
10034 /* The outstanding active mailbox command */
10035 if (psli->mbox_active) {
10036 list_add_tail(&psli->mbox_active->list, &completions);
10037 psli->mbox_active = NULL;
10038 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10039 }
10040 /* The completed mailbox command queue */
10041 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10042 spin_unlock_irqrestore(&phba->hbalock, iflag);
10043
10044 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10045 while (!list_empty(&completions)) {
10046 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10047 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10048 if (pmb->mbox_cmpl)
10049 pmb->mbox_cmpl(phba, pmb);
10050 }
10051}
10052
10053/**
James Smart3621a712009-04-06 18:47:14 -040010054 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010055 * @vport: Pointer to virtual port object.
10056 *
10057 * lpfc_sli_host_down is called to clean up the resources
10058 * associated with a vport before destroying virtual
10059 * port data structures.
10060 * This function does following operations:
10061 * - Free discovery resources associated with this virtual
10062 * port.
10063 * - Free iocbs associated with this virtual port in
10064 * the txq.
10065 * - Send abort for all iocb commands associated with this
10066 * vport in txcmplq.
10067 *
10068 * This function is called with no lock held and always returns 1.
10069 **/
dea31012005-04-17 16:05:31 -050010070int
James Smart92d7f7b2007-06-17 19:56:38 -050010071lpfc_sli_host_down(struct lpfc_vport *vport)
10072{
James Smart858c9f62007-06-17 19:56:39 -050010073 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010074 struct lpfc_hba *phba = vport->phba;
10075 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010076 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010077 struct lpfc_sli_ring *pring;
10078 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010079 int i;
10080 unsigned long flags = 0;
10081 uint16_t prev_pring_flag;
10082
10083 lpfc_cleanup_discovery_resources(vport);
10084
10085 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010086
James Smart895427b2017-02-12 13:52:30 -080010087 /*
10088 * Error everything on the txq since these iocbs
10089 * have not been given to the FW yet.
10090 * Also issue ABTS for everything on the txcmplq
10091 */
10092 if (phba->sli_rev != LPFC_SLI_REV4) {
10093 for (i = 0; i < psli->num_rings; i++) {
10094 pring = &psli->sli3_ring[i];
10095 prev_pring_flag = pring->flag;
10096 /* Only slow rings */
10097 if (pring->ringno == LPFC_ELS_RING) {
10098 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10099 /* Set the lpfc data pending flag */
10100 set_bit(LPFC_DATA_READY, &phba->data_flags);
10101 }
10102 list_for_each_entry_safe(iocb, next_iocb,
10103 &pring->txq, list) {
10104 if (iocb->vport != vport)
10105 continue;
10106 list_move_tail(&iocb->list, &completions);
10107 }
10108 list_for_each_entry_safe(iocb, next_iocb,
10109 &pring->txcmplq, list) {
10110 if (iocb->vport != vport)
10111 continue;
10112 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10113 }
10114 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010115 }
James Smart895427b2017-02-12 13:52:30 -080010116 } else {
10117 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10118 pring = qp->pring;
10119 if (!pring)
10120 continue;
10121 if (pring == phba->sli4_hba.els_wq->pring) {
10122 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10123 /* Set the lpfc data pending flag */
10124 set_bit(LPFC_DATA_READY, &phba->data_flags);
10125 }
10126 prev_pring_flag = pring->flag;
10127 spin_lock_irq(&pring->ring_lock);
10128 list_for_each_entry_safe(iocb, next_iocb,
10129 &pring->txq, list) {
10130 if (iocb->vport != vport)
10131 continue;
10132 list_move_tail(&iocb->list, &completions);
10133 }
10134 spin_unlock_irq(&pring->ring_lock);
10135 list_for_each_entry_safe(iocb, next_iocb,
10136 &pring->txcmplq, list) {
10137 if (iocb->vport != vport)
10138 continue;
10139 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10140 }
10141 pring->flag = prev_pring_flag;
10142 }
James Smart92d7f7b2007-06-17 19:56:38 -050010143 }
James Smart92d7f7b2007-06-17 19:56:38 -050010144 spin_unlock_irqrestore(&phba->hbalock, flags);
10145
James Smarta257bf92009-04-06 18:48:10 -040010146 /* Cancel all the IOCBs from the completions list */
10147 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10148 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010149 return 1;
10150}
10151
James Smarte59058c2008-08-24 21:49:00 -040010152/**
James Smart3621a712009-04-06 18:47:14 -040010153 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010154 * @phba: Pointer to HBA context object.
10155 *
10156 * This function cleans up all iocb, buffers, mailbox commands
10157 * while shutting down the HBA. This function is called with no
10158 * lock held and always returns 1.
10159 * This function does the following to cleanup driver resources:
10160 * - Free discovery resources for each virtual port
10161 * - Cleanup any pending fabric iocbs
10162 * - Iterate through the iocb txq and free each entry
10163 * in the list.
10164 * - Free up any buffer posted to the HBA
10165 * - Free mailbox commands in the mailbox queue.
10166 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010167int
James Smart2e0fef82007-06-17 19:56:36 -050010168lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010169{
James Smart2534ba72007-04-25 09:52:20 -040010170 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010171 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010172 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010173 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010174 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010175 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010176 int i;
10177
10178 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010179 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010180
dea31012005-04-17 16:05:31 -050010181 lpfc_hba_down_prep(phba);
10182
James Smart92d7f7b2007-06-17 19:56:38 -050010183 lpfc_fabric_abort_hba(phba);
10184
James Smart2e0fef82007-06-17 19:56:36 -050010185 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010186
James Smart895427b2017-02-12 13:52:30 -080010187 /*
10188 * Error everything on the txq since these iocbs
10189 * have not been given to the FW yet.
10190 */
10191 if (phba->sli_rev != LPFC_SLI_REV4) {
10192 for (i = 0; i < psli->num_rings; i++) {
10193 pring = &psli->sli3_ring[i];
10194 /* Only slow rings */
10195 if (pring->ringno == LPFC_ELS_RING) {
10196 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10197 /* Set the lpfc data pending flag */
10198 set_bit(LPFC_DATA_READY, &phba->data_flags);
10199 }
10200 list_splice_init(&pring->txq, &completions);
10201 }
10202 } else {
10203 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10204 pring = qp->pring;
10205 if (!pring)
10206 continue;
10207 spin_lock_irq(&pring->ring_lock);
10208 list_splice_init(&pring->txq, &completions);
10209 spin_unlock_irq(&pring->ring_lock);
10210 if (pring == phba->sli4_hba.els_wq->pring) {
10211 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10212 /* Set the lpfc data pending flag */
10213 set_bit(LPFC_DATA_READY, &phba->data_flags);
10214 }
10215 }
dea31012005-04-17 16:05:31 -050010216 }
James Smart2e0fef82007-06-17 19:56:36 -050010217 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010218
James Smarta257bf92009-04-06 18:48:10 -040010219 /* Cancel all the IOCBs from the completions list */
10220 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10221 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010222
James Smart0ff10d42008-01-11 01:52:36 -050010223 spin_lock_irqsave(&phba->hbalock, flags);
10224 list_splice_init(&phba->elsbuf, &completions);
10225 phba->elsbuf_cnt = 0;
10226 phba->elsbuf_prev_cnt = 0;
10227 spin_unlock_irqrestore(&phba->hbalock, flags);
10228
10229 while (!list_empty(&completions)) {
10230 list_remove_head(&completions, buf_ptr,
10231 struct lpfc_dmabuf, list);
10232 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10233 kfree(buf_ptr);
10234 }
10235
dea31012005-04-17 16:05:31 -050010236 /* Return any active mbox cmds */
10237 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010238
James Smartda0436e2009-05-22 14:51:39 -040010239 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010240 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010241 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010242
James Smartda0436e2009-05-22 14:51:39 -040010243 return 1;
10244}
James Smart92d7f7b2007-06-17 19:56:38 -050010245
James Smartda0436e2009-05-22 14:51:39 -040010246/**
James Smart3621a712009-04-06 18:47:14 -040010247 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010248 * @srcp: Source memory pointer.
10249 * @destp: Destination memory pointer.
10250 * @cnt: Number of words required to be copied.
10251 *
10252 * This function is used for copying data between driver memory
10253 * and the SLI memory. This function also changes the endianness
10254 * of each word if native endianness is different from SLI
10255 * endianness. This function can be called with or without
10256 * lock.
10257 **/
dea31012005-04-17 16:05:31 -050010258void
10259lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10260{
10261 uint32_t *src = srcp;
10262 uint32_t *dest = destp;
10263 uint32_t ldata;
10264 int i;
10265
10266 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10267 ldata = *src;
10268 ldata = le32_to_cpu(ldata);
10269 *dest = ldata;
10270 src++;
10271 dest++;
10272 }
10273}
10274
James Smarte59058c2008-08-24 21:49:00 -040010275
10276/**
James Smarta0c87cb2009-07-19 10:01:10 -040010277 * lpfc_sli_bemem_bcopy - SLI memory copy function
10278 * @srcp: Source memory pointer.
10279 * @destp: Destination memory pointer.
10280 * @cnt: Number of words required to be copied.
10281 *
10282 * This function is used for copying data between a data structure
10283 * with big endian representation to local endianness.
10284 * This function can be called with or without lock.
10285 **/
10286void
10287lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10288{
10289 uint32_t *src = srcp;
10290 uint32_t *dest = destp;
10291 uint32_t ldata;
10292 int i;
10293
10294 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10295 ldata = *src;
10296 ldata = be32_to_cpu(ldata);
10297 *dest = ldata;
10298 src++;
10299 dest++;
10300 }
10301}
10302
10303/**
James Smart3621a712009-04-06 18:47:14 -040010304 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010305 * @phba: Pointer to HBA context object.
10306 * @pring: Pointer to driver SLI ring object.
10307 * @mp: Pointer to driver buffer object.
10308 *
10309 * This function is called with no lock held.
10310 * It always return zero after adding the buffer to the postbufq
10311 * buffer list.
10312 **/
dea31012005-04-17 16:05:31 -050010313int
James Smart2e0fef82007-06-17 19:56:36 -050010314lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10315 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010316{
10317 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10318 later */
James Smart2e0fef82007-06-17 19:56:36 -050010319 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010320 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010321 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010322 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010323 return 0;
10324}
10325
James Smarte59058c2008-08-24 21:49:00 -040010326/**
James Smart3621a712009-04-06 18:47:14 -040010327 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010328 * @phba: Pointer to HBA context object.
10329 *
10330 * When HBQ is enabled, buffers are searched based on tags. This function
10331 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10332 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10333 * does not conflict with tags of buffer posted for unsolicited events.
10334 * The function returns the allocated tag. The function is called with
10335 * no locks held.
10336 **/
James Smart76bb24e2007-10-27 13:38:00 -040010337uint32_t
10338lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10339{
10340 spin_lock_irq(&phba->hbalock);
10341 phba->buffer_tag_count++;
10342 /*
10343 * Always set the QUE_BUFTAG_BIT to distiguish between
10344 * a tag assigned by HBQ.
10345 */
10346 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10347 spin_unlock_irq(&phba->hbalock);
10348 return phba->buffer_tag_count;
10349}
10350
James Smarte59058c2008-08-24 21:49:00 -040010351/**
James Smart3621a712009-04-06 18:47:14 -040010352 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010353 * @phba: Pointer to HBA context object.
10354 * @pring: Pointer to driver SLI ring object.
10355 * @tag: Buffer tag.
10356 *
10357 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10358 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10359 * iocb is posted to the response ring with the tag of the buffer.
10360 * This function searches the pring->postbufq list using the tag
10361 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10362 * iocb. If the buffer is found then lpfc_dmabuf object of the
10363 * buffer is returned to the caller else NULL is returned.
10364 * This function is called with no lock held.
10365 **/
James Smart76bb24e2007-10-27 13:38:00 -040010366struct lpfc_dmabuf *
10367lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10368 uint32_t tag)
10369{
10370 struct lpfc_dmabuf *mp, *next_mp;
10371 struct list_head *slp = &pring->postbufq;
10372
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010373 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010374 spin_lock_irq(&phba->hbalock);
10375 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10376 if (mp->buffer_tag == tag) {
10377 list_del_init(&mp->list);
10378 pring->postbufq_cnt--;
10379 spin_unlock_irq(&phba->hbalock);
10380 return mp;
10381 }
10382 }
10383
10384 spin_unlock_irq(&phba->hbalock);
10385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010386 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010387 "ring %d Data x%lx x%p x%p x%x\n",
10388 pring->ringno, (unsigned long) tag,
10389 slp->next, slp->prev, pring->postbufq_cnt);
10390
10391 return NULL;
10392}
dea31012005-04-17 16:05:31 -050010393
James Smarte59058c2008-08-24 21:49:00 -040010394/**
James Smart3621a712009-04-06 18:47:14 -040010395 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010396 * @phba: Pointer to HBA context object.
10397 * @pring: Pointer to driver SLI ring object.
10398 * @phys: DMA address of the buffer.
10399 *
10400 * This function searches the buffer list using the dma_address
10401 * of unsolicited event to find the driver's lpfc_dmabuf object
10402 * corresponding to the dma_address. The function returns the
10403 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10404 * This function is called by the ct and els unsolicited event
10405 * handlers to get the buffer associated with the unsolicited
10406 * event.
10407 *
10408 * This function is called with no lock held.
10409 **/
dea31012005-04-17 16:05:31 -050010410struct lpfc_dmabuf *
10411lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10412 dma_addr_t phys)
10413{
10414 struct lpfc_dmabuf *mp, *next_mp;
10415 struct list_head *slp = &pring->postbufq;
10416
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010417 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010418 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010419 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10420 if (mp->phys == phys) {
10421 list_del_init(&mp->list);
10422 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010423 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010424 return mp;
10425 }
10426 }
10427
James Smart2e0fef82007-06-17 19:56:36 -050010428 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010430 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010431 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010432 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010433 slp->next, slp->prev, pring->postbufq_cnt);
10434 return NULL;
10435}
10436
James Smarte59058c2008-08-24 21:49:00 -040010437/**
James Smart3621a712009-04-06 18:47:14 -040010438 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010439 * @phba: Pointer to HBA context object.
10440 * @cmdiocb: Pointer to driver command iocb object.
10441 * @rspiocb: Pointer to driver response iocb object.
10442 *
10443 * This function is the completion handler for the abort iocbs for
10444 * ELS commands. This function is called from the ELS ring event
10445 * handler with no lock held. This function frees memory resources
10446 * associated with the abort iocb.
10447 **/
dea31012005-04-17 16:05:31 -050010448static void
James Smart2e0fef82007-06-17 19:56:36 -050010449lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10450 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010451{
James Smart2e0fef82007-06-17 19:56:36 -050010452 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010453 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010454 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010455
10456 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010457
10458 /*
10459 * Assume that the port already completed and returned, or
10460 * will return the iocb. Just Log the message.
10461 */
James Smart2680eea2007-04-25 09:52:55 -040010462 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10463 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10464
James Smart2e0fef82007-06-17 19:56:36 -050010465 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010466 if (phba->sli_rev < LPFC_SLI_REV4) {
10467 if (abort_iotag != 0 &&
10468 abort_iotag <= phba->sli.last_iotag)
10469 abort_iocb =
10470 phba->sli.iocbq_lookup[abort_iotag];
10471 } else
10472 /* For sli4 the abort_tag is the XRI,
10473 * so the abort routine puts the iotag of the iocb
10474 * being aborted in the context field of the abort
10475 * IOCB.
10476 */
10477 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010478
James Smart2a9bf3d2010-06-07 15:24:45 -040010479 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10480 "0327 Cannot abort els iocb %p "
10481 "with tag %x context %x, abort status %x, "
10482 "abort code %x\n",
10483 abort_iocb, abort_iotag, abort_context,
10484 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010485
James Smartff78d8f2011-12-13 13:21:35 -050010486 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010487 }
James Bottomley604a3e32005-10-29 10:28:33 -050010488 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010489 return;
10490}
10491
James Smarte59058c2008-08-24 21:49:00 -040010492/**
James Smart3621a712009-04-06 18:47:14 -040010493 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010494 * @phba: Pointer to HBA context object.
10495 * @cmdiocb: Pointer to driver command iocb object.
10496 * @rspiocb: Pointer to driver response iocb object.
10497 *
10498 * The function is called from SLI ring event handler with no
10499 * lock held. This function is the completion handler for ELS commands
10500 * which are aborted. The function frees memory resources used for
10501 * the aborted ELS commands.
10502 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010503static void
10504lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10505 struct lpfc_iocbq *rspiocb)
10506{
10507 IOCB_t *irsp = &rspiocb->iocb;
10508
10509 /* ELS cmd tag <ulpIoTag> completes */
10510 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010511 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010512 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010513 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010514 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010515 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10516 lpfc_ct_free_iocb(phba, cmdiocb);
10517 else
10518 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010519 return;
10520}
10521
James Smarte59058c2008-08-24 21:49:00 -040010522/**
James Smart5af5eee2010-10-22 11:06:38 -040010523 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010524 * @phba: Pointer to HBA context object.
10525 * @pring: Pointer to driver SLI ring object.
10526 * @cmdiocb: Pointer to driver command iocb object.
10527 *
James Smart5af5eee2010-10-22 11:06:38 -040010528 * This function issues an abort iocb for the provided command iocb down to
10529 * the port. Other than the case the outstanding command iocb is an abort
10530 * request, this function issues abort out unconditionally. This function is
10531 * called with hbalock held. The function returns 0 when it fails due to
10532 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010533 **/
James Smart5af5eee2010-10-22 11:06:38 -040010534static int
10535lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010536 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010537{
James Smart2e0fef82007-06-17 19:56:36 -050010538 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010539 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010540 IOCB_t *icmd = NULL;
10541 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010542 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010543 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010544
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010545 lockdep_assert_held(&phba->hbalock);
10546
James Smart92d7f7b2007-06-17 19:56:38 -050010547 /*
10548 * There are certain command types we don't want to abort. And we
10549 * don't want to abort commands that are already in the process of
10550 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010551 */
10552 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010553 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010554 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10555 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010556 return 0;
10557
dea31012005-04-17 16:05:31 -050010558 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010559 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010560 if (abtsiocbp == NULL)
10561 return 0;
dea31012005-04-17 16:05:31 -050010562
James Smart07951072007-04-25 09:51:38 -040010563 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010564 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010565 */
10566 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10567
dea31012005-04-17 16:05:31 -050010568 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010569 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10570 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010571 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010572 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010573 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10574 }
James Smartda0436e2009-05-22 14:51:39 -040010575 else
10576 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010577 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010578 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010579
James Smart5ffc2662009-11-18 15:39:44 -050010580 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010581 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010582 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10583 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010584 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10585 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010586
James Smart2e0fef82007-06-17 19:56:36 -050010587 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010588 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10589 else
10590 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10591
10592 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010593 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010594
James Smarte8b62012007-08-02 11:10:09 -040010595 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10596 "0339 Abort xri x%x, original iotag x%x, "
10597 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010598 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010599 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010600 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010601
10602 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010603 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10604 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010605 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010606 /* Note: both hbalock and ring_lock need to be set here */
10607 spin_lock_irqsave(&pring->ring_lock, iflags);
10608 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10609 abtsiocbp, 0);
10610 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10611 } else {
10612 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10613 abtsiocbp, 0);
10614 }
James Smart07951072007-04-25 09:51:38 -040010615
James Smartd7c255b2008-08-24 21:50:00 -040010616 if (retval)
10617 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010618
10619 /*
10620 * Caller to this routine should check for IOCB_ERROR
10621 * and handle it properly. This routine no longer removes
10622 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10623 */
10624 return retval;
10625}
10626
10627/**
10628 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10629 * @phba: Pointer to HBA context object.
10630 * @pring: Pointer to driver SLI ring object.
10631 * @cmdiocb: Pointer to driver command iocb object.
10632 *
10633 * This function issues an abort iocb for the provided command iocb. In case
10634 * of unloading, the abort iocb will not be issued to commands on the ELS
10635 * ring. Instead, the callback function shall be changed to those commands
10636 * so that nothing happens when them finishes. This function is called with
10637 * hbalock held. The function returns 0 when the command iocb is an abort
10638 * request.
10639 **/
10640int
10641lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10642 struct lpfc_iocbq *cmdiocb)
10643{
10644 struct lpfc_vport *vport = cmdiocb->vport;
10645 int retval = IOCB_ERROR;
10646 IOCB_t *icmd = NULL;
10647
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010648 lockdep_assert_held(&phba->hbalock);
10649
James Smart5af5eee2010-10-22 11:06:38 -040010650 /*
10651 * There are certain command types we don't want to abort. And we
10652 * don't want to abort commands that are already in the process of
10653 * being aborted.
10654 */
10655 icmd = &cmdiocb->iocb;
10656 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10657 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10658 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10659 return 0;
10660
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010661 if (!pring) {
10662 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10663 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10664 else
10665 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10666 goto abort_iotag_exit;
10667 }
10668
James Smart5af5eee2010-10-22 11:06:38 -040010669 /*
10670 * If we're unloading, don't abort iocb on the ELS ring, but change
10671 * the callback so that nothing happens when it finishes.
10672 */
10673 if ((vport->load_flag & FC_UNLOADING) &&
10674 (pring->ringno == LPFC_ELS_RING)) {
10675 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10676 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10677 else
10678 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10679 goto abort_iotag_exit;
10680 }
10681
10682 /* Now, we try to issue the abort to the cmdiocb out */
10683 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10684
James Smart07951072007-04-25 09:51:38 -040010685abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010686 /*
10687 * Caller to this routine should check for IOCB_ERROR
10688 * and handle it properly. This routine no longer removes
10689 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010690 */
James Smart2e0fef82007-06-17 19:56:36 -050010691 return retval;
dea31012005-04-17 16:05:31 -050010692}
10693
James Smarte59058c2008-08-24 21:49:00 -040010694/**
James Smart895427b2017-02-12 13:52:30 -080010695 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10696 * @phba: Pointer to HBA context object.
10697 * @pring: Pointer to driver SLI ring object.
10698 * @cmdiocb: Pointer to driver command iocb object.
10699 *
10700 * This function issues an abort iocb for the provided command iocb down to
10701 * the port. Other than the case the outstanding command iocb is an abort
10702 * request, this function issues abort out unconditionally. This function is
10703 * called with hbalock held. The function returns 0 when it fails due to
10704 * memory allocation failure or when the command iocb is an abort request.
10705 **/
10706static int
10707lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10708 struct lpfc_iocbq *cmdiocb)
10709{
10710 struct lpfc_vport *vport = cmdiocb->vport;
10711 struct lpfc_iocbq *abtsiocbp;
10712 union lpfc_wqe *abts_wqe;
10713 int retval;
10714
10715 /*
10716 * There are certain command types we don't want to abort. And we
10717 * don't want to abort commands that are already in the process of
10718 * being aborted.
10719 */
10720 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10721 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10722 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10723 return 0;
10724
10725 /* issue ABTS for this io based on iotag */
10726 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10727 if (abtsiocbp == NULL)
10728 return 0;
10729
10730 /* This signals the response to set the correct status
10731 * before calling the completion handler
10732 */
10733 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10734
10735 /* Complete prepping the abort wqe and issue to the FW. */
10736 abts_wqe = &abtsiocbp->wqe;
10737 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10738 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10739
10740 /* Explicitly set reserved fields to zero.*/
10741 abts_wqe->abort_cmd.rsrvd4 = 0;
10742 abts_wqe->abort_cmd.rsrvd5 = 0;
10743
10744 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10745 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10746 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10747
10748 /* word 7 */
10749 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10750 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10751 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10752 cmdiocb->iocb.ulpClass);
10753
10754 /* word 8 - tell the FW to abort the IO associated with this
10755 * outstanding exchange ID.
10756 */
10757 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10758
10759 /* word 9 - this is the iotag for the abts_wqe completion. */
10760 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10761 abtsiocbp->iotag);
10762
10763 /* word 10 */
10764 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10765 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10766 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10767
10768 /* word 11 */
10769 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10770 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10771 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10772
10773 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10774 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10775 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010776 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010777 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010778 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010779 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10780 "6147 Failed abts issue_wqe with status x%x "
10781 "for oxid x%x\n",
10782 retval, cmdiocb->sli4_xritag);
10783 lpfc_sli_release_iocbq(phba, abtsiocbp);
10784 return retval;
10785 }
10786
10787 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10788 "6148 Drv Abort NVME Request Issued for "
10789 "ox_id x%x on reqtag x%x\n",
10790 cmdiocb->sli4_xritag,
10791 abtsiocbp->iotag);
10792
10793 return retval;
10794}
10795
10796/**
James Smart5af5eee2010-10-22 11:06:38 -040010797 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10798 * @phba: pointer to lpfc HBA data structure.
10799 *
10800 * This routine will abort all pending and outstanding iocbs to an HBA.
10801 **/
10802void
10803lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10804{
10805 struct lpfc_sli *psli = &phba->sli;
10806 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010807 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010808 int i;
10809
James Smart895427b2017-02-12 13:52:30 -080010810 if (phba->sli_rev != LPFC_SLI_REV4) {
10811 for (i = 0; i < psli->num_rings; i++) {
10812 pring = &psli->sli3_ring[i];
10813 lpfc_sli_abort_iocb_ring(phba, pring);
10814 }
10815 return;
10816 }
10817 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10818 pring = qp->pring;
10819 if (!pring)
10820 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010821 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010822 }
10823}
10824
10825/**
James Smart3621a712009-04-06 18:47:14 -040010826 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010827 * @iocbq: Pointer to driver iocb object.
10828 * @vport: Pointer to driver virtual port object.
10829 * @tgt_id: SCSI ID of the target.
10830 * @lun_id: LUN ID of the scsi device.
10831 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10832 *
James Smart3621a712009-04-06 18:47:14 -040010833 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010834 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10835 * 0 if the filtering criteria is met for the given iocb and will return
10836 * 1 if the filtering criteria is not met.
10837 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10838 * given iocb is for the SCSI device specified by vport, tgt_id and
10839 * lun_id parameter.
10840 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10841 * given iocb is for the SCSI target specified by vport and tgt_id
10842 * parameters.
10843 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10844 * given iocb is for the SCSI host associated with the given vport.
10845 * This function is called with no locks held.
10846 **/
dea31012005-04-17 16:05:31 -050010847static int
James Smart51ef4c22007-08-02 11:10:31 -040010848lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10849 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010850 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010851{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010852 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010853 int rc = 1;
10854
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010855 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10856 return rc;
10857
James Smart51ef4c22007-08-02 11:10:31 -040010858 if (iocbq->vport != vport)
10859 return rc;
10860
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010861 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010862
James Smart495a7142008-06-14 22:52:59 -040010863 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010864 return rc;
10865
10866 switch (ctx_cmd) {
10867 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010868 if ((lpfc_cmd->rdata->pnode) &&
10869 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10870 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010871 rc = 0;
10872 break;
10873 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010874 if ((lpfc_cmd->rdata->pnode) &&
10875 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010876 rc = 0;
10877 break;
dea31012005-04-17 16:05:31 -050010878 case LPFC_CTX_HOST:
10879 rc = 0;
10880 break;
10881 default:
10882 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010883 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010884 break;
10885 }
10886
10887 return rc;
10888}
10889
James Smarte59058c2008-08-24 21:49:00 -040010890/**
James Smart3621a712009-04-06 18:47:14 -040010891 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010892 * @vport: Pointer to virtual port.
10893 * @tgt_id: SCSI ID of the target.
10894 * @lun_id: LUN ID of the scsi device.
10895 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10896 *
10897 * This function returns number of FCP commands pending for the vport.
10898 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10899 * commands pending on the vport associated with SCSI device specified
10900 * by tgt_id and lun_id parameters.
10901 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10902 * commands pending on the vport associated with SCSI target specified
10903 * by tgt_id parameter.
10904 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10905 * commands pending on the vport.
10906 * This function returns the number of iocbs which satisfy the filter.
10907 * This function is called without any lock held.
10908 **/
dea31012005-04-17 16:05:31 -050010909int
James Smart51ef4c22007-08-02 11:10:31 -040010910lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10911 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010912{
James Smart51ef4c22007-08-02 11:10:31 -040010913 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010914 struct lpfc_iocbq *iocbq;
10915 int sum, i;
dea31012005-04-17 16:05:31 -050010916
Johannes Thumshirn31979002016-07-18 16:06:03 +020010917 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010918 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10919 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010920
James Smart51ef4c22007-08-02 11:10:31 -040010921 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10922 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010923 sum++;
dea31012005-04-17 16:05:31 -050010924 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010925 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010926
dea31012005-04-17 16:05:31 -050010927 return sum;
10928}
10929
James Smarte59058c2008-08-24 21:49:00 -040010930/**
James Smart3621a712009-04-06 18:47:14 -040010931 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010932 * @phba: Pointer to HBA context object
10933 * @cmdiocb: Pointer to command iocb object.
10934 * @rspiocb: Pointer to response iocb object.
10935 *
10936 * This function is called when an aborted FCP iocb completes. This
10937 * function is called by the ring event handler with no lock held.
10938 * This function frees the iocb.
10939 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010940void
James Smart2e0fef82007-06-17 19:56:36 -050010941lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10942 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010943{
James Smartcb69f7d2011-12-13 13:21:57 -050010944 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010945 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010946 "original iotag x%x, abort cmd iotag x%x "
10947 "status 0x%x, reason 0x%x\n",
10948 cmdiocb->iocb.un.acxri.abortContextTag,
10949 cmdiocb->iocb.un.acxri.abortIoTag,
10950 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10951 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010952 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010953 return;
10954}
10955
James Smarte59058c2008-08-24 21:49:00 -040010956/**
James Smart3621a712009-04-06 18:47:14 -040010957 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010958 * @vport: Pointer to virtual port.
10959 * @pring: Pointer to driver SLI ring object.
10960 * @tgt_id: SCSI ID of the target.
10961 * @lun_id: LUN ID of the scsi device.
10962 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10963 *
10964 * This function sends an abort command for every SCSI command
10965 * associated with the given virtual port pending on the ring
10966 * filtered by lpfc_sli_validate_fcp_iocb function.
10967 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10968 * FCP iocbs associated with lun specified by tgt_id and lun_id
10969 * parameters
10970 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10971 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10972 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10973 * FCP iocbs associated with virtual port.
10974 * This function returns number of iocbs it failed to abort.
10975 * This function is called with no locks held.
10976 **/
dea31012005-04-17 16:05:31 -050010977int
James Smart51ef4c22007-08-02 11:10:31 -040010978lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10979 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010980{
James Smart51ef4c22007-08-02 11:10:31 -040010981 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010982 struct lpfc_iocbq *iocbq;
10983 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070010984 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050010985 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010986 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010987 int i;
dea31012005-04-17 16:05:31 -050010988
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010989 for (i = 1; i <= phba->sli.last_iotag; i++) {
10990 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010991
James Smart51ef4c22007-08-02 11:10:31 -040010992 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010993 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010994 continue;
10995
James Smartafbd8d82013-09-06 12:22:13 -040010996 /*
10997 * If the iocbq is already being aborted, don't take a second
10998 * action, but do count it.
10999 */
11000 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11001 continue;
11002
dea31012005-04-17 16:05:31 -050011003 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011004 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011005 if (abtsiocb == NULL) {
11006 errcnt++;
11007 continue;
11008 }
dea31012005-04-17 16:05:31 -050011009
James Smartafbd8d82013-09-06 12:22:13 -040011010 /* indicate the IO is being aborted by the driver. */
11011 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11012
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011013 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011014 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11015 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011016 if (phba->sli_rev == LPFC_SLI_REV4)
11017 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11018 else
11019 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011020 abtsiocb->iocb.ulpLe = 1;
11021 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011022 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011023
James Smart5ffc2662009-11-18 15:39:44 -050011024 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011025 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011026 if (iocbq->iocb_flag & LPFC_IO_FCP)
11027 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011028 if (iocbq->iocb_flag & LPFC_IO_FOF)
11029 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011030
James Smart2e0fef82007-06-17 19:56:36 -050011031 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011032 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11033 else
11034 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11035
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011036 /* Setup callback routine and issue the command. */
11037 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011038 if (phba->sli_rev == LPFC_SLI_REV4) {
11039 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11040 if (!pring_s4)
11041 continue;
11042 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11043 abtsiocb, 0);
11044 } else
11045 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11046 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011047 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011048 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011049 errcnt++;
11050 continue;
11051 }
11052 }
11053
11054 return errcnt;
11055}
11056
James Smarte59058c2008-08-24 21:49:00 -040011057/**
James Smart98912dda2014-04-04 13:52:31 -040011058 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11059 * @vport: Pointer to virtual port.
11060 * @pring: Pointer to driver SLI ring object.
11061 * @tgt_id: SCSI ID of the target.
11062 * @lun_id: LUN ID of the scsi device.
11063 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11064 *
11065 * This function sends an abort command for every SCSI command
11066 * associated with the given virtual port pending on the ring
11067 * filtered by lpfc_sli_validate_fcp_iocb function.
11068 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11069 * FCP iocbs associated with lun specified by tgt_id and lun_id
11070 * parameters
11071 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11072 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11073 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11074 * FCP iocbs associated with virtual port.
11075 * This function returns number of iocbs it aborted .
11076 * This function is called with no locks held right after a taskmgmt
11077 * command is sent.
11078 **/
11079int
11080lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11081 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11082{
11083 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011084 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011085 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011086 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011087 struct lpfc_iocbq *iocbq;
11088 IOCB_t *icmd;
11089 int sum, i, ret_val;
11090 unsigned long iflags;
11091 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011092
11093 spin_lock_irq(&phba->hbalock);
11094
11095 /* all I/Os are in process of being flushed */
11096 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11097 spin_unlock_irq(&phba->hbalock);
11098 return 0;
11099 }
11100 sum = 0;
11101
11102 for (i = 1; i <= phba->sli.last_iotag; i++) {
11103 iocbq = phba->sli.iocbq_lookup[i];
11104
11105 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11106 cmd) != 0)
11107 continue;
11108
11109 /*
11110 * If the iocbq is already being aborted, don't take a second
11111 * action, but do count it.
11112 */
11113 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11114 continue;
11115
11116 /* issue ABTS for this IOCB based on iotag */
11117 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11118 if (abtsiocbq == NULL)
11119 continue;
11120
11121 icmd = &iocbq->iocb;
11122 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11123 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11124 if (phba->sli_rev == LPFC_SLI_REV4)
11125 abtsiocbq->iocb.un.acxri.abortIoTag =
11126 iocbq->sli4_xritag;
11127 else
11128 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11129 abtsiocbq->iocb.ulpLe = 1;
11130 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11131 abtsiocbq->vport = vport;
11132
11133 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011134 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011135 if (iocbq->iocb_flag & LPFC_IO_FCP)
11136 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011137 if (iocbq->iocb_flag & LPFC_IO_FOF)
11138 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011139
James Smart8c50d252014-09-03 12:58:16 -040011140 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11141 ndlp = lpfc_cmd->rdata->pnode;
11142
11143 if (lpfc_is_link_up(phba) &&
11144 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011145 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11146 else
11147 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11148
11149 /* Setup callback routine and issue the command. */
11150 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11151
11152 /*
11153 * Indicate the IO is being aborted by the driver and set
11154 * the caller's flag into the aborted IO.
11155 */
11156 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11157
11158 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011159 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11160 if (pring_s4 == NULL)
11161 continue;
James Smart98912dda2014-04-04 13:52:31 -040011162 /* Note: both hbalock and ring_lock must be set here */
11163 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11164 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11165 abtsiocbq, 0);
11166 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11167 } else {
11168 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11169 abtsiocbq, 0);
11170 }
11171
11172
11173 if (ret_val == IOCB_ERROR)
11174 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11175 else
11176 sum++;
11177 }
11178 spin_unlock_irq(&phba->hbalock);
11179 return sum;
11180}
11181
11182/**
James Smart3621a712009-04-06 18:47:14 -040011183 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011184 * @phba: Pointer to HBA context object.
11185 * @cmdiocbq: Pointer to command iocb.
11186 * @rspiocbq: Pointer to response iocb.
11187 *
11188 * This function is the completion handler for iocbs issued using
11189 * lpfc_sli_issue_iocb_wait function. This function is called by the
11190 * ring event handler function without any lock held. This function
11191 * can be called from both worker thread context and interrupt
11192 * context. This function also can be called from other thread which
11193 * cleans up the SLI layer objects.
11194 * This function copy the contents of the response iocb to the
11195 * response iocb memory object provided by the caller of
11196 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11197 * sleeps for the iocb completion.
11198 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011199static void
11200lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11201 struct lpfc_iocbq *cmdiocbq,
11202 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011203{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011204 wait_queue_head_t *pdone_q;
11205 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011206 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011207
James Smart2e0fef82007-06-17 19:56:36 -050011208 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011209 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11210
11211 /*
11212 * A time out has occurred for the iocb. If a time out
11213 * completion handler has been supplied, call it. Otherwise,
11214 * just free the iocbq.
11215 */
11216
11217 spin_unlock_irqrestore(&phba->hbalock, iflags);
11218 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11219 cmdiocbq->wait_iocb_cmpl = NULL;
11220 if (cmdiocbq->iocb_cmpl)
11221 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11222 else
11223 lpfc_sli_release_iocbq(phba, cmdiocbq);
11224 return;
11225 }
11226
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011227 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11228 if (cmdiocbq->context2 && rspiocbq)
11229 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11230 &rspiocbq->iocb, sizeof(IOCB_t));
11231
James Smart0f65ff62010-02-26 14:14:23 -050011232 /* Set the exchange busy flag for task management commands */
11233 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11234 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11235 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11236 cur_iocbq);
11237 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11238 }
11239
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011240 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011241 if (pdone_q)
11242 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011243 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011244 return;
11245}
11246
James Smarte59058c2008-08-24 21:49:00 -040011247/**
James Smartd11e31d2009-06-10 17:23:06 -040011248 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11249 * @phba: Pointer to HBA context object..
11250 * @piocbq: Pointer to command iocb.
11251 * @flag: Flag to test.
11252 *
11253 * This routine grabs the hbalock and then test the iocb_flag to
11254 * see if the passed in flag is set.
11255 * Returns:
11256 * 1 if flag is set.
11257 * 0 if flag is not set.
11258 **/
11259static int
11260lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11261 struct lpfc_iocbq *piocbq, uint32_t flag)
11262{
11263 unsigned long iflags;
11264 int ret;
11265
11266 spin_lock_irqsave(&phba->hbalock, iflags);
11267 ret = piocbq->iocb_flag & flag;
11268 spin_unlock_irqrestore(&phba->hbalock, iflags);
11269 return ret;
11270
11271}
11272
11273/**
James Smart3621a712009-04-06 18:47:14 -040011274 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011275 * @phba: Pointer to HBA context object..
11276 * @pring: Pointer to sli ring.
11277 * @piocb: Pointer to command iocb.
11278 * @prspiocbq: Pointer to response iocb.
11279 * @timeout: Timeout in number of seconds.
11280 *
11281 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011282 * iocb to complete. The iocb_cmpl field of the shall be used
11283 * to handle iocbs which time out. If the field is NULL, the
11284 * function shall free the iocbq structure. If more clean up is
11285 * needed, the caller is expected to provide a completion function
11286 * that will provide the needed clean up. If the iocb command is
11287 * not completed within timeout seconds, the function will either
11288 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11289 * completion function set in the iocb_cmpl field and then return
11290 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11291 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011292 * The function waits for the iocb completion using an
11293 * non-interruptible wait.
11294 * This function will sleep while waiting for iocb completion.
11295 * So, this function should not be called from any context which
11296 * does not allow sleeping. Due to the same reason, this function
11297 * cannot be called with interrupt disabled.
11298 * This function assumes that the iocb completions occur while
11299 * this function sleep. So, this function cannot be called from
11300 * the thread which process iocb completion for this ring.
11301 * This function clears the iocb_flag of the iocb object before
11302 * issuing the iocb and the iocb completion handler sets this
11303 * flag and wakes this thread when the iocb completes.
11304 * The contents of the response iocb will be copied to prspiocbq
11305 * by the completion handler when the command completes.
11306 * This function returns IOCB_SUCCESS when success.
11307 * This function is called with no lock held.
11308 **/
dea31012005-04-17 16:05:31 -050011309int
James Smart2e0fef82007-06-17 19:56:36 -050011310lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011311 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011312 struct lpfc_iocbq *piocb,
11313 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011314 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011315{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011316 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011317 long timeleft, timeout_req = 0;
11318 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011319 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011320 struct lpfc_iocbq *iocb;
11321 int txq_cnt = 0;
11322 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011323 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011324 unsigned long iflags;
11325 bool iocb_completed = true;
11326
James Smart895427b2017-02-12 13:52:30 -080011327 if (phba->sli_rev >= LPFC_SLI_REV4)
11328 pring = lpfc_sli4_calc_ring(phba, piocb);
11329 else
11330 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011331 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011332 * If the caller has provided a response iocbq buffer, then context2
11333 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011334 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011335 if (prspiocbq) {
11336 if (piocb->context2)
11337 return IOCB_ERROR;
11338 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011339 }
11340
James Smart5a0916b2013-07-15 18:31:42 -040011341 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011342 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11343 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011344 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011345
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011346 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011347 if (lpfc_readl(phba->HCregaddr, &creg_val))
11348 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011349 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11350 writel(creg_val, phba->HCregaddr);
11351 readl(phba->HCregaddr); /* flush */
11352 }
11353
James Smart2a9bf3d2010-06-07 15:24:45 -040011354 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11355 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011356 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011357 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011358 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011359 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011360 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011361 spin_lock_irqsave(&phba->hbalock, iflags);
11362 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011363
James Smart5a0916b2013-07-15 18:31:42 -040011364 /*
11365 * IOCB timed out. Inform the wake iocb wait
11366 * completion function and set local status
11367 */
11368
11369 iocb_completed = false;
11370 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11371 }
11372 spin_unlock_irqrestore(&phba->hbalock, iflags);
11373 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011374 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011375 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011376 /* Note: we are not indicating if the IOCB has a success
11377 * status or not - that's for the caller to check.
11378 * IOCB_SUCCESS means just that the command was sent and
11379 * completed. Not that it completed successfully.
11380 * */
James Smart7054a602007-04-25 09:52:34 -040011381 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011383 "0338 IOCB wait timeout error - no "
11384 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011385 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011386 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011387 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011388 "0330 IOCB wake NOT set, "
11389 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011390 timeout, (timeleft / jiffies));
11391 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011392 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011393 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011394 if (phba->cfg_log_verbose & LOG_SLI) {
11395 list_for_each_entry(iocb, &pring->txq, list) {
11396 txq_cnt++;
11397 }
11398 list_for_each_entry(iocb, &pring->txcmplq, list) {
11399 txcmplq_cnt++;
11400 }
11401 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11402 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11403 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11404 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011405 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011406 } else {
11407 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011408 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011409 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011410 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011411 }
11412
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011413 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011414 if (lpfc_readl(phba->HCregaddr, &creg_val))
11415 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011416 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11417 writel(creg_val, phba->HCregaddr);
11418 readl(phba->HCregaddr); /* flush */
11419 }
11420
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011421 if (prspiocbq)
11422 piocb->context2 = NULL;
11423
11424 piocb->context_un.wait_queue = NULL;
11425 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011426 return retval;
11427}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011428
James Smarte59058c2008-08-24 21:49:00 -040011429/**
James Smart3621a712009-04-06 18:47:14 -040011430 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011431 * @phba: Pointer to HBA context object.
11432 * @pmboxq: Pointer to driver mailbox object.
11433 * @timeout: Timeout in number of seconds.
11434 *
11435 * This function issues the mailbox to firmware and waits for the
11436 * mailbox command to complete. If the mailbox command is not
11437 * completed within timeout seconds, it returns MBX_TIMEOUT.
11438 * The function waits for the mailbox completion using an
11439 * interruptible wait. If the thread is woken up due to a
11440 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11441 * should not free the mailbox resources, if this function returns
11442 * MBX_TIMEOUT.
11443 * This function will sleep while waiting for mailbox completion.
11444 * So, this function should not be called from any context which
11445 * does not allow sleeping. Due to the same reason, this function
11446 * cannot be called with interrupt disabled.
11447 * This function assumes that the mailbox completion occurs while
11448 * this function sleep. So, this function cannot be called from
11449 * the worker thread which processes mailbox completion.
11450 * This function is called in the context of HBA management
11451 * applications.
11452 * This function returns MBX_SUCCESS when successful.
11453 * This function is called with no lock held.
11454 **/
dea31012005-04-17 16:05:31 -050011455int
James Smart2e0fef82007-06-17 19:56:36 -050011456lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011457 uint32_t timeout)
11458{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011459 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011460 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011461 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011462 unsigned long flag;
dea31012005-04-17 16:05:31 -050011463
James Smartb230b8a2013-05-31 17:05:27 -040011464 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011465 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011466 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011467
James Smart495a7142008-06-14 22:52:59 -040011468 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011469 /* setup wake call as IOCB callback */
11470 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11471 /* setup context field to pass wait_queue pointer to wake function */
11472 pmboxq->context1 = &done_q;
11473
dea31012005-04-17 16:05:31 -050011474 /* now issue the command */
11475 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011476 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011477 wait_event_interruptible_timeout(done_q,
11478 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011479 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011480
James Smart858c9f62007-06-17 19:56:39 -050011481 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011482 /* restore the possible extended buffer for free resource */
11483 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011484 /*
11485 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11486 * else do not free the resources.
11487 */
James Smartd7c47992010-06-08 18:31:54 -040011488 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011489 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011490 } else {
James Smart7054a602007-04-25 09:52:34 -040011491 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011492 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11493 }
11494 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011495 } else {
11496 /* restore the possible extended buffer for free resource */
11497 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011498 }
11499
dea31012005-04-17 16:05:31 -050011500 return retval;
11501}
11502
James Smarte59058c2008-08-24 21:49:00 -040011503/**
James Smart3772a992009-05-22 14:50:54 -040011504 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011505 * @phba: Pointer to HBA context.
11506 *
James Smart3772a992009-05-22 14:50:54 -040011507 * This function is called to shutdown the driver's mailbox sub-system.
11508 * It first marks the mailbox sub-system is in a block state to prevent
11509 * the asynchronous mailbox command from issued off the pending mailbox
11510 * command queue. If the mailbox command sub-system shutdown is due to
11511 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11512 * the mailbox sub-system flush routine to forcefully bring down the
11513 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11514 * as with offline or HBA function reset), this routine will wait for the
11515 * outstanding mailbox command to complete before invoking the mailbox
11516 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011517 **/
James Smart3772a992009-05-22 14:50:54 -040011518void
James Smart618a5232012-06-12 13:54:36 -040011519lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011520{
James Smart3772a992009-05-22 14:50:54 -040011521 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011522 unsigned long timeout;
11523
James Smart618a5232012-06-12 13:54:36 -040011524 if (mbx_action == LPFC_MBX_NO_WAIT) {
11525 /* delay 100ms for port state */
11526 msleep(100);
11527 lpfc_sli_mbox_sys_flush(phba);
11528 return;
11529 }
James Smarta183a152011-10-10 21:32:43 -040011530 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011531
James Smart3772a992009-05-22 14:50:54 -040011532 spin_lock_irq(&phba->hbalock);
11533 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011534
11535 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011536 /* Determine how long we might wait for the active mailbox
11537 * command to be gracefully completed by firmware.
11538 */
James Smarta183a152011-10-10 21:32:43 -040011539 if (phba->sli.mbox_active)
11540 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11541 phba->sli.mbox_active) *
11542 1000) + jiffies;
11543 spin_unlock_irq(&phba->hbalock);
11544
James Smart3772a992009-05-22 14:50:54 -040011545 while (phba->sli.mbox_active) {
11546 /* Check active mailbox complete status every 2ms */
11547 msleep(2);
11548 if (time_after(jiffies, timeout))
11549 /* Timeout, let the mailbox flush routine to
11550 * forcefully release active mailbox command
11551 */
11552 break;
11553 }
James Smartd7069f02012-03-01 22:36:29 -050011554 } else
11555 spin_unlock_irq(&phba->hbalock);
11556
James Smart3772a992009-05-22 14:50:54 -040011557 lpfc_sli_mbox_sys_flush(phba);
11558}
11559
11560/**
11561 * lpfc_sli_eratt_read - read sli-3 error attention events
11562 * @phba: Pointer to HBA context.
11563 *
11564 * This function is called to read the SLI3 device error attention registers
11565 * for possible error attention events. The caller must hold the hostlock
11566 * with spin_lock_irq().
11567 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011568 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011569 * Register and returns 0 otherwise.
11570 **/
11571static int
11572lpfc_sli_eratt_read(struct lpfc_hba *phba)
11573{
James Smarted957682007-06-17 19:56:37 -050011574 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011575
James Smart3772a992009-05-22 14:50:54 -040011576 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011577 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11578 goto unplug_err;
11579
James Smart3772a992009-05-22 14:50:54 -040011580 if (ha_copy & HA_ERATT) {
11581 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011582 if (lpfc_sli_read_hs(phba))
11583 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011584
James Smart3772a992009-05-22 14:50:54 -040011585 /* Check if there is a deferred error condition is active */
11586 if ((HS_FFER1 & phba->work_hs) &&
11587 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011588 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011589 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011590 /* Clear all interrupt enable conditions */
11591 writel(0, phba->HCregaddr);
11592 readl(phba->HCregaddr);
11593 }
11594
11595 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011596 phba->work_ha |= HA_ERATT;
11597 /* Indicate polling handles this ERATT */
11598 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011599 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011600 }
James Smart3772a992009-05-22 14:50:54 -040011601 return 0;
James Smart9940b972011-03-11 16:06:12 -050011602
11603unplug_err:
11604 /* Set the driver HS work bitmap */
11605 phba->work_hs |= UNPLUG_ERR;
11606 /* Set the driver HA work bitmap */
11607 phba->work_ha |= HA_ERATT;
11608 /* Indicate polling handles this ERATT */
11609 phba->hba_flag |= HBA_ERATT_HANDLED;
11610 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011611}
11612
James Smarte59058c2008-08-24 21:49:00 -040011613/**
James Smartda0436e2009-05-22 14:51:39 -040011614 * lpfc_sli4_eratt_read - read sli-4 error attention events
11615 * @phba: Pointer to HBA context.
11616 *
11617 * This function is called to read the SLI4 device error attention registers
11618 * for possible error attention events. The caller must hold the hostlock
11619 * with spin_lock_irq().
11620 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011621 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011622 * Register and returns 0 otherwise.
11623 **/
11624static int
11625lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11626{
11627 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011628 uint32_t if_type, portsmphr;
11629 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011630
James Smart2fcee4b2010-12-15 17:57:46 -050011631 /*
11632 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011633 * registers for error attention. This can be changed later.
11634 */
James Smart2fcee4b2010-12-15 17:57:46 -050011635 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11636 switch (if_type) {
11637 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011638 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11639 &uerr_sta_lo) ||
11640 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11641 &uerr_sta_hi)) {
11642 phba->work_hs |= UNPLUG_ERR;
11643 phba->work_ha |= HA_ERATT;
11644 phba->hba_flag |= HBA_ERATT_HANDLED;
11645 return 1;
11646 }
James Smart2fcee4b2010-12-15 17:57:46 -050011647 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11648 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11650 "1423 HBA Unrecoverable error: "
11651 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11652 "ue_mask_lo_reg=0x%x, "
11653 "ue_mask_hi_reg=0x%x\n",
11654 uerr_sta_lo, uerr_sta_hi,
11655 phba->sli4_hba.ue_mask_lo,
11656 phba->sli4_hba.ue_mask_hi);
11657 phba->work_status[0] = uerr_sta_lo;
11658 phba->work_status[1] = uerr_sta_hi;
11659 phba->work_ha |= HA_ERATT;
11660 phba->hba_flag |= HBA_ERATT_HANDLED;
11661 return 1;
11662 }
11663 break;
11664 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011665 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11666 &portstat_reg.word0) ||
11667 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11668 &portsmphr)){
11669 phba->work_hs |= UNPLUG_ERR;
11670 phba->work_ha |= HA_ERATT;
11671 phba->hba_flag |= HBA_ERATT_HANDLED;
11672 return 1;
11673 }
James Smart2fcee4b2010-12-15 17:57:46 -050011674 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11675 phba->work_status[0] =
11676 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11677 phba->work_status[1] =
11678 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011680 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011681 "port status reg 0x%x, "
11682 "port smphr reg 0x%x, "
11683 "error 1=0x%x, error 2=0x%x\n",
11684 portstat_reg.word0,
11685 portsmphr,
11686 phba->work_status[0],
11687 phba->work_status[1]);
11688 phba->work_ha |= HA_ERATT;
11689 phba->hba_flag |= HBA_ERATT_HANDLED;
11690 return 1;
11691 }
11692 break;
11693 case LPFC_SLI_INTF_IF_TYPE_1:
11694 default:
James Smarta747c9c2009-11-18 15:41:10 -050011695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011696 "2886 HBA Error Attention on unsupported "
11697 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011698 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011699 }
James Smart2fcee4b2010-12-15 17:57:46 -050011700
James Smartda0436e2009-05-22 14:51:39 -040011701 return 0;
11702}
11703
11704/**
James Smart3621a712009-04-06 18:47:14 -040011705 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011706 * @phba: Pointer to HBA context.
11707 *
James Smart3772a992009-05-22 14:50:54 -040011708 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011709 * error attention register bit for error attention events.
11710 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011711 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011712 * Register and returns 0 otherwise.
11713 **/
11714int
11715lpfc_sli_check_eratt(struct lpfc_hba *phba)
11716{
11717 uint32_t ha_copy;
11718
11719 /* If somebody is waiting to handle an eratt, don't process it
11720 * here. The brdkill function will do this.
11721 */
11722 if (phba->link_flag & LS_IGNORE_ERATT)
11723 return 0;
11724
11725 /* Check if interrupt handler handles this ERATT */
11726 spin_lock_irq(&phba->hbalock);
11727 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11728 /* Interrupt handler has handled ERATT */
11729 spin_unlock_irq(&phba->hbalock);
11730 return 0;
11731 }
11732
James Smarta257bf92009-04-06 18:48:10 -040011733 /*
11734 * If there is deferred error attention, do not check for error
11735 * attention
11736 */
11737 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11738 spin_unlock_irq(&phba->hbalock);
11739 return 0;
11740 }
11741
James Smart3772a992009-05-22 14:50:54 -040011742 /* If PCI channel is offline, don't process it */
11743 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011744 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011745 return 0;
11746 }
11747
11748 switch (phba->sli_rev) {
11749 case LPFC_SLI_REV2:
11750 case LPFC_SLI_REV3:
11751 /* Read chip Host Attention (HA) register */
11752 ha_copy = lpfc_sli_eratt_read(phba);
11753 break;
James Smartda0436e2009-05-22 14:51:39 -040011754 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011755 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011756 ha_copy = lpfc_sli4_eratt_read(phba);
11757 break;
James Smart3772a992009-05-22 14:50:54 -040011758 default:
11759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11760 "0299 Invalid SLI revision (%d)\n",
11761 phba->sli_rev);
11762 ha_copy = 0;
11763 break;
James Smart93996272008-08-24 21:50:30 -040011764 }
11765 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011766
11767 return ha_copy;
11768}
11769
11770/**
11771 * lpfc_intr_state_check - Check device state for interrupt handling
11772 * @phba: Pointer to HBA context.
11773 *
11774 * This inline routine checks whether a device or its PCI slot is in a state
11775 * that the interrupt should be handled.
11776 *
11777 * This function returns 0 if the device or the PCI slot is in a state that
11778 * interrupt should be handled, otherwise -EIO.
11779 */
11780static inline int
11781lpfc_intr_state_check(struct lpfc_hba *phba)
11782{
11783 /* If the pci channel is offline, ignore all the interrupts */
11784 if (unlikely(pci_channel_offline(phba->pcidev)))
11785 return -EIO;
11786
11787 /* Update device level interrupt statistics */
11788 phba->sli.slistat.sli_intr++;
11789
11790 /* Ignore all interrupts during initialization. */
11791 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11792 return -EIO;
11793
James Smart93996272008-08-24 21:50:30 -040011794 return 0;
11795}
11796
11797/**
James Smart3772a992009-05-22 14:50:54 -040011798 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011799 * @irq: Interrupt number.
11800 * @dev_id: The device context pointer.
11801 *
James Smart93996272008-08-24 21:50:30 -040011802 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011803 * service routine when device with SLI-3 interface spec is enabled with
11804 * MSI-X multi-message interrupt mode and there are slow-path events in
11805 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11806 * interrupt mode, this function is called as part of the device-level
11807 * interrupt handler. When the PCI slot is in error recovery or the HBA
11808 * is undergoing initialization, the interrupt handler will not process
11809 * the interrupt. The link attention and ELS ring attention events are
11810 * handled by the worker thread. The interrupt handler signals the worker
11811 * thread and returns for these events. This function is called without
11812 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011813 * structures.
11814 *
11815 * This function returns IRQ_HANDLED when interrupt is handled else it
11816 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011817 **/
dea31012005-04-17 16:05:31 -050011818irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011819lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011820{
James Smart2e0fef82007-06-17 19:56:36 -050011821 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011822 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011823 uint32_t work_ha_copy;
11824 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011825 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011826 uint32_t control;
11827
James Smart92d7f7b2007-06-17 19:56:38 -050011828 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011829 struct lpfc_vport *vport;
11830 struct lpfc_nodelist *ndlp;
11831 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011832 LPFC_MBOXQ_t *pmb;
11833 int rc;
11834
dea31012005-04-17 16:05:31 -050011835 /*
11836 * Get the driver's phba structure from the dev_id and
11837 * assume the HBA is not interrupting.
11838 */
James Smart93996272008-08-24 21:50:30 -040011839 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011840
11841 if (unlikely(!phba))
11842 return IRQ_NONE;
11843
dea31012005-04-17 16:05:31 -050011844 /*
James Smart93996272008-08-24 21:50:30 -040011845 * Stuff needs to be attented to when this function is invoked as an
11846 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011847 */
James Smart93996272008-08-24 21:50:30 -040011848 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011849 /* Check device state for handling interrupt */
11850 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011851 return IRQ_NONE;
11852 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011853 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011854 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11855 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011856 /* If somebody is waiting to handle an eratt don't process it
11857 * here. The brdkill function will do this.
11858 */
11859 if (phba->link_flag & LS_IGNORE_ERATT)
11860 ha_copy &= ~HA_ERATT;
11861 /* Check the need for handling ERATT in interrupt handler */
11862 if (ha_copy & HA_ERATT) {
11863 if (phba->hba_flag & HBA_ERATT_HANDLED)
11864 /* ERATT polling has handled ERATT */
11865 ha_copy &= ~HA_ERATT;
11866 else
11867 /* Indicate interrupt handler handles ERATT */
11868 phba->hba_flag |= HBA_ERATT_HANDLED;
11869 }
James Smarta257bf92009-04-06 18:48:10 -040011870
11871 /*
11872 * If there is deferred error attention, do not check for any
11873 * interrupt.
11874 */
11875 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011876 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011877 return IRQ_NONE;
11878 }
11879
James Smart93996272008-08-24 21:50:30 -040011880 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011881 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11882 goto unplug_error;
11883
James Smarta747c9c2009-11-18 15:41:10 -050011884 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11885 HC_LAINT_ENA | HC_ERINT_ENA),
11886 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011887 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11888 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011889 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011890 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011891 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011892 } else
11893 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011894
dea31012005-04-17 16:05:31 -050011895 work_ha_copy = ha_copy & phba->work_ha_mask;
11896
James Smart93996272008-08-24 21:50:30 -040011897 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011898 if (work_ha_copy & HA_LATT) {
11899 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11900 /*
11901 * Turn off Link Attention interrupts
11902 * until CLEAR_LA done
11903 */
James Smart5b75da22008-12-04 22:39:35 -050011904 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011905 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011906 if (lpfc_readl(phba->HCregaddr, &control))
11907 goto unplug_error;
dea31012005-04-17 16:05:31 -050011908 control &= ~HC_LAINT_ENA;
11909 writel(control, phba->HCregaddr);
11910 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011911 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011912 }
11913 else
11914 work_ha_copy &= ~HA_LATT;
11915 }
11916
James Smart93996272008-08-24 21:50:30 -040011917 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011918 /*
11919 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11920 * the only slow ring.
11921 */
11922 status = (work_ha_copy &
11923 (HA_RXMASK << (4*LPFC_ELS_RING)));
11924 status >>= (4*LPFC_ELS_RING);
11925 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011926 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011927 if (lpfc_readl(phba->HCregaddr, &control))
11928 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011929
11930 lpfc_debugfs_slow_ring_trc(phba,
11931 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11932 control, status,
11933 (uint32_t)phba->sli.slistat.sli_intr);
11934
James Smart858c9f62007-06-17 19:56:39 -050011935 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011936 lpfc_debugfs_slow_ring_trc(phba,
11937 "ISR Disable ring:"
11938 "pwork:x%x hawork:x%x wait:x%x",
11939 phba->work_ha, work_ha_copy,
11940 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011941 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011942
James Smart858c9f62007-06-17 19:56:39 -050011943 control &=
11944 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011945 writel(control, phba->HCregaddr);
11946 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011947 }
James Smarta58cbd52007-08-02 11:09:43 -040011948 else {
11949 lpfc_debugfs_slow_ring_trc(phba,
11950 "ISR slow ring: pwork:"
11951 "x%x hawork:x%x wait:x%x",
11952 phba->work_ha, work_ha_copy,
11953 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011954 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011955 }
James Smart5b75da22008-12-04 22:39:35 -050011956 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011957 }
11958 }
James Smart5b75da22008-12-04 22:39:35 -050011959 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011960 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011961 if (lpfc_sli_read_hs(phba))
11962 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011963 /*
11964 * Check if there is a deferred error condition
11965 * is active
11966 */
11967 if ((HS_FFER1 & phba->work_hs) &&
11968 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011969 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11970 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011971 phba->hba_flag |= DEFER_ERATT;
11972 /* Clear all interrupt enable conditions */
11973 writel(0, phba->HCregaddr);
11974 readl(phba->HCregaddr);
11975 }
11976 }
11977
James Smart93996272008-08-24 21:50:30 -040011978 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011979 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011980 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011981 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011982 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011983
11984 /* First check out the status word */
11985 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11986 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011987 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011988 /*
11989 * Stray Mailbox Interrupt, mbxCommand <cmd>
11990 * mbxStatus <status>
11991 */
James Smart09372822008-01-11 01:52:54 -050011992 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011993 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011994 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011995 "Interrupt mbxCommand x%x "
11996 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011997 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011998 pmbox->mbxCommand,
11999 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012000 /* clear mailbox attention bit */
12001 work_ha_copy &= ~HA_MBATT;
12002 } else {
James Smart97eab632008-04-07 10:16:05 -040012003 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012004 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012005 phba->last_completion_time = jiffies;
12006 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012007 if (pmb->mbox_cmpl) {
12008 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12009 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012010 if (pmb->out_ext_byte_len &&
12011 pmb->context2)
12012 lpfc_sli_pcimem_bcopy(
12013 phba->mbox_ext,
12014 pmb->context2,
12015 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012016 }
James Smart09372822008-01-11 01:52:54 -050012017 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12018 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12019
12020 lpfc_debugfs_disc_trc(vport,
12021 LPFC_DISC_TRC_MBOX_VPORT,
12022 "MBOX dflt rpi: : "
12023 "status:x%x rpi:x%x",
12024 (uint32_t)pmbox->mbxStatus,
12025 pmbox->un.varWords[0], 0);
12026
12027 if (!pmbox->mbxStatus) {
12028 mp = (struct lpfc_dmabuf *)
12029 (pmb->context1);
12030 ndlp = (struct lpfc_nodelist *)
12031 pmb->context2;
12032
12033 /* Reg_LOGIN of dflt RPI was
12034 * successful. new lets get
12035 * rid of the RPI using the
12036 * same mbox buffer.
12037 */
12038 lpfc_unreg_login(phba,
12039 vport->vpi,
12040 pmbox->un.varWords[0],
12041 pmb);
12042 pmb->mbox_cmpl =
12043 lpfc_mbx_cmpl_dflt_rpi;
12044 pmb->context1 = mp;
12045 pmb->context2 = ndlp;
12046 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012047 rc = lpfc_sli_issue_mbox(phba,
12048 pmb,
12049 MBX_NOWAIT);
12050 if (rc != MBX_BUSY)
12051 lpfc_printf_log(phba,
12052 KERN_ERR,
12053 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012054 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012055 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012056 if (rc != MBX_NOT_FINISHED)
12057 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012058 }
12059 }
James Smart5b75da22008-12-04 22:39:35 -050012060 spin_lock_irqsave(
12061 &phba->pport->work_port_lock,
12062 iflag);
James Smart09372822008-01-11 01:52:54 -050012063 phba->pport->work_port_events &=
12064 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012065 spin_unlock_irqrestore(
12066 &phba->pport->work_port_lock,
12067 iflag);
James Smart09372822008-01-11 01:52:54 -050012068 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012069 }
James Smart97eab632008-04-07 10:16:05 -040012070 } else
James Smart5b75da22008-12-04 22:39:35 -050012071 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012072
James Smart92d7f7b2007-06-17 19:56:38 -050012073 if ((work_ha_copy & HA_MBATT) &&
12074 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012075send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012076 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012077 do {
12078 rc = lpfc_sli_issue_mbox(phba, NULL,
12079 MBX_NOWAIT);
12080 } while (rc == MBX_NOT_FINISHED);
12081 if (rc != MBX_SUCCESS)
12082 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12083 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012084 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012085 }
12086
James Smart5b75da22008-12-04 22:39:35 -050012087 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012088 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012089 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012090 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012091 }
James Smart93996272008-08-24 21:50:30 -040012092 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012093unplug_error:
12094 spin_unlock_irqrestore(&phba->hbalock, iflag);
12095 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012096
James Smart3772a992009-05-22 14:50:54 -040012097} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012098
12099/**
James Smart3772a992009-05-22 14:50:54 -040012100 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012101 * @irq: Interrupt number.
12102 * @dev_id: The device context pointer.
12103 *
12104 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012105 * service routine when device with SLI-3 interface spec is enabled with
12106 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12107 * ring event in the HBA. However, when the device is enabled with either
12108 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12109 * device-level interrupt handler. When the PCI slot is in error recovery
12110 * or the HBA is undergoing initialization, the interrupt handler will not
12111 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12112 * the intrrupt context. This function is called without any lock held.
12113 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012114 *
12115 * This function returns IRQ_HANDLED when interrupt is handled else it
12116 * returns IRQ_NONE.
12117 **/
12118irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012119lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012120{
12121 struct lpfc_hba *phba;
12122 uint32_t ha_copy;
12123 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012124 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012125 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012126
12127 /* Get the driver's phba structure from the dev_id and
12128 * assume the HBA is not interrupting.
12129 */
12130 phba = (struct lpfc_hba *) dev_id;
12131
12132 if (unlikely(!phba))
12133 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012134
12135 /*
James Smart93996272008-08-24 21:50:30 -040012136 * Stuff needs to be attented to when this function is invoked as an
12137 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012138 */
James Smart93996272008-08-24 21:50:30 -040012139 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012140 /* Check device state for handling interrupt */
12141 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012142 return IRQ_NONE;
12143 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012144 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12145 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012146 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012147 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012148 /*
12149 * If there is deferred error attention, do not check for
12150 * any interrupt.
12151 */
12152 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012153 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012154 return IRQ_NONE;
12155 }
James Smart93996272008-08-24 21:50:30 -040012156 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12157 phba->HAregaddr);
12158 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012159 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012160 } else
12161 ha_copy = phba->ha_copy;
12162
12163 /*
12164 * Process all events on FCP ring. Take the optimized path for FCP IO.
12165 */
12166 ha_copy &= ~(phba->work_ha_mask);
12167
12168 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012169 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012170 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012171 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012172 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012173
12174 if (phba->cfg_multi_ring_support == 2) {
12175 /*
James Smart93996272008-08-24 21:50:30 -040012176 * Process all events on extra ring. Take the optimized path
12177 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012178 */
James Smart93996272008-08-24 21:50:30 -040012179 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012180 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012181 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012182 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012183 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012184 status);
12185 }
12186 }
dea31012005-04-17 16:05:31 -050012187 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012188} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012189
James Smart93996272008-08-24 21:50:30 -040012190/**
James Smart3772a992009-05-22 14:50:54 -040012191 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012192 * @irq: Interrupt number.
12193 * @dev_id: The device context pointer.
12194 *
James Smart3772a992009-05-22 14:50:54 -040012195 * This function is the HBA device-level interrupt handler to device with
12196 * SLI-3 interface spec, called from the PCI layer when either MSI or
12197 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12198 * requires driver attention. This function invokes the slow-path interrupt
12199 * attention handling function and fast-path interrupt attention handling
12200 * function in turn to process the relevant HBA attention events. This
12201 * function is called without any lock held. It gets the hbalock to access
12202 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012203 *
12204 * This function returns IRQ_HANDLED when interrupt is handled, else it
12205 * returns IRQ_NONE.
12206 **/
12207irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012208lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012209{
12210 struct lpfc_hba *phba;
12211 irqreturn_t sp_irq_rc, fp_irq_rc;
12212 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012213 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012214
12215 /*
12216 * Get the driver's phba structure from the dev_id and
12217 * assume the HBA is not interrupting.
12218 */
12219 phba = (struct lpfc_hba *) dev_id;
12220
12221 if (unlikely(!phba))
12222 return IRQ_NONE;
12223
James Smart3772a992009-05-22 14:50:54 -040012224 /* Check device state for handling interrupt */
12225 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012226 return IRQ_NONE;
12227
12228 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012229 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12230 spin_unlock(&phba->hbalock);
12231 return IRQ_HANDLED;
12232 }
12233
James Smart93996272008-08-24 21:50:30 -040012234 if (unlikely(!phba->ha_copy)) {
12235 spin_unlock(&phba->hbalock);
12236 return IRQ_NONE;
12237 } else if (phba->ha_copy & HA_ERATT) {
12238 if (phba->hba_flag & HBA_ERATT_HANDLED)
12239 /* ERATT polling has handled ERATT */
12240 phba->ha_copy &= ~HA_ERATT;
12241 else
12242 /* Indicate interrupt handler handles ERATT */
12243 phba->hba_flag |= HBA_ERATT_HANDLED;
12244 }
12245
James Smarta257bf92009-04-06 18:48:10 -040012246 /*
12247 * If there is deferred error attention, do not check for any interrupt.
12248 */
12249 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012250 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012251 return IRQ_NONE;
12252 }
12253
James Smart93996272008-08-24 21:50:30 -040012254 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012255 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12256 spin_unlock(&phba->hbalock);
12257 return IRQ_HANDLED;
12258 }
James Smarta747c9c2009-11-18 15:41:10 -050012259 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12260 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12261 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012262 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012263 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012264 readl(phba->HAregaddr); /* flush */
12265 spin_unlock(&phba->hbalock);
12266
12267 /*
12268 * Invokes slow-path host attention interrupt handling as appropriate.
12269 */
12270
12271 /* status of events with mailbox and link attention */
12272 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12273
12274 /* status of events with ELS ring */
12275 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12276 status2 >>= (4*LPFC_ELS_RING);
12277
12278 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012279 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012280 else
12281 sp_irq_rc = IRQ_NONE;
12282
12283 /*
12284 * Invoke fast-path host attention interrupt handling as appropriate.
12285 */
12286
12287 /* status of events with FCP ring */
12288 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12289 status1 >>= (4*LPFC_FCP_RING);
12290
12291 /* status of events with extra ring */
12292 if (phba->cfg_multi_ring_support == 2) {
12293 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12294 status2 >>= (4*LPFC_EXTRA_RING);
12295 } else
12296 status2 = 0;
12297
12298 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012299 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012300 else
12301 fp_irq_rc = IRQ_NONE;
12302
12303 /* Return device-level interrupt handling status */
12304 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012305} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012306
12307/**
12308 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12309 * @phba: pointer to lpfc hba data structure.
12310 *
12311 * This routine is invoked by the worker thread to process all the pending
12312 * SLI4 FCP abort XRI events.
12313 **/
12314void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12315{
12316 struct lpfc_cq_event *cq_event;
12317
12318 /* First, declare the fcp xri abort event has been handled */
12319 spin_lock_irq(&phba->hbalock);
12320 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12321 spin_unlock_irq(&phba->hbalock);
12322 /* Now, handle all the fcp xri abort events */
12323 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12324 /* Get the first event from the head of the event queue */
12325 spin_lock_irq(&phba->hbalock);
12326 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12327 cq_event, struct lpfc_cq_event, list);
12328 spin_unlock_irq(&phba->hbalock);
12329 /* Notify aborted XRI for FCP work queue */
12330 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12331 /* Free the event processed back to the free pool */
12332 lpfc_sli4_cq_event_release(phba, cq_event);
12333 }
12334}
12335
12336/**
12337 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12338 * @phba: pointer to lpfc hba data structure.
12339 *
12340 * This routine is invoked by the worker thread to process all the pending
12341 * SLI4 els abort xri events.
12342 **/
12343void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12344{
12345 struct lpfc_cq_event *cq_event;
12346
12347 /* First, declare the els xri abort event has been handled */
12348 spin_lock_irq(&phba->hbalock);
12349 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12350 spin_unlock_irq(&phba->hbalock);
12351 /* Now, handle all the els xri abort events */
12352 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12353 /* Get the first event from the head of the event queue */
12354 spin_lock_irq(&phba->hbalock);
12355 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12356 cq_event, struct lpfc_cq_event, list);
12357 spin_unlock_irq(&phba->hbalock);
12358 /* Notify aborted XRI for ELS work queue */
12359 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12360 /* Free the event processed back to the free pool */
12361 lpfc_sli4_cq_event_release(phba, cq_event);
12362 }
12363}
12364
James Smart341af102010-01-26 23:07:37 -050012365/**
12366 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12367 * @phba: pointer to lpfc hba data structure
12368 * @pIocbIn: pointer to the rspiocbq
12369 * @pIocbOut: pointer to the cmdiocbq
12370 * @wcqe: pointer to the complete wcqe
12371 *
12372 * This routine transfers the fields of a command iocbq to a response iocbq
12373 * by copying all the IOCB fields from command iocbq and transferring the
12374 * completion status information from the complete wcqe.
12375 **/
James Smart4f774512009-05-22 14:52:35 -040012376static void
James Smart341af102010-01-26 23:07:37 -050012377lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12378 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012379 struct lpfc_iocbq *pIocbOut,
12380 struct lpfc_wcqe_complete *wcqe)
12381{
James Smartaf227412013-10-10 12:23:10 -040012382 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012383 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012384 uint32_t status, max_response;
12385 struct lpfc_dmabuf *dmabuf;
12386 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012387 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12388
12389 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12390 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012391 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012392 status = bf_get(lpfc_wcqe_c_status, wcqe);
12393 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012394 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12395 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12396 pIocbIn->iocb.un.fcpi.fcpi_parm =
12397 pIocbOut->iocb.un.fcpi.fcpi_parm -
12398 wcqe->total_data_placed;
12399 else
12400 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012401 else {
James Smart4f774512009-05-22 14:52:35 -040012402 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012403 switch (pIocbOut->iocb.ulpCommand) {
12404 case CMD_ELS_REQUEST64_CR:
12405 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12406 bpl = (struct ulp_bde64 *)dmabuf->virt;
12407 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12408 max_response = bde.tus.f.bdeSize;
12409 break;
12410 case CMD_GEN_REQUEST64_CR:
12411 max_response = 0;
12412 if (!pIocbOut->context3)
12413 break;
12414 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12415 sizeof(struct ulp_bde64);
12416 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12417 bpl = (struct ulp_bde64 *)dmabuf->virt;
12418 for (i = 0; i < numBdes; i++) {
12419 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12420 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12421 max_response += bde.tus.f.bdeSize;
12422 }
12423 break;
12424 default:
12425 max_response = wcqe->total_data_placed;
12426 break;
12427 }
12428 if (max_response < wcqe->total_data_placed)
12429 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12430 else
12431 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12432 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012433 }
James Smart341af102010-01-26 23:07:37 -050012434
James Smartacd68592012-01-18 16:25:09 -050012435 /* Convert BG errors for completion status */
12436 if (status == CQE_STATUS_DI_ERROR) {
12437 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12438
12439 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12440 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12441 else
12442 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12443
12444 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12445 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12446 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12447 BGS_GUARD_ERR_MASK;
12448 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12449 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12450 BGS_APPTAG_ERR_MASK;
12451 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12452 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12453 BGS_REFTAG_ERR_MASK;
12454
12455 /* Check to see if there was any good data before the error */
12456 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12457 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12458 BGS_HI_WATER_MARK_PRESENT_MASK;
12459 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12460 wcqe->total_data_placed;
12461 }
12462
12463 /*
12464 * Set ALL the error bits to indicate we don't know what
12465 * type of error it is.
12466 */
12467 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12468 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12469 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12470 BGS_GUARD_ERR_MASK);
12471 }
12472
James Smart341af102010-01-26 23:07:37 -050012473 /* Pick up HBA exchange busy condition */
12474 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12475 spin_lock_irqsave(&phba->hbalock, iflags);
12476 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12477 spin_unlock_irqrestore(&phba->hbalock, iflags);
12478 }
James Smart4f774512009-05-22 14:52:35 -040012479}
12480
12481/**
James Smart45ed1192009-10-02 15:17:02 -040012482 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12483 * @phba: Pointer to HBA context object.
12484 * @wcqe: Pointer to work-queue completion queue entry.
12485 *
12486 * This routine handles an ELS work-queue completion event and construct
12487 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12488 * discovery engine to handle.
12489 *
12490 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12491 **/
12492static struct lpfc_iocbq *
12493lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12494 struct lpfc_iocbq *irspiocbq)
12495{
James Smart895427b2017-02-12 13:52:30 -080012496 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012497 struct lpfc_iocbq *cmdiocbq;
12498 struct lpfc_wcqe_complete *wcqe;
12499 unsigned long iflags;
12500
James Smart895427b2017-02-12 13:52:30 -080012501 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012502 if (unlikely(!pring))
12503 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012504
James Smart45ed1192009-10-02 15:17:02 -040012505 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012506 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012507 pring->stats.iocb_event++;
12508 /* Look up the ELS command IOCB and create pseudo response IOCB */
12509 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12510 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012511 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012512 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012513 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12514 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012515 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12516 wcqe->word0, wcqe->total_data_placed,
12517 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012518 lpfc_sli_release_iocbq(phba, irspiocbq);
12519 return NULL;
12520 }
12521
Dick Kennedy401bb412017-09-29 17:34:28 -070012522 /* Put the iocb back on the txcmplq */
12523 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12524 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12525
James Smart45ed1192009-10-02 15:17:02 -040012526 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012527 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012528
12529 return irspiocbq;
12530}
12531
James Smart8a5ca102017-11-20 16:00:30 -080012532inline struct lpfc_cq_event *
12533lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12534{
12535 struct lpfc_cq_event *cq_event;
12536
12537 /* Allocate a new internal CQ_EVENT entry */
12538 cq_event = lpfc_sli4_cq_event_alloc(phba);
12539 if (!cq_event) {
12540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12541 "0602 Failed to alloc CQ_EVENT entry\n");
12542 return NULL;
12543 }
12544
12545 /* Move the CQE into the event */
12546 memcpy(&cq_event->cqe, entry, size);
12547 return cq_event;
12548}
12549
James Smart45ed1192009-10-02 15:17:02 -040012550/**
James Smart04c68492009-05-22 14:52:52 -040012551 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12552 * @phba: Pointer to HBA context object.
12553 * @cqe: Pointer to mailbox completion queue entry.
12554 *
12555 * This routine process a mailbox completion queue entry with asynchrous
12556 * event.
12557 *
12558 * Return: true if work posted to worker thread, otherwise false.
12559 **/
12560static bool
12561lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12562{
12563 struct lpfc_cq_event *cq_event;
12564 unsigned long iflags;
12565
12566 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12567 "0392 Async Event: word0:x%x, word1:x%x, "
12568 "word2:x%x, word3:x%x\n", mcqe->word0,
12569 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12570
James Smart8a5ca102017-11-20 16:00:30 -080012571 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12572 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012573 return false;
James Smart04c68492009-05-22 14:52:52 -040012574 spin_lock_irqsave(&phba->hbalock, iflags);
12575 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12576 /* Set the async event flag */
12577 phba->hba_flag |= ASYNC_EVENT;
12578 spin_unlock_irqrestore(&phba->hbalock, iflags);
12579
12580 return true;
12581}
12582
12583/**
12584 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12585 * @phba: Pointer to HBA context object.
12586 * @cqe: Pointer to mailbox completion queue entry.
12587 *
12588 * This routine process a mailbox completion queue entry with mailbox
12589 * completion event.
12590 *
12591 * Return: true if work posted to worker thread, otherwise false.
12592 **/
12593static bool
12594lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12595{
12596 uint32_t mcqe_status;
12597 MAILBOX_t *mbox, *pmbox;
12598 struct lpfc_mqe *mqe;
12599 struct lpfc_vport *vport;
12600 struct lpfc_nodelist *ndlp;
12601 struct lpfc_dmabuf *mp;
12602 unsigned long iflags;
12603 LPFC_MBOXQ_t *pmb;
12604 bool workposted = false;
12605 int rc;
12606
12607 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12608 if (!bf_get(lpfc_trailer_completed, mcqe))
12609 goto out_no_mqe_complete;
12610
12611 /* Get the reference to the active mbox command */
12612 spin_lock_irqsave(&phba->hbalock, iflags);
12613 pmb = phba->sli.mbox_active;
12614 if (unlikely(!pmb)) {
12615 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12616 "1832 No pending MBOX command to handle\n");
12617 spin_unlock_irqrestore(&phba->hbalock, iflags);
12618 goto out_no_mqe_complete;
12619 }
12620 spin_unlock_irqrestore(&phba->hbalock, iflags);
12621 mqe = &pmb->u.mqe;
12622 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12623 mbox = phba->mbox;
12624 vport = pmb->vport;
12625
12626 /* Reset heartbeat timer */
12627 phba->last_completion_time = jiffies;
12628 del_timer(&phba->sli.mbox_tmo);
12629
12630 /* Move mbox data to caller's mailbox region, do endian swapping */
12631 if (pmb->mbox_cmpl && mbox)
12632 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012633
James Smart73d91e52011-10-10 21:32:10 -040012634 /*
12635 * For mcqe errors, conditionally move a modified error code to
12636 * the mbox so that the error will not be missed.
12637 */
12638 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12639 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12640 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12641 bf_set(lpfc_mqe_status, mqe,
12642 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12643 }
James Smart04c68492009-05-22 14:52:52 -040012644 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12645 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12646 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12647 "MBOX dflt rpi: status:x%x rpi:x%x",
12648 mcqe_status,
12649 pmbox->un.varWords[0], 0);
12650 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12651 mp = (struct lpfc_dmabuf *)(pmb->context1);
12652 ndlp = (struct lpfc_nodelist *)pmb->context2;
12653 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12654 * RID of the PPI using the same mbox buffer.
12655 */
12656 lpfc_unreg_login(phba, vport->vpi,
12657 pmbox->un.varWords[0], pmb);
12658 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12659 pmb->context1 = mp;
12660 pmb->context2 = ndlp;
12661 pmb->vport = vport;
12662 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12663 if (rc != MBX_BUSY)
12664 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12665 LOG_SLI, "0385 rc should "
12666 "have been MBX_BUSY\n");
12667 if (rc != MBX_NOT_FINISHED)
12668 goto send_current_mbox;
12669 }
12670 }
12671 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12672 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12673 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12674
12675 /* There is mailbox completion work to do */
12676 spin_lock_irqsave(&phba->hbalock, iflags);
12677 __lpfc_mbox_cmpl_put(phba, pmb);
12678 phba->work_ha |= HA_MBATT;
12679 spin_unlock_irqrestore(&phba->hbalock, iflags);
12680 workposted = true;
12681
12682send_current_mbox:
12683 spin_lock_irqsave(&phba->hbalock, iflags);
12684 /* Release the mailbox command posting token */
12685 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12686 /* Setting active mailbox pointer need to be in sync to flag clear */
12687 phba->sli.mbox_active = NULL;
12688 spin_unlock_irqrestore(&phba->hbalock, iflags);
12689 /* Wake up worker thread to post the next pending mailbox command */
12690 lpfc_worker_wake_up(phba);
12691out_no_mqe_complete:
12692 if (bf_get(lpfc_trailer_consumed, mcqe))
12693 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12694 return workposted;
12695}
12696
12697/**
12698 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12699 * @phba: Pointer to HBA context object.
12700 * @cqe: Pointer to mailbox completion queue entry.
12701 *
12702 * This routine process a mailbox completion queue entry, it invokes the
12703 * proper mailbox complete handling or asynchrous event handling routine
12704 * according to the MCQE's async bit.
12705 *
12706 * Return: true if work posted to worker thread, otherwise false.
12707 **/
12708static bool
12709lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12710{
12711 struct lpfc_mcqe mcqe;
12712 bool workposted;
12713
12714 /* Copy the mailbox MCQE and convert endian order as needed */
12715 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12716
12717 /* Invoke the proper event handling routine */
12718 if (!bf_get(lpfc_trailer_async, &mcqe))
12719 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12720 else
12721 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12722 return workposted;
12723}
12724
12725/**
James Smart4f774512009-05-22 14:52:35 -040012726 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12727 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012728 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012729 * @wcqe: Pointer to work-queue completion queue entry.
12730 *
12731 * This routine handles an ELS work-queue completion event.
12732 *
12733 * Return: true if work posted to worker thread, otherwise false.
12734 **/
12735static bool
James Smart2a76a282012-08-03 12:35:54 -040012736lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012737 struct lpfc_wcqe_complete *wcqe)
12738{
James Smart4f774512009-05-22 14:52:35 -040012739 struct lpfc_iocbq *irspiocbq;
12740 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012741 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012742 int txq_cnt = 0;
12743 int txcmplq_cnt = 0;
12744 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012745
James Smart45ed1192009-10-02 15:17:02 -040012746 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012747 irspiocbq = lpfc_sli_get_iocbq(phba);
12748 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012749 if (!list_empty(&pring->txq))
12750 txq_cnt++;
12751 if (!list_empty(&pring->txcmplq))
12752 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012754 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12755 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012756 txq_cnt, phba->iocb_cnt,
12757 fcp_txcmplq_cnt,
12758 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012759 return false;
James Smart4f774512009-05-22 14:52:35 -040012760 }
James Smart4f774512009-05-22 14:52:35 -040012761
James Smart45ed1192009-10-02 15:17:02 -040012762 /* Save off the slow-path queue event for work thread to process */
12763 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012764 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012765 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012766 &phba->sli4_hba.sp_queue_event);
12767 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012768 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012769
James Smart45ed1192009-10-02 15:17:02 -040012770 return true;
James Smart4f774512009-05-22 14:52:35 -040012771}
12772
12773/**
12774 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12775 * @phba: Pointer to HBA context object.
12776 * @wcqe: Pointer to work-queue completion queue entry.
12777 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012778 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012779 * proper WQ release routine to the slow-path WQ.
12780 **/
12781static void
12782lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12783 struct lpfc_wcqe_release *wcqe)
12784{
James Smart2e90f4b2011-12-13 13:22:37 -050012785 /* sanity check on queue memory */
12786 if (unlikely(!phba->sli4_hba.els_wq))
12787 return;
James Smart4f774512009-05-22 14:52:35 -040012788 /* Check for the slow-path ELS work queue */
12789 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12790 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12791 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12792 else
12793 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12794 "2579 Slow-path wqe consume event carries "
12795 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12796 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12797 phba->sli4_hba.els_wq->queue_id);
12798}
12799
12800/**
12801 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12802 * @phba: Pointer to HBA context object.
12803 * @cq: Pointer to a WQ completion queue.
12804 * @wcqe: Pointer to work-queue completion queue entry.
12805 *
12806 * This routine handles an XRI abort event.
12807 *
12808 * Return: true if work posted to worker thread, otherwise false.
12809 **/
12810static bool
12811lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12812 struct lpfc_queue *cq,
12813 struct sli4_wcqe_xri_aborted *wcqe)
12814{
12815 bool workposted = false;
12816 struct lpfc_cq_event *cq_event;
12817 unsigned long iflags;
12818
James Smart4f774512009-05-22 14:52:35 -040012819 switch (cq->subtype) {
12820 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080012821 cq_event = lpfc_cq_event_setup(
12822 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12823 if (!cq_event)
12824 return false;
James Smart4f774512009-05-22 14:52:35 -040012825 spin_lock_irqsave(&phba->hbalock, iflags);
12826 list_add_tail(&cq_event->list,
12827 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12828 /* Set the fcp xri abort event flag */
12829 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12830 spin_unlock_irqrestore(&phba->hbalock, iflags);
12831 workposted = true;
12832 break;
James Smart422c4cb2017-11-20 16:00:32 -080012833 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040012834 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080012835 cq_event = lpfc_cq_event_setup(
12836 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12837 if (!cq_event)
12838 return false;
James Smart4f774512009-05-22 14:52:35 -040012839 spin_lock_irqsave(&phba->hbalock, iflags);
12840 list_add_tail(&cq_event->list,
12841 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12842 /* Set the els xri abort event flag */
12843 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12844 spin_unlock_irqrestore(&phba->hbalock, iflags);
12845 workposted = true;
12846 break;
James Smart318083a2017-03-04 09:30:30 -080012847 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080012848 /* Notify aborted XRI for NVME work queue */
12849 if (phba->nvmet_support)
12850 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12851 else
12852 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12853
12854 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080012855 break;
James Smart4f774512009-05-22 14:52:35 -040012856 default:
12857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012858 "0603 Invalid CQ subtype %d: "
12859 "%08x %08x %08x %08x\n",
12860 cq->subtype, wcqe->word0, wcqe->parameter,
12861 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040012862 workposted = false;
12863 break;
12864 }
12865 return workposted;
12866}
12867
12868/**
James Smart4d9ab992009-10-02 15:16:39 -040012869 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012870 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012871 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012872 *
James Smart4d9ab992009-10-02 15:16:39 -040012873 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012874 *
12875 * Return: true if work posted to worker thread, otherwise false.
12876 **/
12877static bool
James Smart4d9ab992009-10-02 15:16:39 -040012878lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12879{
12880 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012881 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012882 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12883 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012884 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012885 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012886 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012887 unsigned long iflags;
12888
James Smart2e90f4b2011-12-13 13:22:37 -050012889 /* sanity check on queue memory */
12890 if (unlikely(!hrq) || unlikely(!drq))
12891 return workposted;
12892
James Smart7851fe22011-07-22 18:36:52 -040012893 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12894 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12895 else
12896 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12897 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012898 goto out;
12899
12900 status = bf_get(lpfc_rcqe_status, rcqe);
12901 switch (status) {
12902 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12903 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12904 "2537 Receive Frame Truncated!!\n");
12905 case FC_STATUS_RQ_SUCCESS:
12906 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080012907 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012908 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12909 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012910 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012911 spin_unlock_irqrestore(&phba->hbalock, iflags);
12912 goto out;
12913 }
James Smartb84daac2012-08-03 12:35:13 -040012914 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012915 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012916 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012917
12918 /* If a NVME LS event (type 0x28), treat it as Fast path */
12919 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12920
James Smart4d9ab992009-10-02 15:16:39 -040012921 /* save off the frame for the word thread to process */
12922 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012923 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012924 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012925 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012926 spin_unlock_irqrestore(&phba->hbalock, iflags);
12927 workposted = true;
12928 break;
James Smart4d9ab992009-10-02 15:16:39 -040012929 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012930 if (phba->nvmet_support) {
12931 tgtp = phba->targetport->private;
12932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12933 "6402 RQE Error x%x, posted %d err_cnt "
12934 "%d: %x %x %x\n",
12935 status, hrq->RQ_buf_posted,
12936 hrq->RQ_no_posted_buf,
12937 atomic_read(&tgtp->rcv_fcp_cmd_in),
12938 atomic_read(&tgtp->rcv_fcp_cmd_out),
12939 atomic_read(&tgtp->xmt_fcp_release));
12940 }
12941 /* fallthrough */
12942
12943 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012944 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012945 /* Post more buffers if possible */
12946 spin_lock_irqsave(&phba->hbalock, iflags);
12947 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12948 spin_unlock_irqrestore(&phba->hbalock, iflags);
12949 workposted = true;
12950 break;
12951 }
12952out:
12953 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012954}
12955
12956/**
12957 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12958 * @phba: Pointer to HBA context object.
12959 * @cq: Pointer to the completion queue.
12960 * @wcqe: Pointer to a completion queue entry.
12961 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012962 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012963 * entry.
12964 *
12965 * Return: true if work posted to worker thread, otherwise false.
12966 **/
12967static bool
12968lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012969 struct lpfc_cqe *cqe)
12970{
James Smart45ed1192009-10-02 15:17:02 -040012971 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012972 bool workposted = false;
12973
12974 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012975 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012976
12977 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012978 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012979 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012980 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012981 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012982 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012983 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012984 break;
12985 case CQE_CODE_RELEASE_WQE:
12986 /* Process the WQ release event */
12987 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012988 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012989 break;
12990 case CQE_CODE_XRI_ABORTED:
12991 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012992 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012993 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012994 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012995 break;
James Smart4d9ab992009-10-02 15:16:39 -040012996 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012997 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012998 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012999 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013000 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013001 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013002 break;
James Smart4f774512009-05-22 14:52:35 -040013003 default:
13004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13005 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013006 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013007 break;
13008 }
13009 return workposted;
13010}
13011
13012/**
James Smart4f774512009-05-22 14:52:35 -040013013 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13014 * @phba: Pointer to HBA context object.
13015 * @eqe: Pointer to fast-path event queue entry.
13016 *
13017 * This routine process a event queue entry from the slow-path event queue.
13018 * It will check the MajorCode and MinorCode to determine this is for a
13019 * completion event on a completion queue, if not, an error shall be logged
13020 * and just return. Otherwise, it will get to the corresponding completion
13021 * queue and process all the entries on that completion queue, rearm the
13022 * completion queue, and then return.
13023 *
13024 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013025static void
James Smart67d12732012-08-03 12:36:13 -040013026lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13027 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013028{
James Smart67d12732012-08-03 12:36:13 -040013029 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013030 uint16_t cqid;
13031
James Smart4f774512009-05-22 14:52:35 -040013032 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013033 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013034
James Smart4f774512009-05-22 14:52:35 -040013035 list_for_each_entry(childq, &speq->child_list, list) {
13036 if (childq->queue_id == cqid) {
13037 cq = childq;
13038 break;
13039 }
13040 }
13041 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013042 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13044 "0365 Slow-path CQ identifier "
13045 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013046 return;
James Smart4f774512009-05-22 14:52:35 -040013047 }
13048
James Smart895427b2017-02-12 13:52:30 -080013049 /* Save EQ associated with this CQ */
13050 cq->assoc_qp = speq;
13051
Dick Kennedyf485c182017-09-29 17:34:34 -070013052 if (!queue_work(phba->wq, &cq->spwork))
13053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13054 "0390 Cannot schedule soft IRQ "
13055 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13056 cqid, cq->queue_id, smp_processor_id());
13057}
13058
13059/**
13060 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13061 * @phba: Pointer to HBA context object.
13062 *
13063 * This routine process a event queue entry from the slow-path event queue.
13064 * It will check the MajorCode and MinorCode to determine this is for a
13065 * completion event on a completion queue, if not, an error shall be logged
13066 * and just return. Otherwise, it will get to the corresponding completion
13067 * queue and process all the entries on that completion queue, rearm the
13068 * completion queue, and then return.
13069 *
13070 **/
13071static void
13072lpfc_sli4_sp_process_cq(struct work_struct *work)
13073{
13074 struct lpfc_queue *cq =
13075 container_of(work, struct lpfc_queue, spwork);
13076 struct lpfc_hba *phba = cq->phba;
13077 struct lpfc_cqe *cqe;
13078 bool workposted = false;
13079 int ccount = 0;
13080
James Smart4f774512009-05-22 14:52:35 -040013081 /* Process all the entries to the CQ */
13082 switch (cq->type) {
13083 case LPFC_MCQ:
13084 while ((cqe = lpfc_sli4_cq_get(cq))) {
13085 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013086 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013087 break;
James Smartb84daac2012-08-03 12:35:13 -040013088 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013089 }
13090 break;
13091 case LPFC_WCQ:
13092 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013093 if (cq->subtype == LPFC_FCP ||
13094 cq->subtype == LPFC_NVME) {
13095#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13096 if (phba->ktime_on)
13097 cq->isr_timestamp = ktime_get_ns();
13098 else
13099 cq->isr_timestamp = 0;
13100#endif
James Smart895427b2017-02-12 13:52:30 -080013101 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013102 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013103 } else {
James Smart05580562011-05-24 11:40:48 -040013104 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13105 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013106 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013107 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013108 break;
James Smart4f774512009-05-22 14:52:35 -040013109 }
James Smartb84daac2012-08-03 12:35:13 -040013110
13111 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013112 if (ccount > cq->CQ_max_cqe)
13113 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013114 break;
13115 default:
13116 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13117 "0370 Invalid completion queue type (%d)\n",
13118 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013119 return;
James Smart4f774512009-05-22 14:52:35 -040013120 }
13121
13122 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013123 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13125 "0371 No entry from the CQ: identifier "
13126 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13127
13128 /* In any case, flash and re-arm the RCQ */
13129 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13130
13131 /* wake up worker thread if there are works to be done */
13132 if (workposted)
13133 lpfc_worker_wake_up(phba);
13134}
13135
13136/**
13137 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013138 * @phba: Pointer to HBA context object.
13139 * @cq: Pointer to associated CQ
13140 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013141 *
13142 * This routine process a fast-path work queue completion entry from fast-path
13143 * event queue for FCP command response completion.
13144 **/
13145static void
James Smart2a76a282012-08-03 12:35:54 -040013146lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013147 struct lpfc_wcqe_complete *wcqe)
13148{
James Smart2a76a282012-08-03 12:35:54 -040013149 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013150 struct lpfc_iocbq *cmdiocbq;
13151 struct lpfc_iocbq irspiocbq;
13152 unsigned long iflags;
13153
James Smart4f774512009-05-22 14:52:35 -040013154 /* Check for response status */
13155 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13156 /* If resource errors reported from HBA, reduce queue
13157 * depth of the SCSI device.
13158 */
James Smarte3d2b802012-08-14 14:25:43 -040013159 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13160 IOSTAT_LOCAL_REJECT)) &&
13161 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13162 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013163 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013164
James Smart4f774512009-05-22 14:52:35 -040013165 /* Log the error status */
13166 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13167 "0373 FCP complete error: status=x%x, "
13168 "hw_status=x%x, total_data_specified=%d, "
13169 "parameter=x%x, word3=x%x\n",
13170 bf_get(lpfc_wcqe_c_status, wcqe),
13171 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13172 wcqe->total_data_placed, wcqe->parameter,
13173 wcqe->word3);
13174 }
13175
13176 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013177 spin_lock_irqsave(&pring->ring_lock, iflags);
13178 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013179 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13180 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013181 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013182 if (unlikely(!cmdiocbq)) {
13183 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13184 "0374 FCP complete with no corresponding "
13185 "cmdiocb: iotag (%d)\n",
13186 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13187 return;
13188 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013189#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13190 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13191#endif
James Smart895427b2017-02-12 13:52:30 -080013192 if (cmdiocbq->iocb_cmpl == NULL) {
13193 if (cmdiocbq->wqe_cmpl) {
13194 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13195 spin_lock_irqsave(&phba->hbalock, iflags);
13196 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13197 spin_unlock_irqrestore(&phba->hbalock, iflags);
13198 }
13199
13200 /* Pass the cmd_iocb and the wcqe to the upper layer */
13201 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13202 return;
13203 }
James Smart4f774512009-05-22 14:52:35 -040013204 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13205 "0375 FCP cmdiocb not callback function "
13206 "iotag: (%d)\n",
13207 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13208 return;
13209 }
13210
13211 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013212 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013213
James Smart0f65ff62010-02-26 14:14:23 -050013214 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13215 spin_lock_irqsave(&phba->hbalock, iflags);
13216 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13217 spin_unlock_irqrestore(&phba->hbalock, iflags);
13218 }
13219
James Smart4f774512009-05-22 14:52:35 -040013220 /* Pass the cmd_iocb and the rsp state to the upper layer */
13221 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13222}
13223
13224/**
13225 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13226 * @phba: Pointer to HBA context object.
13227 * @cq: Pointer to completion queue.
13228 * @wcqe: Pointer to work-queue completion queue entry.
13229 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013230 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013231 * proper WQ release routine to the slow-path WQ.
13232 **/
13233static void
13234lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13235 struct lpfc_wcqe_release *wcqe)
13236{
13237 struct lpfc_queue *childwq;
13238 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013239 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013240
13241 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013242 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013243 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013244 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013245 lpfc_sli4_wq_release(childwq,
13246 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013247 if (childwq->q_flag & HBA_NVMET_WQFULL)
13248 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013249 wqid_matched = true;
13250 break;
13251 }
13252 }
13253 /* Report warning log message if no match found */
13254 if (wqid_matched != true)
13255 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13256 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013257 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013258}
13259
13260/**
James Smart2d7dbc42017-02-12 13:52:35 -080013261 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13262 * @phba: Pointer to HBA context object.
13263 * @rcqe: Pointer to receive-queue completion queue entry.
13264 *
13265 * This routine process a receive-queue completion queue entry.
13266 *
13267 * Return: true if work posted to worker thread, otherwise false.
13268 **/
13269static bool
13270lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13271 struct lpfc_rcqe *rcqe)
13272{
13273 bool workposted = false;
13274 struct lpfc_queue *hrq;
13275 struct lpfc_queue *drq;
13276 struct rqb_dmabuf *dma_buf;
13277 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013278 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013279 uint32_t status, rq_id;
13280 unsigned long iflags;
13281 uint32_t fctl, idx;
13282
13283 if ((phba->nvmet_support == 0) ||
13284 (phba->sli4_hba.nvmet_cqset == NULL))
13285 return workposted;
13286
13287 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13288 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13289 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13290
13291 /* sanity check on queue memory */
13292 if (unlikely(!hrq) || unlikely(!drq))
13293 return workposted;
13294
13295 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13296 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13297 else
13298 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13299
13300 if ((phba->nvmet_support == 0) ||
13301 (rq_id != hrq->queue_id))
13302 return workposted;
13303
13304 status = bf_get(lpfc_rcqe_status, rcqe);
13305 switch (status) {
13306 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13307 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13308 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013309 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013310 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013311 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013312 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013313 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13314 if (!dma_buf) {
13315 hrq->RQ_no_buf_found++;
13316 spin_unlock_irqrestore(&phba->hbalock, iflags);
13317 goto out;
13318 }
13319 spin_unlock_irqrestore(&phba->hbalock, iflags);
13320 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013321 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013322 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13323
13324 /* Just some basic sanity checks on FCP Command frame */
13325 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13326 fc_hdr->fh_f_ctl[1] << 8 |
13327 fc_hdr->fh_f_ctl[2]);
13328 if (((fctl &
13329 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13330 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13331 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13332 goto drop;
13333
13334 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13335 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013336 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013337 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013338 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013339 return false;
13340 }
13341drop:
13342 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13343 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013344 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013345 if (phba->nvmet_support) {
13346 tgtp = phba->targetport->private;
13347 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13348 "6401 RQE Error x%x, posted %d err_cnt "
13349 "%d: %x %x %x\n",
13350 status, hrq->RQ_buf_posted,
13351 hrq->RQ_no_posted_buf,
13352 atomic_read(&tgtp->rcv_fcp_cmd_in),
13353 atomic_read(&tgtp->rcv_fcp_cmd_out),
13354 atomic_read(&tgtp->xmt_fcp_release));
13355 }
13356 /* fallthrough */
13357
13358 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013359 hrq->RQ_no_posted_buf++;
13360 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013361 break;
13362 }
13363out:
13364 return workposted;
13365}
13366
13367/**
James Smart895427b2017-02-12 13:52:30 -080013368 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013369 * @cq: Pointer to the completion queue.
13370 * @eqe: Pointer to fast-path completion queue entry.
13371 *
13372 * This routine process a fast-path work queue completion entry from fast-path
13373 * event queue for FCP command response completion.
13374 **/
13375static int
James Smart895427b2017-02-12 13:52:30 -080013376lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013377 struct lpfc_cqe *cqe)
13378{
13379 struct lpfc_wcqe_release wcqe;
13380 bool workposted = false;
13381
13382 /* Copy the work queue CQE and convert endian order if needed */
13383 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13384
13385 /* Check and process for different type of WCQE and dispatch */
13386 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13387 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013388 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013389 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013390 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013391 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013392 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13393 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13394 (struct lpfc_wcqe_complete *)&wcqe);
13395 if (cq->subtype == LPFC_NVME_LS)
13396 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013397 (struct lpfc_wcqe_complete *)&wcqe);
13398 break;
13399 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013400 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013401 /* Process the WQ release event */
13402 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13403 (struct lpfc_wcqe_release *)&wcqe);
13404 break;
13405 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013406 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013407 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013408 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013409 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13410 (struct sli4_wcqe_xri_aborted *)&wcqe);
13411 break;
James Smart895427b2017-02-12 13:52:30 -080013412 case CQE_CODE_RECEIVE_V1:
13413 case CQE_CODE_RECEIVE:
13414 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013415 if (cq->subtype == LPFC_NVMET) {
13416 workposted = lpfc_sli4_nvmet_handle_rcqe(
13417 phba, cq, (struct lpfc_rcqe *)&wcqe);
13418 }
James Smart895427b2017-02-12 13:52:30 -080013419 break;
James Smart4f774512009-05-22 14:52:35 -040013420 default:
13421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013422 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013423 bf_get(lpfc_wcqe_c_code, &wcqe));
13424 break;
13425 }
13426 return workposted;
13427}
13428
13429/**
James Smart67d12732012-08-03 12:36:13 -040013430 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013431 * @phba: Pointer to HBA context object.
13432 * @eqe: Pointer to fast-path event queue entry.
13433 *
13434 * This routine process a event queue entry from the fast-path event queue.
13435 * It will check the MajorCode and MinorCode to determine this is for a
13436 * completion event on a completion queue, if not, an error shall be logged
13437 * and just return. Otherwise, it will get to the corresponding completion
13438 * queue and process all the entries on the completion queue, rearm the
13439 * completion queue, and then return.
13440 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013441static void
James Smart67d12732012-08-03 12:36:13 -040013442lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13443 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013444{
James Smart895427b2017-02-12 13:52:30 -080013445 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013446 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013447
James Smartcb5172e2010-03-15 11:25:07 -040013448 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013449 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013450 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013451 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013452 bf_get_le32(lpfc_eqe_major_code, eqe),
13453 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013454 return;
James Smart4f774512009-05-22 14:52:35 -040013455 }
13456
James Smart67d12732012-08-03 12:36:13 -040013457 /* Get the reference to the corresponding CQ */
13458 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13459
James Smart2d7dbc42017-02-12 13:52:35 -080013460 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13461 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13462 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13463 /* Process NVMET unsol rcv */
13464 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13465 goto process_cq;
13466 }
13467 }
13468
James Smart895427b2017-02-12 13:52:30 -080013469 if (phba->sli4_hba.nvme_cq_map &&
13470 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013471 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013472 cq = phba->sli4_hba.nvme_cq[qidx];
13473 goto process_cq;
13474 }
13475
13476 if (phba->sli4_hba.fcp_cq_map &&
13477 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13478 /* Process FCP command completion */
13479 cq = phba->sli4_hba.fcp_cq[qidx];
13480 goto process_cq;
13481 }
13482
13483 if (phba->sli4_hba.nvmels_cq &&
13484 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13485 /* Process NVME unsol rcv */
13486 cq = phba->sli4_hba.nvmels_cq;
13487 }
13488
13489 /* Otherwise this is a Slow path event */
13490 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013491 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13492 return;
James Smart67d12732012-08-03 12:36:13 -040013493 }
13494
James Smart895427b2017-02-12 13:52:30 -080013495process_cq:
James Smart4f774512009-05-22 14:52:35 -040013496 if (unlikely(cqid != cq->queue_id)) {
13497 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13498 "0368 Miss-matched fast-path completion "
13499 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13500 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013501 return;
James Smart4f774512009-05-22 14:52:35 -040013502 }
13503
James Smart895427b2017-02-12 13:52:30 -080013504 /* Save EQ associated with this CQ */
13505 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13506
Dick Kennedyf485c182017-09-29 17:34:34 -070013507 if (!queue_work(phba->wq, &cq->irqwork))
13508 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13509 "0363 Cannot schedule soft IRQ "
13510 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13511 cqid, cq->queue_id, smp_processor_id());
13512}
13513
13514/**
13515 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13516 * @phba: Pointer to HBA context object.
13517 * @eqe: Pointer to fast-path event queue entry.
13518 *
13519 * This routine process a event queue entry from the fast-path event queue.
13520 * It will check the MajorCode and MinorCode to determine this is for a
13521 * completion event on a completion queue, if not, an error shall be logged
13522 * and just return. Otherwise, it will get to the corresponding completion
13523 * queue and process all the entries on the completion queue, rearm the
13524 * completion queue, and then return.
13525 **/
13526static void
13527lpfc_sli4_hba_process_cq(struct work_struct *work)
13528{
13529 struct lpfc_queue *cq =
13530 container_of(work, struct lpfc_queue, irqwork);
13531 struct lpfc_hba *phba = cq->phba;
13532 struct lpfc_cqe *cqe;
13533 bool workposted = false;
13534 int ccount = 0;
13535
James Smart4f774512009-05-22 14:52:35 -040013536 /* Process all the entries to the CQ */
13537 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013538#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13539 if (phba->ktime_on)
13540 cq->isr_timestamp = ktime_get_ns();
13541 else
13542 cq->isr_timestamp = 0;
13543#endif
James Smart895427b2017-02-12 13:52:30 -080013544 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013545 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013546 break;
James Smart4f774512009-05-22 14:52:35 -040013547 }
13548
James Smartb84daac2012-08-03 12:35:13 -040013549 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013550 if (ccount > cq->CQ_max_cqe)
13551 cq->CQ_max_cqe = ccount;
13552 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013553
James Smart4f774512009-05-22 14:52:35 -040013554 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013555 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13557 "0369 No entry from fast-path completion "
13558 "queue fcpcqid=%d\n", cq->queue_id);
13559
13560 /* In any case, flash and re-arm the CQ */
13561 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13562
13563 /* wake up worker thread if there are works to be done */
13564 if (workposted)
13565 lpfc_worker_wake_up(phba);
13566}
13567
13568static void
13569lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13570{
13571 struct lpfc_eqe *eqe;
13572
13573 /* walk all the EQ entries and drop on the floor */
13574 while ((eqe = lpfc_sli4_eq_get(eq)))
13575 ;
13576
13577 /* Clear and re-arm the EQ */
13578 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13579}
13580
James Smart1ba981f2014-02-20 09:56:45 -050013581
13582/**
13583 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13584 * entry
13585 * @phba: Pointer to HBA context object.
13586 * @eqe: Pointer to fast-path event queue entry.
13587 *
13588 * This routine process a event queue entry from the Flash Optimized Fabric
13589 * event queue. It will check the MajorCode and MinorCode to determine this
13590 * is for a completion event on a completion queue, if not, an error shall be
13591 * logged and just return. Otherwise, it will get to the corresponding
13592 * completion queue and process all the entries on the completion queue, rearm
13593 * the completion queue, and then return.
13594 **/
13595static void
13596lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13597{
13598 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013599 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013600
13601 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13603 "9147 Not a valid completion "
13604 "event: majorcode=x%x, minorcode=x%x\n",
13605 bf_get_le32(lpfc_eqe_major_code, eqe),
13606 bf_get_le32(lpfc_eqe_minor_code, eqe));
13607 return;
13608 }
13609
13610 /* Get the reference to the corresponding CQ */
13611 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13612
13613 /* Next check for OAS */
13614 cq = phba->sli4_hba.oas_cq;
13615 if (unlikely(!cq)) {
13616 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13617 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13618 "9148 OAS completion queue "
13619 "does not exist\n");
13620 return;
13621 }
13622
13623 if (unlikely(cqid != cq->queue_id)) {
13624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13625 "9149 Miss-matched fast-path compl "
13626 "queue id: eqcqid=%d, fcpcqid=%d\n",
13627 cqid, cq->queue_id);
13628 return;
13629 }
13630
James Smartd41b65b2017-06-15 22:56:41 -070013631 /* Save EQ associated with this CQ */
13632 cq->assoc_qp = phba->sli4_hba.fof_eq;
13633
Dick Kennedyf485c182017-09-29 17:34:34 -070013634 /* CQ work will be processed on CPU affinitized to this IRQ */
13635 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013637 "0367 Cannot schedule soft IRQ "
13638 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13639 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013640}
13641
13642/**
13643 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13644 * @irq: Interrupt number.
13645 * @dev_id: The device context pointer.
13646 *
13647 * This function is directly called from the PCI layer as an interrupt
13648 * service routine when device with SLI-4 interface spec is enabled with
13649 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13650 * IOCB ring event in the HBA. However, when the device is enabled with either
13651 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13652 * device-level interrupt handler. When the PCI slot is in error recovery
13653 * or the HBA is undergoing initialization, the interrupt handler will not
13654 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13655 * the intrrupt context. This function is called without any lock held.
13656 * It gets the hbalock to access and update SLI data structures. Note that,
13657 * the EQ to CQ are one-to-one map such that the EQ index is
13658 * equal to that of CQ index.
13659 *
13660 * This function returns IRQ_HANDLED when interrupt is handled else it
13661 * returns IRQ_NONE.
13662 **/
13663irqreturn_t
13664lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13665{
13666 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013667 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013668 struct lpfc_queue *eq;
13669 struct lpfc_eqe *eqe;
13670 unsigned long iflag;
13671 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013672
13673 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013674 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13675 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013676
13677 if (unlikely(!phba))
13678 return IRQ_NONE;
13679
13680 /* Get to the EQ struct associated with this vector */
13681 eq = phba->sli4_hba.fof_eq;
13682 if (unlikely(!eq))
13683 return IRQ_NONE;
13684
13685 /* Check device state for handling interrupt */
13686 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013687 /* Check again for link_state with lock held */
13688 spin_lock_irqsave(&phba->hbalock, iflag);
13689 if (phba->link_state < LPFC_LINK_DOWN)
13690 /* Flush, clear interrupt, and rearm the EQ */
13691 lpfc_sli4_eq_flush(phba, eq);
13692 spin_unlock_irqrestore(&phba->hbalock, iflag);
13693 return IRQ_NONE;
13694 }
13695
13696 /*
13697 * Process all the event on FCP fast-path EQ
13698 */
13699 while ((eqe = lpfc_sli4_eq_get(eq))) {
13700 lpfc_sli4_fof_handle_eqe(phba, eqe);
13701 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013702 break;
James Smart1ba981f2014-02-20 09:56:45 -050013703 eq->EQ_processed++;
13704 }
13705
13706 /* Track the max number of EQEs processed in 1 intr */
13707 if (ecount > eq->EQ_max_eqe)
13708 eq->EQ_max_eqe = ecount;
13709
13710
13711 if (unlikely(ecount == 0)) {
13712 eq->EQ_no_entry++;
13713
13714 if (phba->intr_type == MSIX)
13715 /* MSI-X treated interrupt served as no EQ share INT */
13716 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13717 "9145 MSI-X interrupt with no EQE\n");
13718 else {
13719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13720 "9146 ISR interrupt with no EQE\n");
13721 /* Non MSI-X treated on interrupt as EQ share INT */
13722 return IRQ_NONE;
13723 }
13724 }
13725 /* Always clear and re-arm the fast-path EQ */
13726 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13727 return IRQ_HANDLED;
13728}
13729
James Smart4f774512009-05-22 14:52:35 -040013730/**
James Smart67d12732012-08-03 12:36:13 -040013731 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013732 * @irq: Interrupt number.
13733 * @dev_id: The device context pointer.
13734 *
13735 * This function is directly called from the PCI layer as an interrupt
13736 * service routine when device with SLI-4 interface spec is enabled with
13737 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13738 * ring event in the HBA. However, when the device is enabled with either
13739 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13740 * device-level interrupt handler. When the PCI slot is in error recovery
13741 * or the HBA is undergoing initialization, the interrupt handler will not
13742 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13743 * the intrrupt context. This function is called without any lock held.
13744 * It gets the hbalock to access and update SLI data structures. Note that,
13745 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13746 * equal to that of FCP CQ index.
13747 *
James Smart67d12732012-08-03 12:36:13 -040013748 * The link attention and ELS ring attention events are handled
13749 * by the worker thread. The interrupt handler signals the worker thread
13750 * and returns for these events. This function is called without any lock
13751 * held. It gets the hbalock to access and update SLI data structures.
13752 *
James Smart4f774512009-05-22 14:52:35 -040013753 * This function returns IRQ_HANDLED when interrupt is handled else it
13754 * returns IRQ_NONE.
13755 **/
13756irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013757lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013758{
13759 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013760 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013761 struct lpfc_queue *fpeq;
13762 struct lpfc_eqe *eqe;
13763 unsigned long iflag;
13764 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013765 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013766
13767 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013768 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13769 phba = hba_eq_hdl->phba;
13770 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013771
13772 if (unlikely(!phba))
13773 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013774 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013775 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013776
13777 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013778 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013779 if (unlikely(!fpeq))
13780 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013781
James Smartba20c852012-08-03 12:36:52 -040013782 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013783 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013784 lpfc_sli4_eq_clr_intr(fpeq);
13785 else {
James Smart895427b2017-02-12 13:52:30 -080013786 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013787 return IRQ_NONE;
13788 }
13789 }
13790
James Smart4f774512009-05-22 14:52:35 -040013791 /* Check device state for handling interrupt */
13792 if (unlikely(lpfc_intr_state_check(phba))) {
13793 /* Check again for link_state with lock held */
13794 spin_lock_irqsave(&phba->hbalock, iflag);
13795 if (phba->link_state < LPFC_LINK_DOWN)
13796 /* Flush, clear interrupt, and rearm the EQ */
13797 lpfc_sli4_eq_flush(phba, fpeq);
13798 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013799 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013800 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013801 return IRQ_NONE;
13802 }
13803
13804 /*
13805 * Process all the event on FCP fast-path EQ
13806 */
13807 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013808 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13809 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013810 break;
James Smartb84daac2012-08-03 12:35:13 -040013811 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013812 }
13813
James Smartb84daac2012-08-03 12:35:13 -040013814 /* Track the max number of EQEs processed in 1 intr */
13815 if (ecount > fpeq->EQ_max_eqe)
13816 fpeq->EQ_max_eqe = ecount;
13817
James Smart4f774512009-05-22 14:52:35 -040013818 /* Always clear and re-arm the fast-path EQ */
13819 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13820
13821 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013822 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013823
13824 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013825 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013826 return IRQ_NONE;
13827 }
13828
James Smart4f774512009-05-22 14:52:35 -040013829 if (phba->intr_type == MSIX)
13830 /* MSI-X treated interrupt served as no EQ share INT */
13831 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13832 "0358 MSI-X interrupt with no EQE\n");
13833 else
13834 /* Non MSI-X treated on interrupt as EQ share INT */
13835 return IRQ_NONE;
13836 }
13837
James Smartba20c852012-08-03 12:36:52 -040013838 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013839 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13840
James Smart4f774512009-05-22 14:52:35 -040013841 return IRQ_HANDLED;
13842} /* lpfc_sli4_fp_intr_handler */
13843
13844/**
13845 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13846 * @irq: Interrupt number.
13847 * @dev_id: The device context pointer.
13848 *
13849 * This function is the device-level interrupt handler to device with SLI-4
13850 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13851 * interrupt mode is enabled and there is an event in the HBA which requires
13852 * driver attention. This function invokes the slow-path interrupt attention
13853 * handling function and fast-path interrupt attention handling function in
13854 * turn to process the relevant HBA attention events. This function is called
13855 * without any lock held. It gets the hbalock to access and update SLI data
13856 * structures.
13857 *
13858 * This function returns IRQ_HANDLED when interrupt is handled, else it
13859 * returns IRQ_NONE.
13860 **/
13861irqreturn_t
13862lpfc_sli4_intr_handler(int irq, void *dev_id)
13863{
13864 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013865 irqreturn_t hba_irq_rc;
13866 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013867 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013868
13869 /* Get the driver's phba structure from the dev_id */
13870 phba = (struct lpfc_hba *)dev_id;
13871
13872 if (unlikely(!phba))
13873 return IRQ_NONE;
13874
13875 /*
James Smart4f774512009-05-22 14:52:35 -040013876 * Invoke fast-path host attention interrupt handling as appropriate.
13877 */
James Smart895427b2017-02-12 13:52:30 -080013878 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013879 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013880 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013881 if (hba_irq_rc == IRQ_HANDLED)
13882 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013883 }
13884
James Smart1ba981f2014-02-20 09:56:45 -050013885 if (phba->cfg_fof) {
13886 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013887 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013888 if (hba_irq_rc == IRQ_HANDLED)
13889 hba_handled |= true;
13890 }
13891
James Smart67d12732012-08-03 12:36:13 -040013892 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013893} /* lpfc_sli4_intr_handler */
13894
13895/**
13896 * lpfc_sli4_queue_free - free a queue structure and associated memory
13897 * @queue: The queue structure to free.
13898 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013899 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013900 * the host resident queue. This function must be called after destroying the
13901 * queue on the HBA.
13902 **/
13903void
13904lpfc_sli4_queue_free(struct lpfc_queue *queue)
13905{
13906 struct lpfc_dmabuf *dmabuf;
13907
13908 if (!queue)
13909 return;
13910
13911 while (!list_empty(&queue->page_list)) {
13912 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13913 list);
James Smart81b96ed2017-11-20 16:00:29 -080013914 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040013915 dmabuf->virt, dmabuf->phys);
13916 kfree(dmabuf);
13917 }
James Smart895427b2017-02-12 13:52:30 -080013918 if (queue->rqbp) {
13919 lpfc_free_rq_buffer(queue->phba, queue);
13920 kfree(queue->rqbp);
13921 }
James Smartd1f525a2017-04-21 16:04:55 -070013922
13923 if (!list_empty(&queue->wq_list))
13924 list_del(&queue->wq_list);
13925
James Smart4f774512009-05-22 14:52:35 -040013926 kfree(queue);
13927 return;
13928}
13929
13930/**
13931 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13932 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080013933 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040013934 * @entry_size: The size of each queue entry for this queue.
13935 * @entry count: The number of entries that this queue will handle.
13936 *
13937 * This function allocates a queue structure and the DMAable memory used for
13938 * the host resident queue. This function must be called before creating the
13939 * queue on the HBA.
13940 **/
13941struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080013942lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13943 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040013944{
13945 struct lpfc_queue *queue;
13946 struct lpfc_dmabuf *dmabuf;
13947 int x, total_qe_count;
13948 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013949 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013950
James Smartcb5172e2010-03-15 11:25:07 -040013951 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080013952 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040013953
James Smart4f774512009-05-22 14:52:35 -040013954 queue = kzalloc(sizeof(struct lpfc_queue) +
13955 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13956 if (!queue)
13957 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013958 queue->page_count = (ALIGN(entry_size * entry_count,
13959 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013960
13961 /* If needed, Adjust page count to match the max the adapter supports */
13962 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13963 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13964
James Smart4f774512009-05-22 14:52:35 -040013965 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013966 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080013967 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040013968 INIT_LIST_HEAD(&queue->page_list);
13969 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080013970
13971 /* Set queue parameters now. If the system cannot provide memory
13972 * resources, the free routine needs to know what was allocated.
13973 */
13974 queue->entry_size = entry_size;
13975 queue->entry_count = entry_count;
13976 queue->page_size = hw_page_size;
13977 queue->phba = phba;
13978
James Smart4f774512009-05-22 14:52:35 -040013979 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13980 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13981 if (!dmabuf)
13982 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013983 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13984 hw_page_size, &dmabuf->phys,
13985 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013986 if (!dmabuf->virt) {
13987 kfree(dmabuf);
13988 goto out_fail;
13989 }
13990 dmabuf->buffer_tag = x;
13991 list_add_tail(&dmabuf->list, &queue->page_list);
13992 /* initialize queue's entry array */
13993 dma_pointer = dmabuf->virt;
13994 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013995 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013996 total_qe_count++, dma_pointer += entry_size) {
13997 queue->qe[total_qe_count].address = dma_pointer;
13998 }
13999 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014000 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14001 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014002
James Smart64eb4dc2017-05-15 15:20:49 -070014003 /* entry_repost will be set during q creation */
14004
James Smart4f774512009-05-22 14:52:35 -040014005 return queue;
14006out_fail:
14007 lpfc_sli4_queue_free(queue);
14008 return NULL;
14009}
14010
14011/**
James Smart962bc512013-01-03 15:44:00 -050014012 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14013 * @phba: HBA structure that indicates port to create a queue on.
14014 * @pci_barset: PCI BAR set flag.
14015 *
14016 * This function shall perform iomap of the specified PCI BAR address to host
14017 * memory address if not already done so and return it. The returned host
14018 * memory address can be NULL.
14019 */
14020static void __iomem *
14021lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14022{
James Smart962bc512013-01-03 15:44:00 -050014023 if (!phba->pcidev)
14024 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014025
14026 switch (pci_barset) {
14027 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014028 return phba->pci_bar0_memmap_p;
14029 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014030 return phba->pci_bar2_memmap_p;
14031 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014032 return phba->pci_bar4_memmap_p;
14033 default:
14034 break;
14035 }
14036 return NULL;
14037}
14038
14039/**
James Smart895427b2017-02-12 13:52:30 -080014040 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014041 * @phba: HBA structure that indicates port to create a queue on.
14042 * @startq: The starting FCP EQ to modify
14043 *
14044 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014045 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14046 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014047 *
14048 * The @phba struct is used to send mailbox command to HBA. The @startq
14049 * is used to get the starting FCP EQ to change.
14050 * This function is asynchronous and will wait for the mailbox
14051 * command to finish before continuing.
14052 *
14053 * On success this function will return a zero. If unable to allocate enough
14054 * memory this function will return -ENOMEM. If the queue create mailbox command
14055 * fails this function will return -ENXIO.
14056 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014057int
James Smart0cf07f842017-06-01 21:07:10 -070014058lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14059 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014060{
14061 struct lpfc_mbx_modify_eq_delay *eq_delay;
14062 LPFC_MBOXQ_t *mbox;
14063 struct lpfc_queue *eq;
14064 int cnt, rc, length, status = 0;
14065 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014066 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014067 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014068 union lpfc_sli4_cfg_shdr *shdr;
14069 uint16_t dmult;
14070
James Smart895427b2017-02-12 13:52:30 -080014071 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014072 return 0;
14073
14074 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14075 if (!mbox)
14076 return -ENOMEM;
14077 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14078 sizeof(struct lpfc_sli4_cfg_mhdr));
14079 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14080 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14081 length, LPFC_SLI4_MBX_EMBED);
14082 eq_delay = &mbox->u.mqe.un.eq_delay;
14083
14084 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014085 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014086 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014087 dmult = 0;
14088 else
14089 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014090 if (dmult > LPFC_DMULT_MAX)
14091 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014092
14093 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014094 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14095 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014096 if (!eq)
14097 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014098 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014099 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14100 eq_delay->u.request.eq[cnt].phase = 0;
14101 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14102 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014103
14104 /* q_mode is only used for auto_imax */
14105 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14106 /* Use EQ Delay Register method for q_mode */
14107
14108 /* Convert for EQ Delay register */
14109 val = phba->cfg_fcp_imax;
14110 if (val) {
14111 /* First, interrupts per sec per EQ */
14112 val = phba->cfg_fcp_imax /
14113 phba->io_channel_irqs;
14114
14115 /* us delay between each interrupt */
14116 val = LPFC_SEC_TO_USEC / val;
14117 }
14118 eq->q_mode = val;
14119 } else {
14120 eq->q_mode = imax;
14121 }
14122
14123 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014124 break;
14125 }
14126 eq_delay->u.request.num_eq = cnt;
14127
14128 mbox->vport = phba->pport;
14129 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14130 mbox->context1 = NULL;
14131 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14132 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14133 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14134 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14135 if (shdr_status || shdr_add_status || rc) {
14136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14137 "2512 MODIFY_EQ_DELAY mailbox failed with "
14138 "status x%x add_status x%x, mbx status x%x\n",
14139 shdr_status, shdr_add_status, rc);
14140 status = -ENXIO;
14141 }
14142 mempool_free(mbox, phba->mbox_mem_pool);
14143 return status;
14144}
14145
14146/**
James Smart4f774512009-05-22 14:52:35 -040014147 * lpfc_eq_create - Create an Event Queue on the HBA
14148 * @phba: HBA structure that indicates port to create a queue on.
14149 * @eq: The queue structure to use to create the event queue.
14150 * @imax: The maximum interrupt per second limit.
14151 *
14152 * This function creates an event queue, as detailed in @eq, on a port,
14153 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14154 *
14155 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14156 * is used to get the entry count and entry size that are necessary to
14157 * determine the number of pages to allocate and use for this queue. This
14158 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14159 * event queue. This function is asynchronous and will wait for the mailbox
14160 * command to finish before continuing.
14161 *
14162 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014163 * memory this function will return -ENOMEM. If the queue create mailbox command
14164 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014165 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014166int
James Smartee020062012-09-29 11:28:52 -040014167lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014168{
14169 struct lpfc_mbx_eq_create *eq_create;
14170 LPFC_MBOXQ_t *mbox;
14171 int rc, length, status = 0;
14172 struct lpfc_dmabuf *dmabuf;
14173 uint32_t shdr_status, shdr_add_status;
14174 union lpfc_sli4_cfg_shdr *shdr;
14175 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014176 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14177
James Smart2e90f4b2011-12-13 13:22:37 -050014178 /* sanity check on queue memory */
14179 if (!eq)
14180 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014181 if (!phba->sli4_hba.pc_sli4_params.supported)
14182 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014183
14184 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14185 if (!mbox)
14186 return -ENOMEM;
14187 length = (sizeof(struct lpfc_mbx_eq_create) -
14188 sizeof(struct lpfc_sli4_cfg_mhdr));
14189 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14190 LPFC_MBOX_OPCODE_EQ_CREATE,
14191 length, LPFC_SLI4_MBX_EMBED);
14192 eq_create = &mbox->u.mqe.un.eq_create;
14193 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14194 eq->page_count);
14195 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14196 LPFC_EQE_SIZE);
14197 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014198 /* don't setup delay multiplier using EQ_CREATE */
14199 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014200 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14201 dmult);
14202 switch (eq->entry_count) {
14203 default:
14204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14205 "0360 Unsupported EQ count. (%d)\n",
14206 eq->entry_count);
14207 if (eq->entry_count < 256)
14208 return -EINVAL;
14209 /* otherwise default to smallest count (drop through) */
14210 case 256:
14211 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14212 LPFC_EQ_CNT_256);
14213 break;
14214 case 512:
14215 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14216 LPFC_EQ_CNT_512);
14217 break;
14218 case 1024:
14219 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14220 LPFC_EQ_CNT_1024);
14221 break;
14222 case 2048:
14223 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14224 LPFC_EQ_CNT_2048);
14225 break;
14226 case 4096:
14227 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14228 LPFC_EQ_CNT_4096);
14229 break;
14230 }
14231 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014232 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014233 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14234 putPaddrLow(dmabuf->phys);
14235 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14236 putPaddrHigh(dmabuf->phys);
14237 }
14238 mbox->vport = phba->pport;
14239 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14240 mbox->context1 = NULL;
14241 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14242 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14243 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14244 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14245 if (shdr_status || shdr_add_status || rc) {
14246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14247 "2500 EQ_CREATE mailbox failed with "
14248 "status x%x add_status x%x, mbx status x%x\n",
14249 shdr_status, shdr_add_status, rc);
14250 status = -ENXIO;
14251 }
14252 eq->type = LPFC_EQ;
14253 eq->subtype = LPFC_NONE;
14254 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14255 if (eq->queue_id == 0xFFFF)
14256 status = -ENXIO;
14257 eq->host_index = 0;
14258 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014259 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014260
James Smart8fa38512009-07-19 10:01:03 -040014261 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014262 return status;
14263}
14264
14265/**
14266 * lpfc_cq_create - Create a Completion Queue on the HBA
14267 * @phba: HBA structure that indicates port to create a queue on.
14268 * @cq: The queue structure to use to create the completion queue.
14269 * @eq: The event queue to bind this completion queue to.
14270 *
14271 * This function creates a completion queue, as detailed in @wq, on a port,
14272 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14273 *
14274 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14275 * is used to get the entry count and entry size that are necessary to
14276 * determine the number of pages to allocate and use for this queue. The @eq
14277 * is used to indicate which event queue to bind this completion queue to. This
14278 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14279 * completion queue. This function is asynchronous and will wait for the mailbox
14280 * command to finish before continuing.
14281 *
14282 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014283 * memory this function will return -ENOMEM. If the queue create mailbox command
14284 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014285 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014286int
James Smart4f774512009-05-22 14:52:35 -040014287lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14288 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14289{
14290 struct lpfc_mbx_cq_create *cq_create;
14291 struct lpfc_dmabuf *dmabuf;
14292 LPFC_MBOXQ_t *mbox;
14293 int rc, length, status = 0;
14294 uint32_t shdr_status, shdr_add_status;
14295 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014296 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14297
James Smart2e90f4b2011-12-13 13:22:37 -050014298 /* sanity check on queue memory */
14299 if (!cq || !eq)
14300 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014301 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014302 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014303
James Smart4f774512009-05-22 14:52:35 -040014304 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14305 if (!mbox)
14306 return -ENOMEM;
14307 length = (sizeof(struct lpfc_mbx_cq_create) -
14308 sizeof(struct lpfc_sli4_cfg_mhdr));
14309 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14310 LPFC_MBOX_OPCODE_CQ_CREATE,
14311 length, LPFC_SLI4_MBX_EMBED);
14312 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014313 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014314 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14315 cq->page_count);
14316 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14317 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014318 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14319 phba->sli4_hba.pc_sli4_params.cqv);
14320 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014321 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14322 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014323 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14324 eq->queue_id);
14325 } else {
14326 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14327 eq->queue_id);
14328 }
James Smart4f774512009-05-22 14:52:35 -040014329 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014330 case 2048:
14331 case 4096:
14332 if (phba->sli4_hba.pc_sli4_params.cqv ==
14333 LPFC_Q_CREATE_VERSION_2) {
14334 cq_create->u.request.context.lpfc_cq_context_count =
14335 cq->entry_count;
14336 bf_set(lpfc_cq_context_count,
14337 &cq_create->u.request.context,
14338 LPFC_CQ_CNT_WORD7);
14339 break;
14340 }
14341 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014342 default:
14343 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014344 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014345 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014346 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014347 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014348 if (cq->entry_count < 256) {
14349 status = -EINVAL;
14350 goto out;
14351 }
James Smart4f774512009-05-22 14:52:35 -040014352 /* otherwise default to smallest count (drop through) */
14353 case 256:
14354 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14355 LPFC_CQ_CNT_256);
14356 break;
14357 case 512:
14358 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14359 LPFC_CQ_CNT_512);
14360 break;
14361 case 1024:
14362 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14363 LPFC_CQ_CNT_1024);
14364 break;
14365 }
14366 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014367 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014368 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14369 putPaddrLow(dmabuf->phys);
14370 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14371 putPaddrHigh(dmabuf->phys);
14372 }
14373 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14374
14375 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014376 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14377 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14378 if (shdr_status || shdr_add_status || rc) {
14379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14380 "2501 CQ_CREATE mailbox failed with "
14381 "status x%x add_status x%x, mbx status x%x\n",
14382 shdr_status, shdr_add_status, rc);
14383 status = -ENXIO;
14384 goto out;
14385 }
14386 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14387 if (cq->queue_id == 0xFFFF) {
14388 status = -ENXIO;
14389 goto out;
14390 }
14391 /* link the cq onto the parent eq child list */
14392 list_add_tail(&cq->list, &eq->child_list);
14393 /* Set up completion queue's type and subtype */
14394 cq->type = type;
14395 cq->subtype = subtype;
14396 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014397 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014398 cq->host_index = 0;
14399 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014400 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014401
James Smart8fa38512009-07-19 10:01:03 -040014402out:
14403 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014404 return status;
14405}
14406
14407/**
James Smart2d7dbc42017-02-12 13:52:35 -080014408 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14409 * @phba: HBA structure that indicates port to create a queue on.
14410 * @cqp: The queue structure array to use to create the completion queues.
14411 * @eqp: The event queue array to bind these completion queues to.
14412 *
14413 * This function creates a set of completion queue, s to support MRQ
14414 * as detailed in @cqp, on a port,
14415 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14416 *
14417 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14418 * is used to get the entry count and entry size that are necessary to
14419 * determine the number of pages to allocate and use for this queue. The @eq
14420 * is used to indicate which event queue to bind this completion queue to. This
14421 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14422 * completion queue. This function is asynchronous and will wait for the mailbox
14423 * command to finish before continuing.
14424 *
14425 * On success this function will return a zero. If unable to allocate enough
14426 * memory this function will return -ENOMEM. If the queue create mailbox command
14427 * fails this function will return -ENXIO.
14428 **/
14429int
14430lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14431 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14432{
14433 struct lpfc_queue *cq;
14434 struct lpfc_queue *eq;
14435 struct lpfc_mbx_cq_create_set *cq_set;
14436 struct lpfc_dmabuf *dmabuf;
14437 LPFC_MBOXQ_t *mbox;
14438 int rc, length, alloclen, status = 0;
14439 int cnt, idx, numcq, page_idx = 0;
14440 uint32_t shdr_status, shdr_add_status;
14441 union lpfc_sli4_cfg_shdr *shdr;
14442 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14443
14444 /* sanity check on queue memory */
14445 numcq = phba->cfg_nvmet_mrq;
14446 if (!cqp || !eqp || !numcq)
14447 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014448
14449 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14450 if (!mbox)
14451 return -ENOMEM;
14452
14453 length = sizeof(struct lpfc_mbx_cq_create_set);
14454 length += ((numcq * cqp[0]->page_count) *
14455 sizeof(struct dma_address));
14456 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14457 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14458 LPFC_SLI4_MBX_NEMBED);
14459 if (alloclen < length) {
14460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14461 "3098 Allocated DMA memory size (%d) is "
14462 "less than the requested DMA memory size "
14463 "(%d)\n", alloclen, length);
14464 status = -ENOMEM;
14465 goto out;
14466 }
14467 cq_set = mbox->sge_array->addr[0];
14468 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14469 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14470
14471 for (idx = 0; idx < numcq; idx++) {
14472 cq = cqp[idx];
14473 eq = eqp[idx];
14474 if (!cq || !eq) {
14475 status = -ENOMEM;
14476 goto out;
14477 }
James Smart81b96ed2017-11-20 16:00:29 -080014478 if (!phba->sli4_hba.pc_sli4_params.supported)
14479 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014480
14481 switch (idx) {
14482 case 0:
14483 bf_set(lpfc_mbx_cq_create_set_page_size,
14484 &cq_set->u.request,
14485 (hw_page_size / SLI4_PAGE_SIZE));
14486 bf_set(lpfc_mbx_cq_create_set_num_pages,
14487 &cq_set->u.request, cq->page_count);
14488 bf_set(lpfc_mbx_cq_create_set_evt,
14489 &cq_set->u.request, 1);
14490 bf_set(lpfc_mbx_cq_create_set_valid,
14491 &cq_set->u.request, 1);
14492 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14493 &cq_set->u.request, 0);
14494 bf_set(lpfc_mbx_cq_create_set_num_cq,
14495 &cq_set->u.request, numcq);
14496 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014497 case 2048:
14498 case 4096:
14499 if (phba->sli4_hba.pc_sli4_params.cqv ==
14500 LPFC_Q_CREATE_VERSION_2) {
14501 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14502 &cq_set->u.request,
14503 cq->entry_count);
14504 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14505 &cq_set->u.request,
14506 LPFC_CQ_CNT_WORD7);
14507 break;
14508 }
14509 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014510 default:
14511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14512 "3118 Bad CQ count. (%d)\n",
14513 cq->entry_count);
14514 if (cq->entry_count < 256) {
14515 status = -EINVAL;
14516 goto out;
14517 }
14518 /* otherwise default to smallest (drop thru) */
14519 case 256:
14520 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14521 &cq_set->u.request, LPFC_CQ_CNT_256);
14522 break;
14523 case 512:
14524 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14525 &cq_set->u.request, LPFC_CQ_CNT_512);
14526 break;
14527 case 1024:
14528 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14529 &cq_set->u.request, LPFC_CQ_CNT_1024);
14530 break;
14531 }
14532 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14533 &cq_set->u.request, eq->queue_id);
14534 break;
14535 case 1:
14536 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14537 &cq_set->u.request, eq->queue_id);
14538 break;
14539 case 2:
14540 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14541 &cq_set->u.request, eq->queue_id);
14542 break;
14543 case 3:
14544 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14545 &cq_set->u.request, eq->queue_id);
14546 break;
14547 case 4:
14548 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14549 &cq_set->u.request, eq->queue_id);
14550 break;
14551 case 5:
14552 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14553 &cq_set->u.request, eq->queue_id);
14554 break;
14555 case 6:
14556 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14557 &cq_set->u.request, eq->queue_id);
14558 break;
14559 case 7:
14560 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14561 &cq_set->u.request, eq->queue_id);
14562 break;
14563 case 8:
14564 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14565 &cq_set->u.request, eq->queue_id);
14566 break;
14567 case 9:
14568 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14569 &cq_set->u.request, eq->queue_id);
14570 break;
14571 case 10:
14572 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14573 &cq_set->u.request, eq->queue_id);
14574 break;
14575 case 11:
14576 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14577 &cq_set->u.request, eq->queue_id);
14578 break;
14579 case 12:
14580 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14581 &cq_set->u.request, eq->queue_id);
14582 break;
14583 case 13:
14584 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14585 &cq_set->u.request, eq->queue_id);
14586 break;
14587 case 14:
14588 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14589 &cq_set->u.request, eq->queue_id);
14590 break;
14591 case 15:
14592 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14593 &cq_set->u.request, eq->queue_id);
14594 break;
14595 }
14596
14597 /* link the cq onto the parent eq child list */
14598 list_add_tail(&cq->list, &eq->child_list);
14599 /* Set up completion queue's type and subtype */
14600 cq->type = type;
14601 cq->subtype = subtype;
14602 cq->assoc_qid = eq->queue_id;
14603 cq->host_index = 0;
14604 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014605 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014606 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014607
14608 rc = 0;
14609 list_for_each_entry(dmabuf, &cq->page_list, list) {
14610 memset(dmabuf->virt, 0, hw_page_size);
14611 cnt = page_idx + dmabuf->buffer_tag;
14612 cq_set->u.request.page[cnt].addr_lo =
14613 putPaddrLow(dmabuf->phys);
14614 cq_set->u.request.page[cnt].addr_hi =
14615 putPaddrHigh(dmabuf->phys);
14616 rc++;
14617 }
14618 page_idx += rc;
14619 }
14620
14621 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14622
14623 /* The IOCTL status is embedded in the mailbox subheader. */
14624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14626 if (shdr_status || shdr_add_status || rc) {
14627 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14628 "3119 CQ_CREATE_SET mailbox failed with "
14629 "status x%x add_status x%x, mbx status x%x\n",
14630 shdr_status, shdr_add_status, rc);
14631 status = -ENXIO;
14632 goto out;
14633 }
14634 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14635 if (rc == 0xFFFF) {
14636 status = -ENXIO;
14637 goto out;
14638 }
14639
14640 for (idx = 0; idx < numcq; idx++) {
14641 cq = cqp[idx];
14642 cq->queue_id = rc + idx;
14643 }
14644
14645out:
14646 lpfc_sli4_mbox_cmd_free(phba, mbox);
14647 return status;
14648}
14649
14650/**
James Smartb19a0612010-04-06 14:48:51 -040014651 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014652 * @phba: HBA structure that indicates port to create a queue on.
14653 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014654 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14655 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014656 *
James Smartb19a0612010-04-06 14:48:51 -040014657 * This function provides failback (fb) functionality when the
14658 * mq_create_ext fails on older FW generations. It's purpose is identical
14659 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014660 *
James Smartb19a0612010-04-06 14:48:51 -040014661 * This routine cannot fail as all attributes were previously accessed and
14662 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014663 **/
James Smartb19a0612010-04-06 14:48:51 -040014664static void
14665lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14666 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014667{
14668 struct lpfc_mbx_mq_create *mq_create;
14669 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014670 int length;
James Smart04c68492009-05-22 14:52:52 -040014671
James Smart04c68492009-05-22 14:52:52 -040014672 length = (sizeof(struct lpfc_mbx_mq_create) -
14673 sizeof(struct lpfc_sli4_cfg_mhdr));
14674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14675 LPFC_MBOX_OPCODE_MQ_CREATE,
14676 length, LPFC_SLI4_MBX_EMBED);
14677 mq_create = &mbox->u.mqe.un.mq_create;
14678 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014679 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014680 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014681 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014682 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14683 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014684 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014685 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14686 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014687 break;
14688 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014689 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14690 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014691 break;
14692 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014693 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14694 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014695 break;
14696 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014697 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14698 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014699 break;
14700 }
14701 list_for_each_entry(dmabuf, &mq->page_list, list) {
14702 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014703 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014704 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014705 putPaddrHigh(dmabuf->phys);
14706 }
14707}
14708
14709/**
14710 * lpfc_mq_create - Create a mailbox Queue on the HBA
14711 * @phba: HBA structure that indicates port to create a queue on.
14712 * @mq: The queue structure to use to create the mailbox queue.
14713 * @cq: The completion queue to associate with this cq.
14714 * @subtype: The queue's subtype.
14715 *
14716 * This function creates a mailbox queue, as detailed in @mq, on a port,
14717 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14718 *
14719 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14720 * is used to get the entry count and entry size that are necessary to
14721 * determine the number of pages to allocate and use for this queue. This
14722 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14723 * mailbox queue. This function is asynchronous and will wait for the mailbox
14724 * command to finish before continuing.
14725 *
14726 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014727 * memory this function will return -ENOMEM. If the queue create mailbox command
14728 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014729 **/
14730int32_t
14731lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14732 struct lpfc_queue *cq, uint32_t subtype)
14733{
14734 struct lpfc_mbx_mq_create *mq_create;
14735 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14736 struct lpfc_dmabuf *dmabuf;
14737 LPFC_MBOXQ_t *mbox;
14738 int rc, length, status = 0;
14739 uint32_t shdr_status, shdr_add_status;
14740 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014741 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014742
James Smart2e90f4b2011-12-13 13:22:37 -050014743 /* sanity check on queue memory */
14744 if (!mq || !cq)
14745 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014746 if (!phba->sli4_hba.pc_sli4_params.supported)
14747 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014748
14749 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14750 if (!mbox)
14751 return -ENOMEM;
14752 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14753 sizeof(struct lpfc_sli4_cfg_mhdr));
14754 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14755 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14756 length, LPFC_SLI4_MBX_EMBED);
14757
14758 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014759 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014760 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14761 &mq_create_ext->u.request, mq->page_count);
14762 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14763 &mq_create_ext->u.request, 1);
14764 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014765 &mq_create_ext->u.request, 1);
14766 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14767 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014768 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14769 &mq_create_ext->u.request, 1);
14770 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14771 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014772 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014773 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14774 phba->sli4_hba.pc_sli4_params.mqv);
14775 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14776 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14777 cq->queue_id);
14778 else
14779 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14780 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014781 switch (mq->entry_count) {
14782 default:
14783 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14784 "0362 Unsupported MQ count. (%d)\n",
14785 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014786 if (mq->entry_count < 16) {
14787 status = -EINVAL;
14788 goto out;
14789 }
James Smartb19a0612010-04-06 14:48:51 -040014790 /* otherwise default to smallest count (drop through) */
14791 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014792 bf_set(lpfc_mq_context_ring_size,
14793 &mq_create_ext->u.request.context,
14794 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014795 break;
14796 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014797 bf_set(lpfc_mq_context_ring_size,
14798 &mq_create_ext->u.request.context,
14799 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014800 break;
14801 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014802 bf_set(lpfc_mq_context_ring_size,
14803 &mq_create_ext->u.request.context,
14804 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014805 break;
14806 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014807 bf_set(lpfc_mq_context_ring_size,
14808 &mq_create_ext->u.request.context,
14809 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014810 break;
14811 }
14812 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014813 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014814 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14815 putPaddrLow(dmabuf->phys);
14816 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014817 putPaddrHigh(dmabuf->phys);
14818 }
14819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014820 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14821 &mq_create_ext->u.response);
14822 if (rc != MBX_SUCCESS) {
14823 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14824 "2795 MQ_CREATE_EXT failed with "
14825 "status x%x. Failback to MQ_CREATE.\n",
14826 rc);
14827 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14828 mq_create = &mbox->u.mqe.un.mq_create;
14829 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14830 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14831 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14832 &mq_create->u.response);
14833 }
14834
James Smart04c68492009-05-22 14:52:52 -040014835 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014836 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14837 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14838 if (shdr_status || shdr_add_status || rc) {
14839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14840 "2502 MQ_CREATE mailbox failed with "
14841 "status x%x add_status x%x, mbx status x%x\n",
14842 shdr_status, shdr_add_status, rc);
14843 status = -ENXIO;
14844 goto out;
14845 }
James Smart04c68492009-05-22 14:52:52 -040014846 if (mq->queue_id == 0xFFFF) {
14847 status = -ENXIO;
14848 goto out;
14849 }
14850 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014851 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014852 mq->subtype = subtype;
14853 mq->host_index = 0;
14854 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014855 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014856
14857 /* link the mq onto the parent cq child list */
14858 list_add_tail(&mq->list, &cq->child_list);
14859out:
James Smart8fa38512009-07-19 10:01:03 -040014860 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014861 return status;
14862}
14863
14864/**
James Smart4f774512009-05-22 14:52:35 -040014865 * lpfc_wq_create - Create a Work Queue on the HBA
14866 * @phba: HBA structure that indicates port to create a queue on.
14867 * @wq: The queue structure to use to create the work queue.
14868 * @cq: The completion queue to bind this work queue to.
14869 * @subtype: The subtype of the work queue indicating its functionality.
14870 *
14871 * This function creates a work queue, as detailed in @wq, on a port, described
14872 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14873 *
14874 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14875 * is used to get the entry count and entry size that are necessary to
14876 * determine the number of pages to allocate and use for this queue. The @cq
14877 * is used to indicate which completion queue to bind this work queue to. This
14878 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14879 * work queue. This function is asynchronous and will wait for the mailbox
14880 * command to finish before continuing.
14881 *
14882 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014883 * memory this function will return -ENOMEM. If the queue create mailbox command
14884 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014885 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014886int
James Smart4f774512009-05-22 14:52:35 -040014887lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14888 struct lpfc_queue *cq, uint32_t subtype)
14889{
14890 struct lpfc_mbx_wq_create *wq_create;
14891 struct lpfc_dmabuf *dmabuf;
14892 LPFC_MBOXQ_t *mbox;
14893 int rc, length, status = 0;
14894 uint32_t shdr_status, shdr_add_status;
14895 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014896 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014897 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014898 void __iomem *bar_memmap_p;
14899 uint32_t db_offset;
14900 uint16_t pci_barset;
James Smart81b96ed2017-11-20 16:00:29 -080014901 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040014902
James Smart2e90f4b2011-12-13 13:22:37 -050014903 /* sanity check on queue memory */
14904 if (!wq || !cq)
14905 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014906 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014907 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040014908
14909 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14910 if (!mbox)
14911 return -ENOMEM;
14912 length = (sizeof(struct lpfc_mbx_wq_create) -
14913 sizeof(struct lpfc_sli4_cfg_mhdr));
14914 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14915 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14916 length, LPFC_SLI4_MBX_EMBED);
14917 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014918 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014919 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14920 wq->page_count);
14921 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14922 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014923
14924 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014925 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14926 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014927
James Smartc176ffa2018-01-30 15:58:46 -080014928 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
14929 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080014930 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14931 else
14932 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14933
14934 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040014935 case LPFC_Q_CREATE_VERSION_0:
14936 switch (wq->entry_size) {
14937 default:
14938 case 64:
14939 /* Nothing to do, version 0 ONLY supports 64 byte */
14940 page = wq_create->u.request.page;
14941 break;
14942 case 128:
14943 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14944 LPFC_WQ_SZ128_SUPPORT)) {
14945 status = -ERANGE;
14946 goto out;
14947 }
14948 /* If we get here the HBA MUST also support V1 and
14949 * we MUST use it
14950 */
14951 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14952 LPFC_Q_CREATE_VERSION_1);
14953
14954 bf_set(lpfc_mbx_wq_create_wqe_count,
14955 &wq_create->u.request_1, wq->entry_count);
14956 bf_set(lpfc_mbx_wq_create_wqe_size,
14957 &wq_create->u.request_1,
14958 LPFC_WQ_WQE_SIZE_128);
14959 bf_set(lpfc_mbx_wq_create_page_size,
14960 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014961 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014962 page = wq_create->u.request_1.page;
14963 break;
14964 }
14965 break;
14966 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014967 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14968 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014969 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14970 LPFC_Q_CREATE_VERSION_1);
14971
James Smart5a6f1332011-03-11 16:05:35 -050014972 switch (wq->entry_size) {
14973 default:
14974 case 64:
14975 bf_set(lpfc_mbx_wq_create_wqe_size,
14976 &wq_create->u.request_1,
14977 LPFC_WQ_WQE_SIZE_64);
14978 break;
14979 case 128:
James Smart0c651872013-07-15 18:33:23 -040014980 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14981 LPFC_WQ_SZ128_SUPPORT)) {
14982 status = -ERANGE;
14983 goto out;
14984 }
James Smart5a6f1332011-03-11 16:05:35 -050014985 bf_set(lpfc_mbx_wq_create_wqe_size,
14986 &wq_create->u.request_1,
14987 LPFC_WQ_WQE_SIZE_128);
14988 break;
14989 }
James Smart8ea73db2017-02-12 13:52:25 -080014990 bf_set(lpfc_mbx_wq_create_page_size,
14991 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080014992 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014993 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014994 break;
14995 default:
14996 status = -ERANGE;
14997 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014998 }
James Smart0c651872013-07-15 18:33:23 -040014999
James Smart4f774512009-05-22 14:52:35 -040015000 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015001 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015002 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15003 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015004 }
James Smart962bc512013-01-03 15:44:00 -050015005
15006 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15007 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15008
James Smart4f774512009-05-22 14:52:35 -040015009 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15010 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015011 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15012 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15013 if (shdr_status || shdr_add_status || rc) {
15014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15015 "2503 WQ_CREATE mailbox failed with "
15016 "status x%x add_status x%x, mbx status x%x\n",
15017 shdr_status, shdr_add_status, rc);
15018 status = -ENXIO;
15019 goto out;
15020 }
15021 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15022 if (wq->queue_id == 0xFFFF) {
15023 status = -ENXIO;
15024 goto out;
15025 }
James Smart962bc512013-01-03 15:44:00 -050015026 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15027 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15028 &wq_create->u.response);
15029 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15030 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15032 "3265 WQ[%d] doorbell format not "
15033 "supported: x%x\n", wq->queue_id,
15034 wq->db_format);
15035 status = -EINVAL;
15036 goto out;
15037 }
15038 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15039 &wq_create->u.response);
15040 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15041 if (!bar_memmap_p) {
15042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15043 "3263 WQ[%d] failed to memmap pci "
15044 "barset:x%x\n", wq->queue_id,
15045 pci_barset);
15046 status = -ENOMEM;
15047 goto out;
15048 }
15049 db_offset = wq_create->u.response.doorbell_offset;
15050 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15051 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15053 "3252 WQ[%d] doorbell offset not "
15054 "supported: x%x\n", wq->queue_id,
15055 db_offset);
15056 status = -EINVAL;
15057 goto out;
15058 }
15059 wq->db_regaddr = bar_memmap_p + db_offset;
15060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015061 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15062 "format:x%x\n", wq->queue_id, pci_barset,
15063 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015064 } else {
15065 wq->db_format = LPFC_DB_LIST_FORMAT;
15066 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15067 }
James Smart895427b2017-02-12 13:52:30 -080015068 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15069 if (wq->pring == NULL) {
15070 status = -ENOMEM;
15071 goto out;
15072 }
James Smart4f774512009-05-22 14:52:35 -040015073 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015074 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015075 wq->subtype = subtype;
15076 wq->host_index = 0;
15077 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015078 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015079
15080 /* link the wq onto the parent cq child list */
15081 list_add_tail(&wq->list, &cq->child_list);
15082out:
James Smart8fa38512009-07-19 10:01:03 -040015083 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015084 return status;
15085}
15086
15087/**
15088 * lpfc_rq_create - Create a Receive Queue on the HBA
15089 * @phba: HBA structure that indicates port to create a queue on.
15090 * @hrq: The queue structure to use to create the header receive queue.
15091 * @drq: The queue structure to use to create the data receive queue.
15092 * @cq: The completion queue to bind this work queue to.
15093 *
15094 * This function creates a receive buffer queue pair , as detailed in @hrq and
15095 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15096 * to the HBA.
15097 *
15098 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15099 * struct is used to get the entry count that is necessary to determine the
15100 * number of pages to use for this queue. The @cq is used to indicate which
15101 * completion queue to bind received buffers that are posted to these queues to.
15102 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15103 * receive queue pair. This function is asynchronous and will wait for the
15104 * mailbox command to finish before continuing.
15105 *
15106 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015107 * memory this function will return -ENOMEM. If the queue create mailbox command
15108 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015109 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015110int
James Smart4f774512009-05-22 14:52:35 -040015111lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15112 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15113{
15114 struct lpfc_mbx_rq_create *rq_create;
15115 struct lpfc_dmabuf *dmabuf;
15116 LPFC_MBOXQ_t *mbox;
15117 int rc, length, status = 0;
15118 uint32_t shdr_status, shdr_add_status;
15119 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015120 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015121 void __iomem *bar_memmap_p;
15122 uint32_t db_offset;
15123 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015124
James Smart2e90f4b2011-12-13 13:22:37 -050015125 /* sanity check on queue memory */
15126 if (!hrq || !drq || !cq)
15127 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015128 if (!phba->sli4_hba.pc_sli4_params.supported)
15129 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015130
15131 if (hrq->entry_count != drq->entry_count)
15132 return -EINVAL;
15133 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15134 if (!mbox)
15135 return -ENOMEM;
15136 length = (sizeof(struct lpfc_mbx_rq_create) -
15137 sizeof(struct lpfc_sli4_cfg_mhdr));
15138 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15139 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15140 length, LPFC_SLI4_MBX_EMBED);
15141 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015142 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15143 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15144 phba->sli4_hba.pc_sli4_params.rqv);
15145 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15146 bf_set(lpfc_rq_context_rqe_count_1,
15147 &rq_create->u.request.context,
15148 hrq->entry_count);
15149 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015150 bf_set(lpfc_rq_context_rqe_size,
15151 &rq_create->u.request.context,
15152 LPFC_RQE_SIZE_8);
15153 bf_set(lpfc_rq_context_page_size,
15154 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015155 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015156 } else {
15157 switch (hrq->entry_count) {
15158 default:
15159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15160 "2535 Unsupported RQ count. (%d)\n",
15161 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015162 if (hrq->entry_count < 512) {
15163 status = -EINVAL;
15164 goto out;
15165 }
James Smart5a6f1332011-03-11 16:05:35 -050015166 /* otherwise default to smallest count (drop through) */
15167 case 512:
15168 bf_set(lpfc_rq_context_rqe_count,
15169 &rq_create->u.request.context,
15170 LPFC_RQ_RING_SIZE_512);
15171 break;
15172 case 1024:
15173 bf_set(lpfc_rq_context_rqe_count,
15174 &rq_create->u.request.context,
15175 LPFC_RQ_RING_SIZE_1024);
15176 break;
15177 case 2048:
15178 bf_set(lpfc_rq_context_rqe_count,
15179 &rq_create->u.request.context,
15180 LPFC_RQ_RING_SIZE_2048);
15181 break;
15182 case 4096:
15183 bf_set(lpfc_rq_context_rqe_count,
15184 &rq_create->u.request.context,
15185 LPFC_RQ_RING_SIZE_4096);
15186 break;
15187 }
15188 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15189 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015190 }
15191 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15192 cq->queue_id);
15193 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15194 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015195 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015196 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015197 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15198 putPaddrLow(dmabuf->phys);
15199 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15200 putPaddrHigh(dmabuf->phys);
15201 }
James Smart962bc512013-01-03 15:44:00 -050015202 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15203 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15204
James Smart4f774512009-05-22 14:52:35 -040015205 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15206 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015207 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15208 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15209 if (shdr_status || shdr_add_status || rc) {
15210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15211 "2504 RQ_CREATE mailbox failed with "
15212 "status x%x add_status x%x, mbx status x%x\n",
15213 shdr_status, shdr_add_status, rc);
15214 status = -ENXIO;
15215 goto out;
15216 }
15217 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15218 if (hrq->queue_id == 0xFFFF) {
15219 status = -ENXIO;
15220 goto out;
15221 }
James Smart962bc512013-01-03 15:44:00 -050015222
15223 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15224 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15225 &rq_create->u.response);
15226 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15227 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15229 "3262 RQ [%d] doorbell format not "
15230 "supported: x%x\n", hrq->queue_id,
15231 hrq->db_format);
15232 status = -EINVAL;
15233 goto out;
15234 }
15235
15236 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15237 &rq_create->u.response);
15238 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15239 if (!bar_memmap_p) {
15240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15241 "3269 RQ[%d] failed to memmap pci "
15242 "barset:x%x\n", hrq->queue_id,
15243 pci_barset);
15244 status = -ENOMEM;
15245 goto out;
15246 }
15247
15248 db_offset = rq_create->u.response.doorbell_offset;
15249 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15250 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15252 "3270 RQ[%d] doorbell offset not "
15253 "supported: x%x\n", hrq->queue_id,
15254 db_offset);
15255 status = -EINVAL;
15256 goto out;
15257 }
15258 hrq->db_regaddr = bar_memmap_p + db_offset;
15259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015260 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15261 "format:x%x\n", hrq->queue_id, pci_barset,
15262 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015263 } else {
15264 hrq->db_format = LPFC_DB_RING_FORMAT;
15265 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15266 }
James Smart4f774512009-05-22 14:52:35 -040015267 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015268 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015269 hrq->subtype = subtype;
15270 hrq->host_index = 0;
15271 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015272 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015273
15274 /* now create the data queue */
15275 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15276 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15277 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015278 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15279 phba->sli4_hba.pc_sli4_params.rqv);
15280 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15281 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015282 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015283 if (subtype == LPFC_NVMET)
15284 rq_create->u.request.context.buffer_size =
15285 LPFC_NVMET_DATA_BUF_SIZE;
15286 else
15287 rq_create->u.request.context.buffer_size =
15288 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015289 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15290 LPFC_RQE_SIZE_8);
15291 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15292 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015293 } else {
15294 switch (drq->entry_count) {
15295 default:
15296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15297 "2536 Unsupported RQ count. (%d)\n",
15298 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015299 if (drq->entry_count < 512) {
15300 status = -EINVAL;
15301 goto out;
15302 }
James Smart5a6f1332011-03-11 16:05:35 -050015303 /* otherwise default to smallest count (drop through) */
15304 case 512:
15305 bf_set(lpfc_rq_context_rqe_count,
15306 &rq_create->u.request.context,
15307 LPFC_RQ_RING_SIZE_512);
15308 break;
15309 case 1024:
15310 bf_set(lpfc_rq_context_rqe_count,
15311 &rq_create->u.request.context,
15312 LPFC_RQ_RING_SIZE_1024);
15313 break;
15314 case 2048:
15315 bf_set(lpfc_rq_context_rqe_count,
15316 &rq_create->u.request.context,
15317 LPFC_RQ_RING_SIZE_2048);
15318 break;
15319 case 4096:
15320 bf_set(lpfc_rq_context_rqe_count,
15321 &rq_create->u.request.context,
15322 LPFC_RQ_RING_SIZE_4096);
15323 break;
15324 }
James Smart3c603be2017-05-15 15:20:44 -070015325 if (subtype == LPFC_NVMET)
15326 bf_set(lpfc_rq_context_buf_size,
15327 &rq_create->u.request.context,
15328 LPFC_NVMET_DATA_BUF_SIZE);
15329 else
15330 bf_set(lpfc_rq_context_buf_size,
15331 &rq_create->u.request.context,
15332 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015333 }
15334 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15335 cq->queue_id);
15336 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15337 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015338 list_for_each_entry(dmabuf, &drq->page_list, list) {
15339 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15340 putPaddrLow(dmabuf->phys);
15341 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15342 putPaddrHigh(dmabuf->phys);
15343 }
James Smart962bc512013-01-03 15:44:00 -050015344 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15345 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015346 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15347 /* The IOCTL status is embedded in the mailbox subheader. */
15348 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15349 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15350 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15351 if (shdr_status || shdr_add_status || rc) {
15352 status = -ENXIO;
15353 goto out;
15354 }
15355 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15356 if (drq->queue_id == 0xFFFF) {
15357 status = -ENXIO;
15358 goto out;
15359 }
15360 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015361 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015362 drq->subtype = subtype;
15363 drq->host_index = 0;
15364 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015365 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015366
15367 /* link the header and data RQs onto the parent cq child list */
15368 list_add_tail(&hrq->list, &cq->child_list);
15369 list_add_tail(&drq->list, &cq->child_list);
15370
15371out:
James Smart8fa38512009-07-19 10:01:03 -040015372 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015373 return status;
15374}
15375
15376/**
James Smart2d7dbc42017-02-12 13:52:35 -080015377 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15378 * @phba: HBA structure that indicates port to create a queue on.
15379 * @hrqp: The queue structure array to use to create the header receive queues.
15380 * @drqp: The queue structure array to use to create the data receive queues.
15381 * @cqp: The completion queue array to bind these receive queues to.
15382 *
15383 * This function creates a receive buffer queue pair , as detailed in @hrq and
15384 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15385 * to the HBA.
15386 *
15387 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15388 * struct is used to get the entry count that is necessary to determine the
15389 * number of pages to use for this queue. The @cq is used to indicate which
15390 * completion queue to bind received buffers that are posted to these queues to.
15391 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15392 * receive queue pair. This function is asynchronous and will wait for the
15393 * mailbox command to finish before continuing.
15394 *
15395 * On success this function will return a zero. If unable to allocate enough
15396 * memory this function will return -ENOMEM. If the queue create mailbox command
15397 * fails this function will return -ENXIO.
15398 **/
15399int
15400lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15401 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15402 uint32_t subtype)
15403{
15404 struct lpfc_queue *hrq, *drq, *cq;
15405 struct lpfc_mbx_rq_create_v2 *rq_create;
15406 struct lpfc_dmabuf *dmabuf;
15407 LPFC_MBOXQ_t *mbox;
15408 int rc, length, alloclen, status = 0;
15409 int cnt, idx, numrq, page_idx = 0;
15410 uint32_t shdr_status, shdr_add_status;
15411 union lpfc_sli4_cfg_shdr *shdr;
15412 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15413
15414 numrq = phba->cfg_nvmet_mrq;
15415 /* sanity check on array memory */
15416 if (!hrqp || !drqp || !cqp || !numrq)
15417 return -ENODEV;
15418 if (!phba->sli4_hba.pc_sli4_params.supported)
15419 hw_page_size = SLI4_PAGE_SIZE;
15420
15421 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15422 if (!mbox)
15423 return -ENOMEM;
15424
15425 length = sizeof(struct lpfc_mbx_rq_create_v2);
15426 length += ((2 * numrq * hrqp[0]->page_count) *
15427 sizeof(struct dma_address));
15428
15429 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15430 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15431 LPFC_SLI4_MBX_NEMBED);
15432 if (alloclen < length) {
15433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15434 "3099 Allocated DMA memory size (%d) is "
15435 "less than the requested DMA memory size "
15436 "(%d)\n", alloclen, length);
15437 status = -ENOMEM;
15438 goto out;
15439 }
15440
15441
15442
15443 rq_create = mbox->sge_array->addr[0];
15444 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15445
15446 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15447 cnt = 0;
15448
15449 for (idx = 0; idx < numrq; idx++) {
15450 hrq = hrqp[idx];
15451 drq = drqp[idx];
15452 cq = cqp[idx];
15453
James Smart2d7dbc42017-02-12 13:52:35 -080015454 /* sanity check on queue memory */
15455 if (!hrq || !drq || !cq) {
15456 status = -ENODEV;
15457 goto out;
15458 }
15459
James Smart7aabe842017-03-04 09:30:22 -080015460 if (hrq->entry_count != drq->entry_count) {
15461 status = -EINVAL;
15462 goto out;
15463 }
15464
James Smart2d7dbc42017-02-12 13:52:35 -080015465 if (idx == 0) {
15466 bf_set(lpfc_mbx_rq_create_num_pages,
15467 &rq_create->u.request,
15468 hrq->page_count);
15469 bf_set(lpfc_mbx_rq_create_rq_cnt,
15470 &rq_create->u.request, (numrq * 2));
15471 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15472 1);
15473 bf_set(lpfc_rq_context_base_cq,
15474 &rq_create->u.request.context,
15475 cq->queue_id);
15476 bf_set(lpfc_rq_context_data_size,
15477 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015478 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015479 bf_set(lpfc_rq_context_hdr_size,
15480 &rq_create->u.request.context,
15481 LPFC_HDR_BUF_SIZE);
15482 bf_set(lpfc_rq_context_rqe_count_1,
15483 &rq_create->u.request.context,
15484 hrq->entry_count);
15485 bf_set(lpfc_rq_context_rqe_size,
15486 &rq_create->u.request.context,
15487 LPFC_RQE_SIZE_8);
15488 bf_set(lpfc_rq_context_page_size,
15489 &rq_create->u.request.context,
15490 (PAGE_SIZE/SLI4_PAGE_SIZE));
15491 }
15492 rc = 0;
15493 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15494 memset(dmabuf->virt, 0, hw_page_size);
15495 cnt = page_idx + dmabuf->buffer_tag;
15496 rq_create->u.request.page[cnt].addr_lo =
15497 putPaddrLow(dmabuf->phys);
15498 rq_create->u.request.page[cnt].addr_hi =
15499 putPaddrHigh(dmabuf->phys);
15500 rc++;
15501 }
15502 page_idx += rc;
15503
15504 rc = 0;
15505 list_for_each_entry(dmabuf, &drq->page_list, list) {
15506 memset(dmabuf->virt, 0, hw_page_size);
15507 cnt = page_idx + dmabuf->buffer_tag;
15508 rq_create->u.request.page[cnt].addr_lo =
15509 putPaddrLow(dmabuf->phys);
15510 rq_create->u.request.page[cnt].addr_hi =
15511 putPaddrHigh(dmabuf->phys);
15512 rc++;
15513 }
15514 page_idx += rc;
15515
15516 hrq->db_format = LPFC_DB_RING_FORMAT;
15517 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15518 hrq->type = LPFC_HRQ;
15519 hrq->assoc_qid = cq->queue_id;
15520 hrq->subtype = subtype;
15521 hrq->host_index = 0;
15522 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015523 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015524
15525 drq->db_format = LPFC_DB_RING_FORMAT;
15526 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15527 drq->type = LPFC_DRQ;
15528 drq->assoc_qid = cq->queue_id;
15529 drq->subtype = subtype;
15530 drq->host_index = 0;
15531 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015532 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015533
15534 list_add_tail(&hrq->list, &cq->child_list);
15535 list_add_tail(&drq->list, &cq->child_list);
15536 }
15537
15538 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15539 /* The IOCTL status is embedded in the mailbox subheader. */
15540 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15541 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15542 if (shdr_status || shdr_add_status || rc) {
15543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15544 "3120 RQ_CREATE mailbox failed with "
15545 "status x%x add_status x%x, mbx status x%x\n",
15546 shdr_status, shdr_add_status, rc);
15547 status = -ENXIO;
15548 goto out;
15549 }
15550 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15551 if (rc == 0xFFFF) {
15552 status = -ENXIO;
15553 goto out;
15554 }
15555
15556 /* Initialize all RQs with associated queue id */
15557 for (idx = 0; idx < numrq; idx++) {
15558 hrq = hrqp[idx];
15559 hrq->queue_id = rc + (2 * idx);
15560 drq = drqp[idx];
15561 drq->queue_id = rc + (2 * idx) + 1;
15562 }
15563
15564out:
15565 lpfc_sli4_mbox_cmd_free(phba, mbox);
15566 return status;
15567}
15568
15569/**
James Smart4f774512009-05-22 14:52:35 -040015570 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15571 * @eq: The queue structure associated with the queue to destroy.
15572 *
15573 * This function destroys a queue, as detailed in @eq by sending an mailbox
15574 * command, specific to the type of queue, to the HBA.
15575 *
15576 * The @eq struct is used to get the queue ID of the queue to destroy.
15577 *
15578 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015579 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015580 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015581int
James Smart4f774512009-05-22 14:52:35 -040015582lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15583{
15584 LPFC_MBOXQ_t *mbox;
15585 int rc, length, status = 0;
15586 uint32_t shdr_status, shdr_add_status;
15587 union lpfc_sli4_cfg_shdr *shdr;
15588
James Smart2e90f4b2011-12-13 13:22:37 -050015589 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015590 if (!eq)
15591 return -ENODEV;
15592 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15593 if (!mbox)
15594 return -ENOMEM;
15595 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15596 sizeof(struct lpfc_sli4_cfg_mhdr));
15597 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15598 LPFC_MBOX_OPCODE_EQ_DESTROY,
15599 length, LPFC_SLI4_MBX_EMBED);
15600 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15601 eq->queue_id);
15602 mbox->vport = eq->phba->pport;
15603 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15604
15605 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15606 /* The IOCTL status is embedded in the mailbox subheader. */
15607 shdr = (union lpfc_sli4_cfg_shdr *)
15608 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15611 if (shdr_status || shdr_add_status || rc) {
15612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15613 "2505 EQ_DESTROY mailbox failed with "
15614 "status x%x add_status x%x, mbx status x%x\n",
15615 shdr_status, shdr_add_status, rc);
15616 status = -ENXIO;
15617 }
15618
15619 /* Remove eq from any list */
15620 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015621 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015622 return status;
15623}
15624
15625/**
15626 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15627 * @cq: The queue structure associated with the queue to destroy.
15628 *
15629 * This function destroys a queue, as detailed in @cq by sending an mailbox
15630 * command, specific to the type of queue, to the HBA.
15631 *
15632 * The @cq struct is used to get the queue ID of the queue to destroy.
15633 *
15634 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015635 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015636 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015637int
James Smart4f774512009-05-22 14:52:35 -040015638lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15639{
15640 LPFC_MBOXQ_t *mbox;
15641 int rc, length, status = 0;
15642 uint32_t shdr_status, shdr_add_status;
15643 union lpfc_sli4_cfg_shdr *shdr;
15644
James Smart2e90f4b2011-12-13 13:22:37 -050015645 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015646 if (!cq)
15647 return -ENODEV;
15648 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15649 if (!mbox)
15650 return -ENOMEM;
15651 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15652 sizeof(struct lpfc_sli4_cfg_mhdr));
15653 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15654 LPFC_MBOX_OPCODE_CQ_DESTROY,
15655 length, LPFC_SLI4_MBX_EMBED);
15656 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15657 cq->queue_id);
15658 mbox->vport = cq->phba->pport;
15659 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15660 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15661 /* The IOCTL status is embedded in the mailbox subheader. */
15662 shdr = (union lpfc_sli4_cfg_shdr *)
15663 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15664 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15665 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15666 if (shdr_status || shdr_add_status || rc) {
15667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15668 "2506 CQ_DESTROY mailbox failed with "
15669 "status x%x add_status x%x, mbx status x%x\n",
15670 shdr_status, shdr_add_status, rc);
15671 status = -ENXIO;
15672 }
15673 /* Remove cq from any list */
15674 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015675 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015676 return status;
15677}
15678
15679/**
James Smart04c68492009-05-22 14:52:52 -040015680 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15681 * @qm: The queue structure associated with the queue to destroy.
15682 *
15683 * This function destroys a queue, as detailed in @mq by sending an mailbox
15684 * command, specific to the type of queue, to the HBA.
15685 *
15686 * The @mq struct is used to get the queue ID of the queue to destroy.
15687 *
15688 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015689 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015690 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015691int
James Smart04c68492009-05-22 14:52:52 -040015692lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15693{
15694 LPFC_MBOXQ_t *mbox;
15695 int rc, length, status = 0;
15696 uint32_t shdr_status, shdr_add_status;
15697 union lpfc_sli4_cfg_shdr *shdr;
15698
James Smart2e90f4b2011-12-13 13:22:37 -050015699 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015700 if (!mq)
15701 return -ENODEV;
15702 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15703 if (!mbox)
15704 return -ENOMEM;
15705 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15706 sizeof(struct lpfc_sli4_cfg_mhdr));
15707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15708 LPFC_MBOX_OPCODE_MQ_DESTROY,
15709 length, LPFC_SLI4_MBX_EMBED);
15710 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15711 mq->queue_id);
15712 mbox->vport = mq->phba->pport;
15713 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15714 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15715 /* The IOCTL status is embedded in the mailbox subheader. */
15716 shdr = (union lpfc_sli4_cfg_shdr *)
15717 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15718 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15719 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15720 if (shdr_status || shdr_add_status || rc) {
15721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15722 "2507 MQ_DESTROY mailbox failed with "
15723 "status x%x add_status x%x, mbx status x%x\n",
15724 shdr_status, shdr_add_status, rc);
15725 status = -ENXIO;
15726 }
15727 /* Remove mq from any list */
15728 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015729 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015730 return status;
15731}
15732
15733/**
James Smart4f774512009-05-22 14:52:35 -040015734 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15735 * @wq: The queue structure associated with the queue to destroy.
15736 *
15737 * This function destroys a queue, as detailed in @wq by sending an mailbox
15738 * command, specific to the type of queue, to the HBA.
15739 *
15740 * The @wq struct is used to get the queue ID of the queue to destroy.
15741 *
15742 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015743 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015744 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015745int
James Smart4f774512009-05-22 14:52:35 -040015746lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15747{
15748 LPFC_MBOXQ_t *mbox;
15749 int rc, length, status = 0;
15750 uint32_t shdr_status, shdr_add_status;
15751 union lpfc_sli4_cfg_shdr *shdr;
15752
James Smart2e90f4b2011-12-13 13:22:37 -050015753 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015754 if (!wq)
15755 return -ENODEV;
15756 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15757 if (!mbox)
15758 return -ENOMEM;
15759 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15760 sizeof(struct lpfc_sli4_cfg_mhdr));
15761 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15762 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15763 length, LPFC_SLI4_MBX_EMBED);
15764 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15765 wq->queue_id);
15766 mbox->vport = wq->phba->pport;
15767 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15768 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15769 shdr = (union lpfc_sli4_cfg_shdr *)
15770 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15771 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15772 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15773 if (shdr_status || shdr_add_status || rc) {
15774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15775 "2508 WQ_DESTROY mailbox failed with "
15776 "status x%x add_status x%x, mbx status x%x\n",
15777 shdr_status, shdr_add_status, rc);
15778 status = -ENXIO;
15779 }
15780 /* Remove wq from any list */
15781 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015782 kfree(wq->pring);
15783 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015784 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015785 return status;
15786}
15787
15788/**
15789 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15790 * @rq: The queue structure associated with the queue to destroy.
15791 *
15792 * This function destroys a queue, as detailed in @rq by sending an mailbox
15793 * command, specific to the type of queue, to the HBA.
15794 *
15795 * The @rq struct is used to get the queue ID of the queue to destroy.
15796 *
15797 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015798 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015799 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015800int
James Smart4f774512009-05-22 14:52:35 -040015801lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15802 struct lpfc_queue *drq)
15803{
15804 LPFC_MBOXQ_t *mbox;
15805 int rc, length, status = 0;
15806 uint32_t shdr_status, shdr_add_status;
15807 union lpfc_sli4_cfg_shdr *shdr;
15808
James Smart2e90f4b2011-12-13 13:22:37 -050015809 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015810 if (!hrq || !drq)
15811 return -ENODEV;
15812 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15813 if (!mbox)
15814 return -ENOMEM;
15815 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015816 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015817 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15818 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15819 length, LPFC_SLI4_MBX_EMBED);
15820 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15821 hrq->queue_id);
15822 mbox->vport = hrq->phba->pport;
15823 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15824 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15825 /* The IOCTL status is embedded in the mailbox subheader. */
15826 shdr = (union lpfc_sli4_cfg_shdr *)
15827 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15828 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15829 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15830 if (shdr_status || shdr_add_status || rc) {
15831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15832 "2509 RQ_DESTROY mailbox failed with "
15833 "status x%x add_status x%x, mbx status x%x\n",
15834 shdr_status, shdr_add_status, rc);
15835 if (rc != MBX_TIMEOUT)
15836 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15837 return -ENXIO;
15838 }
15839 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15840 drq->queue_id);
15841 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15842 shdr = (union lpfc_sli4_cfg_shdr *)
15843 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15844 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15845 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15846 if (shdr_status || shdr_add_status || rc) {
15847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15848 "2510 RQ_DESTROY mailbox failed with "
15849 "status x%x add_status x%x, mbx status x%x\n",
15850 shdr_status, shdr_add_status, rc);
15851 status = -ENXIO;
15852 }
15853 list_del_init(&hrq->list);
15854 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015855 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015856 return status;
15857}
15858
15859/**
15860 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15861 * @phba: The virtual port for which this call being executed.
15862 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15863 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15864 * @xritag: the xritag that ties this io to the SGL pages.
15865 *
15866 * This routine will post the sgl pages for the IO that has the xritag
15867 * that is in the iocbq structure. The xritag is assigned during iocbq
15868 * creation and persists for as long as the driver is loaded.
15869 * if the caller has fewer than 256 scatter gather segments to map then
15870 * pdma_phys_addr1 should be 0.
15871 * If the caller needs to map more than 256 scatter gather segment then
15872 * pdma_phys_addr1 should be a valid physical address.
15873 * physical address for SGLs must be 64 byte aligned.
15874 * If you are going to map 2 SGL's then the first one must have 256 entries
15875 * the second sgl can have between 1 and 256 entries.
15876 *
15877 * Return codes:
15878 * 0 - Success
15879 * -ENXIO, -ENOMEM - Failure
15880 **/
15881int
15882lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15883 dma_addr_t pdma_phys_addr0,
15884 dma_addr_t pdma_phys_addr1,
15885 uint16_t xritag)
15886{
15887 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15888 LPFC_MBOXQ_t *mbox;
15889 int rc;
15890 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015891 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015892 union lpfc_sli4_cfg_shdr *shdr;
15893
15894 if (xritag == NO_XRI) {
15895 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15896 "0364 Invalid param:\n");
15897 return -EINVAL;
15898 }
15899
15900 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15901 if (!mbox)
15902 return -ENOMEM;
15903
15904 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15905 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15906 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015907 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015908
15909 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15910 &mbox->u.mqe.un.post_sgl_pages;
15911 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15912 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15913
15914 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15915 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15916 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15917 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15918
15919 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15920 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15921 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15922 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15923 if (!phba->sli4_hba.intr_enable)
15924 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015925 else {
James Smarta183a152011-10-10 21:32:43 -040015926 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015927 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15928 }
James Smart4f774512009-05-22 14:52:35 -040015929 /* The IOCTL status is embedded in the mailbox subheader. */
15930 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15931 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15932 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15933 if (rc != MBX_TIMEOUT)
15934 mempool_free(mbox, phba->mbox_mem_pool);
15935 if (shdr_status || shdr_add_status || rc) {
15936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15937 "2511 POST_SGL mailbox failed with "
15938 "status x%x add_status x%x, mbx status x%x\n",
15939 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015940 }
15941 return 0;
15942}
James Smart4f774512009-05-22 14:52:35 -040015943
15944/**
James Smart88a2cfb2011-07-22 18:36:33 -040015945 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015946 * @phba: pointer to lpfc hba data structure.
15947 *
15948 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015949 * HBA consistent with the SLI-4 interface spec. This routine
15950 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15951 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015952 *
James Smart88a2cfb2011-07-22 18:36:33 -040015953 * Returns
15954 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15955 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15956 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015957static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015958lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15959{
15960 unsigned long xri;
15961
15962 /*
15963 * Fetch the next logical xri. Because this index is logical,
15964 * the driver starts at 0 each time.
15965 */
15966 spin_lock_irq(&phba->hbalock);
15967 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15968 phba->sli4_hba.max_cfg_param.max_xri, 0);
15969 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15970 spin_unlock_irq(&phba->hbalock);
15971 return NO_XRI;
15972 } else {
15973 set_bit(xri, phba->sli4_hba.xri_bmask);
15974 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015975 }
James Smart6d368e52011-05-24 11:44:12 -040015976 spin_unlock_irq(&phba->hbalock);
15977 return xri;
15978}
15979
15980/**
15981 * lpfc_sli4_free_xri - Release an xri for reuse.
15982 * @phba: pointer to lpfc hba data structure.
15983 *
15984 * This routine is invoked to release an xri to the pool of
15985 * available rpis maintained by the driver.
15986 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015987static void
James Smart6d368e52011-05-24 11:44:12 -040015988__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15989{
15990 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015991 phba->sli4_hba.max_cfg_param.xri_used--;
15992 }
15993}
15994
15995/**
15996 * lpfc_sli4_free_xri - Release an xri for reuse.
15997 * @phba: pointer to lpfc hba data structure.
15998 *
15999 * This routine is invoked to release an xri to the pool of
16000 * available rpis maintained by the driver.
16001 **/
16002void
16003lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16004{
16005 spin_lock_irq(&phba->hbalock);
16006 __lpfc_sli4_free_xri(phba, xri);
16007 spin_unlock_irq(&phba->hbalock);
16008}
16009
16010/**
James Smart4f774512009-05-22 14:52:35 -040016011 * lpfc_sli4_next_xritag - Get an xritag for the io
16012 * @phba: Pointer to HBA context object.
16013 *
16014 * This function gets an xritag for the iocb. If there is no unused xritag
16015 * it will return 0xffff.
16016 * The function returns the allocated xritag if successful, else returns zero.
16017 * Zero is not a valid xritag.
16018 * The caller is not required to hold any lock.
16019 **/
16020uint16_t
16021lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16022{
James Smart6d368e52011-05-24 11:44:12 -040016023 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016024
James Smart6d368e52011-05-24 11:44:12 -040016025 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016026 if (xri_index == NO_XRI)
16027 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16028 "2004 Failed to allocate XRI.last XRITAG is %d"
16029 " Max XRI is %d, Used XRI is %d\n",
16030 xri_index,
16031 phba->sli4_hba.max_cfg_param.max_xri,
16032 phba->sli4_hba.max_cfg_param.xri_used);
16033 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016034}
16035
16036/**
James Smart895427b2017-02-12 13:52:30 -080016037 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016038 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016039 * @post_sgl_list: pointer to els sgl entry list.
16040 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016041 *
16042 * This routine is invoked to post a block of driver's sgl pages to the
16043 * HBA using non-embedded mailbox command. No Lock is held. This routine
16044 * is only called when the driver is loading and after all IO has been
16045 * stopped.
16046 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016047static int
James Smart895427b2017-02-12 13:52:30 -080016048lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016049 struct list_head *post_sgl_list,
16050 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016051{
James Smart8a9d2e82012-05-09 21:16:12 -040016052 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016053 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16054 struct sgl_page_pairs *sgl_pg_pairs;
16055 void *viraddr;
16056 LPFC_MBOXQ_t *mbox;
16057 uint32_t reqlen, alloclen, pg_pairs;
16058 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016059 uint16_t xritag_start = 0;
16060 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016061 uint32_t shdr_status, shdr_add_status;
16062 union lpfc_sli4_cfg_shdr *shdr;
16063
James Smart895427b2017-02-12 13:52:30 -080016064 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016065 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016066 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016068 "2559 Block sgl registration required DMA "
16069 "size (%d) great than a page\n", reqlen);
16070 return -ENOMEM;
16071 }
James Smart895427b2017-02-12 13:52:30 -080016072
James Smart4f774512009-05-22 14:52:35 -040016073 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016074 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016075 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016076
16077 /* Allocate DMA memory and set up the non-embedded mailbox command */
16078 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16079 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16080 LPFC_SLI4_MBX_NEMBED);
16081
16082 if (alloclen < reqlen) {
16083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16084 "0285 Allocated DMA memory size (%d) is "
16085 "less than the requested DMA memory "
16086 "size (%d)\n", alloclen, reqlen);
16087 lpfc_sli4_mbox_cmd_free(phba, mbox);
16088 return -ENOMEM;
16089 }
James Smart4f774512009-05-22 14:52:35 -040016090 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016091 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016092 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16093 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16094
James Smart8a9d2e82012-05-09 21:16:12 -040016095 pg_pairs = 0;
16096 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016097 /* Set up the sge entry */
16098 sgl_pg_pairs->sgl_pg0_addr_lo =
16099 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16100 sgl_pg_pairs->sgl_pg0_addr_hi =
16101 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16102 sgl_pg_pairs->sgl_pg1_addr_lo =
16103 cpu_to_le32(putPaddrLow(0));
16104 sgl_pg_pairs->sgl_pg1_addr_hi =
16105 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016106
James Smart4f774512009-05-22 14:52:35 -040016107 /* Keep the first xritag on the list */
16108 if (pg_pairs == 0)
16109 xritag_start = sglq_entry->sli4_xritag;
16110 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016111 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016112 }
James Smart6d368e52011-05-24 11:44:12 -040016113
16114 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016115 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016116 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016117 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016118
James Smart4f774512009-05-22 14:52:35 -040016119 if (!phba->sli4_hba.intr_enable)
16120 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16121 else {
James Smarta183a152011-10-10 21:32:43 -040016122 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016123 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16124 }
16125 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16126 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16127 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16128 if (rc != MBX_TIMEOUT)
16129 lpfc_sli4_mbox_cmd_free(phba, mbox);
16130 if (shdr_status || shdr_add_status || rc) {
16131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16132 "2513 POST_SGL_BLOCK mailbox command failed "
16133 "status x%x add_status x%x mbx status x%x\n",
16134 shdr_status, shdr_add_status, rc);
16135 rc = -ENXIO;
16136 }
16137 return rc;
16138}
16139
16140/**
16141 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16142 * @phba: pointer to lpfc hba data structure.
16143 * @sblist: pointer to scsi buffer list.
16144 * @count: number of scsi buffers on the list.
16145 *
16146 * This routine is invoked to post a block of @count scsi sgl pages from a
16147 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16148 * No Lock is held.
16149 *
16150 **/
16151int
James Smart8a9d2e82012-05-09 21:16:12 -040016152lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16153 struct list_head *sblist,
16154 int count)
James Smart4f774512009-05-22 14:52:35 -040016155{
16156 struct lpfc_scsi_buf *psb;
16157 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16158 struct sgl_page_pairs *sgl_pg_pairs;
16159 void *viraddr;
16160 LPFC_MBOXQ_t *mbox;
16161 uint32_t reqlen, alloclen, pg_pairs;
16162 uint32_t mbox_tmo;
16163 uint16_t xritag_start = 0;
16164 int rc = 0;
16165 uint32_t shdr_status, shdr_add_status;
16166 dma_addr_t pdma_phys_bpl1;
16167 union lpfc_sli4_cfg_shdr *shdr;
16168
16169 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016170 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016171 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016172 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016173 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16174 "0217 Block sgl registration required DMA "
16175 "size (%d) great than a page\n", reqlen);
16176 return -ENOMEM;
16177 }
16178 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16179 if (!mbox) {
16180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16181 "0283 Failed to allocate mbox cmd memory\n");
16182 return -ENOMEM;
16183 }
16184
16185 /* Allocate DMA memory and set up the non-embedded mailbox command */
16186 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16187 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16188 LPFC_SLI4_MBX_NEMBED);
16189
16190 if (alloclen < reqlen) {
16191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16192 "2561 Allocated DMA memory size (%d) is "
16193 "less than the requested DMA memory "
16194 "size (%d)\n", alloclen, reqlen);
16195 lpfc_sli4_mbox_cmd_free(phba, mbox);
16196 return -ENOMEM;
16197 }
James Smart6d368e52011-05-24 11:44:12 -040016198
James Smart4f774512009-05-22 14:52:35 -040016199 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016200 viraddr = mbox->sge_array->addr[0];
16201
16202 /* Set up the SGL pages in the non-embedded DMA pages */
16203 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16204 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16205
16206 pg_pairs = 0;
16207 list_for_each_entry(psb, sblist, list) {
16208 /* Set up the sge entry */
16209 sgl_pg_pairs->sgl_pg0_addr_lo =
16210 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16211 sgl_pg_pairs->sgl_pg0_addr_hi =
16212 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16213 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16214 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16215 else
16216 pdma_phys_bpl1 = 0;
16217 sgl_pg_pairs->sgl_pg1_addr_lo =
16218 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16219 sgl_pg_pairs->sgl_pg1_addr_hi =
16220 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16221 /* Keep the first xritag on the list */
16222 if (pg_pairs == 0)
16223 xritag_start = psb->cur_iocbq.sli4_xritag;
16224 sgl_pg_pairs++;
16225 pg_pairs++;
16226 }
16227 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16228 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16229 /* Perform endian conversion if necessary */
16230 sgl->word0 = cpu_to_le32(sgl->word0);
16231
16232 if (!phba->sli4_hba.intr_enable)
16233 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16234 else {
James Smarta183a152011-10-10 21:32:43 -040016235 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016236 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16237 }
16238 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16239 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16240 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16241 if (rc != MBX_TIMEOUT)
16242 lpfc_sli4_mbox_cmd_free(phba, mbox);
16243 if (shdr_status || shdr_add_status || rc) {
16244 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16245 "2564 POST_SGL_BLOCK mailbox command failed "
16246 "status x%x add_status x%x mbx status x%x\n",
16247 shdr_status, shdr_add_status, rc);
16248 rc = -ENXIO;
16249 }
16250 return rc;
16251}
16252
16253/**
16254 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16255 * @phba: pointer to lpfc_hba struct that the frame was received on
16256 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16257 *
16258 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16259 * valid type of frame that the LPFC driver will handle. This function will
16260 * return a zero if the frame is a valid frame or a non zero value when the
16261 * frame does not pass the check.
16262 **/
16263static int
16264lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16265{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016266 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016267 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016268 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016269
James Smartae9e28f2017-05-15 15:20:51 -070016270#define FC_RCTL_MDS_DIAGS 0xF4
16271
James Smart4f774512009-05-22 14:52:35 -040016272 switch (fc_hdr->fh_r_ctl) {
16273 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16274 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16275 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16276 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16277 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16278 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16279 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16280 case FC_RCTL_DD_CMD_STATUS: /* command status */
16281 case FC_RCTL_ELS_REQ: /* extended link services request */
16282 case FC_RCTL_ELS_REP: /* extended link services reply */
16283 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16284 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16285 case FC_RCTL_BA_NOP: /* basic link service NOP */
16286 case FC_RCTL_BA_ABTS: /* basic link service abort */
16287 case FC_RCTL_BA_RMC: /* remove connection */
16288 case FC_RCTL_BA_ACC: /* basic accept */
16289 case FC_RCTL_BA_RJT: /* basic reject */
16290 case FC_RCTL_BA_PRMT:
16291 case FC_RCTL_ACK_1: /* acknowledge_1 */
16292 case FC_RCTL_ACK_0: /* acknowledge_0 */
16293 case FC_RCTL_P_RJT: /* port reject */
16294 case FC_RCTL_F_RJT: /* fabric reject */
16295 case FC_RCTL_P_BSY: /* port busy */
16296 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16297 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16298 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016299 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016300 case FC_RCTL_END: /* end */
16301 break;
16302 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16303 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16304 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16305 return lpfc_fc_frame_check(phba, fc_hdr);
16306 default:
16307 goto drop;
16308 }
James Smartae9e28f2017-05-15 15:20:51 -070016309
16310#define FC_TYPE_VENDOR_UNIQUE 0xFF
16311
James Smart4f774512009-05-22 14:52:35 -040016312 switch (fc_hdr->fh_type) {
16313 case FC_TYPE_BLS:
16314 case FC_TYPE_ELS:
16315 case FC_TYPE_FCP:
16316 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016317 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016318 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016319 break;
16320 case FC_TYPE_IP:
16321 case FC_TYPE_ILS:
16322 default:
16323 goto drop;
16324 }
James Smart546fc852011-03-11 16:06:29 -050016325
James Smart4f774512009-05-22 14:52:35 -040016326 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016327 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016328 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016329 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16330 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16331 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16332 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16333 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016334 return 0;
16335drop:
16336 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016337 "2539 Dropped frame rctl:x%x type:x%x\n",
16338 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016339 return 1;
16340}
16341
16342/**
16343 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16344 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16345 *
16346 * This function processes the FC header to retrieve the VFI from the VF
16347 * header, if one exists. This function will return the VFI if one exists
16348 * or 0 if no VSAN Header exists.
16349 **/
16350static uint32_t
16351lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16352{
16353 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16354
16355 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16356 return 0;
16357 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16358}
16359
16360/**
16361 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16362 * @phba: Pointer to the HBA structure to search for the vport on
16363 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16364 * @fcfi: The FC Fabric ID that the frame came from
16365 *
16366 * This function searches the @phba for a vport that matches the content of the
16367 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16368 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16369 * returns the matching vport pointer or NULL if unable to match frame to a
16370 * vport.
16371 **/
16372static struct lpfc_vport *
16373lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016374 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016375{
16376 struct lpfc_vport **vports;
16377 struct lpfc_vport *vport = NULL;
16378 int i;
James Smart939723a2012-05-09 21:19:03 -040016379
James Smartbf086112011-08-21 21:48:13 -040016380 if (did == Fabric_DID)
16381 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016382 if ((phba->pport->fc_flag & FC_PT2PT) &&
16383 !(phba->link_state == LPFC_HBA_READY))
16384 return phba->pport;
16385
James Smart4f774512009-05-22 14:52:35 -040016386 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016387 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016388 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16389 if (phba->fcf.fcfi == fcfi &&
16390 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16391 vports[i]->fc_myDID == did) {
16392 vport = vports[i];
16393 break;
16394 }
16395 }
James Smart895427b2017-02-12 13:52:30 -080016396 }
James Smart4f774512009-05-22 14:52:35 -040016397 lpfc_destroy_vport_work_array(phba, vports);
16398 return vport;
16399}
16400
16401/**
James Smart45ed1192009-10-02 15:17:02 -040016402 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16403 * @vport: The vport to work on.
16404 *
16405 * This function updates the receive sequence time stamp for this vport. The
16406 * receive sequence time stamp indicates the time that the last frame of the
16407 * the sequence that has been idle for the longest amount of time was received.
16408 * the driver uses this time stamp to indicate if any received sequences have
16409 * timed out.
16410 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016411static void
James Smart45ed1192009-10-02 15:17:02 -040016412lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16413{
16414 struct lpfc_dmabuf *h_buf;
16415 struct hbq_dmabuf *dmabuf = NULL;
16416
16417 /* get the oldest sequence on the rcv list */
16418 h_buf = list_get_first(&vport->rcv_buffer_list,
16419 struct lpfc_dmabuf, list);
16420 if (!h_buf)
16421 return;
16422 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16423 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16424}
16425
16426/**
16427 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16428 * @vport: The vport that the received sequences were sent to.
16429 *
16430 * This function cleans up all outstanding received sequences. This is called
16431 * by the driver when a link event or user action invalidates all the received
16432 * sequences.
16433 **/
16434void
16435lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16436{
16437 struct lpfc_dmabuf *h_buf, *hnext;
16438 struct lpfc_dmabuf *d_buf, *dnext;
16439 struct hbq_dmabuf *dmabuf = NULL;
16440
16441 /* start with the oldest sequence on the rcv list */
16442 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16443 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16444 list_del_init(&dmabuf->hbuf.list);
16445 list_for_each_entry_safe(d_buf, dnext,
16446 &dmabuf->dbuf.list, list) {
16447 list_del_init(&d_buf->list);
16448 lpfc_in_buf_free(vport->phba, d_buf);
16449 }
16450 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16451 }
16452}
16453
16454/**
16455 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16456 * @vport: The vport that the received sequences were sent to.
16457 *
16458 * This function determines whether any received sequences have timed out by
16459 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16460 * indicates that there is at least one timed out sequence this routine will
16461 * go through the received sequences one at a time from most inactive to most
16462 * active to determine which ones need to be cleaned up. Once it has determined
16463 * that a sequence needs to be cleaned up it will simply free up the resources
16464 * without sending an abort.
16465 **/
16466void
16467lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16468{
16469 struct lpfc_dmabuf *h_buf, *hnext;
16470 struct lpfc_dmabuf *d_buf, *dnext;
16471 struct hbq_dmabuf *dmabuf = NULL;
16472 unsigned long timeout;
16473 int abort_count = 0;
16474
16475 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16476 vport->rcv_buffer_time_stamp);
16477 if (list_empty(&vport->rcv_buffer_list) ||
16478 time_before(jiffies, timeout))
16479 return;
16480 /* start with the oldest sequence on the rcv list */
16481 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16482 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16483 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16484 dmabuf->time_stamp);
16485 if (time_before(jiffies, timeout))
16486 break;
16487 abort_count++;
16488 list_del_init(&dmabuf->hbuf.list);
16489 list_for_each_entry_safe(d_buf, dnext,
16490 &dmabuf->dbuf.list, list) {
16491 list_del_init(&d_buf->list);
16492 lpfc_in_buf_free(vport->phba, d_buf);
16493 }
16494 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16495 }
16496 if (abort_count)
16497 lpfc_update_rcv_time_stamp(vport);
16498}
16499
16500/**
James Smart4f774512009-05-22 14:52:35 -040016501 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16502 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16503 *
16504 * This function searches through the existing incomplete sequences that have
16505 * been sent to this @vport. If the frame matches one of the incomplete
16506 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16507 * make up that sequence. If no sequence is found that matches this frame then
16508 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16509 * This function returns a pointer to the first dmabuf in the sequence list that
16510 * the frame was linked to.
16511 **/
16512static struct hbq_dmabuf *
16513lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16514{
16515 struct fc_frame_header *new_hdr;
16516 struct fc_frame_header *temp_hdr;
16517 struct lpfc_dmabuf *d_buf;
16518 struct lpfc_dmabuf *h_buf;
16519 struct hbq_dmabuf *seq_dmabuf = NULL;
16520 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016521 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016522
James Smart4d9ab992009-10-02 15:16:39 -040016523 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016524 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016525 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016526
James Smart4f774512009-05-22 14:52:35 -040016527 /* Use the hdr_buf to find the sequence that this frame belongs to */
16528 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16529 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16530 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16531 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16532 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16533 continue;
16534 /* found a pending sequence that matches this frame */
16535 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16536 break;
16537 }
16538 if (!seq_dmabuf) {
16539 /*
16540 * This indicates first frame received for this sequence.
16541 * Queue the buffer on the vport's rcv_buffer_list.
16542 */
16543 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016544 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016545 return dmabuf;
16546 }
16547 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016548 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16549 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016550 list_del_init(&seq_dmabuf->hbuf.list);
16551 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16552 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016553 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016554 return dmabuf;
16555 }
James Smart45ed1192009-10-02 15:17:02 -040016556 /* move this sequence to the tail to indicate a young sequence */
16557 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16558 seq_dmabuf->time_stamp = jiffies;
16559 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016560 if (list_empty(&seq_dmabuf->dbuf.list)) {
16561 temp_hdr = dmabuf->hbuf.virt;
16562 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16563 return seq_dmabuf;
16564 }
James Smart4f774512009-05-22 14:52:35 -040016565 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016566 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16567 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016568 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16569 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16570 /*
16571 * If the frame's sequence count is greater than the frame on
16572 * the list then insert the frame right after this frame
16573 */
James Smarteeead812009-12-21 17:01:23 -050016574 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16575 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016576 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016577 found = 1;
16578 break;
James Smart4f774512009-05-22 14:52:35 -040016579 }
James Smart4360ca92015-12-16 18:12:04 -050016580
16581 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16582 break;
16583 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016584 }
James Smart4360ca92015-12-16 18:12:04 -050016585
16586 if (found)
16587 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016588 return NULL;
16589}
16590
16591/**
James Smart6669f9b2009-10-02 15:16:45 -040016592 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16593 * @vport: pointer to a vitural port
16594 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16595 *
16596 * This function tries to abort from the partially assembed sequence, described
16597 * by the information from basic abbort @dmabuf. It checks to see whether such
16598 * partially assembled sequence held by the driver. If so, it shall free up all
16599 * the frames from the partially assembled sequence.
16600 *
16601 * Return
16602 * true -- if there is matching partially assembled sequence present and all
16603 * the frames freed with the sequence;
16604 * false -- if there is no matching partially assembled sequence present so
16605 * nothing got aborted in the lower layer driver
16606 **/
16607static bool
16608lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16609 struct hbq_dmabuf *dmabuf)
16610{
16611 struct fc_frame_header *new_hdr;
16612 struct fc_frame_header *temp_hdr;
16613 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16614 struct hbq_dmabuf *seq_dmabuf = NULL;
16615
16616 /* Use the hdr_buf to find the sequence that matches this frame */
16617 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16618 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16619 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16620 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16621 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16622 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16623 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16624 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16625 continue;
16626 /* found a pending sequence that matches this frame */
16627 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16628 break;
16629 }
16630
16631 /* Free up all the frames from the partially assembled sequence */
16632 if (seq_dmabuf) {
16633 list_for_each_entry_safe(d_buf, n_buf,
16634 &seq_dmabuf->dbuf.list, list) {
16635 list_del_init(&d_buf->list);
16636 lpfc_in_buf_free(vport->phba, d_buf);
16637 }
16638 return true;
16639 }
16640 return false;
16641}
16642
16643/**
James Smart6dd9e312013-01-03 15:43:37 -050016644 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16645 * @vport: pointer to a vitural port
16646 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16647 *
16648 * This function tries to abort from the assembed sequence from upper level
16649 * protocol, described by the information from basic abbort @dmabuf. It
16650 * checks to see whether such pending context exists at upper level protocol.
16651 * If so, it shall clean up the pending context.
16652 *
16653 * Return
16654 * true -- if there is matching pending context of the sequence cleaned
16655 * at ulp;
16656 * false -- if there is no matching pending context of the sequence present
16657 * at ulp.
16658 **/
16659static bool
16660lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16661{
16662 struct lpfc_hba *phba = vport->phba;
16663 int handled;
16664
16665 /* Accepting abort at ulp with SLI4 only */
16666 if (phba->sli_rev < LPFC_SLI_REV4)
16667 return false;
16668
16669 /* Register all caring upper level protocols to attend abort */
16670 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16671 if (handled)
16672 return true;
16673
16674 return false;
16675}
16676
16677/**
James Smart546fc852011-03-11 16:06:29 -050016678 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016679 * @phba: Pointer to HBA context object.
16680 * @cmd_iocbq: pointer to the command iocbq structure.
16681 * @rsp_iocbq: pointer to the response iocbq structure.
16682 *
James Smart546fc852011-03-11 16:06:29 -050016683 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016684 * event. It properly releases the memory allocated to the sequence abort
16685 * accept iocb.
16686 **/
16687static void
James Smart546fc852011-03-11 16:06:29 -050016688lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016689 struct lpfc_iocbq *cmd_iocbq,
16690 struct lpfc_iocbq *rsp_iocbq)
16691{
James Smart6dd9e312013-01-03 15:43:37 -050016692 struct lpfc_nodelist *ndlp;
16693
16694 if (cmd_iocbq) {
16695 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16696 lpfc_nlp_put(ndlp);
16697 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016698 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016699 }
James Smart6b5151f2012-01-18 16:24:06 -050016700
16701 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16702 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16704 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16705 rsp_iocbq->iocb.ulpStatus,
16706 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016707}
16708
16709/**
James Smart6d368e52011-05-24 11:44:12 -040016710 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16711 * @phba: Pointer to HBA context object.
16712 * @xri: xri id in transaction.
16713 *
16714 * This function validates the xri maps to the known range of XRIs allocated an
16715 * used by the driver.
16716 **/
James Smart7851fe22011-07-22 18:36:52 -040016717uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016718lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16719 uint16_t xri)
16720{
James Smarta2fc4aef2014-09-03 12:57:55 -040016721 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016722
16723 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16724 if (xri == phba->sli4_hba.xri_ids[i])
16725 return i;
16726 }
16727 return NO_XRI;
16728}
16729
James Smart6d368e52011-05-24 11:44:12 -040016730/**
James Smart546fc852011-03-11 16:06:29 -050016731 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016732 * @phba: Pointer to HBA context object.
16733 * @fc_hdr: pointer to a FC frame header.
16734 *
James Smart546fc852011-03-11 16:06:29 -050016735 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016736 * event after aborting the sequence handling.
16737 **/
James Smart86c67372017-04-21 16:05:04 -070016738void
James Smart6dd9e312013-01-03 15:43:37 -050016739lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16740 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016741{
James Smart6dd9e312013-01-03 15:43:37 -050016742 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016743 struct lpfc_iocbq *ctiocb = NULL;
16744 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016745 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016746 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016747 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016748 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016749
16750 if (!lpfc_is_link_up(phba))
16751 return;
16752
16753 sid = sli4_sid_from_fc_hdr(fc_hdr);
16754 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016755 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016756
James Smart6dd9e312013-01-03 15:43:37 -050016757 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016758 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016759 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016760 if (!ndlp) {
16761 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16762 "1268 Failed to allocate ndlp for "
16763 "oxid:x%x SID:x%x\n", oxid, sid);
16764 return;
16765 }
James Smart6dd9e312013-01-03 15:43:37 -050016766 /* Put ndlp onto pport node list */
16767 lpfc_enqueue_node(vport, ndlp);
16768 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16769 /* re-setup ndlp without removing from node list */
16770 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16771 if (!ndlp) {
16772 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16773 "3275 Failed to active ndlp found "
16774 "for oxid:x%x SID:x%x\n", oxid, sid);
16775 return;
16776 }
James Smart6669f9b2009-10-02 15:16:45 -040016777 }
16778
James Smart546fc852011-03-11 16:06:29 -050016779 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016780 ctiocb = lpfc_sli_get_iocbq(phba);
16781 if (!ctiocb)
16782 return;
16783
James Smart5ffc2662009-11-18 15:39:44 -050016784 /* Extract the F_CTL field from FC_HDR */
16785 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16786
James Smart6669f9b2009-10-02 15:16:45 -040016787 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016788 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016789 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016790 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16791 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16792 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16793
16794 /* Fill in the rest of iocb fields */
16795 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16796 icmd->ulpBdeCount = 0;
16797 icmd->ulpLe = 1;
16798 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016799 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016800 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016801
James Smart6669f9b2009-10-02 15:16:45 -040016802 ctiocb->iocb_cmpl = NULL;
16803 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016804 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016805 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016806 ctiocb->sli4_xritag = NO_XRI;
16807
James Smartee0f4fe2012-05-09 21:19:14 -040016808 if (fctl & FC_FC_EX_CTX)
16809 /* Exchange responder sent the abort so we
16810 * own the oxid.
16811 */
16812 xri = oxid;
16813 else
16814 xri = rxid;
16815 lxri = lpfc_sli4_xri_inrange(phba, xri);
16816 if (lxri != NO_XRI)
16817 lpfc_set_rrq_active(phba, ndlp, lxri,
16818 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016819 /* For BA_ABTS from exchange responder, if the logical xri with
16820 * the oxid maps to the FCP XRI range, the port no longer has
16821 * that exchange context, send a BLS_RJT. Override the IOCB for
16822 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016823 */
James Smart6dd9e312013-01-03 15:43:37 -050016824 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016825 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016826 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16827 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16828 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16829 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16830 }
16831
16832 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16833 * the driver no longer has that exchange, send a BLS_RJT. Override
16834 * the IOCB for a BA_RJT.
16835 */
16836 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -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 }
James Smart6669f9b2009-10-02 15:16:45 -040016842
James Smart5ffc2662009-11-18 15:39:44 -050016843 if (fctl & FC_FC_EX_CTX) {
16844 /* ABTS sent by responder to CT exchange, construction
16845 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16846 * field and RX_ID from ABTS for RX_ID field.
16847 */
James Smart546fc852011-03-11 16:06:29 -050016848 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016849 } else {
16850 /* ABTS sent by initiator to CT exchange, construction
16851 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016852 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016853 */
James Smart546fc852011-03-11 16:06:29 -050016854 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016855 }
James Smartf09c3ac2012-03-01 22:33:29 -050016856 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016857 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016858
James Smart546fc852011-03-11 16:06:29 -050016859 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016860 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16861 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16862 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016863
16864 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16865 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016866 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16867 "2925 Failed to issue CT ABTS RSP x%x on "
16868 "xri x%x, Data x%x\n",
16869 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16870 phba->link_state);
16871 lpfc_nlp_put(ndlp);
16872 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016873 lpfc_sli_release_iocbq(phba, ctiocb);
16874 }
James Smart6669f9b2009-10-02 15:16:45 -040016875}
16876
16877/**
16878 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16879 * @vport: Pointer to the vport on which this sequence was received
16880 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16881 *
16882 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16883 * receive sequence is only partially assembed by the driver, it shall abort
16884 * the partially assembled frames for the sequence. Otherwise, if the
16885 * unsolicited receive sequence has been completely assembled and passed to
16886 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16887 * unsolicited sequence has been aborted. After that, it will issue a basic
16888 * accept to accept the abort.
16889 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016890static void
James Smart6669f9b2009-10-02 15:16:45 -040016891lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16892 struct hbq_dmabuf *dmabuf)
16893{
16894 struct lpfc_hba *phba = vport->phba;
16895 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016896 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016897 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016898
James Smart6669f9b2009-10-02 15:16:45 -040016899 /* Make a copy of fc_hdr before the dmabuf being released */
16900 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016901 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016902
James Smart5ffc2662009-11-18 15:39:44 -050016903 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016904 /* ABTS by responder to exchange, no cleanup needed */
16905 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016906 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016907 /* ABTS by initiator to exchange, need to do cleanup */
16908 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16909 if (aborted == false)
16910 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016911 }
James Smart6dd9e312013-01-03 15:43:37 -050016912 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16913
James Smart86c67372017-04-21 16:05:04 -070016914 if (phba->nvmet_support) {
16915 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16916 return;
16917 }
16918
James Smart6dd9e312013-01-03 15:43:37 -050016919 /* Respond with BA_ACC or BA_RJT accordingly */
16920 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016921}
16922
16923/**
James Smart4f774512009-05-22 14:52:35 -040016924 * lpfc_seq_complete - Indicates if a sequence is complete
16925 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16926 *
16927 * This function checks the sequence, starting with the frame described by
16928 * @dmabuf, to see if all the frames associated with this sequence are present.
16929 * the frames associated with this sequence are linked to the @dmabuf using the
16930 * dbuf list. This function looks for two major things. 1) That the first frame
16931 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16932 * set. 3) That there are no holes in the sequence count. The function will
16933 * return 1 when the sequence is complete, otherwise it will return 0.
16934 **/
16935static int
16936lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16937{
16938 struct fc_frame_header *hdr;
16939 struct lpfc_dmabuf *d_buf;
16940 struct hbq_dmabuf *seq_dmabuf;
16941 uint32_t fctl;
16942 int seq_count = 0;
16943
16944 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16945 /* make sure first fame of sequence has a sequence count of zero */
16946 if (hdr->fh_seq_cnt != seq_count)
16947 return 0;
16948 fctl = (hdr->fh_f_ctl[0] << 16 |
16949 hdr->fh_f_ctl[1] << 8 |
16950 hdr->fh_f_ctl[2]);
16951 /* If last frame of sequence we can return success. */
16952 if (fctl & FC_FC_END_SEQ)
16953 return 1;
16954 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16955 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16956 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16957 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016958 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016959 return 0;
16960 fctl = (hdr->fh_f_ctl[0] << 16 |
16961 hdr->fh_f_ctl[1] << 8 |
16962 hdr->fh_f_ctl[2]);
16963 /* If last frame of sequence we can return success. */
16964 if (fctl & FC_FC_END_SEQ)
16965 return 1;
16966 }
16967 return 0;
16968}
16969
16970/**
16971 * lpfc_prep_seq - Prep sequence for ULP processing
16972 * @vport: Pointer to the vport on which this sequence was received
16973 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16974 *
16975 * This function takes a sequence, described by a list of frames, and creates
16976 * a list of iocbq structures to describe the sequence. This iocbq list will be
16977 * used to issue to the generic unsolicited sequence handler. This routine
16978 * returns a pointer to the first iocbq in the list. If the function is unable
16979 * to allocate an iocbq then it throw out the received frames that were not
16980 * able to be described and return a pointer to the first iocbq. If unable to
16981 * allocate any iocbqs (including the first) this function will return NULL.
16982 **/
16983static struct lpfc_iocbq *
16984lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16985{
James Smart7851fe22011-07-22 18:36:52 -040016986 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016987 struct lpfc_dmabuf *d_buf, *n_buf;
16988 struct lpfc_iocbq *first_iocbq, *iocbq;
16989 struct fc_frame_header *fc_hdr;
16990 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016991 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016992 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016993
16994 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16995 /* remove from receive buffer list */
16996 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016997 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016998 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016999 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017000 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017001 /* Get an iocbq struct to fill in. */
17002 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17003 if (first_iocbq) {
17004 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017005 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017006 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017007 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017008
17009 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17010 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17011 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17012 first_iocbq->iocb.un.rcvels.parmRo =
17013 sli4_did_from_fc_hdr(fc_hdr);
17014 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17015 } else
17016 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017017 first_iocbq->iocb.ulpContext = NO_XRI;
17018 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17019 be16_to_cpu(fc_hdr->fh_ox_id);
17020 /* iocbq is prepped for internal consumption. Physical vpi. */
17021 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17022 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017023 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017024 tot_len = bf_get(lpfc_rcqe_length,
17025 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17026
James Smart4f774512009-05-22 14:52:35 -040017027 first_iocbq->context2 = &seq_dmabuf->dbuf;
17028 first_iocbq->context3 = NULL;
17029 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017030 if (tot_len > LPFC_DATA_BUF_SIZE)
17031 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017032 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017033 else
17034 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17035
James Smart4f774512009-05-22 14:52:35 -040017036 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017037
James Smart7851fe22011-07-22 18:36:52 -040017038 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017039 }
17040 iocbq = first_iocbq;
17041 /*
17042 * Each IOCBq can have two Buffers assigned, so go through the list
17043 * of buffers for this sequence and save two buffers in each IOCBq
17044 */
17045 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17046 if (!iocbq) {
17047 lpfc_in_buf_free(vport->phba, d_buf);
17048 continue;
17049 }
17050 if (!iocbq->context3) {
17051 iocbq->context3 = d_buf;
17052 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017053 /* We need to get the size out of the right CQE */
17054 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17055 len = bf_get(lpfc_rcqe_length,
17056 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017057 pbde = (struct ulp_bde64 *)
17058 &iocbq->iocb.unsli3.sli3Words[4];
17059 if (len > LPFC_DATA_BUF_SIZE)
17060 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17061 else
17062 pbde->tus.f.bdeSize = len;
17063
James Smart7851fe22011-07-22 18:36:52 -040017064 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17065 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017066 } else {
17067 iocbq = lpfc_sli_get_iocbq(vport->phba);
17068 if (!iocbq) {
17069 if (first_iocbq) {
17070 first_iocbq->iocb.ulpStatus =
17071 IOSTAT_FCP_RSP_ERROR;
17072 first_iocbq->iocb.un.ulpWord[4] =
17073 IOERR_NO_RESOURCES;
17074 }
17075 lpfc_in_buf_free(vport->phba, d_buf);
17076 continue;
17077 }
James Smart7851fe22011-07-22 18:36:52 -040017078 /* We need to get the size out of the right CQE */
17079 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17080 len = bf_get(lpfc_rcqe_length,
17081 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017082 iocbq->context2 = d_buf;
17083 iocbq->context3 = NULL;
17084 iocbq->iocb.ulpBdeCount = 1;
17085 if (len > LPFC_DATA_BUF_SIZE)
17086 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17087 LPFC_DATA_BUF_SIZE;
17088 else
17089 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17090
James Smart7851fe22011-07-22 18:36:52 -040017091 tot_len += len;
17092 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17093
James Smart4f774512009-05-22 14:52:35 -040017094 iocbq->iocb.un.rcvels.remoteID = sid;
17095 list_add_tail(&iocbq->list, &first_iocbq->list);
17096 }
17097 }
17098 return first_iocbq;
17099}
17100
James Smart6669f9b2009-10-02 15:16:45 -040017101static void
17102lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17103 struct hbq_dmabuf *seq_dmabuf)
17104{
17105 struct fc_frame_header *fc_hdr;
17106 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17107 struct lpfc_hba *phba = vport->phba;
17108
17109 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17110 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17111 if (!iocbq) {
17112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17113 "2707 Ring %d handler: Failed to allocate "
17114 "iocb Rctl x%x Type x%x received\n",
17115 LPFC_ELS_RING,
17116 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17117 return;
17118 }
17119 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017120 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017121 iocbq, fc_hdr->fh_r_ctl,
17122 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017124 "2540 Ring %d handler: unexpected Rctl "
17125 "x%x Type x%x received\n",
17126 LPFC_ELS_RING,
17127 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17128
17129 /* Free iocb created in lpfc_prep_seq */
17130 list_for_each_entry_safe(curr_iocb, next_iocb,
17131 &iocbq->list, list) {
17132 list_del_init(&curr_iocb->list);
17133 lpfc_sli_release_iocbq(phba, curr_iocb);
17134 }
17135 lpfc_sli_release_iocbq(phba, iocbq);
17136}
17137
James Smartae9e28f2017-05-15 15:20:51 -070017138static void
17139lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17140 struct lpfc_iocbq *rspiocb)
17141{
17142 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17143
17144 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017145 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017146 kfree(pcmd);
17147 lpfc_sli_release_iocbq(phba, cmdiocb);
17148}
17149
17150static void
17151lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17152 struct hbq_dmabuf *dmabuf)
17153{
17154 struct fc_frame_header *fc_hdr;
17155 struct lpfc_hba *phba = vport->phba;
17156 struct lpfc_iocbq *iocbq = NULL;
17157 union lpfc_wqe *wqe;
17158 struct lpfc_dmabuf *pcmd = NULL;
17159 uint32_t frame_len;
17160 int rc;
17161
17162 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17163 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17164
17165 /* Send the received frame back */
17166 iocbq = lpfc_sli_get_iocbq(phba);
17167 if (!iocbq)
17168 goto exit;
17169
17170 /* Allocate buffer for command payload */
17171 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17172 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017173 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017174 &pcmd->phys);
17175 if (!pcmd || !pcmd->virt)
17176 goto exit;
17177
17178 INIT_LIST_HEAD(&pcmd->list);
17179
17180 /* copyin the payload */
17181 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17182
17183 /* fill in BDE's for command */
17184 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17185 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17186 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17187 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17188
17189 iocbq->context2 = pcmd;
17190 iocbq->vport = vport;
17191 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17192 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17193
17194 /*
17195 * Setup rest of the iocb as though it were a WQE
17196 * Build the SEND_FRAME WQE
17197 */
17198 wqe = (union lpfc_wqe *)&iocbq->iocb;
17199
17200 wqe->send_frame.frame_len = frame_len;
17201 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17202 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17203 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17204 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17205 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17206 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17207
17208 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17209 iocbq->iocb.ulpLe = 1;
17210 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17211 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17212 if (rc == IOCB_ERROR)
17213 goto exit;
17214
17215 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17216 return;
17217
17218exit:
17219 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17220 "2023 Unable to process MDS loopback frame\n");
17221 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017222 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017223 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017224 if (iocbq)
17225 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017226 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17227}
17228
James Smart4f774512009-05-22 14:52:35 -040017229/**
17230 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17231 * @phba: Pointer to HBA context object.
17232 *
17233 * This function is called with no lock held. This function processes all
17234 * the received buffers and gives it to upper layers when a received buffer
17235 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017236 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017237 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17238 * appropriate receive function when the final frame in a sequence is received.
17239 **/
James Smart4d9ab992009-10-02 15:16:39 -040017240void
17241lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17242 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017243{
James Smart4d9ab992009-10-02 15:16:39 -040017244 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017245 struct fc_frame_header *fc_hdr;
17246 struct lpfc_vport *vport;
17247 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017248 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017249
James Smart4f774512009-05-22 14:52:35 -040017250 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017251 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017252
James Smart4d9ab992009-10-02 15:16:39 -040017253 /* check to see if this a valid type of frame */
17254 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17255 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17256 return;
17257 }
James Smart2ea259e2017-02-12 13:52:27 -080017258
James Smart7851fe22011-07-22 18:36:52 -040017259 if ((bf_get(lpfc_cqe_code,
17260 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17261 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17262 &dmabuf->cq_event.cqe.rcqe_cmpl);
17263 else
17264 fcfi = bf_get(lpfc_rcqe_fcf_id,
17265 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017266
James Smartae9e28f2017-05-15 15:20:51 -070017267 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17268 vport = phba->pport;
17269 /* Handle MDS Loopback frames */
17270 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17271 return;
17272 }
17273
James Smart895427b2017-02-12 13:52:30 -080017274 /* d_id this frame is directed to */
17275 did = sli4_did_from_fc_hdr(fc_hdr);
17276
17277 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017278 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017279 /* throw out the frame */
17280 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17281 return;
17282 }
James Smart939723a2012-05-09 21:19:03 -040017283
James Smart939723a2012-05-09 21:19:03 -040017284 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17285 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17286 (did != Fabric_DID)) {
17287 /*
17288 * Throw out the frame if we are not pt2pt.
17289 * The pt2pt protocol allows for discovery frames
17290 * to be received without a registered VPI.
17291 */
17292 if (!(vport->fc_flag & FC_PT2PT) ||
17293 (phba->link_state == LPFC_HBA_READY)) {
17294 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17295 return;
17296 }
17297 }
17298
James Smart6669f9b2009-10-02 15:16:45 -040017299 /* Handle the basic abort sequence (BA_ABTS) event */
17300 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17301 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17302 return;
17303 }
17304
James Smart4d9ab992009-10-02 15:16:39 -040017305 /* Link this frame */
17306 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17307 if (!seq_dmabuf) {
17308 /* unable to add frame to vport - throw it out */
17309 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17310 return;
17311 }
17312 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017313 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017314 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017315
James Smart6669f9b2009-10-02 15:16:45 -040017316 /* Send the complete sequence to the upper layer protocol */
17317 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017318}
James Smart6fb120a2009-05-22 14:52:59 -040017319
17320/**
17321 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17322 * @phba: pointer to lpfc hba data structure.
17323 *
17324 * This routine is invoked to post rpi header templates to the
17325 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017326 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17327 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017328 *
17329 * This routine does not require any locks. It's usage is expected
17330 * to be driver load or reset recovery when the driver is
17331 * sequential.
17332 *
17333 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017334 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017335 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017336 * When this error occurs, the driver is not guaranteed
17337 * to have any rpi regions posted to the device and
17338 * must either attempt to repost the regions or take a
17339 * fatal error.
17340 **/
17341int
17342lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17343{
17344 struct lpfc_rpi_hdr *rpi_page;
17345 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017346 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017347
James Smart6d368e52011-05-24 11:44:12 -040017348 /* SLI4 ports that support extents do not require RPI headers. */
17349 if (!phba->sli4_hba.rpi_hdrs_in_use)
17350 goto exit;
17351 if (phba->sli4_hba.extents_in_use)
17352 return -EIO;
17353
James Smart6fb120a2009-05-22 14:52:59 -040017354 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017355 /*
17356 * Assign the rpi headers a physical rpi only if the driver
17357 * has not initialized those resources. A port reset only
17358 * needs the headers posted.
17359 */
17360 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17361 LPFC_RPI_RSRC_RDY)
17362 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17363
James Smart6fb120a2009-05-22 14:52:59 -040017364 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17365 if (rc != MBX_SUCCESS) {
17366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17367 "2008 Error %d posting all rpi "
17368 "headers\n", rc);
17369 rc = -EIO;
17370 break;
17371 }
17372 }
17373
James Smart6d368e52011-05-24 11:44:12 -040017374 exit:
17375 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17376 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017377 return rc;
17378}
17379
17380/**
17381 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17382 * @phba: pointer to lpfc hba data structure.
17383 * @rpi_page: pointer to the rpi memory region.
17384 *
17385 * This routine is invoked to post a single rpi header to the
17386 * HBA consistent with the SLI-4 interface spec. This memory region
17387 * maps up to 64 rpi context regions.
17388 *
17389 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017390 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017391 * -ENOMEM - No available memory
17392 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017393 **/
17394int
17395lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17396{
17397 LPFC_MBOXQ_t *mboxq;
17398 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17399 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017400 uint32_t shdr_status, shdr_add_status;
17401 union lpfc_sli4_cfg_shdr *shdr;
17402
James Smart6d368e52011-05-24 11:44:12 -040017403 /* SLI4 ports that support extents do not require RPI headers. */
17404 if (!phba->sli4_hba.rpi_hdrs_in_use)
17405 return rc;
17406 if (phba->sli4_hba.extents_in_use)
17407 return -EIO;
17408
James Smart6fb120a2009-05-22 14:52:59 -040017409 /* The port is notified of the header region via a mailbox command. */
17410 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17411 if (!mboxq) {
17412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17413 "2001 Unable to allocate memory for issuing "
17414 "SLI_CONFIG_SPECIAL mailbox command\n");
17415 return -ENOMEM;
17416 }
17417
17418 /* Post all rpi memory regions to the port. */
17419 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017420 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17421 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17422 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017423 sizeof(struct lpfc_sli4_cfg_mhdr),
17424 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017425
17426
17427 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017428 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17429 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017430 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17431 hdr_tmpl, rpi_page->page_count);
17432
James Smart6fb120a2009-05-22 14:52:59 -040017433 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17434 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017435 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017436 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17437 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17438 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17439 if (rc != MBX_TIMEOUT)
17440 mempool_free(mboxq, phba->mbox_mem_pool);
17441 if (shdr_status || shdr_add_status || rc) {
17442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17443 "2514 POST_RPI_HDR mailbox failed with "
17444 "status x%x add_status x%x, mbx status x%x\n",
17445 shdr_status, shdr_add_status, rc);
17446 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017447 } else {
17448 /*
17449 * The next_rpi stores the next logical module-64 rpi value used
17450 * to post physical rpis in subsequent rpi postings.
17451 */
17452 spin_lock_irq(&phba->hbalock);
17453 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17454 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017455 }
17456 return rc;
17457}
17458
17459/**
17460 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17461 * @phba: pointer to lpfc hba data structure.
17462 *
17463 * This routine is invoked to post rpi header templates to the
17464 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017465 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17466 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017467 *
17468 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017469 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017470 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17471 **/
17472int
17473lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17474{
James Smart6d368e52011-05-24 11:44:12 -040017475 unsigned long rpi;
17476 uint16_t max_rpi, rpi_limit;
17477 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017478 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017479 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017480
James Smart6fb120a2009-05-22 14:52:59 -040017481 /*
James Smart6d368e52011-05-24 11:44:12 -040017482 * Fetch the next logical rpi. Because this index is logical,
17483 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017484 */
James Smart4902b382013-10-10 12:20:35 -040017485 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017486 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17487 rpi_limit = phba->sli4_hba.next_rpi;
17488
James Smart6d368e52011-05-24 11:44:12 -040017489 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17490 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017491 rpi = LPFC_RPI_ALLOC_ERROR;
17492 else {
17493 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17494 phba->sli4_hba.max_cfg_param.rpi_used++;
17495 phba->sli4_hba.rpi_count++;
17496 }
James Smartbe6bb942015-04-07 15:07:22 -040017497 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17498 "0001 rpi:%x max:%x lim:%x\n",
17499 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017500
17501 /*
17502 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017503 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017504 */
17505 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17506 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017507 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017508 return rpi;
17509 }
17510
17511 /*
James Smart6d368e52011-05-24 11:44:12 -040017512 * RPI header postings are not required for SLI4 ports capable of
17513 * extents.
17514 */
17515 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017516 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017517 return rpi;
17518 }
17519
17520 /*
James Smart6fb120a2009-05-22 14:52:59 -040017521 * If the driver is running low on rpi resources, allocate another
17522 * page now. Note that the next_rpi value is used because
17523 * it represents how many are actually in use whereas max_rpi notes
17524 * how many are supported max by the device.
17525 */
James Smart6d368e52011-05-24 11:44:12 -040017526 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017527 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017528 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17529 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17530 if (!rpi_hdr) {
17531 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17532 "2002 Error Could not grow rpi "
17533 "count\n");
17534 } else {
James Smart6d368e52011-05-24 11:44:12 -040017535 lrpi = rpi_hdr->start_rpi;
17536 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017537 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17538 }
17539 }
17540
17541 return rpi;
17542}
17543
17544/**
17545 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17546 * @phba: pointer to lpfc hba data structure.
17547 *
17548 * This routine is invoked to release an rpi to the pool of
17549 * available rpis maintained by the driver.
17550 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017551static void
James Smartd7c47992010-06-08 18:31:54 -040017552__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17553{
17554 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17555 phba->sli4_hba.rpi_count--;
17556 phba->sli4_hba.max_cfg_param.rpi_used--;
17557 }
17558}
17559
17560/**
17561 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17562 * @phba: pointer to lpfc hba data structure.
17563 *
17564 * This routine is invoked to release an rpi to the pool of
17565 * available rpis maintained by the driver.
17566 **/
17567void
James Smart6fb120a2009-05-22 14:52:59 -040017568lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17569{
17570 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017571 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017572 spin_unlock_irq(&phba->hbalock);
17573}
17574
17575/**
17576 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17577 * @phba: pointer to lpfc hba data structure.
17578 *
17579 * This routine is invoked to remove the memory region that
17580 * provided rpi via a bitmask.
17581 **/
17582void
17583lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17584{
17585 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017586 kfree(phba->sli4_hba.rpi_ids);
17587 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017588}
17589
17590/**
17591 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17592 * @phba: pointer to lpfc hba data structure.
17593 *
17594 * This routine is invoked to remove the memory region that
17595 * provided rpi via a bitmask.
17596 **/
17597int
James Smart6b5151f2012-01-18 16:24:06 -050017598lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17599 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017600{
17601 LPFC_MBOXQ_t *mboxq;
17602 struct lpfc_hba *phba = ndlp->phba;
17603 int rc;
17604
17605 /* The port is notified of the header region via a mailbox command. */
17606 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17607 if (!mboxq)
17608 return -ENOMEM;
17609
17610 /* Post all rpi memory regions to the port. */
17611 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017612 if (cmpl) {
17613 mboxq->mbox_cmpl = cmpl;
17614 mboxq->context1 = arg;
17615 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017616 } else
17617 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017618 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017619 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17620 if (rc == MBX_NOT_FINISHED) {
17621 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17622 "2010 Resume RPI Mailbox failed "
17623 "status %d, mbxStatus x%x\n", rc,
17624 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17625 mempool_free(mboxq, phba->mbox_mem_pool);
17626 return -EIO;
17627 }
17628 return 0;
17629}
17630
17631/**
17632 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017633 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017634 *
James Smart76a95d72010-11-20 23:11:48 -050017635 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017636 *
17637 * Returns:
17638 * 0 success
17639 * -Evalue otherwise
17640 **/
17641int
James Smart76a95d72010-11-20 23:11:48 -050017642lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017643{
17644 LPFC_MBOXQ_t *mboxq;
17645 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017646 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017647 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017648 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017649 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17650 if (!mboxq)
17651 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017652 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017653 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017654 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017655 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017656 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017657 "2022 INIT VPI Mailbox failed "
17658 "status %d, mbxStatus x%x\n", rc,
17659 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017660 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017661 }
James Smart6a9c52c2009-10-02 15:16:51 -040017662 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017663 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017664
17665 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017666}
17667
17668/**
17669 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17670 * @phba: pointer to lpfc hba data structure.
17671 * @mboxq: Pointer to mailbox object.
17672 *
17673 * This routine is invoked to manually add a single FCF record. The caller
17674 * must pass a completely initialized FCF_Record. This routine takes
17675 * care of the nonembedded mailbox operations.
17676 **/
17677static void
17678lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17679{
17680 void *virt_addr;
17681 union lpfc_sli4_cfg_shdr *shdr;
17682 uint32_t shdr_status, shdr_add_status;
17683
17684 virt_addr = mboxq->sge_array->addr[0];
17685 /* The IOCTL status is embedded in the mailbox subheader. */
17686 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17687 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17688 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17689
17690 if ((shdr_status || shdr_add_status) &&
17691 (shdr_status != STATUS_FCF_IN_USE))
17692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17693 "2558 ADD_FCF_RECORD mailbox failed with "
17694 "status x%x add_status x%x\n",
17695 shdr_status, shdr_add_status);
17696
17697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17698}
17699
17700/**
17701 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17702 * @phba: pointer to lpfc hba data structure.
17703 * @fcf_record: pointer to the initialized fcf record to add.
17704 *
17705 * This routine is invoked to manually add a single FCF record. The caller
17706 * must pass a completely initialized FCF_Record. This routine takes
17707 * care of the nonembedded mailbox operations.
17708 **/
17709int
17710lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17711{
17712 int rc = 0;
17713 LPFC_MBOXQ_t *mboxq;
17714 uint8_t *bytep;
17715 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017716 struct lpfc_mbx_sge sge;
17717 uint32_t alloc_len, req_len;
17718 uint32_t fcfindex;
17719
17720 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17721 if (!mboxq) {
17722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17723 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17724 return -ENOMEM;
17725 }
17726
17727 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17728 sizeof(uint32_t);
17729
17730 /* Allocate DMA memory and set up the non-embedded mailbox command */
17731 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17732 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17733 req_len, LPFC_SLI4_MBX_NEMBED);
17734 if (alloc_len < req_len) {
17735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17736 "2523 Allocated DMA memory size (x%x) is "
17737 "less than the requested DMA memory "
17738 "size (x%x)\n", alloc_len, req_len);
17739 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17740 return -ENOMEM;
17741 }
17742
17743 /*
17744 * Get the first SGE entry from the non-embedded DMA memory. This
17745 * routine only uses a single SGE.
17746 */
17747 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017748 virt_addr = mboxq->sge_array->addr[0];
17749 /*
17750 * Configure the FCF record for FCFI 0. This is the driver's
17751 * hardcoded default and gets used in nonFIP mode.
17752 */
17753 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17754 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17755 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17756
17757 /*
17758 * Copy the fcf_index and the FCF Record Data. The data starts after
17759 * the FCoE header plus word10. The data copy needs to be endian
17760 * correct.
17761 */
17762 bytep += sizeof(uint32_t);
17763 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17764 mboxq->vport = phba->pport;
17765 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17766 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17767 if (rc == MBX_NOT_FINISHED) {
17768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17769 "2515 ADD_FCF_RECORD mailbox failed with "
17770 "status 0x%x\n", rc);
17771 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17772 rc = -EIO;
17773 } else
17774 rc = 0;
17775
17776 return rc;
17777}
17778
17779/**
17780 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17781 * @phba: pointer to lpfc hba data structure.
17782 * @fcf_record: pointer to the fcf record to write the default data.
17783 * @fcf_index: FCF table entry index.
17784 *
17785 * This routine is invoked to build the driver's default FCF record. The
17786 * values used are hardcoded. This routine handles memory initialization.
17787 *
17788 **/
17789void
17790lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17791 struct fcf_record *fcf_record,
17792 uint16_t fcf_index)
17793{
17794 memset(fcf_record, 0, sizeof(struct fcf_record));
17795 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17796 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17797 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17798 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17799 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17800 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17801 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17802 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17803 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17804 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17805 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17806 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17807 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017808 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017809 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17810 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17811 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17812 /* Set the VLAN bit map */
17813 if (phba->valid_vlan) {
17814 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17815 = 1 << (phba->vlan_id % 8);
17816 }
17817}
17818
17819/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017820 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017821 * @phba: pointer to lpfc hba data structure.
17822 * @fcf_index: FCF table entry offset.
17823 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017824 * This routine is invoked to scan the entire FCF table by reading FCF
17825 * record and processing it one at a time starting from the @fcf_index
17826 * for initial FCF discovery or fast FCF failover rediscovery.
17827 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017828 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017829 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017830 **/
17831int
James Smart0c9ab6f2010-02-26 14:15:57 -050017832lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017833{
17834 int rc = 0, error;
17835 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017836
James Smart32b97932009-07-19 10:01:21 -040017837 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017838 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017839 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17840 if (!mboxq) {
17841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17842 "2000 Failed to allocate mbox for "
17843 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017844 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017845 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017846 }
James Smartecfd03c2010-02-12 14:41:27 -050017847 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017848 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017849 if (rc) {
17850 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017851 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017852 }
James Smartecfd03c2010-02-12 14:41:27 -050017853 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017854 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017855 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017856
17857 spin_lock_irq(&phba->hbalock);
17858 phba->hba_flag |= FCF_TS_INPROG;
17859 spin_unlock_irq(&phba->hbalock);
17860
James Smart6fb120a2009-05-22 14:52:59 -040017861 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017862 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017863 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017864 else {
James Smart38b92ef2010-08-04 16:11:39 -040017865 /* Reset eligible FCF count for new scan */
17866 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017867 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017868 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017869 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017870fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017871 if (error) {
17872 if (mboxq)
17873 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017874 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017875 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017876 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017877 spin_unlock_irq(&phba->hbalock);
17878 }
James Smart6fb120a2009-05-22 14:52:59 -040017879 return error;
17880}
James Smarta0c87cb2009-07-19 10:01:10 -040017881
17882/**
James Smarta93ff372010-10-22 11:06:08 -040017883 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017884 * @phba: pointer to lpfc hba data structure.
17885 * @fcf_index: FCF table entry offset.
17886 *
17887 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017888 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017889 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017890 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017891 * otherwise.
17892 **/
17893int
17894lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17895{
17896 int rc = 0, error;
17897 LPFC_MBOXQ_t *mboxq;
17898
17899 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17900 if (!mboxq) {
17901 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17902 "2763 Failed to allocate mbox for "
17903 "READ_FCF cmd\n");
17904 error = -ENOMEM;
17905 goto fail_fcf_read;
17906 }
17907 /* Construct the read FCF record mailbox command */
17908 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17909 if (rc) {
17910 error = -EINVAL;
17911 goto fail_fcf_read;
17912 }
17913 /* Issue the mailbox command asynchronously */
17914 mboxq->vport = phba->pport;
17915 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17916 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17917 if (rc == MBX_NOT_FINISHED)
17918 error = -EIO;
17919 else
17920 error = 0;
17921
17922fail_fcf_read:
17923 if (error && mboxq)
17924 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17925 return error;
17926}
17927
17928/**
17929 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17930 * @phba: pointer to lpfc hba data structure.
17931 * @fcf_index: FCF table entry offset.
17932 *
17933 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017934 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017935 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017936 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017937 * otherwise.
17938 **/
17939int
17940lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17941{
17942 int rc = 0, error;
17943 LPFC_MBOXQ_t *mboxq;
17944
17945 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17946 if (!mboxq) {
17947 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17948 "2758 Failed to allocate mbox for "
17949 "READ_FCF cmd\n");
17950 error = -ENOMEM;
17951 goto fail_fcf_read;
17952 }
17953 /* Construct the read FCF record mailbox command */
17954 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17955 if (rc) {
17956 error = -EINVAL;
17957 goto fail_fcf_read;
17958 }
17959 /* Issue the mailbox command asynchronously */
17960 mboxq->vport = phba->pport;
17961 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17962 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17963 if (rc == MBX_NOT_FINISHED)
17964 error = -EIO;
17965 else
17966 error = 0;
17967
17968fail_fcf_read:
17969 if (error && mboxq)
17970 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17971 return error;
17972}
17973
17974/**
James Smartf5cb5302015-12-16 18:11:52 -050017975 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017976 * phba pointer to the lpfc_hba struct for this port.
17977 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17978 * routine when the rr_bmask is empty. The FCF indecies are put into the
17979 * rr_bmask based on their priority level. Starting from the highest priority
17980 * to the lowest. The most likely FCF candidate will be in the highest
17981 * priority group. When this routine is called it searches the fcf_pri list for
17982 * next lowest priority group and repopulates the rr_bmask with only those
17983 * fcf_indexes.
17984 * returns:
17985 * 1=success 0=failure
17986 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017987static int
James Smart7d791df2011-07-22 18:37:52 -040017988lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17989{
17990 uint16_t next_fcf_pri;
17991 uint16_t last_index;
17992 struct lpfc_fcf_pri *fcf_pri;
17993 int rc;
17994 int ret = 0;
17995
17996 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17997 LPFC_SLI4_FCF_TBL_INDX_MAX);
17998 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17999 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018000
18001 /* Verify the priority list has 2 or more entries */
18002 spin_lock_irq(&phba->hbalock);
18003 if (list_empty(&phba->fcf.fcf_pri_list) ||
18004 list_is_singular(&phba->fcf.fcf_pri_list)) {
18005 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018006 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18007 "3061 Last IDX %d\n", last_index);
18008 return 0; /* Empty rr list */
18009 }
James Smart25626692013-03-01 16:36:54 -050018010 spin_unlock_irq(&phba->hbalock);
18011
James Smart7d791df2011-07-22 18:37:52 -040018012 next_fcf_pri = 0;
18013 /*
18014 * Clear the rr_bmask and set all of the bits that are at this
18015 * priority.
18016 */
18017 memset(phba->fcf.fcf_rr_bmask, 0,
18018 sizeof(*phba->fcf.fcf_rr_bmask));
18019 spin_lock_irq(&phba->hbalock);
18020 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18021 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18022 continue;
18023 /*
18024 * the 1st priority that has not FLOGI failed
18025 * will be the highest.
18026 */
18027 if (!next_fcf_pri)
18028 next_fcf_pri = fcf_pri->fcf_rec.priority;
18029 spin_unlock_irq(&phba->hbalock);
18030 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18031 rc = lpfc_sli4_fcf_rr_index_set(phba,
18032 fcf_pri->fcf_rec.fcf_index);
18033 if (rc)
18034 return 0;
18035 }
18036 spin_lock_irq(&phba->hbalock);
18037 }
18038 /*
18039 * if next_fcf_pri was not set above and the list is not empty then
18040 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018041 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018042 */
18043 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18044 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18045 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18046 /*
18047 * the 1st priority that has not FLOGI failed
18048 * will be the highest.
18049 */
18050 if (!next_fcf_pri)
18051 next_fcf_pri = fcf_pri->fcf_rec.priority;
18052 spin_unlock_irq(&phba->hbalock);
18053 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18054 rc = lpfc_sli4_fcf_rr_index_set(phba,
18055 fcf_pri->fcf_rec.fcf_index);
18056 if (rc)
18057 return 0;
18058 }
18059 spin_lock_irq(&phba->hbalock);
18060 }
18061 } else
18062 ret = 1;
18063 spin_unlock_irq(&phba->hbalock);
18064
18065 return ret;
18066}
18067/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018068 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18069 * @phba: pointer to lpfc hba data structure.
18070 *
18071 * This routine is to get the next eligible FCF record index in a round
18072 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018073 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018074 * shall be returned, otherwise, the next eligible FCF record's index
18075 * shall be returned.
18076 **/
18077uint16_t
18078lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18079{
18080 uint16_t next_fcf_index;
18081
James Smart421c6622013-01-03 15:44:16 -050018082initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018083 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018084 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18085
James Smart7d791df2011-07-22 18:37:52 -040018086next_priority:
James Smart421c6622013-01-03 15:44:16 -050018087 /* Determine the next fcf index to check */
18088 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018089 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18090 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018091 next_fcf_index);
18092
James Smart0c9ab6f2010-02-26 14:15:57 -050018093 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018094 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18095 /*
18096 * If we have wrapped then we need to clear the bits that
18097 * have been tested so that we can detect when we should
18098 * change the priority level.
18099 */
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, 0);
James Smart7d791df2011-07-22 18:37:52 -040018102 }
18103
James Smart0c9ab6f2010-02-26 14:15:57 -050018104
James Smart3804dc82010-07-14 15:31:37 -040018105 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018106 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18107 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18108 /*
18109 * If next fcf index is not found check if there are lower
18110 * Priority level fcf's in the fcf_priority list.
18111 * Set up the rr_bmask with all of the avaiable fcf bits
18112 * at that level and continue the selection process.
18113 */
18114 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018115 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018116 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18117 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018118 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18119 return LPFC_FCOE_FCF_NEXT_NONE;
18120 else {
18121 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18122 "3063 Only FCF available idx %d, flag %x\n",
18123 next_fcf_index,
18124 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18125 return next_fcf_index;
18126 }
James Smart3804dc82010-07-14 15:31:37 -040018127 }
18128
James Smart7d791df2011-07-22 18:37:52 -040018129 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18130 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018131 LPFC_FCF_FLOGI_FAILED) {
18132 if (list_is_singular(&phba->fcf.fcf_pri_list))
18133 return LPFC_FCOE_FCF_NEXT_NONE;
18134
James Smart7d791df2011-07-22 18:37:52 -040018135 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018136 }
James Smart7d791df2011-07-22 18:37:52 -040018137
James Smart3804dc82010-07-14 15:31:37 -040018138 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018139 "2845 Get next roundrobin failover FCF (x%x)\n",
18140 next_fcf_index);
18141
James Smart0c9ab6f2010-02-26 14:15:57 -050018142 return next_fcf_index;
18143}
18144
18145/**
18146 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18147 * @phba: pointer to lpfc hba data structure.
18148 *
18149 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018150 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018151 * does not go beyond the range of the driver allocated bmask dimension
18152 * before setting the bit.
18153 *
18154 * Returns 0 if the index bit successfully set, otherwise, it returns
18155 * -EINVAL.
18156 **/
18157int
18158lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18159{
18160 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18161 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018162 "2610 FCF (x%x) reached driver's book "
18163 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018164 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18165 return -EINVAL;
18166 }
18167 /* Set the eligible FCF record index bmask */
18168 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18169
James Smart3804dc82010-07-14 15:31:37 -040018170 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018171 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018172 "bmask\n", fcf_index);
18173
James Smart0c9ab6f2010-02-26 14:15:57 -050018174 return 0;
18175}
18176
18177/**
James Smart3804dc82010-07-14 15:31:37 -040018178 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018179 * @phba: pointer to lpfc hba data structure.
18180 *
18181 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018182 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018183 * does not go beyond the range of the driver allocated bmask dimension
18184 * before clearing the bit.
18185 **/
18186void
18187lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18188{
James Smart9a803a72013-09-06 12:17:56 -040018189 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018190 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18191 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018192 "2762 FCF (x%x) reached driver's book "
18193 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018194 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18195 return;
18196 }
18197 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018198 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018199 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18200 list) {
James Smart7d791df2011-07-22 18:37:52 -040018201 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18202 list_del_init(&fcf_pri->list);
18203 break;
18204 }
18205 }
18206 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018207 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018208
18209 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018210 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018211 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018212}
18213
18214/**
James Smartecfd03c2010-02-12 14:41:27 -050018215 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18216 * @phba: pointer to lpfc hba data structure.
18217 *
18218 * This routine is the completion routine for the rediscover FCF table mailbox
18219 * command. If the mailbox command returned failure, it will try to stop the
18220 * FCF rediscover wait timer.
18221 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018222static void
James Smartecfd03c2010-02-12 14:41:27 -050018223lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18224{
18225 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18226 uint32_t shdr_status, shdr_add_status;
18227
18228 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18229
18230 shdr_status = bf_get(lpfc_mbox_hdr_status,
18231 &redisc_fcf->header.cfg_shdr.response);
18232 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18233 &redisc_fcf->header.cfg_shdr.response);
18234 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018235 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018236 "2746 Requesting for FCF rediscovery failed "
18237 "status x%x add_status x%x\n",
18238 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018239 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018240 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018241 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018242 spin_unlock_irq(&phba->hbalock);
18243 /*
18244 * CVL event triggered FCF rediscover request failed,
18245 * last resort to re-try current registered FCF entry.
18246 */
18247 lpfc_retry_pport_discovery(phba);
18248 } else {
18249 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018250 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018251 spin_unlock_irq(&phba->hbalock);
18252 /*
18253 * DEAD FCF event triggered FCF rediscover request
18254 * failed, last resort to fail over as a link down
18255 * to FCF registration.
18256 */
18257 lpfc_sli4_fcf_dead_failthrough(phba);
18258 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018259 } else {
18260 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018261 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018262 /*
18263 * Start FCF rediscovery wait timer for pending FCF
18264 * before rescan FCF record table.
18265 */
18266 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018267 }
James Smartecfd03c2010-02-12 14:41:27 -050018268
18269 mempool_free(mbox, phba->mbox_mem_pool);
18270}
18271
18272/**
James Smart3804dc82010-07-14 15:31:37 -040018273 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018274 * @phba: pointer to lpfc hba data structure.
18275 *
18276 * This routine is invoked to request for rediscovery of the entire FCF table
18277 * by the port.
18278 **/
18279int
18280lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18281{
18282 LPFC_MBOXQ_t *mbox;
18283 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18284 int rc, length;
18285
James Smart0c9ab6f2010-02-26 14:15:57 -050018286 /* Cancel retry delay timers to all vports before FCF rediscover */
18287 lpfc_cancel_all_vport_retry_delay_timer(phba);
18288
James Smartecfd03c2010-02-12 14:41:27 -050018289 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18290 if (!mbox) {
18291 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18292 "2745 Failed to allocate mbox for "
18293 "requesting FCF rediscover.\n");
18294 return -ENOMEM;
18295 }
18296
18297 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18298 sizeof(struct lpfc_sli4_cfg_mhdr));
18299 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18300 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18301 length, LPFC_SLI4_MBX_EMBED);
18302
18303 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18304 /* Set count to 0 for invalidating the entire FCF database */
18305 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18306
18307 /* Issue the mailbox command asynchronously */
18308 mbox->vport = phba->pport;
18309 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18310 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18311
18312 if (rc == MBX_NOT_FINISHED) {
18313 mempool_free(mbox, phba->mbox_mem_pool);
18314 return -EIO;
18315 }
18316 return 0;
18317}
18318
18319/**
James Smartfc2b9892010-02-26 14:15:29 -050018320 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18321 * @phba: pointer to lpfc hba data structure.
18322 *
18323 * This function is the failover routine as a last resort to the FCF DEAD
18324 * event when driver failed to perform fast FCF failover.
18325 **/
18326void
18327lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18328{
18329 uint32_t link_state;
18330
18331 /*
18332 * Last resort as FCF DEAD event failover will treat this as
18333 * a link down, but save the link state because we don't want
18334 * it to be changed to Link Down unless it is already down.
18335 */
18336 link_state = phba->link_state;
18337 lpfc_linkdown(phba);
18338 phba->link_state = link_state;
18339
18340 /* Unregister FCF if no devices connected to it */
18341 lpfc_unregister_unused_fcf(phba);
18342}
18343
18344/**
James Smart026abb82011-12-13 13:20:45 -050018345 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018346 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018347 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018348 *
James Smart026abb82011-12-13 13:20:45 -050018349 * This function gets SLI3 port configure region 23 data through memory dump
18350 * mailbox command. When it successfully retrieves data, the size of the data
18351 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018352 **/
James Smart026abb82011-12-13 13:20:45 -050018353static uint32_t
18354lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018355{
18356 LPFC_MBOXQ_t *pmb = NULL;
18357 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018358 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018359 int rc;
18360
James Smart026abb82011-12-13 13:20:45 -050018361 if (!rgn23_data)
18362 return 0;
18363
James Smarta0c87cb2009-07-19 10:01:10 -040018364 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18365 if (!pmb) {
18366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018367 "2600 failed to allocate mailbox memory\n");
18368 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018369 }
18370 mb = &pmb->u.mb;
18371
James Smarta0c87cb2009-07-19 10:01:10 -040018372 do {
18373 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18374 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18375
18376 if (rc != MBX_SUCCESS) {
18377 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018378 "2601 failed to read config "
18379 "region 23, rc 0x%x Status 0x%x\n",
18380 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018381 mb->un.varDmp.word_cnt = 0;
18382 }
18383 /*
18384 * dump mem may return a zero when finished or we got a
18385 * mailbox error, either way we are done.
18386 */
18387 if (mb->un.varDmp.word_cnt == 0)
18388 break;
18389 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18390 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18391
18392 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018393 rgn23_data + offset,
18394 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018395 offset += mb->un.varDmp.word_cnt;
18396 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18397
James Smart026abb82011-12-13 13:20:45 -050018398 mempool_free(pmb, phba->mbox_mem_pool);
18399 return offset;
18400}
18401
18402/**
18403 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18404 * @phba: pointer to lpfc hba data structure.
18405 * @rgn23_data: pointer to configure region 23 data.
18406 *
18407 * This function gets SLI4 port configure region 23 data through memory dump
18408 * mailbox command. When it successfully retrieves data, the size of the data
18409 * will be returned, otherwise, 0 will be returned.
18410 **/
18411static uint32_t
18412lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18413{
18414 LPFC_MBOXQ_t *mboxq = NULL;
18415 struct lpfc_dmabuf *mp = NULL;
18416 struct lpfc_mqe *mqe;
18417 uint32_t data_length = 0;
18418 int rc;
18419
18420 if (!rgn23_data)
18421 return 0;
18422
18423 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18424 if (!mboxq) {
18425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18426 "3105 failed to allocate mailbox memory\n");
18427 return 0;
18428 }
18429
18430 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18431 goto out;
18432 mqe = &mboxq->u.mqe;
18433 mp = (struct lpfc_dmabuf *) mboxq->context1;
18434 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18435 if (rc)
18436 goto out;
18437 data_length = mqe->un.mb_words[5];
18438 if (data_length == 0)
18439 goto out;
18440 if (data_length > DMP_RGN23_SIZE) {
18441 data_length = 0;
18442 goto out;
18443 }
18444 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18445out:
18446 mempool_free(mboxq, phba->mbox_mem_pool);
18447 if (mp) {
18448 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18449 kfree(mp);
18450 }
18451 return data_length;
18452}
18453
18454/**
18455 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18456 * @phba: pointer to lpfc hba data structure.
18457 *
18458 * This function read region 23 and parse TLV for port status to
18459 * decide if the user disaled the port. If the TLV indicates the
18460 * port is disabled, the hba_flag is set accordingly.
18461 **/
18462void
18463lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18464{
18465 uint8_t *rgn23_data = NULL;
18466 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18467 uint32_t offset = 0;
18468
18469 /* Get adapter Region 23 data */
18470 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18471 if (!rgn23_data)
18472 goto out;
18473
18474 if (phba->sli_rev < LPFC_SLI_REV4)
18475 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18476 else {
18477 if_type = bf_get(lpfc_sli_intf_if_type,
18478 &phba->sli4_hba.sli_intf);
18479 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18480 goto out;
18481 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18482 }
James Smarta0c87cb2009-07-19 10:01:10 -040018483
18484 if (!data_size)
18485 goto out;
18486
18487 /* Check the region signature first */
18488 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18490 "2619 Config region 23 has bad signature\n");
18491 goto out;
18492 }
18493 offset += 4;
18494
18495 /* Check the data structure version */
18496 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18498 "2620 Config region 23 has bad version\n");
18499 goto out;
18500 }
18501 offset += 4;
18502
18503 /* Parse TLV entries in the region */
18504 while (offset < data_size) {
18505 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18506 break;
18507 /*
18508 * If the TLV is not driver specific TLV or driver id is
18509 * not linux driver id, skip the record.
18510 */
18511 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18512 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18513 (rgn23_data[offset + 3] != 0)) {
18514 offset += rgn23_data[offset + 1] * 4 + 4;
18515 continue;
18516 }
18517
18518 /* Driver found a driver specific TLV in the config region */
18519 sub_tlv_len = rgn23_data[offset + 1] * 4;
18520 offset += 4;
18521 tlv_offset = 0;
18522
18523 /*
18524 * Search for configured port state sub-TLV.
18525 */
18526 while ((offset < data_size) &&
18527 (tlv_offset < sub_tlv_len)) {
18528 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18529 offset += 4;
18530 tlv_offset += 4;
18531 break;
18532 }
18533 if (rgn23_data[offset] != PORT_STE_TYPE) {
18534 offset += rgn23_data[offset + 1] * 4 + 4;
18535 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18536 continue;
18537 }
18538
18539 /* This HBA contains PORT_STE configured */
18540 if (!rgn23_data[offset + 2])
18541 phba->hba_flag |= LINK_DISABLED;
18542
18543 goto out;
18544 }
18545 }
James Smart026abb82011-12-13 13:20:45 -050018546
James Smarta0c87cb2009-07-19 10:01:10 -040018547out:
James Smarta0c87cb2009-07-19 10:01:10 -040018548 kfree(rgn23_data);
18549 return;
18550}
James Smart695a8142010-01-26 23:08:03 -050018551
18552/**
James Smart52d52442011-05-24 11:42:45 -040018553 * lpfc_wr_object - write an object to the firmware
18554 * @phba: HBA structure that indicates port to create a queue on.
18555 * @dmabuf_list: list of dmabufs to write to the port.
18556 * @size: the total byte value of the objects to write to the port.
18557 * @offset: the current offset to be used to start the transfer.
18558 *
18559 * This routine will create a wr_object mailbox command to send to the port.
18560 * the mailbox command will be constructed using the dma buffers described in
18561 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18562 * BDEs that the imbedded mailbox can support. The @offset variable will be
18563 * used to indicate the starting offset of the transfer and will also return
18564 * the offset after the write object mailbox has completed. @size is used to
18565 * determine the end of the object and whether the eof bit should be set.
18566 *
18567 * Return 0 is successful and offset will contain the the new offset to use
18568 * for the next write.
18569 * Return negative value for error cases.
18570 **/
18571int
18572lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18573 uint32_t size, uint32_t *offset)
18574{
18575 struct lpfc_mbx_wr_object *wr_object;
18576 LPFC_MBOXQ_t *mbox;
18577 int rc = 0, i = 0;
18578 uint32_t shdr_status, shdr_add_status;
18579 uint32_t mbox_tmo;
18580 union lpfc_sli4_cfg_shdr *shdr;
18581 struct lpfc_dmabuf *dmabuf;
18582 uint32_t written = 0;
18583
18584 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18585 if (!mbox)
18586 return -ENOMEM;
18587
18588 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18589 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18590 sizeof(struct lpfc_mbx_wr_object) -
18591 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18592
18593 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18594 wr_object->u.request.write_offset = *offset;
18595 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18596 wr_object->u.request.object_name[0] =
18597 cpu_to_le32(wr_object->u.request.object_name[0]);
18598 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18599 list_for_each_entry(dmabuf, dmabuf_list, list) {
18600 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18601 break;
18602 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18603 wr_object->u.request.bde[i].addrHigh =
18604 putPaddrHigh(dmabuf->phys);
18605 if (written + SLI4_PAGE_SIZE >= size) {
18606 wr_object->u.request.bde[i].tus.f.bdeSize =
18607 (size - written);
18608 written += (size - written);
18609 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18610 } else {
18611 wr_object->u.request.bde[i].tus.f.bdeSize =
18612 SLI4_PAGE_SIZE;
18613 written += SLI4_PAGE_SIZE;
18614 }
18615 i++;
18616 }
18617 wr_object->u.request.bde_count = i;
18618 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18619 if (!phba->sli4_hba.intr_enable)
18620 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18621 else {
James Smarta183a152011-10-10 21:32:43 -040018622 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18624 }
18625 /* The IOCTL status is embedded in the mailbox subheader. */
18626 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18627 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18628 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18629 if (rc != MBX_TIMEOUT)
18630 mempool_free(mbox, phba->mbox_mem_pool);
18631 if (shdr_status || shdr_add_status || rc) {
18632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18633 "3025 Write Object mailbox failed with "
18634 "status x%x add_status x%x, mbx status x%x\n",
18635 shdr_status, shdr_add_status, rc);
18636 rc = -ENXIO;
18637 } else
18638 *offset += wr_object->u.response.actual_write_length;
18639 return rc;
18640}
18641
18642/**
James Smart695a8142010-01-26 23:08:03 -050018643 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18644 * @vport: pointer to vport data structure.
18645 *
18646 * This function iterate through the mailboxq and clean up all REG_LOGIN
18647 * and REG_VPI mailbox commands associated with the vport. This function
18648 * is called when driver want to restart discovery of the vport due to
18649 * a Clear Virtual Link event.
18650 **/
18651void
18652lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18653{
18654 struct lpfc_hba *phba = vport->phba;
18655 LPFC_MBOXQ_t *mb, *nextmb;
18656 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018657 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018658 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018659 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018660 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018661 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018662
James Smartd439d282010-09-29 11:18:45 -040018663 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018664 spin_lock_irq(&phba->hbalock);
18665 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18666 if (mb->vport != vport)
18667 continue;
18668
18669 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18670 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18671 continue;
18672
James Smartd439d282010-09-29 11:18:45 -040018673 list_del(&mb->list);
18674 list_add_tail(&mb->list, &mbox_cmd_list);
18675 }
18676 /* Clean up active mailbox command with the vport */
18677 mb = phba->sli.mbox_active;
18678 if (mb && (mb->vport == vport)) {
18679 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18680 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18681 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18682 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18683 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18684 /* Put reference count for delayed processing */
18685 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18686 /* Unregister the RPI when mailbox complete */
18687 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18688 }
18689 }
James Smart63e801c2010-11-20 23:14:19 -050018690 /* Cleanup any mailbox completions which are not yet processed */
18691 do {
18692 restart_loop = 0;
18693 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18694 /*
18695 * If this mailox is already processed or it is
18696 * for another vport ignore it.
18697 */
18698 if ((mb->vport != vport) ||
18699 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18700 continue;
18701
18702 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18703 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18704 continue;
18705
18706 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18707 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18708 ndlp = (struct lpfc_nodelist *)mb->context2;
18709 /* Unregister the RPI when mailbox complete */
18710 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18711 restart_loop = 1;
18712 spin_unlock_irq(&phba->hbalock);
18713 spin_lock(shost->host_lock);
18714 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18715 spin_unlock(shost->host_lock);
18716 spin_lock_irq(&phba->hbalock);
18717 break;
18718 }
18719 }
18720 } while (restart_loop);
18721
James Smartd439d282010-09-29 11:18:45 -040018722 spin_unlock_irq(&phba->hbalock);
18723
18724 /* Release the cleaned-up mailbox commands */
18725 while (!list_empty(&mbox_cmd_list)) {
18726 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018727 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18728 mp = (struct lpfc_dmabuf *) (mb->context1);
18729 if (mp) {
18730 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18731 kfree(mp);
18732 }
James Smart78730cf2010-04-06 15:06:30 -040018733 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018734 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018735 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018736 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018737 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018738 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018739 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018740 }
James Smart695a8142010-01-26 23:08:03 -050018741 }
James Smart695a8142010-01-26 23:08:03 -050018742 mempool_free(mb, phba->mbox_mem_pool);
18743 }
James Smartd439d282010-09-29 11:18:45 -040018744
18745 /* Release the ndlp with the cleaned-up active mailbox command */
18746 if (act_mbx_ndlp) {
18747 spin_lock(shost->host_lock);
18748 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18749 spin_unlock(shost->host_lock);
18750 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018751 }
James Smart695a8142010-01-26 23:08:03 -050018752}
18753
James Smart2a9bf3d2010-06-07 15:24:45 -040018754/**
18755 * lpfc_drain_txq - Drain the txq
18756 * @phba: Pointer to HBA context object.
18757 *
18758 * This function attempt to submit IOCBs on the txq
18759 * to the adapter. For SLI4 adapters, the txq contains
18760 * ELS IOCBs that have been deferred because the there
18761 * are no SGLs. This congestion can occur with large
18762 * vport counts during node discovery.
18763 **/
18764
18765uint32_t
18766lpfc_drain_txq(struct lpfc_hba *phba)
18767{
18768 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018769 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018770 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018771 unsigned long iflags = 0;
18772 char *fail_msg = NULL;
18773 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018774 union lpfc_wqe128 wqe128;
18775 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018776 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018777
James Smart895427b2017-02-12 13:52:30 -080018778 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070018779 if (unlikely(!pring))
18780 return 0;
James Smart895427b2017-02-12 13:52:30 -080018781
James Smart398d81c2013-05-31 17:04:19 -040018782 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018783 list_for_each_entry(piocbq, &pring->txq, list) {
18784 txq_cnt++;
18785 }
18786
18787 if (txq_cnt > pring->txq_max)
18788 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018789
James Smart398d81c2013-05-31 17:04:19 -040018790 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018791
James Smart0e9bb8d2013-03-01 16:35:12 -050018792 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018793 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018794
James Smart19ca7602010-11-20 23:11:55 -050018795 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018796 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018797 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18799 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018800 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018801 break;
18802 }
James Smart895427b2017-02-12 13:52:30 -080018803 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018804 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018805 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018806 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018807 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018808 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018809 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018810
18811 /* The xri and iocb resources secured,
18812 * attempt to issue request
18813 */
James Smart6d368e52011-05-24 11:44:12 -040018814 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018815 piocbq->sli4_xritag = sglq->sli4_xritag;
18816 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18817 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018818 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018819 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018820 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018821 fail_msg = " - Wq is full";
18822 else
18823 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18824
18825 if (fail_msg) {
18826 /* Failed means we can't issue and need to cancel */
18827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18828 "2822 IOCB failed %s iotag 0x%x "
18829 "xri 0x%x\n",
18830 fail_msg,
18831 piocbq->iotag, piocbq->sli4_xritag);
18832 list_add_tail(&piocbq->list, &completions);
18833 }
James Smart398d81c2013-05-31 17:04:19 -040018834 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018835 }
18836
James Smart2a9bf3d2010-06-07 15:24:45 -040018837 /* Cancel all the IOCBs that cannot be issued */
18838 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18839 IOERR_SLI_ABORTED);
18840
James Smart0e9bb8d2013-03-01 16:35:12 -050018841 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018842}
James Smart895427b2017-02-12 13:52:30 -080018843
18844/**
18845 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18846 * @phba: Pointer to HBA context object.
18847 * @pwqe: Pointer to command WQE.
18848 * @sglq: Pointer to the scatter gather queue object.
18849 *
18850 * This routine converts the bpl or bde that is in the WQE
18851 * to a sgl list for the sli4 hardware. The physical address
18852 * of the bpl/bde is converted back to a virtual address.
18853 * If the WQE contains a BPL then the list of BDE's is
18854 * converted to sli4_sge's. If the WQE contains a single
18855 * BDE then it is converted to a single sli_sge.
18856 * The WQE is still in cpu endianness so the contents of
18857 * the bpl can be used without byte swapping.
18858 *
18859 * Returns valid XRI = Success, NO_XRI = Failure.
18860 */
18861static uint16_t
18862lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18863 struct lpfc_sglq *sglq)
18864{
18865 uint16_t xritag = NO_XRI;
18866 struct ulp_bde64 *bpl = NULL;
18867 struct ulp_bde64 bde;
18868 struct sli4_sge *sgl = NULL;
18869 struct lpfc_dmabuf *dmabuf;
18870 union lpfc_wqe *wqe;
18871 int numBdes = 0;
18872 int i = 0;
18873 uint32_t offset = 0; /* accumulated offset in the sg request list */
18874 int inbound = 0; /* number of sg reply entries inbound from firmware */
18875 uint32_t cmd;
18876
18877 if (!pwqeq || !sglq)
18878 return xritag;
18879
18880 sgl = (struct sli4_sge *)sglq->sgl;
18881 wqe = &pwqeq->wqe;
18882 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18883
18884 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18885 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18886 return sglq->sli4_xritag;
18887 numBdes = pwqeq->rsvd2;
18888 if (numBdes) {
18889 /* The addrHigh and addrLow fields within the WQE
18890 * have not been byteswapped yet so there is no
18891 * need to swap them back.
18892 */
18893 if (pwqeq->context3)
18894 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18895 else
18896 return xritag;
18897
18898 bpl = (struct ulp_bde64 *)dmabuf->virt;
18899 if (!bpl)
18900 return xritag;
18901
18902 for (i = 0; i < numBdes; i++) {
18903 /* Should already be byte swapped. */
18904 sgl->addr_hi = bpl->addrHigh;
18905 sgl->addr_lo = bpl->addrLow;
18906
18907 sgl->word2 = le32_to_cpu(sgl->word2);
18908 if ((i+1) == numBdes)
18909 bf_set(lpfc_sli4_sge_last, sgl, 1);
18910 else
18911 bf_set(lpfc_sli4_sge_last, sgl, 0);
18912 /* swap the size field back to the cpu so we
18913 * can assign it to the sgl.
18914 */
18915 bde.tus.w = le32_to_cpu(bpl->tus.w);
18916 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18917 /* The offsets in the sgl need to be accumulated
18918 * separately for the request and reply lists.
18919 * The request is always first, the reply follows.
18920 */
18921 switch (cmd) {
18922 case CMD_GEN_REQUEST64_WQE:
18923 /* add up the reply sg entries */
18924 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18925 inbound++;
18926 /* first inbound? reset the offset */
18927 if (inbound == 1)
18928 offset = 0;
18929 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18930 bf_set(lpfc_sli4_sge_type, sgl,
18931 LPFC_SGE_TYPE_DATA);
18932 offset += bde.tus.f.bdeSize;
18933 break;
18934 case CMD_FCP_TRSP64_WQE:
18935 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18936 bf_set(lpfc_sli4_sge_type, sgl,
18937 LPFC_SGE_TYPE_DATA);
18938 break;
18939 case CMD_FCP_TSEND64_WQE:
18940 case CMD_FCP_TRECEIVE64_WQE:
18941 bf_set(lpfc_sli4_sge_type, sgl,
18942 bpl->tus.f.bdeFlags);
18943 if (i < 3)
18944 offset = 0;
18945 else
18946 offset += bde.tus.f.bdeSize;
18947 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18948 break;
18949 }
18950 sgl->word2 = cpu_to_le32(sgl->word2);
18951 bpl++;
18952 sgl++;
18953 }
18954 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18955 /* The addrHigh and addrLow fields of the BDE have not
18956 * been byteswapped yet so they need to be swapped
18957 * before putting them in the sgl.
18958 */
18959 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18960 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18961 sgl->word2 = le32_to_cpu(sgl->word2);
18962 bf_set(lpfc_sli4_sge_last, sgl, 1);
18963 sgl->word2 = cpu_to_le32(sgl->word2);
18964 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18965 }
18966 return sglq->sli4_xritag;
18967}
18968
18969/**
18970 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18971 * @phba: Pointer to HBA context object.
18972 * @ring_number: Base sli ring number
18973 * @pwqe: Pointer to command WQE.
18974 **/
18975int
18976lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18977 struct lpfc_iocbq *pwqe)
18978{
18979 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018980 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018981 struct lpfc_queue *wq;
18982 struct lpfc_sglq *sglq;
18983 struct lpfc_sli_ring *pring;
18984 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070018985 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080018986
18987 /* NVME_LS and NVME_LS ABTS requests. */
18988 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18989 pring = phba->sli4_hba.nvmels_wq->pring;
18990 spin_lock_irqsave(&pring->ring_lock, iflags);
18991 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18992 if (!sglq) {
18993 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18994 return WQE_BUSY;
18995 }
18996 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18997 pwqe->sli4_xritag = sglq->sli4_xritag;
18998 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18999 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19000 return WQE_ERROR;
19001 }
19002 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19003 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019004 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19005 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019006 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019007 return ret;
James Smart895427b2017-02-12 13:52:30 -080019008 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019009
James Smart895427b2017-02-12 13:52:30 -080019010 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19011 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19012 return 0;
19013 }
19014
19015 /* NVME_FCREQ and NVME_ABTS requests */
19016 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19017 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19018 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19019
19020 spin_lock_irqsave(&pring->ring_lock, iflags);
19021 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19022 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19023 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019024 ret = lpfc_sli4_wq_put(wq, wqe);
19025 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019026 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019027 return ret;
James Smart895427b2017-02-12 13:52:30 -080019028 }
19029 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19030 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19031 return 0;
19032 }
19033
James Smartf358dd02017-02-12 13:52:34 -080019034 /* NVMET requests */
19035 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19036 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19037 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19038
19039 spin_lock_irqsave(&pring->ring_lock, iflags);
19040 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019041 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019042 if (pwqe->sli4_xritag == NO_XRI) {
19043 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19044 pwqe->sli4_xritag = sglq->sli4_xritag;
19045 }
19046 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19047 pwqe->sli4_xritag);
19048 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19049 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19050 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019051 ret = lpfc_sli4_wq_put(wq, wqe);
19052 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019053 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019054 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019055 }
19056 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19057 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19058 return 0;
19059 }
James Smart895427b2017-02-12 13:52:30 -080019060 return WQE_ERROR;
19061}