blob: 9feeaff47a5227baa190d8b510f6dd828d2fa6f2 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040032#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040033#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050034
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_hw.h"
37#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040039#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_crtn.h"
44#include "lpfc_logmsg.h"
45#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050046#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040047#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050048
49/* There are only four IOCB completion types. */
50typedef enum _lpfc_iocb_type {
51 LPFC_UNKNOWN_IOCB,
52 LPFC_UNSOL_IOCB,
53 LPFC_SOL_IOCB,
54 LPFC_ABORT_IOCB
55} lpfc_iocb_type;
56
James Smart4f774512009-05-22 14:52:35 -040057
58/* Provide function prototypes local to this module. */
59static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
60 uint32_t);
61static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040062 uint8_t *, uint32_t *);
63static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
64 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040065static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
66 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040067static IOCB_t *
68lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
69{
70 return &iocbq->iocb;
71}
72
73/**
74 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
75 * @q: The Work Queue to operate on.
76 * @wqe: The work Queue Entry to put on the Work queue.
77 *
78 * This routine will copy the contents of @wqe to the next available entry on
79 * the @q. This function will then ring the Work Queue Doorbell to signal the
80 * HBA to start processing the Work Queue Entry. This function returns 0 if
81 * successful. If no entries are available on @q then this function will return
82 * -ENOMEM.
83 * The caller is expected to hold the hbalock when calling this routine.
84 **/
85static uint32_t
86lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
87{
88 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
89 struct lpfc_register doorbell;
90 uint32_t host_index;
91
92 /* If the host has not yet processed the next entry then we are done */
93 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
94 return -ENOMEM;
95 /* set consumption flag every once in a while */
96 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
97 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
98
99 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
100
101 /* Update the host index before invoking device */
102 host_index = q->host_index;
103 q->host_index = ((q->host_index + 1) % q->entry_count);
104
105 /* Ring Doorbell */
106 doorbell.word0 = 0;
107 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
108 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
109 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
110 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
111 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
112
113 return 0;
114}
115
116/**
117 * lpfc_sli4_wq_release - Updates internal hba index for WQ
118 * @q: The Work Queue to operate on.
119 * @index: The index to advance the hba index to.
120 *
121 * This routine will update the HBA index of a queue to reflect consumption of
122 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
123 * an entry the host calls this function to update the queue's internal
124 * pointers. This routine returns the number of entries that were consumed by
125 * the HBA.
126 **/
127static uint32_t
128lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
129{
130 uint32_t released = 0;
131
132 if (q->hba_index == index)
133 return 0;
134 do {
135 q->hba_index = ((q->hba_index + 1) % q->entry_count);
136 released++;
137 } while (q->hba_index != index);
138 return released;
139}
140
141/**
142 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
143 * @q: The Mailbox Queue to operate on.
144 * @wqe: The Mailbox Queue Entry to put on the Work queue.
145 *
146 * This routine will copy the contents of @mqe to the next available entry on
147 * the @q. This function will then ring the Work Queue Doorbell to signal the
148 * HBA to start processing the Work Queue Entry. This function returns 0 if
149 * successful. If no entries are available on @q then this function will return
150 * -ENOMEM.
151 * The caller is expected to hold the hbalock when calling this routine.
152 **/
153static uint32_t
154lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
155{
156 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
157 struct lpfc_register doorbell;
158 uint32_t host_index;
159
160 /* If the host has not yet processed the next entry then we are done */
161 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
162 return -ENOMEM;
163 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
164 /* Save off the mailbox pointer for completion */
165 q->phba->mbox = (MAILBOX_t *)temp_mqe;
166
167 /* Update the host index before invoking device */
168 host_index = q->host_index;
169 q->host_index = ((q->host_index + 1) % q->entry_count);
170
171 /* Ring Doorbell */
172 doorbell.word0 = 0;
173 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
174 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
175 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
176 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
177 return 0;
178}
179
180/**
181 * lpfc_sli4_mq_release - Updates internal hba index for MQ
182 * @q: The Mailbox Queue to operate on.
183 *
184 * This routine will update the HBA index of a queue to reflect consumption of
185 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
186 * an entry the host calls this function to update the queue's internal
187 * pointers. This routine returns the number of entries that were consumed by
188 * the HBA.
189 **/
190static uint32_t
191lpfc_sli4_mq_release(struct lpfc_queue *q)
192{
193 /* Clear the mailbox pointer for completion */
194 q->phba->mbox = NULL;
195 q->hba_index = ((q->hba_index + 1) % q->entry_count);
196 return 1;
197}
198
199/**
200 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
201 * @q: The Event Queue to get the first valid EQE from
202 *
203 * This routine will get the first valid Event Queue Entry from @q, update
204 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
205 * the Queue (no more work to do), or the Queue is full of EQEs that have been
206 * processed, but not popped back to the HBA then this routine will return NULL.
207 **/
208static struct lpfc_eqe *
209lpfc_sli4_eq_get(struct lpfc_queue *q)
210{
211 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
212
213 /* If the next EQE is not valid then we are done */
214 if (!bf_get(lpfc_eqe_valid, eqe))
215 return NULL;
216 /* If the host has not yet processed the next entry then we are done */
217 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
218 return NULL;
219
220 q->hba_index = ((q->hba_index + 1) % q->entry_count);
221 return eqe;
222}
223
224/**
225 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
226 * @q: The Event Queue that the host has completed processing for.
227 * @arm: Indicates whether the host wants to arms this CQ.
228 *
229 * This routine will mark all Event Queue Entries on @q, from the last
230 * known completed entry to the last entry that was processed, as completed
231 * by clearing the valid bit for each completion queue entry. Then it will
232 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
233 * The internal host index in the @q will be updated by this routine to indicate
234 * that the host has finished processing the entries. The @arm parameter
235 * indicates that the queue should be rearmed when ringing the doorbell.
236 *
237 * This function will return the number of EQEs that were popped.
238 **/
239uint32_t
240lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
241{
242 uint32_t released = 0;
243 struct lpfc_eqe *temp_eqe;
244 struct lpfc_register doorbell;
245
246 /* while there are valid entries */
247 while (q->hba_index != q->host_index) {
248 temp_eqe = q->qe[q->host_index].eqe;
249 bf_set(lpfc_eqe_valid, temp_eqe, 0);
250 released++;
251 q->host_index = ((q->host_index + 1) % q->entry_count);
252 }
253 if (unlikely(released == 0 && !arm))
254 return 0;
255
256 /* ring doorbell for number popped */
257 doorbell.word0 = 0;
258 if (arm) {
259 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
260 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
261 }
262 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
263 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
264 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
265 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500266 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
267 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
268 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400269 return released;
270}
271
272/**
273 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
274 * @q: The Completion Queue to get the first valid CQE from
275 *
276 * This routine will get the first valid Completion Queue Entry from @q, update
277 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
278 * the Queue (no more work to do), or the Queue is full of CQEs that have been
279 * processed, but not popped back to the HBA then this routine will return NULL.
280 **/
281static struct lpfc_cqe *
282lpfc_sli4_cq_get(struct lpfc_queue *q)
283{
284 struct lpfc_cqe *cqe;
285
286 /* If the next CQE is not valid then we are done */
287 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
288 return NULL;
289 /* If the host has not yet processed the next entry then we are done */
290 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
291 return NULL;
292
293 cqe = q->qe[q->hba_index].cqe;
294 q->hba_index = ((q->hba_index + 1) % q->entry_count);
295 return cqe;
296}
297
298/**
299 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
300 * @q: The Completion Queue that the host has completed processing for.
301 * @arm: Indicates whether the host wants to arms this CQ.
302 *
303 * This routine will mark all Completion queue entries on @q, from the last
304 * known completed entry to the last entry that was processed, as completed
305 * by clearing the valid bit for each completion queue entry. Then it will
306 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
307 * The internal host index in the @q will be updated by this routine to indicate
308 * that the host has finished processing the entries. The @arm parameter
309 * indicates that the queue should be rearmed when ringing the doorbell.
310 *
311 * This function will return the number of CQEs that were released.
312 **/
313uint32_t
314lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
315{
316 uint32_t released = 0;
317 struct lpfc_cqe *temp_qe;
318 struct lpfc_register doorbell;
319
320 /* while there are valid entries */
321 while (q->hba_index != q->host_index) {
322 temp_qe = q->qe[q->host_index].cqe;
323 bf_set(lpfc_cqe_valid, temp_qe, 0);
324 released++;
325 q->host_index = ((q->host_index + 1) % q->entry_count);
326 }
327 if (unlikely(released == 0 && !arm))
328 return 0;
329
330 /* ring doorbell for number popped */
331 doorbell.word0 = 0;
332 if (arm)
333 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
334 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
335 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
336 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
337 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
338 return released;
339}
340
341/**
342 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
343 * @q: The Header Receive Queue to operate on.
344 * @wqe: The Receive Queue Entry to put on the Receive queue.
345 *
346 * This routine will copy the contents of @wqe to the next available entry on
347 * the @q. This function will then ring the Receive Queue Doorbell to signal the
348 * HBA to start processing the Receive Queue Entry. This function returns the
349 * index that the rqe was copied to if successful. If no entries are available
350 * on @q then this function will return -ENOMEM.
351 * The caller is expected to hold the hbalock when calling this routine.
352 **/
353static int
354lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
355 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
356{
357 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
358 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
359 struct lpfc_register doorbell;
360 int put_index = hq->host_index;
361
362 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
363 return -EINVAL;
364 if (hq->host_index != dq->host_index)
365 return -EINVAL;
366 /* If the host has not yet processed the next entry then we are done */
367 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
368 return -EBUSY;
369 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
370 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
371
372 /* Update the host index to point to the next slot */
373 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
374 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
375
376 /* Ring The Header Receive Queue Doorbell */
377 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
378 doorbell.word0 = 0;
379 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
380 LPFC_RQ_POST_BATCH);
381 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
382 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
383 }
384 return put_index;
385}
386
387/**
388 * lpfc_sli4_rq_release - Updates internal hba index for RQ
389 * @q: The Header Receive Queue to operate on.
390 *
391 * This routine will update the HBA index of a queue to reflect consumption of
392 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
393 * consumed an entry the host calls this function to update the queue's
394 * internal pointers. This routine returns the number of entries that were
395 * consumed by the HBA.
396 **/
397static uint32_t
398lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
399{
400 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
401 return 0;
402 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
403 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
404 return 1;
405}
406
James Smarte59058c2008-08-24 21:49:00 -0400407/**
James Smart3621a712009-04-06 18:47:14 -0400408 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400409 * @phba: Pointer to HBA context object.
410 * @pring: Pointer to driver SLI ring object.
411 *
412 * This function returns pointer to next command iocb entry
413 * in the command ring. The caller must hold hbalock to prevent
414 * other threads consume the next command iocb.
415 * SLI-2/SLI-3 provide different sized iocbs.
416 **/
James Smarted957682007-06-17 19:56:37 -0500417static inline IOCB_t *
418lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
419{
420 return (IOCB_t *) (((char *) pring->cmdringaddr) +
421 pring->cmdidx * phba->iocb_cmd_size);
422}
423
James Smarte59058c2008-08-24 21:49:00 -0400424/**
James Smart3621a712009-04-06 18:47:14 -0400425 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400426 * @phba: Pointer to HBA context object.
427 * @pring: Pointer to driver SLI ring object.
428 *
429 * This function returns pointer to next response iocb entry
430 * in the response ring. The caller must hold hbalock to make sure
431 * that no other thread consume the next response iocb.
432 * SLI-2/SLI-3 provide different sized iocbs.
433 **/
James Smarted957682007-06-17 19:56:37 -0500434static inline IOCB_t *
435lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
436{
437 return (IOCB_t *) (((char *) pring->rspringaddr) +
438 pring->rspidx * phba->iocb_rsp_size);
439}
440
James Smarte59058c2008-08-24 21:49:00 -0400441/**
James Smart3621a712009-04-06 18:47:14 -0400442 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400443 * @phba: Pointer to HBA context object.
444 *
445 * This function is called with hbalock held. This function
446 * allocates a new driver iocb object from the iocb pool. If the
447 * allocation is successful, it returns pointer to the newly
448 * allocated iocb object else it returns NULL.
449 **/
James Smart2e0fef82007-06-17 19:56:36 -0500450static struct lpfc_iocbq *
451__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400452{
453 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
454 struct lpfc_iocbq * iocbq = NULL;
455
456 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
457 return iocbq;
458}
459
James Smarte59058c2008-08-24 21:49:00 -0400460/**
James Smartda0436e2009-05-22 14:51:39 -0400461 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
462 * @phba: Pointer to HBA context object.
463 * @xritag: XRI value.
464 *
465 * This function clears the sglq pointer from the array of acive
466 * sglq's. The xritag that is passed in is used to index into the
467 * array. Before the xritag can be used it needs to be adjusted
468 * by subtracting the xribase.
469 *
470 * Returns sglq ponter = success, NULL = Failure.
471 **/
472static struct lpfc_sglq *
473__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
474{
475 uint16_t adj_xri;
476 struct lpfc_sglq *sglq;
477 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
478 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
479 return NULL;
480 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
481 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
482 return sglq;
483}
484
485/**
486 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
487 * @phba: Pointer to HBA context object.
488 * @xritag: XRI value.
489 *
490 * This function returns the sglq pointer from the array of acive
491 * sglq's. The xritag that is passed in is used to index into the
492 * array. Before the xritag can be used it needs to be adjusted
493 * by subtracting the xribase.
494 *
495 * Returns sglq ponter = success, NULL = Failure.
496 **/
James Smart0f65ff62010-02-26 14:14:23 -0500497struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400498__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
499{
500 uint16_t adj_xri;
501 struct lpfc_sglq *sglq;
502 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
503 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
504 return NULL;
505 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
506 return sglq;
507}
508
509/**
510 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
511 * @phba: Pointer to HBA context object.
512 *
513 * This function is called with hbalock held. This function
514 * Gets a new driver sglq object from the sglq list. If the
515 * list is not empty then it is successful, it returns pointer to the newly
516 * allocated sglq object else it returns NULL.
517 **/
518static struct lpfc_sglq *
519__lpfc_sli_get_sglq(struct lpfc_hba *phba)
520{
521 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
522 struct lpfc_sglq *sglq = NULL;
523 uint16_t adj_xri;
524 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart6a9c52c2009-10-02 15:16:51 -0400525 if (!sglq)
526 return NULL;
James Smartda0436e2009-05-22 14:51:39 -0400527 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
528 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
James Smart0f65ff62010-02-26 14:14:23 -0500529 sglq->state = SGL_ALLOCATED;
James Smartda0436e2009-05-22 14:51:39 -0400530 return sglq;
531}
532
533/**
James Smart3621a712009-04-06 18:47:14 -0400534 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400535 * @phba: Pointer to HBA context object.
536 *
537 * This function is called with no lock held. This function
538 * allocates a new driver iocb object from the iocb pool. If the
539 * allocation is successful, it returns pointer to the newly
540 * allocated iocb object else it returns NULL.
541 **/
James Smart2e0fef82007-06-17 19:56:36 -0500542struct lpfc_iocbq *
543lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500544{
James Smart2e0fef82007-06-17 19:56:36 -0500545 struct lpfc_iocbq * iocbq = NULL;
546 unsigned long iflags;
547
548 spin_lock_irqsave(&phba->hbalock, iflags);
549 iocbq = __lpfc_sli_get_iocbq(phba);
550 spin_unlock_irqrestore(&phba->hbalock, iflags);
551 return iocbq;
552}
553
James Smarte59058c2008-08-24 21:49:00 -0400554/**
James Smart4f774512009-05-22 14:52:35 -0400555 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
556 * @phba: Pointer to HBA context object.
557 * @iocbq: Pointer to driver iocb object.
558 *
559 * This function is called with hbalock held to release driver
560 * iocb object to the iocb pool. The iotag in the iocb object
561 * does not change for each use of the iocb object. This function
562 * clears all other fields of the iocb object when it is freed.
563 * The sqlq structure that holds the xritag and phys and virtual
564 * mappings for the scatter gather list is retrieved from the
565 * active array of sglq. The get of the sglq pointer also clears
566 * the entry in the array. If the status of the IO indiactes that
567 * this IO was aborted then the sglq entry it put on the
568 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
569 * IO has good status or fails for any other reason then the sglq
570 * entry is added to the free list (lpfc_sgl_list).
571 **/
572static void
573__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
574{
575 struct lpfc_sglq *sglq;
576 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
577 unsigned long iflag;
578
579 if (iocbq->sli4_xritag == NO_XRI)
580 sglq = NULL;
581 else
582 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
583 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -0500584 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
585 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -0400586 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
587 iflag);
588 list_add(&sglq->list,
589 &phba->sli4_hba.lpfc_abts_els_sgl_list);
590 spin_unlock_irqrestore(
591 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -0500592 } else {
593 sglq->state = SGL_FREED;
James Smart4f774512009-05-22 14:52:35 -0400594 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
James Smart0f65ff62010-02-26 14:14:23 -0500595 }
James Smart4f774512009-05-22 14:52:35 -0400596 }
597
598
599 /*
600 * Clean all volatile data fields, preserve iotag and node struct.
601 */
602 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
603 iocbq->sli4_xritag = NO_XRI;
604 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
605}
606
607/**
James Smart3772a992009-05-22 14:50:54 -0400608 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
609 * @phba: Pointer to HBA context object.
610 * @iocbq: Pointer to driver iocb object.
611 *
612 * This function is called with hbalock held to release driver
613 * iocb object to the iocb pool. The iotag in the iocb object
614 * does not change for each use of the iocb object. This function
615 * clears all other fields of the iocb object when it is freed.
616 **/
617static void
618__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
619{
620 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
621
622 /*
623 * Clean all volatile data fields, preserve iotag and node struct.
624 */
625 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
626 iocbq->sli4_xritag = NO_XRI;
627 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
628}
629
630/**
James Smart3621a712009-04-06 18:47:14 -0400631 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400632 * @phba: Pointer to HBA context object.
633 * @iocbq: Pointer to driver iocb object.
634 *
635 * This function is called with hbalock held to release driver
636 * iocb object to the iocb pool. The iotag in the iocb object
637 * does not change for each use of the iocb object. This function
638 * clears all other fields of the iocb object when it is freed.
639 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100640static void
James Smart2e0fef82007-06-17 19:56:36 -0500641__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
642{
James Smart3772a992009-05-22 14:50:54 -0400643 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500644}
645
James Smarte59058c2008-08-24 21:49:00 -0400646/**
James Smart3621a712009-04-06 18:47:14 -0400647 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400648 * @phba: Pointer to HBA context object.
649 * @iocbq: Pointer to driver iocb object.
650 *
651 * This function is called with no lock held to release the iocb to
652 * iocb pool.
653 **/
James Smart2e0fef82007-06-17 19:56:36 -0500654void
655lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
656{
657 unsigned long iflags;
658
659 /*
660 * Clean all volatile data fields, preserve iotag and node struct.
661 */
662 spin_lock_irqsave(&phba->hbalock, iflags);
663 __lpfc_sli_release_iocbq(phba, iocbq);
664 spin_unlock_irqrestore(&phba->hbalock, iflags);
665}
666
James Smarte59058c2008-08-24 21:49:00 -0400667/**
James Smarta257bf92009-04-06 18:48:10 -0400668 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
669 * @phba: Pointer to HBA context object.
670 * @iocblist: List of IOCBs.
671 * @ulpstatus: ULP status in IOCB command field.
672 * @ulpWord4: ULP word-4 in IOCB command field.
673 *
674 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
675 * on the list by invoking the complete callback function associated with the
676 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
677 * fields.
678 **/
679void
680lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
681 uint32_t ulpstatus, uint32_t ulpWord4)
682{
683 struct lpfc_iocbq *piocb;
684
685 while (!list_empty(iocblist)) {
686 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
687
688 if (!piocb->iocb_cmpl)
689 lpfc_sli_release_iocbq(phba, piocb);
690 else {
691 piocb->iocb.ulpStatus = ulpstatus;
692 piocb->iocb.un.ulpWord[4] = ulpWord4;
693 (piocb->iocb_cmpl) (phba, piocb, piocb);
694 }
695 }
696 return;
697}
698
699/**
James Smart3621a712009-04-06 18:47:14 -0400700 * lpfc_sli_iocb_cmd_type - Get the iocb type
701 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400702 *
703 * This function is called by ring event handler function to get the iocb type.
704 * This function translates the iocb command to an iocb command type used to
705 * decide the final disposition of each completed IOCB.
706 * The function returns
707 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
708 * LPFC_SOL_IOCB if it is a solicited iocb completion
709 * LPFC_ABORT_IOCB if it is an abort iocb
710 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
711 *
712 * The caller is not required to hold any lock.
713 **/
dea31012005-04-17 16:05:31 -0500714static lpfc_iocb_type
715lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
716{
717 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
718
719 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
720 return 0;
721
722 switch (iocb_cmnd) {
723 case CMD_XMIT_SEQUENCE_CR:
724 case CMD_XMIT_SEQUENCE_CX:
725 case CMD_XMIT_BCAST_CN:
726 case CMD_XMIT_BCAST_CX:
727 case CMD_ELS_REQUEST_CR:
728 case CMD_ELS_REQUEST_CX:
729 case CMD_CREATE_XRI_CR:
730 case CMD_CREATE_XRI_CX:
731 case CMD_GET_RPI_CN:
732 case CMD_XMIT_ELS_RSP_CX:
733 case CMD_GET_RPI_CR:
734 case CMD_FCP_IWRITE_CR:
735 case CMD_FCP_IWRITE_CX:
736 case CMD_FCP_IREAD_CR:
737 case CMD_FCP_IREAD_CX:
738 case CMD_FCP_ICMND_CR:
739 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500740 case CMD_FCP_TSEND_CX:
741 case CMD_FCP_TRSP_CX:
742 case CMD_FCP_TRECEIVE_CX:
743 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500744 case CMD_ADAPTER_MSG:
745 case CMD_ADAPTER_DUMP:
746 case CMD_XMIT_SEQUENCE64_CR:
747 case CMD_XMIT_SEQUENCE64_CX:
748 case CMD_XMIT_BCAST64_CN:
749 case CMD_XMIT_BCAST64_CX:
750 case CMD_ELS_REQUEST64_CR:
751 case CMD_ELS_REQUEST64_CX:
752 case CMD_FCP_IWRITE64_CR:
753 case CMD_FCP_IWRITE64_CX:
754 case CMD_FCP_IREAD64_CR:
755 case CMD_FCP_IREAD64_CX:
756 case CMD_FCP_ICMND64_CR:
757 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500758 case CMD_FCP_TSEND64_CX:
759 case CMD_FCP_TRSP64_CX:
760 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500761 case CMD_GEN_REQUEST64_CR:
762 case CMD_GEN_REQUEST64_CX:
763 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400764 case DSSCMD_IWRITE64_CR:
765 case DSSCMD_IWRITE64_CX:
766 case DSSCMD_IREAD64_CR:
767 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500768 type = LPFC_SOL_IOCB;
769 break;
770 case CMD_ABORT_XRI_CN:
771 case CMD_ABORT_XRI_CX:
772 case CMD_CLOSE_XRI_CN:
773 case CMD_CLOSE_XRI_CX:
774 case CMD_XRI_ABORTED_CX:
775 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400776 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500777 type = LPFC_ABORT_IOCB;
778 break;
779 case CMD_RCV_SEQUENCE_CX:
780 case CMD_RCV_ELS_REQ_CX:
781 case CMD_RCV_SEQUENCE64_CX:
782 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400783 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500784 case CMD_IOCB_RCV_SEQ64_CX:
785 case CMD_IOCB_RCV_ELS64_CX:
786 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500787 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500788 type = LPFC_UNSOL_IOCB;
789 break;
James Smart3163f722008-02-08 18:50:25 -0500790 case CMD_IOCB_XMIT_MSEQ64_CR:
791 case CMD_IOCB_XMIT_MSEQ64_CX:
792 case CMD_IOCB_RCV_SEQ_LIST64_CX:
793 case CMD_IOCB_RCV_ELS_LIST64_CX:
794 case CMD_IOCB_CLOSE_EXTENDED_CN:
795 case CMD_IOCB_ABORT_EXTENDED_CN:
796 case CMD_IOCB_RET_HBQE64_CN:
797 case CMD_IOCB_FCP_IBIDIR64_CR:
798 case CMD_IOCB_FCP_IBIDIR64_CX:
799 case CMD_IOCB_FCP_ITASKMGT64_CX:
800 case CMD_IOCB_LOGENTRY_CN:
801 case CMD_IOCB_LOGENTRY_ASYNC_CN:
802 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700803 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500804 type = LPFC_UNKNOWN_IOCB;
805 break;
dea31012005-04-17 16:05:31 -0500806 default:
807 type = LPFC_UNKNOWN_IOCB;
808 break;
809 }
810
811 return type;
812}
813
James Smarte59058c2008-08-24 21:49:00 -0400814/**
James Smart3621a712009-04-06 18:47:14 -0400815 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400816 * @phba: Pointer to HBA context object.
817 *
818 * This function is called from SLI initialization code
819 * to configure every ring of the HBA's SLI interface. The
820 * caller is not required to hold any lock. This function issues
821 * a config_ring mailbox command for each ring.
822 * This function returns zero if successful else returns a negative
823 * error code.
824 **/
dea31012005-04-17 16:05:31 -0500825static int
James Smarted957682007-06-17 19:56:37 -0500826lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500827{
828 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500829 LPFC_MBOXQ_t *pmb;
830 MAILBOX_t *pmbox;
831 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500832
James Smarted957682007-06-17 19:56:37 -0500833 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
834 if (!pmb)
835 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400836 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500837 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500838 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500839 lpfc_config_ring(phba, i, pmb);
840 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
841 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400843 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500844 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
845 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400846 rc, pmbox->mbxCommand,
847 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500848 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500849 ret = -ENXIO;
850 break;
dea31012005-04-17 16:05:31 -0500851 }
852 }
James Smarted957682007-06-17 19:56:37 -0500853 mempool_free(pmb, phba->mbox_mem_pool);
854 return ret;
dea31012005-04-17 16:05:31 -0500855}
856
James Smarte59058c2008-08-24 21:49:00 -0400857/**
James Smart3621a712009-04-06 18:47:14 -0400858 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400859 * @phba: Pointer to HBA context object.
860 * @pring: Pointer to driver SLI ring object.
861 * @piocb: Pointer to the driver iocb object.
862 *
863 * This function is called with hbalock held. The function adds the
864 * new iocb to txcmplq of the given ring. This function always returns
865 * 0. If this function is called for ELS ring, this function checks if
866 * there is a vport associated with the ELS command. This function also
867 * starts els_tmofunc timer if this is an ELS command.
868 **/
dea31012005-04-17 16:05:31 -0500869static int
James Smart2e0fef82007-06-17 19:56:36 -0500870lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
871 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500872{
dea31012005-04-17 16:05:31 -0500873 list_add_tail(&piocb->list, &pring->txcmplq);
874 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500875 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
876 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
877 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
878 if (!piocb->vport)
879 BUG();
880 else
881 mod_timer(&piocb->vport->els_tmofunc,
882 jiffies + HZ * (phba->fc_ratov << 1));
883 }
884
dea31012005-04-17 16:05:31 -0500885
James Smart2e0fef82007-06-17 19:56:36 -0500886 return 0;
dea31012005-04-17 16:05:31 -0500887}
888
James Smarte59058c2008-08-24 21:49:00 -0400889/**
James Smart3621a712009-04-06 18:47:14 -0400890 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400891 * @phba: Pointer to HBA context object.
892 * @pring: Pointer to driver SLI ring object.
893 *
894 * This function is called with hbalock held to get next
895 * iocb in txq of the given ring. If there is any iocb in
896 * the txq, the function returns first iocb in the list after
897 * removing the iocb from the list, else it returns NULL.
898 **/
dea31012005-04-17 16:05:31 -0500899static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500900lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500901{
dea31012005-04-17 16:05:31 -0500902 struct lpfc_iocbq *cmd_iocb;
903
James Smart858c9f62007-06-17 19:56:39 -0500904 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
905 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500906 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500907 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500908}
909
James Smarte59058c2008-08-24 21:49:00 -0400910/**
James Smart3621a712009-04-06 18:47:14 -0400911 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400912 * @phba: Pointer to HBA context object.
913 * @pring: Pointer to driver SLI ring object.
914 *
915 * This function is called with hbalock held and the caller must post the
916 * iocb without releasing the lock. If the caller releases the lock,
917 * iocb slot returned by the function is not guaranteed to be available.
918 * The function returns pointer to the next available iocb slot if there
919 * is available slot in the ring, else it returns NULL.
920 * If the get index of the ring is ahead of the put index, the function
921 * will post an error attention event to the worker thread to take the
922 * HBA to offline state.
923 **/
dea31012005-04-17 16:05:31 -0500924static IOCB_t *
925lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
926{
James Smart34b02dc2008-08-24 21:49:55 -0400927 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500928 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500929 if ((pring->next_cmdidx == pring->cmdidx) &&
930 (++pring->next_cmdidx >= max_cmd_idx))
931 pring->next_cmdidx = 0;
932
933 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
934
935 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
936
937 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
938 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400939 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200940 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400941 pring->ringno,
dea31012005-04-17 16:05:31 -0500942 pring->local_getidx, max_cmd_idx);
943
James Smart2e0fef82007-06-17 19:56:36 -0500944 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500945 /*
946 * All error attention handlers are posted to
947 * worker thread
948 */
949 phba->work_ha |= HA_ERATT;
950 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500951
James Smart5e9d9b82008-06-14 22:52:53 -0400952 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500953
954 return NULL;
955 }
956
957 if (pring->local_getidx == pring->next_cmdidx)
958 return NULL;
959 }
960
James Smarted957682007-06-17 19:56:37 -0500961 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500962}
963
James Smarte59058c2008-08-24 21:49:00 -0400964/**
James Smart3621a712009-04-06 18:47:14 -0400965 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400966 * @phba: Pointer to HBA context object.
967 * @iocbq: Pointer to driver iocb object.
968 *
969 * This function gets an iotag for the iocb. If there is no unused iotag and
970 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
971 * array and assigns a new iotag.
972 * The function returns the allocated iotag if successful, else returns zero.
973 * Zero is not a valid iotag.
974 * The caller is not required to hold any lock.
975 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500976uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500977lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500978{
James Smart2e0fef82007-06-17 19:56:36 -0500979 struct lpfc_iocbq **new_arr;
980 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500981 size_t new_len;
982 struct lpfc_sli *psli = &phba->sli;
983 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500984
James Smart2e0fef82007-06-17 19:56:36 -0500985 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500986 iotag = psli->last_iotag;
987 if(++iotag < psli->iocbq_lookup_len) {
988 psli->last_iotag = iotag;
989 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500990 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500991 iocbq->iotag = iotag;
992 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500993 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500994 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
995 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500996 spin_unlock_irq(&phba->hbalock);
997 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500998 GFP_KERNEL);
999 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001000 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001001 old_arr = psli->iocbq_lookup;
1002 if (new_len <= psli->iocbq_lookup_len) {
1003 /* highly unprobable case */
1004 kfree(new_arr);
1005 iotag = psli->last_iotag;
1006 if(++iotag < psli->iocbq_lookup_len) {
1007 psli->last_iotag = iotag;
1008 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001010 iocbq->iotag = iotag;
1011 return iotag;
1012 }
James Smart2e0fef82007-06-17 19:56:36 -05001013 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001014 return 0;
1015 }
1016 if (psli->iocbq_lookup)
1017 memcpy(new_arr, old_arr,
1018 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001019 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001020 psli->iocbq_lookup = new_arr;
1021 psli->iocbq_lookup_len = new_len;
1022 psli->last_iotag = iotag;
1023 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001024 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001025 iocbq->iotag = iotag;
1026 kfree(old_arr);
1027 return iotag;
1028 }
James Smart8f6d98d2006-08-01 07:34:00 -04001029 } else
James Smart2e0fef82007-06-17 19:56:36 -05001030 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001031
James Bottomley604a3e32005-10-29 10:28:33 -05001032 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001033 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1034 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001035
James Bottomley604a3e32005-10-29 10:28:33 -05001036 return 0;
dea31012005-04-17 16:05:31 -05001037}
1038
James Smarte59058c2008-08-24 21:49:00 -04001039/**
James Smart3621a712009-04-06 18:47:14 -04001040 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001041 * @phba: Pointer to HBA context object.
1042 * @pring: Pointer to driver SLI ring object.
1043 * @iocb: Pointer to iocb slot in the ring.
1044 * @nextiocb: Pointer to driver iocb object which need to be
1045 * posted to firmware.
1046 *
1047 * This function is called with hbalock held to post a new iocb to
1048 * the firmware. This function copies the new iocb to ring iocb slot and
1049 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1050 * a completion call back for this iocb else the function will free the
1051 * iocb object.
1052 **/
dea31012005-04-17 16:05:31 -05001053static void
1054lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1055 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1056{
1057 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001058 * Set up an iotag
dea31012005-04-17 16:05:31 -05001059 */
James Bottomley604a3e32005-10-29 10:28:33 -05001060 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001061
James Smarte2a0a9d2008-12-04 22:40:02 -05001062
James Smarta58cbd52007-08-02 11:09:43 -04001063 if (pring->ringno == LPFC_ELS_RING) {
1064 lpfc_debugfs_slow_ring_trc(phba,
1065 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1066 *(((uint32_t *) &nextiocb->iocb) + 4),
1067 *(((uint32_t *) &nextiocb->iocb) + 6),
1068 *(((uint32_t *) &nextiocb->iocb) + 7));
1069 }
1070
dea31012005-04-17 16:05:31 -05001071 /*
1072 * Issue iocb command to adapter
1073 */
James Smart92d7f7b2007-06-17 19:56:38 -05001074 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001075 wmb();
1076 pring->stats.iocb_cmd++;
1077
1078 /*
1079 * If there is no completion routine to call, we can release the
1080 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1081 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1082 */
1083 if (nextiocb->iocb_cmpl)
1084 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001085 else
James Smart2e0fef82007-06-17 19:56:36 -05001086 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001087
1088 /*
1089 * Let the HBA know what IOCB slot will be the next one the
1090 * driver will put a command into.
1091 */
1092 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001093 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001094}
1095
James Smarte59058c2008-08-24 21:49:00 -04001096/**
James Smart3621a712009-04-06 18:47:14 -04001097 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001098 * @phba: Pointer to HBA context object.
1099 * @pring: Pointer to driver SLI ring object.
1100 *
1101 * The caller is not required to hold any lock for calling this function.
1102 * This function updates the chip attention bits for the ring to inform firmware
1103 * that there are pending work to be done for this ring and requests an
1104 * interrupt when there is space available in the ring. This function is
1105 * called when the driver is unable to post more iocbs to the ring due
1106 * to unavailability of space in the ring.
1107 **/
dea31012005-04-17 16:05:31 -05001108static void
James Smart2e0fef82007-06-17 19:56:36 -05001109lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001110{
1111 int ringno = pring->ringno;
1112
1113 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1114
1115 wmb();
1116
1117 /*
1118 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1119 * The HBA will tell us when an IOCB entry is available.
1120 */
1121 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1122 readl(phba->CAregaddr); /* flush */
1123
1124 pring->stats.iocb_cmd_full++;
1125}
1126
James Smarte59058c2008-08-24 21:49:00 -04001127/**
James Smart3621a712009-04-06 18:47:14 -04001128 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001129 * @phba: Pointer to HBA context object.
1130 * @pring: Pointer to driver SLI ring object.
1131 *
1132 * This function updates the chip attention register bit for the
1133 * given ring to inform HBA that there is more work to be done
1134 * in this ring. The caller is not required to hold any lock.
1135 **/
dea31012005-04-17 16:05:31 -05001136static void
James Smart2e0fef82007-06-17 19:56:36 -05001137lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001138{
1139 int ringno = pring->ringno;
1140
1141 /*
1142 * Tell the HBA that there is work to do in this ring.
1143 */
James Smart34b02dc2008-08-24 21:49:55 -04001144 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1145 wmb();
1146 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1147 readl(phba->CAregaddr); /* flush */
1148 }
dea31012005-04-17 16:05:31 -05001149}
1150
James Smarte59058c2008-08-24 21:49:00 -04001151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001153 * @phba: Pointer to HBA context object.
1154 * @pring: Pointer to driver SLI ring object.
1155 *
1156 * This function is called with hbalock held to post pending iocbs
1157 * in the txq to the firmware. This function is called when driver
1158 * detects space available in the ring.
1159 **/
dea31012005-04-17 16:05:31 -05001160static void
James Smart2e0fef82007-06-17 19:56:36 -05001161lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001162{
1163 IOCB_t *iocb;
1164 struct lpfc_iocbq *nextiocb;
1165
1166 /*
1167 * Check to see if:
1168 * (a) there is anything on the txq to send
1169 * (b) link is up
1170 * (c) link attention events can be processed (fcp ring only)
1171 * (d) IOCB processing is not blocked by the outstanding mbox command.
1172 */
1173 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001174 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001175 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001176 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001177
1178 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1179 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1180 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1181
1182 if (iocb)
1183 lpfc_sli_update_ring(phba, pring);
1184 else
1185 lpfc_sli_update_full_ring(phba, pring);
1186 }
1187
1188 return;
1189}
1190
James Smarte59058c2008-08-24 21:49:00 -04001191/**
James Smart3621a712009-04-06 18:47:14 -04001192 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001193 * @phba: Pointer to HBA context object.
1194 * @hbqno: HBQ number.
1195 *
1196 * This function is called with hbalock held to get the next
1197 * available slot for the given HBQ. If there is free slot
1198 * available for the HBQ it will return pointer to the next available
1199 * HBQ entry else it will return NULL.
1200 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001201static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001202lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1203{
1204 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1205
1206 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1207 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1208 hbqp->next_hbqPutIdx = 0;
1209
1210 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001211 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001212 uint32_t getidx = le32_to_cpu(raw_index);
1213
1214 hbqp->local_hbqGetIdx = getidx;
1215
1216 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1217 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001218 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001219 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001220 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001221 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001222 hbqp->entry_count);
1223
1224 phba->link_state = LPFC_HBA_ERROR;
1225 return NULL;
1226 }
1227
1228 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1229 return NULL;
1230 }
1231
James Smart51ef4c22007-08-02 11:10:31 -04001232 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1233 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001234}
1235
James Smarte59058c2008-08-24 21:49:00 -04001236/**
James Smart3621a712009-04-06 18:47:14 -04001237 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001238 * @phba: Pointer to HBA context object.
1239 *
1240 * This function is called with no lock held to free all the
1241 * hbq buffers while uninitializing the SLI interface. It also
1242 * frees the HBQ buffers returned by the firmware but not yet
1243 * processed by the upper layers.
1244 **/
James Smarted957682007-06-17 19:56:37 -05001245void
1246lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1247{
James Smart92d7f7b2007-06-17 19:56:38 -05001248 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1249 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001250 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001251 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001252 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001253
James Smart51ef4c22007-08-02 11:10:31 -04001254 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001255 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001256 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001257 for (i = 0; i < hbq_count; ++i) {
1258 list_for_each_entry_safe(dmabuf, next_dmabuf,
1259 &phba->hbqs[i].hbq_buffer_list, list) {
1260 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1261 list_del(&hbq_buf->dbuf.list);
1262 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1263 }
James Smarta8adb832007-10-27 13:37:53 -04001264 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001265 }
James Smart3163f722008-02-08 18:50:25 -05001266 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001267 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1268 list) {
James Smart3163f722008-02-08 18:50:25 -05001269 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1270 list_del(&hbq_buf->dbuf.list);
1271 if (hbq_buf->tag == -1) {
1272 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1273 (phba, hbq_buf);
1274 } else {
1275 hbqno = hbq_buf->tag >> 16;
1276 if (hbqno >= LPFC_MAX_HBQS)
1277 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1278 (phba, hbq_buf);
1279 else
1280 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1281 hbq_buf);
1282 }
1283 }
1284
1285 /* Mark the HBQs not in use */
1286 phba->hbq_in_use = 0;
1287 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001288}
1289
James Smarte59058c2008-08-24 21:49:00 -04001290/**
James Smart3621a712009-04-06 18:47:14 -04001291 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001292 * @phba: Pointer to HBA context object.
1293 * @hbqno: HBQ number.
1294 * @hbq_buf: Pointer to HBQ buffer.
1295 *
1296 * This function is called with the hbalock held to post a
1297 * hbq buffer to the firmware. If the function finds an empty
1298 * slot in the HBQ, it will post the buffer. The function will return
1299 * pointer to the hbq entry if it successfully post the buffer
1300 * else it will return NULL.
1301 **/
James Smart3772a992009-05-22 14:50:54 -04001302static int
James Smarted957682007-06-17 19:56:37 -05001303lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001304 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001305{
James Smart3772a992009-05-22 14:50:54 -04001306 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1307}
1308
1309/**
1310 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1311 * @phba: Pointer to HBA context object.
1312 * @hbqno: HBQ number.
1313 * @hbq_buf: Pointer to HBQ buffer.
1314 *
1315 * This function is called with the hbalock held to post a hbq buffer to the
1316 * firmware. If the function finds an empty slot in the HBQ, it will post the
1317 * buffer and place it on the hbq_buffer_list. The function will return zero if
1318 * it successfully post the buffer else it will return an error.
1319 **/
1320static int
1321lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1322 struct hbq_dmabuf *hbq_buf)
1323{
James Smarted957682007-06-17 19:56:37 -05001324 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001325 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001326
1327 /* Get next HBQ entry slot to use */
1328 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1329 if (hbqe) {
1330 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1331
James Smart92d7f7b2007-06-17 19:56:38 -05001332 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1333 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001334 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001335 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001336 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1337 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1338 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001339 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1340 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001341 /* flush */
James Smarted957682007-06-17 19:56:37 -05001342 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001343 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001344 return 0;
1345 } else
1346 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001347}
1348
James Smart4f774512009-05-22 14:52:35 -04001349/**
1350 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1351 * @phba: Pointer to HBA context object.
1352 * @hbqno: HBQ number.
1353 * @hbq_buf: Pointer to HBQ buffer.
1354 *
1355 * This function is called with the hbalock held to post an RQE to the SLI4
1356 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1357 * the hbq_buffer_list and return zero, otherwise it will return an error.
1358 **/
1359static int
1360lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1361 struct hbq_dmabuf *hbq_buf)
1362{
1363 int rc;
1364 struct lpfc_rqe hrqe;
1365 struct lpfc_rqe drqe;
1366
1367 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1368 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1369 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1370 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1371 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1372 &hrqe, &drqe);
1373 if (rc < 0)
1374 return rc;
1375 hbq_buf->tag = rc;
1376 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1377 return 0;
1378}
1379
James Smarte59058c2008-08-24 21:49:00 -04001380/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001381static struct lpfc_hbq_init lpfc_els_hbq = {
1382 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001383 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001384 .mask_count = 0,
1385 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001386 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001387 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001388 .init_count = 40,
1389 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001390};
James Smarted957682007-06-17 19:56:37 -05001391
James Smarte59058c2008-08-24 21:49:00 -04001392/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001393static struct lpfc_hbq_init lpfc_extra_hbq = {
1394 .rn = 1,
1395 .entry_count = 200,
1396 .mask_count = 0,
1397 .profile = 0,
1398 .ring_mask = (1 << LPFC_EXTRA_RING),
1399 .buffer_count = 0,
1400 .init_count = 0,
1401 .add_count = 5,
1402};
1403
James Smarte59058c2008-08-24 21:49:00 -04001404/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001405struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001406 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001407 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001408};
1409
James Smarte59058c2008-08-24 21:49:00 -04001410/**
James Smart3621a712009-04-06 18:47:14 -04001411 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001412 * @phba: Pointer to HBA context object.
1413 * @hbqno: HBQ number.
1414 * @count: Number of HBQ buffers to be posted.
1415 *
James Smartd7c255b2008-08-24 21:50:00 -04001416 * This function is called with no lock held to post more hbq buffers to the
1417 * given HBQ. The function returns the number of HBQ buffers successfully
1418 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001419 **/
James Smart311464e2007-08-02 11:10:37 -04001420static int
James Smart92d7f7b2007-06-17 19:56:38 -05001421lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1422{
James Smartd7c255b2008-08-24 21:50:00 -04001423 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001424 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001425 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001426 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001427 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001428 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001429
James Smartd7c255b2008-08-24 21:50:00 -04001430 if ((phba->hbqs[hbqno].buffer_count + count) >
1431 lpfc_hbq_defs[hbqno]->entry_count)
1432 count = lpfc_hbq_defs[hbqno]->entry_count -
1433 phba->hbqs[hbqno].buffer_count;
1434 if (!count)
1435 return 0;
1436 /* Allocate HBQ entries */
1437 for (i = 0; i < count; i++) {
1438 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1439 if (!hbq_buffer)
1440 break;
1441 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1442 }
James Smart3163f722008-02-08 18:50:25 -05001443 /* Check whether HBQ is still in use */
1444 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001445 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001446 goto err;
1447 while (!list_empty(&hbq_buf_list)) {
1448 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1449 dbuf.list);
1450 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1451 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001452 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001453 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001454 posted++;
1455 } else
James Smart51ef4c22007-08-02 11:10:31 -04001456 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001457 }
James Smart3163f722008-02-08 18:50:25 -05001458 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001459 return posted;
1460err:
1461 spin_unlock_irqrestore(&phba->hbalock, flags);
1462 while (!list_empty(&hbq_buf_list)) {
1463 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1464 dbuf.list);
1465 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1466 }
James Smart92d7f7b2007-06-17 19:56:38 -05001467 return 0;
James Smarted957682007-06-17 19:56:37 -05001468}
1469
James Smarte59058c2008-08-24 21:49:00 -04001470/**
James Smart3621a712009-04-06 18:47:14 -04001471 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001472 * @phba: Pointer to HBA context object.
1473 * @qno: HBQ number.
1474 *
1475 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001476 * is called with no lock held. The function returns the number of HBQ entries
1477 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001478 **/
James Smarted957682007-06-17 19:56:37 -05001479int
James Smart92d7f7b2007-06-17 19:56:38 -05001480lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001481{
James Smartdef9c7a2009-12-21 17:02:28 -05001482 if (phba->sli_rev == LPFC_SLI_REV4)
1483 return 0;
1484 else
1485 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1486 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001487}
1488
James Smarte59058c2008-08-24 21:49:00 -04001489/**
James Smart3621a712009-04-06 18:47:14 -04001490 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001491 * @phba: Pointer to HBA context object.
1492 * @qno: HBQ queue number.
1493 *
1494 * This function is called from SLI initialization code path with
1495 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001496 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001497 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001498static int
James Smart92d7f7b2007-06-17 19:56:38 -05001499lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001500{
James Smartdef9c7a2009-12-21 17:02:28 -05001501 if (phba->sli_rev == LPFC_SLI_REV4)
1502 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503 lpfc_hbq_defs[qno]->entry_count);
1504 else
1505 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1506 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001507}
1508
James Smarte59058c2008-08-24 21:49:00 -04001509/**
James Smart3772a992009-05-22 14:50:54 -04001510 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1511 * @phba: Pointer to HBA context object.
1512 * @hbqno: HBQ number.
1513 *
1514 * This function removes the first hbq buffer on an hbq list and returns a
1515 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1516 **/
1517static struct hbq_dmabuf *
1518lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1519{
1520 struct lpfc_dmabuf *d_buf;
1521
1522 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1523 if (!d_buf)
1524 return NULL;
1525 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1526}
1527
1528/**
James Smart3621a712009-04-06 18:47:14 -04001529 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001530 * @phba: Pointer to HBA context object.
1531 * @tag: Tag of the hbq buffer.
1532 *
1533 * This function is called with hbalock held. This function searches
1534 * for the hbq buffer associated with the given tag in the hbq buffer
1535 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1536 * it returns NULL.
1537 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001538static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001539lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1540{
James Smart92d7f7b2007-06-17 19:56:38 -05001541 struct lpfc_dmabuf *d_buf;
1542 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001543 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001544
James Smart51ef4c22007-08-02 11:10:31 -04001545 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001546 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001547 return NULL;
1548
James Smart3772a992009-05-22 14:50:54 -04001549 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001550 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001551 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001552 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001553 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001554 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001555 }
1556 }
James Smart3772a992009-05-22 14:50:54 -04001557 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001559 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001560 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001561 return NULL;
James Smarted957682007-06-17 19:56:37 -05001562}
1563
James Smarte59058c2008-08-24 21:49:00 -04001564/**
James Smart3621a712009-04-06 18:47:14 -04001565 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001566 * @phba: Pointer to HBA context object.
1567 * @hbq_buffer: Pointer to HBQ buffer.
1568 *
1569 * This function is called with hbalock. This function gives back
1570 * the hbq buffer to firmware. If the HBQ does not have space to
1571 * post the buffer, it will free the buffer.
1572 **/
James Smarted957682007-06-17 19:56:37 -05001573void
James Smart51ef4c22007-08-02 11:10:31 -04001574lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001575{
1576 uint32_t hbqno;
1577
James Smart51ef4c22007-08-02 11:10:31 -04001578 if (hbq_buffer) {
1579 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001580 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001581 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001582 }
1583}
1584
James Smarte59058c2008-08-24 21:49:00 -04001585/**
James Smart3621a712009-04-06 18:47:14 -04001586 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001587 * @mbxCommand: mailbox command code.
1588 *
1589 * This function is called by the mailbox event handler function to verify
1590 * that the completed mailbox command is a legitimate mailbox command. If the
1591 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1592 * and the mailbox event handler will take the HBA offline.
1593 **/
dea31012005-04-17 16:05:31 -05001594static int
1595lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1596{
1597 uint8_t ret;
1598
1599 switch (mbxCommand) {
1600 case MBX_LOAD_SM:
1601 case MBX_READ_NV:
1602 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001603 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001604 case MBX_RUN_BIU_DIAG:
1605 case MBX_INIT_LINK:
1606 case MBX_DOWN_LINK:
1607 case MBX_CONFIG_LINK:
1608 case MBX_CONFIG_RING:
1609 case MBX_RESET_RING:
1610 case MBX_READ_CONFIG:
1611 case MBX_READ_RCONFIG:
1612 case MBX_READ_SPARM:
1613 case MBX_READ_STATUS:
1614 case MBX_READ_RPI:
1615 case MBX_READ_XRI:
1616 case MBX_READ_REV:
1617 case MBX_READ_LNK_STAT:
1618 case MBX_REG_LOGIN:
1619 case MBX_UNREG_LOGIN:
1620 case MBX_READ_LA:
1621 case MBX_CLEAR_LA:
1622 case MBX_DUMP_MEMORY:
1623 case MBX_DUMP_CONTEXT:
1624 case MBX_RUN_DIAGS:
1625 case MBX_RESTART:
1626 case MBX_UPDATE_CFG:
1627 case MBX_DOWN_LOAD:
1628 case MBX_DEL_LD_ENTRY:
1629 case MBX_RUN_PROGRAM:
1630 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001631 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001632 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001633 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001634 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001635 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001636 case MBX_LOAD_AREA:
1637 case MBX_RUN_BIU_DIAG64:
1638 case MBX_CONFIG_PORT:
1639 case MBX_READ_SPARM64:
1640 case MBX_READ_RPI64:
1641 case MBX_REG_LOGIN64:
1642 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001643 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001644 case MBX_SET_DEBUG:
1645 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001646 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001647 case MBX_REG_VPI:
1648 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001649 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001650 case MBX_PORT_CAPABILITIES:
1651 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001652 case MBX_SLI4_CONFIG:
1653 case MBX_SLI4_REQ_FTRS:
1654 case MBX_REG_FCFI:
1655 case MBX_UNREG_FCFI:
1656 case MBX_REG_VFI:
1657 case MBX_UNREG_VFI:
1658 case MBX_INIT_VPI:
1659 case MBX_INIT_VFI:
1660 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001661 ret = mbxCommand;
1662 break;
1663 default:
1664 ret = MBX_SHUTDOWN;
1665 break;
1666 }
James Smart2e0fef82007-06-17 19:56:36 -05001667 return ret;
dea31012005-04-17 16:05:31 -05001668}
James Smarte59058c2008-08-24 21:49:00 -04001669
1670/**
James Smart3621a712009-04-06 18:47:14 -04001671 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001672 * @phba: Pointer to HBA context object.
1673 * @pmboxq: Pointer to mailbox command.
1674 *
1675 * This is completion handler function for mailbox commands issued from
1676 * lpfc_sli_issue_mbox_wait function. This function is called by the
1677 * mailbox event handler function with no lock held. This function
1678 * will wake up thread waiting on the wait queue pointed by context1
1679 * of the mailbox.
1680 **/
James Smart04c68492009-05-22 14:52:52 -04001681void
James Smart2e0fef82007-06-17 19:56:36 -05001682lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001683{
1684 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001685 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001686
1687 /*
1688 * If pdone_q is empty, the driver thread gave up waiting and
1689 * continued running.
1690 */
James Smart7054a602007-04-25 09:52:34 -04001691 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001692 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001693 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1694 if (pdone_q)
1695 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001696 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001697 return;
1698}
1699
James Smarte59058c2008-08-24 21:49:00 -04001700
1701/**
James Smart3621a712009-04-06 18:47:14 -04001702 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001703 * @phba: Pointer to HBA context object.
1704 * @pmb: Pointer to mailbox object.
1705 *
1706 * This function is the default mailbox completion handler. It
1707 * frees the memory resources associated with the completed mailbox
1708 * command. If the completed command is a REG_LOGIN mailbox command,
1709 * this function will issue a UREG_LOGIN to re-claim the RPI.
1710 **/
dea31012005-04-17 16:05:31 -05001711void
James Smart2e0fef82007-06-17 19:56:36 -05001712lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001713{
1714 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001715 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001716 int rc;
James Smart695a8142010-01-26 23:08:03 -05001717 struct lpfc_vport *vport = pmb->vport;
James Smart7054a602007-04-25 09:52:34 -04001718
dea31012005-04-17 16:05:31 -05001719 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001720
dea31012005-04-17 16:05:31 -05001721 if (mp) {
1722 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1723 kfree(mp);
1724 }
James Smart7054a602007-04-25 09:52:34 -04001725
James Smart04c68492009-05-22 14:52:52 -04001726 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1727 (phba->sli_rev == LPFC_SLI_REV4))
1728 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1729
James Smart7054a602007-04-25 09:52:34 -04001730 /*
1731 * If a REG_LOGIN succeeded after node is destroyed or node
1732 * is in re-discovery driver need to cleanup the RPI.
1733 */
James Smart2e0fef82007-06-17 19:56:36 -05001734 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001735 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1736 !pmb->u.mb.mbxStatus) {
1737 rpi = pmb->u.mb.un.varWords[0];
1738 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1739 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001740 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001741 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1742 if (rc != MBX_NOT_FINISHED)
1743 return;
1744 }
1745
James Smart695a8142010-01-26 23:08:03 -05001746 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1747 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1748 !(phba->pport->load_flag & FC_UNLOADING) &&
1749 !pmb->u.mb.mbxStatus) {
1750 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1751 pmb->vport = vport;
1752 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1753 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1754 if (rc != MBX_NOT_FINISHED)
1755 return;
1756 }
1757
James Smart04c68492009-05-22 14:52:52 -04001758 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1759 lpfc_sli4_mbox_cmd_free(phba, pmb);
1760 else
1761 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001762}
1763
James Smarte59058c2008-08-24 21:49:00 -04001764/**
James Smart3621a712009-04-06 18:47:14 -04001765 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001766 * @phba: Pointer to HBA context object.
1767 *
1768 * This function is called with no lock held. This function processes all
1769 * the completed mailbox commands and gives it to upper layers. The interrupt
1770 * service routine processes mailbox completion interrupt and adds completed
1771 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1772 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1773 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1774 * function returns the mailbox commands to the upper layer by calling the
1775 * completion handler function of each mailbox.
1776 **/
dea31012005-04-17 16:05:31 -05001777int
James Smart2e0fef82007-06-17 19:56:36 -05001778lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001779{
James Smart92d7f7b2007-06-17 19:56:38 -05001780 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001781 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001782 int rc;
1783 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001784
1785 phba->sli.slistat.mbox_event++;
1786
James Smart92d7f7b2007-06-17 19:56:38 -05001787 /* Get all completed mailboxe buffers into the cmplq */
1788 spin_lock_irq(&phba->hbalock);
1789 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1790 spin_unlock_irq(&phba->hbalock);
1791
dea31012005-04-17 16:05:31 -05001792 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001793 do {
1794 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1795 if (pmb == NULL)
1796 break;
1797
James Smart04c68492009-05-22 14:52:52 -04001798 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001799
James Smart858c9f62007-06-17 19:56:39 -05001800 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1801 if (pmb->vport) {
1802 lpfc_debugfs_disc_trc(pmb->vport,
1803 LPFC_DISC_TRC_MBOX_VPORT,
1804 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1805 (uint32_t)pmbox->mbxCommand,
1806 pmbox->un.varWords[0],
1807 pmbox->un.varWords[1]);
1808 }
1809 else {
1810 lpfc_debugfs_disc_trc(phba->pport,
1811 LPFC_DISC_TRC_MBOX,
1812 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1813 (uint32_t)pmbox->mbxCommand,
1814 pmbox->un.varWords[0],
1815 pmbox->un.varWords[1]);
1816 }
1817 }
1818
dea31012005-04-17 16:05:31 -05001819 /*
1820 * It is a fatal error if unknown mbox command completion.
1821 */
1822 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1823 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001824 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001825 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001826 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001827 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001828 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001829 pmbox->mbxCommand,
1830 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001831 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001832 phba->work_hs = HS_FFER3;
1833 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001834 continue;
dea31012005-04-17 16:05:31 -05001835 }
1836
dea31012005-04-17 16:05:31 -05001837 if (pmbox->mbxStatus) {
1838 phba->sli.slistat.mbox_stat_err++;
1839 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1840 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001841 lpfc_printf_log(phba, KERN_INFO,
1842 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001843 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001844 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001845 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001846 pmb->vport ? pmb->vport->vpi :0,
1847 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001848 lpfc_sli4_mbox_opcode_get(phba,
1849 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001850 pmbox->mbxStatus,
1851 pmbox->un.varWords[0],
1852 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001853 pmbox->mbxStatus = 0;
1854 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001855 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001856 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001857 continue;
dea31012005-04-17 16:05:31 -05001858 }
1859 }
1860
1861 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001862 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001863 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001864 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001865 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001866 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001867 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001868 pmb->mbox_cmpl,
1869 *((uint32_t *) pmbox),
1870 pmbox->un.varWords[0],
1871 pmbox->un.varWords[1],
1872 pmbox->un.varWords[2],
1873 pmbox->un.varWords[3],
1874 pmbox->un.varWords[4],
1875 pmbox->un.varWords[5],
1876 pmbox->un.varWords[6],
1877 pmbox->un.varWords[7]);
1878
James Smart92d7f7b2007-06-17 19:56:38 -05001879 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001880 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001881 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001882 return 0;
dea31012005-04-17 16:05:31 -05001883}
James Smart92d7f7b2007-06-17 19:56:38 -05001884
James Smarte59058c2008-08-24 21:49:00 -04001885/**
James Smart3621a712009-04-06 18:47:14 -04001886 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001887 * @phba: Pointer to HBA context object.
1888 * @pring: Pointer to driver SLI ring object.
1889 * @tag: buffer tag.
1890 *
1891 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1892 * is set in the tag the buffer is posted for a particular exchange,
1893 * the function will return the buffer without replacing the buffer.
1894 * If the buffer is for unsolicited ELS or CT traffic, this function
1895 * returns the buffer and also posts another buffer to the firmware.
1896 **/
James Smart76bb24e2007-10-27 13:38:00 -04001897static struct lpfc_dmabuf *
1898lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001899 struct lpfc_sli_ring *pring,
1900 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001901{
James Smart9f1e1b52008-12-04 22:39:40 -05001902 struct hbq_dmabuf *hbq_entry;
1903
James Smart76bb24e2007-10-27 13:38:00 -04001904 if (tag & QUE_BUFTAG_BIT)
1905 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001906 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1907 if (!hbq_entry)
1908 return NULL;
1909 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001910}
James Smart57127f12007-10-27 13:37:05 -04001911
James Smart3772a992009-05-22 14:50:54 -04001912/**
1913 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1914 * @phba: Pointer to HBA context object.
1915 * @pring: Pointer to driver SLI ring object.
1916 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1917 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1918 * @fch_type: the type for the first frame of the sequence.
1919 *
1920 * This function is called with no lock held. This function uses the r_ctl and
1921 * type of the received sequence to find the correct callback function to call
1922 * to process the sequence.
1923 **/
1924static int
1925lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1926 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1927 uint32_t fch_type)
1928{
1929 int i;
1930
1931 /* unSolicited Responses */
1932 if (pring->prt[0].profile) {
1933 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1934 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1935 saveq);
1936 return 1;
1937 }
1938 /* We must search, based on rctl / type
1939 for the right routine */
1940 for (i = 0; i < pring->num_mask; i++) {
1941 if ((pring->prt[i].rctl == fch_r_ctl) &&
1942 (pring->prt[i].type == fch_type)) {
1943 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1944 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1945 (phba, pring, saveq);
1946 return 1;
1947 }
1948 }
1949 return 0;
1950}
James Smarte59058c2008-08-24 21:49:00 -04001951
1952/**
James Smart3621a712009-04-06 18:47:14 -04001953 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001954 * @phba: Pointer to HBA context object.
1955 * @pring: Pointer to driver SLI ring object.
1956 * @saveq: Pointer to the unsolicited iocb.
1957 *
1958 * This function is called with no lock held by the ring event handler
1959 * when there is an unsolicited iocb posted to the response ring by the
1960 * firmware. This function gets the buffer associated with the iocbs
1961 * and calls the event handler for the ring. This function handles both
1962 * qring buffers and hbq buffers.
1963 * When the function returns 1 the caller can free the iocb object otherwise
1964 * upper layer functions will free the iocb objects.
1965 **/
dea31012005-04-17 16:05:31 -05001966static int
1967lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1968 struct lpfc_iocbq *saveq)
1969{
1970 IOCB_t * irsp;
1971 WORD5 * w5p;
1972 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001973 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001974 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001975 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001976
1977 match = 0;
1978 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001979
1980 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1981 if (pring->lpfc_sli_rcv_async_status)
1982 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1983 else
1984 lpfc_printf_log(phba,
1985 KERN_WARNING,
1986 LOG_SLI,
1987 "0316 Ring %d handler: unexpected "
1988 "ASYNC_STATUS iocb received evt_code "
1989 "0x%x\n",
1990 pring->ringno,
1991 irsp->un.asyncstat.evt_code);
1992 return 1;
1993 }
1994
James Smart3163f722008-02-08 18:50:25 -05001995 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1996 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1997 if (irsp->ulpBdeCount > 0) {
1998 dmzbuf = lpfc_sli_get_buff(phba, pring,
1999 irsp->un.ulpWord[3]);
2000 lpfc_in_buf_free(phba, dmzbuf);
2001 }
2002
2003 if (irsp->ulpBdeCount > 1) {
2004 dmzbuf = lpfc_sli_get_buff(phba, pring,
2005 irsp->unsli3.sli3Words[3]);
2006 lpfc_in_buf_free(phba, dmzbuf);
2007 }
2008
2009 if (irsp->ulpBdeCount > 2) {
2010 dmzbuf = lpfc_sli_get_buff(phba, pring,
2011 irsp->unsli3.sli3Words[7]);
2012 lpfc_in_buf_free(phba, dmzbuf);
2013 }
2014
2015 return 1;
2016 }
2017
James Smart92d7f7b2007-06-17 19:56:38 -05002018 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002019 if (irsp->ulpBdeCount != 0) {
2020 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002021 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002022 if (!saveq->context2)
2023 lpfc_printf_log(phba,
2024 KERN_ERR,
2025 LOG_SLI,
2026 "0341 Ring %d Cannot find buffer for "
2027 "an unsolicited iocb. tag 0x%x\n",
2028 pring->ringno,
2029 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002030 }
2031 if (irsp->ulpBdeCount == 2) {
2032 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002033 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002034 if (!saveq->context3)
2035 lpfc_printf_log(phba,
2036 KERN_ERR,
2037 LOG_SLI,
2038 "0342 Ring %d Cannot find buffer for an"
2039 " unsolicited iocb. tag 0x%x\n",
2040 pring->ringno,
2041 irsp->unsli3.sli3Words[7]);
2042 }
2043 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002044 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002045 if (irsp->ulpBdeCount != 0) {
2046 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2047 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002048 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_SLI,
2052 "0343 Ring %d Cannot find "
2053 "buffer for an unsolicited iocb"
2054 ". tag 0x%x\n", pring->ringno,
2055 irsp->un.ulpWord[3]);
2056 }
2057 if (irsp->ulpBdeCount == 2) {
2058 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2059 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002060 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002061 lpfc_printf_log(phba,
2062 KERN_ERR,
2063 LOG_SLI,
2064 "0344 Ring %d Cannot find "
2065 "buffer for an unsolicited "
2066 "iocb. tag 0x%x\n",
2067 pring->ringno,
2068 irsp->unsli3.sli3Words[7]);
2069 }
2070 }
James Smart92d7f7b2007-06-17 19:56:38 -05002071 }
James Smart9c2face2008-01-11 01:53:18 -05002072 if (irsp->ulpBdeCount != 0 &&
2073 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2074 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2075 int found = 0;
2076
2077 /* search continue save q for same XRI */
2078 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2079 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2080 list_add_tail(&saveq->list, &iocbq->list);
2081 found = 1;
2082 break;
2083 }
2084 }
2085 if (!found)
2086 list_add_tail(&saveq->clist,
2087 &pring->iocb_continue_saveq);
2088 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2089 list_del_init(&iocbq->clist);
2090 saveq = iocbq;
2091 irsp = &(saveq->iocb);
2092 } else
2093 return 0;
2094 }
2095 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2096 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2097 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002098 Rctl = FC_RCTL_ELS_REQ;
2099 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002100 } else {
2101 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2102 Rctl = w5p->hcsw.Rctl;
2103 Type = w5p->hcsw.Type;
2104
2105 /* Firmware Workaround */
2106 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2107 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2108 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002109 Rctl = FC_RCTL_ELS_REQ;
2110 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002111 w5p->hcsw.Rctl = Rctl;
2112 w5p->hcsw.Type = Type;
2113 }
2114 }
James Smart92d7f7b2007-06-17 19:56:38 -05002115
James Smart3772a992009-05-22 14:50:54 -04002116 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002117 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002118 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002119 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002120 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002121
James Smart92d7f7b2007-06-17 19:56:38 -05002122 return 1;
dea31012005-04-17 16:05:31 -05002123}
2124
James Smarte59058c2008-08-24 21:49:00 -04002125/**
James Smart3621a712009-04-06 18:47:14 -04002126 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002127 * @phba: Pointer to HBA context object.
2128 * @pring: Pointer to driver SLI ring object.
2129 * @prspiocb: Pointer to response iocb object.
2130 *
2131 * This function looks up the iocb_lookup table to get the command iocb
2132 * corresponding to the given response iocb using the iotag of the
2133 * response iocb. This function is called with the hbalock held.
2134 * This function returns the command iocb object if it finds the command
2135 * iocb else returns NULL.
2136 **/
dea31012005-04-17 16:05:31 -05002137static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002138lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2139 struct lpfc_sli_ring *pring,
2140 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002141{
dea31012005-04-17 16:05:31 -05002142 struct lpfc_iocbq *cmd_iocb = NULL;
2143 uint16_t iotag;
2144
James Bottomley604a3e32005-10-29 10:28:33 -05002145 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002146
James Bottomley604a3e32005-10-29 10:28:33 -05002147 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2148 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002149 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002150 pring->txcmplq_cnt--;
2151 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002152 }
2153
dea31012005-04-17 16:05:31 -05002154 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002155 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002156 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002157 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002158 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002159 return NULL;
2160}
2161
James Smarte59058c2008-08-24 21:49:00 -04002162/**
James Smart3772a992009-05-22 14:50:54 -04002163 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2164 * @phba: Pointer to HBA context object.
2165 * @pring: Pointer to driver SLI ring object.
2166 * @iotag: IOCB tag.
2167 *
2168 * This function looks up the iocb_lookup table to get the command iocb
2169 * corresponding to the given iotag. This function is called with the
2170 * hbalock held.
2171 * This function returns the command iocb object if it finds the command
2172 * iocb else returns NULL.
2173 **/
2174static struct lpfc_iocbq *
2175lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2176 struct lpfc_sli_ring *pring, uint16_t iotag)
2177{
2178 struct lpfc_iocbq *cmd_iocb;
2179
2180 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2181 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2182 list_del_init(&cmd_iocb->list);
2183 pring->txcmplq_cnt--;
2184 return cmd_iocb;
2185 }
2186
2187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2188 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2189 iotag, phba->sli.last_iotag);
2190 return NULL;
2191}
2192
2193/**
James Smart3621a712009-04-06 18:47:14 -04002194 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002195 * @phba: Pointer to HBA context object.
2196 * @pring: Pointer to driver SLI ring object.
2197 * @saveq: Pointer to the response iocb to be processed.
2198 *
2199 * This function is called by the ring event handler for non-fcp
2200 * rings when there is a new response iocb in the response ring.
2201 * The caller is not required to hold any locks. This function
2202 * gets the command iocb associated with the response iocb and
2203 * calls the completion handler for the command iocb. If there
2204 * is no completion handler, the function will free the resources
2205 * associated with command iocb. If the response iocb is for
2206 * an already aborted command iocb, the status of the completion
2207 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2208 * This function always returns 1.
2209 **/
dea31012005-04-17 16:05:31 -05002210static int
James Smart2e0fef82007-06-17 19:56:36 -05002211lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002212 struct lpfc_iocbq *saveq)
2213{
James Smart2e0fef82007-06-17 19:56:36 -05002214 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002215 int rc = 1;
2216 unsigned long iflag;
2217
2218 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002219 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002220 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002221 spin_unlock_irqrestore(&phba->hbalock, iflag);
2222
dea31012005-04-17 16:05:31 -05002223 if (cmdiocbp) {
2224 if (cmdiocbp->iocb_cmpl) {
2225 /*
James Smartea2151b2008-09-07 11:52:10 -04002226 * If an ELS command failed send an event to mgmt
2227 * application.
2228 */
2229 if (saveq->iocb.ulpStatus &&
2230 (pring->ringno == LPFC_ELS_RING) &&
2231 (cmdiocbp->iocb.ulpCommand ==
2232 CMD_ELS_REQUEST64_CR))
2233 lpfc_send_els_failure_event(phba,
2234 cmdiocbp, saveq);
2235
2236 /*
dea31012005-04-17 16:05:31 -05002237 * Post all ELS completions to the worker thread.
2238 * All other are passed to the completion callback.
2239 */
2240 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002241 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2242 (cmdiocbp->iocb_flag &
2243 LPFC_DRIVER_ABORTED)) {
2244 spin_lock_irqsave(&phba->hbalock,
2245 iflag);
James Smart07951072007-04-25 09:51:38 -04002246 cmdiocbp->iocb_flag &=
2247 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002248 spin_unlock_irqrestore(&phba->hbalock,
2249 iflag);
James Smart07951072007-04-25 09:51:38 -04002250 saveq->iocb.ulpStatus =
2251 IOSTAT_LOCAL_REJECT;
2252 saveq->iocb.un.ulpWord[4] =
2253 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002254
2255 /* Firmware could still be in progress
2256 * of DMAing payload, so don't free data
2257 * buffer till after a hbeat.
2258 */
James Smart341af102010-01-26 23:07:37 -05002259 spin_lock_irqsave(&phba->hbalock,
2260 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002261 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002262 spin_unlock_irqrestore(&phba->hbalock,
2263 iflag);
2264 }
James Smart0f65ff62010-02-26 14:14:23 -05002265 if (phba->sli_rev == LPFC_SLI_REV4) {
2266 if (saveq->iocb_flag &
2267 LPFC_EXCHANGE_BUSY) {
2268 /* Set cmdiocb flag for the
2269 * exchange busy so sgl (xri)
2270 * will not be released until
2271 * the abort xri is received
2272 * from hba.
2273 */
2274 spin_lock_irqsave(
2275 &phba->hbalock, iflag);
2276 cmdiocbp->iocb_flag |=
2277 LPFC_EXCHANGE_BUSY;
2278 spin_unlock_irqrestore(
2279 &phba->hbalock, iflag);
2280 }
2281 if (cmdiocbp->iocb_flag &
2282 LPFC_DRIVER_ABORTED) {
2283 /*
2284 * Clear LPFC_DRIVER_ABORTED
2285 * bit in case it was driver
2286 * initiated abort.
2287 */
2288 spin_lock_irqsave(
2289 &phba->hbalock, iflag);
2290 cmdiocbp->iocb_flag &=
2291 ~LPFC_DRIVER_ABORTED;
2292 spin_unlock_irqrestore(
2293 &phba->hbalock, iflag);
2294 cmdiocbp->iocb.ulpStatus =
2295 IOSTAT_LOCAL_REJECT;
2296 cmdiocbp->iocb.un.ulpWord[4] =
2297 IOERR_ABORT_REQUESTED;
2298 /*
2299 * For SLI4, irsiocb contains
2300 * NO_XRI in sli_xritag, it
2301 * shall not affect releasing
2302 * sgl (xri) process.
2303 */
2304 saveq->iocb.ulpStatus =
2305 IOSTAT_LOCAL_REJECT;
2306 saveq->iocb.un.ulpWord[4] =
2307 IOERR_SLI_ABORTED;
2308 spin_lock_irqsave(
2309 &phba->hbalock, iflag);
2310 saveq->iocb_flag |=
2311 LPFC_DELAY_MEM_FREE;
2312 spin_unlock_irqrestore(
2313 &phba->hbalock, iflag);
2314 }
James Smart07951072007-04-25 09:51:38 -04002315 }
dea31012005-04-17 16:05:31 -05002316 }
James Smart2e0fef82007-06-17 19:56:36 -05002317 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002318 } else
2319 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002320 } else {
2321 /*
2322 * Unknown initiating command based on the response iotag.
2323 * This could be the case on the ELS ring because of
2324 * lpfc_els_abort().
2325 */
2326 if (pring->ringno != LPFC_ELS_RING) {
2327 /*
2328 * Ring <ringno> handler: unexpected completion IoTag
2329 * <IoTag>
2330 */
James Smarta257bf92009-04-06 18:48:10 -04002331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002332 "0322 Ring %d handler: "
2333 "unexpected completion IoTag x%x "
2334 "Data: x%x x%x x%x x%x\n",
2335 pring->ringno,
2336 saveq->iocb.ulpIoTag,
2337 saveq->iocb.ulpStatus,
2338 saveq->iocb.un.ulpWord[4],
2339 saveq->iocb.ulpCommand,
2340 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002341 }
2342 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002343
dea31012005-04-17 16:05:31 -05002344 return rc;
2345}
2346
James Smarte59058c2008-08-24 21:49:00 -04002347/**
James Smart3621a712009-04-06 18:47:14 -04002348 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002349 * @phba: Pointer to HBA context object.
2350 * @pring: Pointer to driver SLI ring object.
2351 *
2352 * This function is called from the iocb ring event handlers when
2353 * put pointer is ahead of the get pointer for a ring. This function signal
2354 * an error attention condition to the worker thread and the worker
2355 * thread will transition the HBA to offline state.
2356 **/
James Smart2e0fef82007-06-17 19:56:36 -05002357static void
2358lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002359{
James Smart34b02dc2008-08-24 21:49:55 -04002360 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002361 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002362 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002363 * rsp ring <portRspMax>
2364 */
2365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002366 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002367 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002368 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002369 pring->numRiocb);
2370
James Smart2e0fef82007-06-17 19:56:36 -05002371 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002372
2373 /*
2374 * All error attention handlers are posted to
2375 * worker thread
2376 */
2377 phba->work_ha |= HA_ERATT;
2378 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002379
James Smart5e9d9b82008-06-14 22:52:53 -04002380 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002381
2382 return;
2383}
2384
James Smarte59058c2008-08-24 21:49:00 -04002385/**
James Smart3621a712009-04-06 18:47:14 -04002386 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002387 * @ptr: Pointer to address of HBA context object.
2388 *
2389 * This function is invoked by the Error Attention polling timer when the
2390 * timer times out. It will check the SLI Error Attention register for
2391 * possible attention events. If so, it will post an Error Attention event
2392 * and wake up worker thread to process it. Otherwise, it will set up the
2393 * Error Attention polling timer for the next poll.
2394 **/
2395void lpfc_poll_eratt(unsigned long ptr)
2396{
2397 struct lpfc_hba *phba;
2398 uint32_t eratt = 0;
2399
2400 phba = (struct lpfc_hba *)ptr;
2401
2402 /* Check chip HA register for error event */
2403 eratt = lpfc_sli_check_eratt(phba);
2404
2405 if (eratt)
2406 /* Tell the worker thread there is work to do */
2407 lpfc_worker_wake_up(phba);
2408 else
2409 /* Restart the timer for next eratt poll */
2410 mod_timer(&phba->eratt_poll, jiffies +
2411 HZ * LPFC_ERATT_POLL_INTERVAL);
2412 return;
2413}
2414
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002415
James Smarte59058c2008-08-24 21:49:00 -04002416/**
James Smart3621a712009-04-06 18:47:14 -04002417 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002418 * @phba: Pointer to HBA context object.
2419 * @pring: Pointer to driver SLI ring object.
2420 * @mask: Host attention register mask for this ring.
2421 *
2422 * This function is called from the interrupt context when there is a ring
2423 * event for the fcp ring. The caller does not hold any lock.
2424 * The function processes each response iocb in the response ring until it
2425 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2426 * LE bit set. The function will call the completion handler of the command iocb
2427 * if the response iocb indicates a completion for a command iocb or it is
2428 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2429 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002430 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002431 * to check it explicitly.
2432 */
2433int
James Smart2e0fef82007-06-17 19:56:36 -05002434lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2435 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002436{
James Smart34b02dc2008-08-24 21:49:55 -04002437 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002438 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002439 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002440 struct lpfc_iocbq *cmdiocbq = NULL;
2441 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002442 uint32_t status;
2443 uint32_t portRspPut, portRspMax;
2444 int rc = 1;
2445 lpfc_iocb_type type;
2446 unsigned long iflag;
2447 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002448
James Smart2e0fef82007-06-17 19:56:36 -05002449 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002450 pring->stats.iocb_event++;
2451
dea31012005-04-17 16:05:31 -05002452 /*
2453 * The next available response entry should never exceed the maximum
2454 * entries. If it does, treat it as an adapter hardware error.
2455 */
2456 portRspMax = pring->numRiocb;
2457 portRspPut = le32_to_cpu(pgp->rspPutInx);
2458 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002459 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002460 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002461 return 1;
2462 }
James Smart45ed1192009-10-02 15:17:02 -04002463 if (phba->fcp_ring_in_use) {
2464 spin_unlock_irqrestore(&phba->hbalock, iflag);
2465 return 1;
2466 } else
2467 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002468
2469 rmb();
2470 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002471 /*
2472 * Fetch an entry off the ring and copy it into a local data
2473 * structure. The copy involves a byte-swap since the
2474 * network byte order and pci byte orders are different.
2475 */
James Smarted957682007-06-17 19:56:37 -05002476 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002477 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002478
2479 if (++pring->rspidx >= portRspMax)
2480 pring->rspidx = 0;
2481
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002482 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2483 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002484 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002485 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002486 irsp = &rspiocbq.iocb;
2487
dea31012005-04-17 16:05:31 -05002488 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2489 pring->stats.iocb_rsp++;
2490 rsp_cmpl++;
2491
2492 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002493 /*
2494 * If resource errors reported from HBA, reduce
2495 * queuedepths of the SCSI device.
2496 */
2497 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2498 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2499 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002500 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002501 spin_lock_irqsave(&phba->hbalock, iflag);
2502 }
2503
dea31012005-04-17 16:05:31 -05002504 /* Rsp ring <ringno> error: IOCB */
2505 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002506 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002507 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002508 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002509 irsp->un.ulpWord[0],
2510 irsp->un.ulpWord[1],
2511 irsp->un.ulpWord[2],
2512 irsp->un.ulpWord[3],
2513 irsp->un.ulpWord[4],
2514 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002515 *(uint32_t *)&irsp->un1,
2516 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002517 }
2518
2519 switch (type) {
2520 case LPFC_ABORT_IOCB:
2521 case LPFC_SOL_IOCB:
2522 /*
2523 * Idle exchange closed via ABTS from port. No iocb
2524 * resources need to be recovered.
2525 */
2526 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002528 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002529 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002530 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002531 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002532 break;
2533 }
2534
James Bottomley604a3e32005-10-29 10:28:33 -05002535 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2536 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05002537 if (unlikely(!cmdiocbq))
2538 break;
2539 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2540 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2541 if (cmdiocbq->iocb_cmpl) {
2542 spin_unlock_irqrestore(&phba->hbalock, iflag);
2543 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2544 &rspiocbq);
2545 spin_lock_irqsave(&phba->hbalock, iflag);
2546 }
dea31012005-04-17 16:05:31 -05002547 break;
James Smarta4bc3372006-12-02 13:34:16 -05002548 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002549 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002550 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002551 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002552 break;
dea31012005-04-17 16:05:31 -05002553 default:
2554 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2555 char adaptermsg[LPFC_MAX_ADPTMSG];
2556 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2557 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2558 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002559 dev_warn(&((phba->pcidev)->dev),
2560 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002561 phba->brd_no, adaptermsg);
2562 } else {
2563 /* Unknown IOCB command */
2564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002565 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002566 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002567 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002568 irsp->ulpStatus,
2569 irsp->ulpIoTag,
2570 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002571 }
2572 break;
2573 }
2574
2575 /*
2576 * The response IOCB has been processed. Update the ring
2577 * pointer in SLIM. If the port response put pointer has not
2578 * been updated, sync the pgp->rspPutInx and fetch the new port
2579 * response put pointer.
2580 */
James Smarted957682007-06-17 19:56:37 -05002581 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002582
2583 if (pring->rspidx == portRspPut)
2584 portRspPut = le32_to_cpu(pgp->rspPutInx);
2585 }
2586
2587 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2588 pring->stats.iocb_rsp_full++;
2589 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2590 writel(status, phba->CAregaddr);
2591 readl(phba->CAregaddr);
2592 }
2593 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2594 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2595 pring->stats.iocb_cmd_empty++;
2596
2597 /* Force update of the local copy of cmdGetInx */
2598 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2599 lpfc_sli_resume_iocb(phba, pring);
2600
2601 if ((pring->lpfc_sli_cmd_available))
2602 (pring->lpfc_sli_cmd_available) (phba, pring);
2603
2604 }
2605
James Smart45ed1192009-10-02 15:17:02 -04002606 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002607 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002608 return rc;
2609}
2610
James Smarte59058c2008-08-24 21:49:00 -04002611/**
James Smart3772a992009-05-22 14:50:54 -04002612 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2613 * @phba: Pointer to HBA context object.
2614 * @pring: Pointer to driver SLI ring object.
2615 * @rspiocbp: Pointer to driver response IOCB object.
2616 *
2617 * This function is called from the worker thread when there is a slow-path
2618 * response IOCB to process. This function chains all the response iocbs until
2619 * seeing the iocb with the LE bit set. The function will call
2620 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2621 * completion of a command iocb. The function will call the
2622 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2623 * The function frees the resources or calls the completion handler if this
2624 * iocb is an abort completion. The function returns NULL when the response
2625 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2626 * this function shall chain the iocb on to the iocb_continueq and return the
2627 * response iocb passed in.
2628 **/
2629static struct lpfc_iocbq *
2630lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2631 struct lpfc_iocbq *rspiocbp)
2632{
2633 struct lpfc_iocbq *saveq;
2634 struct lpfc_iocbq *cmdiocbp;
2635 struct lpfc_iocbq *next_iocb;
2636 IOCB_t *irsp = NULL;
2637 uint32_t free_saveq;
2638 uint8_t iocb_cmd_type;
2639 lpfc_iocb_type type;
2640 unsigned long iflag;
2641 int rc;
2642
2643 spin_lock_irqsave(&phba->hbalock, iflag);
2644 /* First add the response iocb to the countinueq list */
2645 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2646 pring->iocb_continueq_cnt++;
2647
2648 /* Now, determine whetehr the list is completed for processing */
2649 irsp = &rspiocbp->iocb;
2650 if (irsp->ulpLe) {
2651 /*
2652 * By default, the driver expects to free all resources
2653 * associated with this iocb completion.
2654 */
2655 free_saveq = 1;
2656 saveq = list_get_first(&pring->iocb_continueq,
2657 struct lpfc_iocbq, list);
2658 irsp = &(saveq->iocb);
2659 list_del_init(&pring->iocb_continueq);
2660 pring->iocb_continueq_cnt = 0;
2661
2662 pring->stats.iocb_rsp++;
2663
2664 /*
2665 * If resource errors reported from HBA, reduce
2666 * queuedepths of the SCSI device.
2667 */
2668 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2669 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2670 spin_unlock_irqrestore(&phba->hbalock, iflag);
2671 phba->lpfc_rampdown_queue_depth(phba);
2672 spin_lock_irqsave(&phba->hbalock, iflag);
2673 }
2674
2675 if (irsp->ulpStatus) {
2676 /* Rsp ring <ringno> error: IOCB */
2677 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2678 "0328 Rsp Ring %d error: "
2679 "IOCB Data: "
2680 "x%x x%x x%x x%x "
2681 "x%x x%x x%x x%x "
2682 "x%x x%x x%x x%x "
2683 "x%x x%x x%x x%x\n",
2684 pring->ringno,
2685 irsp->un.ulpWord[0],
2686 irsp->un.ulpWord[1],
2687 irsp->un.ulpWord[2],
2688 irsp->un.ulpWord[3],
2689 irsp->un.ulpWord[4],
2690 irsp->un.ulpWord[5],
2691 *(((uint32_t *) irsp) + 6),
2692 *(((uint32_t *) irsp) + 7),
2693 *(((uint32_t *) irsp) + 8),
2694 *(((uint32_t *) irsp) + 9),
2695 *(((uint32_t *) irsp) + 10),
2696 *(((uint32_t *) irsp) + 11),
2697 *(((uint32_t *) irsp) + 12),
2698 *(((uint32_t *) irsp) + 13),
2699 *(((uint32_t *) irsp) + 14),
2700 *(((uint32_t *) irsp) + 15));
2701 }
2702
2703 /*
2704 * Fetch the IOCB command type and call the correct completion
2705 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2706 * get freed back to the lpfc_iocb_list by the discovery
2707 * kernel thread.
2708 */
2709 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2710 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2711 switch (type) {
2712 case LPFC_SOL_IOCB:
2713 spin_unlock_irqrestore(&phba->hbalock, iflag);
2714 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2715 spin_lock_irqsave(&phba->hbalock, iflag);
2716 break;
2717
2718 case LPFC_UNSOL_IOCB:
2719 spin_unlock_irqrestore(&phba->hbalock, iflag);
2720 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2721 spin_lock_irqsave(&phba->hbalock, iflag);
2722 if (!rc)
2723 free_saveq = 0;
2724 break;
2725
2726 case LPFC_ABORT_IOCB:
2727 cmdiocbp = NULL;
2728 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2729 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2730 saveq);
2731 if (cmdiocbp) {
2732 /* Call the specified completion routine */
2733 if (cmdiocbp->iocb_cmpl) {
2734 spin_unlock_irqrestore(&phba->hbalock,
2735 iflag);
2736 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2737 saveq);
2738 spin_lock_irqsave(&phba->hbalock,
2739 iflag);
2740 } else
2741 __lpfc_sli_release_iocbq(phba,
2742 cmdiocbp);
2743 }
2744 break;
2745
2746 case LPFC_UNKNOWN_IOCB:
2747 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2748 char adaptermsg[LPFC_MAX_ADPTMSG];
2749 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2750 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2751 MAX_MSG_DATA);
2752 dev_warn(&((phba->pcidev)->dev),
2753 "lpfc%d: %s\n",
2754 phba->brd_no, adaptermsg);
2755 } else {
2756 /* Unknown IOCB command */
2757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2758 "0335 Unknown IOCB "
2759 "command Data: x%x "
2760 "x%x x%x x%x\n",
2761 irsp->ulpCommand,
2762 irsp->ulpStatus,
2763 irsp->ulpIoTag,
2764 irsp->ulpContext);
2765 }
2766 break;
2767 }
2768
2769 if (free_saveq) {
2770 list_for_each_entry_safe(rspiocbp, next_iocb,
2771 &saveq->list, list) {
2772 list_del(&rspiocbp->list);
2773 __lpfc_sli_release_iocbq(phba, rspiocbp);
2774 }
2775 __lpfc_sli_release_iocbq(phba, saveq);
2776 }
2777 rspiocbp = NULL;
2778 }
2779 spin_unlock_irqrestore(&phba->hbalock, iflag);
2780 return rspiocbp;
2781}
2782
2783/**
2784 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002785 * @phba: Pointer to HBA context object.
2786 * @pring: Pointer to driver SLI ring object.
2787 * @mask: Host attention register mask for this ring.
2788 *
James Smart3772a992009-05-22 14:50:54 -04002789 * This routine wraps the actual slow_ring event process routine from the
2790 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002791 **/
James Smart3772a992009-05-22 14:50:54 -04002792void
James Smart2e0fef82007-06-17 19:56:36 -05002793lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2794 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002795{
James Smart3772a992009-05-22 14:50:54 -04002796 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2797}
2798
2799/**
2800 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2801 * @phba: Pointer to HBA context object.
2802 * @pring: Pointer to driver SLI ring object.
2803 * @mask: Host attention register mask for this ring.
2804 *
2805 * This function is called from the worker thread when there is a ring event
2806 * for non-fcp rings. The caller does not hold any lock. The function will
2807 * remove each response iocb in the response ring and calls the handle
2808 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2809 **/
2810static void
2811lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2812 struct lpfc_sli_ring *pring, uint32_t mask)
2813{
James Smart34b02dc2008-08-24 21:49:55 -04002814 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002815 IOCB_t *entry;
2816 IOCB_t *irsp = NULL;
2817 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002818 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002819 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002820 uint32_t status;
dea31012005-04-17 16:05:31 -05002821
James Smart34b02dc2008-08-24 21:49:55 -04002822 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002823 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002824 pring->stats.iocb_event++;
2825
dea31012005-04-17 16:05:31 -05002826 /*
2827 * The next available response entry should never exceed the maximum
2828 * entries. If it does, treat it as an adapter hardware error.
2829 */
2830 portRspMax = pring->numRiocb;
2831 portRspPut = le32_to_cpu(pgp->rspPutInx);
2832 if (portRspPut >= portRspMax) {
2833 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002834 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002835 * rsp ring <portRspMax>
2836 */
James Smarted957682007-06-17 19:56:37 -05002837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002838 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002839 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002840 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002841
James Smart2e0fef82007-06-17 19:56:36 -05002842 phba->link_state = LPFC_HBA_ERROR;
2843 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002844
2845 phba->work_hs = HS_FFER3;
2846 lpfc_handle_eratt(phba);
2847
James Smart3772a992009-05-22 14:50:54 -04002848 return;
dea31012005-04-17 16:05:31 -05002849 }
2850
2851 rmb();
dea31012005-04-17 16:05:31 -05002852 while (pring->rspidx != portRspPut) {
2853 /*
2854 * Build a completion list and call the appropriate handler.
2855 * The process is to get the next available response iocb, get
2856 * a free iocb from the list, copy the response data into the
2857 * free iocb, insert to the continuation list, and update the
2858 * next response index to slim. This process makes response
2859 * iocb's in the ring available to DMA as fast as possible but
2860 * pays a penalty for a copy operation. Since the iocb is
2861 * only 32 bytes, this penalty is considered small relative to
2862 * the PCI reads for register values and a slim write. When
2863 * the ulpLe field is set, the entire Command has been
2864 * received.
2865 */
James Smarted957682007-06-17 19:56:37 -05002866 entry = lpfc_resp_iocb(phba, pring);
2867
James Smart858c9f62007-06-17 19:56:39 -05002868 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002869 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002870 if (rspiocbp == NULL) {
2871 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002872 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002873 break;
2874 }
2875
James Smarted957682007-06-17 19:56:37 -05002876 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2877 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002878 irsp = &rspiocbp->iocb;
2879
2880 if (++pring->rspidx >= portRspMax)
2881 pring->rspidx = 0;
2882
James Smarta58cbd52007-08-02 11:09:43 -04002883 if (pring->ringno == LPFC_ELS_RING) {
2884 lpfc_debugfs_slow_ring_trc(phba,
2885 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2886 *(((uint32_t *) irsp) + 4),
2887 *(((uint32_t *) irsp) + 6),
2888 *(((uint32_t *) irsp) + 7));
2889 }
2890
James Smarted957682007-06-17 19:56:37 -05002891 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002892
James Smart3772a992009-05-22 14:50:54 -04002893 spin_unlock_irqrestore(&phba->hbalock, iflag);
2894 /* Handle the response IOCB */
2895 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2896 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002897
2898 /*
2899 * If the port response put pointer has not been updated, sync
2900 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2901 * response put pointer.
2902 */
2903 if (pring->rspidx == portRspPut) {
2904 portRspPut = le32_to_cpu(pgp->rspPutInx);
2905 }
2906 } /* while (pring->rspidx != portRspPut) */
2907
James Smart92d7f7b2007-06-17 19:56:38 -05002908 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002909 /* At least one response entry has been freed */
2910 pring->stats.iocb_rsp_full++;
2911 /* SET RxRE_RSP in Chip Att register */
2912 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2913 writel(status, phba->CAregaddr);
2914 readl(phba->CAregaddr); /* flush */
2915 }
2916 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2917 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2918 pring->stats.iocb_cmd_empty++;
2919
2920 /* Force update of the local copy of cmdGetInx */
2921 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2922 lpfc_sli_resume_iocb(phba, pring);
2923
2924 if ((pring->lpfc_sli_cmd_available))
2925 (pring->lpfc_sli_cmd_available) (phba, pring);
2926
2927 }
2928
James Smart2e0fef82007-06-17 19:56:36 -05002929 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002930 return;
dea31012005-04-17 16:05:31 -05002931}
2932
James Smarte59058c2008-08-24 21:49:00 -04002933/**
James Smart4f774512009-05-22 14:52:35 -04002934 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2935 * @phba: Pointer to HBA context object.
2936 * @pring: Pointer to driver SLI ring object.
2937 * @mask: Host attention register mask for this ring.
2938 *
2939 * This function is called from the worker thread when there is a pending
2940 * ELS response iocb on the driver internal slow-path response iocb worker
2941 * queue. The caller does not hold any lock. The function will remove each
2942 * response iocb from the response worker queue and calls the handle
2943 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2944 **/
2945static void
2946lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2947 struct lpfc_sli_ring *pring, uint32_t mask)
2948{
2949 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002950 struct hbq_dmabuf *dmabuf;
2951 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002952 unsigned long iflag;
2953
James Smart45ed1192009-10-02 15:17:02 -04002954 spin_lock_irqsave(&phba->hbalock, iflag);
2955 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2956 spin_unlock_irqrestore(&phba->hbalock, iflag);
2957 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002958 /* Get the response iocb from the head of work queue */
2959 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002960 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002961 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002962 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002963
2964 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2965 case CQE_CODE_COMPL_WQE:
2966 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2967 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04002968 /* Translate ELS WCQE to response IOCBQ */
2969 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2970 irspiocbq);
2971 if (irspiocbq)
2972 lpfc_sli_sp_handle_rspiocb(phba, pring,
2973 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04002974 break;
2975 case CQE_CODE_RECEIVE:
2976 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2977 cq_event);
2978 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2979 break;
2980 default:
2981 break;
2982 }
James Smart4f774512009-05-22 14:52:35 -04002983 }
2984}
2985
2986/**
James Smart3621a712009-04-06 18:47:14 -04002987 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002988 * @phba: Pointer to HBA context object.
2989 * @pring: Pointer to driver SLI ring object.
2990 *
2991 * This function aborts all iocbs in the given ring and frees all the iocb
2992 * objects in txq. This function issues an abort iocb for all the iocb commands
2993 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2994 * the return of this function. The caller is not required to hold any locks.
2995 **/
James Smart2e0fef82007-06-17 19:56:36 -05002996void
dea31012005-04-17 16:05:31 -05002997lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2998{
James Smart2534ba72007-04-25 09:52:20 -04002999 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003000 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003001
James Smart92d7f7b2007-06-17 19:56:38 -05003002 if (pring->ringno == LPFC_ELS_RING) {
3003 lpfc_fabric_abort_hba(phba);
3004 }
3005
dea31012005-04-17 16:05:31 -05003006 /* Error everything on txq and txcmplq
3007 * First do the txq.
3008 */
James Smart2e0fef82007-06-17 19:56:36 -05003009 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003010 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003011 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003012
3013 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003014 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3015 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3016
James Smart2e0fef82007-06-17 19:56:36 -05003017 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003018
James Smarta257bf92009-04-06 18:48:10 -04003019 /* Cancel all the IOCBs from the completions list */
3020 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3021 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003022}
3023
James Smarte59058c2008-08-24 21:49:00 -04003024/**
James Smart3621a712009-04-06 18:47:14 -04003025 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003026 * @phba: Pointer to HBA context object.
3027 *
3028 * This function flushes all iocbs in the fcp ring and frees all the iocb
3029 * objects in txq and txcmplq. This function will not issue abort iocbs
3030 * for all the iocb commands in txcmplq, they will just be returned with
3031 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3032 * slot has been permanently disabled.
3033 **/
3034void
3035lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3036{
3037 LIST_HEAD(txq);
3038 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003039 struct lpfc_sli *psli = &phba->sli;
3040 struct lpfc_sli_ring *pring;
3041
3042 /* Currently, only one fcp ring */
3043 pring = &psli->ring[psli->fcp_ring];
3044
3045 spin_lock_irq(&phba->hbalock);
3046 /* Retrieve everything on txq */
3047 list_splice_init(&pring->txq, &txq);
3048 pring->txq_cnt = 0;
3049
3050 /* Retrieve everything on the txcmplq */
3051 list_splice_init(&pring->txcmplq, &txcmplq);
3052 pring->txcmplq_cnt = 0;
3053 spin_unlock_irq(&phba->hbalock);
3054
3055 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003056 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3057 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003058
3059 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003060 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3061 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003062}
3063
3064/**
James Smart3772a992009-05-22 14:50:54 -04003065 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003066 * @phba: Pointer to HBA context object.
3067 * @mask: Bit mask to be checked.
3068 *
3069 * This function reads the host status register and compares
3070 * with the provided bit mask to check if HBA completed
3071 * the restart. This function will wait in a loop for the
3072 * HBA to complete restart. If the HBA does not restart within
3073 * 15 iterations, the function will reset the HBA again. The
3074 * function returns 1 when HBA fail to restart otherwise returns
3075 * zero.
3076 **/
James Smart3772a992009-05-22 14:50:54 -04003077static int
3078lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003079{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003080 uint32_t status;
3081 int i = 0;
3082 int retval = 0;
dea31012005-04-17 16:05:31 -05003083
Jamie Wellnitz41415862006-02-28 19:25:27 -05003084 /* Read the HBA Host Status Register */
3085 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003086
Jamie Wellnitz41415862006-02-28 19:25:27 -05003087 /*
3088 * Check status register every 100ms for 5 retries, then every
3089 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3090 * every 2.5 sec for 4.
3091 * Break our of the loop if errors occurred during init.
3092 */
3093 while (((status & mask) != mask) &&
3094 !(status & HS_FFERM) &&
3095 i++ < 20) {
dea31012005-04-17 16:05:31 -05003096
Jamie Wellnitz41415862006-02-28 19:25:27 -05003097 if (i <= 5)
3098 msleep(10);
3099 else if (i <= 10)
3100 msleep(500);
3101 else
3102 msleep(2500);
dea31012005-04-17 16:05:31 -05003103
Jamie Wellnitz41415862006-02-28 19:25:27 -05003104 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003105 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003106 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003107 lpfc_sli_brdrestart(phba);
3108 }
3109 /* Read the HBA Host Status Register */
3110 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003111 }
dea31012005-04-17 16:05:31 -05003112
Jamie Wellnitz41415862006-02-28 19:25:27 -05003113 /* Check to see if any errors occurred during init */
3114 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3116 "2751 Adapter failed to restart, "
3117 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3118 status,
3119 readl(phba->MBslimaddr + 0xa8),
3120 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003121 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003122 retval = 1;
3123 }
dea31012005-04-17 16:05:31 -05003124
Jamie Wellnitz41415862006-02-28 19:25:27 -05003125 return retval;
dea31012005-04-17 16:05:31 -05003126}
3127
James Smartda0436e2009-05-22 14:51:39 -04003128/**
3129 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3130 * @phba: Pointer to HBA context object.
3131 * @mask: Bit mask to be checked.
3132 *
3133 * This function checks the host status register to check if HBA is
3134 * ready. This function will wait in a loop for the HBA to be ready
3135 * If the HBA is not ready , the function will will reset the HBA PCI
3136 * function again. The function returns 1 when HBA fail to be ready
3137 * otherwise returns zero.
3138 **/
3139static int
3140lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3141{
3142 uint32_t status;
3143 int retval = 0;
3144
3145 /* Read the HBA Host Status Register */
3146 status = lpfc_sli4_post_status_check(phba);
3147
3148 if (status) {
3149 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3150 lpfc_sli_brdrestart(phba);
3151 status = lpfc_sli4_post_status_check(phba);
3152 }
3153
3154 /* Check to see if any errors occurred during init */
3155 if (status) {
3156 phba->link_state = LPFC_HBA_ERROR;
3157 retval = 1;
3158 } else
3159 phba->sli4_hba.intr_enable = 0;
3160
3161 return retval;
3162}
3163
3164/**
3165 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3166 * @phba: Pointer to HBA context object.
3167 * @mask: Bit mask to be checked.
3168 *
3169 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3170 * from the API jump table function pointer from the lpfc_hba struct.
3171 **/
3172int
3173lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3174{
3175 return phba->lpfc_sli_brdready(phba, mask);
3176}
3177
James Smart92908312006-03-07 15:04:13 -05003178#define BARRIER_TEST_PATTERN (0xdeadbeef)
3179
James Smarte59058c2008-08-24 21:49:00 -04003180/**
James Smart3621a712009-04-06 18:47:14 -04003181 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003182 * @phba: Pointer to HBA context object.
3183 *
3184 * This function is called before resetting an HBA. This
3185 * function requests HBA to quiesce DMAs before a reset.
3186 **/
James Smart2e0fef82007-06-17 19:56:36 -05003187void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003188{
James Smart65a29c12006-07-06 15:50:50 -04003189 uint32_t __iomem *resp_buf;
3190 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003191 volatile uint32_t mbox;
3192 uint32_t hc_copy;
3193 int i;
3194 uint8_t hdrtype;
3195
3196 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3197 if (hdrtype != 0x80 ||
3198 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3199 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3200 return;
3201
3202 /*
3203 * Tell the other part of the chip to suspend temporarily all
3204 * its DMA activity.
3205 */
James Smart65a29c12006-07-06 15:50:50 -04003206 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003207
3208 /* Disable the error attention */
3209 hc_copy = readl(phba->HCregaddr);
3210 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3211 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003212 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003213
3214 if (readl(phba->HAregaddr) & HA_ERATT) {
3215 /* Clear Chip error bit */
3216 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003217 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003218 }
3219
3220 mbox = 0;
3221 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3222 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3223
3224 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003225 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003226 writel(mbox, mbox_buf);
3227
3228 for (i = 0;
3229 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3230 mdelay(1);
3231
3232 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003233 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003234 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003235 goto restore_hc;
3236 else
3237 goto clear_errat;
3238 }
3239
3240 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3241 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3242 mdelay(1);
3243
3244clear_errat:
3245
3246 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3247 mdelay(1);
3248
3249 if (readl(phba->HAregaddr) & HA_ERATT) {
3250 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003251 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003252 }
3253
3254restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003255 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003256 writel(hc_copy, phba->HCregaddr);
3257 readl(phba->HCregaddr); /* flush */
3258}
3259
James Smarte59058c2008-08-24 21:49:00 -04003260/**
James Smart3621a712009-04-06 18:47:14 -04003261 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003262 * @phba: Pointer to HBA context object.
3263 *
3264 * This function issues a kill_board mailbox command and waits for
3265 * the error attention interrupt. This function is called for stopping
3266 * the firmware processing. The caller is not required to hold any
3267 * locks. This function calls lpfc_hba_down_post function to free
3268 * any pending commands after the kill. The function will return 1 when it
3269 * fails to kill the board else will return 0.
3270 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003271int
James Smart2e0fef82007-06-17 19:56:36 -05003272lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003273{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003274 struct lpfc_sli *psli;
3275 LPFC_MBOXQ_t *pmb;
3276 uint32_t status;
3277 uint32_t ha_copy;
3278 int retval;
3279 int i = 0;
3280
3281 psli = &phba->sli;
3282
3283 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003285 "0329 Kill HBA Data: x%x x%x\n",
3286 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003287
James Smart98c9ea52007-10-27 13:37:33 -04003288 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3289 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003290 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003291
3292 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003293 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003294 status = readl(phba->HCregaddr);
3295 status &= ~HC_ERINT_ENA;
3296 writel(status, phba->HCregaddr);
3297 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003298 phba->link_flag |= LS_IGNORE_ERATT;
3299 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003300
3301 lpfc_kill_board(phba, pmb);
3302 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3303 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3304
3305 if (retval != MBX_SUCCESS) {
3306 if (retval != MBX_BUSY)
3307 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3309 "2752 KILL_BOARD command failed retval %d\n",
3310 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003311 spin_lock_irq(&phba->hbalock);
3312 phba->link_flag &= ~LS_IGNORE_ERATT;
3313 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003314 return 1;
3315 }
3316
James Smartf4b4c682009-05-22 14:53:12 -04003317 spin_lock_irq(&phba->hbalock);
3318 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3319 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003320
Jamie Wellnitz41415862006-02-28 19:25:27 -05003321 mempool_free(pmb, phba->mbox_mem_pool);
3322
3323 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3324 * attention every 100ms for 3 seconds. If we don't get ERATT after
3325 * 3 seconds we still set HBA_ERROR state because the status of the
3326 * board is now undefined.
3327 */
3328 ha_copy = readl(phba->HAregaddr);
3329
3330 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3331 mdelay(100);
3332 ha_copy = readl(phba->HAregaddr);
3333 }
3334
3335 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003336 if (ha_copy & HA_ERATT) {
3337 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003338 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003339 }
James Smart2e0fef82007-06-17 19:56:36 -05003340 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003341 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003342 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003343 phba->link_flag &= ~LS_IGNORE_ERATT;
3344 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003345
Jamie Wellnitz41415862006-02-28 19:25:27 -05003346 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003347 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003348
James Smart2e0fef82007-06-17 19:56:36 -05003349 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003350}
3351
James Smarte59058c2008-08-24 21:49:00 -04003352/**
James Smart3772a992009-05-22 14:50:54 -04003353 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003354 * @phba: Pointer to HBA context object.
3355 *
3356 * This function resets the HBA by writing HC_INITFF to the control
3357 * register. After the HBA resets, this function resets all the iocb ring
3358 * indices. This function disables PCI layer parity checking during
3359 * the reset.
3360 * This function returns 0 always.
3361 * The caller is not required to hold any locks.
3362 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003363int
James Smart2e0fef82007-06-17 19:56:36 -05003364lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003365{
3366 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003367 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003368 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003369 int i;
dea31012005-04-17 16:05:31 -05003370
Jamie Wellnitz41415862006-02-28 19:25:27 -05003371 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003372
Jamie Wellnitz41415862006-02-28 19:25:27 -05003373 /* Reset HBA */
3374 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003375 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003376 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003377
3378 /* perform board reset */
3379 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003380 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003381 phba->pport->fc_myDID = 0;
3382 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003383
Jamie Wellnitz41415862006-02-28 19:25:27 -05003384 /* Turn off parity checking and serr during the physical reset */
3385 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3386 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3387 (cfg_value &
3388 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3389
James Smart3772a992009-05-22 14:50:54 -04003390 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3391
Jamie Wellnitz41415862006-02-28 19:25:27 -05003392 /* Now toggle INITFF bit in the Host Control Register */
3393 writel(HC_INITFF, phba->HCregaddr);
3394 mdelay(1);
3395 readl(phba->HCregaddr); /* flush */
3396 writel(0, phba->HCregaddr);
3397 readl(phba->HCregaddr); /* flush */
3398
3399 /* Restore PCI cmd register */
3400 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003401
3402 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003403 for (i = 0; i < psli->num_rings; i++) {
3404 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003405 pring->flag = 0;
3406 pring->rspidx = 0;
3407 pring->next_cmdidx = 0;
3408 pring->local_getidx = 0;
3409 pring->cmdidx = 0;
3410 pring->missbufcnt = 0;
3411 }
dea31012005-04-17 16:05:31 -05003412
James Smart2e0fef82007-06-17 19:56:36 -05003413 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003414 return 0;
3415}
3416
James Smarte59058c2008-08-24 21:49:00 -04003417/**
James Smartda0436e2009-05-22 14:51:39 -04003418 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3419 * @phba: Pointer to HBA context object.
3420 *
3421 * This function resets a SLI4 HBA. This function disables PCI layer parity
3422 * checking during resets the device. The caller is not required to hold
3423 * any locks.
3424 *
3425 * This function returns 0 always.
3426 **/
3427int
3428lpfc_sli4_brdreset(struct lpfc_hba *phba)
3429{
3430 struct lpfc_sli *psli = &phba->sli;
3431 uint16_t cfg_value;
3432 uint8_t qindx;
3433
3434 /* Reset HBA */
3435 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3436 "0295 Reset HBA Data: x%x x%x\n",
3437 phba->pport->port_state, psli->sli_flag);
3438
3439 /* perform board reset */
3440 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003441 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003442 phba->pport->fc_myDID = 0;
3443 phba->pport->fc_prevDID = 0;
3444
3445 /* Turn off parity checking and serr during the physical reset */
3446 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3447 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3448 (cfg_value &
3449 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3450
3451 spin_lock_irq(&phba->hbalock);
3452 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3453 phba->fcf.fcf_flag = 0;
3454 /* Clean up the child queue list for the CQs */
3455 list_del_init(&phba->sli4_hba.mbx_wq->list);
3456 list_del_init(&phba->sli4_hba.els_wq->list);
3457 list_del_init(&phba->sli4_hba.hdr_rq->list);
3458 list_del_init(&phba->sli4_hba.dat_rq->list);
3459 list_del_init(&phba->sli4_hba.mbx_cq->list);
3460 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003461 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3462 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3463 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3464 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3465 spin_unlock_irq(&phba->hbalock);
3466
3467 /* Now physically reset the device */
3468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3469 "0389 Performing PCI function reset!\n");
3470 /* Perform FCoE PCI function reset */
3471 lpfc_pci_function_reset(phba);
3472
3473 return 0;
3474}
3475
3476/**
3477 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003478 * @phba: Pointer to HBA context object.
3479 *
3480 * This function is called in the SLI initialization code path to
3481 * restart the HBA. The caller is not required to hold any lock.
3482 * This function writes MBX_RESTART mailbox command to the SLIM and
3483 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3484 * function to free any pending commands. The function enables
3485 * POST only during the first initialization. The function returns zero.
3486 * The function does not guarantee completion of MBX_RESTART mailbox
3487 * command before the return of this function.
3488 **/
James Smartda0436e2009-05-22 14:51:39 -04003489static int
3490lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003491{
3492 MAILBOX_t *mb;
3493 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003494 volatile uint32_t word0;
3495 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003496 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003497
James Smart2e0fef82007-06-17 19:56:36 -05003498 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003499
James Smart0d878412009-10-02 15:16:56 -04003500 /* Take PCIe device Advanced Error Reporting (AER) state */
3501 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3502
Jamie Wellnitz41415862006-02-28 19:25:27 -05003503 psli = &phba->sli;
3504
3505 /* Restart HBA */
3506 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003507 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003508 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003509
3510 word0 = 0;
3511 mb = (MAILBOX_t *) &word0;
3512 mb->mbxCommand = MBX_RESTART;
3513 mb->mbxHc = 1;
3514
James Smart92908312006-03-07 15:04:13 -05003515 lpfc_reset_barrier(phba);
3516
Jamie Wellnitz41415862006-02-28 19:25:27 -05003517 to_slim = phba->MBslimaddr;
3518 writel(*(uint32_t *) mb, to_slim);
3519 readl(to_slim); /* flush */
3520
3521 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003522 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003523 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003524 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003525 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003526 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003527 writel(*(uint32_t *) mb, to_slim);
3528 readl(to_slim); /* flush */
3529
3530 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003531 phba->pport->stopped = 0;
3532 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003533 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003534 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003535
James Smart64ba8812006-08-02 15:24:34 -04003536 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3537 psli->stats_start = get_seconds();
3538
James Smarteaf15d52008-12-04 22:39:29 -05003539 /* Give the INITFF and Post time to settle. */
3540 mdelay(100);
dea31012005-04-17 16:05:31 -05003541
James Smart0d878412009-10-02 15:16:56 -04003542 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3543 if (hba_aer_enabled)
3544 pci_disable_pcie_error_reporting(phba->pcidev);
3545
Jamie Wellnitz41415862006-02-28 19:25:27 -05003546 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003547
3548 return 0;
3549}
3550
James Smarte59058c2008-08-24 21:49:00 -04003551/**
James Smartda0436e2009-05-22 14:51:39 -04003552 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3553 * @phba: Pointer to HBA context object.
3554 *
3555 * This function is called in the SLI initialization code path to restart
3556 * a SLI4 HBA. The caller is not required to hold any lock.
3557 * At the end of the function, it calls lpfc_hba_down_post function to
3558 * free any pending commands.
3559 **/
3560static int
3561lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3562{
3563 struct lpfc_sli *psli = &phba->sli;
3564
3565
3566 /* Restart HBA */
3567 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3568 "0296 Restart HBA Data: x%x x%x\n",
3569 phba->pport->port_state, psli->sli_flag);
3570
3571 lpfc_sli4_brdreset(phba);
3572
3573 spin_lock_irq(&phba->hbalock);
3574 phba->pport->stopped = 0;
3575 phba->link_state = LPFC_INIT_START;
3576 phba->hba_flag = 0;
3577 spin_unlock_irq(&phba->hbalock);
3578
3579 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3580 psli->stats_start = get_seconds();
3581
3582 lpfc_hba_down_post(phba);
3583
3584 return 0;
3585}
3586
3587/**
3588 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3589 * @phba: Pointer to HBA context object.
3590 *
3591 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3592 * API jump table function pointer from the lpfc_hba struct.
3593**/
3594int
3595lpfc_sli_brdrestart(struct lpfc_hba *phba)
3596{
3597 return phba->lpfc_sli_brdrestart(phba);
3598}
3599
3600/**
James Smart3621a712009-04-06 18:47:14 -04003601 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003602 * @phba: Pointer to HBA context object.
3603 *
3604 * This function is called after a HBA restart to wait for successful
3605 * restart of the HBA. Successful restart of the HBA is indicated by
3606 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3607 * iteration, the function will restart the HBA again. The function returns
3608 * zero if HBA successfully restarted else returns negative error code.
3609 **/
dea31012005-04-17 16:05:31 -05003610static int
3611lpfc_sli_chipset_init(struct lpfc_hba *phba)
3612{
3613 uint32_t status, i = 0;
3614
3615 /* Read the HBA Host Status Register */
3616 status = readl(phba->HSregaddr);
3617
3618 /* Check status register to see what current state is */
3619 i = 0;
3620 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3621
3622 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3623 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3624 * 4.
3625 */
3626 if (i++ >= 20) {
3627 /* Adapter failed to init, timeout, status reg
3628 <status> */
James Smarted957682007-06-17 19:56:37 -05003629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003630 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003631 "timeout, status reg x%x, "
3632 "FW Data: A8 x%x AC x%x\n", status,
3633 readl(phba->MBslimaddr + 0xa8),
3634 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003635 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003636 return -ETIMEDOUT;
3637 }
3638
3639 /* Check to see if any errors occurred during init */
3640 if (status & HS_FFERM) {
3641 /* ERROR: During chipset initialization */
3642 /* Adapter failed to init, chipset, status reg
3643 <status> */
James Smarted957682007-06-17 19:56:37 -05003644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003645 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003646 "chipset, status reg x%x, "
3647 "FW Data: A8 x%x AC x%x\n", status,
3648 readl(phba->MBslimaddr + 0xa8),
3649 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003650 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003651 return -EIO;
3652 }
3653
3654 if (i <= 5) {
3655 msleep(10);
3656 } else if (i <= 10) {
3657 msleep(500);
3658 } else {
3659 msleep(2500);
3660 }
3661
3662 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003663 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003664 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003665 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003666 }
3667 /* Read the HBA Host Status Register */
3668 status = readl(phba->HSregaddr);
3669 }
3670
3671 /* Check to see if any errors occurred during init */
3672 if (status & HS_FFERM) {
3673 /* ERROR: During chipset initialization */
3674 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003676 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003677 "status reg x%x, "
3678 "FW Data: A8 x%x AC x%x\n", status,
3679 readl(phba->MBslimaddr + 0xa8),
3680 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003681 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003682 return -EIO;
3683 }
3684
3685 /* Clear all interrupt enable conditions */
3686 writel(0, phba->HCregaddr);
3687 readl(phba->HCregaddr); /* flush */
3688
3689 /* setup host attn register */
3690 writel(0xffffffff, phba->HAregaddr);
3691 readl(phba->HAregaddr); /* flush */
3692 return 0;
3693}
3694
James Smarte59058c2008-08-24 21:49:00 -04003695/**
James Smart3621a712009-04-06 18:47:14 -04003696 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003697 *
3698 * This function calculates and returns the number of HBQs required to be
3699 * configured.
3700 **/
James Smart78b2d852007-08-02 11:10:21 -04003701int
James Smarted957682007-06-17 19:56:37 -05003702lpfc_sli_hbq_count(void)
3703{
James Smart92d7f7b2007-06-17 19:56:38 -05003704 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003705}
3706
James Smarte59058c2008-08-24 21:49:00 -04003707/**
James Smart3621a712009-04-06 18:47:14 -04003708 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003709 *
3710 * This function adds the number of hbq entries in every HBQ to get
3711 * the total number of hbq entries required for the HBA and returns
3712 * the total count.
3713 **/
James Smarted957682007-06-17 19:56:37 -05003714static int
3715lpfc_sli_hbq_entry_count(void)
3716{
3717 int hbq_count = lpfc_sli_hbq_count();
3718 int count = 0;
3719 int i;
3720
3721 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003722 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003723 return count;
3724}
3725
James Smarte59058c2008-08-24 21:49:00 -04003726/**
James Smart3621a712009-04-06 18:47:14 -04003727 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003728 *
3729 * This function calculates amount of memory required for all hbq entries
3730 * to be configured and returns the total memory required.
3731 **/
dea31012005-04-17 16:05:31 -05003732int
James Smarted957682007-06-17 19:56:37 -05003733lpfc_sli_hbq_size(void)
3734{
3735 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3736}
3737
James Smarte59058c2008-08-24 21:49:00 -04003738/**
James Smart3621a712009-04-06 18:47:14 -04003739 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003740 * @phba: Pointer to HBA context object.
3741 *
3742 * This function is called during the SLI initialization to configure
3743 * all the HBQs and post buffers to the HBQ. The caller is not
3744 * required to hold any locks. This function will return zero if successful
3745 * else it will return negative error code.
3746 **/
James Smarted957682007-06-17 19:56:37 -05003747static int
3748lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3749{
3750 int hbq_count = lpfc_sli_hbq_count();
3751 LPFC_MBOXQ_t *pmb;
3752 MAILBOX_t *pmbox;
3753 uint32_t hbqno;
3754 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003755
James Smart92d7f7b2007-06-17 19:56:38 -05003756 /* Get a Mailbox buffer to setup mailbox
3757 * commands for HBA initialization
3758 */
James Smarted957682007-06-17 19:56:37 -05003759 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3760
3761 if (!pmb)
3762 return -ENOMEM;
3763
James Smart04c68492009-05-22 14:52:52 -04003764 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003765
3766 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3767 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003768 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003769
3770 hbq_entry_index = 0;
3771 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3772 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3773 phba->hbqs[hbqno].hbqPutIdx = 0;
3774 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3775 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003776 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003777 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3778 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003779 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3780
3781 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3782 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3783 mbxStatus <status>, ring <num> */
3784
3785 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003786 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003787 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003788 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003789 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003790 pmbox->mbxStatus, hbqno);
3791
3792 phba->link_state = LPFC_HBA_ERROR;
3793 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003794 return ENXIO;
3795 }
3796 }
3797 phba->hbq_count = hbq_count;
3798
James Smarted957682007-06-17 19:56:37 -05003799 mempool_free(pmb, phba->mbox_mem_pool);
3800
James Smart92d7f7b2007-06-17 19:56:38 -05003801 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003802 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3803 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003804 return 0;
3805}
3806
James Smarte59058c2008-08-24 21:49:00 -04003807/**
James Smart4f774512009-05-22 14:52:35 -04003808 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3809 * @phba: Pointer to HBA context object.
3810 *
3811 * This function is called during the SLI initialization to configure
3812 * all the HBQs and post buffers to the HBQ. The caller is not
3813 * required to hold any locks. This function will return zero if successful
3814 * else it will return negative error code.
3815 **/
3816static int
3817lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3818{
3819 phba->hbq_in_use = 1;
3820 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3821 phba->hbq_count = 1;
3822 /* Initially populate or replenish the HBQs */
3823 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3824 return 0;
3825}
3826
3827/**
James Smart3621a712009-04-06 18:47:14 -04003828 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003829 * @phba: Pointer to HBA context object.
3830 * @sli_mode: sli mode - 2/3
3831 *
3832 * This function is called by the sli intialization code path
3833 * to issue config_port mailbox command. This function restarts the
3834 * HBA firmware and issues a config_port mailbox command to configure
3835 * the SLI interface in the sli mode specified by sli_mode
3836 * variable. The caller is not required to hold any locks.
3837 * The function returns 0 if successful, else returns negative error
3838 * code.
3839 **/
James Smart93996272008-08-24 21:50:30 -04003840int
3841lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003842{
3843 LPFC_MBOXQ_t *pmb;
3844 uint32_t resetcount = 0, rc = 0, done = 0;
3845
3846 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3847 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003848 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003849 return -ENOMEM;
3850 }
3851
James Smarted957682007-06-17 19:56:37 -05003852 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003853 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003854 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003855 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003856 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003857 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003858 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003859 rc = lpfc_sli_chipset_init(phba);
3860 if (rc)
3861 break;
3862
James Smart2e0fef82007-06-17 19:56:36 -05003863 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003864 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003865 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003866 resetcount++;
3867
James Smarted957682007-06-17 19:56:37 -05003868 /* Call pre CONFIG_PORT mailbox command initialization. A
3869 * value of 0 means the call was successful. Any other
3870 * nonzero value is a failure, but if ERESTART is returned,
3871 * the driver may reset the HBA and try again.
3872 */
dea31012005-04-17 16:05:31 -05003873 rc = lpfc_config_port_prep(phba);
3874 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003875 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003876 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003877 } else if (rc)
dea31012005-04-17 16:05:31 -05003878 break;
James Smart2e0fef82007-06-17 19:56:36 -05003879 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003880 lpfc_config_port(phba, pmb);
3881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003882 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3883 LPFC_SLI3_HBQ_ENABLED |
3884 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003885 LPFC_SLI3_INB_ENABLED |
3886 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003887 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003889 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003890 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003891 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003892 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003893 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003894 spin_unlock_irq(&phba->hbalock);
3895 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003896 } else {
3897 /* Allow asynchronous mailbox command to go through */
3898 spin_lock_irq(&phba->hbalock);
3899 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3900 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003901 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003902 }
dea31012005-04-17 16:05:31 -05003903 }
James Smarted957682007-06-17 19:56:37 -05003904 if (!done) {
3905 rc = -EINVAL;
3906 goto do_prep_failed;
3907 }
James Smart04c68492009-05-22 14:52:52 -04003908 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3909 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003910 rc = -ENXIO;
3911 goto do_prep_failed;
3912 }
James Smart04c68492009-05-22 14:52:52 -04003913 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003914 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003915 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3916 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3917 phba->max_vpi : phba->max_vports;
3918
James Smart34b02dc2008-08-24 21:49:55 -04003919 } else
3920 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003921 if (pmb->u.mb.un.varCfgPort.gdss)
3922 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3923 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003924 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003925 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003926 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003927 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003928 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003929 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003930 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3931 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3932 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3933 phba->inb_last_counter =
3934 phba->mbox->us.s3_inb_pgp.counter;
3935 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003936 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003937 phba->port_gp = phba->mbox->us.s3_pgp.port;
3938 phba->inb_ha_copy = NULL;
3939 phba->inb_counter = NULL;
3940 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003941
3942 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003943 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003944 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3945 else
3946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3947 "0443 Adapter did not grant "
3948 "BlockGuard\n");
3949 }
James Smart34b02dc2008-08-24 21:49:55 -04003950 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003951 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003952 phba->port_gp = phba->mbox->us.s2.port;
3953 phba->inb_ha_copy = NULL;
3954 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003955 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003956 }
James Smart92d7f7b2007-06-17 19:56:38 -05003957do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003958 mempool_free(pmb, phba->mbox_mem_pool);
3959 return rc;
3960}
3961
James Smarte59058c2008-08-24 21:49:00 -04003962
3963/**
James Smart3621a712009-04-06 18:47:14 -04003964 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003965 * @phba: Pointer to HBA context object.
3966 *
3967 * This function is the main SLI intialization function. This function
3968 * is called by the HBA intialization code, HBA reset code and HBA
3969 * error attention handler code. Caller is not required to hold any
3970 * locks. This function issues config_port mailbox command to configure
3971 * the SLI, setup iocb rings and HBQ rings. In the end the function
3972 * calls the config_port_post function to issue init_link mailbox
3973 * command and to start the discovery. The function will return zero
3974 * if successful, else it will return negative error code.
3975 **/
James Smarted957682007-06-17 19:56:37 -05003976int
3977lpfc_sli_hba_setup(struct lpfc_hba *phba)
3978{
3979 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003980 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003981
3982 switch (lpfc_sli_mode) {
3983 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003984 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003986 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003987 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003988 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003989 break;
3990 }
James Smarted957682007-06-17 19:56:37 -05003991 mode = 2;
3992 break;
3993 case 0:
3994 case 3:
3995 break;
3996 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003998 "1819 Unrecognized lpfc_sli_mode "
3999 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004000
4001 break;
4002 }
4003
James Smart93996272008-08-24 21:50:30 -04004004 rc = lpfc_sli_config_port(phba, mode);
4005
James Smarted957682007-06-17 19:56:37 -05004006 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004008 "1820 Unable to select SLI-3. "
4009 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004010 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004011 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004012 if (rc)
dea31012005-04-17 16:05:31 -05004013 goto lpfc_sli_hba_setup_error;
4014
James Smart0d878412009-10-02 15:16:56 -04004015 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4016 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4017 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4018 if (!rc) {
4019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4020 "2709 This device supports "
4021 "Advanced Error Reporting (AER)\n");
4022 spin_lock_irq(&phba->hbalock);
4023 phba->hba_flag |= HBA_AER_ENABLED;
4024 spin_unlock_irq(&phba->hbalock);
4025 } else {
4026 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4027 "2708 This device does not support "
4028 "Advanced Error Reporting (AER)\n");
4029 phba->cfg_aer_support = 0;
4030 }
4031 }
4032
James Smarted957682007-06-17 19:56:37 -05004033 if (phba->sli_rev == 3) {
4034 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4035 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004036 } else {
4037 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4038 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004039 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004040 }
4041
4042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004043 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4044 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004045 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004046
4047 if (rc)
4048 goto lpfc_sli_hba_setup_error;
4049
James Smart93996272008-08-24 21:50:30 -04004050 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004051 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4052 rc = lpfc_sli_hbq_setup(phba);
4053 if (rc)
4054 goto lpfc_sli_hba_setup_error;
4055 }
James Smart04c68492009-05-22 14:52:52 -04004056 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004057 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004058 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004059
4060 rc = lpfc_config_port_post(phba);
4061 if (rc)
4062 goto lpfc_sli_hba_setup_error;
4063
James Smarted957682007-06-17 19:56:37 -05004064 return rc;
4065
James Smart92d7f7b2007-06-17 19:56:38 -05004066lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004067 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004069 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004070 return rc;
4071}
4072
James Smartda0436e2009-05-22 14:51:39 -04004073/**
4074 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4075 * @phba: Pointer to HBA context object.
4076 * @mboxq: mailbox pointer.
4077 * This function issue a dump mailbox command to read config region
4078 * 23 and parse the records in the region and populate driver
4079 * data structure.
4080 **/
4081static int
4082lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4083 LPFC_MBOXQ_t *mboxq)
4084{
4085 struct lpfc_dmabuf *mp;
4086 struct lpfc_mqe *mqe;
4087 uint32_t data_length;
4088 int rc;
4089
4090 /* Program the default value of vlan_id and fc_map */
4091 phba->valid_vlan = 0;
4092 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4093 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4094 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4095
4096 mqe = &mboxq->u.mqe;
4097 if (lpfc_dump_fcoe_param(phba, mboxq))
4098 return -ENOMEM;
4099
4100 mp = (struct lpfc_dmabuf *) mboxq->context1;
4101 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4102
4103 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4104 "(%d):2571 Mailbox cmd x%x Status x%x "
4105 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4106 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107 "CQ: x%x x%x x%x x%x\n",
4108 mboxq->vport ? mboxq->vport->vpi : 0,
4109 bf_get(lpfc_mqe_command, mqe),
4110 bf_get(lpfc_mqe_status, mqe),
4111 mqe->un.mb_words[0], mqe->un.mb_words[1],
4112 mqe->un.mb_words[2], mqe->un.mb_words[3],
4113 mqe->un.mb_words[4], mqe->un.mb_words[5],
4114 mqe->un.mb_words[6], mqe->un.mb_words[7],
4115 mqe->un.mb_words[8], mqe->un.mb_words[9],
4116 mqe->un.mb_words[10], mqe->un.mb_words[11],
4117 mqe->un.mb_words[12], mqe->un.mb_words[13],
4118 mqe->un.mb_words[14], mqe->un.mb_words[15],
4119 mqe->un.mb_words[16], mqe->un.mb_words[50],
4120 mboxq->mcqe.word0,
4121 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4122 mboxq->mcqe.trailer);
4123
4124 if (rc) {
4125 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4126 kfree(mp);
4127 return -EIO;
4128 }
4129 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004130 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004131 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4132 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004133 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004134 }
James Smartda0436e2009-05-22 14:51:39 -04004135
4136 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4137 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4138 kfree(mp);
4139 return 0;
4140}
4141
4142/**
4143 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4144 * @phba: pointer to lpfc hba data structure.
4145 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4146 * @vpd: pointer to the memory to hold resulting port vpd data.
4147 * @vpd_size: On input, the number of bytes allocated to @vpd.
4148 * On output, the number of data bytes in @vpd.
4149 *
4150 * This routine executes a READ_REV SLI4 mailbox command. In
4151 * addition, this routine gets the port vpd data.
4152 *
4153 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004154 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004155 * ENOMEM - could not allocated memory.
4156 **/
4157static int
4158lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4159 uint8_t *vpd, uint32_t *vpd_size)
4160{
4161 int rc = 0;
4162 uint32_t dma_size;
4163 struct lpfc_dmabuf *dmabuf;
4164 struct lpfc_mqe *mqe;
4165
4166 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4167 if (!dmabuf)
4168 return -ENOMEM;
4169
4170 /*
4171 * Get a DMA buffer for the vpd data resulting from the READ_REV
4172 * mailbox command.
4173 */
4174 dma_size = *vpd_size;
4175 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4176 dma_size,
4177 &dmabuf->phys,
4178 GFP_KERNEL);
4179 if (!dmabuf->virt) {
4180 kfree(dmabuf);
4181 return -ENOMEM;
4182 }
4183 memset(dmabuf->virt, 0, dma_size);
4184
4185 /*
4186 * The SLI4 implementation of READ_REV conflicts at word1,
4187 * bits 31:16 and SLI4 adds vpd functionality not present
4188 * in SLI3. This code corrects the conflicts.
4189 */
4190 lpfc_read_rev(phba, mboxq);
4191 mqe = &mboxq->u.mqe;
4192 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4193 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4194 mqe->un.read_rev.word1 &= 0x0000FFFF;
4195 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4196 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4197
4198 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4199 if (rc) {
4200 dma_free_coherent(&phba->pcidev->dev, dma_size,
4201 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004202 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004203 return -EIO;
4204 }
4205
James Smartda0436e2009-05-22 14:51:39 -04004206 /*
4207 * The available vpd length cannot be bigger than the
4208 * DMA buffer passed to the port. Catch the less than
4209 * case and update the caller's size.
4210 */
4211 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4212 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4213
4214 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4215 dma_free_coherent(&phba->pcidev->dev, dma_size,
4216 dmabuf->virt, dmabuf->phys);
4217 kfree(dmabuf);
4218 return 0;
4219}
4220
4221/**
4222 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4223 * @phba: pointer to lpfc hba data structure.
4224 *
4225 * This routine is called to explicitly arm the SLI4 device's completion and
4226 * event queues
4227 **/
4228static void
4229lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4230{
4231 uint8_t fcp_eqidx;
4232
4233 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4234 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004235 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4236 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4237 LPFC_QUEUE_REARM);
4238 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4239 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4240 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4241 LPFC_QUEUE_REARM);
4242}
4243
4244/**
4245 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4246 * @phba: Pointer to HBA context object.
4247 *
4248 * This function is the main SLI4 device intialization PCI function. This
4249 * function is called by the HBA intialization code, HBA reset code and
4250 * HBA error attention handler code. Caller is not required to hold any
4251 * locks.
4252 **/
4253int
4254lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4255{
4256 int rc;
4257 LPFC_MBOXQ_t *mboxq;
4258 struct lpfc_mqe *mqe;
4259 uint8_t *vpd;
4260 uint32_t vpd_size;
4261 uint32_t ftr_rsp = 0;
4262 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4263 struct lpfc_vport *vport = phba->pport;
4264 struct lpfc_dmabuf *mp;
4265
4266 /* Perform a PCI function reset to start from clean */
4267 rc = lpfc_pci_function_reset(phba);
4268 if (unlikely(rc))
4269 return -ENODEV;
4270
4271 /* Check the HBA Host Status Register for readyness */
4272 rc = lpfc_sli4_post_status_check(phba);
4273 if (unlikely(rc))
4274 return -ENODEV;
4275 else {
4276 spin_lock_irq(&phba->hbalock);
4277 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4278 spin_unlock_irq(&phba->hbalock);
4279 }
4280
4281 /*
4282 * Allocate a single mailbox container for initializing the
4283 * port.
4284 */
4285 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!mboxq)
4287 return -ENOMEM;
4288
4289 /*
4290 * Continue initialization with default values even if driver failed
4291 * to read FCoE param config regions
4292 */
4293 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4294 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004295 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004296
4297 /* Issue READ_REV to collect vpd and FW information. */
4298 vpd_size = PAGE_SIZE;
4299 vpd = kzalloc(vpd_size, GFP_KERNEL);
4300 if (!vpd) {
4301 rc = -ENOMEM;
4302 goto out_free_mbox;
4303 }
4304
4305 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4306 if (unlikely(rc))
4307 goto out_free_vpd;
4308
4309 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004310 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4311 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4312 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004313
4314 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4315 LPFC_DCBX_CEE_MODE)
4316 phba->hba_flag |= HBA_FIP_SUPPORT;
4317 else
4318 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4319
James Smartf1126682009-06-10 17:22:44 -04004320 if (phba->sli_rev != LPFC_SLI_REV4 ||
4321 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4323 "0376 READ_REV Error. SLI Level %d "
4324 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004325 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004326 rc = -EIO;
4327 goto out_free_vpd;
4328 }
James Smartda0436e2009-05-22 14:51:39 -04004329 /*
4330 * Evaluate the read rev and vpd data. Populate the driver
4331 * state with the results. If this routine fails, the failure
4332 * is not fatal as the driver will use generic values.
4333 */
4334 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4335 if (unlikely(!rc)) {
4336 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4337 "0377 Error %d parsing vpd. "
4338 "Using defaults.\n", rc);
4339 rc = 0;
4340 }
4341
James Smartf1126682009-06-10 17:22:44 -04004342 /* Save information as VPD data */
4343 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4344 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4345 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4346 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4347 &mqe->un.read_rev);
4348 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4349 &mqe->un.read_rev);
4350 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4351 &mqe->un.read_rev);
4352 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4353 &mqe->un.read_rev);
4354 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4355 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4356 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4357 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4358 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4359 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4360 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4361 "(%d):0380 READ_REV Status x%x "
4362 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4363 mboxq->vport ? mboxq->vport->vpi : 0,
4364 bf_get(lpfc_mqe_status, mqe),
4365 phba->vpd.rev.opFwName,
4366 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4367 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004368
4369 /*
4370 * Discover the port's supported feature set and match it against the
4371 * hosts requests.
4372 */
4373 lpfc_request_features(phba, mboxq);
4374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4375 if (unlikely(rc)) {
4376 rc = -EIO;
4377 goto out_free_vpd;
4378 }
4379
4380 /*
4381 * The port must support FCP initiator mode as this is the
4382 * only mode running in the host.
4383 */
4384 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4385 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4386 "0378 No support for fcpi mode.\n");
4387 ftr_rsp++;
4388 }
4389
4390 /*
4391 * If the port cannot support the host's requested features
4392 * then turn off the global config parameters to disable the
4393 * feature in the driver. This is not a fatal error.
4394 */
4395 if ((phba->cfg_enable_bg) &&
4396 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4397 ftr_rsp++;
4398
4399 if (phba->max_vpi && phba->cfg_enable_npiv &&
4400 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4401 ftr_rsp++;
4402
4403 if (ftr_rsp) {
4404 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4405 "0379 Feature Mismatch Data: x%08x %08x "
4406 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4407 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4408 phba->cfg_enable_npiv, phba->max_vpi);
4409 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4410 phba->cfg_enable_bg = 0;
4411 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4412 phba->cfg_enable_npiv = 0;
4413 }
4414
4415 /* These SLI3 features are assumed in SLI4 */
4416 spin_lock_irq(&phba->hbalock);
4417 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4418 spin_unlock_irq(&phba->hbalock);
4419
4420 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004421 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4422 if (rc) {
4423 phba->link_state = LPFC_HBA_ERROR;
4424 rc = -ENOMEM;
4425 goto out_free_vpd;
4426 }
4427
James Smartda0436e2009-05-22 14:51:39 -04004428 mboxq->vport = vport;
4429 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4430 mp = (struct lpfc_dmabuf *) mboxq->context1;
4431 if (rc == MBX_SUCCESS) {
4432 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4433 rc = 0;
4434 }
4435
4436 /*
4437 * This memory was allocated by the lpfc_read_sparam routine. Release
4438 * it to the mbuf pool.
4439 */
4440 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4441 kfree(mp);
4442 mboxq->context1 = NULL;
4443 if (unlikely(rc)) {
4444 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4445 "0382 READ_SPARAM command failed "
4446 "status %d, mbxStatus x%x\n",
4447 rc, bf_get(lpfc_mqe_status, mqe));
4448 phba->link_state = LPFC_HBA_ERROR;
4449 rc = -EIO;
4450 goto out_free_vpd;
4451 }
4452
4453 if (phba->cfg_soft_wwnn)
4454 u64_to_wwn(phba->cfg_soft_wwnn,
4455 vport->fc_sparam.nodeName.u.wwn);
4456 if (phba->cfg_soft_wwpn)
4457 u64_to_wwn(phba->cfg_soft_wwpn,
4458 vport->fc_sparam.portName.u.wwn);
4459 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4460 sizeof(struct lpfc_name));
4461 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4462 sizeof(struct lpfc_name));
4463
4464 /* Update the fc_host data structures with new wwn. */
4465 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4466 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4467
4468 /* Register SGL pool to the device using non-embedded mailbox command */
4469 rc = lpfc_sli4_post_sgl_list(phba);
4470 if (unlikely(rc)) {
4471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004472 "0582 Error %d during sgl post operation\n",
4473 rc);
James Smartda0436e2009-05-22 14:51:39 -04004474 rc = -ENODEV;
4475 goto out_free_vpd;
4476 }
4477
4478 /* Register SCSI SGL pool to the device */
4479 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4480 if (unlikely(rc)) {
4481 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004482 "0383 Error %d during scsi sgl post "
4483 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004484 /* Some Scsi buffers were moved to the abort scsi list */
4485 /* A pci function reset will repost them */
4486 rc = -ENODEV;
4487 goto out_free_vpd;
4488 }
4489
4490 /* Post the rpi header region to the device. */
4491 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4492 if (unlikely(rc)) {
4493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4494 "0393 Error %d during rpi post operation\n",
4495 rc);
4496 rc = -ENODEV;
4497 goto out_free_vpd;
4498 }
James Smartda0436e2009-05-22 14:51:39 -04004499
4500 /* Set up all the queues to the device */
4501 rc = lpfc_sli4_queue_setup(phba);
4502 if (unlikely(rc)) {
4503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4504 "0381 Error %d during queue setup.\n ", rc);
4505 goto out_stop_timers;
4506 }
4507
4508 /* Arm the CQs and then EQs on device */
4509 lpfc_sli4_arm_cqeq_intr(phba);
4510
4511 /* Indicate device interrupt mode */
4512 phba->sli4_hba.intr_enable = 1;
4513
4514 /* Allow asynchronous mailbox command to go through */
4515 spin_lock_irq(&phba->hbalock);
4516 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4517 spin_unlock_irq(&phba->hbalock);
4518
4519 /* Post receive buffers to the device */
4520 lpfc_sli4_rb_setup(phba);
4521
4522 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004523 mod_timer(&vport->els_tmofunc,
4524 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004525
4526 /* Start heart beat timer */
4527 mod_timer(&phba->hb_tmofunc,
4528 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4529 phba->hb_outstanding = 0;
4530 phba->last_completion_time = jiffies;
4531
4532 /* Start error attention (ERATT) polling timer */
4533 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4534
4535 /*
4536 * The port is ready, set the host's link state to LINK_DOWN
4537 * in preparation for link interrupts.
4538 */
4539 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4540 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4541 lpfc_set_loopback_flag(phba);
4542 /* Change driver state to LPFC_LINK_DOWN right before init link */
4543 spin_lock_irq(&phba->hbalock);
4544 phba->link_state = LPFC_LINK_DOWN;
4545 spin_unlock_irq(&phba->hbalock);
4546 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4547 if (unlikely(rc != MBX_NOT_FINISHED)) {
4548 kfree(vpd);
4549 return 0;
4550 } else
4551 rc = -EIO;
4552
4553 /* Unset all the queues set up in this routine when error out */
4554 if (rc)
4555 lpfc_sli4_queue_unset(phba);
4556
4557out_stop_timers:
4558 if (rc)
4559 lpfc_stop_hba_timers(phba);
4560out_free_vpd:
4561 kfree(vpd);
4562out_free_mbox:
4563 mempool_free(mboxq, phba->mbox_mem_pool);
4564 return rc;
4565}
James Smarte59058c2008-08-24 21:49:00 -04004566
4567/**
James Smart3621a712009-04-06 18:47:14 -04004568 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004569 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004570 *
James Smarte59058c2008-08-24 21:49:00 -04004571 * This is the callback function for mailbox timer. The mailbox
4572 * timer is armed when a new mailbox command is issued and the timer
4573 * is deleted when the mailbox complete. The function is called by
4574 * the kernel timer code when a mailbox does not complete within
4575 * expected time. This function wakes up the worker thread to
4576 * process the mailbox timeout and returns. All the processing is
4577 * done by the worker thread function lpfc_mbox_timeout_handler.
4578 **/
dea31012005-04-17 16:05:31 -05004579void
4580lpfc_mbox_timeout(unsigned long ptr)
4581{
James Smart92d7f7b2007-06-17 19:56:38 -05004582 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004583 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004584 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004585
James Smart2e0fef82007-06-17 19:56:36 -05004586 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004587 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004588 if (!tmo_posted)
4589 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4590 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4591
James Smart5e9d9b82008-06-14 22:52:53 -04004592 if (!tmo_posted)
4593 lpfc_worker_wake_up(phba);
4594 return;
dea31012005-04-17 16:05:31 -05004595}
4596
James Smarte59058c2008-08-24 21:49:00 -04004597
4598/**
James Smart3621a712009-04-06 18:47:14 -04004599 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004600 * @phba: Pointer to HBA context object.
4601 *
4602 * This function is called from worker thread when a mailbox command times out.
4603 * The caller is not required to hold any locks. This function will reset the
4604 * HBA and recover all the pending commands.
4605 **/
dea31012005-04-17 16:05:31 -05004606void
4607lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4608{
James Smart2e0fef82007-06-17 19:56:36 -05004609 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004610 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004611 struct lpfc_sli *psli = &phba->sli;
4612 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004613
James Smarta257bf92009-04-06 18:48:10 -04004614 /* Check the pmbox pointer first. There is a race condition
4615 * between the mbox timeout handler getting executed in the
4616 * worklist and the mailbox actually completing. When this
4617 * race condition occurs, the mbox_active will be NULL.
4618 */
4619 spin_lock_irq(&phba->hbalock);
4620 if (pmbox == NULL) {
4621 lpfc_printf_log(phba, KERN_WARNING,
4622 LOG_MBOX | LOG_SLI,
4623 "0353 Active Mailbox cleared - mailbox timeout "
4624 "exiting\n");
4625 spin_unlock_irq(&phba->hbalock);
4626 return;
4627 }
4628
dea31012005-04-17 16:05:31 -05004629 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004630 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004631 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004632 mb->mbxCommand,
4633 phba->pport->port_state,
4634 phba->sli.sli_flag,
4635 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004636 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004637
James Smart1dcb58e2007-04-25 09:51:30 -04004638 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4639 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4640 * it to fail all oustanding SCSI IO.
4641 */
James Smart2e0fef82007-06-17 19:56:36 -05004642 spin_lock_irq(&phba->pport->work_port_lock);
4643 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4644 spin_unlock_irq(&phba->pport->work_port_lock);
4645 spin_lock_irq(&phba->hbalock);
4646 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004647 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004648 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004649
4650 pring = &psli->ring[psli->fcp_ring];
4651 lpfc_sli_abort_iocb_ring(phba, pring);
4652
4653 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004654 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004655
4656 /* Reset the HBA device */
4657 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004658}
4659
James Smarte59058c2008-08-24 21:49:00 -04004660/**
James Smart3772a992009-05-22 14:50:54 -04004661 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004662 * @phba: Pointer to HBA context object.
4663 * @pmbox: Pointer to mailbox object.
4664 * @flag: Flag indicating how the mailbox need to be processed.
4665 *
4666 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004667 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4668 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004669 * The mailbox command can be submitted in polling mode, in which case
4670 * this function will wait in a polling loop for the completion of the
4671 * mailbox.
4672 * If the mailbox is submitted in no_wait mode (not polling) the
4673 * function will submit the command and returns immediately without waiting
4674 * for the mailbox completion. The no_wait is supported only when HBA
4675 * is in SLI2/SLI3 mode - interrupts are enabled.
4676 * The SLI interface allows only one mailbox pending at a time. If the
4677 * mailbox is issued in polling mode and there is already a mailbox
4678 * pending, then the function will return an error. If the mailbox is issued
4679 * in NO_WAIT mode and there is a mailbox pending already, the function
4680 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4681 * The sli layer owns the mailbox object until the completion of mailbox
4682 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4683 * return codes the caller owns the mailbox command after the return of
4684 * the function.
4685 **/
James Smart3772a992009-05-22 14:50:54 -04004686static int
4687lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4688 uint32_t flag)
dea31012005-04-17 16:05:31 -05004689{
dea31012005-04-17 16:05:31 -05004690 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004691 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004692 uint32_t status, evtctr;
4693 uint32_t ha_copy;
4694 int i;
James Smart09372822008-01-11 01:52:54 -05004695 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004696 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004697 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004698 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004699 int processing_queue = 0;
4700
4701 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4702 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004703 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004704 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004705 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4706 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4707 return MBX_SUCCESS;
4708 }
James Smart58da1ff2008-04-07 10:15:56 -04004709 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004710 pmbox = lpfc_mbox_get(phba);
4711 if (!pmbox) {
4712 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4713 return MBX_SUCCESS;
4714 }
4715 }
dea31012005-04-17 16:05:31 -05004716
James Smarted957682007-06-17 19:56:37 -05004717 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004718 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004719 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004720 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004721 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004722 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004723 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004724 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004725 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004726 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004727 }
4728 }
4729
Linas Vepstas8d63f372007-02-14 14:28:36 -06004730 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004731 if (unlikely(pci_channel_offline(phba->pcidev))) {
4732 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4733 goto out_not_finished;
4734 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004735
James Smarta257bf92009-04-06 18:48:10 -04004736 /* If HBA has a deferred error attention, fail the iocb. */
4737 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4738 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4739 goto out_not_finished;
4740 }
4741
dea31012005-04-17 16:05:31 -05004742 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004743
James Smart3772a992009-05-22 14:50:54 -04004744 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004745 status = MBX_SUCCESS;
4746
James Smart2e0fef82007-06-17 19:56:36 -05004747 if (phba->link_state == LPFC_HBA_ERROR) {
4748 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004749
4750 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004751 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4752 "(%d):0311 Mailbox command x%x cannot "
4753 "issue Data: x%x x%x\n",
4754 pmbox->vport ? pmbox->vport->vpi : 0,
4755 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004756 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004757 }
4758
James Smart92908312006-03-07 15:04:13 -05004759 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4760 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004761 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4763 "(%d):2528 Mailbox command x%x cannot "
4764 "issue Data: x%x x%x\n",
4765 pmbox->vport ? pmbox->vport->vpi : 0,
4766 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004767 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004768 }
4769
dea31012005-04-17 16:05:31 -05004770 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4771 /* Polling for a mbox command when another one is already active
4772 * is not allowed in SLI. Also, the driver must have established
4773 * SLI2 mode to queue and process multiple mbox commands.
4774 */
4775
4776 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004777 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004778
4779 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004780 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4781 "(%d):2529 Mailbox command x%x "
4782 "cannot issue Data: x%x x%x\n",
4783 pmbox->vport ? pmbox->vport->vpi : 0,
4784 pmbox->u.mb.mbxCommand,
4785 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004786 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004787 }
4788
James Smart3772a992009-05-22 14:50:54 -04004789 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004790 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004791 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4793 "(%d):2530 Mailbox command x%x "
4794 "cannot issue Data: x%x x%x\n",
4795 pmbox->vport ? pmbox->vport->vpi : 0,
4796 pmbox->u.mb.mbxCommand,
4797 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004798 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004799 }
4800
dea31012005-04-17 16:05:31 -05004801 /* Another mailbox command is still being processed, queue this
4802 * command to be processed later.
4803 */
4804 lpfc_mbox_put(phba, pmbox);
4805
4806 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004807 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004808 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004809 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004810 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4811 mb->mbxCommand, phba->pport->port_state,
4812 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004813
4814 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004815 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004816
James Smart858c9f62007-06-17 19:56:39 -05004817 if (pmbox->vport) {
4818 lpfc_debugfs_disc_trc(pmbox->vport,
4819 LPFC_DISC_TRC_MBOX_VPORT,
4820 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4821 (uint32_t)mb->mbxCommand,
4822 mb->un.varWords[0], mb->un.varWords[1]);
4823 }
4824 else {
4825 lpfc_debugfs_disc_trc(phba->pport,
4826 LPFC_DISC_TRC_MBOX,
4827 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4828 (uint32_t)mb->mbxCommand,
4829 mb->un.varWords[0], mb->un.varWords[1]);
4830 }
4831
James Smart2e0fef82007-06-17 19:56:36 -05004832 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004833 }
4834
dea31012005-04-17 16:05:31 -05004835 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4836
4837 /* If we are not polling, we MUST be in SLI2 mode */
4838 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004839 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004840 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004841 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004842 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004843 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004844 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4845 "(%d):2531 Mailbox command x%x "
4846 "cannot issue Data: x%x x%x\n",
4847 pmbox->vport ? pmbox->vport->vpi : 0,
4848 pmbox->u.mb.mbxCommand,
4849 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004850 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004851 }
4852 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004853 mod_timer(&psli->mbox_tmo, (jiffies +
4854 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004855 }
4856
4857 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004858 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004859 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004860 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004861 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004862 mb->mbxCommand, phba->pport->port_state,
4863 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004864
James Smart858c9f62007-06-17 19:56:39 -05004865 if (mb->mbxCommand != MBX_HEARTBEAT) {
4866 if (pmbox->vport) {
4867 lpfc_debugfs_disc_trc(pmbox->vport,
4868 LPFC_DISC_TRC_MBOX_VPORT,
4869 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4870 (uint32_t)mb->mbxCommand,
4871 mb->un.varWords[0], mb->un.varWords[1]);
4872 }
4873 else {
4874 lpfc_debugfs_disc_trc(phba->pport,
4875 LPFC_DISC_TRC_MBOX,
4876 "MBOX Send: cmd:x%x mb:x%x x%x",
4877 (uint32_t)mb->mbxCommand,
4878 mb->un.varWords[0], mb->un.varWords[1]);
4879 }
4880 }
4881
dea31012005-04-17 16:05:31 -05004882 psli->slistat.mbox_cmd++;
4883 evtctr = psli->slistat.mbox_event;
4884
4885 /* next set own bit for the adapter and copy over command word */
4886 mb->mbxOwner = OWN_CHIP;
4887
James Smart3772a992009-05-22 14:50:54 -04004888 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004889 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004890 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004891 } else {
James Smart92908312006-03-07 15:04:13 -05004892 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004893 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004894 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004895 }
4896
4897 /* First copy mbox command data to HBA SLIM, skip past first
4898 word */
4899 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4900 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4901 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4902
4903 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004904 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004905 to_slim = phba->MBslimaddr;
4906 writel(ldata, to_slim);
4907 readl(to_slim); /* flush */
4908
4909 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4910 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004911 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004912 }
4913 }
4914
4915 wmb();
dea31012005-04-17 16:05:31 -05004916
4917 switch (flag) {
4918 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004919 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004920 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004921 /* Interrupt board to do it */
4922 writel(CA_MBATT, phba->CAregaddr);
4923 readl(phba->CAregaddr); /* flush */
4924 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004925 break;
4926
4927 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004928 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004929 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004930 /* Interrupt board to do it */
4931 writel(CA_MBATT, phba->CAregaddr);
4932 readl(phba->CAregaddr); /* flush */
4933
James Smart3772a992009-05-22 14:50:54 -04004934 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004935 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004936 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004937 word0 = le32_to_cpu(word0);
4938 } else {
4939 /* First read mbox status word */
4940 word0 = readl(phba->MBslimaddr);
4941 }
4942
4943 /* Read the HBA Host Attention Register */
4944 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004945 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4946 mb->mbxCommand) *
4947 1000) + jiffies;
4948 i = 0;
dea31012005-04-17 16:05:31 -05004949 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004950 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4951 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004952 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004953 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004954 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004955 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004956 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004957 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004958 }
4959
4960 /* Check if we took a mbox interrupt while we were
4961 polling */
4962 if (((word0 & OWN_CHIP) != OWN_CHIP)
4963 && (evtctr != psli->slistat.mbox_event))
4964 break;
4965
James Smart09372822008-01-11 01:52:54 -05004966 if (i++ > 10) {
4967 spin_unlock_irqrestore(&phba->hbalock,
4968 drvr_flag);
4969 msleep(1);
4970 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4971 }
dea31012005-04-17 16:05:31 -05004972
James Smart3772a992009-05-22 14:50:54 -04004973 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004974 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004975 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004976 word0 = le32_to_cpu(word0);
4977 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4978 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004979 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004980 /* Check real SLIM for any errors */
4981 slimword0 = readl(phba->MBslimaddr);
4982 slimmb = (MAILBOX_t *) & slimword0;
4983 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4984 && slimmb->mbxStatus) {
4985 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004986 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004987 word0 = slimword0;
4988 }
4989 }
4990 } else {
4991 /* First copy command data */
4992 word0 = readl(phba->MBslimaddr);
4993 }
4994 /* Read the HBA Host Attention Register */
4995 ha_copy = readl(phba->HAregaddr);
4996 }
4997
James Smart3772a992009-05-22 14:50:54 -04004998 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004999 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005000 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005001 } else {
5002 /* First copy command data */
5003 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5004 MAILBOX_CMD_SIZE);
5005 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5006 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05005007 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05005008 phba->MBslimaddr + DMP_RSP_OFFSET,
5009 mb->un.varDmp.word_cnt);
5010 }
5011 }
5012
5013 writel(HA_MBATT, phba->HAregaddr);
5014 readl(phba->HAregaddr); /* flush */
5015
5016 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5017 status = mb->mbxStatus;
5018 }
5019
James Smart2e0fef82007-06-17 19:56:36 -05005020 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5021 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005022
5023out_not_finished:
5024 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005025 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005026 lpfc_mbox_cmpl_put(phba, pmbox);
5027 }
5028 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005029}
5030
James Smarte59058c2008-08-24 21:49:00 -04005031/**
James Smartf1126682009-06-10 17:22:44 -04005032 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5033 * @phba: Pointer to HBA context object.
5034 *
5035 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5036 * the driver internal pending mailbox queue. It will then try to wait out the
5037 * possible outstanding mailbox command before return.
5038 *
5039 * Returns:
5040 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5041 * the outstanding mailbox command timed out.
5042 **/
5043static int
5044lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5045{
5046 struct lpfc_sli *psli = &phba->sli;
5047 uint8_t actcmd = MBX_HEARTBEAT;
5048 int rc = 0;
5049 unsigned long timeout;
5050
5051 /* Mark the asynchronous mailbox command posting as blocked */
5052 spin_lock_irq(&phba->hbalock);
5053 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5054 if (phba->sli.mbox_active)
5055 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5056 spin_unlock_irq(&phba->hbalock);
5057 /* Determine how long we might wait for the active mailbox
5058 * command to be gracefully completed by firmware.
5059 */
5060 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5061 jiffies;
5062 /* Wait for the outstnading mailbox command to complete */
5063 while (phba->sli.mbox_active) {
5064 /* Check active mailbox complete status every 2ms */
5065 msleep(2);
5066 if (time_after(jiffies, timeout)) {
5067 /* Timeout, marked the outstanding cmd not complete */
5068 rc = 1;
5069 break;
5070 }
5071 }
5072
5073 /* Can not cleanly block async mailbox command, fails it */
5074 if (rc) {
5075 spin_lock_irq(&phba->hbalock);
5076 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5077 spin_unlock_irq(&phba->hbalock);
5078 }
5079 return rc;
5080}
5081
5082/**
5083 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5084 * @phba: Pointer to HBA context object.
5085 *
5086 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5087 * commands from the driver internal pending mailbox queue. It makes sure
5088 * that there is no outstanding mailbox command before resuming posting
5089 * asynchronous mailbox commands. If, for any reason, there is outstanding
5090 * mailbox command, it will try to wait it out before resuming asynchronous
5091 * mailbox command posting.
5092 **/
5093static void
5094lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5095{
5096 struct lpfc_sli *psli = &phba->sli;
5097
5098 spin_lock_irq(&phba->hbalock);
5099 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5100 /* Asynchronous mailbox posting is not blocked, do nothing */
5101 spin_unlock_irq(&phba->hbalock);
5102 return;
5103 }
5104
5105 /* Outstanding synchronous mailbox command is guaranteed to be done,
5106 * successful or timeout, after timing-out the outstanding mailbox
5107 * command shall always be removed, so just unblock posting async
5108 * mailbox command and resume
5109 */
5110 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5111 spin_unlock_irq(&phba->hbalock);
5112
5113 /* wake up worker thread to post asynchronlous mailbox command */
5114 lpfc_worker_wake_up(phba);
5115}
5116
5117/**
James Smartda0436e2009-05-22 14:51:39 -04005118 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5119 * @phba: Pointer to HBA context object.
5120 * @mboxq: Pointer to mailbox object.
5121 *
5122 * The function posts a mailbox to the port. The mailbox is expected
5123 * to be comletely filled in and ready for the port to operate on it.
5124 * This routine executes a synchronous completion operation on the
5125 * mailbox by polling for its completion.
5126 *
5127 * The caller must not be holding any locks when calling this routine.
5128 *
5129 * Returns:
5130 * MBX_SUCCESS - mailbox posted successfully
5131 * Any of the MBX error values.
5132 **/
5133static int
5134lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5135{
5136 int rc = MBX_SUCCESS;
5137 unsigned long iflag;
5138 uint32_t db_ready;
5139 uint32_t mcqe_status;
5140 uint32_t mbx_cmnd;
5141 unsigned long timeout;
5142 struct lpfc_sli *psli = &phba->sli;
5143 struct lpfc_mqe *mb = &mboxq->u.mqe;
5144 struct lpfc_bmbx_create *mbox_rgn;
5145 struct dma_address *dma_address;
5146 struct lpfc_register bmbx_reg;
5147
5148 /*
5149 * Only one mailbox can be active to the bootstrap mailbox region
5150 * at a time and there is no queueing provided.
5151 */
5152 spin_lock_irqsave(&phba->hbalock, iflag);
5153 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5154 spin_unlock_irqrestore(&phba->hbalock, iflag);
5155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5156 "(%d):2532 Mailbox command x%x (x%x) "
5157 "cannot issue Data: x%x x%x\n",
5158 mboxq->vport ? mboxq->vport->vpi : 0,
5159 mboxq->u.mb.mbxCommand,
5160 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5161 psli->sli_flag, MBX_POLL);
5162 return MBXERR_ERROR;
5163 }
5164 /* The server grabs the token and owns it until release */
5165 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5166 phba->sli.mbox_active = mboxq;
5167 spin_unlock_irqrestore(&phba->hbalock, iflag);
5168
5169 /*
5170 * Initialize the bootstrap memory region to avoid stale data areas
5171 * in the mailbox post. Then copy the caller's mailbox contents to
5172 * the bmbx mailbox region.
5173 */
5174 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5175 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5176 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5177 sizeof(struct lpfc_mqe));
5178
5179 /* Post the high mailbox dma address to the port and wait for ready. */
5180 dma_address = &phba->sli4_hba.bmbx.dma_address;
5181 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5182
5183 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5184 * 1000) + jiffies;
5185 do {
5186 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5187 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5188 if (!db_ready)
5189 msleep(2);
5190
5191 if (time_after(jiffies, timeout)) {
5192 rc = MBXERR_ERROR;
5193 goto exit;
5194 }
5195 } while (!db_ready);
5196
5197 /* Post the low mailbox dma address to the port. */
5198 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5199 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5200 * 1000) + jiffies;
5201 do {
5202 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5203 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5204 if (!db_ready)
5205 msleep(2);
5206
5207 if (time_after(jiffies, timeout)) {
5208 rc = MBXERR_ERROR;
5209 goto exit;
5210 }
5211 } while (!db_ready);
5212
5213 /*
5214 * Read the CQ to ensure the mailbox has completed.
5215 * If so, update the mailbox status so that the upper layers
5216 * can complete the request normally.
5217 */
5218 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5219 sizeof(struct lpfc_mqe));
5220 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5221 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5222 sizeof(struct lpfc_mcqe));
5223 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5224
5225 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5226 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5227 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5228 rc = MBXERR_ERROR;
5229 }
5230
5231 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5232 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5233 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5234 " x%x x%x CQ: x%x x%x x%x x%x\n",
5235 mboxq->vport ? mboxq->vport->vpi : 0,
5236 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5237 bf_get(lpfc_mqe_status, mb),
5238 mb->un.mb_words[0], mb->un.mb_words[1],
5239 mb->un.mb_words[2], mb->un.mb_words[3],
5240 mb->un.mb_words[4], mb->un.mb_words[5],
5241 mb->un.mb_words[6], mb->un.mb_words[7],
5242 mb->un.mb_words[8], mb->un.mb_words[9],
5243 mb->un.mb_words[10], mb->un.mb_words[11],
5244 mb->un.mb_words[12], mboxq->mcqe.word0,
5245 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5246 mboxq->mcqe.trailer);
5247exit:
5248 /* We are holding the token, no needed for lock when release */
5249 spin_lock_irqsave(&phba->hbalock, iflag);
5250 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5251 phba->sli.mbox_active = NULL;
5252 spin_unlock_irqrestore(&phba->hbalock, iflag);
5253 return rc;
5254}
5255
5256/**
5257 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5258 * @phba: Pointer to HBA context object.
5259 * @pmbox: Pointer to mailbox object.
5260 * @flag: Flag indicating how the mailbox need to be processed.
5261 *
5262 * This function is called by discovery code and HBA management code to submit
5263 * a mailbox command to firmware with SLI-4 interface spec.
5264 *
5265 * Return codes the caller owns the mailbox command after the return of the
5266 * function.
5267 **/
5268static int
5269lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5270 uint32_t flag)
5271{
5272 struct lpfc_sli *psli = &phba->sli;
5273 unsigned long iflags;
5274 int rc;
5275
James Smart8fa38512009-07-19 10:01:03 -04005276 rc = lpfc_mbox_dev_check(phba);
5277 if (unlikely(rc)) {
5278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5279 "(%d):2544 Mailbox command x%x (x%x) "
5280 "cannot issue Data: x%x x%x\n",
5281 mboxq->vport ? mboxq->vport->vpi : 0,
5282 mboxq->u.mb.mbxCommand,
5283 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5284 psli->sli_flag, flag);
5285 goto out_not_finished;
5286 }
5287
James Smartda0436e2009-05-22 14:51:39 -04005288 /* Detect polling mode and jump to a handler */
5289 if (!phba->sli4_hba.intr_enable) {
5290 if (flag == MBX_POLL)
5291 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5292 else
5293 rc = -EIO;
5294 if (rc != MBX_SUCCESS)
5295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5296 "(%d):2541 Mailbox command x%x "
5297 "(x%x) cannot issue Data: x%x x%x\n",
5298 mboxq->vport ? mboxq->vport->vpi : 0,
5299 mboxq->u.mb.mbxCommand,
5300 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5301 psli->sli_flag, flag);
5302 return rc;
5303 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005304 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5305 "(%d):2542 Try to issue mailbox command "
5306 "x%x (x%x) synchronously ahead of async"
5307 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005308 mboxq->vport ? mboxq->vport->vpi : 0,
5309 mboxq->u.mb.mbxCommand,
5310 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5311 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005312 /* Try to block the asynchronous mailbox posting */
5313 rc = lpfc_sli4_async_mbox_block(phba);
5314 if (!rc) {
5315 /* Successfully blocked, now issue sync mbox cmd */
5316 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5317 if (rc != MBX_SUCCESS)
5318 lpfc_printf_log(phba, KERN_ERR,
5319 LOG_MBOX | LOG_SLI,
5320 "(%d):2597 Mailbox command "
5321 "x%x (x%x) cannot issue "
5322 "Data: x%x x%x\n",
5323 mboxq->vport ?
5324 mboxq->vport->vpi : 0,
5325 mboxq->u.mb.mbxCommand,
5326 lpfc_sli4_mbox_opcode_get(phba,
5327 mboxq),
5328 psli->sli_flag, flag);
5329 /* Unblock the async mailbox posting afterward */
5330 lpfc_sli4_async_mbox_unblock(phba);
5331 }
5332 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005333 }
5334
5335 /* Now, interrupt mode asynchrous mailbox command */
5336 rc = lpfc_mbox_cmd_check(phba, mboxq);
5337 if (rc) {
5338 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5339 "(%d):2543 Mailbox command x%x (x%x) "
5340 "cannot issue Data: x%x x%x\n",
5341 mboxq->vport ? mboxq->vport->vpi : 0,
5342 mboxq->u.mb.mbxCommand,
5343 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5344 psli->sli_flag, flag);
5345 goto out_not_finished;
5346 }
James Smartda0436e2009-05-22 14:51:39 -04005347
5348 /* Put the mailbox command to the driver internal FIFO */
5349 psli->slistat.mbox_busy++;
5350 spin_lock_irqsave(&phba->hbalock, iflags);
5351 lpfc_mbox_put(phba, mboxq);
5352 spin_unlock_irqrestore(&phba->hbalock, iflags);
5353 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5354 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5355 "x%x (x%x) x%x x%x x%x\n",
5356 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5357 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5358 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5359 phba->pport->port_state,
5360 psli->sli_flag, MBX_NOWAIT);
5361 /* Wake up worker thread to transport mailbox command from head */
5362 lpfc_worker_wake_up(phba);
5363
5364 return MBX_BUSY;
5365
5366out_not_finished:
5367 return MBX_NOT_FINISHED;
5368}
5369
5370/**
5371 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5372 * @phba: Pointer to HBA context object.
5373 *
5374 * This function is called by worker thread to send a mailbox command to
5375 * SLI4 HBA firmware.
5376 *
5377 **/
5378int
5379lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5380{
5381 struct lpfc_sli *psli = &phba->sli;
5382 LPFC_MBOXQ_t *mboxq;
5383 int rc = MBX_SUCCESS;
5384 unsigned long iflags;
5385 struct lpfc_mqe *mqe;
5386 uint32_t mbx_cmnd;
5387
5388 /* Check interrupt mode before post async mailbox command */
5389 if (unlikely(!phba->sli4_hba.intr_enable))
5390 return MBX_NOT_FINISHED;
5391
5392 /* Check for mailbox command service token */
5393 spin_lock_irqsave(&phba->hbalock, iflags);
5394 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5395 spin_unlock_irqrestore(&phba->hbalock, iflags);
5396 return MBX_NOT_FINISHED;
5397 }
5398 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5399 spin_unlock_irqrestore(&phba->hbalock, iflags);
5400 return MBX_NOT_FINISHED;
5401 }
5402 if (unlikely(phba->sli.mbox_active)) {
5403 spin_unlock_irqrestore(&phba->hbalock, iflags);
5404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5405 "0384 There is pending active mailbox cmd\n");
5406 return MBX_NOT_FINISHED;
5407 }
5408 /* Take the mailbox command service token */
5409 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5410
5411 /* Get the next mailbox command from head of queue */
5412 mboxq = lpfc_mbox_get(phba);
5413
5414 /* If no more mailbox command waiting for post, we're done */
5415 if (!mboxq) {
5416 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5417 spin_unlock_irqrestore(&phba->hbalock, iflags);
5418 return MBX_SUCCESS;
5419 }
5420 phba->sli.mbox_active = mboxq;
5421 spin_unlock_irqrestore(&phba->hbalock, iflags);
5422
5423 /* Check device readiness for posting mailbox command */
5424 rc = lpfc_mbox_dev_check(phba);
5425 if (unlikely(rc))
5426 /* Driver clean routine will clean up pending mailbox */
5427 goto out_not_finished;
5428
5429 /* Prepare the mbox command to be posted */
5430 mqe = &mboxq->u.mqe;
5431 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5432
5433 /* Start timer for the mbox_tmo and log some mailbox post messages */
5434 mod_timer(&psli->mbox_tmo, (jiffies +
5435 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5436
5437 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5438 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5439 "x%x x%x\n",
5440 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5441 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5442 phba->pport->port_state, psli->sli_flag);
5443
5444 if (mbx_cmnd != MBX_HEARTBEAT) {
5445 if (mboxq->vport) {
5446 lpfc_debugfs_disc_trc(mboxq->vport,
5447 LPFC_DISC_TRC_MBOX_VPORT,
5448 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5449 mbx_cmnd, mqe->un.mb_words[0],
5450 mqe->un.mb_words[1]);
5451 } else {
5452 lpfc_debugfs_disc_trc(phba->pport,
5453 LPFC_DISC_TRC_MBOX,
5454 "MBOX Send: cmd:x%x mb:x%x x%x",
5455 mbx_cmnd, mqe->un.mb_words[0],
5456 mqe->un.mb_words[1]);
5457 }
5458 }
5459 psli->slistat.mbox_cmd++;
5460
5461 /* Post the mailbox command to the port */
5462 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5463 if (rc != MBX_SUCCESS) {
5464 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5465 "(%d):2533 Mailbox command x%x (x%x) "
5466 "cannot issue Data: x%x x%x\n",
5467 mboxq->vport ? mboxq->vport->vpi : 0,
5468 mboxq->u.mb.mbxCommand,
5469 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5470 psli->sli_flag, MBX_NOWAIT);
5471 goto out_not_finished;
5472 }
5473
5474 return rc;
5475
5476out_not_finished:
5477 spin_lock_irqsave(&phba->hbalock, iflags);
5478 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5479 __lpfc_mbox_cmpl_put(phba, mboxq);
5480 /* Release the token */
5481 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5482 phba->sli.mbox_active = NULL;
5483 spin_unlock_irqrestore(&phba->hbalock, iflags);
5484
5485 return MBX_NOT_FINISHED;
5486}
5487
5488/**
5489 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5490 * @phba: Pointer to HBA context object.
5491 * @pmbox: Pointer to mailbox object.
5492 * @flag: Flag indicating how the mailbox need to be processed.
5493 *
5494 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5495 * the API jump table function pointer from the lpfc_hba struct.
5496 *
5497 * Return codes the caller owns the mailbox command after the return of the
5498 * function.
5499 **/
5500int
5501lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5502{
5503 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5504}
5505
5506/**
5507 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5508 * @phba: The hba struct for which this call is being executed.
5509 * @dev_grp: The HBA PCI-Device group number.
5510 *
5511 * This routine sets up the mbox interface API function jump table in @phba
5512 * struct.
5513 * Returns: 0 - success, -ENODEV - failure.
5514 **/
5515int
5516lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5517{
5518
5519 switch (dev_grp) {
5520 case LPFC_PCI_DEV_LP:
5521 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5522 phba->lpfc_sli_handle_slow_ring_event =
5523 lpfc_sli_handle_slow_ring_event_s3;
5524 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5525 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5526 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5527 break;
5528 case LPFC_PCI_DEV_OC:
5529 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5530 phba->lpfc_sli_handle_slow_ring_event =
5531 lpfc_sli_handle_slow_ring_event_s4;
5532 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5533 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5534 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5535 break;
5536 default:
5537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5538 "1420 Invalid HBA PCI-device group: 0x%x\n",
5539 dev_grp);
5540 return -ENODEV;
5541 break;
5542 }
5543 return 0;
5544}
5545
5546/**
James Smart3621a712009-04-06 18:47:14 -04005547 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005548 * @phba: Pointer to HBA context object.
5549 * @pring: Pointer to driver SLI ring object.
5550 * @piocb: Pointer to address of newly added command iocb.
5551 *
5552 * This function is called with hbalock held to add a command
5553 * iocb to the txq when SLI layer cannot submit the command iocb
5554 * to the ring.
5555 **/
James Smart858c9f62007-06-17 19:56:39 -05005556static void
James Smart92d7f7b2007-06-17 19:56:38 -05005557__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005558 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005559{
5560 /* Insert the caller's iocb in the txq tail for later processing. */
5561 list_add_tail(&piocb->list, &pring->txq);
5562 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005563}
5564
James Smarte59058c2008-08-24 21:49:00 -04005565/**
James Smart3621a712009-04-06 18:47:14 -04005566 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005567 * @phba: Pointer to HBA context object.
5568 * @pring: Pointer to driver SLI ring object.
5569 * @piocb: Pointer to address of newly added command iocb.
5570 *
5571 * This function is called with hbalock held before a new
5572 * iocb is submitted to the firmware. This function checks
5573 * txq to flush the iocbs in txq to Firmware before
5574 * submitting new iocbs to the Firmware.
5575 * If there are iocbs in the txq which need to be submitted
5576 * to firmware, lpfc_sli_next_iocb returns the first element
5577 * of the txq after dequeuing it from txq.
5578 * If there is no iocb in the txq then the function will return
5579 * *piocb and *piocb is set to NULL. Caller needs to check
5580 * *piocb to find if there are more commands in the txq.
5581 **/
dea31012005-04-17 16:05:31 -05005582static struct lpfc_iocbq *
5583lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005584 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005585{
5586 struct lpfc_iocbq * nextiocb;
5587
5588 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5589 if (!nextiocb) {
5590 nextiocb = *piocb;
5591 *piocb = NULL;
5592 }
5593
5594 return nextiocb;
5595}
5596
James Smarte59058c2008-08-24 21:49:00 -04005597/**
James Smart3772a992009-05-22 14:50:54 -04005598 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005599 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005600 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005601 * @piocb: Pointer to command iocb.
5602 * @flag: Flag indicating if this command can be put into txq.
5603 *
James Smart3772a992009-05-22 14:50:54 -04005604 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5605 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5606 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5607 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5608 * this function allows only iocbs for posting buffers. This function finds
5609 * next available slot in the command ring and posts the command to the
5610 * available slot and writes the port attention register to request HBA start
5611 * processing new iocb. If there is no slot available in the ring and
5612 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5613 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005614 *
James Smart3772a992009-05-22 14:50:54 -04005615 * This function is called with hbalock held. The function will return success
5616 * after it successfully submit the iocb to firmware or after adding to the
5617 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005618 **/
James Smart98c9ea52007-10-27 13:37:33 -04005619static int
James Smart3772a992009-05-22 14:50:54 -04005620__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005621 struct lpfc_iocbq *piocb, uint32_t flag)
5622{
5623 struct lpfc_iocbq *nextiocb;
5624 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005625 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005626
James Smart92d7f7b2007-06-17 19:56:38 -05005627 if (piocb->iocb_cmpl && (!piocb->vport) &&
5628 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5629 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5630 lpfc_printf_log(phba, KERN_ERR,
5631 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005632 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005633 piocb->iocb.ulpCommand);
5634 dump_stack();
5635 return IOCB_ERROR;
5636 }
5637
5638
Linas Vepstas8d63f372007-02-14 14:28:36 -06005639 /* If the PCI channel is in offline state, do not post iocbs. */
5640 if (unlikely(pci_channel_offline(phba->pcidev)))
5641 return IOCB_ERROR;
5642
James Smarta257bf92009-04-06 18:48:10 -04005643 /* If HBA has a deferred error attention, fail the iocb. */
5644 if (unlikely(phba->hba_flag & DEFER_ERATT))
5645 return IOCB_ERROR;
5646
dea31012005-04-17 16:05:31 -05005647 /*
5648 * We should never get an IOCB if we are in a < LINK_DOWN state
5649 */
James Smart2e0fef82007-06-17 19:56:36 -05005650 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005651 return IOCB_ERROR;
5652
5653 /*
5654 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005655 * outstanding event.
dea31012005-04-17 16:05:31 -05005656 */
James Smart0b727fe2007-10-27 13:37:25 -04005657 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005658 goto iocb_busy;
5659
James Smart2e0fef82007-06-17 19:56:36 -05005660 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005661 /*
James Smart2680eea2007-04-25 09:52:55 -04005662 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005663 * can be issued if the link is not up.
5664 */
5665 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005666 case CMD_GEN_REQUEST64_CR:
5667 case CMD_GEN_REQUEST64_CX:
5668 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5669 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005670 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005671 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5672 MENLO_TRANSPORT_TYPE))
5673
5674 goto iocb_busy;
5675 break;
dea31012005-04-17 16:05:31 -05005676 case CMD_QUE_RING_BUF_CN:
5677 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005678 /*
5679 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5680 * completion, iocb_cmpl MUST be 0.
5681 */
5682 if (piocb->iocb_cmpl)
5683 piocb->iocb_cmpl = NULL;
5684 /*FALLTHROUGH*/
5685 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005686 case CMD_CLOSE_XRI_CN:
5687 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005688 break;
5689 default:
5690 goto iocb_busy;
5691 }
5692
5693 /*
5694 * For FCP commands, we must be in a state where we can process link
5695 * attention events.
5696 */
5697 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005698 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005699 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005700 }
dea31012005-04-17 16:05:31 -05005701
dea31012005-04-17 16:05:31 -05005702 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5703 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5704 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5705
5706 if (iocb)
5707 lpfc_sli_update_ring(phba, pring);
5708 else
5709 lpfc_sli_update_full_ring(phba, pring);
5710
5711 if (!piocb)
5712 return IOCB_SUCCESS;
5713
5714 goto out_busy;
5715
5716 iocb_busy:
5717 pring->stats.iocb_cmd_delay++;
5718
5719 out_busy:
5720
5721 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005722 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005723 return IOCB_SUCCESS;
5724 }
5725
5726 return IOCB_BUSY;
5727}
5728
James Smart3772a992009-05-22 14:50:54 -04005729/**
James Smart4f774512009-05-22 14:52:35 -04005730 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5731 * @phba: Pointer to HBA context object.
5732 * @piocb: Pointer to command iocb.
5733 * @sglq: Pointer to the scatter gather queue object.
5734 *
5735 * This routine converts the bpl or bde that is in the IOCB
5736 * to a sgl list for the sli4 hardware. The physical address
5737 * of the bpl/bde is converted back to a virtual address.
5738 * If the IOCB contains a BPL then the list of BDE's is
5739 * converted to sli4_sge's. If the IOCB contains a single
5740 * BDE then it is converted to a single sli_sge.
5741 * The IOCB is still in cpu endianess so the contents of
5742 * the bpl can be used without byte swapping.
5743 *
5744 * Returns valid XRI = Success, NO_XRI = Failure.
5745**/
5746static uint16_t
5747lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5748 struct lpfc_sglq *sglq)
5749{
5750 uint16_t xritag = NO_XRI;
5751 struct ulp_bde64 *bpl = NULL;
5752 struct ulp_bde64 bde;
5753 struct sli4_sge *sgl = NULL;
5754 IOCB_t *icmd;
5755 int numBdes = 0;
5756 int i = 0;
5757
5758 if (!piocbq || !sglq)
5759 return xritag;
5760
5761 sgl = (struct sli4_sge *)sglq->sgl;
5762 icmd = &piocbq->iocb;
5763 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5764 numBdes = icmd->un.genreq64.bdl.bdeSize /
5765 sizeof(struct ulp_bde64);
5766 /* The addrHigh and addrLow fields within the IOCB
5767 * have not been byteswapped yet so there is no
5768 * need to swap them back.
5769 */
5770 bpl = (struct ulp_bde64 *)
5771 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5772
5773 if (!bpl)
5774 return xritag;
5775
5776 for (i = 0; i < numBdes; i++) {
5777 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005778 sgl->addr_hi = bpl->addrHigh;
5779 sgl->addr_lo = bpl->addrLow;
5780
James Smart4f774512009-05-22 14:52:35 -04005781 if ((i+1) == numBdes)
5782 bf_set(lpfc_sli4_sge_last, sgl, 1);
5783 else
5784 bf_set(lpfc_sli4_sge_last, sgl, 0);
5785 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005786 /* swap the size field back to the cpu so we
5787 * can assign it to the sgl.
5788 */
5789 bde.tus.w = le32_to_cpu(bpl->tus.w);
5790 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005791 bpl++;
5792 sgl++;
5793 }
5794 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5795 /* The addrHigh and addrLow fields of the BDE have not
5796 * been byteswapped yet so they need to be swapped
5797 * before putting them in the sgl.
5798 */
5799 sgl->addr_hi =
5800 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5801 sgl->addr_lo =
5802 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005803 bf_set(lpfc_sli4_sge_last, sgl, 1);
5804 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005805 sgl->sge_len =
5806 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005807 }
5808 return sglq->sli4_xritag;
5809}
5810
5811/**
5812 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5813 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005814 *
5815 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005816 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5817 * held.
James Smart4f774512009-05-22 14:52:35 -04005818 *
5819 * Return: index into SLI4 fast-path FCP queue index.
5820 **/
5821static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005822lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005823{
James Smart8fa38512009-07-19 10:01:03 -04005824 ++phba->fcp_qidx;
5825 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5826 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005827
James Smart8fa38512009-07-19 10:01:03 -04005828 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005829}
5830
5831/**
5832 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5833 * @phba: Pointer to HBA context object.
5834 * @piocb: Pointer to command iocb.
5835 * @wqe: Pointer to the work queue entry.
5836 *
5837 * This routine converts the iocb command to its Work Queue Entry
5838 * equivalent. The wqe pointer should not have any fields set when
5839 * this routine is called because it will memcpy over them.
5840 * This routine does not set the CQ_ID or the WQEC bits in the
5841 * wqe.
5842 *
5843 * Returns: 0 = Success, IOCB_ERROR = Failure.
5844 **/
5845static int
5846lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5847 union lpfc_wqe *wqe)
5848{
James Smart5ffc2662009-11-18 15:39:44 -05005849 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005850 uint8_t ct = 0;
5851 uint32_t fip;
5852 uint32_t abort_tag;
5853 uint8_t command_type = ELS_COMMAND_NON_FIP;
5854 uint8_t cmnd;
5855 uint16_t xritag;
5856 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005857 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005858 int numBdes, i;
5859 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005860
James Smart45ed1192009-10-02 15:17:02 -04005861 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005862 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005863 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005864 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005865 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005866 command_type = ELS_COMMAND_FIP;
5867 else
5868 command_type = ELS_COMMAND_NON_FIP;
5869
James Smart4f774512009-05-22 14:52:35 -04005870 /* Some of the fields are in the right position already */
5871 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5872 abort_tag = (uint32_t) iocbq->iotag;
5873 xritag = iocbq->sli4_xritag;
5874 wqe->words[7] = 0; /* The ct field has moved so reset */
5875 /* words0-2 bpl convert bde */
5876 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005877 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5878 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005879 bpl = (struct ulp_bde64 *)
5880 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5881 if (!bpl)
5882 return IOCB_ERROR;
5883
5884 /* Should already be byte swapped. */
5885 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5886 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5887 /* swap the size field back to the cpu so we
5888 * can assign it to the sgl.
5889 */
5890 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005891 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5892 total_len = 0;
5893 for (i = 0; i < numBdes; i++) {
5894 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5895 total_len += bde.tus.f.bdeSize;
5896 }
James Smart4f774512009-05-22 14:52:35 -04005897 } else
James Smart5ffc2662009-11-18 15:39:44 -05005898 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005899
5900 iocbq->iocb.ulpIoTag = iocbq->iotag;
5901 cmnd = iocbq->iocb.ulpCommand;
5902
5903 switch (iocbq->iocb.ulpCommand) {
5904 case CMD_ELS_REQUEST64_CR:
5905 if (!iocbq->iocb.ulpLe) {
5906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5907 "2007 Only Limited Edition cmd Format"
5908 " supported 0x%x\n",
5909 iocbq->iocb.ulpCommand);
5910 return IOCB_ERROR;
5911 }
James Smart5ffc2662009-11-18 15:39:44 -05005912 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005913 /* Els_reguest64 has a TMO */
5914 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5915 iocbq->iocb.ulpTimeout);
5916 /* Need a VF for word 4 set the vf bit*/
5917 bf_set(els_req64_vf, &wqe->els_req, 0);
5918 /* And a VFID for word 12 */
5919 bf_set(els_req64_vfid, &wqe->els_req, 0);
5920 /*
5921 * Set ct field to 3, indicates that the context_tag field
5922 * contains the FCFI and remote N_Port_ID is
5923 * in word 5.
5924 */
5925
5926 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5927 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5928 iocbq->iocb.ulpContext);
5929
James Smart4f774512009-05-22 14:52:35 -04005930 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5931 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5932 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005933
5934 if (command_type == ELS_COMMAND_FIP) {
5935 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5936 >> LPFC_FIP_ELS_ID_SHIFT);
5937 }
5938 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5939
James Smart4f774512009-05-22 14:52:35 -04005940 break;
James Smart5ffc2662009-11-18 15:39:44 -05005941 case CMD_XMIT_SEQUENCE64_CX:
5942 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5943 iocbq->iocb.un.ulpWord[3]);
5944 wqe->generic.word3 = 0;
5945 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05005946 /* The entire sequence is transmitted for this IOCB */
5947 xmit_len = total_len;
5948 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005949 case CMD_XMIT_SEQUENCE64_CR:
5950 /* word3 iocb=io_tag32 wqe=payload_offset */
5951 /* payload offset used for multilpe outstanding
5952 * sequences on the same exchange
5953 */
5954 wqe->words[3] = 0;
5955 /* word4 relative_offset memcpy */
5956 /* word5 r_ctl/df_ctl memcpy */
5957 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005958 wqe->xmit_sequence.xmit_len = xmit_len;
5959 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005960 break;
5961 case CMD_XMIT_BCAST64_CN:
5962 /* word3 iocb=iotag32 wqe=payload_len */
5963 wqe->words[3] = 0; /* no definition for this in wqe */
5964 /* word4 iocb=rsvd wqe=rsvd */
5965 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5966 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5967 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5968 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5969 break;
5970 case CMD_FCP_IWRITE64_CR:
5971 command_type = FCP_COMMAND_DATA_OUT;
5972 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5973 * confusing.
5974 * word3 is payload_len: byte offset to the sgl entry for the
5975 * fcp_command.
5976 * word4 is total xfer len, same as the IOCB->ulpParameter.
5977 * word5 is initial xfer len 0 = wait for xfer-ready
5978 */
5979
5980 /* Always wait for xfer-ready before sending data */
5981 wqe->fcp_iwrite.initial_xfer_len = 0;
5982 /* word 4 (xfer length) should have been set on the memcpy */
5983
5984 /* allow write to fall through to read */
5985 case CMD_FCP_IREAD64_CR:
5986 /* FCP_CMD is always the 1st sgl entry */
5987 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05005988 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04005989
5990 /* word 4 (xfer length) should have been set on the memcpy */
5991
5992 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5993 iocbq->iocb.ulpFCP2Rcvy);
5994 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5995 /* The XC bit and the XS bit are similar. The driver never
5996 * tracked whether or not the exchange was previouslly open.
5997 * XC = Exchange create, 0 is create. 1 is already open.
5998 * XS = link cmd: 1 do not close the exchange after command.
5999 * XS = 0 close exchange when command completes.
6000 * The only time we would not set the XC bit is when the XS bit
6001 * is set and we are sending our 2nd or greater command on
6002 * this exchange.
6003 */
James Smart4f774512009-05-22 14:52:35 -04006004 /* Always open the exchange */
6005 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6006
6007 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6008 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006009 break;
6010 case CMD_FCP_ICMND64_CR:
6011 /* Always open the exchange */
6012 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6013
6014 wqe->words[4] = 0;
6015 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6016 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006017 break;
6018 case CMD_GEN_REQUEST64_CR:
6019 /* word3 command length is described as byte offset to the
6020 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6021 * sgl[0] = cmnd
6022 * sgl[1] = rsp.
6023 *
6024 */
James Smart5ffc2662009-11-18 15:39:44 -05006025 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006026 /* Word4 parameter copied in the memcpy */
6027 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6028 /* word6 context tag copied in memcpy */
6029 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6030 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6032 "2015 Invalid CT %x command 0x%x\n",
6033 ct, iocbq->iocb.ulpCommand);
6034 return IOCB_ERROR;
6035 }
6036 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6037 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6038 iocbq->iocb.ulpTimeout);
6039
6040 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6041 command_type = OTHER_COMMAND;
6042 break;
6043 case CMD_XMIT_ELS_RSP64_CX:
6044 /* words0-2 BDE memcpy */
6045 /* word3 iocb=iotag32 wqe=rsvd */
6046 wqe->words[3] = 0;
6047 /* word4 iocb=did wge=rsvd. */
6048 wqe->words[4] = 0;
6049 /* word5 iocb=rsvd wge=did */
6050 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6051 iocbq->iocb.un.elsreq64.remoteID);
6052
6053 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6054 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6055
6056 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6057 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6058 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6059 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6060 iocbq->vport->vpi + phba->vpi_base);
6061 command_type = OTHER_COMMAND;
6062 break;
6063 case CMD_CLOSE_XRI_CN:
6064 case CMD_ABORT_XRI_CN:
6065 case CMD_ABORT_XRI_CX:
6066 /* words 0-2 memcpy should be 0 rserved */
6067 /* port will send abts */
6068 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6069 /*
6070 * The link is down so the fw does not need to send abts
6071 * on the wire.
6072 */
6073 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6074 else
6075 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6076 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006077 wqe->words[5] = 0;
6078 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6079 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6080 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006081 /*
6082 * The abort handler will send us CMD_ABORT_XRI_CN or
6083 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6084 */
6085 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6086 cmnd = CMD_ABORT_XRI_CX;
6087 command_type = OTHER_COMMAND;
6088 xritag = 0;
6089 break;
James Smart6669f9b2009-10-02 15:16:45 -04006090 case CMD_XMIT_BLS_RSP64_CX:
6091 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6092 * we re-construct this WQE here based on information in
6093 * iocbq from scratch.
6094 */
6095 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006096 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006097 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006098 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6099 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6100 LPFC_ABTS_UNSOL_INT) {
6101 /* ABTS sent by initiator to CT exchange, the
6102 * RX_ID field will be filled with the newly
6103 * allocated responder XRI.
6104 */
6105 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6106 iocbq->sli4_xritag);
6107 } else {
6108 /* ABTS sent by responder to CT exchange, the
6109 * RX_ID field will be filled with the responder
6110 * RX_ID from ABTS.
6111 */
6112 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6113 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6114 }
James Smart6669f9b2009-10-02 15:16:45 -04006115 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6116 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6117 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6118 iocbq->iocb.ulpContext);
6119 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6120 command_type = OTHER_COMMAND;
6121 break;
James Smart4f774512009-05-22 14:52:35 -04006122 case CMD_XRI_ABORTED_CX:
6123 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6124 /* words0-2 are all 0's no bde */
6125 /* word3 and word4 are rsvrd */
6126 wqe->words[3] = 0;
6127 wqe->words[4] = 0;
6128 /* word5 iocb=rsvd wge=did */
6129 /* There is no remote port id in the IOCB? */
6130 /* Let this fall through and fail */
6131 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6132 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6133 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6134 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6135 default:
6136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6137 "2014 Invalid command 0x%x\n",
6138 iocbq->iocb.ulpCommand);
6139 return IOCB_ERROR;
6140 break;
6141
6142 }
6143 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6144 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6145 wqe->generic.abort_tag = abort_tag;
6146 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6147 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6148 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6149 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6150
6151 return 0;
6152}
6153
6154/**
6155 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6156 * @phba: Pointer to HBA context object.
6157 * @ring_number: SLI ring number to issue iocb on.
6158 * @piocb: Pointer to command iocb.
6159 * @flag: Flag indicating if this command can be put into txq.
6160 *
6161 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6162 * an iocb command to an HBA with SLI-4 interface spec.
6163 *
6164 * This function is called with hbalock held. The function will return success
6165 * after it successfully submit the iocb to firmware or after adding to the
6166 * txq.
6167 **/
6168static int
6169__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6170 struct lpfc_iocbq *piocb, uint32_t flag)
6171{
6172 struct lpfc_sglq *sglq;
6173 uint16_t xritag;
6174 union lpfc_wqe wqe;
6175 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006176
6177 if (piocb->sli4_xritag == NO_XRI) {
6178 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006179 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006180 sglq = NULL;
6181 else {
6182 sglq = __lpfc_sli_get_sglq(phba);
6183 if (!sglq)
6184 return IOCB_ERROR;
6185 piocb->sli4_xritag = sglq->sli4_xritag;
6186 }
6187 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6188 sglq = NULL; /* These IO's already have an XRI and
6189 * a mapped sgl.
6190 */
6191 } else {
6192 /* This is a continuation of a commandi,(CX) so this
6193 * sglq is on the active list
6194 */
6195 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6196 if (!sglq)
6197 return IOCB_ERROR;
6198 }
6199
6200 if (sglq) {
6201 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6202 if (xritag != sglq->sli4_xritag)
6203 return IOCB_ERROR;
6204 }
6205
6206 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6207 return IOCB_ERROR;
6208
James Smart341af102010-01-26 23:07:37 -05006209 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6210 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006211 /*
6212 * For FCP command IOCB, get a new WQ index to distribute
6213 * WQE across the WQsr. On the other hand, for abort IOCB,
6214 * it carries the same WQ index to the original command
6215 * IOCB.
6216 */
James Smart341af102010-01-26 23:07:37 -05006217 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006218 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6219 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6220 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006221 return IOCB_ERROR;
6222 } else {
6223 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6224 return IOCB_ERROR;
6225 }
6226 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6227
6228 return 0;
6229}
6230
6231/**
James Smart3772a992009-05-22 14:50:54 -04006232 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6233 *
6234 * This routine wraps the actual lockless version for issusing IOCB function
6235 * pointer from the lpfc_hba struct.
6236 *
6237 * Return codes:
6238 * IOCB_ERROR - Error
6239 * IOCB_SUCCESS - Success
6240 * IOCB_BUSY - Busy
6241 **/
6242static inline int
6243__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6244 struct lpfc_iocbq *piocb, uint32_t flag)
6245{
6246 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6247}
6248
6249/**
6250 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6251 * @phba: The hba struct for which this call is being executed.
6252 * @dev_grp: The HBA PCI-Device group number.
6253 *
6254 * This routine sets up the SLI interface API function jump table in @phba
6255 * struct.
6256 * Returns: 0 - success, -ENODEV - failure.
6257 **/
6258int
6259lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6260{
6261
6262 switch (dev_grp) {
6263 case LPFC_PCI_DEV_LP:
6264 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6265 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6266 break;
James Smart4f774512009-05-22 14:52:35 -04006267 case LPFC_PCI_DEV_OC:
6268 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6269 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6270 break;
James Smart3772a992009-05-22 14:50:54 -04006271 default:
6272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6273 "1419 Invalid HBA PCI-device group: 0x%x\n",
6274 dev_grp);
6275 return -ENODEV;
6276 break;
6277 }
6278 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6279 return 0;
6280}
James Smart92d7f7b2007-06-17 19:56:38 -05006281
James Smarte59058c2008-08-24 21:49:00 -04006282/**
James Smart3621a712009-04-06 18:47:14 -04006283 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006284 * @phba: Pointer to HBA context object.
6285 * @pring: Pointer to driver SLI ring object.
6286 * @piocb: Pointer to command iocb.
6287 * @flag: Flag indicating if this command can be put into txq.
6288 *
6289 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6290 * function. This function gets the hbalock and calls
6291 * __lpfc_sli_issue_iocb function and will return the error returned
6292 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6293 * functions which do not hold hbalock.
6294 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006295int
James Smart3772a992009-05-22 14:50:54 -04006296lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006297 struct lpfc_iocbq *piocb, uint32_t flag)
6298{
6299 unsigned long iflags;
6300 int rc;
6301
6302 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006303 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006304 spin_unlock_irqrestore(&phba->hbalock, iflags);
6305
6306 return rc;
6307}
6308
James Smarte59058c2008-08-24 21:49:00 -04006309/**
James Smart3621a712009-04-06 18:47:14 -04006310 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006311 * @phba: Pointer to HBA context object.
6312 *
6313 * This function is called while driver attaches with the
6314 * HBA to setup the extra ring. The extra ring is used
6315 * only when driver needs to support target mode functionality
6316 * or IP over FC functionalities.
6317 *
6318 * This function is called with no lock held.
6319 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006320static int
6321lpfc_extra_ring_setup( struct lpfc_hba *phba)
6322{
6323 struct lpfc_sli *psli;
6324 struct lpfc_sli_ring *pring;
6325
6326 psli = &phba->sli;
6327
6328 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006329
6330 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006331 pring = &psli->ring[psli->fcp_ring];
6332 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6333 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6334 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6335 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6336
James Smarta4bc3372006-12-02 13:34:16 -05006337 /* and give them to the extra ring */
6338 pring = &psli->ring[psli->extra_ring];
6339
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006340 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6341 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6342 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6343 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6344
6345 /* Setup default profile for this ring */
6346 pring->iotag_max = 4096;
6347 pring->num_mask = 1;
6348 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006349 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6350 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006351 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6352 return 0;
6353}
6354
James Smarte59058c2008-08-24 21:49:00 -04006355/**
James Smart3621a712009-04-06 18:47:14 -04006356 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006357 * @phba: Pointer to HBA context object.
6358 * @pring: Pointer to driver SLI ring object.
6359 * @iocbq: Pointer to iocb object.
6360 *
6361 * This function is called by the slow ring event handler
6362 * function when there is an ASYNC event iocb in the ring.
6363 * This function is called with no lock held.
6364 * Currently this function handles only temperature related
6365 * ASYNC events. The function decodes the temperature sensor
6366 * event message and posts events for the management applications.
6367 **/
James Smart98c9ea52007-10-27 13:37:33 -04006368static void
James Smart57127f12007-10-27 13:37:05 -04006369lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6370 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6371{
6372 IOCB_t *icmd;
6373 uint16_t evt_code;
6374 uint16_t temp;
6375 struct temp_event temp_event_data;
6376 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006377 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006378
6379 icmd = &iocbq->iocb;
6380 evt_code = icmd->un.asyncstat.evt_code;
6381 temp = icmd->ulpContext;
6382
6383 if ((evt_code != ASYNC_TEMP_WARN) &&
6384 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006385 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006386 lpfc_printf_log(phba,
6387 KERN_ERR,
6388 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006389 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006390 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006391 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6392 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6393 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6394 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006395 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006396 icmd->un.asyncstat.evt_code,
6397 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6398 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6399 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6400 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6401
James Smart57127f12007-10-27 13:37:05 -04006402 return;
6403 }
6404 temp_event_data.data = (uint32_t)temp;
6405 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6406 if (evt_code == ASYNC_TEMP_WARN) {
6407 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6408 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006409 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006410 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006411 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006412 "corrective action. temperature : %d Celsius\n",
6413 temp);
6414 }
6415 if (evt_code == ASYNC_TEMP_SAFE) {
6416 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6417 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006418 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006419 LOG_TEMP,
6420 "0340 Adapter temperature is OK now. "
6421 "temperature : %d Celsius\n",
6422 temp);
6423 }
6424
6425 /* Send temperature change event to applications */
6426 shost = lpfc_shost_from_vport(phba->pport);
6427 fc_host_post_vendor_event(shost, fc_get_event_number(),
6428 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006429 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006430
6431}
6432
6433
James Smarte59058c2008-08-24 21:49:00 -04006434/**
James Smart3621a712009-04-06 18:47:14 -04006435 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006436 * @phba: Pointer to HBA context object.
6437 *
6438 * lpfc_sli_setup sets up rings of the SLI interface with
6439 * number of iocbs per ring and iotags. This function is
6440 * called while driver attach to the HBA and before the
6441 * interrupts are enabled. So there is no need for locking.
6442 *
6443 * This function always returns 0.
6444 **/
dea31012005-04-17 16:05:31 -05006445int
6446lpfc_sli_setup(struct lpfc_hba *phba)
6447{
James Smarted957682007-06-17 19:56:37 -05006448 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006449 struct lpfc_sli *psli = &phba->sli;
6450 struct lpfc_sli_ring *pring;
6451
6452 psli->num_rings = MAX_CONFIGURED_RINGS;
6453 psli->sli_flag = 0;
6454 psli->fcp_ring = LPFC_FCP_RING;
6455 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006456 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006457
James Bottomley604a3e32005-10-29 10:28:33 -05006458 psli->iocbq_lookup = NULL;
6459 psli->iocbq_lookup_len = 0;
6460 psli->last_iotag = 0;
6461
dea31012005-04-17 16:05:31 -05006462 for (i = 0; i < psli->num_rings; i++) {
6463 pring = &psli->ring[i];
6464 switch (i) {
6465 case LPFC_FCP_RING: /* ring 0 - FCP */
6466 /* numCiocb and numRiocb are used in config_port */
6467 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6468 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6469 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6470 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6471 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6472 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006473 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006474 SLI3_IOCB_CMD_SIZE :
6475 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006476 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006477 SLI3_IOCB_RSP_SIZE :
6478 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006479 pring->iotag_ctr = 0;
6480 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006481 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006482 pring->fast_iotag = pring->iotag_max;
6483 pring->num_mask = 0;
6484 break;
James Smarta4bc3372006-12-02 13:34:16 -05006485 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006486 /* numCiocb and numRiocb are used in config_port */
6487 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6488 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006489 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006490 SLI3_IOCB_CMD_SIZE :
6491 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006492 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006493 SLI3_IOCB_RSP_SIZE :
6494 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006495 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006496 pring->num_mask = 0;
6497 break;
6498 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6499 /* numCiocb and numRiocb are used in config_port */
6500 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6501 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006502 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006503 SLI3_IOCB_CMD_SIZE :
6504 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006505 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006506 SLI3_IOCB_RSP_SIZE :
6507 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006508 pring->fast_iotag = 0;
6509 pring->iotag_ctr = 0;
6510 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006511 pring->lpfc_sli_rcv_async_status =
6512 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006513 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006514 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006515 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6516 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006517 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006518 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006519 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006520 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6521 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006522 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006523 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006524 pring->prt[2].profile = 0; /* Mask 2 */
6525 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006526 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006527 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006528 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006529 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006530 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006531 pring->prt[3].profile = 0; /* Mask 3 */
6532 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006533 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006534 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006535 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006536 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006537 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006538 /* abort unsolicited sequence */
6539 pring->prt[4].profile = 0; /* Mask 4 */
6540 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6541 pring->prt[4].type = FC_TYPE_BLS;
6542 pring->prt[4].lpfc_sli_rcv_unsol_event =
6543 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006544 break;
6545 }
James Smarted957682007-06-17 19:56:37 -05006546 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006547 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006548 }
James Smarted957682007-06-17 19:56:37 -05006549 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006550 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006551 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6552 "SLI2 SLIM Data: x%x x%lx\n",
6553 phba->brd_no, totiocbsize,
6554 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006555 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006556 if (phba->cfg_multi_ring_support == 2)
6557 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006558
6559 return 0;
6560}
6561
James Smarte59058c2008-08-24 21:49:00 -04006562/**
James Smart3621a712009-04-06 18:47:14 -04006563 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006564 * @phba: Pointer to HBA context object.
6565 *
6566 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6567 * ring. This function also initializes ring indices of each ring.
6568 * This function is called during the initialization of the SLI
6569 * interface of an HBA.
6570 * This function is called with no lock held and always returns
6571 * 1.
6572 **/
dea31012005-04-17 16:05:31 -05006573int
James Smart2e0fef82007-06-17 19:56:36 -05006574lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006575{
6576 struct lpfc_sli *psli;
6577 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006578 int i;
dea31012005-04-17 16:05:31 -05006579
6580 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006581 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006582 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006583 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006584 /* Initialize list headers for txq and txcmplq as double linked lists */
6585 for (i = 0; i < psli->num_rings; i++) {
6586 pring = &psli->ring[i];
6587 pring->ringno = i;
6588 pring->next_cmdidx = 0;
6589 pring->local_getidx = 0;
6590 pring->cmdidx = 0;
6591 INIT_LIST_HEAD(&pring->txq);
6592 INIT_LIST_HEAD(&pring->txcmplq);
6593 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006594 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006595 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006596 }
James Smart2e0fef82007-06-17 19:56:36 -05006597 spin_unlock_irq(&phba->hbalock);
6598 return 1;
dea31012005-04-17 16:05:31 -05006599}
6600
James Smarte59058c2008-08-24 21:49:00 -04006601/**
James Smart04c68492009-05-22 14:52:52 -04006602 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6603 * @phba: Pointer to HBA context object.
6604 *
6605 * This routine flushes the mailbox command subsystem. It will unconditionally
6606 * flush all the mailbox commands in the three possible stages in the mailbox
6607 * command sub-system: pending mailbox command queue; the outstanding mailbox
6608 * command; and completed mailbox command queue. It is caller's responsibility
6609 * to make sure that the driver is in the proper state to flush the mailbox
6610 * command sub-system. Namely, the posting of mailbox commands into the
6611 * pending mailbox command queue from the various clients must be stopped;
6612 * either the HBA is in a state that it will never works on the outstanding
6613 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6614 * mailbox command has been completed.
6615 **/
6616static void
6617lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6618{
6619 LIST_HEAD(completions);
6620 struct lpfc_sli *psli = &phba->sli;
6621 LPFC_MBOXQ_t *pmb;
6622 unsigned long iflag;
6623
6624 /* Flush all the mailbox commands in the mbox system */
6625 spin_lock_irqsave(&phba->hbalock, iflag);
6626 /* The pending mailbox command queue */
6627 list_splice_init(&phba->sli.mboxq, &completions);
6628 /* The outstanding active mailbox command */
6629 if (psli->mbox_active) {
6630 list_add_tail(&psli->mbox_active->list, &completions);
6631 psli->mbox_active = NULL;
6632 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6633 }
6634 /* The completed mailbox command queue */
6635 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6636 spin_unlock_irqrestore(&phba->hbalock, iflag);
6637
6638 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6639 while (!list_empty(&completions)) {
6640 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6641 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6642 if (pmb->mbox_cmpl)
6643 pmb->mbox_cmpl(phba, pmb);
6644 }
6645}
6646
6647/**
James Smart3621a712009-04-06 18:47:14 -04006648 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006649 * @vport: Pointer to virtual port object.
6650 *
6651 * lpfc_sli_host_down is called to clean up the resources
6652 * associated with a vport before destroying virtual
6653 * port data structures.
6654 * This function does following operations:
6655 * - Free discovery resources associated with this virtual
6656 * port.
6657 * - Free iocbs associated with this virtual port in
6658 * the txq.
6659 * - Send abort for all iocb commands associated with this
6660 * vport in txcmplq.
6661 *
6662 * This function is called with no lock held and always returns 1.
6663 **/
dea31012005-04-17 16:05:31 -05006664int
James Smart92d7f7b2007-06-17 19:56:38 -05006665lpfc_sli_host_down(struct lpfc_vport *vport)
6666{
James Smart858c9f62007-06-17 19:56:39 -05006667 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006668 struct lpfc_hba *phba = vport->phba;
6669 struct lpfc_sli *psli = &phba->sli;
6670 struct lpfc_sli_ring *pring;
6671 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006672 int i;
6673 unsigned long flags = 0;
6674 uint16_t prev_pring_flag;
6675
6676 lpfc_cleanup_discovery_resources(vport);
6677
6678 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006679 for (i = 0; i < psli->num_rings; i++) {
6680 pring = &psli->ring[i];
6681 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006682 /* Only slow rings */
6683 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006684 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006685 /* Set the lpfc data pending flag */
6686 set_bit(LPFC_DATA_READY, &phba->data_flags);
6687 }
James Smart92d7f7b2007-06-17 19:56:38 -05006688 /*
6689 * Error everything on the txq since these iocbs have not been
6690 * given to the FW yet.
6691 */
James Smart92d7f7b2007-06-17 19:56:38 -05006692 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6693 if (iocb->vport != vport)
6694 continue;
James Smart858c9f62007-06-17 19:56:39 -05006695 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006696 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006697 }
6698
6699 /* Next issue ABTS for everything on the txcmplq */
6700 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6701 list) {
6702 if (iocb->vport != vport)
6703 continue;
6704 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6705 }
6706
6707 pring->flag = prev_pring_flag;
6708 }
6709
6710 spin_unlock_irqrestore(&phba->hbalock, flags);
6711
James Smarta257bf92009-04-06 18:48:10 -04006712 /* Cancel all the IOCBs from the completions list */
6713 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6714 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006715 return 1;
6716}
6717
James Smarte59058c2008-08-24 21:49:00 -04006718/**
James Smart3621a712009-04-06 18:47:14 -04006719 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006720 * @phba: Pointer to HBA context object.
6721 *
6722 * This function cleans up all iocb, buffers, mailbox commands
6723 * while shutting down the HBA. This function is called with no
6724 * lock held and always returns 1.
6725 * This function does the following to cleanup driver resources:
6726 * - Free discovery resources for each virtual port
6727 * - Cleanup any pending fabric iocbs
6728 * - Iterate through the iocb txq and free each entry
6729 * in the list.
6730 * - Free up any buffer posted to the HBA
6731 * - Free mailbox commands in the mailbox queue.
6732 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006733int
James Smart2e0fef82007-06-17 19:56:36 -05006734lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006735{
James Smart2534ba72007-04-25 09:52:20 -04006736 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006737 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006738 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006739 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006740 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006741 int i;
6742
6743 /* Shutdown the mailbox command sub-system */
6744 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006745
dea31012005-04-17 16:05:31 -05006746 lpfc_hba_down_prep(phba);
6747
James Smart92d7f7b2007-06-17 19:56:38 -05006748 lpfc_fabric_abort_hba(phba);
6749
James Smart2e0fef82007-06-17 19:56:36 -05006750 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006751 for (i = 0; i < psli->num_rings; i++) {
6752 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006753 /* Only slow rings */
6754 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006755 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006756 /* Set the lpfc data pending flag */
6757 set_bit(LPFC_DATA_READY, &phba->data_flags);
6758 }
dea31012005-04-17 16:05:31 -05006759
6760 /*
6761 * Error everything on the txq since these iocbs have not been
6762 * given to the FW yet.
6763 */
James Smart2534ba72007-04-25 09:52:20 -04006764 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006765 pring->txq_cnt = 0;
6766
dea31012005-04-17 16:05:31 -05006767 }
James Smart2e0fef82007-06-17 19:56:36 -05006768 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006769
James Smarta257bf92009-04-06 18:48:10 -04006770 /* Cancel all the IOCBs from the completions list */
6771 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6772 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006773
James Smart0ff10d42008-01-11 01:52:36 -05006774 spin_lock_irqsave(&phba->hbalock, flags);
6775 list_splice_init(&phba->elsbuf, &completions);
6776 phba->elsbuf_cnt = 0;
6777 phba->elsbuf_prev_cnt = 0;
6778 spin_unlock_irqrestore(&phba->hbalock, flags);
6779
6780 while (!list_empty(&completions)) {
6781 list_remove_head(&completions, buf_ptr,
6782 struct lpfc_dmabuf, list);
6783 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6784 kfree(buf_ptr);
6785 }
6786
dea31012005-04-17 16:05:31 -05006787 /* Return any active mbox cmds */
6788 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006789
James Smartda0436e2009-05-22 14:51:39 -04006790 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006791 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006792 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006793
James Smartda0436e2009-05-22 14:51:39 -04006794 return 1;
6795}
James Smart92d7f7b2007-06-17 19:56:38 -05006796
James Smartda0436e2009-05-22 14:51:39 -04006797/**
6798 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6799 * @phba: Pointer to HBA context object.
6800 *
6801 * This function cleans up all queues, iocb, buffers, mailbox commands while
6802 * shutting down the SLI4 HBA FCoE function. This function is called with no
6803 * lock held and always returns 1.
6804 *
6805 * This function does the following to cleanup driver FCoE function resources:
6806 * - Free discovery resources for each virtual port
6807 * - Cleanup any pending fabric iocbs
6808 * - Iterate through the iocb txq and free each entry in the list.
6809 * - Free up any buffer posted to the HBA.
6810 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6811 * - Free mailbox commands in the mailbox queue.
6812 **/
6813int
6814lpfc_sli4_hba_down(struct lpfc_hba *phba)
6815{
6816 /* Stop the SLI4 device port */
6817 lpfc_stop_port(phba);
6818
6819 /* Tear down the queues in the HBA */
6820 lpfc_sli4_queue_unset(phba);
6821
6822 /* unregister default FCFI from the HBA */
6823 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6824
dea31012005-04-17 16:05:31 -05006825 return 1;
6826}
6827
James Smarte59058c2008-08-24 21:49:00 -04006828/**
James Smart3621a712009-04-06 18:47:14 -04006829 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006830 * @srcp: Source memory pointer.
6831 * @destp: Destination memory pointer.
6832 * @cnt: Number of words required to be copied.
6833 *
6834 * This function is used for copying data between driver memory
6835 * and the SLI memory. This function also changes the endianness
6836 * of each word if native endianness is different from SLI
6837 * endianness. This function can be called with or without
6838 * lock.
6839 **/
dea31012005-04-17 16:05:31 -05006840void
6841lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6842{
6843 uint32_t *src = srcp;
6844 uint32_t *dest = destp;
6845 uint32_t ldata;
6846 int i;
6847
6848 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6849 ldata = *src;
6850 ldata = le32_to_cpu(ldata);
6851 *dest = ldata;
6852 src++;
6853 dest++;
6854 }
6855}
6856
James Smarte59058c2008-08-24 21:49:00 -04006857
6858/**
James Smarta0c87cb2009-07-19 10:01:10 -04006859 * lpfc_sli_bemem_bcopy - SLI memory copy function
6860 * @srcp: Source memory pointer.
6861 * @destp: Destination memory pointer.
6862 * @cnt: Number of words required to be copied.
6863 *
6864 * This function is used for copying data between a data structure
6865 * with big endian representation to local endianness.
6866 * This function can be called with or without lock.
6867 **/
6868void
6869lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6870{
6871 uint32_t *src = srcp;
6872 uint32_t *dest = destp;
6873 uint32_t ldata;
6874 int i;
6875
6876 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6877 ldata = *src;
6878 ldata = be32_to_cpu(ldata);
6879 *dest = ldata;
6880 src++;
6881 dest++;
6882 }
6883}
6884
6885/**
James Smart3621a712009-04-06 18:47:14 -04006886 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006887 * @phba: Pointer to HBA context object.
6888 * @pring: Pointer to driver SLI ring object.
6889 * @mp: Pointer to driver buffer object.
6890 *
6891 * This function is called with no lock held.
6892 * It always return zero after adding the buffer to the postbufq
6893 * buffer list.
6894 **/
dea31012005-04-17 16:05:31 -05006895int
James Smart2e0fef82007-06-17 19:56:36 -05006896lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6897 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006898{
6899 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6900 later */
James Smart2e0fef82007-06-17 19:56:36 -05006901 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006902 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006903 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006904 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006905 return 0;
6906}
6907
James Smarte59058c2008-08-24 21:49:00 -04006908/**
James Smart3621a712009-04-06 18:47:14 -04006909 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006910 * @phba: Pointer to HBA context object.
6911 *
6912 * When HBQ is enabled, buffers are searched based on tags. This function
6913 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6914 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6915 * does not conflict with tags of buffer posted for unsolicited events.
6916 * The function returns the allocated tag. The function is called with
6917 * no locks held.
6918 **/
James Smart76bb24e2007-10-27 13:38:00 -04006919uint32_t
6920lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6921{
6922 spin_lock_irq(&phba->hbalock);
6923 phba->buffer_tag_count++;
6924 /*
6925 * Always set the QUE_BUFTAG_BIT to distiguish between
6926 * a tag assigned by HBQ.
6927 */
6928 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6929 spin_unlock_irq(&phba->hbalock);
6930 return phba->buffer_tag_count;
6931}
6932
James Smarte59058c2008-08-24 21:49:00 -04006933/**
James Smart3621a712009-04-06 18:47:14 -04006934 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006935 * @phba: Pointer to HBA context object.
6936 * @pring: Pointer to driver SLI ring object.
6937 * @tag: Buffer tag.
6938 *
6939 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6940 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6941 * iocb is posted to the response ring with the tag of the buffer.
6942 * This function searches the pring->postbufq list using the tag
6943 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6944 * iocb. If the buffer is found then lpfc_dmabuf object of the
6945 * buffer is returned to the caller else NULL is returned.
6946 * This function is called with no lock held.
6947 **/
James Smart76bb24e2007-10-27 13:38:00 -04006948struct lpfc_dmabuf *
6949lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6950 uint32_t tag)
6951{
6952 struct lpfc_dmabuf *mp, *next_mp;
6953 struct list_head *slp = &pring->postbufq;
6954
6955 /* Search postbufq, from the begining, looking for a match on tag */
6956 spin_lock_irq(&phba->hbalock);
6957 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6958 if (mp->buffer_tag == tag) {
6959 list_del_init(&mp->list);
6960 pring->postbufq_cnt--;
6961 spin_unlock_irq(&phba->hbalock);
6962 return mp;
6963 }
6964 }
6965
6966 spin_unlock_irq(&phba->hbalock);
6967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006968 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006969 "ring %d Data x%lx x%p x%p x%x\n",
6970 pring->ringno, (unsigned long) tag,
6971 slp->next, slp->prev, pring->postbufq_cnt);
6972
6973 return NULL;
6974}
dea31012005-04-17 16:05:31 -05006975
James Smarte59058c2008-08-24 21:49:00 -04006976/**
James Smart3621a712009-04-06 18:47:14 -04006977 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006978 * @phba: Pointer to HBA context object.
6979 * @pring: Pointer to driver SLI ring object.
6980 * @phys: DMA address of the buffer.
6981 *
6982 * This function searches the buffer list using the dma_address
6983 * of unsolicited event to find the driver's lpfc_dmabuf object
6984 * corresponding to the dma_address. The function returns the
6985 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6986 * This function is called by the ct and els unsolicited event
6987 * handlers to get the buffer associated with the unsolicited
6988 * event.
6989 *
6990 * This function is called with no lock held.
6991 **/
dea31012005-04-17 16:05:31 -05006992struct lpfc_dmabuf *
6993lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6994 dma_addr_t phys)
6995{
6996 struct lpfc_dmabuf *mp, *next_mp;
6997 struct list_head *slp = &pring->postbufq;
6998
6999 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007000 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007001 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7002 if (mp->phys == phys) {
7003 list_del_init(&mp->list);
7004 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007005 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007006 return mp;
7007 }
7008 }
7009
James Smart2e0fef82007-06-17 19:56:36 -05007010 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007012 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007013 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007014 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007015 slp->next, slp->prev, pring->postbufq_cnt);
7016 return NULL;
7017}
7018
James Smarte59058c2008-08-24 21:49:00 -04007019/**
James Smart3621a712009-04-06 18:47:14 -04007020 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007021 * @phba: Pointer to HBA context object.
7022 * @cmdiocb: Pointer to driver command iocb object.
7023 * @rspiocb: Pointer to driver response iocb object.
7024 *
7025 * This function is the completion handler for the abort iocbs for
7026 * ELS commands. This function is called from the ELS ring event
7027 * handler with no lock held. This function frees memory resources
7028 * associated with the abort iocb.
7029 **/
dea31012005-04-17 16:05:31 -05007030static void
James Smart2e0fef82007-06-17 19:56:36 -05007031lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7032 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007033{
James Smart2e0fef82007-06-17 19:56:36 -05007034 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007035 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007036 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007037 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7038
7039 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007040
7041 if (irsp->ulpStatus) {
7042 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7043 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7044
James Smart2e0fef82007-06-17 19:56:36 -05007045 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007046 if (phba->sli_rev < LPFC_SLI_REV4) {
7047 if (abort_iotag != 0 &&
7048 abort_iotag <= phba->sli.last_iotag)
7049 abort_iocb =
7050 phba->sli.iocbq_lookup[abort_iotag];
7051 } else
7052 /* For sli4 the abort_tag is the XRI,
7053 * so the abort routine puts the iotag of the iocb
7054 * being aborted in the context field of the abort
7055 * IOCB.
7056 */
7057 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007058
James Smart92d7f7b2007-06-17 19:56:38 -05007059 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007060 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007061 "with tag %x context %x, abort status %x, "
7062 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007063 abort_iocb, abort_iotag, abort_context,
7064 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007065
7066 /*
James Smart58da1ff2008-04-07 10:15:56 -04007067 * If the iocb is not found in Firmware queue the iocb
7068 * might have completed already. Do not free it again.
7069 */
James Smart9b379602008-04-07 10:16:00 -04007070 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007071 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7072 spin_unlock_irq(&phba->hbalock);
7073 lpfc_sli_release_iocbq(phba, cmdiocb);
7074 return;
7075 }
7076 /* For SLI4 the ulpContext field for abort IOCB
7077 * holds the iotag of the IOCB being aborted so
7078 * the local abort_context needs to be reset to
7079 * match the aborted IOCBs ulpContext.
7080 */
7081 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7082 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007083 }
7084 /*
James Smart2680eea2007-04-25 09:52:55 -04007085 * make sure we have the right iocbq before taking it
7086 * off the txcmplq and try to call completion routine.
7087 */
James Smart2e0fef82007-06-17 19:56:36 -05007088 if (!abort_iocb ||
7089 abort_iocb->iocb.ulpContext != abort_context ||
7090 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7091 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007092 else if (phba->sli_rev < LPFC_SLI_REV4) {
7093 /*
7094 * leave the SLI4 aborted command on the txcmplq
7095 * list and the command complete WCQE's XB bit
7096 * will tell whether the SGL (XRI) can be released
7097 * immediately or to the aborted SGL list for the
7098 * following abort XRI from the HBA.
7099 */
James Smart92d7f7b2007-06-17 19:56:38 -05007100 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007101 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007102 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007103
James Smart0ff10d42008-01-11 01:52:36 -05007104 /* Firmware could still be in progress of DMAing
7105 * payload, so don't free data buffer till after
7106 * a hbeat.
7107 */
James Smart341af102010-01-26 23:07:37 -05007108 spin_lock_irq(&phba->hbalock);
James Smart0ff10d42008-01-11 01:52:36 -05007109 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007110 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007111 spin_unlock_irq(&phba->hbalock);
7112
James Smart92d7f7b2007-06-17 19:56:38 -05007113 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007114 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007115 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007116 }
7117 }
7118
James Bottomley604a3e32005-10-29 10:28:33 -05007119 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007120 return;
7121}
7122
James Smarte59058c2008-08-24 21:49:00 -04007123/**
James Smart3621a712009-04-06 18:47:14 -04007124 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007125 * @phba: Pointer to HBA context object.
7126 * @cmdiocb: Pointer to driver command iocb object.
7127 * @rspiocb: Pointer to driver response iocb object.
7128 *
7129 * The function is called from SLI ring event handler with no
7130 * lock held. This function is the completion handler for ELS commands
7131 * which are aborted. The function frees memory resources used for
7132 * the aborted ELS commands.
7133 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007134static void
7135lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7136 struct lpfc_iocbq *rspiocb)
7137{
7138 IOCB_t *irsp = &rspiocb->iocb;
7139
7140 /* ELS cmd tag <ulpIoTag> completes */
7141 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007142 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007143 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007144 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007145 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007146 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7147 lpfc_ct_free_iocb(phba, cmdiocb);
7148 else
7149 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007150 return;
7151}
7152
James Smarte59058c2008-08-24 21:49:00 -04007153/**
James Smart3621a712009-04-06 18:47:14 -04007154 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007155 * @phba: Pointer to HBA context object.
7156 * @pring: Pointer to driver SLI ring object.
7157 * @cmdiocb: Pointer to driver command iocb object.
7158 *
7159 * This function issues an abort iocb for the provided command
7160 * iocb. This function is called with hbalock held.
7161 * The function returns 0 when it fails due to memory allocation
7162 * failure or when the command iocb is an abort request.
7163 **/
dea31012005-04-17 16:05:31 -05007164int
James Smart2e0fef82007-06-17 19:56:36 -05007165lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7166 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007167{
James Smart2e0fef82007-06-17 19:56:36 -05007168 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007169 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007170 IOCB_t *icmd = NULL;
7171 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007172 int retval = IOCB_ERROR;
7173
James Smart92d7f7b2007-06-17 19:56:38 -05007174 /*
7175 * There are certain command types we don't want to abort. And we
7176 * don't want to abort commands that are already in the process of
7177 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007178 */
7179 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007180 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007181 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7182 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007183 return 0;
7184
James Smart858c9f62007-06-17 19:56:39 -05007185 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7186 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007187 */
James Smart858c9f62007-06-17 19:56:39 -05007188 if ((vport->load_flag & FC_UNLOADING) &&
7189 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007190 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7191 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7192 else
7193 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007194 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007195 }
dea31012005-04-17 16:05:31 -05007196
7197 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007198 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007199 if (abtsiocbp == NULL)
7200 return 0;
dea31012005-04-17 16:05:31 -05007201
James Smart07951072007-04-25 09:51:38 -04007202 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007203 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007204 */
7205 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7206
dea31012005-04-17 16:05:31 -05007207 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007208 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7209 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007210 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007211 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007212 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7213 }
James Smartda0436e2009-05-22 14:51:39 -04007214 else
7215 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007216 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007217 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007218
James Smart5ffc2662009-11-18 15:39:44 -05007219 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7220 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007221 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7222 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007223
James Smart2e0fef82007-06-17 19:56:36 -05007224 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007225 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7226 else
7227 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7228
7229 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007230
James Smarte8b62012007-08-02 11:10:09 -04007231 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7232 "0339 Abort xri x%x, original iotag x%x, "
7233 "abort cmd iotag x%x\n",
7234 iabt->un.acxri.abortContextTag,
7235 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007236 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007237
James Smartd7c255b2008-08-24 21:50:00 -04007238 if (retval)
7239 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007240abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007241 /*
7242 * Caller to this routine should check for IOCB_ERROR
7243 * and handle it properly. This routine no longer removes
7244 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007245 */
James Smart2e0fef82007-06-17 19:56:36 -05007246 return retval;
dea31012005-04-17 16:05:31 -05007247}
7248
James Smarte59058c2008-08-24 21:49:00 -04007249/**
James Smart3621a712009-04-06 18:47:14 -04007250 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007251 * @iocbq: Pointer to driver iocb object.
7252 * @vport: Pointer to driver virtual port object.
7253 * @tgt_id: SCSI ID of the target.
7254 * @lun_id: LUN ID of the scsi device.
7255 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7256 *
James Smart3621a712009-04-06 18:47:14 -04007257 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007258 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7259 * 0 if the filtering criteria is met for the given iocb and will return
7260 * 1 if the filtering criteria is not met.
7261 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7262 * given iocb is for the SCSI device specified by vport, tgt_id and
7263 * lun_id parameter.
7264 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7265 * given iocb is for the SCSI target specified by vport and tgt_id
7266 * parameters.
7267 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7268 * given iocb is for the SCSI host associated with the given vport.
7269 * This function is called with no locks held.
7270 **/
dea31012005-04-17 16:05:31 -05007271static int
James Smart51ef4c22007-08-02 11:10:31 -04007272lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7273 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007274 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007275{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007276 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007277 int rc = 1;
7278
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007279 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7280 return rc;
7281
James Smart51ef4c22007-08-02 11:10:31 -04007282 if (iocbq->vport != vport)
7283 return rc;
7284
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007285 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007286
James Smart495a7142008-06-14 22:52:59 -04007287 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007288 return rc;
7289
7290 switch (ctx_cmd) {
7291 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007292 if ((lpfc_cmd->rdata->pnode) &&
7293 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7294 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007295 rc = 0;
7296 break;
7297 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007298 if ((lpfc_cmd->rdata->pnode) &&
7299 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007300 rc = 0;
7301 break;
dea31012005-04-17 16:05:31 -05007302 case LPFC_CTX_HOST:
7303 rc = 0;
7304 break;
7305 default:
7306 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007307 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007308 break;
7309 }
7310
7311 return rc;
7312}
7313
James Smarte59058c2008-08-24 21:49:00 -04007314/**
James Smart3621a712009-04-06 18:47:14 -04007315 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007316 * @vport: Pointer to virtual port.
7317 * @tgt_id: SCSI ID of the target.
7318 * @lun_id: LUN ID of the scsi device.
7319 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7320 *
7321 * This function returns number of FCP commands pending for the vport.
7322 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7323 * commands pending on the vport associated with SCSI device specified
7324 * by tgt_id and lun_id parameters.
7325 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7326 * commands pending on the vport associated with SCSI target specified
7327 * by tgt_id parameter.
7328 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7329 * commands pending on the vport.
7330 * This function returns the number of iocbs which satisfy the filter.
7331 * This function is called without any lock held.
7332 **/
dea31012005-04-17 16:05:31 -05007333int
James Smart51ef4c22007-08-02 11:10:31 -04007334lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7335 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007336{
James Smart51ef4c22007-08-02 11:10:31 -04007337 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007338 struct lpfc_iocbq *iocbq;
7339 int sum, i;
dea31012005-04-17 16:05:31 -05007340
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007341 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7342 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007343
James Smart51ef4c22007-08-02 11:10:31 -04007344 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7345 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007346 sum++;
dea31012005-04-17 16:05:31 -05007347 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007348
dea31012005-04-17 16:05:31 -05007349 return sum;
7350}
7351
James Smarte59058c2008-08-24 21:49:00 -04007352/**
James Smart3621a712009-04-06 18:47:14 -04007353 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007354 * @phba: Pointer to HBA context object
7355 * @cmdiocb: Pointer to command iocb object.
7356 * @rspiocb: Pointer to response iocb object.
7357 *
7358 * This function is called when an aborted FCP iocb completes. This
7359 * function is called by the ring event handler with no lock held.
7360 * This function frees the iocb.
7361 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007362void
James Smart2e0fef82007-06-17 19:56:36 -05007363lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7364 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007365{
James Bottomley604a3e32005-10-29 10:28:33 -05007366 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007367 return;
7368}
7369
James Smarte59058c2008-08-24 21:49:00 -04007370/**
James Smart3621a712009-04-06 18:47:14 -04007371 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007372 * @vport: Pointer to virtual port.
7373 * @pring: Pointer to driver SLI ring object.
7374 * @tgt_id: SCSI ID of the target.
7375 * @lun_id: LUN ID of the scsi device.
7376 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7377 *
7378 * This function sends an abort command for every SCSI command
7379 * associated with the given virtual port pending on the ring
7380 * filtered by lpfc_sli_validate_fcp_iocb function.
7381 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7382 * FCP iocbs associated with lun specified by tgt_id and lun_id
7383 * parameters
7384 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7385 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7386 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7387 * FCP iocbs associated with virtual port.
7388 * This function returns number of iocbs it failed to abort.
7389 * This function is called with no locks held.
7390 **/
dea31012005-04-17 16:05:31 -05007391int
James Smart51ef4c22007-08-02 11:10:31 -04007392lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7393 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007394{
James Smart51ef4c22007-08-02 11:10:31 -04007395 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007396 struct lpfc_iocbq *iocbq;
7397 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007398 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007399 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007400 int i;
dea31012005-04-17 16:05:31 -05007401
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007402 for (i = 1; i <= phba->sli.last_iotag; i++) {
7403 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007404
James Smart51ef4c22007-08-02 11:10:31 -04007405 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007406 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007407 continue;
7408
7409 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007410 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007411 if (abtsiocb == NULL) {
7412 errcnt++;
7413 continue;
7414 }
dea31012005-04-17 16:05:31 -05007415
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007416 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007417 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7418 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007419 if (phba->sli_rev == LPFC_SLI_REV4)
7420 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7421 else
7422 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007423 abtsiocb->iocb.ulpLe = 1;
7424 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007425 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007426
James Smart5ffc2662009-11-18 15:39:44 -05007427 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7428 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007429 if (iocbq->iocb_flag & LPFC_IO_FCP)
7430 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007431
James Smart2e0fef82007-06-17 19:56:36 -05007432 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007433 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7434 else
7435 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7436
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007437 /* Setup callback routine and issue the command. */
7438 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007439 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7440 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007441 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007442 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007443 errcnt++;
7444 continue;
7445 }
7446 }
7447
7448 return errcnt;
7449}
7450
James Smarte59058c2008-08-24 21:49:00 -04007451/**
James Smart3621a712009-04-06 18:47:14 -04007452 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007453 * @phba: Pointer to HBA context object.
7454 * @cmdiocbq: Pointer to command iocb.
7455 * @rspiocbq: Pointer to response iocb.
7456 *
7457 * This function is the completion handler for iocbs issued using
7458 * lpfc_sli_issue_iocb_wait function. This function is called by the
7459 * ring event handler function without any lock held. This function
7460 * can be called from both worker thread context and interrupt
7461 * context. This function also can be called from other thread which
7462 * cleans up the SLI layer objects.
7463 * This function copy the contents of the response iocb to the
7464 * response iocb memory object provided by the caller of
7465 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7466 * sleeps for the iocb completion.
7467 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007468static void
7469lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7470 struct lpfc_iocbq *cmdiocbq,
7471 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007472{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007473 wait_queue_head_t *pdone_q;
7474 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007475 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007476
James Smart2e0fef82007-06-17 19:56:36 -05007477 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007478 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7479 if (cmdiocbq->context2 && rspiocbq)
7480 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7481 &rspiocbq->iocb, sizeof(IOCB_t));
7482
James Smart0f65ff62010-02-26 14:14:23 -05007483 /* Set the exchange busy flag for task management commands */
7484 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7485 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7486 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7487 cur_iocbq);
7488 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7489 }
7490
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007491 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007492 if (pdone_q)
7493 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007494 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007495 return;
7496}
7497
James Smarte59058c2008-08-24 21:49:00 -04007498/**
James Smartd11e31d2009-06-10 17:23:06 -04007499 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7500 * @phba: Pointer to HBA context object..
7501 * @piocbq: Pointer to command iocb.
7502 * @flag: Flag to test.
7503 *
7504 * This routine grabs the hbalock and then test the iocb_flag to
7505 * see if the passed in flag is set.
7506 * Returns:
7507 * 1 if flag is set.
7508 * 0 if flag is not set.
7509 **/
7510static int
7511lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7512 struct lpfc_iocbq *piocbq, uint32_t flag)
7513{
7514 unsigned long iflags;
7515 int ret;
7516
7517 spin_lock_irqsave(&phba->hbalock, iflags);
7518 ret = piocbq->iocb_flag & flag;
7519 spin_unlock_irqrestore(&phba->hbalock, iflags);
7520 return ret;
7521
7522}
7523
7524/**
James Smart3621a712009-04-06 18:47:14 -04007525 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007526 * @phba: Pointer to HBA context object..
7527 * @pring: Pointer to sli ring.
7528 * @piocb: Pointer to command iocb.
7529 * @prspiocbq: Pointer to response iocb.
7530 * @timeout: Timeout in number of seconds.
7531 *
7532 * This function issues the iocb to firmware and waits for the
7533 * iocb to complete. If the iocb command is not
7534 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7535 * Caller should not free the iocb resources if this function
7536 * returns IOCB_TIMEDOUT.
7537 * The function waits for the iocb completion using an
7538 * non-interruptible wait.
7539 * This function will sleep while waiting for iocb completion.
7540 * So, this function should not be called from any context which
7541 * does not allow sleeping. Due to the same reason, this function
7542 * cannot be called with interrupt disabled.
7543 * This function assumes that the iocb completions occur while
7544 * this function sleep. So, this function cannot be called from
7545 * the thread which process iocb completion for this ring.
7546 * This function clears the iocb_flag of the iocb object before
7547 * issuing the iocb and the iocb completion handler sets this
7548 * flag and wakes this thread when the iocb completes.
7549 * The contents of the response iocb will be copied to prspiocbq
7550 * by the completion handler when the command completes.
7551 * This function returns IOCB_SUCCESS when success.
7552 * This function is called with no lock held.
7553 **/
dea31012005-04-17 16:05:31 -05007554int
James Smart2e0fef82007-06-17 19:56:36 -05007555lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007556 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007557 struct lpfc_iocbq *piocb,
7558 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007559 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007560{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007561 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007562 long timeleft, timeout_req = 0;
7563 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007564 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007565
7566 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007567 * If the caller has provided a response iocbq buffer, then context2
7568 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007569 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007570 if (prspiocbq) {
7571 if (piocb->context2)
7572 return IOCB_ERROR;
7573 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007574 }
7575
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007576 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7577 piocb->context_un.wait_queue = &done_q;
7578 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007579
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007580 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7581 creg_val = readl(phba->HCregaddr);
7582 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7583 writel(creg_val, phba->HCregaddr);
7584 readl(phba->HCregaddr); /* flush */
7585 }
7586
James Smartda0436e2009-05-22 14:51:39 -04007587 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007588 if (retval == IOCB_SUCCESS) {
7589 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007590 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007591 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007592 timeout_req);
dea31012005-04-17 16:05:31 -05007593
James Smart7054a602007-04-25 09:52:34 -04007594 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7595 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007596 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007597 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007599 "0338 IOCB wait timeout error - no "
7600 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007601 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007602 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007604 "0330 IOCB wake NOT set, "
7605 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007606 timeout, (timeleft / jiffies));
7607 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007608 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007609 } else {
7610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007611 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007612 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007613 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007614 }
7615
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007616 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7617 creg_val = readl(phba->HCregaddr);
7618 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7619 writel(creg_val, phba->HCregaddr);
7620 readl(phba->HCregaddr); /* flush */
7621 }
7622
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007623 if (prspiocbq)
7624 piocb->context2 = NULL;
7625
7626 piocb->context_un.wait_queue = NULL;
7627 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007628 return retval;
7629}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007630
James Smarte59058c2008-08-24 21:49:00 -04007631/**
James Smart3621a712009-04-06 18:47:14 -04007632 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007633 * @phba: Pointer to HBA context object.
7634 * @pmboxq: Pointer to driver mailbox object.
7635 * @timeout: Timeout in number of seconds.
7636 *
7637 * This function issues the mailbox to firmware and waits for the
7638 * mailbox command to complete. If the mailbox command is not
7639 * completed within timeout seconds, it returns MBX_TIMEOUT.
7640 * The function waits for the mailbox completion using an
7641 * interruptible wait. If the thread is woken up due to a
7642 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7643 * should not free the mailbox resources, if this function returns
7644 * MBX_TIMEOUT.
7645 * This function will sleep while waiting for mailbox completion.
7646 * So, this function should not be called from any context which
7647 * does not allow sleeping. Due to the same reason, this function
7648 * cannot be called with interrupt disabled.
7649 * This function assumes that the mailbox completion occurs while
7650 * this function sleep. So, this function cannot be called from
7651 * the worker thread which processes mailbox completion.
7652 * This function is called in the context of HBA management
7653 * applications.
7654 * This function returns MBX_SUCCESS when successful.
7655 * This function is called with no lock held.
7656 **/
dea31012005-04-17 16:05:31 -05007657int
James Smart2e0fef82007-06-17 19:56:36 -05007658lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007659 uint32_t timeout)
7660{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007661 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007662 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007663 unsigned long flag;
dea31012005-04-17 16:05:31 -05007664
7665 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007666 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007667 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007668
James Smart495a7142008-06-14 22:52:59 -04007669 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007670 /* setup wake call as IOCB callback */
7671 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7672 /* setup context field to pass wait_queue pointer to wake function */
7673 pmboxq->context1 = &done_q;
7674
dea31012005-04-17 16:05:31 -05007675 /* now issue the command */
7676 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7677
7678 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007679 wait_event_interruptible_timeout(done_q,
7680 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7681 timeout * HZ);
7682
James Smart858c9f62007-06-17 19:56:39 -05007683 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007684 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007685 /*
7686 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7687 * else do not free the resources.
7688 */
7689 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007690 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007691 else {
James Smart7054a602007-04-25 09:52:34 -04007692 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007693 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7694 }
7695 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007696 }
7697
dea31012005-04-17 16:05:31 -05007698 return retval;
7699}
7700
James Smarte59058c2008-08-24 21:49:00 -04007701/**
James Smart3772a992009-05-22 14:50:54 -04007702 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007703 * @phba: Pointer to HBA context.
7704 *
James Smart3772a992009-05-22 14:50:54 -04007705 * This function is called to shutdown the driver's mailbox sub-system.
7706 * It first marks the mailbox sub-system is in a block state to prevent
7707 * the asynchronous mailbox command from issued off the pending mailbox
7708 * command queue. If the mailbox command sub-system shutdown is due to
7709 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7710 * the mailbox sub-system flush routine to forcefully bring down the
7711 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7712 * as with offline or HBA function reset), this routine will wait for the
7713 * outstanding mailbox command to complete before invoking the mailbox
7714 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007715 **/
James Smart3772a992009-05-22 14:50:54 -04007716void
7717lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007718{
James Smart3772a992009-05-22 14:50:54 -04007719 struct lpfc_sli *psli = &phba->sli;
7720 uint8_t actcmd = MBX_HEARTBEAT;
7721 unsigned long timeout;
7722
7723 spin_lock_irq(&phba->hbalock);
7724 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7725 spin_unlock_irq(&phba->hbalock);
7726
7727 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7728 spin_lock_irq(&phba->hbalock);
7729 if (phba->sli.mbox_active)
7730 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7731 spin_unlock_irq(&phba->hbalock);
7732 /* Determine how long we might wait for the active mailbox
7733 * command to be gracefully completed by firmware.
7734 */
7735 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7736 1000) + jiffies;
7737 while (phba->sli.mbox_active) {
7738 /* Check active mailbox complete status every 2ms */
7739 msleep(2);
7740 if (time_after(jiffies, timeout))
7741 /* Timeout, let the mailbox flush routine to
7742 * forcefully release active mailbox command
7743 */
7744 break;
7745 }
7746 }
7747 lpfc_sli_mbox_sys_flush(phba);
7748}
7749
7750/**
7751 * lpfc_sli_eratt_read - read sli-3 error attention events
7752 * @phba: Pointer to HBA context.
7753 *
7754 * This function is called to read the SLI3 device error attention registers
7755 * for possible error attention events. The caller must hold the hostlock
7756 * with spin_lock_irq().
7757 *
7758 * This fucntion returns 1 when there is Error Attention in the Host Attention
7759 * Register and returns 0 otherwise.
7760 **/
7761static int
7762lpfc_sli_eratt_read(struct lpfc_hba *phba)
7763{
James Smarted957682007-06-17 19:56:37 -05007764 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007765
James Smart3772a992009-05-22 14:50:54 -04007766 /* Read chip Host Attention (HA) register */
7767 ha_copy = readl(phba->HAregaddr);
7768 if (ha_copy & HA_ERATT) {
7769 /* Read host status register to retrieve error event */
7770 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007771
James Smart3772a992009-05-22 14:50:54 -04007772 /* Check if there is a deferred error condition is active */
7773 if ((HS_FFER1 & phba->work_hs) &&
7774 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7775 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007776 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007777 /* Clear all interrupt enable conditions */
7778 writel(0, phba->HCregaddr);
7779 readl(phba->HCregaddr);
7780 }
7781
7782 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007783 phba->work_ha |= HA_ERATT;
7784 /* Indicate polling handles this ERATT */
7785 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007786 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007787 }
James Smart3772a992009-05-22 14:50:54 -04007788 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007789}
7790
James Smarte59058c2008-08-24 21:49:00 -04007791/**
James Smartda0436e2009-05-22 14:51:39 -04007792 * lpfc_sli4_eratt_read - read sli-4 error attention events
7793 * @phba: Pointer to HBA context.
7794 *
7795 * This function is called to read the SLI4 device error attention registers
7796 * for possible error attention events. The caller must hold the hostlock
7797 * with spin_lock_irq().
7798 *
7799 * This fucntion returns 1 when there is Error Attention in the Host Attention
7800 * Register and returns 0 otherwise.
7801 **/
7802static int
7803lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7804{
7805 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007806
7807 /* For now, use the SLI4 device internal unrecoverable error
7808 * registers for error attention. This can be changed later.
7809 */
James Smarta747c9c2009-11-18 15:41:10 -05007810 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7811 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7812 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7813 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7815 "1423 HBA Unrecoverable error: "
7816 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7817 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7818 uerr_sta_lo, uerr_sta_hi,
7819 phba->sli4_hba.ue_mask_lo,
7820 phba->sli4_hba.ue_mask_hi);
7821 phba->work_status[0] = uerr_sta_lo;
7822 phba->work_status[1] = uerr_sta_hi;
7823 /* Set the driver HA work bitmap */
7824 phba->work_ha |= HA_ERATT;
7825 /* Indicate polling handles this ERATT */
7826 phba->hba_flag |= HBA_ERATT_HANDLED;
7827 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007828 }
7829 return 0;
7830}
7831
7832/**
James Smart3621a712009-04-06 18:47:14 -04007833 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007834 * @phba: Pointer to HBA context.
7835 *
James Smart3772a992009-05-22 14:50:54 -04007836 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007837 * error attention register bit for error attention events.
7838 *
7839 * This fucntion returns 1 when there is Error Attention in the Host Attention
7840 * Register and returns 0 otherwise.
7841 **/
7842int
7843lpfc_sli_check_eratt(struct lpfc_hba *phba)
7844{
7845 uint32_t ha_copy;
7846
7847 /* If somebody is waiting to handle an eratt, don't process it
7848 * here. The brdkill function will do this.
7849 */
7850 if (phba->link_flag & LS_IGNORE_ERATT)
7851 return 0;
7852
7853 /* Check if interrupt handler handles this ERATT */
7854 spin_lock_irq(&phba->hbalock);
7855 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7856 /* Interrupt handler has handled ERATT */
7857 spin_unlock_irq(&phba->hbalock);
7858 return 0;
7859 }
7860
James Smarta257bf92009-04-06 18:48:10 -04007861 /*
7862 * If there is deferred error attention, do not check for error
7863 * attention
7864 */
7865 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7866 spin_unlock_irq(&phba->hbalock);
7867 return 0;
7868 }
7869
James Smart3772a992009-05-22 14:50:54 -04007870 /* If PCI channel is offline, don't process it */
7871 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007872 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007873 return 0;
7874 }
7875
7876 switch (phba->sli_rev) {
7877 case LPFC_SLI_REV2:
7878 case LPFC_SLI_REV3:
7879 /* Read chip Host Attention (HA) register */
7880 ha_copy = lpfc_sli_eratt_read(phba);
7881 break;
James Smartda0436e2009-05-22 14:51:39 -04007882 case LPFC_SLI_REV4:
7883 /* Read devcie Uncoverable Error (UERR) registers */
7884 ha_copy = lpfc_sli4_eratt_read(phba);
7885 break;
James Smart3772a992009-05-22 14:50:54 -04007886 default:
7887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7888 "0299 Invalid SLI revision (%d)\n",
7889 phba->sli_rev);
7890 ha_copy = 0;
7891 break;
James Smart93996272008-08-24 21:50:30 -04007892 }
7893 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007894
7895 return ha_copy;
7896}
7897
7898/**
7899 * lpfc_intr_state_check - Check device state for interrupt handling
7900 * @phba: Pointer to HBA context.
7901 *
7902 * This inline routine checks whether a device or its PCI slot is in a state
7903 * that the interrupt should be handled.
7904 *
7905 * This function returns 0 if the device or the PCI slot is in a state that
7906 * interrupt should be handled, otherwise -EIO.
7907 */
7908static inline int
7909lpfc_intr_state_check(struct lpfc_hba *phba)
7910{
7911 /* If the pci channel is offline, ignore all the interrupts */
7912 if (unlikely(pci_channel_offline(phba->pcidev)))
7913 return -EIO;
7914
7915 /* Update device level interrupt statistics */
7916 phba->sli.slistat.sli_intr++;
7917
7918 /* Ignore all interrupts during initialization. */
7919 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7920 return -EIO;
7921
James Smart93996272008-08-24 21:50:30 -04007922 return 0;
7923}
7924
7925/**
James Smart3772a992009-05-22 14:50:54 -04007926 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007927 * @irq: Interrupt number.
7928 * @dev_id: The device context pointer.
7929 *
James Smart93996272008-08-24 21:50:30 -04007930 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007931 * service routine when device with SLI-3 interface spec is enabled with
7932 * MSI-X multi-message interrupt mode and there are slow-path events in
7933 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7934 * interrupt mode, this function is called as part of the device-level
7935 * interrupt handler. When the PCI slot is in error recovery or the HBA
7936 * is undergoing initialization, the interrupt handler will not process
7937 * the interrupt. The link attention and ELS ring attention events are
7938 * handled by the worker thread. The interrupt handler signals the worker
7939 * thread and returns for these events. This function is called without
7940 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007941 * structures.
7942 *
7943 * This function returns IRQ_HANDLED when interrupt is handled else it
7944 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007945 **/
dea31012005-04-17 16:05:31 -05007946irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007947lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007948{
James Smart2e0fef82007-06-17 19:56:36 -05007949 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007950 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007951 uint32_t work_ha_copy;
7952 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007953 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007954 uint32_t control;
7955
James Smart92d7f7b2007-06-17 19:56:38 -05007956 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007957 struct lpfc_vport *vport;
7958 struct lpfc_nodelist *ndlp;
7959 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007960 LPFC_MBOXQ_t *pmb;
7961 int rc;
7962
dea31012005-04-17 16:05:31 -05007963 /*
7964 * Get the driver's phba structure from the dev_id and
7965 * assume the HBA is not interrupting.
7966 */
James Smart93996272008-08-24 21:50:30 -04007967 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007968
7969 if (unlikely(!phba))
7970 return IRQ_NONE;
7971
dea31012005-04-17 16:05:31 -05007972 /*
James Smart93996272008-08-24 21:50:30 -04007973 * Stuff needs to be attented to when this function is invoked as an
7974 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007975 */
James Smart93996272008-08-24 21:50:30 -04007976 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007977 /* Check device state for handling interrupt */
7978 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007979 return IRQ_NONE;
7980 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007981 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007982 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007983 /* If somebody is waiting to handle an eratt don't process it
7984 * here. The brdkill function will do this.
7985 */
7986 if (phba->link_flag & LS_IGNORE_ERATT)
7987 ha_copy &= ~HA_ERATT;
7988 /* Check the need for handling ERATT in interrupt handler */
7989 if (ha_copy & HA_ERATT) {
7990 if (phba->hba_flag & HBA_ERATT_HANDLED)
7991 /* ERATT polling has handled ERATT */
7992 ha_copy &= ~HA_ERATT;
7993 else
7994 /* Indicate interrupt handler handles ERATT */
7995 phba->hba_flag |= HBA_ERATT_HANDLED;
7996 }
James Smarta257bf92009-04-06 18:48:10 -04007997
7998 /*
7999 * If there is deferred error attention, do not check for any
8000 * interrupt.
8001 */
8002 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008003 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008004 return IRQ_NONE;
8005 }
8006
James Smart93996272008-08-24 21:50:30 -04008007 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008008 hc_copy = readl(phba->HCregaddr);
8009 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8010 HC_LAINT_ENA | HC_ERINT_ENA),
8011 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008012 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8013 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008014 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008015 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008016 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008017 } else
8018 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008019
dea31012005-04-17 16:05:31 -05008020 work_ha_copy = ha_copy & phba->work_ha_mask;
8021
James Smart93996272008-08-24 21:50:30 -04008022 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008023 if (work_ha_copy & HA_LATT) {
8024 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8025 /*
8026 * Turn off Link Attention interrupts
8027 * until CLEAR_LA done
8028 */
James Smart5b75da22008-12-04 22:39:35 -05008029 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008030 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8031 control = readl(phba->HCregaddr);
8032 control &= ~HC_LAINT_ENA;
8033 writel(control, phba->HCregaddr);
8034 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008035 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008036 }
8037 else
8038 work_ha_copy &= ~HA_LATT;
8039 }
8040
James Smart93996272008-08-24 21:50:30 -04008041 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008042 /*
8043 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8044 * the only slow ring.
8045 */
8046 status = (work_ha_copy &
8047 (HA_RXMASK << (4*LPFC_ELS_RING)));
8048 status >>= (4*LPFC_ELS_RING);
8049 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008050 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008051 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008052
8053 lpfc_debugfs_slow_ring_trc(phba,
8054 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8055 control, status,
8056 (uint32_t)phba->sli.slistat.sli_intr);
8057
James Smart858c9f62007-06-17 19:56:39 -05008058 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008059 lpfc_debugfs_slow_ring_trc(phba,
8060 "ISR Disable ring:"
8061 "pwork:x%x hawork:x%x wait:x%x",
8062 phba->work_ha, work_ha_copy,
8063 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008064 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008065
James Smart858c9f62007-06-17 19:56:39 -05008066 control &=
8067 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008068 writel(control, phba->HCregaddr);
8069 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008070 }
James Smarta58cbd52007-08-02 11:09:43 -04008071 else {
8072 lpfc_debugfs_slow_ring_trc(phba,
8073 "ISR slow ring: pwork:"
8074 "x%x hawork:x%x wait:x%x",
8075 phba->work_ha, work_ha_copy,
8076 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008077 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008078 }
James Smart5b75da22008-12-04 22:39:35 -05008079 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008080 }
8081 }
James Smart5b75da22008-12-04 22:39:35 -05008082 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008083 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008084 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008085 /*
8086 * Check if there is a deferred error condition
8087 * is active
8088 */
8089 if ((HS_FFER1 & phba->work_hs) &&
8090 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8091 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8092 phba->hba_flag |= DEFER_ERATT;
8093 /* Clear all interrupt enable conditions */
8094 writel(0, phba->HCregaddr);
8095 readl(phba->HCregaddr);
8096 }
8097 }
8098
James Smart93996272008-08-24 21:50:30 -04008099 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008100 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008101 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008102 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008103 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008104
8105 /* First check out the status word */
8106 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8107 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008108 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008109 /*
8110 * Stray Mailbox Interrupt, mbxCommand <cmd>
8111 * mbxStatus <status>
8112 */
James Smart09372822008-01-11 01:52:54 -05008113 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008114 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008115 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008116 "Interrupt mbxCommand x%x "
8117 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008118 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008119 pmbox->mbxCommand,
8120 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008121 /* clear mailbox attention bit */
8122 work_ha_copy &= ~HA_MBATT;
8123 } else {
James Smart97eab632008-04-07 10:16:05 -04008124 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008125 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008126 phba->last_completion_time = jiffies;
8127 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008128 if (pmb->mbox_cmpl) {
8129 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8130 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008131 }
James Smart09372822008-01-11 01:52:54 -05008132 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8133 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8134
8135 lpfc_debugfs_disc_trc(vport,
8136 LPFC_DISC_TRC_MBOX_VPORT,
8137 "MBOX dflt rpi: : "
8138 "status:x%x rpi:x%x",
8139 (uint32_t)pmbox->mbxStatus,
8140 pmbox->un.varWords[0], 0);
8141
8142 if (!pmbox->mbxStatus) {
8143 mp = (struct lpfc_dmabuf *)
8144 (pmb->context1);
8145 ndlp = (struct lpfc_nodelist *)
8146 pmb->context2;
8147
8148 /* Reg_LOGIN of dflt RPI was
8149 * successful. new lets get
8150 * rid of the RPI using the
8151 * same mbox buffer.
8152 */
8153 lpfc_unreg_login(phba,
8154 vport->vpi,
8155 pmbox->un.varWords[0],
8156 pmb);
8157 pmb->mbox_cmpl =
8158 lpfc_mbx_cmpl_dflt_rpi;
8159 pmb->context1 = mp;
8160 pmb->context2 = ndlp;
8161 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008162 rc = lpfc_sli_issue_mbox(phba,
8163 pmb,
8164 MBX_NOWAIT);
8165 if (rc != MBX_BUSY)
8166 lpfc_printf_log(phba,
8167 KERN_ERR,
8168 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008169 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008170 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008171 if (rc != MBX_NOT_FINISHED)
8172 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008173 }
8174 }
James Smart5b75da22008-12-04 22:39:35 -05008175 spin_lock_irqsave(
8176 &phba->pport->work_port_lock,
8177 iflag);
James Smart09372822008-01-11 01:52:54 -05008178 phba->pport->work_port_events &=
8179 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008180 spin_unlock_irqrestore(
8181 &phba->pport->work_port_lock,
8182 iflag);
James Smart09372822008-01-11 01:52:54 -05008183 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008184 }
James Smart97eab632008-04-07 10:16:05 -04008185 } else
James Smart5b75da22008-12-04 22:39:35 -05008186 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008187
James Smart92d7f7b2007-06-17 19:56:38 -05008188 if ((work_ha_copy & HA_MBATT) &&
8189 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008190send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008191 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008192 do {
8193 rc = lpfc_sli_issue_mbox(phba, NULL,
8194 MBX_NOWAIT);
8195 } while (rc == MBX_NOT_FINISHED);
8196 if (rc != MBX_SUCCESS)
8197 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8198 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008199 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008200 }
8201
James Smart5b75da22008-12-04 22:39:35 -05008202 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008203 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008204 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008205 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008206 }
James Smart93996272008-08-24 21:50:30 -04008207 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008208
James Smart3772a992009-05-22 14:50:54 -04008209} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008210
8211/**
James Smart3772a992009-05-22 14:50:54 -04008212 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008213 * @irq: Interrupt number.
8214 * @dev_id: The device context pointer.
8215 *
8216 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008217 * service routine when device with SLI-3 interface spec is enabled with
8218 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8219 * ring event in the HBA. However, when the device is enabled with either
8220 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8221 * device-level interrupt handler. When the PCI slot is in error recovery
8222 * or the HBA is undergoing initialization, the interrupt handler will not
8223 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8224 * the intrrupt context. This function is called without any lock held.
8225 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008226 *
8227 * This function returns IRQ_HANDLED when interrupt is handled else it
8228 * returns IRQ_NONE.
8229 **/
8230irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008231lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008232{
8233 struct lpfc_hba *phba;
8234 uint32_t ha_copy;
8235 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008236 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008237
8238 /* Get the driver's phba structure from the dev_id and
8239 * assume the HBA is not interrupting.
8240 */
8241 phba = (struct lpfc_hba *) dev_id;
8242
8243 if (unlikely(!phba))
8244 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008245
8246 /*
James Smart93996272008-08-24 21:50:30 -04008247 * Stuff needs to be attented to when this function is invoked as an
8248 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008249 */
James Smart93996272008-08-24 21:50:30 -04008250 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008251 /* Check device state for handling interrupt */
8252 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008253 return IRQ_NONE;
8254 /* Need to read HA REG for FCP ring and other ring events */
8255 ha_copy = readl(phba->HAregaddr);
8256 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008257 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008258 /*
8259 * If there is deferred error attention, do not check for
8260 * any interrupt.
8261 */
8262 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008263 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008264 return IRQ_NONE;
8265 }
James Smart93996272008-08-24 21:50:30 -04008266 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8267 phba->HAregaddr);
8268 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008269 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008270 } else
8271 ha_copy = phba->ha_copy;
8272
8273 /*
8274 * Process all events on FCP ring. Take the optimized path for FCP IO.
8275 */
8276 ha_copy &= ~(phba->work_ha_mask);
8277
8278 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008279 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008280 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008281 lpfc_sli_handle_fast_ring_event(phba,
8282 &phba->sli.ring[LPFC_FCP_RING],
8283 status);
James Smarta4bc3372006-12-02 13:34:16 -05008284
8285 if (phba->cfg_multi_ring_support == 2) {
8286 /*
James Smart93996272008-08-24 21:50:30 -04008287 * Process all events on extra ring. Take the optimized path
8288 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008289 */
James Smart93996272008-08-24 21:50:30 -04008290 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008291 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008292 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008293 lpfc_sli_handle_fast_ring_event(phba,
8294 &phba->sli.ring[LPFC_EXTRA_RING],
8295 status);
8296 }
8297 }
dea31012005-04-17 16:05:31 -05008298 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008299} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008300
James Smart93996272008-08-24 21:50:30 -04008301/**
James Smart3772a992009-05-22 14:50:54 -04008302 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008303 * @irq: Interrupt number.
8304 * @dev_id: The device context pointer.
8305 *
James Smart3772a992009-05-22 14:50:54 -04008306 * This function is the HBA device-level interrupt handler to device with
8307 * SLI-3 interface spec, called from the PCI layer when either MSI or
8308 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8309 * requires driver attention. This function invokes the slow-path interrupt
8310 * attention handling function and fast-path interrupt attention handling
8311 * function in turn to process the relevant HBA attention events. This
8312 * function is called without any lock held. It gets the hbalock to access
8313 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008314 *
8315 * This function returns IRQ_HANDLED when interrupt is handled, else it
8316 * returns IRQ_NONE.
8317 **/
8318irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008319lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008320{
8321 struct lpfc_hba *phba;
8322 irqreturn_t sp_irq_rc, fp_irq_rc;
8323 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008324 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008325
8326 /*
8327 * Get the driver's phba structure from the dev_id and
8328 * assume the HBA is not interrupting.
8329 */
8330 phba = (struct lpfc_hba *) dev_id;
8331
8332 if (unlikely(!phba))
8333 return IRQ_NONE;
8334
James Smart3772a992009-05-22 14:50:54 -04008335 /* Check device state for handling interrupt */
8336 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008337 return IRQ_NONE;
8338
8339 spin_lock(&phba->hbalock);
8340 phba->ha_copy = readl(phba->HAregaddr);
8341 if (unlikely(!phba->ha_copy)) {
8342 spin_unlock(&phba->hbalock);
8343 return IRQ_NONE;
8344 } else if (phba->ha_copy & HA_ERATT) {
8345 if (phba->hba_flag & HBA_ERATT_HANDLED)
8346 /* ERATT polling has handled ERATT */
8347 phba->ha_copy &= ~HA_ERATT;
8348 else
8349 /* Indicate interrupt handler handles ERATT */
8350 phba->hba_flag |= HBA_ERATT_HANDLED;
8351 }
8352
James Smarta257bf92009-04-06 18:48:10 -04008353 /*
8354 * If there is deferred error attention, do not check for any interrupt.
8355 */
8356 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8357 spin_unlock_irq(&phba->hbalock);
8358 return IRQ_NONE;
8359 }
8360
James Smart93996272008-08-24 21:50:30 -04008361 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008362 hc_copy = readl(phba->HCregaddr);
8363 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8364 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8365 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008366 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008367 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008368 readl(phba->HAregaddr); /* flush */
8369 spin_unlock(&phba->hbalock);
8370
8371 /*
8372 * Invokes slow-path host attention interrupt handling as appropriate.
8373 */
8374
8375 /* status of events with mailbox and link attention */
8376 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8377
8378 /* status of events with ELS ring */
8379 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8380 status2 >>= (4*LPFC_ELS_RING);
8381
8382 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008383 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008384 else
8385 sp_irq_rc = IRQ_NONE;
8386
8387 /*
8388 * Invoke fast-path host attention interrupt handling as appropriate.
8389 */
8390
8391 /* status of events with FCP ring */
8392 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8393 status1 >>= (4*LPFC_FCP_RING);
8394
8395 /* status of events with extra ring */
8396 if (phba->cfg_multi_ring_support == 2) {
8397 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8398 status2 >>= (4*LPFC_EXTRA_RING);
8399 } else
8400 status2 = 0;
8401
8402 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008403 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008404 else
8405 fp_irq_rc = IRQ_NONE;
8406
8407 /* Return device-level interrupt handling status */
8408 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008409} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008410
8411/**
8412 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8413 * @phba: pointer to lpfc hba data structure.
8414 *
8415 * This routine is invoked by the worker thread to process all the pending
8416 * SLI4 FCP abort XRI events.
8417 **/
8418void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8419{
8420 struct lpfc_cq_event *cq_event;
8421
8422 /* First, declare the fcp xri abort event has been handled */
8423 spin_lock_irq(&phba->hbalock);
8424 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8425 spin_unlock_irq(&phba->hbalock);
8426 /* Now, handle all the fcp xri abort events */
8427 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8428 /* Get the first event from the head of the event queue */
8429 spin_lock_irq(&phba->hbalock);
8430 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8431 cq_event, struct lpfc_cq_event, list);
8432 spin_unlock_irq(&phba->hbalock);
8433 /* Notify aborted XRI for FCP work queue */
8434 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8435 /* Free the event processed back to the free pool */
8436 lpfc_sli4_cq_event_release(phba, cq_event);
8437 }
8438}
8439
8440/**
8441 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8442 * @phba: pointer to lpfc hba data structure.
8443 *
8444 * This routine is invoked by the worker thread to process all the pending
8445 * SLI4 els abort xri events.
8446 **/
8447void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8448{
8449 struct lpfc_cq_event *cq_event;
8450
8451 /* First, declare the els xri abort event has been handled */
8452 spin_lock_irq(&phba->hbalock);
8453 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8454 spin_unlock_irq(&phba->hbalock);
8455 /* Now, handle all the els xri abort events */
8456 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8457 /* Get the first event from the head of the event queue */
8458 spin_lock_irq(&phba->hbalock);
8459 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8460 cq_event, struct lpfc_cq_event, list);
8461 spin_unlock_irq(&phba->hbalock);
8462 /* Notify aborted XRI for ELS work queue */
8463 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8464 /* Free the event processed back to the free pool */
8465 lpfc_sli4_cq_event_release(phba, cq_event);
8466 }
8467}
8468
James Smart341af102010-01-26 23:07:37 -05008469/**
8470 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8471 * @phba: pointer to lpfc hba data structure
8472 * @pIocbIn: pointer to the rspiocbq
8473 * @pIocbOut: pointer to the cmdiocbq
8474 * @wcqe: pointer to the complete wcqe
8475 *
8476 * This routine transfers the fields of a command iocbq to a response iocbq
8477 * by copying all the IOCB fields from command iocbq and transferring the
8478 * completion status information from the complete wcqe.
8479 **/
James Smart4f774512009-05-22 14:52:35 -04008480static void
James Smart341af102010-01-26 23:07:37 -05008481lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8482 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008483 struct lpfc_iocbq *pIocbOut,
8484 struct lpfc_wcqe_complete *wcqe)
8485{
James Smart341af102010-01-26 23:07:37 -05008486 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008487 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8488
8489 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8490 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008491 /* Map WCQE parameters into irspiocb parameters */
8492 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8493 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8494 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8495 pIocbIn->iocb.un.fcpi.fcpi_parm =
8496 pIocbOut->iocb.un.fcpi.fcpi_parm -
8497 wcqe->total_data_placed;
8498 else
8499 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008500 else {
James Smart4f774512009-05-22 14:52:35 -04008501 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008502 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8503 }
James Smart341af102010-01-26 23:07:37 -05008504
8505 /* Pick up HBA exchange busy condition */
8506 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8507 spin_lock_irqsave(&phba->hbalock, iflags);
8508 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8509 spin_unlock_irqrestore(&phba->hbalock, iflags);
8510 }
James Smart4f774512009-05-22 14:52:35 -04008511}
8512
8513/**
James Smart45ed1192009-10-02 15:17:02 -04008514 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8515 * @phba: Pointer to HBA context object.
8516 * @wcqe: Pointer to work-queue completion queue entry.
8517 *
8518 * This routine handles an ELS work-queue completion event and construct
8519 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8520 * discovery engine to handle.
8521 *
8522 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8523 **/
8524static struct lpfc_iocbq *
8525lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8526 struct lpfc_iocbq *irspiocbq)
8527{
8528 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8529 struct lpfc_iocbq *cmdiocbq;
8530 struct lpfc_wcqe_complete *wcqe;
8531 unsigned long iflags;
8532
8533 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8534 spin_lock_irqsave(&phba->hbalock, iflags);
8535 pring->stats.iocb_event++;
8536 /* Look up the ELS command IOCB and create pseudo response IOCB */
8537 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8538 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8539 spin_unlock_irqrestore(&phba->hbalock, iflags);
8540
8541 if (unlikely(!cmdiocbq)) {
8542 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8543 "0386 ELS complete with no corresponding "
8544 "cmdiocb: iotag (%d)\n",
8545 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8546 lpfc_sli_release_iocbq(phba, irspiocbq);
8547 return NULL;
8548 }
8549
8550 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008551 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008552
8553 return irspiocbq;
8554}
8555
8556/**
James Smart04c68492009-05-22 14:52:52 -04008557 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8558 * @phba: Pointer to HBA context object.
8559 * @cqe: Pointer to mailbox completion queue entry.
8560 *
8561 * This routine process a mailbox completion queue entry with asynchrous
8562 * event.
8563 *
8564 * Return: true if work posted to worker thread, otherwise false.
8565 **/
8566static bool
8567lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8568{
8569 struct lpfc_cq_event *cq_event;
8570 unsigned long iflags;
8571
8572 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8573 "0392 Async Event: word0:x%x, word1:x%x, "
8574 "word2:x%x, word3:x%x\n", mcqe->word0,
8575 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8576
8577 /* Allocate a new internal CQ_EVENT entry */
8578 cq_event = lpfc_sli4_cq_event_alloc(phba);
8579 if (!cq_event) {
8580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8581 "0394 Failed to allocate CQ_EVENT entry\n");
8582 return false;
8583 }
8584
8585 /* Move the CQE into an asynchronous event entry */
8586 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8587 spin_lock_irqsave(&phba->hbalock, iflags);
8588 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8589 /* Set the async event flag */
8590 phba->hba_flag |= ASYNC_EVENT;
8591 spin_unlock_irqrestore(&phba->hbalock, iflags);
8592
8593 return true;
8594}
8595
8596/**
8597 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8598 * @phba: Pointer to HBA context object.
8599 * @cqe: Pointer to mailbox completion queue entry.
8600 *
8601 * This routine process a mailbox completion queue entry with mailbox
8602 * completion event.
8603 *
8604 * Return: true if work posted to worker thread, otherwise false.
8605 **/
8606static bool
8607lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8608{
8609 uint32_t mcqe_status;
8610 MAILBOX_t *mbox, *pmbox;
8611 struct lpfc_mqe *mqe;
8612 struct lpfc_vport *vport;
8613 struct lpfc_nodelist *ndlp;
8614 struct lpfc_dmabuf *mp;
8615 unsigned long iflags;
8616 LPFC_MBOXQ_t *pmb;
8617 bool workposted = false;
8618 int rc;
8619
8620 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8621 if (!bf_get(lpfc_trailer_completed, mcqe))
8622 goto out_no_mqe_complete;
8623
8624 /* Get the reference to the active mbox command */
8625 spin_lock_irqsave(&phba->hbalock, iflags);
8626 pmb = phba->sli.mbox_active;
8627 if (unlikely(!pmb)) {
8628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8629 "1832 No pending MBOX command to handle\n");
8630 spin_unlock_irqrestore(&phba->hbalock, iflags);
8631 goto out_no_mqe_complete;
8632 }
8633 spin_unlock_irqrestore(&phba->hbalock, iflags);
8634 mqe = &pmb->u.mqe;
8635 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8636 mbox = phba->mbox;
8637 vport = pmb->vport;
8638
8639 /* Reset heartbeat timer */
8640 phba->last_completion_time = jiffies;
8641 del_timer(&phba->sli.mbox_tmo);
8642
8643 /* Move mbox data to caller's mailbox region, do endian swapping */
8644 if (pmb->mbox_cmpl && mbox)
8645 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8646 /* Set the mailbox status with SLI4 range 0x4000 */
8647 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8648 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8649 bf_set(lpfc_mqe_status, mqe,
8650 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8651
8652 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8653 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8654 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8655 "MBOX dflt rpi: status:x%x rpi:x%x",
8656 mcqe_status,
8657 pmbox->un.varWords[0], 0);
8658 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8659 mp = (struct lpfc_dmabuf *)(pmb->context1);
8660 ndlp = (struct lpfc_nodelist *)pmb->context2;
8661 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8662 * RID of the PPI using the same mbox buffer.
8663 */
8664 lpfc_unreg_login(phba, vport->vpi,
8665 pmbox->un.varWords[0], pmb);
8666 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8667 pmb->context1 = mp;
8668 pmb->context2 = ndlp;
8669 pmb->vport = vport;
8670 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8671 if (rc != MBX_BUSY)
8672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8673 LOG_SLI, "0385 rc should "
8674 "have been MBX_BUSY\n");
8675 if (rc != MBX_NOT_FINISHED)
8676 goto send_current_mbox;
8677 }
8678 }
8679 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8680 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8681 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8682
8683 /* There is mailbox completion work to do */
8684 spin_lock_irqsave(&phba->hbalock, iflags);
8685 __lpfc_mbox_cmpl_put(phba, pmb);
8686 phba->work_ha |= HA_MBATT;
8687 spin_unlock_irqrestore(&phba->hbalock, iflags);
8688 workposted = true;
8689
8690send_current_mbox:
8691 spin_lock_irqsave(&phba->hbalock, iflags);
8692 /* Release the mailbox command posting token */
8693 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8694 /* Setting active mailbox pointer need to be in sync to flag clear */
8695 phba->sli.mbox_active = NULL;
8696 spin_unlock_irqrestore(&phba->hbalock, iflags);
8697 /* Wake up worker thread to post the next pending mailbox command */
8698 lpfc_worker_wake_up(phba);
8699out_no_mqe_complete:
8700 if (bf_get(lpfc_trailer_consumed, mcqe))
8701 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8702 return workposted;
8703}
8704
8705/**
8706 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8707 * @phba: Pointer to HBA context object.
8708 * @cqe: Pointer to mailbox completion queue entry.
8709 *
8710 * This routine process a mailbox completion queue entry, it invokes the
8711 * proper mailbox complete handling or asynchrous event handling routine
8712 * according to the MCQE's async bit.
8713 *
8714 * Return: true if work posted to worker thread, otherwise false.
8715 **/
8716static bool
8717lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8718{
8719 struct lpfc_mcqe mcqe;
8720 bool workposted;
8721
8722 /* Copy the mailbox MCQE and convert endian order as needed */
8723 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8724
8725 /* Invoke the proper event handling routine */
8726 if (!bf_get(lpfc_trailer_async, &mcqe))
8727 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8728 else
8729 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8730 return workposted;
8731}
8732
8733/**
James Smart4f774512009-05-22 14:52:35 -04008734 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8735 * @phba: Pointer to HBA context object.
8736 * @wcqe: Pointer to work-queue completion queue entry.
8737 *
8738 * This routine handles an ELS work-queue completion event.
8739 *
8740 * Return: true if work posted to worker thread, otherwise false.
8741 **/
8742static bool
8743lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8744 struct lpfc_wcqe_complete *wcqe)
8745{
James Smart4f774512009-05-22 14:52:35 -04008746 struct lpfc_iocbq *irspiocbq;
8747 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008748
James Smart45ed1192009-10-02 15:17:02 -04008749 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008750 irspiocbq = lpfc_sli_get_iocbq(phba);
8751 if (!irspiocbq) {
8752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8753 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008754 return false;
James Smart4f774512009-05-22 14:52:35 -04008755 }
James Smart4f774512009-05-22 14:52:35 -04008756
James Smart45ed1192009-10-02 15:17:02 -04008757 /* Save off the slow-path queue event for work thread to process */
8758 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008759 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008760 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008761 &phba->sli4_hba.sp_queue_event);
8762 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008763 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008764
James Smart45ed1192009-10-02 15:17:02 -04008765 return true;
James Smart4f774512009-05-22 14:52:35 -04008766}
8767
8768/**
8769 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8770 * @phba: Pointer to HBA context object.
8771 * @wcqe: Pointer to work-queue completion queue entry.
8772 *
8773 * This routine handles slow-path WQ entry comsumed event by invoking the
8774 * proper WQ release routine to the slow-path WQ.
8775 **/
8776static void
8777lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8778 struct lpfc_wcqe_release *wcqe)
8779{
8780 /* Check for the slow-path ELS work queue */
8781 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8782 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8783 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8784 else
8785 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8786 "2579 Slow-path wqe consume event carries "
8787 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8788 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8789 phba->sli4_hba.els_wq->queue_id);
8790}
8791
8792/**
8793 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8794 * @phba: Pointer to HBA context object.
8795 * @cq: Pointer to a WQ completion queue.
8796 * @wcqe: Pointer to work-queue completion queue entry.
8797 *
8798 * This routine handles an XRI abort event.
8799 *
8800 * Return: true if work posted to worker thread, otherwise false.
8801 **/
8802static bool
8803lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8804 struct lpfc_queue *cq,
8805 struct sli4_wcqe_xri_aborted *wcqe)
8806{
8807 bool workposted = false;
8808 struct lpfc_cq_event *cq_event;
8809 unsigned long iflags;
8810
8811 /* Allocate a new internal CQ_EVENT entry */
8812 cq_event = lpfc_sli4_cq_event_alloc(phba);
8813 if (!cq_event) {
8814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8815 "0602 Failed to allocate CQ_EVENT entry\n");
8816 return false;
8817 }
8818
8819 /* Move the CQE into the proper xri abort event list */
8820 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8821 switch (cq->subtype) {
8822 case LPFC_FCP:
8823 spin_lock_irqsave(&phba->hbalock, iflags);
8824 list_add_tail(&cq_event->list,
8825 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8826 /* Set the fcp xri abort event flag */
8827 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8828 spin_unlock_irqrestore(&phba->hbalock, iflags);
8829 workposted = true;
8830 break;
8831 case LPFC_ELS:
8832 spin_lock_irqsave(&phba->hbalock, iflags);
8833 list_add_tail(&cq_event->list,
8834 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8835 /* Set the els xri abort event flag */
8836 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8837 spin_unlock_irqrestore(&phba->hbalock, iflags);
8838 workposted = true;
8839 break;
8840 default:
8841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8842 "0603 Invalid work queue CQE subtype (x%x)\n",
8843 cq->subtype);
8844 workposted = false;
8845 break;
8846 }
8847 return workposted;
8848}
8849
8850/**
James Smart4d9ab992009-10-02 15:16:39 -04008851 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008852 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008853 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008854 *
James Smart4d9ab992009-10-02 15:16:39 -04008855 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008856 *
8857 * Return: true if work posted to worker thread, otherwise false.
8858 **/
8859static bool
James Smart4d9ab992009-10-02 15:16:39 -04008860lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8861{
8862 bool workposted = false;
8863 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8864 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8865 struct hbq_dmabuf *dma_buf;
8866 uint32_t status;
8867 unsigned long iflags;
8868
James Smart4d9ab992009-10-02 15:16:39 -04008869 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8870 goto out;
8871
8872 status = bf_get(lpfc_rcqe_status, rcqe);
8873 switch (status) {
8874 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8876 "2537 Receive Frame Truncated!!\n");
8877 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008878 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008879 spin_lock_irqsave(&phba->hbalock, iflags);
8880 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8881 if (!dma_buf) {
8882 spin_unlock_irqrestore(&phba->hbalock, iflags);
8883 goto out;
8884 }
8885 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8886 /* save off the frame for the word thread to process */
8887 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008888 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008889 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008890 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008891 spin_unlock_irqrestore(&phba->hbalock, iflags);
8892 workposted = true;
8893 break;
8894 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8895 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8896 /* Post more buffers if possible */
8897 spin_lock_irqsave(&phba->hbalock, iflags);
8898 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8899 spin_unlock_irqrestore(&phba->hbalock, iflags);
8900 workposted = true;
8901 break;
8902 }
8903out:
8904 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008905}
8906
8907/**
8908 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8909 * @phba: Pointer to HBA context object.
8910 * @cq: Pointer to the completion queue.
8911 * @wcqe: Pointer to a completion queue entry.
8912 *
8913 * This routine process a slow-path work-queue or recieve queue completion queue
8914 * entry.
8915 *
8916 * Return: true if work posted to worker thread, otherwise false.
8917 **/
8918static bool
8919lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008920 struct lpfc_cqe *cqe)
8921{
James Smart45ed1192009-10-02 15:17:02 -04008922 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008923 bool workposted = false;
8924
8925 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008926 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008927
8928 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008929 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008930 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008931 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008932 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008933 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008934 break;
8935 case CQE_CODE_RELEASE_WQE:
8936 /* Process the WQ release event */
8937 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008938 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008939 break;
8940 case CQE_CODE_XRI_ABORTED:
8941 /* Process the WQ XRI abort event */
8942 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008943 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008944 break;
James Smart4d9ab992009-10-02 15:16:39 -04008945 case CQE_CODE_RECEIVE:
8946 /* Process the RQ event */
8947 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008948 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008949 break;
James Smart4f774512009-05-22 14:52:35 -04008950 default:
8951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8952 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008953 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008954 break;
8955 }
8956 return workposted;
8957}
8958
8959/**
James Smart4f774512009-05-22 14:52:35 -04008960 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8961 * @phba: Pointer to HBA context object.
8962 * @eqe: Pointer to fast-path event queue entry.
8963 *
8964 * This routine process a event queue entry from the slow-path event queue.
8965 * It will check the MajorCode and MinorCode to determine this is for a
8966 * completion event on a completion queue, if not, an error shall be logged
8967 * and just return. Otherwise, it will get to the corresponding completion
8968 * queue and process all the entries on that completion queue, rearm the
8969 * completion queue, and then return.
8970 *
8971 **/
8972static void
8973lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8974{
8975 struct lpfc_queue *cq = NULL, *childq, *speq;
8976 struct lpfc_cqe *cqe;
8977 bool workposted = false;
8978 int ecount = 0;
8979 uint16_t cqid;
8980
James Smart28baac72010-02-12 14:42:03 -05008981 if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04008982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8983 "0359 Not a valid slow-path completion "
8984 "event: majorcode=x%x, minorcode=x%x\n",
8985 bf_get(lpfc_eqe_major_code, eqe),
8986 bf_get(lpfc_eqe_minor_code, eqe));
8987 return;
8988 }
8989
8990 /* Get the reference to the corresponding CQ */
8991 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8992
8993 /* Search for completion queue pointer matching this cqid */
8994 speq = phba->sli4_hba.sp_eq;
8995 list_for_each_entry(childq, &speq->child_list, list) {
8996 if (childq->queue_id == cqid) {
8997 cq = childq;
8998 break;
8999 }
9000 }
9001 if (unlikely(!cq)) {
9002 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9003 "0365 Slow-path CQ identifier (%d) does "
9004 "not exist\n", cqid);
9005 return;
9006 }
9007
9008 /* Process all the entries to the CQ */
9009 switch (cq->type) {
9010 case LPFC_MCQ:
9011 while ((cqe = lpfc_sli4_cq_get(cq))) {
9012 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9013 if (!(++ecount % LPFC_GET_QE_REL_INT))
9014 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9015 }
9016 break;
9017 case LPFC_WCQ:
9018 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009019 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009020 if (!(++ecount % LPFC_GET_QE_REL_INT))
9021 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9022 }
9023 break;
9024 default:
9025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9026 "0370 Invalid completion queue type (%d)\n",
9027 cq->type);
9028 return;
9029 }
9030
9031 /* Catch the no cq entry condition, log an error */
9032 if (unlikely(ecount == 0))
9033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9034 "0371 No entry from the CQ: identifier "
9035 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9036
9037 /* In any case, flash and re-arm the RCQ */
9038 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9039
9040 /* wake up worker thread if there are works to be done */
9041 if (workposted)
9042 lpfc_worker_wake_up(phba);
9043}
9044
9045/**
9046 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9047 * @eqe: Pointer to fast-path completion queue entry.
9048 *
9049 * This routine process a fast-path work queue completion entry from fast-path
9050 * event queue for FCP command response completion.
9051 **/
9052static void
9053lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9054 struct lpfc_wcqe_complete *wcqe)
9055{
9056 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9057 struct lpfc_iocbq *cmdiocbq;
9058 struct lpfc_iocbq irspiocbq;
9059 unsigned long iflags;
9060
9061 spin_lock_irqsave(&phba->hbalock, iflags);
9062 pring->stats.iocb_event++;
9063 spin_unlock_irqrestore(&phba->hbalock, iflags);
9064
9065 /* Check for response status */
9066 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9067 /* If resource errors reported from HBA, reduce queue
9068 * depth of the SCSI device.
9069 */
9070 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9071 IOSTAT_LOCAL_REJECT) &&
9072 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9073 phba->lpfc_rampdown_queue_depth(phba);
9074 }
9075 /* Log the error status */
9076 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9077 "0373 FCP complete error: status=x%x, "
9078 "hw_status=x%x, total_data_specified=%d, "
9079 "parameter=x%x, word3=x%x\n",
9080 bf_get(lpfc_wcqe_c_status, wcqe),
9081 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9082 wcqe->total_data_placed, wcqe->parameter,
9083 wcqe->word3);
9084 }
9085
9086 /* Look up the FCP command IOCB and create pseudo response IOCB */
9087 spin_lock_irqsave(&phba->hbalock, iflags);
9088 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9089 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9090 spin_unlock_irqrestore(&phba->hbalock, iflags);
9091 if (unlikely(!cmdiocbq)) {
9092 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9093 "0374 FCP complete with no corresponding "
9094 "cmdiocb: iotag (%d)\n",
9095 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9096 return;
9097 }
9098 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9099 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9100 "0375 FCP cmdiocb not callback function "
9101 "iotag: (%d)\n",
9102 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9103 return;
9104 }
9105
9106 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009107 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009108
James Smart0f65ff62010-02-26 14:14:23 -05009109 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9110 spin_lock_irqsave(&phba->hbalock, iflags);
9111 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9112 spin_unlock_irqrestore(&phba->hbalock, iflags);
9113 }
9114
James Smart4f774512009-05-22 14:52:35 -04009115 /* Pass the cmd_iocb and the rsp state to the upper layer */
9116 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9117}
9118
9119/**
9120 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9121 * @phba: Pointer to HBA context object.
9122 * @cq: Pointer to completion queue.
9123 * @wcqe: Pointer to work-queue completion queue entry.
9124 *
9125 * This routine handles an fast-path WQ entry comsumed event by invoking the
9126 * proper WQ release routine to the slow-path WQ.
9127 **/
9128static void
9129lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9130 struct lpfc_wcqe_release *wcqe)
9131{
9132 struct lpfc_queue *childwq;
9133 bool wqid_matched = false;
9134 uint16_t fcp_wqid;
9135
9136 /* Check for fast-path FCP work queue release */
9137 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9138 list_for_each_entry(childwq, &cq->child_list, list) {
9139 if (childwq->queue_id == fcp_wqid) {
9140 lpfc_sli4_wq_release(childwq,
9141 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9142 wqid_matched = true;
9143 break;
9144 }
9145 }
9146 /* Report warning log message if no match found */
9147 if (wqid_matched != true)
9148 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9149 "2580 Fast-path wqe consume event carries "
9150 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9151}
9152
9153/**
9154 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9155 * @cq: Pointer to the completion queue.
9156 * @eqe: Pointer to fast-path completion queue entry.
9157 *
9158 * This routine process a fast-path work queue completion entry from fast-path
9159 * event queue for FCP command response completion.
9160 **/
9161static int
9162lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9163 struct lpfc_cqe *cqe)
9164{
9165 struct lpfc_wcqe_release wcqe;
9166 bool workposted = false;
9167
9168 /* Copy the work queue CQE and convert endian order if needed */
9169 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9170
9171 /* Check and process for different type of WCQE and dispatch */
9172 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9173 case CQE_CODE_COMPL_WQE:
9174 /* Process the WQ complete event */
9175 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9176 (struct lpfc_wcqe_complete *)&wcqe);
9177 break;
9178 case CQE_CODE_RELEASE_WQE:
9179 /* Process the WQ release event */
9180 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9181 (struct lpfc_wcqe_release *)&wcqe);
9182 break;
9183 case CQE_CODE_XRI_ABORTED:
9184 /* Process the WQ XRI abort event */
9185 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9186 (struct sli4_wcqe_xri_aborted *)&wcqe);
9187 break;
9188 default:
9189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9190 "0144 Not a valid WCQE code: x%x\n",
9191 bf_get(lpfc_wcqe_c_code, &wcqe));
9192 break;
9193 }
9194 return workposted;
9195}
9196
9197/**
9198 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9199 * @phba: Pointer to HBA context object.
9200 * @eqe: Pointer to fast-path event queue entry.
9201 *
9202 * This routine process a event queue entry from the fast-path event queue.
9203 * It will check the MajorCode and MinorCode to determine this is for a
9204 * completion event on a completion queue, if not, an error shall be logged
9205 * and just return. Otherwise, it will get to the corresponding completion
9206 * queue and process all the entries on the completion queue, rearm the
9207 * completion queue, and then return.
9208 **/
9209static void
9210lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9211 uint32_t fcp_cqidx)
9212{
9213 struct lpfc_queue *cq;
9214 struct lpfc_cqe *cqe;
9215 bool workposted = false;
9216 uint16_t cqid;
9217 int ecount = 0;
9218
James Smart28baac72010-02-12 14:42:03 -05009219 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9221 "0366 Not a valid fast-path completion "
9222 "event: majorcode=x%x, minorcode=x%x\n",
9223 bf_get(lpfc_eqe_major_code, eqe),
9224 bf_get(lpfc_eqe_minor_code, eqe));
9225 return;
9226 }
9227
9228 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9229 if (unlikely(!cq)) {
9230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9231 "0367 Fast-path completion queue does not "
9232 "exist\n");
9233 return;
9234 }
9235
9236 /* Get the reference to the corresponding CQ */
9237 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9238 if (unlikely(cqid != cq->queue_id)) {
9239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9240 "0368 Miss-matched fast-path completion "
9241 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9242 cqid, cq->queue_id);
9243 return;
9244 }
9245
9246 /* Process all the entries to the CQ */
9247 while ((cqe = lpfc_sli4_cq_get(cq))) {
9248 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9249 if (!(++ecount % LPFC_GET_QE_REL_INT))
9250 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9251 }
9252
9253 /* Catch the no cq entry condition */
9254 if (unlikely(ecount == 0))
9255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9256 "0369 No entry from fast-path completion "
9257 "queue fcpcqid=%d\n", cq->queue_id);
9258
9259 /* In any case, flash and re-arm the CQ */
9260 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9261
9262 /* wake up worker thread if there are works to be done */
9263 if (workposted)
9264 lpfc_worker_wake_up(phba);
9265}
9266
9267static void
9268lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9269{
9270 struct lpfc_eqe *eqe;
9271
9272 /* walk all the EQ entries and drop on the floor */
9273 while ((eqe = lpfc_sli4_eq_get(eq)))
9274 ;
9275
9276 /* Clear and re-arm the EQ */
9277 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9278}
9279
9280/**
9281 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9282 * @irq: Interrupt number.
9283 * @dev_id: The device context pointer.
9284 *
9285 * This function is directly called from the PCI layer as an interrupt
9286 * service routine when device with SLI-4 interface spec is enabled with
9287 * MSI-X multi-message interrupt mode and there are slow-path events in
9288 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9289 * interrupt mode, this function is called as part of the device-level
9290 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9291 * undergoing initialization, the interrupt handler will not process the
9292 * interrupt. The link attention and ELS ring attention events are handled
9293 * by the worker thread. The interrupt handler signals the worker thread
9294 * and returns for these events. This function is called without any lock
9295 * held. It gets the hbalock to access and update SLI data structures.
9296 *
9297 * This function returns IRQ_HANDLED when interrupt is handled else it
9298 * returns IRQ_NONE.
9299 **/
9300irqreturn_t
9301lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9302{
9303 struct lpfc_hba *phba;
9304 struct lpfc_queue *speq;
9305 struct lpfc_eqe *eqe;
9306 unsigned long iflag;
9307 int ecount = 0;
9308
9309 /*
9310 * Get the driver's phba structure from the dev_id
9311 */
9312 phba = (struct lpfc_hba *)dev_id;
9313
9314 if (unlikely(!phba))
9315 return IRQ_NONE;
9316
9317 /* Get to the EQ struct associated with this vector */
9318 speq = phba->sli4_hba.sp_eq;
9319
9320 /* Check device state for handling interrupt */
9321 if (unlikely(lpfc_intr_state_check(phba))) {
9322 /* Check again for link_state with lock held */
9323 spin_lock_irqsave(&phba->hbalock, iflag);
9324 if (phba->link_state < LPFC_LINK_DOWN)
9325 /* Flush, clear interrupt, and rearm the EQ */
9326 lpfc_sli4_eq_flush(phba, speq);
9327 spin_unlock_irqrestore(&phba->hbalock, iflag);
9328 return IRQ_NONE;
9329 }
9330
9331 /*
9332 * Process all the event on FCP slow-path EQ
9333 */
9334 while ((eqe = lpfc_sli4_eq_get(speq))) {
9335 lpfc_sli4_sp_handle_eqe(phba, eqe);
9336 if (!(++ecount % LPFC_GET_QE_REL_INT))
9337 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9338 }
9339
9340 /* Always clear and re-arm the slow-path EQ */
9341 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9342
9343 /* Catch the no cq entry condition */
9344 if (unlikely(ecount == 0)) {
9345 if (phba->intr_type == MSIX)
9346 /* MSI-X treated interrupt served as no EQ share INT */
9347 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9348 "0357 MSI-X interrupt with no EQE\n");
9349 else
9350 /* Non MSI-X treated on interrupt as EQ share INT */
9351 return IRQ_NONE;
9352 }
9353
9354 return IRQ_HANDLED;
9355} /* lpfc_sli4_sp_intr_handler */
9356
9357/**
9358 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9359 * @irq: Interrupt number.
9360 * @dev_id: The device context pointer.
9361 *
9362 * This function is directly called from the PCI layer as an interrupt
9363 * service routine when device with SLI-4 interface spec is enabled with
9364 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9365 * ring event in the HBA. However, when the device is enabled with either
9366 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9367 * device-level interrupt handler. When the PCI slot is in error recovery
9368 * or the HBA is undergoing initialization, the interrupt handler will not
9369 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9370 * the intrrupt context. This function is called without any lock held.
9371 * It gets the hbalock to access and update SLI data structures. Note that,
9372 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9373 * equal to that of FCP CQ index.
9374 *
9375 * This function returns IRQ_HANDLED when interrupt is handled else it
9376 * returns IRQ_NONE.
9377 **/
9378irqreturn_t
9379lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9380{
9381 struct lpfc_hba *phba;
9382 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9383 struct lpfc_queue *fpeq;
9384 struct lpfc_eqe *eqe;
9385 unsigned long iflag;
9386 int ecount = 0;
9387 uint32_t fcp_eqidx;
9388
9389 /* Get the driver's phba structure from the dev_id */
9390 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9391 phba = fcp_eq_hdl->phba;
9392 fcp_eqidx = fcp_eq_hdl->idx;
9393
9394 if (unlikely(!phba))
9395 return IRQ_NONE;
9396
9397 /* Get to the EQ struct associated with this vector */
9398 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9399
9400 /* Check device state for handling interrupt */
9401 if (unlikely(lpfc_intr_state_check(phba))) {
9402 /* Check again for link_state with lock held */
9403 spin_lock_irqsave(&phba->hbalock, iflag);
9404 if (phba->link_state < LPFC_LINK_DOWN)
9405 /* Flush, clear interrupt, and rearm the EQ */
9406 lpfc_sli4_eq_flush(phba, fpeq);
9407 spin_unlock_irqrestore(&phba->hbalock, iflag);
9408 return IRQ_NONE;
9409 }
9410
9411 /*
9412 * Process all the event on FCP fast-path EQ
9413 */
9414 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9415 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9416 if (!(++ecount % LPFC_GET_QE_REL_INT))
9417 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9418 }
9419
9420 /* Always clear and re-arm the fast-path EQ */
9421 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9422
9423 if (unlikely(ecount == 0)) {
9424 if (phba->intr_type == MSIX)
9425 /* MSI-X treated interrupt served as no EQ share INT */
9426 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9427 "0358 MSI-X interrupt with no EQE\n");
9428 else
9429 /* Non MSI-X treated on interrupt as EQ share INT */
9430 return IRQ_NONE;
9431 }
9432
9433 return IRQ_HANDLED;
9434} /* lpfc_sli4_fp_intr_handler */
9435
9436/**
9437 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9438 * @irq: Interrupt number.
9439 * @dev_id: The device context pointer.
9440 *
9441 * This function is the device-level interrupt handler to device with SLI-4
9442 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9443 * interrupt mode is enabled and there is an event in the HBA which requires
9444 * driver attention. This function invokes the slow-path interrupt attention
9445 * handling function and fast-path interrupt attention handling function in
9446 * turn to process the relevant HBA attention events. This function is called
9447 * without any lock held. It gets the hbalock to access and update SLI data
9448 * structures.
9449 *
9450 * This function returns IRQ_HANDLED when interrupt is handled, else it
9451 * returns IRQ_NONE.
9452 **/
9453irqreturn_t
9454lpfc_sli4_intr_handler(int irq, void *dev_id)
9455{
9456 struct lpfc_hba *phba;
9457 irqreturn_t sp_irq_rc, fp_irq_rc;
9458 bool fp_handled = false;
9459 uint32_t fcp_eqidx;
9460
9461 /* Get the driver's phba structure from the dev_id */
9462 phba = (struct lpfc_hba *)dev_id;
9463
9464 if (unlikely(!phba))
9465 return IRQ_NONE;
9466
9467 /*
9468 * Invokes slow-path host attention interrupt handling as appropriate.
9469 */
9470 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9471
9472 /*
9473 * Invoke fast-path host attention interrupt handling as appropriate.
9474 */
9475 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9476 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9477 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9478 if (fp_irq_rc == IRQ_HANDLED)
9479 fp_handled |= true;
9480 }
9481
9482 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9483} /* lpfc_sli4_intr_handler */
9484
9485/**
9486 * lpfc_sli4_queue_free - free a queue structure and associated memory
9487 * @queue: The queue structure to free.
9488 *
9489 * This function frees a queue structure and the DMAable memeory used for
9490 * the host resident queue. This function must be called after destroying the
9491 * queue on the HBA.
9492 **/
9493void
9494lpfc_sli4_queue_free(struct lpfc_queue *queue)
9495{
9496 struct lpfc_dmabuf *dmabuf;
9497
9498 if (!queue)
9499 return;
9500
9501 while (!list_empty(&queue->page_list)) {
9502 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9503 list);
9504 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9505 dmabuf->virt, dmabuf->phys);
9506 kfree(dmabuf);
9507 }
9508 kfree(queue);
9509 return;
9510}
9511
9512/**
9513 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9514 * @phba: The HBA that this queue is being created on.
9515 * @entry_size: The size of each queue entry for this queue.
9516 * @entry count: The number of entries that this queue will handle.
9517 *
9518 * This function allocates a queue structure and the DMAable memory used for
9519 * the host resident queue. This function must be called before creating the
9520 * queue on the HBA.
9521 **/
9522struct lpfc_queue *
9523lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9524 uint32_t entry_count)
9525{
9526 struct lpfc_queue *queue;
9527 struct lpfc_dmabuf *dmabuf;
9528 int x, total_qe_count;
9529 void *dma_pointer;
9530
9531
9532 queue = kzalloc(sizeof(struct lpfc_queue) +
9533 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9534 if (!queue)
9535 return NULL;
9536 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9537 INIT_LIST_HEAD(&queue->list);
9538 INIT_LIST_HEAD(&queue->page_list);
9539 INIT_LIST_HEAD(&queue->child_list);
9540 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9541 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9542 if (!dmabuf)
9543 goto out_fail;
9544 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9545 PAGE_SIZE, &dmabuf->phys,
9546 GFP_KERNEL);
9547 if (!dmabuf->virt) {
9548 kfree(dmabuf);
9549 goto out_fail;
9550 }
James Smartd11e31d2009-06-10 17:23:06 -04009551 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009552 dmabuf->buffer_tag = x;
9553 list_add_tail(&dmabuf->list, &queue->page_list);
9554 /* initialize queue's entry array */
9555 dma_pointer = dmabuf->virt;
9556 for (; total_qe_count < entry_count &&
9557 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9558 total_qe_count++, dma_pointer += entry_size) {
9559 queue->qe[total_qe_count].address = dma_pointer;
9560 }
9561 }
9562 queue->entry_size = entry_size;
9563 queue->entry_count = entry_count;
9564 queue->phba = phba;
9565
9566 return queue;
9567out_fail:
9568 lpfc_sli4_queue_free(queue);
9569 return NULL;
9570}
9571
9572/**
9573 * lpfc_eq_create - Create an Event Queue on the HBA
9574 * @phba: HBA structure that indicates port to create a queue on.
9575 * @eq: The queue structure to use to create the event queue.
9576 * @imax: The maximum interrupt per second limit.
9577 *
9578 * This function creates an event queue, as detailed in @eq, on a port,
9579 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9580 *
9581 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9582 * is used to get the entry count and entry size that are necessary to
9583 * determine the number of pages to allocate and use for this queue. This
9584 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9585 * event queue. This function is asynchronous and will wait for the mailbox
9586 * command to finish before continuing.
9587 *
9588 * On success this function will return a zero. If unable to allocate enough
9589 * memory this function will return ENOMEM. If the queue create mailbox command
9590 * fails this function will return ENXIO.
9591 **/
9592uint32_t
9593lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9594{
9595 struct lpfc_mbx_eq_create *eq_create;
9596 LPFC_MBOXQ_t *mbox;
9597 int rc, length, status = 0;
9598 struct lpfc_dmabuf *dmabuf;
9599 uint32_t shdr_status, shdr_add_status;
9600 union lpfc_sli4_cfg_shdr *shdr;
9601 uint16_t dmult;
9602
9603 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9604 if (!mbox)
9605 return -ENOMEM;
9606 length = (sizeof(struct lpfc_mbx_eq_create) -
9607 sizeof(struct lpfc_sli4_cfg_mhdr));
9608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9609 LPFC_MBOX_OPCODE_EQ_CREATE,
9610 length, LPFC_SLI4_MBX_EMBED);
9611 eq_create = &mbox->u.mqe.un.eq_create;
9612 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9613 eq->page_count);
9614 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9615 LPFC_EQE_SIZE);
9616 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9617 /* Calculate delay multiper from maximum interrupt per second */
9618 dmult = LPFC_DMULT_CONST/imax - 1;
9619 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9620 dmult);
9621 switch (eq->entry_count) {
9622 default:
9623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9624 "0360 Unsupported EQ count. (%d)\n",
9625 eq->entry_count);
9626 if (eq->entry_count < 256)
9627 return -EINVAL;
9628 /* otherwise default to smallest count (drop through) */
9629 case 256:
9630 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9631 LPFC_EQ_CNT_256);
9632 break;
9633 case 512:
9634 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9635 LPFC_EQ_CNT_512);
9636 break;
9637 case 1024:
9638 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9639 LPFC_EQ_CNT_1024);
9640 break;
9641 case 2048:
9642 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9643 LPFC_EQ_CNT_2048);
9644 break;
9645 case 4096:
9646 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9647 LPFC_EQ_CNT_4096);
9648 break;
9649 }
9650 list_for_each_entry(dmabuf, &eq->page_list, list) {
9651 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9652 putPaddrLow(dmabuf->phys);
9653 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9654 putPaddrHigh(dmabuf->phys);
9655 }
9656 mbox->vport = phba->pport;
9657 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9658 mbox->context1 = NULL;
9659 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9660 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9661 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9662 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9663 if (shdr_status || shdr_add_status || rc) {
9664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9665 "2500 EQ_CREATE mailbox failed with "
9666 "status x%x add_status x%x, mbx status x%x\n",
9667 shdr_status, shdr_add_status, rc);
9668 status = -ENXIO;
9669 }
9670 eq->type = LPFC_EQ;
9671 eq->subtype = LPFC_NONE;
9672 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9673 if (eq->queue_id == 0xFFFF)
9674 status = -ENXIO;
9675 eq->host_index = 0;
9676 eq->hba_index = 0;
9677
James Smart8fa38512009-07-19 10:01:03 -04009678 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009679 return status;
9680}
9681
9682/**
9683 * lpfc_cq_create - Create a Completion Queue on the HBA
9684 * @phba: HBA structure that indicates port to create a queue on.
9685 * @cq: The queue structure to use to create the completion queue.
9686 * @eq: The event queue to bind this completion queue to.
9687 *
9688 * This function creates a completion queue, as detailed in @wq, on a port,
9689 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9690 *
9691 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9692 * is used to get the entry count and entry size that are necessary to
9693 * determine the number of pages to allocate and use for this queue. The @eq
9694 * is used to indicate which event queue to bind this completion queue to. This
9695 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9696 * completion queue. This function is asynchronous and will wait for the mailbox
9697 * command to finish before continuing.
9698 *
9699 * On success this function will return a zero. If unable to allocate enough
9700 * memory this function will return ENOMEM. If the queue create mailbox command
9701 * fails this function will return ENXIO.
9702 **/
9703uint32_t
9704lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9705 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9706{
9707 struct lpfc_mbx_cq_create *cq_create;
9708 struct lpfc_dmabuf *dmabuf;
9709 LPFC_MBOXQ_t *mbox;
9710 int rc, length, status = 0;
9711 uint32_t shdr_status, shdr_add_status;
9712 union lpfc_sli4_cfg_shdr *shdr;
9713
9714 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9715 if (!mbox)
9716 return -ENOMEM;
9717 length = (sizeof(struct lpfc_mbx_cq_create) -
9718 sizeof(struct lpfc_sli4_cfg_mhdr));
9719 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9720 LPFC_MBOX_OPCODE_CQ_CREATE,
9721 length, LPFC_SLI4_MBX_EMBED);
9722 cq_create = &mbox->u.mqe.un.cq_create;
9723 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9724 cq->page_count);
9725 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9726 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9727 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9728 switch (cq->entry_count) {
9729 default:
9730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9731 "0361 Unsupported CQ count. (%d)\n",
9732 cq->entry_count);
9733 if (cq->entry_count < 256)
9734 return -EINVAL;
9735 /* otherwise default to smallest count (drop through) */
9736 case 256:
9737 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9738 LPFC_CQ_CNT_256);
9739 break;
9740 case 512:
9741 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9742 LPFC_CQ_CNT_512);
9743 break;
9744 case 1024:
9745 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9746 LPFC_CQ_CNT_1024);
9747 break;
9748 }
9749 list_for_each_entry(dmabuf, &cq->page_list, list) {
9750 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9751 putPaddrLow(dmabuf->phys);
9752 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9753 putPaddrHigh(dmabuf->phys);
9754 }
9755 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9756
9757 /* The IOCTL status is embedded in the mailbox subheader. */
9758 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9759 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9760 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9761 if (shdr_status || shdr_add_status || rc) {
9762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9763 "2501 CQ_CREATE mailbox failed with "
9764 "status x%x add_status x%x, mbx status x%x\n",
9765 shdr_status, shdr_add_status, rc);
9766 status = -ENXIO;
9767 goto out;
9768 }
9769 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9770 if (cq->queue_id == 0xFFFF) {
9771 status = -ENXIO;
9772 goto out;
9773 }
9774 /* link the cq onto the parent eq child list */
9775 list_add_tail(&cq->list, &eq->child_list);
9776 /* Set up completion queue's type and subtype */
9777 cq->type = type;
9778 cq->subtype = subtype;
9779 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9780 cq->host_index = 0;
9781 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009782
James Smart8fa38512009-07-19 10:01:03 -04009783out:
9784 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009785 return status;
9786}
9787
9788/**
James Smart04c68492009-05-22 14:52:52 -04009789 * lpfc_mq_create - Create a mailbox Queue on the HBA
9790 * @phba: HBA structure that indicates port to create a queue on.
9791 * @mq: The queue structure to use to create the mailbox queue.
9792 *
9793 * This function creates a mailbox queue, as detailed in @mq, on a port,
9794 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9795 *
9796 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9797 * is used to get the entry count and entry size that are necessary to
9798 * determine the number of pages to allocate and use for this queue. This
9799 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9800 * mailbox queue. This function is asynchronous and will wait for the mailbox
9801 * command to finish before continuing.
9802 *
9803 * On success this function will return a zero. If unable to allocate enough
9804 * memory this function will return ENOMEM. If the queue create mailbox command
9805 * fails this function will return ENXIO.
9806 **/
9807uint32_t
9808lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9809 struct lpfc_queue *cq, uint32_t subtype)
9810{
9811 struct lpfc_mbx_mq_create *mq_create;
9812 struct lpfc_dmabuf *dmabuf;
9813 LPFC_MBOXQ_t *mbox;
9814 int rc, length, status = 0;
9815 uint32_t shdr_status, shdr_add_status;
9816 union lpfc_sli4_cfg_shdr *shdr;
9817
9818 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9819 if (!mbox)
9820 return -ENOMEM;
9821 length = (sizeof(struct lpfc_mbx_mq_create) -
9822 sizeof(struct lpfc_sli4_cfg_mhdr));
9823 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9824 LPFC_MBOX_OPCODE_MQ_CREATE,
9825 length, LPFC_SLI4_MBX_EMBED);
9826 mq_create = &mbox->u.mqe.un.mq_create;
9827 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9828 mq->page_count);
9829 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9830 cq->queue_id);
9831 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9832 switch (mq->entry_count) {
9833 default:
9834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9835 "0362 Unsupported MQ count. (%d)\n",
9836 mq->entry_count);
9837 if (mq->entry_count < 16)
9838 return -EINVAL;
9839 /* otherwise default to smallest count (drop through) */
9840 case 16:
9841 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9842 LPFC_MQ_CNT_16);
9843 break;
9844 case 32:
9845 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9846 LPFC_MQ_CNT_32);
9847 break;
9848 case 64:
9849 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9850 LPFC_MQ_CNT_64);
9851 break;
9852 case 128:
9853 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9854 LPFC_MQ_CNT_128);
9855 break;
9856 }
9857 list_for_each_entry(dmabuf, &mq->page_list, list) {
9858 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9859 putPaddrLow(dmabuf->phys);
9860 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9861 putPaddrHigh(dmabuf->phys);
9862 }
9863 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9864 /* The IOCTL status is embedded in the mailbox subheader. */
9865 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9868 if (shdr_status || shdr_add_status || rc) {
9869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9870 "2502 MQ_CREATE mailbox failed with "
9871 "status x%x add_status x%x, mbx status x%x\n",
9872 shdr_status, shdr_add_status, rc);
9873 status = -ENXIO;
9874 goto out;
9875 }
9876 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9877 if (mq->queue_id == 0xFFFF) {
9878 status = -ENXIO;
9879 goto out;
9880 }
9881 mq->type = LPFC_MQ;
9882 mq->subtype = subtype;
9883 mq->host_index = 0;
9884 mq->hba_index = 0;
9885
9886 /* link the mq onto the parent cq child list */
9887 list_add_tail(&mq->list, &cq->child_list);
9888out:
James Smart8fa38512009-07-19 10:01:03 -04009889 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009890 return status;
9891}
9892
9893/**
James Smart4f774512009-05-22 14:52:35 -04009894 * lpfc_wq_create - Create a Work Queue on the HBA
9895 * @phba: HBA structure that indicates port to create a queue on.
9896 * @wq: The queue structure to use to create the work queue.
9897 * @cq: The completion queue to bind this work queue to.
9898 * @subtype: The subtype of the work queue indicating its functionality.
9899 *
9900 * This function creates a work queue, as detailed in @wq, on a port, described
9901 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9902 *
9903 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9904 * is used to get the entry count and entry size that are necessary to
9905 * determine the number of pages to allocate and use for this queue. The @cq
9906 * is used to indicate which completion queue to bind this work queue to. This
9907 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9908 * work queue. This function is asynchronous and will wait for the mailbox
9909 * command to finish before continuing.
9910 *
9911 * On success this function will return a zero. If unable to allocate enough
9912 * memory this function will return ENOMEM. If the queue create mailbox command
9913 * fails this function will return ENXIO.
9914 **/
9915uint32_t
9916lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9917 struct lpfc_queue *cq, uint32_t subtype)
9918{
9919 struct lpfc_mbx_wq_create *wq_create;
9920 struct lpfc_dmabuf *dmabuf;
9921 LPFC_MBOXQ_t *mbox;
9922 int rc, length, status = 0;
9923 uint32_t shdr_status, shdr_add_status;
9924 union lpfc_sli4_cfg_shdr *shdr;
9925
9926 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9927 if (!mbox)
9928 return -ENOMEM;
9929 length = (sizeof(struct lpfc_mbx_wq_create) -
9930 sizeof(struct lpfc_sli4_cfg_mhdr));
9931 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9932 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9933 length, LPFC_SLI4_MBX_EMBED);
9934 wq_create = &mbox->u.mqe.un.wq_create;
9935 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9936 wq->page_count);
9937 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9938 cq->queue_id);
9939 list_for_each_entry(dmabuf, &wq->page_list, list) {
9940 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9941 putPaddrLow(dmabuf->phys);
9942 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9943 putPaddrHigh(dmabuf->phys);
9944 }
9945 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9946 /* The IOCTL status is embedded in the mailbox subheader. */
9947 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9948 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9949 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9950 if (shdr_status || shdr_add_status || rc) {
9951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9952 "2503 WQ_CREATE mailbox failed with "
9953 "status x%x add_status x%x, mbx status x%x\n",
9954 shdr_status, shdr_add_status, rc);
9955 status = -ENXIO;
9956 goto out;
9957 }
9958 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9959 if (wq->queue_id == 0xFFFF) {
9960 status = -ENXIO;
9961 goto out;
9962 }
9963 wq->type = LPFC_WQ;
9964 wq->subtype = subtype;
9965 wq->host_index = 0;
9966 wq->hba_index = 0;
9967
9968 /* link the wq onto the parent cq child list */
9969 list_add_tail(&wq->list, &cq->child_list);
9970out:
James Smart8fa38512009-07-19 10:01:03 -04009971 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009972 return status;
9973}
9974
9975/**
9976 * lpfc_rq_create - Create a Receive Queue on the HBA
9977 * @phba: HBA structure that indicates port to create a queue on.
9978 * @hrq: The queue structure to use to create the header receive queue.
9979 * @drq: The queue structure to use to create the data receive queue.
9980 * @cq: The completion queue to bind this work queue to.
9981 *
9982 * This function creates a receive buffer queue pair , as detailed in @hrq and
9983 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9984 * to the HBA.
9985 *
9986 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9987 * struct is used to get the entry count that is necessary to determine the
9988 * number of pages to use for this queue. The @cq is used to indicate which
9989 * completion queue to bind received buffers that are posted to these queues to.
9990 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9991 * receive queue pair. This function is asynchronous and will wait for the
9992 * mailbox command to finish before continuing.
9993 *
9994 * On success this function will return a zero. If unable to allocate enough
9995 * memory this function will return ENOMEM. If the queue create mailbox command
9996 * fails this function will return ENXIO.
9997 **/
9998uint32_t
9999lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10000 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10001{
10002 struct lpfc_mbx_rq_create *rq_create;
10003 struct lpfc_dmabuf *dmabuf;
10004 LPFC_MBOXQ_t *mbox;
10005 int rc, length, status = 0;
10006 uint32_t shdr_status, shdr_add_status;
10007 union lpfc_sli4_cfg_shdr *shdr;
10008
10009 if (hrq->entry_count != drq->entry_count)
10010 return -EINVAL;
10011 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10012 if (!mbox)
10013 return -ENOMEM;
10014 length = (sizeof(struct lpfc_mbx_rq_create) -
10015 sizeof(struct lpfc_sli4_cfg_mhdr));
10016 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10017 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10018 length, LPFC_SLI4_MBX_EMBED);
10019 rq_create = &mbox->u.mqe.un.rq_create;
10020 switch (hrq->entry_count) {
10021 default:
10022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10023 "2535 Unsupported RQ count. (%d)\n",
10024 hrq->entry_count);
10025 if (hrq->entry_count < 512)
10026 return -EINVAL;
10027 /* otherwise default to smallest count (drop through) */
10028 case 512:
10029 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10030 LPFC_RQ_RING_SIZE_512);
10031 break;
10032 case 1024:
10033 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10034 LPFC_RQ_RING_SIZE_1024);
10035 break;
10036 case 2048:
10037 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10038 LPFC_RQ_RING_SIZE_2048);
10039 break;
10040 case 4096:
10041 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10042 LPFC_RQ_RING_SIZE_4096);
10043 break;
10044 }
10045 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10046 cq->queue_id);
10047 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10048 hrq->page_count);
10049 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10050 LPFC_HDR_BUF_SIZE);
10051 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10052 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10053 putPaddrLow(dmabuf->phys);
10054 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10055 putPaddrHigh(dmabuf->phys);
10056 }
10057 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10058 /* The IOCTL status is embedded in the mailbox subheader. */
10059 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10060 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10061 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10062 if (shdr_status || shdr_add_status || rc) {
10063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10064 "2504 RQ_CREATE mailbox failed with "
10065 "status x%x add_status x%x, mbx status x%x\n",
10066 shdr_status, shdr_add_status, rc);
10067 status = -ENXIO;
10068 goto out;
10069 }
10070 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10071 if (hrq->queue_id == 0xFFFF) {
10072 status = -ENXIO;
10073 goto out;
10074 }
10075 hrq->type = LPFC_HRQ;
10076 hrq->subtype = subtype;
10077 hrq->host_index = 0;
10078 hrq->hba_index = 0;
10079
10080 /* now create the data queue */
10081 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10082 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10083 length, LPFC_SLI4_MBX_EMBED);
10084 switch (drq->entry_count) {
10085 default:
10086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10087 "2536 Unsupported RQ count. (%d)\n",
10088 drq->entry_count);
10089 if (drq->entry_count < 512)
10090 return -EINVAL;
10091 /* otherwise default to smallest count (drop through) */
10092 case 512:
10093 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10094 LPFC_RQ_RING_SIZE_512);
10095 break;
10096 case 1024:
10097 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10098 LPFC_RQ_RING_SIZE_1024);
10099 break;
10100 case 2048:
10101 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10102 LPFC_RQ_RING_SIZE_2048);
10103 break;
10104 case 4096:
10105 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10106 LPFC_RQ_RING_SIZE_4096);
10107 break;
10108 }
10109 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10110 cq->queue_id);
10111 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10112 drq->page_count);
10113 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10114 LPFC_DATA_BUF_SIZE);
10115 list_for_each_entry(dmabuf, &drq->page_list, list) {
10116 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10117 putPaddrLow(dmabuf->phys);
10118 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10119 putPaddrHigh(dmabuf->phys);
10120 }
10121 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10122 /* The IOCTL status is embedded in the mailbox subheader. */
10123 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10126 if (shdr_status || shdr_add_status || rc) {
10127 status = -ENXIO;
10128 goto out;
10129 }
10130 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10131 if (drq->queue_id == 0xFFFF) {
10132 status = -ENXIO;
10133 goto out;
10134 }
10135 drq->type = LPFC_DRQ;
10136 drq->subtype = subtype;
10137 drq->host_index = 0;
10138 drq->hba_index = 0;
10139
10140 /* link the header and data RQs onto the parent cq child list */
10141 list_add_tail(&hrq->list, &cq->child_list);
10142 list_add_tail(&drq->list, &cq->child_list);
10143
10144out:
James Smart8fa38512009-07-19 10:01:03 -040010145 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010146 return status;
10147}
10148
10149/**
10150 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10151 * @eq: The queue structure associated with the queue to destroy.
10152 *
10153 * This function destroys a queue, as detailed in @eq by sending an mailbox
10154 * command, specific to the type of queue, to the HBA.
10155 *
10156 * The @eq struct is used to get the queue ID of the queue to destroy.
10157 *
10158 * On success this function will return a zero. If the queue destroy mailbox
10159 * command fails this function will return ENXIO.
10160 **/
10161uint32_t
10162lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10163{
10164 LPFC_MBOXQ_t *mbox;
10165 int rc, length, status = 0;
10166 uint32_t shdr_status, shdr_add_status;
10167 union lpfc_sli4_cfg_shdr *shdr;
10168
10169 if (!eq)
10170 return -ENODEV;
10171 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10172 if (!mbox)
10173 return -ENOMEM;
10174 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10175 sizeof(struct lpfc_sli4_cfg_mhdr));
10176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10177 LPFC_MBOX_OPCODE_EQ_DESTROY,
10178 length, LPFC_SLI4_MBX_EMBED);
10179 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10180 eq->queue_id);
10181 mbox->vport = eq->phba->pport;
10182 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10183
10184 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10185 /* The IOCTL status is embedded in the mailbox subheader. */
10186 shdr = (union lpfc_sli4_cfg_shdr *)
10187 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10188 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10189 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10190 if (shdr_status || shdr_add_status || rc) {
10191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10192 "2505 EQ_DESTROY mailbox failed with "
10193 "status x%x add_status x%x, mbx status x%x\n",
10194 shdr_status, shdr_add_status, rc);
10195 status = -ENXIO;
10196 }
10197
10198 /* Remove eq from any list */
10199 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010200 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010201 return status;
10202}
10203
10204/**
10205 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10206 * @cq: The queue structure associated with the queue to destroy.
10207 *
10208 * This function destroys a queue, as detailed in @cq by sending an mailbox
10209 * command, specific to the type of queue, to the HBA.
10210 *
10211 * The @cq struct is used to get the queue ID of the queue to destroy.
10212 *
10213 * On success this function will return a zero. If the queue destroy mailbox
10214 * command fails this function will return ENXIO.
10215 **/
10216uint32_t
10217lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10218{
10219 LPFC_MBOXQ_t *mbox;
10220 int rc, length, status = 0;
10221 uint32_t shdr_status, shdr_add_status;
10222 union lpfc_sli4_cfg_shdr *shdr;
10223
10224 if (!cq)
10225 return -ENODEV;
10226 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10227 if (!mbox)
10228 return -ENOMEM;
10229 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10230 sizeof(struct lpfc_sli4_cfg_mhdr));
10231 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10232 LPFC_MBOX_OPCODE_CQ_DESTROY,
10233 length, LPFC_SLI4_MBX_EMBED);
10234 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10235 cq->queue_id);
10236 mbox->vport = cq->phba->pport;
10237 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10238 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10239 /* The IOCTL status is embedded in the mailbox subheader. */
10240 shdr = (union lpfc_sli4_cfg_shdr *)
10241 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10242 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10243 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10244 if (shdr_status || shdr_add_status || rc) {
10245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10246 "2506 CQ_DESTROY mailbox failed with "
10247 "status x%x add_status x%x, mbx status x%x\n",
10248 shdr_status, shdr_add_status, rc);
10249 status = -ENXIO;
10250 }
10251 /* Remove cq from any list */
10252 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010253 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010254 return status;
10255}
10256
10257/**
James Smart04c68492009-05-22 14:52:52 -040010258 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10259 * @qm: The queue structure associated with the queue to destroy.
10260 *
10261 * This function destroys a queue, as detailed in @mq by sending an mailbox
10262 * command, specific to the type of queue, to the HBA.
10263 *
10264 * The @mq struct is used to get the queue ID of the queue to destroy.
10265 *
10266 * On success this function will return a zero. If the queue destroy mailbox
10267 * command fails this function will return ENXIO.
10268 **/
10269uint32_t
10270lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10271{
10272 LPFC_MBOXQ_t *mbox;
10273 int rc, length, status = 0;
10274 uint32_t shdr_status, shdr_add_status;
10275 union lpfc_sli4_cfg_shdr *shdr;
10276
10277 if (!mq)
10278 return -ENODEV;
10279 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10280 if (!mbox)
10281 return -ENOMEM;
10282 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10283 sizeof(struct lpfc_sli4_cfg_mhdr));
10284 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10285 LPFC_MBOX_OPCODE_MQ_DESTROY,
10286 length, LPFC_SLI4_MBX_EMBED);
10287 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10288 mq->queue_id);
10289 mbox->vport = mq->phba->pport;
10290 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10291 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10292 /* The IOCTL status is embedded in the mailbox subheader. */
10293 shdr = (union lpfc_sli4_cfg_shdr *)
10294 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10295 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10296 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10297 if (shdr_status || shdr_add_status || rc) {
10298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10299 "2507 MQ_DESTROY mailbox failed with "
10300 "status x%x add_status x%x, mbx status x%x\n",
10301 shdr_status, shdr_add_status, rc);
10302 status = -ENXIO;
10303 }
10304 /* Remove mq from any list */
10305 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010306 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010307 return status;
10308}
10309
10310/**
James Smart4f774512009-05-22 14:52:35 -040010311 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10312 * @wq: The queue structure associated with the queue to destroy.
10313 *
10314 * This function destroys a queue, as detailed in @wq by sending an mailbox
10315 * command, specific to the type of queue, to the HBA.
10316 *
10317 * The @wq struct is used to get the queue ID of the queue to destroy.
10318 *
10319 * On success this function will return a zero. If the queue destroy mailbox
10320 * command fails this function will return ENXIO.
10321 **/
10322uint32_t
10323lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10324{
10325 LPFC_MBOXQ_t *mbox;
10326 int rc, length, status = 0;
10327 uint32_t shdr_status, shdr_add_status;
10328 union lpfc_sli4_cfg_shdr *shdr;
10329
10330 if (!wq)
10331 return -ENODEV;
10332 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10333 if (!mbox)
10334 return -ENOMEM;
10335 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10336 sizeof(struct lpfc_sli4_cfg_mhdr));
10337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10338 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10339 length, LPFC_SLI4_MBX_EMBED);
10340 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10341 wq->queue_id);
10342 mbox->vport = wq->phba->pport;
10343 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10344 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10345 shdr = (union lpfc_sli4_cfg_shdr *)
10346 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10347 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10348 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10349 if (shdr_status || shdr_add_status || rc) {
10350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10351 "2508 WQ_DESTROY mailbox failed with "
10352 "status x%x add_status x%x, mbx status x%x\n",
10353 shdr_status, shdr_add_status, rc);
10354 status = -ENXIO;
10355 }
10356 /* Remove wq from any list */
10357 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010358 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010359 return status;
10360}
10361
10362/**
10363 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10364 * @rq: The queue structure associated with the queue to destroy.
10365 *
10366 * This function destroys a queue, as detailed in @rq by sending an mailbox
10367 * command, specific to the type of queue, to the HBA.
10368 *
10369 * The @rq struct is used to get the queue ID of the queue to destroy.
10370 *
10371 * On success this function will return a zero. If the queue destroy mailbox
10372 * command fails this function will return ENXIO.
10373 **/
10374uint32_t
10375lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10376 struct lpfc_queue *drq)
10377{
10378 LPFC_MBOXQ_t *mbox;
10379 int rc, length, status = 0;
10380 uint32_t shdr_status, shdr_add_status;
10381 union lpfc_sli4_cfg_shdr *shdr;
10382
10383 if (!hrq || !drq)
10384 return -ENODEV;
10385 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10386 if (!mbox)
10387 return -ENOMEM;
10388 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10389 sizeof(struct mbox_header));
10390 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10391 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10392 length, LPFC_SLI4_MBX_EMBED);
10393 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10394 hrq->queue_id);
10395 mbox->vport = hrq->phba->pport;
10396 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10397 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10398 /* The IOCTL status is embedded in the mailbox subheader. */
10399 shdr = (union lpfc_sli4_cfg_shdr *)
10400 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10403 if (shdr_status || shdr_add_status || rc) {
10404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10405 "2509 RQ_DESTROY mailbox failed with "
10406 "status x%x add_status x%x, mbx status x%x\n",
10407 shdr_status, shdr_add_status, rc);
10408 if (rc != MBX_TIMEOUT)
10409 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10410 return -ENXIO;
10411 }
10412 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10413 drq->queue_id);
10414 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10415 shdr = (union lpfc_sli4_cfg_shdr *)
10416 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10419 if (shdr_status || shdr_add_status || rc) {
10420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10421 "2510 RQ_DESTROY mailbox failed with "
10422 "status x%x add_status x%x, mbx status x%x\n",
10423 shdr_status, shdr_add_status, rc);
10424 status = -ENXIO;
10425 }
10426 list_del_init(&hrq->list);
10427 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010428 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010429 return status;
10430}
10431
10432/**
10433 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10434 * @phba: The virtual port for which this call being executed.
10435 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10436 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10437 * @xritag: the xritag that ties this io to the SGL pages.
10438 *
10439 * This routine will post the sgl pages for the IO that has the xritag
10440 * that is in the iocbq structure. The xritag is assigned during iocbq
10441 * creation and persists for as long as the driver is loaded.
10442 * if the caller has fewer than 256 scatter gather segments to map then
10443 * pdma_phys_addr1 should be 0.
10444 * If the caller needs to map more than 256 scatter gather segment then
10445 * pdma_phys_addr1 should be a valid physical address.
10446 * physical address for SGLs must be 64 byte aligned.
10447 * If you are going to map 2 SGL's then the first one must have 256 entries
10448 * the second sgl can have between 1 and 256 entries.
10449 *
10450 * Return codes:
10451 * 0 - Success
10452 * -ENXIO, -ENOMEM - Failure
10453 **/
10454int
10455lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10456 dma_addr_t pdma_phys_addr0,
10457 dma_addr_t pdma_phys_addr1,
10458 uint16_t xritag)
10459{
10460 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10461 LPFC_MBOXQ_t *mbox;
10462 int rc;
10463 uint32_t shdr_status, shdr_add_status;
10464 union lpfc_sli4_cfg_shdr *shdr;
10465
10466 if (xritag == NO_XRI) {
10467 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10468 "0364 Invalid param:\n");
10469 return -EINVAL;
10470 }
10471
10472 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10473 if (!mbox)
10474 return -ENOMEM;
10475
10476 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10477 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10478 sizeof(struct lpfc_mbx_post_sgl_pages) -
10479 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10480
10481 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10482 &mbox->u.mqe.un.post_sgl_pages;
10483 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10484 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10485
10486 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10487 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10488 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10489 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10490
10491 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10492 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10493 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10494 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10495 if (!phba->sli4_hba.intr_enable)
10496 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10497 else
10498 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10499 /* The IOCTL status is embedded in the mailbox subheader. */
10500 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10503 if (rc != MBX_TIMEOUT)
10504 mempool_free(mbox, phba->mbox_mem_pool);
10505 if (shdr_status || shdr_add_status || rc) {
10506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10507 "2511 POST_SGL mailbox failed with "
10508 "status x%x add_status x%x, mbx status x%x\n",
10509 shdr_status, shdr_add_status, rc);
10510 rc = -ENXIO;
10511 }
10512 return 0;
10513}
10514/**
10515 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10516 * @phba: The virtual port for which this call being executed.
10517 *
10518 * This routine will remove all of the sgl pages registered with the hba.
10519 *
10520 * Return codes:
10521 * 0 - Success
10522 * -ENXIO, -ENOMEM - Failure
10523 **/
10524int
10525lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10526{
10527 LPFC_MBOXQ_t *mbox;
10528 int rc;
10529 uint32_t shdr_status, shdr_add_status;
10530 union lpfc_sli4_cfg_shdr *shdr;
10531
10532 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10533 if (!mbox)
10534 return -ENOMEM;
10535
10536 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10537 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10538 LPFC_SLI4_MBX_EMBED);
10539 if (!phba->sli4_hba.intr_enable)
10540 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10541 else
10542 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10543 /* The IOCTL status is embedded in the mailbox subheader. */
10544 shdr = (union lpfc_sli4_cfg_shdr *)
10545 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10546 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10547 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10548 if (rc != MBX_TIMEOUT)
10549 mempool_free(mbox, phba->mbox_mem_pool);
10550 if (shdr_status || shdr_add_status || rc) {
10551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10552 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10553 "status x%x add_status x%x, mbx status x%x\n",
10554 shdr_status, shdr_add_status, rc);
10555 rc = -ENXIO;
10556 }
10557 return rc;
10558}
10559
10560/**
10561 * lpfc_sli4_next_xritag - Get an xritag for the io
10562 * @phba: Pointer to HBA context object.
10563 *
10564 * This function gets an xritag for the iocb. If there is no unused xritag
10565 * it will return 0xffff.
10566 * The function returns the allocated xritag if successful, else returns zero.
10567 * Zero is not a valid xritag.
10568 * The caller is not required to hold any lock.
10569 **/
10570uint16_t
10571lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10572{
10573 uint16_t xritag;
10574
10575 spin_lock_irq(&phba->hbalock);
10576 xritag = phba->sli4_hba.next_xri;
10577 if ((xritag != (uint16_t) -1) && xritag <
10578 (phba->sli4_hba.max_cfg_param.max_xri
10579 + phba->sli4_hba.max_cfg_param.xri_base)) {
10580 phba->sli4_hba.next_xri++;
10581 phba->sli4_hba.max_cfg_param.xri_used++;
10582 spin_unlock_irq(&phba->hbalock);
10583 return xritag;
10584 }
10585 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010586 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010587 "2004 Failed to allocate XRI.last XRITAG is %d"
10588 " Max XRI is %d, Used XRI is %d\n",
10589 phba->sli4_hba.next_xri,
10590 phba->sli4_hba.max_cfg_param.max_xri,
10591 phba->sli4_hba.max_cfg_param.xri_used);
10592 return -1;
10593}
10594
10595/**
10596 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10597 * @phba: pointer to lpfc hba data structure.
10598 *
10599 * This routine is invoked to post a block of driver's sgl pages to the
10600 * HBA using non-embedded mailbox command. No Lock is held. This routine
10601 * is only called when the driver is loading and after all IO has been
10602 * stopped.
10603 **/
10604int
10605lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10606{
10607 struct lpfc_sglq *sglq_entry;
10608 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10609 struct sgl_page_pairs *sgl_pg_pairs;
10610 void *viraddr;
10611 LPFC_MBOXQ_t *mbox;
10612 uint32_t reqlen, alloclen, pg_pairs;
10613 uint32_t mbox_tmo;
10614 uint16_t xritag_start = 0;
10615 int els_xri_cnt, rc = 0;
10616 uint32_t shdr_status, shdr_add_status;
10617 union lpfc_sli4_cfg_shdr *shdr;
10618
10619 /* The number of sgls to be posted */
10620 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10621
10622 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10623 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10624 if (reqlen > PAGE_SIZE) {
10625 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10626 "2559 Block sgl registration required DMA "
10627 "size (%d) great than a page\n", reqlen);
10628 return -ENOMEM;
10629 }
10630 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10631 if (!mbox) {
10632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10633 "2560 Failed to allocate mbox cmd memory\n");
10634 return -ENOMEM;
10635 }
10636
10637 /* Allocate DMA memory and set up the non-embedded mailbox command */
10638 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10639 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10640 LPFC_SLI4_MBX_NEMBED);
10641
10642 if (alloclen < reqlen) {
10643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10644 "0285 Allocated DMA memory size (%d) is "
10645 "less than the requested DMA memory "
10646 "size (%d)\n", alloclen, reqlen);
10647 lpfc_sli4_mbox_cmd_free(phba, mbox);
10648 return -ENOMEM;
10649 }
James Smart4f774512009-05-22 14:52:35 -040010650 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010651 viraddr = mbox->sge_array->addr[0];
10652
10653 /* Set up the SGL pages in the non-embedded DMA pages */
10654 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10655 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10656
10657 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10658 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10659 /* Set up the sge entry */
10660 sgl_pg_pairs->sgl_pg0_addr_lo =
10661 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10662 sgl_pg_pairs->sgl_pg0_addr_hi =
10663 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10664 sgl_pg_pairs->sgl_pg1_addr_lo =
10665 cpu_to_le32(putPaddrLow(0));
10666 sgl_pg_pairs->sgl_pg1_addr_hi =
10667 cpu_to_le32(putPaddrHigh(0));
10668 /* Keep the first xritag on the list */
10669 if (pg_pairs == 0)
10670 xritag_start = sglq_entry->sli4_xritag;
10671 sgl_pg_pairs++;
10672 }
10673 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010674 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010675 /* Perform endian conversion if necessary */
10676 sgl->word0 = cpu_to_le32(sgl->word0);
10677
10678 if (!phba->sli4_hba.intr_enable)
10679 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10680 else {
10681 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10682 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10683 }
10684 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10685 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10686 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10687 if (rc != MBX_TIMEOUT)
10688 lpfc_sli4_mbox_cmd_free(phba, mbox);
10689 if (shdr_status || shdr_add_status || rc) {
10690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10691 "2513 POST_SGL_BLOCK mailbox command failed "
10692 "status x%x add_status x%x mbx status x%x\n",
10693 shdr_status, shdr_add_status, rc);
10694 rc = -ENXIO;
10695 }
10696 return rc;
10697}
10698
10699/**
10700 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10701 * @phba: pointer to lpfc hba data structure.
10702 * @sblist: pointer to scsi buffer list.
10703 * @count: number of scsi buffers on the list.
10704 *
10705 * This routine is invoked to post a block of @count scsi sgl pages from a
10706 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10707 * No Lock is held.
10708 *
10709 **/
10710int
10711lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10712 int cnt)
10713{
10714 struct lpfc_scsi_buf *psb;
10715 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10716 struct sgl_page_pairs *sgl_pg_pairs;
10717 void *viraddr;
10718 LPFC_MBOXQ_t *mbox;
10719 uint32_t reqlen, alloclen, pg_pairs;
10720 uint32_t mbox_tmo;
10721 uint16_t xritag_start = 0;
10722 int rc = 0;
10723 uint32_t shdr_status, shdr_add_status;
10724 dma_addr_t pdma_phys_bpl1;
10725 union lpfc_sli4_cfg_shdr *shdr;
10726
10727 /* Calculate the requested length of the dma memory */
10728 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10729 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10730 if (reqlen > PAGE_SIZE) {
10731 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10732 "0217 Block sgl registration required DMA "
10733 "size (%d) great than a page\n", reqlen);
10734 return -ENOMEM;
10735 }
10736 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10737 if (!mbox) {
10738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10739 "0283 Failed to allocate mbox cmd memory\n");
10740 return -ENOMEM;
10741 }
10742
10743 /* Allocate DMA memory and set up the non-embedded mailbox command */
10744 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10745 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10746 LPFC_SLI4_MBX_NEMBED);
10747
10748 if (alloclen < reqlen) {
10749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10750 "2561 Allocated DMA memory size (%d) is "
10751 "less than the requested DMA memory "
10752 "size (%d)\n", alloclen, reqlen);
10753 lpfc_sli4_mbox_cmd_free(phba, mbox);
10754 return -ENOMEM;
10755 }
James Smart4f774512009-05-22 14:52:35 -040010756 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010757 viraddr = mbox->sge_array->addr[0];
10758
10759 /* Set up the SGL pages in the non-embedded DMA pages */
10760 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10761 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10762
10763 pg_pairs = 0;
10764 list_for_each_entry(psb, sblist, list) {
10765 /* Set up the sge entry */
10766 sgl_pg_pairs->sgl_pg0_addr_lo =
10767 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10768 sgl_pg_pairs->sgl_pg0_addr_hi =
10769 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10770 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10771 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10772 else
10773 pdma_phys_bpl1 = 0;
10774 sgl_pg_pairs->sgl_pg1_addr_lo =
10775 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10776 sgl_pg_pairs->sgl_pg1_addr_hi =
10777 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10778 /* Keep the first xritag on the list */
10779 if (pg_pairs == 0)
10780 xritag_start = psb->cur_iocbq.sli4_xritag;
10781 sgl_pg_pairs++;
10782 pg_pairs++;
10783 }
10784 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10785 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10786 /* Perform endian conversion if necessary */
10787 sgl->word0 = cpu_to_le32(sgl->word0);
10788
10789 if (!phba->sli4_hba.intr_enable)
10790 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10791 else {
10792 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10793 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10794 }
10795 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10798 if (rc != MBX_TIMEOUT)
10799 lpfc_sli4_mbox_cmd_free(phba, mbox);
10800 if (shdr_status || shdr_add_status || rc) {
10801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10802 "2564 POST_SGL_BLOCK mailbox command failed "
10803 "status x%x add_status x%x mbx status x%x\n",
10804 shdr_status, shdr_add_status, rc);
10805 rc = -ENXIO;
10806 }
10807 return rc;
10808}
10809
10810/**
10811 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10812 * @phba: pointer to lpfc_hba struct that the frame was received on
10813 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10814 *
10815 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10816 * valid type of frame that the LPFC driver will handle. This function will
10817 * return a zero if the frame is a valid frame or a non zero value when the
10818 * frame does not pass the check.
10819 **/
10820static int
10821lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10822{
10823 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10824 char *type_names[] = FC_TYPE_NAMES_INIT;
10825 struct fc_vft_header *fc_vft_hdr;
10826
10827 switch (fc_hdr->fh_r_ctl) {
10828 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10829 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10830 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10831 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10832 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10833 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10834 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10835 case FC_RCTL_DD_CMD_STATUS: /* command status */
10836 case FC_RCTL_ELS_REQ: /* extended link services request */
10837 case FC_RCTL_ELS_REP: /* extended link services reply */
10838 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10839 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10840 case FC_RCTL_BA_NOP: /* basic link service NOP */
10841 case FC_RCTL_BA_ABTS: /* basic link service abort */
10842 case FC_RCTL_BA_RMC: /* remove connection */
10843 case FC_RCTL_BA_ACC: /* basic accept */
10844 case FC_RCTL_BA_RJT: /* basic reject */
10845 case FC_RCTL_BA_PRMT:
10846 case FC_RCTL_ACK_1: /* acknowledge_1 */
10847 case FC_RCTL_ACK_0: /* acknowledge_0 */
10848 case FC_RCTL_P_RJT: /* port reject */
10849 case FC_RCTL_F_RJT: /* fabric reject */
10850 case FC_RCTL_P_BSY: /* port busy */
10851 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10852 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10853 case FC_RCTL_LCR: /* link credit reset */
10854 case FC_RCTL_END: /* end */
10855 break;
10856 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10857 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10858 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10859 return lpfc_fc_frame_check(phba, fc_hdr);
10860 default:
10861 goto drop;
10862 }
10863 switch (fc_hdr->fh_type) {
10864 case FC_TYPE_BLS:
10865 case FC_TYPE_ELS:
10866 case FC_TYPE_FCP:
10867 case FC_TYPE_CT:
10868 break;
10869 case FC_TYPE_IP:
10870 case FC_TYPE_ILS:
10871 default:
10872 goto drop;
10873 }
10874 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10875 "2538 Received frame rctl:%s type:%s\n",
10876 rctl_names[fc_hdr->fh_r_ctl],
10877 type_names[fc_hdr->fh_type]);
10878 return 0;
10879drop:
10880 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10881 "2539 Dropped frame rctl:%s type:%s\n",
10882 rctl_names[fc_hdr->fh_r_ctl],
10883 type_names[fc_hdr->fh_type]);
10884 return 1;
10885}
10886
10887/**
10888 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10889 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10890 *
10891 * This function processes the FC header to retrieve the VFI from the VF
10892 * header, if one exists. This function will return the VFI if one exists
10893 * or 0 if no VSAN Header exists.
10894 **/
10895static uint32_t
10896lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10897{
10898 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10899
10900 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10901 return 0;
10902 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10903}
10904
10905/**
10906 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10907 * @phba: Pointer to the HBA structure to search for the vport on
10908 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10909 * @fcfi: The FC Fabric ID that the frame came from
10910 *
10911 * This function searches the @phba for a vport that matches the content of the
10912 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10913 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10914 * returns the matching vport pointer or NULL if unable to match frame to a
10915 * vport.
10916 **/
10917static struct lpfc_vport *
10918lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10919 uint16_t fcfi)
10920{
10921 struct lpfc_vport **vports;
10922 struct lpfc_vport *vport = NULL;
10923 int i;
10924 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10925 fc_hdr->fh_d_id[1] << 8 |
10926 fc_hdr->fh_d_id[2]);
10927
10928 vports = lpfc_create_vport_work_array(phba);
10929 if (vports != NULL)
10930 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10931 if (phba->fcf.fcfi == fcfi &&
10932 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10933 vports[i]->fc_myDID == did) {
10934 vport = vports[i];
10935 break;
10936 }
10937 }
10938 lpfc_destroy_vport_work_array(phba, vports);
10939 return vport;
10940}
10941
10942/**
James Smart45ed1192009-10-02 15:17:02 -040010943 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10944 * @vport: The vport to work on.
10945 *
10946 * This function updates the receive sequence time stamp for this vport. The
10947 * receive sequence time stamp indicates the time that the last frame of the
10948 * the sequence that has been idle for the longest amount of time was received.
10949 * the driver uses this time stamp to indicate if any received sequences have
10950 * timed out.
10951 **/
10952void
10953lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10954{
10955 struct lpfc_dmabuf *h_buf;
10956 struct hbq_dmabuf *dmabuf = NULL;
10957
10958 /* get the oldest sequence on the rcv list */
10959 h_buf = list_get_first(&vport->rcv_buffer_list,
10960 struct lpfc_dmabuf, list);
10961 if (!h_buf)
10962 return;
10963 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10964 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10965}
10966
10967/**
10968 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10969 * @vport: The vport that the received sequences were sent to.
10970 *
10971 * This function cleans up all outstanding received sequences. This is called
10972 * by the driver when a link event or user action invalidates all the received
10973 * sequences.
10974 **/
10975void
10976lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10977{
10978 struct lpfc_dmabuf *h_buf, *hnext;
10979 struct lpfc_dmabuf *d_buf, *dnext;
10980 struct hbq_dmabuf *dmabuf = NULL;
10981
10982 /* start with the oldest sequence on the rcv list */
10983 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10984 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10985 list_del_init(&dmabuf->hbuf.list);
10986 list_for_each_entry_safe(d_buf, dnext,
10987 &dmabuf->dbuf.list, list) {
10988 list_del_init(&d_buf->list);
10989 lpfc_in_buf_free(vport->phba, d_buf);
10990 }
10991 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10992 }
10993}
10994
10995/**
10996 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10997 * @vport: The vport that the received sequences were sent to.
10998 *
10999 * This function determines whether any received sequences have timed out by
11000 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11001 * indicates that there is at least one timed out sequence this routine will
11002 * go through the received sequences one at a time from most inactive to most
11003 * active to determine which ones need to be cleaned up. Once it has determined
11004 * that a sequence needs to be cleaned up it will simply free up the resources
11005 * without sending an abort.
11006 **/
11007void
11008lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11009{
11010 struct lpfc_dmabuf *h_buf, *hnext;
11011 struct lpfc_dmabuf *d_buf, *dnext;
11012 struct hbq_dmabuf *dmabuf = NULL;
11013 unsigned long timeout;
11014 int abort_count = 0;
11015
11016 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11017 vport->rcv_buffer_time_stamp);
11018 if (list_empty(&vport->rcv_buffer_list) ||
11019 time_before(jiffies, timeout))
11020 return;
11021 /* start with the oldest sequence on the rcv list */
11022 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11023 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11024 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11025 dmabuf->time_stamp);
11026 if (time_before(jiffies, timeout))
11027 break;
11028 abort_count++;
11029 list_del_init(&dmabuf->hbuf.list);
11030 list_for_each_entry_safe(d_buf, dnext,
11031 &dmabuf->dbuf.list, list) {
11032 list_del_init(&d_buf->list);
11033 lpfc_in_buf_free(vport->phba, d_buf);
11034 }
11035 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11036 }
11037 if (abort_count)
11038 lpfc_update_rcv_time_stamp(vport);
11039}
11040
11041/**
James Smart4f774512009-05-22 14:52:35 -040011042 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11043 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11044 *
11045 * This function searches through the existing incomplete sequences that have
11046 * been sent to this @vport. If the frame matches one of the incomplete
11047 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11048 * make up that sequence. If no sequence is found that matches this frame then
11049 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11050 * This function returns a pointer to the first dmabuf in the sequence list that
11051 * the frame was linked to.
11052 **/
11053static struct hbq_dmabuf *
11054lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11055{
11056 struct fc_frame_header *new_hdr;
11057 struct fc_frame_header *temp_hdr;
11058 struct lpfc_dmabuf *d_buf;
11059 struct lpfc_dmabuf *h_buf;
11060 struct hbq_dmabuf *seq_dmabuf = NULL;
11061 struct hbq_dmabuf *temp_dmabuf = NULL;
11062
James Smart4d9ab992009-10-02 15:16:39 -040011063 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011064 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011065 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11066 /* Use the hdr_buf to find the sequence that this frame belongs to */
11067 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11068 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11069 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11070 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11071 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11072 continue;
11073 /* found a pending sequence that matches this frame */
11074 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11075 break;
11076 }
11077 if (!seq_dmabuf) {
11078 /*
11079 * This indicates first frame received for this sequence.
11080 * Queue the buffer on the vport's rcv_buffer_list.
11081 */
11082 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011083 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011084 return dmabuf;
11085 }
11086 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011087 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11088 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011089 list_del_init(&seq_dmabuf->hbuf.list);
11090 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11091 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011092 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011093 return dmabuf;
11094 }
James Smart45ed1192009-10-02 15:17:02 -040011095 /* move this sequence to the tail to indicate a young sequence */
11096 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11097 seq_dmabuf->time_stamp = jiffies;
11098 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011099 if (list_empty(&seq_dmabuf->dbuf.list)) {
11100 temp_hdr = dmabuf->hbuf.virt;
11101 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11102 return seq_dmabuf;
11103 }
James Smart4f774512009-05-22 14:52:35 -040011104 /* find the correct place in the sequence to insert this frame */
11105 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11106 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11107 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11108 /*
11109 * If the frame's sequence count is greater than the frame on
11110 * the list then insert the frame right after this frame
11111 */
James Smarteeead812009-12-21 17:01:23 -050011112 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11113 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011114 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11115 return seq_dmabuf;
11116 }
11117 }
11118 return NULL;
11119}
11120
11121/**
James Smart6669f9b2009-10-02 15:16:45 -040011122 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11123 * @vport: pointer to a vitural port
11124 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11125 *
11126 * This function tries to abort from the partially assembed sequence, described
11127 * by the information from basic abbort @dmabuf. It checks to see whether such
11128 * partially assembled sequence held by the driver. If so, it shall free up all
11129 * the frames from the partially assembled sequence.
11130 *
11131 * Return
11132 * true -- if there is matching partially assembled sequence present and all
11133 * the frames freed with the sequence;
11134 * false -- if there is no matching partially assembled sequence present so
11135 * nothing got aborted in the lower layer driver
11136 **/
11137static bool
11138lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11139 struct hbq_dmabuf *dmabuf)
11140{
11141 struct fc_frame_header *new_hdr;
11142 struct fc_frame_header *temp_hdr;
11143 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11144 struct hbq_dmabuf *seq_dmabuf = NULL;
11145
11146 /* Use the hdr_buf to find the sequence that matches this frame */
11147 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11148 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11149 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11150 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11151 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11152 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11153 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11154 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11155 continue;
11156 /* found a pending sequence that matches this frame */
11157 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11158 break;
11159 }
11160
11161 /* Free up all the frames from the partially assembled sequence */
11162 if (seq_dmabuf) {
11163 list_for_each_entry_safe(d_buf, n_buf,
11164 &seq_dmabuf->dbuf.list, list) {
11165 list_del_init(&d_buf->list);
11166 lpfc_in_buf_free(vport->phba, d_buf);
11167 }
11168 return true;
11169 }
11170 return false;
11171}
11172
11173/**
11174 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11175 * @phba: Pointer to HBA context object.
11176 * @cmd_iocbq: pointer to the command iocbq structure.
11177 * @rsp_iocbq: pointer to the response iocbq structure.
11178 *
11179 * This function handles the sequence abort accept iocb command complete
11180 * event. It properly releases the memory allocated to the sequence abort
11181 * accept iocb.
11182 **/
11183static void
11184lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11185 struct lpfc_iocbq *cmd_iocbq,
11186 struct lpfc_iocbq *rsp_iocbq)
11187{
11188 if (cmd_iocbq)
11189 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11190}
11191
11192/**
11193 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11194 * @phba: Pointer to HBA context object.
11195 * @fc_hdr: pointer to a FC frame header.
11196 *
11197 * This function sends a basic accept to a previous unsol sequence abort
11198 * event after aborting the sequence handling.
11199 **/
11200static void
11201lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11202 struct fc_frame_header *fc_hdr)
11203{
11204 struct lpfc_iocbq *ctiocb = NULL;
11205 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011206 uint16_t oxid, rxid;
11207 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011208 IOCB_t *icmd;
11209
11210 if (!lpfc_is_link_up(phba))
11211 return;
11212
11213 sid = sli4_sid_from_fc_hdr(fc_hdr);
11214 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011215 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011216
11217 ndlp = lpfc_findnode_did(phba->pport, sid);
11218 if (!ndlp) {
11219 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11220 "1268 Find ndlp returned NULL for oxid:x%x "
11221 "SID:x%x\n", oxid, sid);
11222 return;
11223 }
11224
11225 /* Allocate buffer for acc iocb */
11226 ctiocb = lpfc_sli_get_iocbq(phba);
11227 if (!ctiocb)
11228 return;
11229
James Smart5ffc2662009-11-18 15:39:44 -050011230 /* Extract the F_CTL field from FC_HDR */
11231 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11232
James Smart6669f9b2009-10-02 15:16:45 -040011233 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011234 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011235 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011236 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11237 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11238 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11239
11240 /* Fill in the rest of iocb fields */
11241 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11242 icmd->ulpBdeCount = 0;
11243 icmd->ulpLe = 1;
11244 icmd->ulpClass = CLASS3;
11245 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011246
James Smart6669f9b2009-10-02 15:16:45 -040011247 ctiocb->iocb_cmpl = NULL;
11248 ctiocb->vport = phba->pport;
11249 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11250
James Smart5ffc2662009-11-18 15:39:44 -050011251 if (fctl & FC_FC_EX_CTX) {
11252 /* ABTS sent by responder to CT exchange, construction
11253 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11254 * field and RX_ID from ABTS for RX_ID field.
11255 */
11256 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11257 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11258 ctiocb->sli4_xritag = oxid;
11259 } else {
11260 /* ABTS sent by initiator to CT exchange, construction
11261 * of BA_ACC will need to allocate a new XRI as for the
11262 * XRI_TAG and RX_ID fields.
11263 */
11264 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11265 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11266 ctiocb->sli4_xritag = NO_XRI;
11267 }
11268 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11269
James Smart6669f9b2009-10-02 15:16:45 -040011270 /* Xmit CT abts accept on exchange <xid> */
11271 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11272 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11273 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11274 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11275}
11276
11277/**
11278 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11279 * @vport: Pointer to the vport on which this sequence was received
11280 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11281 *
11282 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11283 * receive sequence is only partially assembed by the driver, it shall abort
11284 * the partially assembled frames for the sequence. Otherwise, if the
11285 * unsolicited receive sequence has been completely assembled and passed to
11286 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11287 * unsolicited sequence has been aborted. After that, it will issue a basic
11288 * accept to accept the abort.
11289 **/
11290void
11291lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11292 struct hbq_dmabuf *dmabuf)
11293{
11294 struct lpfc_hba *phba = vport->phba;
11295 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011296 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011297 bool abts_par;
11298
James Smart6669f9b2009-10-02 15:16:45 -040011299 /* Make a copy of fc_hdr before the dmabuf being released */
11300 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011301 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011302
James Smart5ffc2662009-11-18 15:39:44 -050011303 if (fctl & FC_FC_EX_CTX) {
11304 /*
11305 * ABTS sent by responder to exchange, just free the buffer
11306 */
James Smart6669f9b2009-10-02 15:16:45 -040011307 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011308 } else {
11309 /*
11310 * ABTS sent by initiator to exchange, need to do cleanup
11311 */
11312 /* Try to abort partially assembled seq */
11313 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11314
11315 /* Send abort to ULP if partially seq abort failed */
11316 if (abts_par == false)
11317 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11318 else
11319 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11320 }
James Smart6669f9b2009-10-02 15:16:45 -040011321 /* Send basic accept (BA_ACC) to the abort requester */
11322 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11323}
11324
11325/**
James Smart4f774512009-05-22 14:52:35 -040011326 * lpfc_seq_complete - Indicates if a sequence is complete
11327 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11328 *
11329 * This function checks the sequence, starting with the frame described by
11330 * @dmabuf, to see if all the frames associated with this sequence are present.
11331 * the frames associated with this sequence are linked to the @dmabuf using the
11332 * dbuf list. This function looks for two major things. 1) That the first frame
11333 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11334 * set. 3) That there are no holes in the sequence count. The function will
11335 * return 1 when the sequence is complete, otherwise it will return 0.
11336 **/
11337static int
11338lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11339{
11340 struct fc_frame_header *hdr;
11341 struct lpfc_dmabuf *d_buf;
11342 struct hbq_dmabuf *seq_dmabuf;
11343 uint32_t fctl;
11344 int seq_count = 0;
11345
11346 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11347 /* make sure first fame of sequence has a sequence count of zero */
11348 if (hdr->fh_seq_cnt != seq_count)
11349 return 0;
11350 fctl = (hdr->fh_f_ctl[0] << 16 |
11351 hdr->fh_f_ctl[1] << 8 |
11352 hdr->fh_f_ctl[2]);
11353 /* If last frame of sequence we can return success. */
11354 if (fctl & FC_FC_END_SEQ)
11355 return 1;
11356 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11357 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11358 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11359 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011360 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011361 return 0;
11362 fctl = (hdr->fh_f_ctl[0] << 16 |
11363 hdr->fh_f_ctl[1] << 8 |
11364 hdr->fh_f_ctl[2]);
11365 /* If last frame of sequence we can return success. */
11366 if (fctl & FC_FC_END_SEQ)
11367 return 1;
11368 }
11369 return 0;
11370}
11371
11372/**
11373 * lpfc_prep_seq - Prep sequence for ULP processing
11374 * @vport: Pointer to the vport on which this sequence was received
11375 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11376 *
11377 * This function takes a sequence, described by a list of frames, and creates
11378 * a list of iocbq structures to describe the sequence. This iocbq list will be
11379 * used to issue to the generic unsolicited sequence handler. This routine
11380 * returns a pointer to the first iocbq in the list. If the function is unable
11381 * to allocate an iocbq then it throw out the received frames that were not
11382 * able to be described and return a pointer to the first iocbq. If unable to
11383 * allocate any iocbqs (including the first) this function will return NULL.
11384 **/
11385static struct lpfc_iocbq *
11386lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11387{
11388 struct lpfc_dmabuf *d_buf, *n_buf;
11389 struct lpfc_iocbq *first_iocbq, *iocbq;
11390 struct fc_frame_header *fc_hdr;
11391 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011392 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011393
11394 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11395 /* remove from receive buffer list */
11396 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011397 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011398 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011399 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011400 /* Get an iocbq struct to fill in. */
11401 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11402 if (first_iocbq) {
11403 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011404 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011405 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11406 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11407 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11408 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11409 vport->vpi + vport->phba->vpi_base;
11410 /* put the first buffer into the first IOCBq */
11411 first_iocbq->context2 = &seq_dmabuf->dbuf;
11412 first_iocbq->context3 = NULL;
11413 first_iocbq->iocb.ulpBdeCount = 1;
11414 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11415 LPFC_DATA_BUF_SIZE;
11416 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011417 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011418 bf_get(lpfc_rcqe_length,
11419 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011420 }
11421 iocbq = first_iocbq;
11422 /*
11423 * Each IOCBq can have two Buffers assigned, so go through the list
11424 * of buffers for this sequence and save two buffers in each IOCBq
11425 */
11426 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11427 if (!iocbq) {
11428 lpfc_in_buf_free(vport->phba, d_buf);
11429 continue;
11430 }
11431 if (!iocbq->context3) {
11432 iocbq->context3 = d_buf;
11433 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011434 pbde = (struct ulp_bde64 *)
11435 &iocbq->iocb.unsli3.sli3Words[4];
11436 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011437 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011438 bf_get(lpfc_rcqe_length,
11439 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011440 } else {
11441 iocbq = lpfc_sli_get_iocbq(vport->phba);
11442 if (!iocbq) {
11443 if (first_iocbq) {
11444 first_iocbq->iocb.ulpStatus =
11445 IOSTAT_FCP_RSP_ERROR;
11446 first_iocbq->iocb.un.ulpWord[4] =
11447 IOERR_NO_RESOURCES;
11448 }
11449 lpfc_in_buf_free(vport->phba, d_buf);
11450 continue;
11451 }
11452 iocbq->context2 = d_buf;
11453 iocbq->context3 = NULL;
11454 iocbq->iocb.ulpBdeCount = 1;
11455 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11456 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011457 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011458 bf_get(lpfc_rcqe_length,
11459 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011460 iocbq->iocb.un.rcvels.remoteID = sid;
11461 list_add_tail(&iocbq->list, &first_iocbq->list);
11462 }
11463 }
11464 return first_iocbq;
11465}
11466
James Smart6669f9b2009-10-02 15:16:45 -040011467static void
11468lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11469 struct hbq_dmabuf *seq_dmabuf)
11470{
11471 struct fc_frame_header *fc_hdr;
11472 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11473 struct lpfc_hba *phba = vport->phba;
11474
11475 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11476 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11477 if (!iocbq) {
11478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11479 "2707 Ring %d handler: Failed to allocate "
11480 "iocb Rctl x%x Type x%x received\n",
11481 LPFC_ELS_RING,
11482 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11483 return;
11484 }
11485 if (!lpfc_complete_unsol_iocb(phba,
11486 &phba->sli.ring[LPFC_ELS_RING],
11487 iocbq, fc_hdr->fh_r_ctl,
11488 fc_hdr->fh_type))
11489 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11490 "2540 Ring %d handler: unexpected Rctl "
11491 "x%x Type x%x received\n",
11492 LPFC_ELS_RING,
11493 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11494
11495 /* Free iocb created in lpfc_prep_seq */
11496 list_for_each_entry_safe(curr_iocb, next_iocb,
11497 &iocbq->list, list) {
11498 list_del_init(&curr_iocb->list);
11499 lpfc_sli_release_iocbq(phba, curr_iocb);
11500 }
11501 lpfc_sli_release_iocbq(phba, iocbq);
11502}
11503
James Smart4f774512009-05-22 14:52:35 -040011504/**
11505 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11506 * @phba: Pointer to HBA context object.
11507 *
11508 * This function is called with no lock held. This function processes all
11509 * the received buffers and gives it to upper layers when a received buffer
11510 * indicates that it is the final frame in the sequence. The interrupt
11511 * service routine processes received buffers at interrupt contexts and adds
11512 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11513 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11514 * appropriate receive function when the final frame in a sequence is received.
11515 **/
James Smart4d9ab992009-10-02 15:16:39 -040011516void
11517lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11518 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011519{
James Smart4d9ab992009-10-02 15:16:39 -040011520 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011521 struct fc_frame_header *fc_hdr;
11522 struct lpfc_vport *vport;
11523 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011524
James Smart4f774512009-05-22 14:52:35 -040011525 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011526 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11527 /* check to see if this a valid type of frame */
11528 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11529 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11530 return;
11531 }
11532 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11533 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011534 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011535 /* throw out the frame */
11536 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11537 return;
11538 }
James Smart6669f9b2009-10-02 15:16:45 -040011539 /* Handle the basic abort sequence (BA_ABTS) event */
11540 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11541 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11542 return;
11543 }
11544
James Smart4d9ab992009-10-02 15:16:39 -040011545 /* Link this frame */
11546 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11547 if (!seq_dmabuf) {
11548 /* unable to add frame to vport - throw it out */
11549 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11550 return;
11551 }
11552 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011553 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011554 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011555
James Smart6669f9b2009-10-02 15:16:45 -040011556 /* Send the complete sequence to the upper layer protocol */
11557 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011558}
James Smart6fb120a2009-05-22 14:52:59 -040011559
11560/**
11561 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11562 * @phba: pointer to lpfc hba data structure.
11563 *
11564 * This routine is invoked to post rpi header templates to the
11565 * HBA consistent with the SLI-4 interface spec. This routine
11566 * posts a PAGE_SIZE memory region to the port to hold up to
11567 * PAGE_SIZE modulo 64 rpi context headers.
11568 *
11569 * This routine does not require any locks. It's usage is expected
11570 * to be driver load or reset recovery when the driver is
11571 * sequential.
11572 *
11573 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011574 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011575 * EIO - The mailbox failed to complete successfully.
11576 * When this error occurs, the driver is not guaranteed
11577 * to have any rpi regions posted to the device and
11578 * must either attempt to repost the regions or take a
11579 * fatal error.
11580 **/
11581int
11582lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11583{
11584 struct lpfc_rpi_hdr *rpi_page;
11585 uint32_t rc = 0;
11586
11587 /* Post all rpi memory regions to the port. */
11588 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11589 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11590 if (rc != MBX_SUCCESS) {
11591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11592 "2008 Error %d posting all rpi "
11593 "headers\n", rc);
11594 rc = -EIO;
11595 break;
11596 }
11597 }
11598
11599 return rc;
11600}
11601
11602/**
11603 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11604 * @phba: pointer to lpfc hba data structure.
11605 * @rpi_page: pointer to the rpi memory region.
11606 *
11607 * This routine is invoked to post a single rpi header to the
11608 * HBA consistent with the SLI-4 interface spec. This memory region
11609 * maps up to 64 rpi context regions.
11610 *
11611 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011612 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011613 * ENOMEM - No available memory
11614 * EIO - The mailbox failed to complete successfully.
11615 **/
11616int
11617lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11618{
11619 LPFC_MBOXQ_t *mboxq;
11620 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11621 uint32_t rc = 0;
11622 uint32_t mbox_tmo;
11623 uint32_t shdr_status, shdr_add_status;
11624 union lpfc_sli4_cfg_shdr *shdr;
11625
11626 /* The port is notified of the header region via a mailbox command. */
11627 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11628 if (!mboxq) {
11629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11630 "2001 Unable to allocate memory for issuing "
11631 "SLI_CONFIG_SPECIAL mailbox command\n");
11632 return -ENOMEM;
11633 }
11634
11635 /* Post all rpi memory regions to the port. */
11636 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11637 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11638 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11639 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11640 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11641 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11642 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11643 hdr_tmpl, rpi_page->page_count);
11644 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11645 rpi_page->start_rpi);
11646 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11647 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011648 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011649 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11650 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11651 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11652 if (rc != MBX_TIMEOUT)
11653 mempool_free(mboxq, phba->mbox_mem_pool);
11654 if (shdr_status || shdr_add_status || rc) {
11655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11656 "2514 POST_RPI_HDR mailbox failed with "
11657 "status x%x add_status x%x, mbx status x%x\n",
11658 shdr_status, shdr_add_status, rc);
11659 rc = -ENXIO;
11660 }
11661 return rc;
11662}
11663
11664/**
11665 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11666 * @phba: pointer to lpfc hba data structure.
11667 *
11668 * This routine is invoked to post rpi header templates to the
11669 * HBA consistent with the SLI-4 interface spec. This routine
11670 * posts a PAGE_SIZE memory region to the port to hold up to
11671 * PAGE_SIZE modulo 64 rpi context headers.
11672 *
11673 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011674 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011675 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11676 **/
11677int
11678lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11679{
11680 int rpi;
11681 uint16_t max_rpi, rpi_base, rpi_limit;
11682 uint16_t rpi_remaining;
11683 struct lpfc_rpi_hdr *rpi_hdr;
11684
11685 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11686 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11687 rpi_limit = phba->sli4_hba.next_rpi;
11688
11689 /*
11690 * The valid rpi range is not guaranteed to be zero-based. Start
11691 * the search at the rpi_base as reported by the port.
11692 */
11693 spin_lock_irq(&phba->hbalock);
11694 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11695 if (rpi >= rpi_limit || rpi < rpi_base)
11696 rpi = LPFC_RPI_ALLOC_ERROR;
11697 else {
11698 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11699 phba->sli4_hba.max_cfg_param.rpi_used++;
11700 phba->sli4_hba.rpi_count++;
11701 }
11702
11703 /*
11704 * Don't try to allocate more rpi header regions if the device limit
11705 * on available rpis max has been exhausted.
11706 */
11707 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11708 (phba->sli4_hba.rpi_count >= max_rpi)) {
11709 spin_unlock_irq(&phba->hbalock);
11710 return rpi;
11711 }
11712
11713 /*
11714 * If the driver is running low on rpi resources, allocate another
11715 * page now. Note that the next_rpi value is used because
11716 * it represents how many are actually in use whereas max_rpi notes
11717 * how many are supported max by the device.
11718 */
11719 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11720 phba->sli4_hba.rpi_count;
11721 spin_unlock_irq(&phba->hbalock);
11722 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11723 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11724 if (!rpi_hdr) {
11725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11726 "2002 Error Could not grow rpi "
11727 "count\n");
11728 } else {
11729 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11730 }
11731 }
11732
11733 return rpi;
11734}
11735
11736/**
11737 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11738 * @phba: pointer to lpfc hba data structure.
11739 *
11740 * This routine is invoked to release an rpi to the pool of
11741 * available rpis maintained by the driver.
11742 **/
11743void
11744lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11745{
11746 spin_lock_irq(&phba->hbalock);
11747 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11748 phba->sli4_hba.rpi_count--;
11749 phba->sli4_hba.max_cfg_param.rpi_used--;
11750 spin_unlock_irq(&phba->hbalock);
11751}
11752
11753/**
11754 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11755 * @phba: pointer to lpfc hba data structure.
11756 *
11757 * This routine is invoked to remove the memory region that
11758 * provided rpi via a bitmask.
11759 **/
11760void
11761lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11762{
11763 kfree(phba->sli4_hba.rpi_bmask);
11764}
11765
11766/**
11767 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11768 * @phba: pointer to lpfc hba data structure.
11769 *
11770 * This routine is invoked to remove the memory region that
11771 * provided rpi via a bitmask.
11772 **/
11773int
11774lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11775{
11776 LPFC_MBOXQ_t *mboxq;
11777 struct lpfc_hba *phba = ndlp->phba;
11778 int rc;
11779
11780 /* The port is notified of the header region via a mailbox command. */
11781 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11782 if (!mboxq)
11783 return -ENOMEM;
11784
11785 /* Post all rpi memory regions to the port. */
11786 lpfc_resume_rpi(mboxq, ndlp);
11787 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11788 if (rc == MBX_NOT_FINISHED) {
11789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11790 "2010 Resume RPI Mailbox failed "
11791 "status %d, mbxStatus x%x\n", rc,
11792 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11793 mempool_free(mboxq, phba->mbox_mem_pool);
11794 return -EIO;
11795 }
11796 return 0;
11797}
11798
11799/**
11800 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11801 * @phba: pointer to lpfc hba data structure.
11802 * @vpi: vpi value to activate with the port.
11803 *
11804 * This routine is invoked to activate a vpi with the
11805 * port when the host intends to use vports with a
11806 * nonzero vpi.
11807 *
11808 * Returns:
11809 * 0 success
11810 * -Evalue otherwise
11811 **/
11812int
11813lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11814{
11815 LPFC_MBOXQ_t *mboxq;
11816 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011817 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011818 uint32_t mbox_tmo;
11819
11820 if (vpi == 0)
11821 return -EINVAL;
11822 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11823 if (!mboxq)
11824 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011825 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011826 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11827 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011828 if (rc != MBX_SUCCESS) {
11829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11830 "2022 INIT VPI Mailbox failed "
11831 "status %d, mbxStatus x%x\n", rc,
11832 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011833 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011834 }
James Smart6a9c52c2009-10-02 15:16:51 -040011835 if (rc != MBX_TIMEOUT)
11836 mempool_free(mboxq, phba->mbox_mem_pool);
11837
11838 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011839}
11840
11841/**
11842 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11843 * @phba: pointer to lpfc hba data structure.
11844 * @mboxq: Pointer to mailbox object.
11845 *
11846 * This routine is invoked to manually add a single FCF record. The caller
11847 * must pass a completely initialized FCF_Record. This routine takes
11848 * care of the nonembedded mailbox operations.
11849 **/
11850static void
11851lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11852{
11853 void *virt_addr;
11854 union lpfc_sli4_cfg_shdr *shdr;
11855 uint32_t shdr_status, shdr_add_status;
11856
11857 virt_addr = mboxq->sge_array->addr[0];
11858 /* The IOCTL status is embedded in the mailbox subheader. */
11859 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11860 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11861 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11862
11863 if ((shdr_status || shdr_add_status) &&
11864 (shdr_status != STATUS_FCF_IN_USE))
11865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11866 "2558 ADD_FCF_RECORD mailbox failed with "
11867 "status x%x add_status x%x\n",
11868 shdr_status, shdr_add_status);
11869
11870 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11871}
11872
11873/**
11874 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11875 * @phba: pointer to lpfc hba data structure.
11876 * @fcf_record: pointer to the initialized fcf record to add.
11877 *
11878 * This routine is invoked to manually add a single FCF record. The caller
11879 * must pass a completely initialized FCF_Record. This routine takes
11880 * care of the nonembedded mailbox operations.
11881 **/
11882int
11883lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11884{
11885 int rc = 0;
11886 LPFC_MBOXQ_t *mboxq;
11887 uint8_t *bytep;
11888 void *virt_addr;
11889 dma_addr_t phys_addr;
11890 struct lpfc_mbx_sge sge;
11891 uint32_t alloc_len, req_len;
11892 uint32_t fcfindex;
11893
11894 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11895 if (!mboxq) {
11896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11897 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11898 return -ENOMEM;
11899 }
11900
11901 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11902 sizeof(uint32_t);
11903
11904 /* Allocate DMA memory and set up the non-embedded mailbox command */
11905 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11906 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11907 req_len, LPFC_SLI4_MBX_NEMBED);
11908 if (alloc_len < req_len) {
11909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11910 "2523 Allocated DMA memory size (x%x) is "
11911 "less than the requested DMA memory "
11912 "size (x%x)\n", alloc_len, req_len);
11913 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11914 return -ENOMEM;
11915 }
11916
11917 /*
11918 * Get the first SGE entry from the non-embedded DMA memory. This
11919 * routine only uses a single SGE.
11920 */
11921 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11922 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011923 virt_addr = mboxq->sge_array->addr[0];
11924 /*
11925 * Configure the FCF record for FCFI 0. This is the driver's
11926 * hardcoded default and gets used in nonFIP mode.
11927 */
11928 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11929 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11930 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11931
11932 /*
11933 * Copy the fcf_index and the FCF Record Data. The data starts after
11934 * the FCoE header plus word10. The data copy needs to be endian
11935 * correct.
11936 */
11937 bytep += sizeof(uint32_t);
11938 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11939 mboxq->vport = phba->pport;
11940 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11941 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11942 if (rc == MBX_NOT_FINISHED) {
11943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11944 "2515 ADD_FCF_RECORD mailbox failed with "
11945 "status 0x%x\n", rc);
11946 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11947 rc = -EIO;
11948 } else
11949 rc = 0;
11950
11951 return rc;
11952}
11953
11954/**
11955 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11956 * @phba: pointer to lpfc hba data structure.
11957 * @fcf_record: pointer to the fcf record to write the default data.
11958 * @fcf_index: FCF table entry index.
11959 *
11960 * This routine is invoked to build the driver's default FCF record. The
11961 * values used are hardcoded. This routine handles memory initialization.
11962 *
11963 **/
11964void
11965lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11966 struct fcf_record *fcf_record,
11967 uint16_t fcf_index)
11968{
11969 memset(fcf_record, 0, sizeof(struct fcf_record));
11970 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11971 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11972 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11973 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11974 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11975 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11976 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11977 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11978 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11979 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11980 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11981 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11982 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011983 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011984 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11985 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11986 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11987 /* Set the VLAN bit map */
11988 if (phba->valid_vlan) {
11989 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11990 = 1 << (phba->vlan_id % 8);
11991 }
11992}
11993
11994/**
11995 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11996 * @phba: pointer to lpfc hba data structure.
11997 * @fcf_index: FCF table entry offset.
11998 *
11999 * This routine is invoked to read up to @fcf_num of FCF record from the
12000 * device starting with the given @fcf_index.
12001 **/
12002int
12003lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
12004{
12005 int rc = 0, error;
12006 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012007
James Smart32b97932009-07-19 10:01:21 -040012008 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012009 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12010 if (!mboxq) {
12011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12012 "2000 Failed to allocate mbox for "
12013 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012014 error = -ENOMEM;
12015 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040012016 }
James Smartecfd03c2010-02-12 14:41:27 -050012017 /* Construct the read FCF record mailbox command */
12018 rc = lpfc_sli4_mbx_read_fcf_record(phba, mboxq, fcf_index);
12019 if (rc) {
12020 error = -EINVAL;
James Smart4d9ab992009-10-02 15:16:39 -040012021 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040012022 }
James Smartecfd03c2010-02-12 14:41:27 -050012023 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012024 mboxq->vport = phba->pport;
12025 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
12026 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012027 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012028 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012029 else {
James Smart32b97932009-07-19 10:01:21 -040012030 spin_lock_irq(&phba->hbalock);
12031 phba->hba_flag |= FCF_DISC_INPROGRESS;
12032 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040012033 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012034 }
James Smart4d9ab992009-10-02 15:16:39 -040012035fail_fcfscan:
12036 if (error) {
12037 if (mboxq)
12038 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12039 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12040 spin_lock_irq(&phba->hbalock);
12041 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12042 spin_unlock_irq(&phba->hbalock);
12043 }
James Smart6fb120a2009-05-22 14:52:59 -040012044 return error;
12045}
James Smarta0c87cb2009-07-19 10:01:10 -040012046
12047/**
James Smartecfd03c2010-02-12 14:41:27 -050012048 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12049 * @phba: pointer to lpfc hba data structure.
12050 *
12051 * This routine is the completion routine for the rediscover FCF table mailbox
12052 * command. If the mailbox command returned failure, it will try to stop the
12053 * FCF rediscover wait timer.
12054 **/
12055void
12056lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12057{
12058 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12059 uint32_t shdr_status, shdr_add_status;
12060
12061 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12062
12063 shdr_status = bf_get(lpfc_mbox_hdr_status,
12064 &redisc_fcf->header.cfg_shdr.response);
12065 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12066 &redisc_fcf->header.cfg_shdr.response);
12067 if (shdr_status || shdr_add_status) {
12068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12069 "2746 Requesting for FCF rediscovery failed "
12070 "status x%x add_status x%x\n",
12071 shdr_status, shdr_add_status);
12072 /*
12073 * Request failed, last resort to re-try current
12074 * registered FCF entry
12075 */
12076 lpfc_retry_pport_discovery(phba);
12077 } else
12078 /*
12079 * Start FCF rediscovery wait timer for pending FCF
12080 * before rescan FCF record table.
12081 */
12082 lpfc_fcf_redisc_wait_start_timer(phba);
12083
12084 mempool_free(mbox, phba->mbox_mem_pool);
12085}
12086
12087/**
12088 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12089 * @phba: pointer to lpfc hba data structure.
12090 *
12091 * This routine is invoked to request for rediscovery of the entire FCF table
12092 * by the port.
12093 **/
12094int
12095lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12096{
12097 LPFC_MBOXQ_t *mbox;
12098 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12099 int rc, length;
12100
12101 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12102 if (!mbox) {
12103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12104 "2745 Failed to allocate mbox for "
12105 "requesting FCF rediscover.\n");
12106 return -ENOMEM;
12107 }
12108
12109 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12110 sizeof(struct lpfc_sli4_cfg_mhdr));
12111 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12112 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12113 length, LPFC_SLI4_MBX_EMBED);
12114
12115 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12116 /* Set count to 0 for invalidating the entire FCF database */
12117 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12118
12119 /* Issue the mailbox command asynchronously */
12120 mbox->vport = phba->pport;
12121 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12122 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12123
12124 if (rc == MBX_NOT_FINISHED) {
12125 mempool_free(mbox, phba->mbox_mem_pool);
12126 return -EIO;
12127 }
12128 return 0;
12129}
12130
12131/**
James Smarta0c87cb2009-07-19 10:01:10 -040012132 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12133 * @phba: pointer to lpfc hba data structure.
12134 *
12135 * This function read region 23 and parse TLV for port status to
12136 * decide if the user disaled the port. If the TLV indicates the
12137 * port is disabled, the hba_flag is set accordingly.
12138 **/
12139void
12140lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12141{
12142 LPFC_MBOXQ_t *pmb = NULL;
12143 MAILBOX_t *mb;
12144 uint8_t *rgn23_data = NULL;
12145 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12146 int rc;
12147
12148 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12149 if (!pmb) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "2600 lpfc_sli_read_serdes_param failed to"
12152 " allocate mailbox memory\n");
12153 goto out;
12154 }
12155 mb = &pmb->u.mb;
12156
12157 /* Get adapter Region 23 data */
12158 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12159 if (!rgn23_data)
12160 goto out;
12161
12162 do {
12163 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12164 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12165
12166 if (rc != MBX_SUCCESS) {
12167 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12168 "2601 lpfc_sli_read_link_ste failed to"
12169 " read config region 23 rc 0x%x Status 0x%x\n",
12170 rc, mb->mbxStatus);
12171 mb->un.varDmp.word_cnt = 0;
12172 }
12173 /*
12174 * dump mem may return a zero when finished or we got a
12175 * mailbox error, either way we are done.
12176 */
12177 if (mb->un.varDmp.word_cnt == 0)
12178 break;
12179 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12180 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12181
12182 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12183 rgn23_data + offset,
12184 mb->un.varDmp.word_cnt);
12185 offset += mb->un.varDmp.word_cnt;
12186 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12187
12188 data_size = offset;
12189 offset = 0;
12190
12191 if (!data_size)
12192 goto out;
12193
12194 /* Check the region signature first */
12195 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12197 "2619 Config region 23 has bad signature\n");
12198 goto out;
12199 }
12200 offset += 4;
12201
12202 /* Check the data structure version */
12203 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12205 "2620 Config region 23 has bad version\n");
12206 goto out;
12207 }
12208 offset += 4;
12209
12210 /* Parse TLV entries in the region */
12211 while (offset < data_size) {
12212 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12213 break;
12214 /*
12215 * If the TLV is not driver specific TLV or driver id is
12216 * not linux driver id, skip the record.
12217 */
12218 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12219 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12220 (rgn23_data[offset + 3] != 0)) {
12221 offset += rgn23_data[offset + 1] * 4 + 4;
12222 continue;
12223 }
12224
12225 /* Driver found a driver specific TLV in the config region */
12226 sub_tlv_len = rgn23_data[offset + 1] * 4;
12227 offset += 4;
12228 tlv_offset = 0;
12229
12230 /*
12231 * Search for configured port state sub-TLV.
12232 */
12233 while ((offset < data_size) &&
12234 (tlv_offset < sub_tlv_len)) {
12235 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12236 offset += 4;
12237 tlv_offset += 4;
12238 break;
12239 }
12240 if (rgn23_data[offset] != PORT_STE_TYPE) {
12241 offset += rgn23_data[offset + 1] * 4 + 4;
12242 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12243 continue;
12244 }
12245
12246 /* This HBA contains PORT_STE configured */
12247 if (!rgn23_data[offset + 2])
12248 phba->hba_flag |= LINK_DISABLED;
12249
12250 goto out;
12251 }
12252 }
12253out:
12254 if (pmb)
12255 mempool_free(pmb, phba->mbox_mem_pool);
12256 kfree(rgn23_data);
12257 return;
12258}
James Smart695a8142010-01-26 23:08:03 -050012259
12260/**
12261 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12262 * @vport: pointer to vport data structure.
12263 *
12264 * This function iterate through the mailboxq and clean up all REG_LOGIN
12265 * and REG_VPI mailbox commands associated with the vport. This function
12266 * is called when driver want to restart discovery of the vport due to
12267 * a Clear Virtual Link event.
12268 **/
12269void
12270lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12271{
12272 struct lpfc_hba *phba = vport->phba;
12273 LPFC_MBOXQ_t *mb, *nextmb;
12274 struct lpfc_dmabuf *mp;
12275
12276 spin_lock_irq(&phba->hbalock);
12277 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12278 if (mb->vport != vport)
12279 continue;
12280
12281 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12282 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12283 continue;
12284
12285 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12286 mp = (struct lpfc_dmabuf *) (mb->context1);
12287 if (mp) {
12288 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12289 kfree(mp);
12290 }
12291 }
12292 list_del(&mb->list);
12293 mempool_free(mb, phba->mbox_mem_pool);
12294 }
12295 mb = phba->sli.mbox_active;
12296 if (mb && (mb->vport == vport)) {
12297 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12298 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12299 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12300 }
12301 spin_unlock_irq(&phba->hbalock);
12302}
12303