blob: bb6a4426d469d745de485031b91ac1222a31fb15 [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
James Smartfc2b9892010-02-26 14:15:29 -05004522 /* Reset HBA FCF states after HBA reset */
4523 phba->fcf.fcf_flag = 0;
4524 phba->fcf.current_rec.flag = 0;
4525
James Smartda0436e2009-05-22 14:51:39 -04004526 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004527 mod_timer(&vport->els_tmofunc,
4528 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004529
4530 /* Start heart beat timer */
4531 mod_timer(&phba->hb_tmofunc,
4532 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4533 phba->hb_outstanding = 0;
4534 phba->last_completion_time = jiffies;
4535
4536 /* Start error attention (ERATT) polling timer */
4537 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4538
4539 /*
4540 * The port is ready, set the host's link state to LINK_DOWN
4541 * in preparation for link interrupts.
4542 */
4543 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4544 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4545 lpfc_set_loopback_flag(phba);
4546 /* Change driver state to LPFC_LINK_DOWN right before init link */
4547 spin_lock_irq(&phba->hbalock);
4548 phba->link_state = LPFC_LINK_DOWN;
4549 spin_unlock_irq(&phba->hbalock);
4550 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4551 if (unlikely(rc != MBX_NOT_FINISHED)) {
4552 kfree(vpd);
4553 return 0;
4554 } else
4555 rc = -EIO;
4556
4557 /* Unset all the queues set up in this routine when error out */
4558 if (rc)
4559 lpfc_sli4_queue_unset(phba);
4560
4561out_stop_timers:
4562 if (rc)
4563 lpfc_stop_hba_timers(phba);
4564out_free_vpd:
4565 kfree(vpd);
4566out_free_mbox:
4567 mempool_free(mboxq, phba->mbox_mem_pool);
4568 return rc;
4569}
James Smarte59058c2008-08-24 21:49:00 -04004570
4571/**
James Smart3621a712009-04-06 18:47:14 -04004572 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004573 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004574 *
James Smarte59058c2008-08-24 21:49:00 -04004575 * This is the callback function for mailbox timer. The mailbox
4576 * timer is armed when a new mailbox command is issued and the timer
4577 * is deleted when the mailbox complete. The function is called by
4578 * the kernel timer code when a mailbox does not complete within
4579 * expected time. This function wakes up the worker thread to
4580 * process the mailbox timeout and returns. All the processing is
4581 * done by the worker thread function lpfc_mbox_timeout_handler.
4582 **/
dea31012005-04-17 16:05:31 -05004583void
4584lpfc_mbox_timeout(unsigned long ptr)
4585{
James Smart92d7f7b2007-06-17 19:56:38 -05004586 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004587 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004588 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004589
James Smart2e0fef82007-06-17 19:56:36 -05004590 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004591 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004592 if (!tmo_posted)
4593 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4594 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4595
James Smart5e9d9b82008-06-14 22:52:53 -04004596 if (!tmo_posted)
4597 lpfc_worker_wake_up(phba);
4598 return;
dea31012005-04-17 16:05:31 -05004599}
4600
James Smarte59058c2008-08-24 21:49:00 -04004601
4602/**
James Smart3621a712009-04-06 18:47:14 -04004603 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004604 * @phba: Pointer to HBA context object.
4605 *
4606 * This function is called from worker thread when a mailbox command times out.
4607 * The caller is not required to hold any locks. This function will reset the
4608 * HBA and recover all the pending commands.
4609 **/
dea31012005-04-17 16:05:31 -05004610void
4611lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4612{
James Smart2e0fef82007-06-17 19:56:36 -05004613 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004614 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004615 struct lpfc_sli *psli = &phba->sli;
4616 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004617
James Smarta257bf92009-04-06 18:48:10 -04004618 /* Check the pmbox pointer first. There is a race condition
4619 * between the mbox timeout handler getting executed in the
4620 * worklist and the mailbox actually completing. When this
4621 * race condition occurs, the mbox_active will be NULL.
4622 */
4623 spin_lock_irq(&phba->hbalock);
4624 if (pmbox == NULL) {
4625 lpfc_printf_log(phba, KERN_WARNING,
4626 LOG_MBOX | LOG_SLI,
4627 "0353 Active Mailbox cleared - mailbox timeout "
4628 "exiting\n");
4629 spin_unlock_irq(&phba->hbalock);
4630 return;
4631 }
4632
dea31012005-04-17 16:05:31 -05004633 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004634 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004635 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004636 mb->mbxCommand,
4637 phba->pport->port_state,
4638 phba->sli.sli_flag,
4639 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004640 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004641
James Smart1dcb58e2007-04-25 09:51:30 -04004642 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4643 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4644 * it to fail all oustanding SCSI IO.
4645 */
James Smart2e0fef82007-06-17 19:56:36 -05004646 spin_lock_irq(&phba->pport->work_port_lock);
4647 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4648 spin_unlock_irq(&phba->pport->work_port_lock);
4649 spin_lock_irq(&phba->hbalock);
4650 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004651 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004652 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004653
4654 pring = &psli->ring[psli->fcp_ring];
4655 lpfc_sli_abort_iocb_ring(phba, pring);
4656
4657 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004658 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004659
4660 /* Reset the HBA device */
4661 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004662}
4663
James Smarte59058c2008-08-24 21:49:00 -04004664/**
James Smart3772a992009-05-22 14:50:54 -04004665 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004666 * @phba: Pointer to HBA context object.
4667 * @pmbox: Pointer to mailbox object.
4668 * @flag: Flag indicating how the mailbox need to be processed.
4669 *
4670 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004671 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4672 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004673 * The mailbox command can be submitted in polling mode, in which case
4674 * this function will wait in a polling loop for the completion of the
4675 * mailbox.
4676 * If the mailbox is submitted in no_wait mode (not polling) the
4677 * function will submit the command and returns immediately without waiting
4678 * for the mailbox completion. The no_wait is supported only when HBA
4679 * is in SLI2/SLI3 mode - interrupts are enabled.
4680 * The SLI interface allows only one mailbox pending at a time. If the
4681 * mailbox is issued in polling mode and there is already a mailbox
4682 * pending, then the function will return an error. If the mailbox is issued
4683 * in NO_WAIT mode and there is a mailbox pending already, the function
4684 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4685 * The sli layer owns the mailbox object until the completion of mailbox
4686 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4687 * return codes the caller owns the mailbox command after the return of
4688 * the function.
4689 **/
James Smart3772a992009-05-22 14:50:54 -04004690static int
4691lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4692 uint32_t flag)
dea31012005-04-17 16:05:31 -05004693{
dea31012005-04-17 16:05:31 -05004694 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004695 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004696 uint32_t status, evtctr;
4697 uint32_t ha_copy;
4698 int i;
James Smart09372822008-01-11 01:52:54 -05004699 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004700 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004701 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004702 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004703 int processing_queue = 0;
4704
4705 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4706 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004707 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004708 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004709 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4710 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4711 return MBX_SUCCESS;
4712 }
James Smart58da1ff2008-04-07 10:15:56 -04004713 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004714 pmbox = lpfc_mbox_get(phba);
4715 if (!pmbox) {
4716 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4717 return MBX_SUCCESS;
4718 }
4719 }
dea31012005-04-17 16:05:31 -05004720
James Smarted957682007-06-17 19:56:37 -05004721 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004722 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004723 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004724 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004725 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004726 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004727 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004728 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004729 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004730 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004731 }
4732 }
4733
Linas Vepstas8d63f372007-02-14 14:28:36 -06004734 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004735 if (unlikely(pci_channel_offline(phba->pcidev))) {
4736 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4737 goto out_not_finished;
4738 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004739
James Smarta257bf92009-04-06 18:48:10 -04004740 /* If HBA has a deferred error attention, fail the iocb. */
4741 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4742 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4743 goto out_not_finished;
4744 }
4745
dea31012005-04-17 16:05:31 -05004746 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004747
James Smart3772a992009-05-22 14:50:54 -04004748 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004749 status = MBX_SUCCESS;
4750
James Smart2e0fef82007-06-17 19:56:36 -05004751 if (phba->link_state == LPFC_HBA_ERROR) {
4752 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004753
4754 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4756 "(%d):0311 Mailbox command x%x cannot "
4757 "issue Data: x%x x%x\n",
4758 pmbox->vport ? pmbox->vport->vpi : 0,
4759 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004760 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004761 }
4762
James Smart92908312006-03-07 15:04:13 -05004763 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4764 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004765 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004766 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4767 "(%d):2528 Mailbox command x%x cannot "
4768 "issue Data: x%x x%x\n",
4769 pmbox->vport ? pmbox->vport->vpi : 0,
4770 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004771 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004772 }
4773
dea31012005-04-17 16:05:31 -05004774 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4775 /* Polling for a mbox command when another one is already active
4776 * is not allowed in SLI. Also, the driver must have established
4777 * SLI2 mode to queue and process multiple mbox commands.
4778 */
4779
4780 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004781 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004782
4783 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004784 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4785 "(%d):2529 Mailbox command x%x "
4786 "cannot issue Data: x%x x%x\n",
4787 pmbox->vport ? pmbox->vport->vpi : 0,
4788 pmbox->u.mb.mbxCommand,
4789 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004790 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004791 }
4792
James Smart3772a992009-05-22 14:50:54 -04004793 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004794 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004795 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004796 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4797 "(%d):2530 Mailbox command x%x "
4798 "cannot issue Data: x%x x%x\n",
4799 pmbox->vport ? pmbox->vport->vpi : 0,
4800 pmbox->u.mb.mbxCommand,
4801 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004802 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004803 }
4804
dea31012005-04-17 16:05:31 -05004805 /* Another mailbox command is still being processed, queue this
4806 * command to be processed later.
4807 */
4808 lpfc_mbox_put(phba, pmbox);
4809
4810 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004811 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004812 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004813 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004814 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4815 mb->mbxCommand, phba->pport->port_state,
4816 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004817
4818 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004819 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004820
James Smart858c9f62007-06-17 19:56:39 -05004821 if (pmbox->vport) {
4822 lpfc_debugfs_disc_trc(pmbox->vport,
4823 LPFC_DISC_TRC_MBOX_VPORT,
4824 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4825 (uint32_t)mb->mbxCommand,
4826 mb->un.varWords[0], mb->un.varWords[1]);
4827 }
4828 else {
4829 lpfc_debugfs_disc_trc(phba->pport,
4830 LPFC_DISC_TRC_MBOX,
4831 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4832 (uint32_t)mb->mbxCommand,
4833 mb->un.varWords[0], mb->un.varWords[1]);
4834 }
4835
James Smart2e0fef82007-06-17 19:56:36 -05004836 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004837 }
4838
dea31012005-04-17 16:05:31 -05004839 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4840
4841 /* If we are not polling, we MUST be in SLI2 mode */
4842 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004843 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004844 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004845 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004846 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004847 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4849 "(%d):2531 Mailbox command x%x "
4850 "cannot issue Data: x%x x%x\n",
4851 pmbox->vport ? pmbox->vport->vpi : 0,
4852 pmbox->u.mb.mbxCommand,
4853 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004854 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004855 }
4856 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004857 mod_timer(&psli->mbox_tmo, (jiffies +
4858 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004859 }
4860
4861 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004862 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004863 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004864 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004865 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004866 mb->mbxCommand, phba->pport->port_state,
4867 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004868
James Smart858c9f62007-06-17 19:56:39 -05004869 if (mb->mbxCommand != MBX_HEARTBEAT) {
4870 if (pmbox->vport) {
4871 lpfc_debugfs_disc_trc(pmbox->vport,
4872 LPFC_DISC_TRC_MBOX_VPORT,
4873 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4874 (uint32_t)mb->mbxCommand,
4875 mb->un.varWords[0], mb->un.varWords[1]);
4876 }
4877 else {
4878 lpfc_debugfs_disc_trc(phba->pport,
4879 LPFC_DISC_TRC_MBOX,
4880 "MBOX Send: cmd:x%x mb:x%x x%x",
4881 (uint32_t)mb->mbxCommand,
4882 mb->un.varWords[0], mb->un.varWords[1]);
4883 }
4884 }
4885
dea31012005-04-17 16:05:31 -05004886 psli->slistat.mbox_cmd++;
4887 evtctr = psli->slistat.mbox_event;
4888
4889 /* next set own bit for the adapter and copy over command word */
4890 mb->mbxOwner = OWN_CHIP;
4891
James Smart3772a992009-05-22 14:50:54 -04004892 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004893 /* First copy command data to host SLIM area */
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 } else {
James Smart92908312006-03-07 15:04:13 -05004896 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004897 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004898 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004899 }
4900
4901 /* First copy mbox command data to HBA SLIM, skip past first
4902 word */
4903 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4904 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4905 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4906
4907 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004908 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004909 to_slim = phba->MBslimaddr;
4910 writel(ldata, to_slim);
4911 readl(to_slim); /* flush */
4912
4913 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4914 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004915 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004916 }
4917 }
4918
4919 wmb();
dea31012005-04-17 16:05:31 -05004920
4921 switch (flag) {
4922 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004923 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004924 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004925 /* Interrupt board to do it */
4926 writel(CA_MBATT, phba->CAregaddr);
4927 readl(phba->CAregaddr); /* flush */
4928 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004929 break;
4930
4931 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004932 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004933 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004934 /* Interrupt board to do it */
4935 writel(CA_MBATT, phba->CAregaddr);
4936 readl(phba->CAregaddr); /* flush */
4937
James Smart3772a992009-05-22 14:50:54 -04004938 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004939 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004940 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004941 word0 = le32_to_cpu(word0);
4942 } else {
4943 /* First read mbox status word */
4944 word0 = readl(phba->MBslimaddr);
4945 }
4946
4947 /* Read the HBA Host Attention Register */
4948 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004949 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4950 mb->mbxCommand) *
4951 1000) + jiffies;
4952 i = 0;
dea31012005-04-17 16:05:31 -05004953 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004954 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4955 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004956 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004957 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004958 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004959 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004960 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004961 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004962 }
4963
4964 /* Check if we took a mbox interrupt while we were
4965 polling */
4966 if (((word0 & OWN_CHIP) != OWN_CHIP)
4967 && (evtctr != psli->slistat.mbox_event))
4968 break;
4969
James Smart09372822008-01-11 01:52:54 -05004970 if (i++ > 10) {
4971 spin_unlock_irqrestore(&phba->hbalock,
4972 drvr_flag);
4973 msleep(1);
4974 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4975 }
dea31012005-04-17 16:05:31 -05004976
James Smart3772a992009-05-22 14:50:54 -04004977 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004978 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004979 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004980 word0 = le32_to_cpu(word0);
4981 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4982 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004983 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004984 /* Check real SLIM for any errors */
4985 slimword0 = readl(phba->MBslimaddr);
4986 slimmb = (MAILBOX_t *) & slimword0;
4987 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4988 && slimmb->mbxStatus) {
4989 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004990 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004991 word0 = slimword0;
4992 }
4993 }
4994 } else {
4995 /* First copy command data */
4996 word0 = readl(phba->MBslimaddr);
4997 }
4998 /* Read the HBA Host Attention Register */
4999 ha_copy = readl(phba->HAregaddr);
5000 }
5001
James Smart3772a992009-05-22 14:50:54 -04005002 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005003 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005004 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005005 } else {
5006 /* First copy command data */
5007 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5008 MAILBOX_CMD_SIZE);
5009 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5010 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05005011 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05005012 phba->MBslimaddr + DMP_RSP_OFFSET,
5013 mb->un.varDmp.word_cnt);
5014 }
5015 }
5016
5017 writel(HA_MBATT, phba->HAregaddr);
5018 readl(phba->HAregaddr); /* flush */
5019
5020 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5021 status = mb->mbxStatus;
5022 }
5023
James Smart2e0fef82007-06-17 19:56:36 -05005024 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5025 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005026
5027out_not_finished:
5028 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005029 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005030 lpfc_mbox_cmpl_put(phba, pmbox);
5031 }
5032 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005033}
5034
James Smarte59058c2008-08-24 21:49:00 -04005035/**
James Smartf1126682009-06-10 17:22:44 -04005036 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5037 * @phba: Pointer to HBA context object.
5038 *
5039 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5040 * the driver internal pending mailbox queue. It will then try to wait out the
5041 * possible outstanding mailbox command before return.
5042 *
5043 * Returns:
5044 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5045 * the outstanding mailbox command timed out.
5046 **/
5047static int
5048lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5049{
5050 struct lpfc_sli *psli = &phba->sli;
5051 uint8_t actcmd = MBX_HEARTBEAT;
5052 int rc = 0;
5053 unsigned long timeout;
5054
5055 /* Mark the asynchronous mailbox command posting as blocked */
5056 spin_lock_irq(&phba->hbalock);
5057 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5058 if (phba->sli.mbox_active)
5059 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5060 spin_unlock_irq(&phba->hbalock);
5061 /* Determine how long we might wait for the active mailbox
5062 * command to be gracefully completed by firmware.
5063 */
5064 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5065 jiffies;
5066 /* Wait for the outstnading mailbox command to complete */
5067 while (phba->sli.mbox_active) {
5068 /* Check active mailbox complete status every 2ms */
5069 msleep(2);
5070 if (time_after(jiffies, timeout)) {
5071 /* Timeout, marked the outstanding cmd not complete */
5072 rc = 1;
5073 break;
5074 }
5075 }
5076
5077 /* Can not cleanly block async mailbox command, fails it */
5078 if (rc) {
5079 spin_lock_irq(&phba->hbalock);
5080 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5081 spin_unlock_irq(&phba->hbalock);
5082 }
5083 return rc;
5084}
5085
5086/**
5087 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5088 * @phba: Pointer to HBA context object.
5089 *
5090 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5091 * commands from the driver internal pending mailbox queue. It makes sure
5092 * that there is no outstanding mailbox command before resuming posting
5093 * asynchronous mailbox commands. If, for any reason, there is outstanding
5094 * mailbox command, it will try to wait it out before resuming asynchronous
5095 * mailbox command posting.
5096 **/
5097static void
5098lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5099{
5100 struct lpfc_sli *psli = &phba->sli;
5101
5102 spin_lock_irq(&phba->hbalock);
5103 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5104 /* Asynchronous mailbox posting is not blocked, do nothing */
5105 spin_unlock_irq(&phba->hbalock);
5106 return;
5107 }
5108
5109 /* Outstanding synchronous mailbox command is guaranteed to be done,
5110 * successful or timeout, after timing-out the outstanding mailbox
5111 * command shall always be removed, so just unblock posting async
5112 * mailbox command and resume
5113 */
5114 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5115 spin_unlock_irq(&phba->hbalock);
5116
5117 /* wake up worker thread to post asynchronlous mailbox command */
5118 lpfc_worker_wake_up(phba);
5119}
5120
5121/**
James Smartda0436e2009-05-22 14:51:39 -04005122 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5123 * @phba: Pointer to HBA context object.
5124 * @mboxq: Pointer to mailbox object.
5125 *
5126 * The function posts a mailbox to the port. The mailbox is expected
5127 * to be comletely filled in and ready for the port to operate on it.
5128 * This routine executes a synchronous completion operation on the
5129 * mailbox by polling for its completion.
5130 *
5131 * The caller must not be holding any locks when calling this routine.
5132 *
5133 * Returns:
5134 * MBX_SUCCESS - mailbox posted successfully
5135 * Any of the MBX error values.
5136 **/
5137static int
5138lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5139{
5140 int rc = MBX_SUCCESS;
5141 unsigned long iflag;
5142 uint32_t db_ready;
5143 uint32_t mcqe_status;
5144 uint32_t mbx_cmnd;
5145 unsigned long timeout;
5146 struct lpfc_sli *psli = &phba->sli;
5147 struct lpfc_mqe *mb = &mboxq->u.mqe;
5148 struct lpfc_bmbx_create *mbox_rgn;
5149 struct dma_address *dma_address;
5150 struct lpfc_register bmbx_reg;
5151
5152 /*
5153 * Only one mailbox can be active to the bootstrap mailbox region
5154 * at a time and there is no queueing provided.
5155 */
5156 spin_lock_irqsave(&phba->hbalock, iflag);
5157 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5158 spin_unlock_irqrestore(&phba->hbalock, iflag);
5159 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5160 "(%d):2532 Mailbox command x%x (x%x) "
5161 "cannot issue Data: x%x x%x\n",
5162 mboxq->vport ? mboxq->vport->vpi : 0,
5163 mboxq->u.mb.mbxCommand,
5164 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5165 psli->sli_flag, MBX_POLL);
5166 return MBXERR_ERROR;
5167 }
5168 /* The server grabs the token and owns it until release */
5169 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5170 phba->sli.mbox_active = mboxq;
5171 spin_unlock_irqrestore(&phba->hbalock, iflag);
5172
5173 /*
5174 * Initialize the bootstrap memory region to avoid stale data areas
5175 * in the mailbox post. Then copy the caller's mailbox contents to
5176 * the bmbx mailbox region.
5177 */
5178 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5179 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5180 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5181 sizeof(struct lpfc_mqe));
5182
5183 /* Post the high mailbox dma address to the port and wait for ready. */
5184 dma_address = &phba->sli4_hba.bmbx.dma_address;
5185 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5186
5187 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5188 * 1000) + jiffies;
5189 do {
5190 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5191 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5192 if (!db_ready)
5193 msleep(2);
5194
5195 if (time_after(jiffies, timeout)) {
5196 rc = MBXERR_ERROR;
5197 goto exit;
5198 }
5199 } while (!db_ready);
5200
5201 /* Post the low mailbox dma address to the port. */
5202 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5203 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5204 * 1000) + jiffies;
5205 do {
5206 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5207 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5208 if (!db_ready)
5209 msleep(2);
5210
5211 if (time_after(jiffies, timeout)) {
5212 rc = MBXERR_ERROR;
5213 goto exit;
5214 }
5215 } while (!db_ready);
5216
5217 /*
5218 * Read the CQ to ensure the mailbox has completed.
5219 * If so, update the mailbox status so that the upper layers
5220 * can complete the request normally.
5221 */
5222 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5223 sizeof(struct lpfc_mqe));
5224 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5225 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5226 sizeof(struct lpfc_mcqe));
5227 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5228
5229 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5230 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5231 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5232 rc = MBXERR_ERROR;
5233 }
5234
5235 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5236 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5237 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5238 " x%x x%x CQ: x%x x%x x%x x%x\n",
5239 mboxq->vport ? mboxq->vport->vpi : 0,
5240 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5241 bf_get(lpfc_mqe_status, mb),
5242 mb->un.mb_words[0], mb->un.mb_words[1],
5243 mb->un.mb_words[2], mb->un.mb_words[3],
5244 mb->un.mb_words[4], mb->un.mb_words[5],
5245 mb->un.mb_words[6], mb->un.mb_words[7],
5246 mb->un.mb_words[8], mb->un.mb_words[9],
5247 mb->un.mb_words[10], mb->un.mb_words[11],
5248 mb->un.mb_words[12], mboxq->mcqe.word0,
5249 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5250 mboxq->mcqe.trailer);
5251exit:
5252 /* We are holding the token, no needed for lock when release */
5253 spin_lock_irqsave(&phba->hbalock, iflag);
5254 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5255 phba->sli.mbox_active = NULL;
5256 spin_unlock_irqrestore(&phba->hbalock, iflag);
5257 return rc;
5258}
5259
5260/**
5261 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5262 * @phba: Pointer to HBA context object.
5263 * @pmbox: Pointer to mailbox object.
5264 * @flag: Flag indicating how the mailbox need to be processed.
5265 *
5266 * This function is called by discovery code and HBA management code to submit
5267 * a mailbox command to firmware with SLI-4 interface spec.
5268 *
5269 * Return codes the caller owns the mailbox command after the return of the
5270 * function.
5271 **/
5272static int
5273lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5274 uint32_t flag)
5275{
5276 struct lpfc_sli *psli = &phba->sli;
5277 unsigned long iflags;
5278 int rc;
5279
James Smart8fa38512009-07-19 10:01:03 -04005280 rc = lpfc_mbox_dev_check(phba);
5281 if (unlikely(rc)) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5283 "(%d):2544 Mailbox command x%x (x%x) "
5284 "cannot issue Data: x%x x%x\n",
5285 mboxq->vport ? mboxq->vport->vpi : 0,
5286 mboxq->u.mb.mbxCommand,
5287 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5288 psli->sli_flag, flag);
5289 goto out_not_finished;
5290 }
5291
James Smartda0436e2009-05-22 14:51:39 -04005292 /* Detect polling mode and jump to a handler */
5293 if (!phba->sli4_hba.intr_enable) {
5294 if (flag == MBX_POLL)
5295 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5296 else
5297 rc = -EIO;
5298 if (rc != MBX_SUCCESS)
5299 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5300 "(%d):2541 Mailbox command x%x "
5301 "(x%x) cannot issue Data: x%x x%x\n",
5302 mboxq->vport ? mboxq->vport->vpi : 0,
5303 mboxq->u.mb.mbxCommand,
5304 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5305 psli->sli_flag, flag);
5306 return rc;
5307 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005308 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5309 "(%d):2542 Try to issue mailbox command "
5310 "x%x (x%x) synchronously ahead of async"
5311 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005312 mboxq->vport ? mboxq->vport->vpi : 0,
5313 mboxq->u.mb.mbxCommand,
5314 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5315 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005316 /* Try to block the asynchronous mailbox posting */
5317 rc = lpfc_sli4_async_mbox_block(phba);
5318 if (!rc) {
5319 /* Successfully blocked, now issue sync mbox cmd */
5320 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5321 if (rc != MBX_SUCCESS)
5322 lpfc_printf_log(phba, KERN_ERR,
5323 LOG_MBOX | LOG_SLI,
5324 "(%d):2597 Mailbox command "
5325 "x%x (x%x) cannot issue "
5326 "Data: x%x x%x\n",
5327 mboxq->vport ?
5328 mboxq->vport->vpi : 0,
5329 mboxq->u.mb.mbxCommand,
5330 lpfc_sli4_mbox_opcode_get(phba,
5331 mboxq),
5332 psli->sli_flag, flag);
5333 /* Unblock the async mailbox posting afterward */
5334 lpfc_sli4_async_mbox_unblock(phba);
5335 }
5336 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005337 }
5338
5339 /* Now, interrupt mode asynchrous mailbox command */
5340 rc = lpfc_mbox_cmd_check(phba, mboxq);
5341 if (rc) {
5342 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5343 "(%d):2543 Mailbox command x%x (x%x) "
5344 "cannot issue Data: x%x x%x\n",
5345 mboxq->vport ? mboxq->vport->vpi : 0,
5346 mboxq->u.mb.mbxCommand,
5347 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5348 psli->sli_flag, flag);
5349 goto out_not_finished;
5350 }
James Smartda0436e2009-05-22 14:51:39 -04005351
5352 /* Put the mailbox command to the driver internal FIFO */
5353 psli->slistat.mbox_busy++;
5354 spin_lock_irqsave(&phba->hbalock, iflags);
5355 lpfc_mbox_put(phba, mboxq);
5356 spin_unlock_irqrestore(&phba->hbalock, iflags);
5357 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5358 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5359 "x%x (x%x) x%x x%x x%x\n",
5360 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5361 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5362 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5363 phba->pport->port_state,
5364 psli->sli_flag, MBX_NOWAIT);
5365 /* Wake up worker thread to transport mailbox command from head */
5366 lpfc_worker_wake_up(phba);
5367
5368 return MBX_BUSY;
5369
5370out_not_finished:
5371 return MBX_NOT_FINISHED;
5372}
5373
5374/**
5375 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5376 * @phba: Pointer to HBA context object.
5377 *
5378 * This function is called by worker thread to send a mailbox command to
5379 * SLI4 HBA firmware.
5380 *
5381 **/
5382int
5383lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5384{
5385 struct lpfc_sli *psli = &phba->sli;
5386 LPFC_MBOXQ_t *mboxq;
5387 int rc = MBX_SUCCESS;
5388 unsigned long iflags;
5389 struct lpfc_mqe *mqe;
5390 uint32_t mbx_cmnd;
5391
5392 /* Check interrupt mode before post async mailbox command */
5393 if (unlikely(!phba->sli4_hba.intr_enable))
5394 return MBX_NOT_FINISHED;
5395
5396 /* Check for mailbox command service token */
5397 spin_lock_irqsave(&phba->hbalock, iflags);
5398 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5399 spin_unlock_irqrestore(&phba->hbalock, iflags);
5400 return MBX_NOT_FINISHED;
5401 }
5402 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5403 spin_unlock_irqrestore(&phba->hbalock, iflags);
5404 return MBX_NOT_FINISHED;
5405 }
5406 if (unlikely(phba->sli.mbox_active)) {
5407 spin_unlock_irqrestore(&phba->hbalock, iflags);
5408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5409 "0384 There is pending active mailbox cmd\n");
5410 return MBX_NOT_FINISHED;
5411 }
5412 /* Take the mailbox command service token */
5413 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5414
5415 /* Get the next mailbox command from head of queue */
5416 mboxq = lpfc_mbox_get(phba);
5417
5418 /* If no more mailbox command waiting for post, we're done */
5419 if (!mboxq) {
5420 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5421 spin_unlock_irqrestore(&phba->hbalock, iflags);
5422 return MBX_SUCCESS;
5423 }
5424 phba->sli.mbox_active = mboxq;
5425 spin_unlock_irqrestore(&phba->hbalock, iflags);
5426
5427 /* Check device readiness for posting mailbox command */
5428 rc = lpfc_mbox_dev_check(phba);
5429 if (unlikely(rc))
5430 /* Driver clean routine will clean up pending mailbox */
5431 goto out_not_finished;
5432
5433 /* Prepare the mbox command to be posted */
5434 mqe = &mboxq->u.mqe;
5435 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5436
5437 /* Start timer for the mbox_tmo and log some mailbox post messages */
5438 mod_timer(&psli->mbox_tmo, (jiffies +
5439 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5440
5441 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5442 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5443 "x%x x%x\n",
5444 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5445 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5446 phba->pport->port_state, psli->sli_flag);
5447
5448 if (mbx_cmnd != MBX_HEARTBEAT) {
5449 if (mboxq->vport) {
5450 lpfc_debugfs_disc_trc(mboxq->vport,
5451 LPFC_DISC_TRC_MBOX_VPORT,
5452 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5453 mbx_cmnd, mqe->un.mb_words[0],
5454 mqe->un.mb_words[1]);
5455 } else {
5456 lpfc_debugfs_disc_trc(phba->pport,
5457 LPFC_DISC_TRC_MBOX,
5458 "MBOX Send: cmd:x%x mb:x%x x%x",
5459 mbx_cmnd, mqe->un.mb_words[0],
5460 mqe->un.mb_words[1]);
5461 }
5462 }
5463 psli->slistat.mbox_cmd++;
5464
5465 /* Post the mailbox command to the port */
5466 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5467 if (rc != MBX_SUCCESS) {
5468 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5469 "(%d):2533 Mailbox command x%x (x%x) "
5470 "cannot issue Data: x%x x%x\n",
5471 mboxq->vport ? mboxq->vport->vpi : 0,
5472 mboxq->u.mb.mbxCommand,
5473 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5474 psli->sli_flag, MBX_NOWAIT);
5475 goto out_not_finished;
5476 }
5477
5478 return rc;
5479
5480out_not_finished:
5481 spin_lock_irqsave(&phba->hbalock, iflags);
5482 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5483 __lpfc_mbox_cmpl_put(phba, mboxq);
5484 /* Release the token */
5485 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5486 phba->sli.mbox_active = NULL;
5487 spin_unlock_irqrestore(&phba->hbalock, iflags);
5488
5489 return MBX_NOT_FINISHED;
5490}
5491
5492/**
5493 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5494 * @phba: Pointer to HBA context object.
5495 * @pmbox: Pointer to mailbox object.
5496 * @flag: Flag indicating how the mailbox need to be processed.
5497 *
5498 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5499 * the API jump table function pointer from the lpfc_hba struct.
5500 *
5501 * Return codes the caller owns the mailbox command after the return of the
5502 * function.
5503 **/
5504int
5505lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5506{
5507 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5508}
5509
5510/**
5511 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5512 * @phba: The hba struct for which this call is being executed.
5513 * @dev_grp: The HBA PCI-Device group number.
5514 *
5515 * This routine sets up the mbox interface API function jump table in @phba
5516 * struct.
5517 * Returns: 0 - success, -ENODEV - failure.
5518 **/
5519int
5520lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5521{
5522
5523 switch (dev_grp) {
5524 case LPFC_PCI_DEV_LP:
5525 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5526 phba->lpfc_sli_handle_slow_ring_event =
5527 lpfc_sli_handle_slow_ring_event_s3;
5528 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5529 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5530 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5531 break;
5532 case LPFC_PCI_DEV_OC:
5533 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5534 phba->lpfc_sli_handle_slow_ring_event =
5535 lpfc_sli_handle_slow_ring_event_s4;
5536 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5537 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5538 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5539 break;
5540 default:
5541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5542 "1420 Invalid HBA PCI-device group: 0x%x\n",
5543 dev_grp);
5544 return -ENODEV;
5545 break;
5546 }
5547 return 0;
5548}
5549
5550/**
James Smart3621a712009-04-06 18:47:14 -04005551 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005552 * @phba: Pointer to HBA context object.
5553 * @pring: Pointer to driver SLI ring object.
5554 * @piocb: Pointer to address of newly added command iocb.
5555 *
5556 * This function is called with hbalock held to add a command
5557 * iocb to the txq when SLI layer cannot submit the command iocb
5558 * to the ring.
5559 **/
James Smart858c9f62007-06-17 19:56:39 -05005560static void
James Smart92d7f7b2007-06-17 19:56:38 -05005561__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005562 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005563{
5564 /* Insert the caller's iocb in the txq tail for later processing. */
5565 list_add_tail(&piocb->list, &pring->txq);
5566 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005567}
5568
James Smarte59058c2008-08-24 21:49:00 -04005569/**
James Smart3621a712009-04-06 18:47:14 -04005570 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005571 * @phba: Pointer to HBA context object.
5572 * @pring: Pointer to driver SLI ring object.
5573 * @piocb: Pointer to address of newly added command iocb.
5574 *
5575 * This function is called with hbalock held before a new
5576 * iocb is submitted to the firmware. This function checks
5577 * txq to flush the iocbs in txq to Firmware before
5578 * submitting new iocbs to the Firmware.
5579 * If there are iocbs in the txq which need to be submitted
5580 * to firmware, lpfc_sli_next_iocb returns the first element
5581 * of the txq after dequeuing it from txq.
5582 * If there is no iocb in the txq then the function will return
5583 * *piocb and *piocb is set to NULL. Caller needs to check
5584 * *piocb to find if there are more commands in the txq.
5585 **/
dea31012005-04-17 16:05:31 -05005586static struct lpfc_iocbq *
5587lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005588 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005589{
5590 struct lpfc_iocbq * nextiocb;
5591
5592 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5593 if (!nextiocb) {
5594 nextiocb = *piocb;
5595 *piocb = NULL;
5596 }
5597
5598 return nextiocb;
5599}
5600
James Smarte59058c2008-08-24 21:49:00 -04005601/**
James Smart3772a992009-05-22 14:50:54 -04005602 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005603 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005604 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005605 * @piocb: Pointer to command iocb.
5606 * @flag: Flag indicating if this command can be put into txq.
5607 *
James Smart3772a992009-05-22 14:50:54 -04005608 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5609 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5610 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5611 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5612 * this function allows only iocbs for posting buffers. This function finds
5613 * next available slot in the command ring and posts the command to the
5614 * available slot and writes the port attention register to request HBA start
5615 * processing new iocb. If there is no slot available in the ring and
5616 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5617 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005618 *
James Smart3772a992009-05-22 14:50:54 -04005619 * This function is called with hbalock held. The function will return success
5620 * after it successfully submit the iocb to firmware or after adding to the
5621 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005622 **/
James Smart98c9ea52007-10-27 13:37:33 -04005623static int
James Smart3772a992009-05-22 14:50:54 -04005624__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005625 struct lpfc_iocbq *piocb, uint32_t flag)
5626{
5627 struct lpfc_iocbq *nextiocb;
5628 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005629 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005630
James Smart92d7f7b2007-06-17 19:56:38 -05005631 if (piocb->iocb_cmpl && (!piocb->vport) &&
5632 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5633 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5634 lpfc_printf_log(phba, KERN_ERR,
5635 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005636 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005637 piocb->iocb.ulpCommand);
5638 dump_stack();
5639 return IOCB_ERROR;
5640 }
5641
5642
Linas Vepstas8d63f372007-02-14 14:28:36 -06005643 /* If the PCI channel is in offline state, do not post iocbs. */
5644 if (unlikely(pci_channel_offline(phba->pcidev)))
5645 return IOCB_ERROR;
5646
James Smarta257bf92009-04-06 18:48:10 -04005647 /* If HBA has a deferred error attention, fail the iocb. */
5648 if (unlikely(phba->hba_flag & DEFER_ERATT))
5649 return IOCB_ERROR;
5650
dea31012005-04-17 16:05:31 -05005651 /*
5652 * We should never get an IOCB if we are in a < LINK_DOWN state
5653 */
James Smart2e0fef82007-06-17 19:56:36 -05005654 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005655 return IOCB_ERROR;
5656
5657 /*
5658 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005659 * outstanding event.
dea31012005-04-17 16:05:31 -05005660 */
James Smart0b727fe2007-10-27 13:37:25 -04005661 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005662 goto iocb_busy;
5663
James Smart2e0fef82007-06-17 19:56:36 -05005664 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005665 /*
James Smart2680eea2007-04-25 09:52:55 -04005666 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005667 * can be issued if the link is not up.
5668 */
5669 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005670 case CMD_GEN_REQUEST64_CR:
5671 case CMD_GEN_REQUEST64_CX:
5672 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5673 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005674 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005675 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5676 MENLO_TRANSPORT_TYPE))
5677
5678 goto iocb_busy;
5679 break;
dea31012005-04-17 16:05:31 -05005680 case CMD_QUE_RING_BUF_CN:
5681 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005682 /*
5683 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5684 * completion, iocb_cmpl MUST be 0.
5685 */
5686 if (piocb->iocb_cmpl)
5687 piocb->iocb_cmpl = NULL;
5688 /*FALLTHROUGH*/
5689 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005690 case CMD_CLOSE_XRI_CN:
5691 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005692 break;
5693 default:
5694 goto iocb_busy;
5695 }
5696
5697 /*
5698 * For FCP commands, we must be in a state where we can process link
5699 * attention events.
5700 */
5701 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005702 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005703 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005704 }
dea31012005-04-17 16:05:31 -05005705
dea31012005-04-17 16:05:31 -05005706 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5707 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5708 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5709
5710 if (iocb)
5711 lpfc_sli_update_ring(phba, pring);
5712 else
5713 lpfc_sli_update_full_ring(phba, pring);
5714
5715 if (!piocb)
5716 return IOCB_SUCCESS;
5717
5718 goto out_busy;
5719
5720 iocb_busy:
5721 pring->stats.iocb_cmd_delay++;
5722
5723 out_busy:
5724
5725 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005726 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005727 return IOCB_SUCCESS;
5728 }
5729
5730 return IOCB_BUSY;
5731}
5732
James Smart3772a992009-05-22 14:50:54 -04005733/**
James Smart4f774512009-05-22 14:52:35 -04005734 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5735 * @phba: Pointer to HBA context object.
5736 * @piocb: Pointer to command iocb.
5737 * @sglq: Pointer to the scatter gather queue object.
5738 *
5739 * This routine converts the bpl or bde that is in the IOCB
5740 * to a sgl list for the sli4 hardware. The physical address
5741 * of the bpl/bde is converted back to a virtual address.
5742 * If the IOCB contains a BPL then the list of BDE's is
5743 * converted to sli4_sge's. If the IOCB contains a single
5744 * BDE then it is converted to a single sli_sge.
5745 * The IOCB is still in cpu endianess so the contents of
5746 * the bpl can be used without byte swapping.
5747 *
5748 * Returns valid XRI = Success, NO_XRI = Failure.
5749**/
5750static uint16_t
5751lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5752 struct lpfc_sglq *sglq)
5753{
5754 uint16_t xritag = NO_XRI;
5755 struct ulp_bde64 *bpl = NULL;
5756 struct ulp_bde64 bde;
5757 struct sli4_sge *sgl = NULL;
5758 IOCB_t *icmd;
5759 int numBdes = 0;
5760 int i = 0;
5761
5762 if (!piocbq || !sglq)
5763 return xritag;
5764
5765 sgl = (struct sli4_sge *)sglq->sgl;
5766 icmd = &piocbq->iocb;
5767 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5768 numBdes = icmd->un.genreq64.bdl.bdeSize /
5769 sizeof(struct ulp_bde64);
5770 /* The addrHigh and addrLow fields within the IOCB
5771 * have not been byteswapped yet so there is no
5772 * need to swap them back.
5773 */
5774 bpl = (struct ulp_bde64 *)
5775 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5776
5777 if (!bpl)
5778 return xritag;
5779
5780 for (i = 0; i < numBdes; i++) {
5781 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005782 sgl->addr_hi = bpl->addrHigh;
5783 sgl->addr_lo = bpl->addrLow;
5784
James Smart4f774512009-05-22 14:52:35 -04005785 if ((i+1) == numBdes)
5786 bf_set(lpfc_sli4_sge_last, sgl, 1);
5787 else
5788 bf_set(lpfc_sli4_sge_last, sgl, 0);
5789 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005790 /* swap the size field back to the cpu so we
5791 * can assign it to the sgl.
5792 */
5793 bde.tus.w = le32_to_cpu(bpl->tus.w);
5794 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005795 bpl++;
5796 sgl++;
5797 }
5798 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5799 /* The addrHigh and addrLow fields of the BDE have not
5800 * been byteswapped yet so they need to be swapped
5801 * before putting them in the sgl.
5802 */
5803 sgl->addr_hi =
5804 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5805 sgl->addr_lo =
5806 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005807 bf_set(lpfc_sli4_sge_last, sgl, 1);
5808 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005809 sgl->sge_len =
5810 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005811 }
5812 return sglq->sli4_xritag;
5813}
5814
5815/**
5816 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5817 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005818 *
5819 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005820 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5821 * held.
James Smart4f774512009-05-22 14:52:35 -04005822 *
5823 * Return: index into SLI4 fast-path FCP queue index.
5824 **/
5825static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005826lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005827{
James Smart8fa38512009-07-19 10:01:03 -04005828 ++phba->fcp_qidx;
5829 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5830 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005831
James Smart8fa38512009-07-19 10:01:03 -04005832 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005833}
5834
5835/**
5836 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5837 * @phba: Pointer to HBA context object.
5838 * @piocb: Pointer to command iocb.
5839 * @wqe: Pointer to the work queue entry.
5840 *
5841 * This routine converts the iocb command to its Work Queue Entry
5842 * equivalent. The wqe pointer should not have any fields set when
5843 * this routine is called because it will memcpy over them.
5844 * This routine does not set the CQ_ID or the WQEC bits in the
5845 * wqe.
5846 *
5847 * Returns: 0 = Success, IOCB_ERROR = Failure.
5848 **/
5849static int
5850lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5851 union lpfc_wqe *wqe)
5852{
James Smart5ffc2662009-11-18 15:39:44 -05005853 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005854 uint8_t ct = 0;
5855 uint32_t fip;
5856 uint32_t abort_tag;
5857 uint8_t command_type = ELS_COMMAND_NON_FIP;
5858 uint8_t cmnd;
5859 uint16_t xritag;
5860 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005861 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005862 int numBdes, i;
5863 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005864
James Smart45ed1192009-10-02 15:17:02 -04005865 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005866 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005867 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005868 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005869 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005870 command_type = ELS_COMMAND_FIP;
5871 else
5872 command_type = ELS_COMMAND_NON_FIP;
5873
James Smart4f774512009-05-22 14:52:35 -04005874 /* Some of the fields are in the right position already */
5875 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5876 abort_tag = (uint32_t) iocbq->iotag;
5877 xritag = iocbq->sli4_xritag;
5878 wqe->words[7] = 0; /* The ct field has moved so reset */
5879 /* words0-2 bpl convert bde */
5880 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005881 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5882 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005883 bpl = (struct ulp_bde64 *)
5884 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5885 if (!bpl)
5886 return IOCB_ERROR;
5887
5888 /* Should already be byte swapped. */
5889 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5890 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5891 /* swap the size field back to the cpu so we
5892 * can assign it to the sgl.
5893 */
5894 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005895 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5896 total_len = 0;
5897 for (i = 0; i < numBdes; i++) {
5898 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5899 total_len += bde.tus.f.bdeSize;
5900 }
James Smart4f774512009-05-22 14:52:35 -04005901 } else
James Smart5ffc2662009-11-18 15:39:44 -05005902 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005903
5904 iocbq->iocb.ulpIoTag = iocbq->iotag;
5905 cmnd = iocbq->iocb.ulpCommand;
5906
5907 switch (iocbq->iocb.ulpCommand) {
5908 case CMD_ELS_REQUEST64_CR:
5909 if (!iocbq->iocb.ulpLe) {
5910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5911 "2007 Only Limited Edition cmd Format"
5912 " supported 0x%x\n",
5913 iocbq->iocb.ulpCommand);
5914 return IOCB_ERROR;
5915 }
James Smart5ffc2662009-11-18 15:39:44 -05005916 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005917 /* Els_reguest64 has a TMO */
5918 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5919 iocbq->iocb.ulpTimeout);
5920 /* Need a VF for word 4 set the vf bit*/
5921 bf_set(els_req64_vf, &wqe->els_req, 0);
5922 /* And a VFID for word 12 */
5923 bf_set(els_req64_vfid, &wqe->els_req, 0);
5924 /*
5925 * Set ct field to 3, indicates that the context_tag field
5926 * contains the FCFI and remote N_Port_ID is
5927 * in word 5.
5928 */
5929
5930 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5931 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5932 iocbq->iocb.ulpContext);
5933
James Smart4f774512009-05-22 14:52:35 -04005934 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5935 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5936 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005937
5938 if (command_type == ELS_COMMAND_FIP) {
5939 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5940 >> LPFC_FIP_ELS_ID_SHIFT);
5941 }
5942 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5943
James Smart4f774512009-05-22 14:52:35 -04005944 break;
James Smart5ffc2662009-11-18 15:39:44 -05005945 case CMD_XMIT_SEQUENCE64_CX:
5946 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5947 iocbq->iocb.un.ulpWord[3]);
5948 wqe->generic.word3 = 0;
5949 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05005950 /* The entire sequence is transmitted for this IOCB */
5951 xmit_len = total_len;
5952 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005953 case CMD_XMIT_SEQUENCE64_CR:
5954 /* word3 iocb=io_tag32 wqe=payload_offset */
5955 /* payload offset used for multilpe outstanding
5956 * sequences on the same exchange
5957 */
5958 wqe->words[3] = 0;
5959 /* word4 relative_offset memcpy */
5960 /* word5 r_ctl/df_ctl memcpy */
5961 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005962 wqe->xmit_sequence.xmit_len = xmit_len;
5963 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005964 break;
5965 case CMD_XMIT_BCAST64_CN:
5966 /* word3 iocb=iotag32 wqe=payload_len */
5967 wqe->words[3] = 0; /* no definition for this in wqe */
5968 /* word4 iocb=rsvd wqe=rsvd */
5969 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5970 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5971 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5972 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5973 break;
5974 case CMD_FCP_IWRITE64_CR:
5975 command_type = FCP_COMMAND_DATA_OUT;
5976 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5977 * confusing.
5978 * word3 is payload_len: byte offset to the sgl entry for the
5979 * fcp_command.
5980 * word4 is total xfer len, same as the IOCB->ulpParameter.
5981 * word5 is initial xfer len 0 = wait for xfer-ready
5982 */
5983
5984 /* Always wait for xfer-ready before sending data */
5985 wqe->fcp_iwrite.initial_xfer_len = 0;
5986 /* word 4 (xfer length) should have been set on the memcpy */
5987
5988 /* allow write to fall through to read */
5989 case CMD_FCP_IREAD64_CR:
5990 /* FCP_CMD is always the 1st sgl entry */
5991 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05005992 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04005993
5994 /* word 4 (xfer length) should have been set on the memcpy */
5995
5996 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5997 iocbq->iocb.ulpFCP2Rcvy);
5998 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5999 /* The XC bit and the XS bit are similar. The driver never
6000 * tracked whether or not the exchange was previouslly open.
6001 * XC = Exchange create, 0 is create. 1 is already open.
6002 * XS = link cmd: 1 do not close the exchange after command.
6003 * XS = 0 close exchange when command completes.
6004 * The only time we would not set the XC bit is when the XS bit
6005 * is set and we are sending our 2nd or greater command on
6006 * this exchange.
6007 */
James Smart4f774512009-05-22 14:52:35 -04006008 /* Always open the exchange */
6009 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6010
6011 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6012 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006013 break;
6014 case CMD_FCP_ICMND64_CR:
6015 /* Always open the exchange */
6016 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6017
6018 wqe->words[4] = 0;
6019 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6020 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006021 break;
6022 case CMD_GEN_REQUEST64_CR:
6023 /* word3 command length is described as byte offset to the
6024 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6025 * sgl[0] = cmnd
6026 * sgl[1] = rsp.
6027 *
6028 */
James Smart5ffc2662009-11-18 15:39:44 -05006029 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04006030 /* Word4 parameter copied in the memcpy */
6031 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6032 /* word6 context tag copied in memcpy */
6033 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6034 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6036 "2015 Invalid CT %x command 0x%x\n",
6037 ct, iocbq->iocb.ulpCommand);
6038 return IOCB_ERROR;
6039 }
6040 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6041 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6042 iocbq->iocb.ulpTimeout);
6043
6044 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6045 command_type = OTHER_COMMAND;
6046 break;
6047 case CMD_XMIT_ELS_RSP64_CX:
6048 /* words0-2 BDE memcpy */
6049 /* word3 iocb=iotag32 wqe=rsvd */
6050 wqe->words[3] = 0;
6051 /* word4 iocb=did wge=rsvd. */
6052 wqe->words[4] = 0;
6053 /* word5 iocb=rsvd wge=did */
6054 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6055 iocbq->iocb.un.elsreq64.remoteID);
6056
6057 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6058 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6059
6060 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6061 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6062 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6063 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6064 iocbq->vport->vpi + phba->vpi_base);
6065 command_type = OTHER_COMMAND;
6066 break;
6067 case CMD_CLOSE_XRI_CN:
6068 case CMD_ABORT_XRI_CN:
6069 case CMD_ABORT_XRI_CX:
6070 /* words 0-2 memcpy should be 0 rserved */
6071 /* port will send abts */
6072 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6073 /*
6074 * The link is down so the fw does not need to send abts
6075 * on the wire.
6076 */
6077 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6078 else
6079 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6080 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006081 wqe->words[5] = 0;
6082 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6083 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6084 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006085 /*
6086 * The abort handler will send us CMD_ABORT_XRI_CN or
6087 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6088 */
6089 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6090 cmnd = CMD_ABORT_XRI_CX;
6091 command_type = OTHER_COMMAND;
6092 xritag = 0;
6093 break;
James Smart6669f9b2009-10-02 15:16:45 -04006094 case CMD_XMIT_BLS_RSP64_CX:
6095 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6096 * we re-construct this WQE here based on information in
6097 * iocbq from scratch.
6098 */
6099 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006100 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006101 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006102 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6103 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6104 LPFC_ABTS_UNSOL_INT) {
6105 /* ABTS sent by initiator to CT exchange, the
6106 * RX_ID field will be filled with the newly
6107 * allocated responder XRI.
6108 */
6109 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6110 iocbq->sli4_xritag);
6111 } else {
6112 /* ABTS sent by responder to CT exchange, the
6113 * RX_ID field will be filled with the responder
6114 * RX_ID from ABTS.
6115 */
6116 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6117 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6118 }
James Smart6669f9b2009-10-02 15:16:45 -04006119 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6120 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6121 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6122 iocbq->iocb.ulpContext);
6123 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6124 command_type = OTHER_COMMAND;
6125 break;
James Smart4f774512009-05-22 14:52:35 -04006126 case CMD_XRI_ABORTED_CX:
6127 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6128 /* words0-2 are all 0's no bde */
6129 /* word3 and word4 are rsvrd */
6130 wqe->words[3] = 0;
6131 wqe->words[4] = 0;
6132 /* word5 iocb=rsvd wge=did */
6133 /* There is no remote port id in the IOCB? */
6134 /* Let this fall through and fail */
6135 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6136 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6137 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6138 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6139 default:
6140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6141 "2014 Invalid command 0x%x\n",
6142 iocbq->iocb.ulpCommand);
6143 return IOCB_ERROR;
6144 break;
6145
6146 }
6147 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6148 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6149 wqe->generic.abort_tag = abort_tag;
6150 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6151 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6152 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6153 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6154
6155 return 0;
6156}
6157
6158/**
6159 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6160 * @phba: Pointer to HBA context object.
6161 * @ring_number: SLI ring number to issue iocb on.
6162 * @piocb: Pointer to command iocb.
6163 * @flag: Flag indicating if this command can be put into txq.
6164 *
6165 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6166 * an iocb command to an HBA with SLI-4 interface spec.
6167 *
6168 * This function is called with hbalock held. The function will return success
6169 * after it successfully submit the iocb to firmware or after adding to the
6170 * txq.
6171 **/
6172static int
6173__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6174 struct lpfc_iocbq *piocb, uint32_t flag)
6175{
6176 struct lpfc_sglq *sglq;
6177 uint16_t xritag;
6178 union lpfc_wqe wqe;
6179 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006180
6181 if (piocb->sli4_xritag == NO_XRI) {
6182 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006183 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006184 sglq = NULL;
6185 else {
6186 sglq = __lpfc_sli_get_sglq(phba);
6187 if (!sglq)
6188 return IOCB_ERROR;
6189 piocb->sli4_xritag = sglq->sli4_xritag;
6190 }
6191 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6192 sglq = NULL; /* These IO's already have an XRI and
6193 * a mapped sgl.
6194 */
6195 } else {
6196 /* This is a continuation of a commandi,(CX) so this
6197 * sglq is on the active list
6198 */
6199 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6200 if (!sglq)
6201 return IOCB_ERROR;
6202 }
6203
6204 if (sglq) {
6205 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6206 if (xritag != sglq->sli4_xritag)
6207 return IOCB_ERROR;
6208 }
6209
6210 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6211 return IOCB_ERROR;
6212
James Smart341af102010-01-26 23:07:37 -05006213 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6214 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006215 /*
6216 * For FCP command IOCB, get a new WQ index to distribute
6217 * WQE across the WQsr. On the other hand, for abort IOCB,
6218 * it carries the same WQ index to the original command
6219 * IOCB.
6220 */
James Smart341af102010-01-26 23:07:37 -05006221 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006222 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6223 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6224 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006225 return IOCB_ERROR;
6226 } else {
6227 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6228 return IOCB_ERROR;
6229 }
6230 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6231
6232 return 0;
6233}
6234
6235/**
James Smart3772a992009-05-22 14:50:54 -04006236 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6237 *
6238 * This routine wraps the actual lockless version for issusing IOCB function
6239 * pointer from the lpfc_hba struct.
6240 *
6241 * Return codes:
6242 * IOCB_ERROR - Error
6243 * IOCB_SUCCESS - Success
6244 * IOCB_BUSY - Busy
6245 **/
6246static inline int
6247__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6248 struct lpfc_iocbq *piocb, uint32_t flag)
6249{
6250 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6251}
6252
6253/**
6254 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6255 * @phba: The hba struct for which this call is being executed.
6256 * @dev_grp: The HBA PCI-Device group number.
6257 *
6258 * This routine sets up the SLI interface API function jump table in @phba
6259 * struct.
6260 * Returns: 0 - success, -ENODEV - failure.
6261 **/
6262int
6263lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6264{
6265
6266 switch (dev_grp) {
6267 case LPFC_PCI_DEV_LP:
6268 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6269 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6270 break;
James Smart4f774512009-05-22 14:52:35 -04006271 case LPFC_PCI_DEV_OC:
6272 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6273 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6274 break;
James Smart3772a992009-05-22 14:50:54 -04006275 default:
6276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6277 "1419 Invalid HBA PCI-device group: 0x%x\n",
6278 dev_grp);
6279 return -ENODEV;
6280 break;
6281 }
6282 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6283 return 0;
6284}
James Smart92d7f7b2007-06-17 19:56:38 -05006285
James Smarte59058c2008-08-24 21:49:00 -04006286/**
James Smart3621a712009-04-06 18:47:14 -04006287 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006288 * @phba: Pointer to HBA context object.
6289 * @pring: Pointer to driver SLI ring object.
6290 * @piocb: Pointer to command iocb.
6291 * @flag: Flag indicating if this command can be put into txq.
6292 *
6293 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6294 * function. This function gets the hbalock and calls
6295 * __lpfc_sli_issue_iocb function and will return the error returned
6296 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6297 * functions which do not hold hbalock.
6298 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006299int
James Smart3772a992009-05-22 14:50:54 -04006300lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006301 struct lpfc_iocbq *piocb, uint32_t flag)
6302{
6303 unsigned long iflags;
6304 int rc;
6305
6306 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006307 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006308 spin_unlock_irqrestore(&phba->hbalock, iflags);
6309
6310 return rc;
6311}
6312
James Smarte59058c2008-08-24 21:49:00 -04006313/**
James Smart3621a712009-04-06 18:47:14 -04006314 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006315 * @phba: Pointer to HBA context object.
6316 *
6317 * This function is called while driver attaches with the
6318 * HBA to setup the extra ring. The extra ring is used
6319 * only when driver needs to support target mode functionality
6320 * or IP over FC functionalities.
6321 *
6322 * This function is called with no lock held.
6323 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006324static int
6325lpfc_extra_ring_setup( struct lpfc_hba *phba)
6326{
6327 struct lpfc_sli *psli;
6328 struct lpfc_sli_ring *pring;
6329
6330 psli = &phba->sli;
6331
6332 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006333
6334 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006335 pring = &psli->ring[psli->fcp_ring];
6336 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6337 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6338 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6339 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6340
James Smarta4bc3372006-12-02 13:34:16 -05006341 /* and give them to the extra ring */
6342 pring = &psli->ring[psli->extra_ring];
6343
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006344 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6345 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6346 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6347 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6348
6349 /* Setup default profile for this ring */
6350 pring->iotag_max = 4096;
6351 pring->num_mask = 1;
6352 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006353 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6354 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006355 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6356 return 0;
6357}
6358
James Smarte59058c2008-08-24 21:49:00 -04006359/**
James Smart3621a712009-04-06 18:47:14 -04006360 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006361 * @phba: Pointer to HBA context object.
6362 * @pring: Pointer to driver SLI ring object.
6363 * @iocbq: Pointer to iocb object.
6364 *
6365 * This function is called by the slow ring event handler
6366 * function when there is an ASYNC event iocb in the ring.
6367 * This function is called with no lock held.
6368 * Currently this function handles only temperature related
6369 * ASYNC events. The function decodes the temperature sensor
6370 * event message and posts events for the management applications.
6371 **/
James Smart98c9ea52007-10-27 13:37:33 -04006372static void
James Smart57127f12007-10-27 13:37:05 -04006373lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6374 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6375{
6376 IOCB_t *icmd;
6377 uint16_t evt_code;
6378 uint16_t temp;
6379 struct temp_event temp_event_data;
6380 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006381 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006382
6383 icmd = &iocbq->iocb;
6384 evt_code = icmd->un.asyncstat.evt_code;
6385 temp = icmd->ulpContext;
6386
6387 if ((evt_code != ASYNC_TEMP_WARN) &&
6388 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006389 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006390 lpfc_printf_log(phba,
6391 KERN_ERR,
6392 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006393 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006394 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006395 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6396 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6397 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6398 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006399 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006400 icmd->un.asyncstat.evt_code,
6401 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6402 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6403 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6404 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6405
James Smart57127f12007-10-27 13:37:05 -04006406 return;
6407 }
6408 temp_event_data.data = (uint32_t)temp;
6409 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6410 if (evt_code == ASYNC_TEMP_WARN) {
6411 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6412 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006413 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006414 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006415 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006416 "corrective action. temperature : %d Celsius\n",
6417 temp);
6418 }
6419 if (evt_code == ASYNC_TEMP_SAFE) {
6420 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6421 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006422 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006423 LOG_TEMP,
6424 "0340 Adapter temperature is OK now. "
6425 "temperature : %d Celsius\n",
6426 temp);
6427 }
6428
6429 /* Send temperature change event to applications */
6430 shost = lpfc_shost_from_vport(phba->pport);
6431 fc_host_post_vendor_event(shost, fc_get_event_number(),
6432 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006433 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006434
6435}
6436
6437
James Smarte59058c2008-08-24 21:49:00 -04006438/**
James Smart3621a712009-04-06 18:47:14 -04006439 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006440 * @phba: Pointer to HBA context object.
6441 *
6442 * lpfc_sli_setup sets up rings of the SLI interface with
6443 * number of iocbs per ring and iotags. This function is
6444 * called while driver attach to the HBA and before the
6445 * interrupts are enabled. So there is no need for locking.
6446 *
6447 * This function always returns 0.
6448 **/
dea31012005-04-17 16:05:31 -05006449int
6450lpfc_sli_setup(struct lpfc_hba *phba)
6451{
James Smarted957682007-06-17 19:56:37 -05006452 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006453 struct lpfc_sli *psli = &phba->sli;
6454 struct lpfc_sli_ring *pring;
6455
6456 psli->num_rings = MAX_CONFIGURED_RINGS;
6457 psli->sli_flag = 0;
6458 psli->fcp_ring = LPFC_FCP_RING;
6459 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006460 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006461
James Bottomley604a3e32005-10-29 10:28:33 -05006462 psli->iocbq_lookup = NULL;
6463 psli->iocbq_lookup_len = 0;
6464 psli->last_iotag = 0;
6465
dea31012005-04-17 16:05:31 -05006466 for (i = 0; i < psli->num_rings; i++) {
6467 pring = &psli->ring[i];
6468 switch (i) {
6469 case LPFC_FCP_RING: /* ring 0 - FCP */
6470 /* numCiocb and numRiocb are used in config_port */
6471 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6472 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6473 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6474 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6475 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6476 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006477 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006478 SLI3_IOCB_CMD_SIZE :
6479 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006480 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006481 SLI3_IOCB_RSP_SIZE :
6482 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006483 pring->iotag_ctr = 0;
6484 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006485 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006486 pring->fast_iotag = pring->iotag_max;
6487 pring->num_mask = 0;
6488 break;
James Smarta4bc3372006-12-02 13:34:16 -05006489 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006490 /* numCiocb and numRiocb are used in config_port */
6491 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6492 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006493 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006494 SLI3_IOCB_CMD_SIZE :
6495 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006496 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006497 SLI3_IOCB_RSP_SIZE :
6498 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006499 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006500 pring->num_mask = 0;
6501 break;
6502 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6503 /* numCiocb and numRiocb are used in config_port */
6504 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6505 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006506 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006507 SLI3_IOCB_CMD_SIZE :
6508 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006509 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006510 SLI3_IOCB_RSP_SIZE :
6511 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006512 pring->fast_iotag = 0;
6513 pring->iotag_ctr = 0;
6514 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006515 pring->lpfc_sli_rcv_async_status =
6516 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006517 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006518 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006519 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6520 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006521 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006522 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006523 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006524 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6525 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006526 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006527 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006528 pring->prt[2].profile = 0; /* Mask 2 */
6529 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006530 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006531 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006532 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006533 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006534 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006535 pring->prt[3].profile = 0; /* Mask 3 */
6536 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006537 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006538 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006539 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006540 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006541 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006542 /* abort unsolicited sequence */
6543 pring->prt[4].profile = 0; /* Mask 4 */
6544 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6545 pring->prt[4].type = FC_TYPE_BLS;
6546 pring->prt[4].lpfc_sli_rcv_unsol_event =
6547 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006548 break;
6549 }
James Smarted957682007-06-17 19:56:37 -05006550 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006551 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006552 }
James Smarted957682007-06-17 19:56:37 -05006553 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006554 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006555 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6556 "SLI2 SLIM Data: x%x x%lx\n",
6557 phba->brd_no, totiocbsize,
6558 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006559 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006560 if (phba->cfg_multi_ring_support == 2)
6561 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006562
6563 return 0;
6564}
6565
James Smarte59058c2008-08-24 21:49:00 -04006566/**
James Smart3621a712009-04-06 18:47:14 -04006567 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006568 * @phba: Pointer to HBA context object.
6569 *
6570 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6571 * ring. This function also initializes ring indices of each ring.
6572 * This function is called during the initialization of the SLI
6573 * interface of an HBA.
6574 * This function is called with no lock held and always returns
6575 * 1.
6576 **/
dea31012005-04-17 16:05:31 -05006577int
James Smart2e0fef82007-06-17 19:56:36 -05006578lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006579{
6580 struct lpfc_sli *psli;
6581 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006582 int i;
dea31012005-04-17 16:05:31 -05006583
6584 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006585 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006586 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006587 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006588 /* Initialize list headers for txq and txcmplq as double linked lists */
6589 for (i = 0; i < psli->num_rings; i++) {
6590 pring = &psli->ring[i];
6591 pring->ringno = i;
6592 pring->next_cmdidx = 0;
6593 pring->local_getidx = 0;
6594 pring->cmdidx = 0;
6595 INIT_LIST_HEAD(&pring->txq);
6596 INIT_LIST_HEAD(&pring->txcmplq);
6597 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006598 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006599 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006600 }
James Smart2e0fef82007-06-17 19:56:36 -05006601 spin_unlock_irq(&phba->hbalock);
6602 return 1;
dea31012005-04-17 16:05:31 -05006603}
6604
James Smarte59058c2008-08-24 21:49:00 -04006605/**
James Smart04c68492009-05-22 14:52:52 -04006606 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6607 * @phba: Pointer to HBA context object.
6608 *
6609 * This routine flushes the mailbox command subsystem. It will unconditionally
6610 * flush all the mailbox commands in the three possible stages in the mailbox
6611 * command sub-system: pending mailbox command queue; the outstanding mailbox
6612 * command; and completed mailbox command queue. It is caller's responsibility
6613 * to make sure that the driver is in the proper state to flush the mailbox
6614 * command sub-system. Namely, the posting of mailbox commands into the
6615 * pending mailbox command queue from the various clients must be stopped;
6616 * either the HBA is in a state that it will never works on the outstanding
6617 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6618 * mailbox command has been completed.
6619 **/
6620static void
6621lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6622{
6623 LIST_HEAD(completions);
6624 struct lpfc_sli *psli = &phba->sli;
6625 LPFC_MBOXQ_t *pmb;
6626 unsigned long iflag;
6627
6628 /* Flush all the mailbox commands in the mbox system */
6629 spin_lock_irqsave(&phba->hbalock, iflag);
6630 /* The pending mailbox command queue */
6631 list_splice_init(&phba->sli.mboxq, &completions);
6632 /* The outstanding active mailbox command */
6633 if (psli->mbox_active) {
6634 list_add_tail(&psli->mbox_active->list, &completions);
6635 psli->mbox_active = NULL;
6636 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6637 }
6638 /* The completed mailbox command queue */
6639 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6640 spin_unlock_irqrestore(&phba->hbalock, iflag);
6641
6642 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6643 while (!list_empty(&completions)) {
6644 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6645 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6646 if (pmb->mbox_cmpl)
6647 pmb->mbox_cmpl(phba, pmb);
6648 }
6649}
6650
6651/**
James Smart3621a712009-04-06 18:47:14 -04006652 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006653 * @vport: Pointer to virtual port object.
6654 *
6655 * lpfc_sli_host_down is called to clean up the resources
6656 * associated with a vport before destroying virtual
6657 * port data structures.
6658 * This function does following operations:
6659 * - Free discovery resources associated with this virtual
6660 * port.
6661 * - Free iocbs associated with this virtual port in
6662 * the txq.
6663 * - Send abort for all iocb commands associated with this
6664 * vport in txcmplq.
6665 *
6666 * This function is called with no lock held and always returns 1.
6667 **/
dea31012005-04-17 16:05:31 -05006668int
James Smart92d7f7b2007-06-17 19:56:38 -05006669lpfc_sli_host_down(struct lpfc_vport *vport)
6670{
James Smart858c9f62007-06-17 19:56:39 -05006671 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006672 struct lpfc_hba *phba = vport->phba;
6673 struct lpfc_sli *psli = &phba->sli;
6674 struct lpfc_sli_ring *pring;
6675 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006676 int i;
6677 unsigned long flags = 0;
6678 uint16_t prev_pring_flag;
6679
6680 lpfc_cleanup_discovery_resources(vport);
6681
6682 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006683 for (i = 0; i < psli->num_rings; i++) {
6684 pring = &psli->ring[i];
6685 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006686 /* Only slow rings */
6687 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006688 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006689 /* Set the lpfc data pending flag */
6690 set_bit(LPFC_DATA_READY, &phba->data_flags);
6691 }
James Smart92d7f7b2007-06-17 19:56:38 -05006692 /*
6693 * Error everything on the txq since these iocbs have not been
6694 * given to the FW yet.
6695 */
James Smart92d7f7b2007-06-17 19:56:38 -05006696 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6697 if (iocb->vport != vport)
6698 continue;
James Smart858c9f62007-06-17 19:56:39 -05006699 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006700 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006701 }
6702
6703 /* Next issue ABTS for everything on the txcmplq */
6704 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6705 list) {
6706 if (iocb->vport != vport)
6707 continue;
6708 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6709 }
6710
6711 pring->flag = prev_pring_flag;
6712 }
6713
6714 spin_unlock_irqrestore(&phba->hbalock, flags);
6715
James Smarta257bf92009-04-06 18:48:10 -04006716 /* Cancel all the IOCBs from the completions list */
6717 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6718 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006719 return 1;
6720}
6721
James Smarte59058c2008-08-24 21:49:00 -04006722/**
James Smart3621a712009-04-06 18:47:14 -04006723 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006724 * @phba: Pointer to HBA context object.
6725 *
6726 * This function cleans up all iocb, buffers, mailbox commands
6727 * while shutting down the HBA. This function is called with no
6728 * lock held and always returns 1.
6729 * This function does the following to cleanup driver resources:
6730 * - Free discovery resources for each virtual port
6731 * - Cleanup any pending fabric iocbs
6732 * - Iterate through the iocb txq and free each entry
6733 * in the list.
6734 * - Free up any buffer posted to the HBA
6735 * - Free mailbox commands in the mailbox queue.
6736 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006737int
James Smart2e0fef82007-06-17 19:56:36 -05006738lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006739{
James Smart2534ba72007-04-25 09:52:20 -04006740 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006741 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006742 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006743 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006744 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006745 int i;
6746
6747 /* Shutdown the mailbox command sub-system */
6748 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006749
dea31012005-04-17 16:05:31 -05006750 lpfc_hba_down_prep(phba);
6751
James Smart92d7f7b2007-06-17 19:56:38 -05006752 lpfc_fabric_abort_hba(phba);
6753
James Smart2e0fef82007-06-17 19:56:36 -05006754 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006755 for (i = 0; i < psli->num_rings; i++) {
6756 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006757 /* Only slow rings */
6758 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006759 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006760 /* Set the lpfc data pending flag */
6761 set_bit(LPFC_DATA_READY, &phba->data_flags);
6762 }
dea31012005-04-17 16:05:31 -05006763
6764 /*
6765 * Error everything on the txq since these iocbs have not been
6766 * given to the FW yet.
6767 */
James Smart2534ba72007-04-25 09:52:20 -04006768 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006769 pring->txq_cnt = 0;
6770
dea31012005-04-17 16:05:31 -05006771 }
James Smart2e0fef82007-06-17 19:56:36 -05006772 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006773
James Smarta257bf92009-04-06 18:48:10 -04006774 /* Cancel all the IOCBs from the completions list */
6775 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6776 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006777
James Smart0ff10d42008-01-11 01:52:36 -05006778 spin_lock_irqsave(&phba->hbalock, flags);
6779 list_splice_init(&phba->elsbuf, &completions);
6780 phba->elsbuf_cnt = 0;
6781 phba->elsbuf_prev_cnt = 0;
6782 spin_unlock_irqrestore(&phba->hbalock, flags);
6783
6784 while (!list_empty(&completions)) {
6785 list_remove_head(&completions, buf_ptr,
6786 struct lpfc_dmabuf, list);
6787 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6788 kfree(buf_ptr);
6789 }
6790
dea31012005-04-17 16:05:31 -05006791 /* Return any active mbox cmds */
6792 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006793
James Smartda0436e2009-05-22 14:51:39 -04006794 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006795 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006796 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006797
James Smartda0436e2009-05-22 14:51:39 -04006798 return 1;
6799}
James Smart92d7f7b2007-06-17 19:56:38 -05006800
James Smartda0436e2009-05-22 14:51:39 -04006801/**
6802 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6803 * @phba: Pointer to HBA context object.
6804 *
6805 * This function cleans up all queues, iocb, buffers, mailbox commands while
6806 * shutting down the SLI4 HBA FCoE function. This function is called with no
6807 * lock held and always returns 1.
6808 *
6809 * This function does the following to cleanup driver FCoE function resources:
6810 * - Free discovery resources for each virtual port
6811 * - Cleanup any pending fabric iocbs
6812 * - Iterate through the iocb txq and free each entry in the list.
6813 * - Free up any buffer posted to the HBA.
6814 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6815 * - Free mailbox commands in the mailbox queue.
6816 **/
6817int
6818lpfc_sli4_hba_down(struct lpfc_hba *phba)
6819{
6820 /* Stop the SLI4 device port */
6821 lpfc_stop_port(phba);
6822
6823 /* Tear down the queues in the HBA */
6824 lpfc_sli4_queue_unset(phba);
6825
6826 /* unregister default FCFI from the HBA */
6827 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6828
dea31012005-04-17 16:05:31 -05006829 return 1;
6830}
6831
James Smarte59058c2008-08-24 21:49:00 -04006832/**
James Smart3621a712009-04-06 18:47:14 -04006833 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006834 * @srcp: Source memory pointer.
6835 * @destp: Destination memory pointer.
6836 * @cnt: Number of words required to be copied.
6837 *
6838 * This function is used for copying data between driver memory
6839 * and the SLI memory. This function also changes the endianness
6840 * of each word if native endianness is different from SLI
6841 * endianness. This function can be called with or without
6842 * lock.
6843 **/
dea31012005-04-17 16:05:31 -05006844void
6845lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6846{
6847 uint32_t *src = srcp;
6848 uint32_t *dest = destp;
6849 uint32_t ldata;
6850 int i;
6851
6852 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6853 ldata = *src;
6854 ldata = le32_to_cpu(ldata);
6855 *dest = ldata;
6856 src++;
6857 dest++;
6858 }
6859}
6860
James Smarte59058c2008-08-24 21:49:00 -04006861
6862/**
James Smarta0c87cb2009-07-19 10:01:10 -04006863 * lpfc_sli_bemem_bcopy - SLI memory copy function
6864 * @srcp: Source memory pointer.
6865 * @destp: Destination memory pointer.
6866 * @cnt: Number of words required to be copied.
6867 *
6868 * This function is used for copying data between a data structure
6869 * with big endian representation to local endianness.
6870 * This function can be called with or without lock.
6871 **/
6872void
6873lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6874{
6875 uint32_t *src = srcp;
6876 uint32_t *dest = destp;
6877 uint32_t ldata;
6878 int i;
6879
6880 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6881 ldata = *src;
6882 ldata = be32_to_cpu(ldata);
6883 *dest = ldata;
6884 src++;
6885 dest++;
6886 }
6887}
6888
6889/**
James Smart3621a712009-04-06 18:47:14 -04006890 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006891 * @phba: Pointer to HBA context object.
6892 * @pring: Pointer to driver SLI ring object.
6893 * @mp: Pointer to driver buffer object.
6894 *
6895 * This function is called with no lock held.
6896 * It always return zero after adding the buffer to the postbufq
6897 * buffer list.
6898 **/
dea31012005-04-17 16:05:31 -05006899int
James Smart2e0fef82007-06-17 19:56:36 -05006900lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6901 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006902{
6903 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6904 later */
James Smart2e0fef82007-06-17 19:56:36 -05006905 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006906 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006907 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006908 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006909 return 0;
6910}
6911
James Smarte59058c2008-08-24 21:49:00 -04006912/**
James Smart3621a712009-04-06 18:47:14 -04006913 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006914 * @phba: Pointer to HBA context object.
6915 *
6916 * When HBQ is enabled, buffers are searched based on tags. This function
6917 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6918 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6919 * does not conflict with tags of buffer posted for unsolicited events.
6920 * The function returns the allocated tag. The function is called with
6921 * no locks held.
6922 **/
James Smart76bb24e2007-10-27 13:38:00 -04006923uint32_t
6924lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6925{
6926 spin_lock_irq(&phba->hbalock);
6927 phba->buffer_tag_count++;
6928 /*
6929 * Always set the QUE_BUFTAG_BIT to distiguish between
6930 * a tag assigned by HBQ.
6931 */
6932 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6933 spin_unlock_irq(&phba->hbalock);
6934 return phba->buffer_tag_count;
6935}
6936
James Smarte59058c2008-08-24 21:49:00 -04006937/**
James Smart3621a712009-04-06 18:47:14 -04006938 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006939 * @phba: Pointer to HBA context object.
6940 * @pring: Pointer to driver SLI ring object.
6941 * @tag: Buffer tag.
6942 *
6943 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6944 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6945 * iocb is posted to the response ring with the tag of the buffer.
6946 * This function searches the pring->postbufq list using the tag
6947 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6948 * iocb. If the buffer is found then lpfc_dmabuf object of the
6949 * buffer is returned to the caller else NULL is returned.
6950 * This function is called with no lock held.
6951 **/
James Smart76bb24e2007-10-27 13:38:00 -04006952struct lpfc_dmabuf *
6953lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6954 uint32_t tag)
6955{
6956 struct lpfc_dmabuf *mp, *next_mp;
6957 struct list_head *slp = &pring->postbufq;
6958
6959 /* Search postbufq, from the begining, looking for a match on tag */
6960 spin_lock_irq(&phba->hbalock);
6961 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6962 if (mp->buffer_tag == tag) {
6963 list_del_init(&mp->list);
6964 pring->postbufq_cnt--;
6965 spin_unlock_irq(&phba->hbalock);
6966 return mp;
6967 }
6968 }
6969
6970 spin_unlock_irq(&phba->hbalock);
6971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006972 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006973 "ring %d Data x%lx x%p x%p x%x\n",
6974 pring->ringno, (unsigned long) tag,
6975 slp->next, slp->prev, pring->postbufq_cnt);
6976
6977 return NULL;
6978}
dea31012005-04-17 16:05:31 -05006979
James Smarte59058c2008-08-24 21:49:00 -04006980/**
James Smart3621a712009-04-06 18:47:14 -04006981 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006982 * @phba: Pointer to HBA context object.
6983 * @pring: Pointer to driver SLI ring object.
6984 * @phys: DMA address of the buffer.
6985 *
6986 * This function searches the buffer list using the dma_address
6987 * of unsolicited event to find the driver's lpfc_dmabuf object
6988 * corresponding to the dma_address. The function returns the
6989 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6990 * This function is called by the ct and els unsolicited event
6991 * handlers to get the buffer associated with the unsolicited
6992 * event.
6993 *
6994 * This function is called with no lock held.
6995 **/
dea31012005-04-17 16:05:31 -05006996struct lpfc_dmabuf *
6997lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6998 dma_addr_t phys)
6999{
7000 struct lpfc_dmabuf *mp, *next_mp;
7001 struct list_head *slp = &pring->postbufq;
7002
7003 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05007004 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007005 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7006 if (mp->phys == phys) {
7007 list_del_init(&mp->list);
7008 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007009 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007010 return mp;
7011 }
7012 }
7013
James Smart2e0fef82007-06-17 19:56:36 -05007014 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04007016 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05007017 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007018 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05007019 slp->next, slp->prev, pring->postbufq_cnt);
7020 return NULL;
7021}
7022
James Smarte59058c2008-08-24 21:49:00 -04007023/**
James Smart3621a712009-04-06 18:47:14 -04007024 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04007025 * @phba: Pointer to HBA context object.
7026 * @cmdiocb: Pointer to driver command iocb object.
7027 * @rspiocb: Pointer to driver response iocb object.
7028 *
7029 * This function is the completion handler for the abort iocbs for
7030 * ELS commands. This function is called from the ELS ring event
7031 * handler with no lock held. This function frees memory resources
7032 * associated with the abort iocb.
7033 **/
dea31012005-04-17 16:05:31 -05007034static void
James Smart2e0fef82007-06-17 19:56:36 -05007035lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7036 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007037{
James Smart2e0fef82007-06-17 19:56:36 -05007038 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007039 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007040 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007041 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7042
7043 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007044
7045 if (irsp->ulpStatus) {
7046 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7047 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7048
James Smart2e0fef82007-06-17 19:56:36 -05007049 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007050 if (phba->sli_rev < LPFC_SLI_REV4) {
7051 if (abort_iotag != 0 &&
7052 abort_iotag <= phba->sli.last_iotag)
7053 abort_iocb =
7054 phba->sli.iocbq_lookup[abort_iotag];
7055 } else
7056 /* For sli4 the abort_tag is the XRI,
7057 * so the abort routine puts the iotag of the iocb
7058 * being aborted in the context field of the abort
7059 * IOCB.
7060 */
7061 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007062
James Smart92d7f7b2007-06-17 19:56:38 -05007063 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007064 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007065 "with tag %x context %x, abort status %x, "
7066 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007067 abort_iocb, abort_iotag, abort_context,
7068 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007069
7070 /*
James Smart58da1ff2008-04-07 10:15:56 -04007071 * If the iocb is not found in Firmware queue the iocb
7072 * might have completed already. Do not free it again.
7073 */
James Smart9b379602008-04-07 10:16:00 -04007074 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007075 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7076 spin_unlock_irq(&phba->hbalock);
7077 lpfc_sli_release_iocbq(phba, cmdiocb);
7078 return;
7079 }
7080 /* For SLI4 the ulpContext field for abort IOCB
7081 * holds the iotag of the IOCB being aborted so
7082 * the local abort_context needs to be reset to
7083 * match the aborted IOCBs ulpContext.
7084 */
7085 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7086 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007087 }
7088 /*
James Smart2680eea2007-04-25 09:52:55 -04007089 * make sure we have the right iocbq before taking it
7090 * off the txcmplq and try to call completion routine.
7091 */
James Smart2e0fef82007-06-17 19:56:36 -05007092 if (!abort_iocb ||
7093 abort_iocb->iocb.ulpContext != abort_context ||
7094 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7095 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007096 else if (phba->sli_rev < LPFC_SLI_REV4) {
7097 /*
7098 * leave the SLI4 aborted command on the txcmplq
7099 * list and the command complete WCQE's XB bit
7100 * will tell whether the SGL (XRI) can be released
7101 * immediately or to the aborted SGL list for the
7102 * following abort XRI from the HBA.
7103 */
James Smart92d7f7b2007-06-17 19:56:38 -05007104 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007105 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007106 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007107
James Smart0ff10d42008-01-11 01:52:36 -05007108 /* Firmware could still be in progress of DMAing
7109 * payload, so don't free data buffer till after
7110 * a hbeat.
7111 */
James Smart341af102010-01-26 23:07:37 -05007112 spin_lock_irq(&phba->hbalock);
James Smart0ff10d42008-01-11 01:52:36 -05007113 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007114 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007115 spin_unlock_irq(&phba->hbalock);
7116
James Smart92d7f7b2007-06-17 19:56:38 -05007117 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007118 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007119 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007120 }
7121 }
7122
James Bottomley604a3e32005-10-29 10:28:33 -05007123 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007124 return;
7125}
7126
James Smarte59058c2008-08-24 21:49:00 -04007127/**
James Smart3621a712009-04-06 18:47:14 -04007128 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007129 * @phba: Pointer to HBA context object.
7130 * @cmdiocb: Pointer to driver command iocb object.
7131 * @rspiocb: Pointer to driver response iocb object.
7132 *
7133 * The function is called from SLI ring event handler with no
7134 * lock held. This function is the completion handler for ELS commands
7135 * which are aborted. The function frees memory resources used for
7136 * the aborted ELS commands.
7137 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007138static void
7139lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7140 struct lpfc_iocbq *rspiocb)
7141{
7142 IOCB_t *irsp = &rspiocb->iocb;
7143
7144 /* ELS cmd tag <ulpIoTag> completes */
7145 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007146 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007147 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007148 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007149 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007150 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7151 lpfc_ct_free_iocb(phba, cmdiocb);
7152 else
7153 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007154 return;
7155}
7156
James Smarte59058c2008-08-24 21:49:00 -04007157/**
James Smart3621a712009-04-06 18:47:14 -04007158 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007159 * @phba: Pointer to HBA context object.
7160 * @pring: Pointer to driver SLI ring object.
7161 * @cmdiocb: Pointer to driver command iocb object.
7162 *
7163 * This function issues an abort iocb for the provided command
7164 * iocb. This function is called with hbalock held.
7165 * The function returns 0 when it fails due to memory allocation
7166 * failure or when the command iocb is an abort request.
7167 **/
dea31012005-04-17 16:05:31 -05007168int
James Smart2e0fef82007-06-17 19:56:36 -05007169lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7170 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007171{
James Smart2e0fef82007-06-17 19:56:36 -05007172 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007173 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007174 IOCB_t *icmd = NULL;
7175 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007176 int retval = IOCB_ERROR;
7177
James Smart92d7f7b2007-06-17 19:56:38 -05007178 /*
7179 * There are certain command types we don't want to abort. And we
7180 * don't want to abort commands that are already in the process of
7181 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007182 */
7183 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007184 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007185 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7186 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007187 return 0;
7188
James Smart858c9f62007-06-17 19:56:39 -05007189 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7190 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007191 */
James Smart858c9f62007-06-17 19:56:39 -05007192 if ((vport->load_flag & FC_UNLOADING) &&
7193 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007194 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7195 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7196 else
7197 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007198 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007199 }
dea31012005-04-17 16:05:31 -05007200
7201 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007202 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007203 if (abtsiocbp == NULL)
7204 return 0;
dea31012005-04-17 16:05:31 -05007205
James Smart07951072007-04-25 09:51:38 -04007206 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007207 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007208 */
7209 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7210
dea31012005-04-17 16:05:31 -05007211 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007212 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7213 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007214 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007215 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007216 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7217 }
James Smartda0436e2009-05-22 14:51:39 -04007218 else
7219 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007220 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007221 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007222
James Smart5ffc2662009-11-18 15:39:44 -05007223 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7224 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007225 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7226 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007227
James Smart2e0fef82007-06-17 19:56:36 -05007228 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007229 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7230 else
7231 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7232
7233 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007234
James Smarte8b62012007-08-02 11:10:09 -04007235 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7236 "0339 Abort xri x%x, original iotag x%x, "
7237 "abort cmd iotag x%x\n",
7238 iabt->un.acxri.abortContextTag,
7239 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007240 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007241
James Smartd7c255b2008-08-24 21:50:00 -04007242 if (retval)
7243 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007244abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007245 /*
7246 * Caller to this routine should check for IOCB_ERROR
7247 * and handle it properly. This routine no longer removes
7248 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007249 */
James Smart2e0fef82007-06-17 19:56:36 -05007250 return retval;
dea31012005-04-17 16:05:31 -05007251}
7252
James Smarte59058c2008-08-24 21:49:00 -04007253/**
James Smart3621a712009-04-06 18:47:14 -04007254 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007255 * @iocbq: Pointer to driver iocb object.
7256 * @vport: Pointer to driver virtual port object.
7257 * @tgt_id: SCSI ID of the target.
7258 * @lun_id: LUN ID of the scsi device.
7259 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7260 *
James Smart3621a712009-04-06 18:47:14 -04007261 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007262 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7263 * 0 if the filtering criteria is met for the given iocb and will return
7264 * 1 if the filtering criteria is not met.
7265 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7266 * given iocb is for the SCSI device specified by vport, tgt_id and
7267 * lun_id parameter.
7268 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7269 * given iocb is for the SCSI target specified by vport and tgt_id
7270 * parameters.
7271 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7272 * given iocb is for the SCSI host associated with the given vport.
7273 * This function is called with no locks held.
7274 **/
dea31012005-04-17 16:05:31 -05007275static int
James Smart51ef4c22007-08-02 11:10:31 -04007276lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7277 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007278 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007279{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007280 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007281 int rc = 1;
7282
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007283 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7284 return rc;
7285
James Smart51ef4c22007-08-02 11:10:31 -04007286 if (iocbq->vport != vport)
7287 return rc;
7288
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007289 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007290
James Smart495a7142008-06-14 22:52:59 -04007291 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007292 return rc;
7293
7294 switch (ctx_cmd) {
7295 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007296 if ((lpfc_cmd->rdata->pnode) &&
7297 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7298 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007299 rc = 0;
7300 break;
7301 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007302 if ((lpfc_cmd->rdata->pnode) &&
7303 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007304 rc = 0;
7305 break;
dea31012005-04-17 16:05:31 -05007306 case LPFC_CTX_HOST:
7307 rc = 0;
7308 break;
7309 default:
7310 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007311 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007312 break;
7313 }
7314
7315 return rc;
7316}
7317
James Smarte59058c2008-08-24 21:49:00 -04007318/**
James Smart3621a712009-04-06 18:47:14 -04007319 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007320 * @vport: Pointer to virtual port.
7321 * @tgt_id: SCSI ID of the target.
7322 * @lun_id: LUN ID of the scsi device.
7323 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7324 *
7325 * This function returns number of FCP commands pending for the vport.
7326 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7327 * commands pending on the vport associated with SCSI device specified
7328 * by tgt_id and lun_id parameters.
7329 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7330 * commands pending on the vport associated with SCSI target specified
7331 * by tgt_id parameter.
7332 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7333 * commands pending on the vport.
7334 * This function returns the number of iocbs which satisfy the filter.
7335 * This function is called without any lock held.
7336 **/
dea31012005-04-17 16:05:31 -05007337int
James Smart51ef4c22007-08-02 11:10:31 -04007338lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7339 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007340{
James Smart51ef4c22007-08-02 11:10:31 -04007341 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007342 struct lpfc_iocbq *iocbq;
7343 int sum, i;
dea31012005-04-17 16:05:31 -05007344
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007345 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7346 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007347
James Smart51ef4c22007-08-02 11:10:31 -04007348 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7349 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007350 sum++;
dea31012005-04-17 16:05:31 -05007351 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007352
dea31012005-04-17 16:05:31 -05007353 return sum;
7354}
7355
James Smarte59058c2008-08-24 21:49:00 -04007356/**
James Smart3621a712009-04-06 18:47:14 -04007357 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007358 * @phba: Pointer to HBA context object
7359 * @cmdiocb: Pointer to command iocb object.
7360 * @rspiocb: Pointer to response iocb object.
7361 *
7362 * This function is called when an aborted FCP iocb completes. This
7363 * function is called by the ring event handler with no lock held.
7364 * This function frees the iocb.
7365 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007366void
James Smart2e0fef82007-06-17 19:56:36 -05007367lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7368 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007369{
James Bottomley604a3e32005-10-29 10:28:33 -05007370 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007371 return;
7372}
7373
James Smarte59058c2008-08-24 21:49:00 -04007374/**
James Smart3621a712009-04-06 18:47:14 -04007375 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007376 * @vport: Pointer to virtual port.
7377 * @pring: Pointer to driver SLI ring object.
7378 * @tgt_id: SCSI ID of the target.
7379 * @lun_id: LUN ID of the scsi device.
7380 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7381 *
7382 * This function sends an abort command for every SCSI command
7383 * associated with the given virtual port pending on the ring
7384 * filtered by lpfc_sli_validate_fcp_iocb function.
7385 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7386 * FCP iocbs associated with lun specified by tgt_id and lun_id
7387 * parameters
7388 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7389 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7390 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7391 * FCP iocbs associated with virtual port.
7392 * This function returns number of iocbs it failed to abort.
7393 * This function is called with no locks held.
7394 **/
dea31012005-04-17 16:05:31 -05007395int
James Smart51ef4c22007-08-02 11:10:31 -04007396lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7397 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007398{
James Smart51ef4c22007-08-02 11:10:31 -04007399 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007400 struct lpfc_iocbq *iocbq;
7401 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007402 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007403 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007404 int i;
dea31012005-04-17 16:05:31 -05007405
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007406 for (i = 1; i <= phba->sli.last_iotag; i++) {
7407 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007408
James Smart51ef4c22007-08-02 11:10:31 -04007409 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007410 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007411 continue;
7412
7413 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007414 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007415 if (abtsiocb == NULL) {
7416 errcnt++;
7417 continue;
7418 }
dea31012005-04-17 16:05:31 -05007419
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007420 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007421 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7422 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007423 if (phba->sli_rev == LPFC_SLI_REV4)
7424 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7425 else
7426 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007427 abtsiocb->iocb.ulpLe = 1;
7428 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007429 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007430
James Smart5ffc2662009-11-18 15:39:44 -05007431 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7432 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007433 if (iocbq->iocb_flag & LPFC_IO_FCP)
7434 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007435
James Smart2e0fef82007-06-17 19:56:36 -05007436 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007437 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7438 else
7439 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7440
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007441 /* Setup callback routine and issue the command. */
7442 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007443 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7444 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007445 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007446 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007447 errcnt++;
7448 continue;
7449 }
7450 }
7451
7452 return errcnt;
7453}
7454
James Smarte59058c2008-08-24 21:49:00 -04007455/**
James Smart3621a712009-04-06 18:47:14 -04007456 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007457 * @phba: Pointer to HBA context object.
7458 * @cmdiocbq: Pointer to command iocb.
7459 * @rspiocbq: Pointer to response iocb.
7460 *
7461 * This function is the completion handler for iocbs issued using
7462 * lpfc_sli_issue_iocb_wait function. This function is called by the
7463 * ring event handler function without any lock held. This function
7464 * can be called from both worker thread context and interrupt
7465 * context. This function also can be called from other thread which
7466 * cleans up the SLI layer objects.
7467 * This function copy the contents of the response iocb to the
7468 * response iocb memory object provided by the caller of
7469 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7470 * sleeps for the iocb completion.
7471 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007472static void
7473lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7474 struct lpfc_iocbq *cmdiocbq,
7475 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007476{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007477 wait_queue_head_t *pdone_q;
7478 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -05007479 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007480
James Smart2e0fef82007-06-17 19:56:36 -05007481 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007482 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7483 if (cmdiocbq->context2 && rspiocbq)
7484 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7485 &rspiocbq->iocb, sizeof(IOCB_t));
7486
James Smart0f65ff62010-02-26 14:14:23 -05007487 /* Set the exchange busy flag for task management commands */
7488 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7489 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7490 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7491 cur_iocbq);
7492 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7493 }
7494
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007495 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007496 if (pdone_q)
7497 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007498 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007499 return;
7500}
7501
James Smarte59058c2008-08-24 21:49:00 -04007502/**
James Smartd11e31d2009-06-10 17:23:06 -04007503 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7504 * @phba: Pointer to HBA context object..
7505 * @piocbq: Pointer to command iocb.
7506 * @flag: Flag to test.
7507 *
7508 * This routine grabs the hbalock and then test the iocb_flag to
7509 * see if the passed in flag is set.
7510 * Returns:
7511 * 1 if flag is set.
7512 * 0 if flag is not set.
7513 **/
7514static int
7515lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7516 struct lpfc_iocbq *piocbq, uint32_t flag)
7517{
7518 unsigned long iflags;
7519 int ret;
7520
7521 spin_lock_irqsave(&phba->hbalock, iflags);
7522 ret = piocbq->iocb_flag & flag;
7523 spin_unlock_irqrestore(&phba->hbalock, iflags);
7524 return ret;
7525
7526}
7527
7528/**
James Smart3621a712009-04-06 18:47:14 -04007529 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007530 * @phba: Pointer to HBA context object..
7531 * @pring: Pointer to sli ring.
7532 * @piocb: Pointer to command iocb.
7533 * @prspiocbq: Pointer to response iocb.
7534 * @timeout: Timeout in number of seconds.
7535 *
7536 * This function issues the iocb to firmware and waits for the
7537 * iocb to complete. If the iocb command is not
7538 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7539 * Caller should not free the iocb resources if this function
7540 * returns IOCB_TIMEDOUT.
7541 * The function waits for the iocb completion using an
7542 * non-interruptible wait.
7543 * This function will sleep while waiting for iocb completion.
7544 * So, this function should not be called from any context which
7545 * does not allow sleeping. Due to the same reason, this function
7546 * cannot be called with interrupt disabled.
7547 * This function assumes that the iocb completions occur while
7548 * this function sleep. So, this function cannot be called from
7549 * the thread which process iocb completion for this ring.
7550 * This function clears the iocb_flag of the iocb object before
7551 * issuing the iocb and the iocb completion handler sets this
7552 * flag and wakes this thread when the iocb completes.
7553 * The contents of the response iocb will be copied to prspiocbq
7554 * by the completion handler when the command completes.
7555 * This function returns IOCB_SUCCESS when success.
7556 * This function is called with no lock held.
7557 **/
dea31012005-04-17 16:05:31 -05007558int
James Smart2e0fef82007-06-17 19:56:36 -05007559lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007560 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007561 struct lpfc_iocbq *piocb,
7562 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007563 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007564{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007565 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007566 long timeleft, timeout_req = 0;
7567 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007568 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007569
7570 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007571 * If the caller has provided a response iocbq buffer, then context2
7572 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007573 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007574 if (prspiocbq) {
7575 if (piocb->context2)
7576 return IOCB_ERROR;
7577 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007578 }
7579
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007580 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7581 piocb->context_un.wait_queue = &done_q;
7582 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007583
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007584 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7585 creg_val = readl(phba->HCregaddr);
7586 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7587 writel(creg_val, phba->HCregaddr);
7588 readl(phba->HCregaddr); /* flush */
7589 }
7590
James Smartda0436e2009-05-22 14:51:39 -04007591 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007592 if (retval == IOCB_SUCCESS) {
7593 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007594 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007595 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007596 timeout_req);
dea31012005-04-17 16:05:31 -05007597
James Smart7054a602007-04-25 09:52:34 -04007598 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7599 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007600 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007601 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007603 "0338 IOCB wait timeout error - no "
7604 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007605 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007606 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007607 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007608 "0330 IOCB wake NOT set, "
7609 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007610 timeout, (timeleft / jiffies));
7611 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007612 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007613 } else {
7614 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007615 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007616 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007617 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007618 }
7619
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007620 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7621 creg_val = readl(phba->HCregaddr);
7622 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7623 writel(creg_val, phba->HCregaddr);
7624 readl(phba->HCregaddr); /* flush */
7625 }
7626
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007627 if (prspiocbq)
7628 piocb->context2 = NULL;
7629
7630 piocb->context_un.wait_queue = NULL;
7631 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007632 return retval;
7633}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007634
James Smarte59058c2008-08-24 21:49:00 -04007635/**
James Smart3621a712009-04-06 18:47:14 -04007636 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007637 * @phba: Pointer to HBA context object.
7638 * @pmboxq: Pointer to driver mailbox object.
7639 * @timeout: Timeout in number of seconds.
7640 *
7641 * This function issues the mailbox to firmware and waits for the
7642 * mailbox command to complete. If the mailbox command is not
7643 * completed within timeout seconds, it returns MBX_TIMEOUT.
7644 * The function waits for the mailbox completion using an
7645 * interruptible wait. If the thread is woken up due to a
7646 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7647 * should not free the mailbox resources, if this function returns
7648 * MBX_TIMEOUT.
7649 * This function will sleep while waiting for mailbox completion.
7650 * So, this function should not be called from any context which
7651 * does not allow sleeping. Due to the same reason, this function
7652 * cannot be called with interrupt disabled.
7653 * This function assumes that the mailbox completion occurs while
7654 * this function sleep. So, this function cannot be called from
7655 * the worker thread which processes mailbox completion.
7656 * This function is called in the context of HBA management
7657 * applications.
7658 * This function returns MBX_SUCCESS when successful.
7659 * This function is called with no lock held.
7660 **/
dea31012005-04-17 16:05:31 -05007661int
James Smart2e0fef82007-06-17 19:56:36 -05007662lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007663 uint32_t timeout)
7664{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007665 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007666 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007667 unsigned long flag;
dea31012005-04-17 16:05:31 -05007668
7669 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007670 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007671 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007672
James Smart495a7142008-06-14 22:52:59 -04007673 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007674 /* setup wake call as IOCB callback */
7675 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7676 /* setup context field to pass wait_queue pointer to wake function */
7677 pmboxq->context1 = &done_q;
7678
dea31012005-04-17 16:05:31 -05007679 /* now issue the command */
7680 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7681
7682 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007683 wait_event_interruptible_timeout(done_q,
7684 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7685 timeout * HZ);
7686
James Smart858c9f62007-06-17 19:56:39 -05007687 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007688 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007689 /*
7690 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7691 * else do not free the resources.
7692 */
7693 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007694 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007695 else {
James Smart7054a602007-04-25 09:52:34 -04007696 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007697 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7698 }
7699 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007700 }
7701
dea31012005-04-17 16:05:31 -05007702 return retval;
7703}
7704
James Smarte59058c2008-08-24 21:49:00 -04007705/**
James Smart3772a992009-05-22 14:50:54 -04007706 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007707 * @phba: Pointer to HBA context.
7708 *
James Smart3772a992009-05-22 14:50:54 -04007709 * This function is called to shutdown the driver's mailbox sub-system.
7710 * It first marks the mailbox sub-system is in a block state to prevent
7711 * the asynchronous mailbox command from issued off the pending mailbox
7712 * command queue. If the mailbox command sub-system shutdown is due to
7713 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7714 * the mailbox sub-system flush routine to forcefully bring down the
7715 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7716 * as with offline or HBA function reset), this routine will wait for the
7717 * outstanding mailbox command to complete before invoking the mailbox
7718 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007719 **/
James Smart3772a992009-05-22 14:50:54 -04007720void
7721lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007722{
James Smart3772a992009-05-22 14:50:54 -04007723 struct lpfc_sli *psli = &phba->sli;
7724 uint8_t actcmd = MBX_HEARTBEAT;
7725 unsigned long timeout;
7726
7727 spin_lock_irq(&phba->hbalock);
7728 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7729 spin_unlock_irq(&phba->hbalock);
7730
7731 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7732 spin_lock_irq(&phba->hbalock);
7733 if (phba->sli.mbox_active)
7734 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7735 spin_unlock_irq(&phba->hbalock);
7736 /* Determine how long we might wait for the active mailbox
7737 * command to be gracefully completed by firmware.
7738 */
7739 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7740 1000) + jiffies;
7741 while (phba->sli.mbox_active) {
7742 /* Check active mailbox complete status every 2ms */
7743 msleep(2);
7744 if (time_after(jiffies, timeout))
7745 /* Timeout, let the mailbox flush routine to
7746 * forcefully release active mailbox command
7747 */
7748 break;
7749 }
7750 }
7751 lpfc_sli_mbox_sys_flush(phba);
7752}
7753
7754/**
7755 * lpfc_sli_eratt_read - read sli-3 error attention events
7756 * @phba: Pointer to HBA context.
7757 *
7758 * This function is called to read the SLI3 device error attention registers
7759 * for possible error attention events. The caller must hold the hostlock
7760 * with spin_lock_irq().
7761 *
7762 * This fucntion returns 1 when there is Error Attention in the Host Attention
7763 * Register and returns 0 otherwise.
7764 **/
7765static int
7766lpfc_sli_eratt_read(struct lpfc_hba *phba)
7767{
James Smarted957682007-06-17 19:56:37 -05007768 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007769
James Smart3772a992009-05-22 14:50:54 -04007770 /* Read chip Host Attention (HA) register */
7771 ha_copy = readl(phba->HAregaddr);
7772 if (ha_copy & HA_ERATT) {
7773 /* Read host status register to retrieve error event */
7774 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007775
James Smart3772a992009-05-22 14:50:54 -04007776 /* Check if there is a deferred error condition is active */
7777 if ((HS_FFER1 & phba->work_hs) &&
7778 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7779 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007780 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007781 /* Clear all interrupt enable conditions */
7782 writel(0, phba->HCregaddr);
7783 readl(phba->HCregaddr);
7784 }
7785
7786 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007787 phba->work_ha |= HA_ERATT;
7788 /* Indicate polling handles this ERATT */
7789 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007790 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007791 }
James Smart3772a992009-05-22 14:50:54 -04007792 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007793}
7794
James Smarte59058c2008-08-24 21:49:00 -04007795/**
James Smartda0436e2009-05-22 14:51:39 -04007796 * lpfc_sli4_eratt_read - read sli-4 error attention events
7797 * @phba: Pointer to HBA context.
7798 *
7799 * This function is called to read the SLI4 device error attention registers
7800 * for possible error attention events. The caller must hold the hostlock
7801 * with spin_lock_irq().
7802 *
7803 * This fucntion returns 1 when there is Error Attention in the Host Attention
7804 * Register and returns 0 otherwise.
7805 **/
7806static int
7807lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7808{
7809 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007810
7811 /* For now, use the SLI4 device internal unrecoverable error
7812 * registers for error attention. This can be changed later.
7813 */
James Smarta747c9c2009-11-18 15:41:10 -05007814 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7815 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7816 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7817 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7819 "1423 HBA Unrecoverable error: "
7820 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7821 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7822 uerr_sta_lo, uerr_sta_hi,
7823 phba->sli4_hba.ue_mask_lo,
7824 phba->sli4_hba.ue_mask_hi);
7825 phba->work_status[0] = uerr_sta_lo;
7826 phba->work_status[1] = uerr_sta_hi;
7827 /* Set the driver HA work bitmap */
7828 phba->work_ha |= HA_ERATT;
7829 /* Indicate polling handles this ERATT */
7830 phba->hba_flag |= HBA_ERATT_HANDLED;
7831 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007832 }
7833 return 0;
7834}
7835
7836/**
James Smart3621a712009-04-06 18:47:14 -04007837 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007838 * @phba: Pointer to HBA context.
7839 *
James Smart3772a992009-05-22 14:50:54 -04007840 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007841 * error attention register bit for error attention events.
7842 *
7843 * This fucntion returns 1 when there is Error Attention in the Host Attention
7844 * Register and returns 0 otherwise.
7845 **/
7846int
7847lpfc_sli_check_eratt(struct lpfc_hba *phba)
7848{
7849 uint32_t ha_copy;
7850
7851 /* If somebody is waiting to handle an eratt, don't process it
7852 * here. The brdkill function will do this.
7853 */
7854 if (phba->link_flag & LS_IGNORE_ERATT)
7855 return 0;
7856
7857 /* Check if interrupt handler handles this ERATT */
7858 spin_lock_irq(&phba->hbalock);
7859 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7860 /* Interrupt handler has handled ERATT */
7861 spin_unlock_irq(&phba->hbalock);
7862 return 0;
7863 }
7864
James Smarta257bf92009-04-06 18:48:10 -04007865 /*
7866 * If there is deferred error attention, do not check for error
7867 * attention
7868 */
7869 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7870 spin_unlock_irq(&phba->hbalock);
7871 return 0;
7872 }
7873
James Smart3772a992009-05-22 14:50:54 -04007874 /* If PCI channel is offline, don't process it */
7875 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007876 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007877 return 0;
7878 }
7879
7880 switch (phba->sli_rev) {
7881 case LPFC_SLI_REV2:
7882 case LPFC_SLI_REV3:
7883 /* Read chip Host Attention (HA) register */
7884 ha_copy = lpfc_sli_eratt_read(phba);
7885 break;
James Smartda0436e2009-05-22 14:51:39 -04007886 case LPFC_SLI_REV4:
7887 /* Read devcie Uncoverable Error (UERR) registers */
7888 ha_copy = lpfc_sli4_eratt_read(phba);
7889 break;
James Smart3772a992009-05-22 14:50:54 -04007890 default:
7891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7892 "0299 Invalid SLI revision (%d)\n",
7893 phba->sli_rev);
7894 ha_copy = 0;
7895 break;
James Smart93996272008-08-24 21:50:30 -04007896 }
7897 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007898
7899 return ha_copy;
7900}
7901
7902/**
7903 * lpfc_intr_state_check - Check device state for interrupt handling
7904 * @phba: Pointer to HBA context.
7905 *
7906 * This inline routine checks whether a device or its PCI slot is in a state
7907 * that the interrupt should be handled.
7908 *
7909 * This function returns 0 if the device or the PCI slot is in a state that
7910 * interrupt should be handled, otherwise -EIO.
7911 */
7912static inline int
7913lpfc_intr_state_check(struct lpfc_hba *phba)
7914{
7915 /* If the pci channel is offline, ignore all the interrupts */
7916 if (unlikely(pci_channel_offline(phba->pcidev)))
7917 return -EIO;
7918
7919 /* Update device level interrupt statistics */
7920 phba->sli.slistat.sli_intr++;
7921
7922 /* Ignore all interrupts during initialization. */
7923 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7924 return -EIO;
7925
James Smart93996272008-08-24 21:50:30 -04007926 return 0;
7927}
7928
7929/**
James Smart3772a992009-05-22 14:50:54 -04007930 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007931 * @irq: Interrupt number.
7932 * @dev_id: The device context pointer.
7933 *
James Smart93996272008-08-24 21:50:30 -04007934 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007935 * service routine when device with SLI-3 interface spec is enabled with
7936 * MSI-X multi-message interrupt mode and there are slow-path events in
7937 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7938 * interrupt mode, this function is called as part of the device-level
7939 * interrupt handler. When the PCI slot is in error recovery or the HBA
7940 * is undergoing initialization, the interrupt handler will not process
7941 * the interrupt. The link attention and ELS ring attention events are
7942 * handled by the worker thread. The interrupt handler signals the worker
7943 * thread and returns for these events. This function is called without
7944 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007945 * structures.
7946 *
7947 * This function returns IRQ_HANDLED when interrupt is handled else it
7948 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007949 **/
dea31012005-04-17 16:05:31 -05007950irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007951lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007952{
James Smart2e0fef82007-06-17 19:56:36 -05007953 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007954 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007955 uint32_t work_ha_copy;
7956 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007957 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007958 uint32_t control;
7959
James Smart92d7f7b2007-06-17 19:56:38 -05007960 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007961 struct lpfc_vport *vport;
7962 struct lpfc_nodelist *ndlp;
7963 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007964 LPFC_MBOXQ_t *pmb;
7965 int rc;
7966
dea31012005-04-17 16:05:31 -05007967 /*
7968 * Get the driver's phba structure from the dev_id and
7969 * assume the HBA is not interrupting.
7970 */
James Smart93996272008-08-24 21:50:30 -04007971 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007972
7973 if (unlikely(!phba))
7974 return IRQ_NONE;
7975
dea31012005-04-17 16:05:31 -05007976 /*
James Smart93996272008-08-24 21:50:30 -04007977 * Stuff needs to be attented to when this function is invoked as an
7978 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007979 */
James Smart93996272008-08-24 21:50:30 -04007980 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007981 /* Check device state for handling interrupt */
7982 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007983 return IRQ_NONE;
7984 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007985 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007986 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007987 /* If somebody is waiting to handle an eratt don't process it
7988 * here. The brdkill function will do this.
7989 */
7990 if (phba->link_flag & LS_IGNORE_ERATT)
7991 ha_copy &= ~HA_ERATT;
7992 /* Check the need for handling ERATT in interrupt handler */
7993 if (ha_copy & HA_ERATT) {
7994 if (phba->hba_flag & HBA_ERATT_HANDLED)
7995 /* ERATT polling has handled ERATT */
7996 ha_copy &= ~HA_ERATT;
7997 else
7998 /* Indicate interrupt handler handles ERATT */
7999 phba->hba_flag |= HBA_ERATT_HANDLED;
8000 }
James Smarta257bf92009-04-06 18:48:10 -04008001
8002 /*
8003 * If there is deferred error attention, do not check for any
8004 * interrupt.
8005 */
8006 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008007 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008008 return IRQ_NONE;
8009 }
8010
James Smart93996272008-08-24 21:50:30 -04008011 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05008012 hc_copy = readl(phba->HCregaddr);
8013 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8014 HC_LAINT_ENA | HC_ERINT_ENA),
8015 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008016 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8017 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008018 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008019 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008020 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008021 } else
8022 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05008023
dea31012005-04-17 16:05:31 -05008024 work_ha_copy = ha_copy & phba->work_ha_mask;
8025
James Smart93996272008-08-24 21:50:30 -04008026 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05008027 if (work_ha_copy & HA_LATT) {
8028 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8029 /*
8030 * Turn off Link Attention interrupts
8031 * until CLEAR_LA done
8032 */
James Smart5b75da22008-12-04 22:39:35 -05008033 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008034 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8035 control = readl(phba->HCregaddr);
8036 control &= ~HC_LAINT_ENA;
8037 writel(control, phba->HCregaddr);
8038 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008039 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008040 }
8041 else
8042 work_ha_copy &= ~HA_LATT;
8043 }
8044
James Smart93996272008-08-24 21:50:30 -04008045 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008046 /*
8047 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8048 * the only slow ring.
8049 */
8050 status = (work_ha_copy &
8051 (HA_RXMASK << (4*LPFC_ELS_RING)));
8052 status >>= (4*LPFC_ELS_RING);
8053 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008054 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008055 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008056
8057 lpfc_debugfs_slow_ring_trc(phba,
8058 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8059 control, status,
8060 (uint32_t)phba->sli.slistat.sli_intr);
8061
James Smart858c9f62007-06-17 19:56:39 -05008062 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008063 lpfc_debugfs_slow_ring_trc(phba,
8064 "ISR Disable ring:"
8065 "pwork:x%x hawork:x%x wait:x%x",
8066 phba->work_ha, work_ha_copy,
8067 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008068 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008069
James Smart858c9f62007-06-17 19:56:39 -05008070 control &=
8071 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008072 writel(control, phba->HCregaddr);
8073 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008074 }
James Smarta58cbd52007-08-02 11:09:43 -04008075 else {
8076 lpfc_debugfs_slow_ring_trc(phba,
8077 "ISR slow ring: pwork:"
8078 "x%x hawork:x%x wait:x%x",
8079 phba->work_ha, work_ha_copy,
8080 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008081 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008082 }
James Smart5b75da22008-12-04 22:39:35 -05008083 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008084 }
8085 }
James Smart5b75da22008-12-04 22:39:35 -05008086 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008087 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008088 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008089 /*
8090 * Check if there is a deferred error condition
8091 * is active
8092 */
8093 if ((HS_FFER1 & phba->work_hs) &&
8094 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8095 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8096 phba->hba_flag |= DEFER_ERATT;
8097 /* Clear all interrupt enable conditions */
8098 writel(0, phba->HCregaddr);
8099 readl(phba->HCregaddr);
8100 }
8101 }
8102
James Smart93996272008-08-24 21:50:30 -04008103 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008104 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008105 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008106 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008107 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008108
8109 /* First check out the status word */
8110 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8111 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008112 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008113 /*
8114 * Stray Mailbox Interrupt, mbxCommand <cmd>
8115 * mbxStatus <status>
8116 */
James Smart09372822008-01-11 01:52:54 -05008117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008118 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008119 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008120 "Interrupt mbxCommand x%x "
8121 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008122 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008123 pmbox->mbxCommand,
8124 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008125 /* clear mailbox attention bit */
8126 work_ha_copy &= ~HA_MBATT;
8127 } else {
James Smart97eab632008-04-07 10:16:05 -04008128 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008129 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008130 phba->last_completion_time = jiffies;
8131 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008132 if (pmb->mbox_cmpl) {
8133 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8134 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008135 }
James Smart09372822008-01-11 01:52:54 -05008136 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8137 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8138
8139 lpfc_debugfs_disc_trc(vport,
8140 LPFC_DISC_TRC_MBOX_VPORT,
8141 "MBOX dflt rpi: : "
8142 "status:x%x rpi:x%x",
8143 (uint32_t)pmbox->mbxStatus,
8144 pmbox->un.varWords[0], 0);
8145
8146 if (!pmbox->mbxStatus) {
8147 mp = (struct lpfc_dmabuf *)
8148 (pmb->context1);
8149 ndlp = (struct lpfc_nodelist *)
8150 pmb->context2;
8151
8152 /* Reg_LOGIN of dflt RPI was
8153 * successful. new lets get
8154 * rid of the RPI using the
8155 * same mbox buffer.
8156 */
8157 lpfc_unreg_login(phba,
8158 vport->vpi,
8159 pmbox->un.varWords[0],
8160 pmb);
8161 pmb->mbox_cmpl =
8162 lpfc_mbx_cmpl_dflt_rpi;
8163 pmb->context1 = mp;
8164 pmb->context2 = ndlp;
8165 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008166 rc = lpfc_sli_issue_mbox(phba,
8167 pmb,
8168 MBX_NOWAIT);
8169 if (rc != MBX_BUSY)
8170 lpfc_printf_log(phba,
8171 KERN_ERR,
8172 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008173 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008174 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008175 if (rc != MBX_NOT_FINISHED)
8176 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008177 }
8178 }
James Smart5b75da22008-12-04 22:39:35 -05008179 spin_lock_irqsave(
8180 &phba->pport->work_port_lock,
8181 iflag);
James Smart09372822008-01-11 01:52:54 -05008182 phba->pport->work_port_events &=
8183 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008184 spin_unlock_irqrestore(
8185 &phba->pport->work_port_lock,
8186 iflag);
James Smart09372822008-01-11 01:52:54 -05008187 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008188 }
James Smart97eab632008-04-07 10:16:05 -04008189 } else
James Smart5b75da22008-12-04 22:39:35 -05008190 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008191
James Smart92d7f7b2007-06-17 19:56:38 -05008192 if ((work_ha_copy & HA_MBATT) &&
8193 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008194send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008195 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008196 do {
8197 rc = lpfc_sli_issue_mbox(phba, NULL,
8198 MBX_NOWAIT);
8199 } while (rc == MBX_NOT_FINISHED);
8200 if (rc != MBX_SUCCESS)
8201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8202 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008203 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008204 }
8205
James Smart5b75da22008-12-04 22:39:35 -05008206 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008207 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008208 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008209 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008210 }
James Smart93996272008-08-24 21:50:30 -04008211 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008212
James Smart3772a992009-05-22 14:50:54 -04008213} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008214
8215/**
James Smart3772a992009-05-22 14:50:54 -04008216 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008217 * @irq: Interrupt number.
8218 * @dev_id: The device context pointer.
8219 *
8220 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008221 * service routine when device with SLI-3 interface spec is enabled with
8222 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8223 * ring event in the HBA. However, when the device is enabled with either
8224 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8225 * device-level interrupt handler. When the PCI slot is in error recovery
8226 * or the HBA is undergoing initialization, the interrupt handler will not
8227 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8228 * the intrrupt context. This function is called without any lock held.
8229 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008230 *
8231 * This function returns IRQ_HANDLED when interrupt is handled else it
8232 * returns IRQ_NONE.
8233 **/
8234irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008235lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008236{
8237 struct lpfc_hba *phba;
8238 uint32_t ha_copy;
8239 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008240 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008241
8242 /* Get the driver's phba structure from the dev_id and
8243 * assume the HBA is not interrupting.
8244 */
8245 phba = (struct lpfc_hba *) dev_id;
8246
8247 if (unlikely(!phba))
8248 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008249
8250 /*
James Smart93996272008-08-24 21:50:30 -04008251 * Stuff needs to be attented to when this function is invoked as an
8252 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008253 */
James Smart93996272008-08-24 21:50:30 -04008254 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008255 /* Check device state for handling interrupt */
8256 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008257 return IRQ_NONE;
8258 /* Need to read HA REG for FCP ring and other ring events */
8259 ha_copy = readl(phba->HAregaddr);
8260 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008261 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008262 /*
8263 * If there is deferred error attention, do not check for
8264 * any interrupt.
8265 */
8266 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008267 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008268 return IRQ_NONE;
8269 }
James Smart93996272008-08-24 21:50:30 -04008270 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8271 phba->HAregaddr);
8272 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008273 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008274 } else
8275 ha_copy = phba->ha_copy;
8276
8277 /*
8278 * Process all events on FCP ring. Take the optimized path for FCP IO.
8279 */
8280 ha_copy &= ~(phba->work_ha_mask);
8281
8282 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008283 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008284 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008285 lpfc_sli_handle_fast_ring_event(phba,
8286 &phba->sli.ring[LPFC_FCP_RING],
8287 status);
James Smarta4bc3372006-12-02 13:34:16 -05008288
8289 if (phba->cfg_multi_ring_support == 2) {
8290 /*
James Smart93996272008-08-24 21:50:30 -04008291 * Process all events on extra ring. Take the optimized path
8292 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008293 */
James Smart93996272008-08-24 21:50:30 -04008294 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008295 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008296 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008297 lpfc_sli_handle_fast_ring_event(phba,
8298 &phba->sli.ring[LPFC_EXTRA_RING],
8299 status);
8300 }
8301 }
dea31012005-04-17 16:05:31 -05008302 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008303} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008304
James Smart93996272008-08-24 21:50:30 -04008305/**
James Smart3772a992009-05-22 14:50:54 -04008306 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008307 * @irq: Interrupt number.
8308 * @dev_id: The device context pointer.
8309 *
James Smart3772a992009-05-22 14:50:54 -04008310 * This function is the HBA device-level interrupt handler to device with
8311 * SLI-3 interface spec, called from the PCI layer when either MSI or
8312 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8313 * requires driver attention. This function invokes the slow-path interrupt
8314 * attention handling function and fast-path interrupt attention handling
8315 * function in turn to process the relevant HBA attention events. This
8316 * function is called without any lock held. It gets the hbalock to access
8317 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008318 *
8319 * This function returns IRQ_HANDLED when interrupt is handled, else it
8320 * returns IRQ_NONE.
8321 **/
8322irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008323lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008324{
8325 struct lpfc_hba *phba;
8326 irqreturn_t sp_irq_rc, fp_irq_rc;
8327 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008328 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008329
8330 /*
8331 * Get the driver's phba structure from the dev_id and
8332 * assume the HBA is not interrupting.
8333 */
8334 phba = (struct lpfc_hba *) dev_id;
8335
8336 if (unlikely(!phba))
8337 return IRQ_NONE;
8338
James Smart3772a992009-05-22 14:50:54 -04008339 /* Check device state for handling interrupt */
8340 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008341 return IRQ_NONE;
8342
8343 spin_lock(&phba->hbalock);
8344 phba->ha_copy = readl(phba->HAregaddr);
8345 if (unlikely(!phba->ha_copy)) {
8346 spin_unlock(&phba->hbalock);
8347 return IRQ_NONE;
8348 } else if (phba->ha_copy & HA_ERATT) {
8349 if (phba->hba_flag & HBA_ERATT_HANDLED)
8350 /* ERATT polling has handled ERATT */
8351 phba->ha_copy &= ~HA_ERATT;
8352 else
8353 /* Indicate interrupt handler handles ERATT */
8354 phba->hba_flag |= HBA_ERATT_HANDLED;
8355 }
8356
James Smarta257bf92009-04-06 18:48:10 -04008357 /*
8358 * If there is deferred error attention, do not check for any interrupt.
8359 */
8360 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8361 spin_unlock_irq(&phba->hbalock);
8362 return IRQ_NONE;
8363 }
8364
James Smart93996272008-08-24 21:50:30 -04008365 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008366 hc_copy = readl(phba->HCregaddr);
8367 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8368 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8369 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008370 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008371 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008372 readl(phba->HAregaddr); /* flush */
8373 spin_unlock(&phba->hbalock);
8374
8375 /*
8376 * Invokes slow-path host attention interrupt handling as appropriate.
8377 */
8378
8379 /* status of events with mailbox and link attention */
8380 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8381
8382 /* status of events with ELS ring */
8383 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8384 status2 >>= (4*LPFC_ELS_RING);
8385
8386 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008387 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008388 else
8389 sp_irq_rc = IRQ_NONE;
8390
8391 /*
8392 * Invoke fast-path host attention interrupt handling as appropriate.
8393 */
8394
8395 /* status of events with FCP ring */
8396 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8397 status1 >>= (4*LPFC_FCP_RING);
8398
8399 /* status of events with extra ring */
8400 if (phba->cfg_multi_ring_support == 2) {
8401 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8402 status2 >>= (4*LPFC_EXTRA_RING);
8403 } else
8404 status2 = 0;
8405
8406 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008407 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008408 else
8409 fp_irq_rc = IRQ_NONE;
8410
8411 /* Return device-level interrupt handling status */
8412 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008413} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008414
8415/**
8416 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8417 * @phba: pointer to lpfc hba data structure.
8418 *
8419 * This routine is invoked by the worker thread to process all the pending
8420 * SLI4 FCP abort XRI events.
8421 **/
8422void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8423{
8424 struct lpfc_cq_event *cq_event;
8425
8426 /* First, declare the fcp xri abort event has been handled */
8427 spin_lock_irq(&phba->hbalock);
8428 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8429 spin_unlock_irq(&phba->hbalock);
8430 /* Now, handle all the fcp xri abort events */
8431 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8432 /* Get the first event from the head of the event queue */
8433 spin_lock_irq(&phba->hbalock);
8434 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8435 cq_event, struct lpfc_cq_event, list);
8436 spin_unlock_irq(&phba->hbalock);
8437 /* Notify aborted XRI for FCP work queue */
8438 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8439 /* Free the event processed back to the free pool */
8440 lpfc_sli4_cq_event_release(phba, cq_event);
8441 }
8442}
8443
8444/**
8445 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8446 * @phba: pointer to lpfc hba data structure.
8447 *
8448 * This routine is invoked by the worker thread to process all the pending
8449 * SLI4 els abort xri events.
8450 **/
8451void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8452{
8453 struct lpfc_cq_event *cq_event;
8454
8455 /* First, declare the els xri abort event has been handled */
8456 spin_lock_irq(&phba->hbalock);
8457 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8458 spin_unlock_irq(&phba->hbalock);
8459 /* Now, handle all the els xri abort events */
8460 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8461 /* Get the first event from the head of the event queue */
8462 spin_lock_irq(&phba->hbalock);
8463 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8464 cq_event, struct lpfc_cq_event, list);
8465 spin_unlock_irq(&phba->hbalock);
8466 /* Notify aborted XRI for ELS work queue */
8467 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8468 /* Free the event processed back to the free pool */
8469 lpfc_sli4_cq_event_release(phba, cq_event);
8470 }
8471}
8472
James Smart341af102010-01-26 23:07:37 -05008473/**
8474 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8475 * @phba: pointer to lpfc hba data structure
8476 * @pIocbIn: pointer to the rspiocbq
8477 * @pIocbOut: pointer to the cmdiocbq
8478 * @wcqe: pointer to the complete wcqe
8479 *
8480 * This routine transfers the fields of a command iocbq to a response iocbq
8481 * by copying all the IOCB fields from command iocbq and transferring the
8482 * completion status information from the complete wcqe.
8483 **/
James Smart4f774512009-05-22 14:52:35 -04008484static void
James Smart341af102010-01-26 23:07:37 -05008485lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8486 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008487 struct lpfc_iocbq *pIocbOut,
8488 struct lpfc_wcqe_complete *wcqe)
8489{
James Smart341af102010-01-26 23:07:37 -05008490 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008491 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8492
8493 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8494 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008495 /* Map WCQE parameters into irspiocb parameters */
8496 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8497 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8498 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8499 pIocbIn->iocb.un.fcpi.fcpi_parm =
8500 pIocbOut->iocb.un.fcpi.fcpi_parm -
8501 wcqe->total_data_placed;
8502 else
8503 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008504 else {
James Smart4f774512009-05-22 14:52:35 -04008505 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008506 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8507 }
James Smart341af102010-01-26 23:07:37 -05008508
8509 /* Pick up HBA exchange busy condition */
8510 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8511 spin_lock_irqsave(&phba->hbalock, iflags);
8512 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8513 spin_unlock_irqrestore(&phba->hbalock, iflags);
8514 }
James Smart4f774512009-05-22 14:52:35 -04008515}
8516
8517/**
James Smart45ed1192009-10-02 15:17:02 -04008518 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8519 * @phba: Pointer to HBA context object.
8520 * @wcqe: Pointer to work-queue completion queue entry.
8521 *
8522 * This routine handles an ELS work-queue completion event and construct
8523 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8524 * discovery engine to handle.
8525 *
8526 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8527 **/
8528static struct lpfc_iocbq *
8529lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8530 struct lpfc_iocbq *irspiocbq)
8531{
8532 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8533 struct lpfc_iocbq *cmdiocbq;
8534 struct lpfc_wcqe_complete *wcqe;
8535 unsigned long iflags;
8536
8537 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8538 spin_lock_irqsave(&phba->hbalock, iflags);
8539 pring->stats.iocb_event++;
8540 /* Look up the ELS command IOCB and create pseudo response IOCB */
8541 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8542 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8543 spin_unlock_irqrestore(&phba->hbalock, iflags);
8544
8545 if (unlikely(!cmdiocbq)) {
8546 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8547 "0386 ELS complete with no corresponding "
8548 "cmdiocb: iotag (%d)\n",
8549 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8550 lpfc_sli_release_iocbq(phba, irspiocbq);
8551 return NULL;
8552 }
8553
8554 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008555 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008556
8557 return irspiocbq;
8558}
8559
8560/**
James Smart04c68492009-05-22 14:52:52 -04008561 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8562 * @phba: Pointer to HBA context object.
8563 * @cqe: Pointer to mailbox completion queue entry.
8564 *
8565 * This routine process a mailbox completion queue entry with asynchrous
8566 * event.
8567 *
8568 * Return: true if work posted to worker thread, otherwise false.
8569 **/
8570static bool
8571lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8572{
8573 struct lpfc_cq_event *cq_event;
8574 unsigned long iflags;
8575
8576 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8577 "0392 Async Event: word0:x%x, word1:x%x, "
8578 "word2:x%x, word3:x%x\n", mcqe->word0,
8579 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8580
8581 /* Allocate a new internal CQ_EVENT entry */
8582 cq_event = lpfc_sli4_cq_event_alloc(phba);
8583 if (!cq_event) {
8584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8585 "0394 Failed to allocate CQ_EVENT entry\n");
8586 return false;
8587 }
8588
8589 /* Move the CQE into an asynchronous event entry */
8590 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8591 spin_lock_irqsave(&phba->hbalock, iflags);
8592 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8593 /* Set the async event flag */
8594 phba->hba_flag |= ASYNC_EVENT;
8595 spin_unlock_irqrestore(&phba->hbalock, iflags);
8596
8597 return true;
8598}
8599
8600/**
8601 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8602 * @phba: Pointer to HBA context object.
8603 * @cqe: Pointer to mailbox completion queue entry.
8604 *
8605 * This routine process a mailbox completion queue entry with mailbox
8606 * completion event.
8607 *
8608 * Return: true if work posted to worker thread, otherwise false.
8609 **/
8610static bool
8611lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8612{
8613 uint32_t mcqe_status;
8614 MAILBOX_t *mbox, *pmbox;
8615 struct lpfc_mqe *mqe;
8616 struct lpfc_vport *vport;
8617 struct lpfc_nodelist *ndlp;
8618 struct lpfc_dmabuf *mp;
8619 unsigned long iflags;
8620 LPFC_MBOXQ_t *pmb;
8621 bool workposted = false;
8622 int rc;
8623
8624 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8625 if (!bf_get(lpfc_trailer_completed, mcqe))
8626 goto out_no_mqe_complete;
8627
8628 /* Get the reference to the active mbox command */
8629 spin_lock_irqsave(&phba->hbalock, iflags);
8630 pmb = phba->sli.mbox_active;
8631 if (unlikely(!pmb)) {
8632 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8633 "1832 No pending MBOX command to handle\n");
8634 spin_unlock_irqrestore(&phba->hbalock, iflags);
8635 goto out_no_mqe_complete;
8636 }
8637 spin_unlock_irqrestore(&phba->hbalock, iflags);
8638 mqe = &pmb->u.mqe;
8639 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8640 mbox = phba->mbox;
8641 vport = pmb->vport;
8642
8643 /* Reset heartbeat timer */
8644 phba->last_completion_time = jiffies;
8645 del_timer(&phba->sli.mbox_tmo);
8646
8647 /* Move mbox data to caller's mailbox region, do endian swapping */
8648 if (pmb->mbox_cmpl && mbox)
8649 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8650 /* Set the mailbox status with SLI4 range 0x4000 */
8651 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8652 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8653 bf_set(lpfc_mqe_status, mqe,
8654 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8655
8656 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8657 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8658 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8659 "MBOX dflt rpi: status:x%x rpi:x%x",
8660 mcqe_status,
8661 pmbox->un.varWords[0], 0);
8662 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8663 mp = (struct lpfc_dmabuf *)(pmb->context1);
8664 ndlp = (struct lpfc_nodelist *)pmb->context2;
8665 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8666 * RID of the PPI using the same mbox buffer.
8667 */
8668 lpfc_unreg_login(phba, vport->vpi,
8669 pmbox->un.varWords[0], pmb);
8670 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8671 pmb->context1 = mp;
8672 pmb->context2 = ndlp;
8673 pmb->vport = vport;
8674 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8675 if (rc != MBX_BUSY)
8676 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8677 LOG_SLI, "0385 rc should "
8678 "have been MBX_BUSY\n");
8679 if (rc != MBX_NOT_FINISHED)
8680 goto send_current_mbox;
8681 }
8682 }
8683 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8684 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8685 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8686
8687 /* There is mailbox completion work to do */
8688 spin_lock_irqsave(&phba->hbalock, iflags);
8689 __lpfc_mbox_cmpl_put(phba, pmb);
8690 phba->work_ha |= HA_MBATT;
8691 spin_unlock_irqrestore(&phba->hbalock, iflags);
8692 workposted = true;
8693
8694send_current_mbox:
8695 spin_lock_irqsave(&phba->hbalock, iflags);
8696 /* Release the mailbox command posting token */
8697 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8698 /* Setting active mailbox pointer need to be in sync to flag clear */
8699 phba->sli.mbox_active = NULL;
8700 spin_unlock_irqrestore(&phba->hbalock, iflags);
8701 /* Wake up worker thread to post the next pending mailbox command */
8702 lpfc_worker_wake_up(phba);
8703out_no_mqe_complete:
8704 if (bf_get(lpfc_trailer_consumed, mcqe))
8705 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8706 return workposted;
8707}
8708
8709/**
8710 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8711 * @phba: Pointer to HBA context object.
8712 * @cqe: Pointer to mailbox completion queue entry.
8713 *
8714 * This routine process a mailbox completion queue entry, it invokes the
8715 * proper mailbox complete handling or asynchrous event handling routine
8716 * according to the MCQE's async bit.
8717 *
8718 * Return: true if work posted to worker thread, otherwise false.
8719 **/
8720static bool
8721lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8722{
8723 struct lpfc_mcqe mcqe;
8724 bool workposted;
8725
8726 /* Copy the mailbox MCQE and convert endian order as needed */
8727 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8728
8729 /* Invoke the proper event handling routine */
8730 if (!bf_get(lpfc_trailer_async, &mcqe))
8731 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8732 else
8733 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8734 return workposted;
8735}
8736
8737/**
James Smart4f774512009-05-22 14:52:35 -04008738 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8739 * @phba: Pointer to HBA context object.
8740 * @wcqe: Pointer to work-queue completion queue entry.
8741 *
8742 * This routine handles an ELS work-queue completion event.
8743 *
8744 * Return: true if work posted to worker thread, otherwise false.
8745 **/
8746static bool
8747lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8748 struct lpfc_wcqe_complete *wcqe)
8749{
James Smart4f774512009-05-22 14:52:35 -04008750 struct lpfc_iocbq *irspiocbq;
8751 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008752
James Smart45ed1192009-10-02 15:17:02 -04008753 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008754 irspiocbq = lpfc_sli_get_iocbq(phba);
8755 if (!irspiocbq) {
8756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8757 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008758 return false;
James Smart4f774512009-05-22 14:52:35 -04008759 }
James Smart4f774512009-05-22 14:52:35 -04008760
James Smart45ed1192009-10-02 15:17:02 -04008761 /* Save off the slow-path queue event for work thread to process */
8762 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008763 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008764 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008765 &phba->sli4_hba.sp_queue_event);
8766 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008767 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008768
James Smart45ed1192009-10-02 15:17:02 -04008769 return true;
James Smart4f774512009-05-22 14:52:35 -04008770}
8771
8772/**
8773 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8774 * @phba: Pointer to HBA context object.
8775 * @wcqe: Pointer to work-queue completion queue entry.
8776 *
8777 * This routine handles slow-path WQ entry comsumed event by invoking the
8778 * proper WQ release routine to the slow-path WQ.
8779 **/
8780static void
8781lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8782 struct lpfc_wcqe_release *wcqe)
8783{
8784 /* Check for the slow-path ELS work queue */
8785 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8786 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8787 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8788 else
8789 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8790 "2579 Slow-path wqe consume event carries "
8791 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8792 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8793 phba->sli4_hba.els_wq->queue_id);
8794}
8795
8796/**
8797 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8798 * @phba: Pointer to HBA context object.
8799 * @cq: Pointer to a WQ completion queue.
8800 * @wcqe: Pointer to work-queue completion queue entry.
8801 *
8802 * This routine handles an XRI abort event.
8803 *
8804 * Return: true if work posted to worker thread, otherwise false.
8805 **/
8806static bool
8807lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8808 struct lpfc_queue *cq,
8809 struct sli4_wcqe_xri_aborted *wcqe)
8810{
8811 bool workposted = false;
8812 struct lpfc_cq_event *cq_event;
8813 unsigned long iflags;
8814
8815 /* Allocate a new internal CQ_EVENT entry */
8816 cq_event = lpfc_sli4_cq_event_alloc(phba);
8817 if (!cq_event) {
8818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8819 "0602 Failed to allocate CQ_EVENT entry\n");
8820 return false;
8821 }
8822
8823 /* Move the CQE into the proper xri abort event list */
8824 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8825 switch (cq->subtype) {
8826 case LPFC_FCP:
8827 spin_lock_irqsave(&phba->hbalock, iflags);
8828 list_add_tail(&cq_event->list,
8829 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8830 /* Set the fcp xri abort event flag */
8831 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8832 spin_unlock_irqrestore(&phba->hbalock, iflags);
8833 workposted = true;
8834 break;
8835 case LPFC_ELS:
8836 spin_lock_irqsave(&phba->hbalock, iflags);
8837 list_add_tail(&cq_event->list,
8838 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8839 /* Set the els xri abort event flag */
8840 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8841 spin_unlock_irqrestore(&phba->hbalock, iflags);
8842 workposted = true;
8843 break;
8844 default:
8845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8846 "0603 Invalid work queue CQE subtype (x%x)\n",
8847 cq->subtype);
8848 workposted = false;
8849 break;
8850 }
8851 return workposted;
8852}
8853
8854/**
James Smart4d9ab992009-10-02 15:16:39 -04008855 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008856 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008857 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008858 *
James Smart4d9ab992009-10-02 15:16:39 -04008859 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008860 *
8861 * Return: true if work posted to worker thread, otherwise false.
8862 **/
8863static bool
James Smart4d9ab992009-10-02 15:16:39 -04008864lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8865{
8866 bool workposted = false;
8867 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8868 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8869 struct hbq_dmabuf *dma_buf;
8870 uint32_t status;
8871 unsigned long iflags;
8872
James Smart4d9ab992009-10-02 15:16:39 -04008873 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8874 goto out;
8875
8876 status = bf_get(lpfc_rcqe_status, rcqe);
8877 switch (status) {
8878 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8880 "2537 Receive Frame Truncated!!\n");
8881 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008882 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008883 spin_lock_irqsave(&phba->hbalock, iflags);
8884 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8885 if (!dma_buf) {
8886 spin_unlock_irqrestore(&phba->hbalock, iflags);
8887 goto out;
8888 }
8889 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8890 /* save off the frame for the word thread to process */
8891 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008892 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008893 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008894 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008895 spin_unlock_irqrestore(&phba->hbalock, iflags);
8896 workposted = true;
8897 break;
8898 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8899 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8900 /* Post more buffers if possible */
8901 spin_lock_irqsave(&phba->hbalock, iflags);
8902 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8903 spin_unlock_irqrestore(&phba->hbalock, iflags);
8904 workposted = true;
8905 break;
8906 }
8907out:
8908 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008909}
8910
8911/**
8912 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8913 * @phba: Pointer to HBA context object.
8914 * @cq: Pointer to the completion queue.
8915 * @wcqe: Pointer to a completion queue entry.
8916 *
8917 * This routine process a slow-path work-queue or recieve queue completion queue
8918 * entry.
8919 *
8920 * Return: true if work posted to worker thread, otherwise false.
8921 **/
8922static bool
8923lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008924 struct lpfc_cqe *cqe)
8925{
James Smart45ed1192009-10-02 15:17:02 -04008926 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008927 bool workposted = false;
8928
8929 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008930 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008931
8932 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008933 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008934 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008935 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008936 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008937 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008938 break;
8939 case CQE_CODE_RELEASE_WQE:
8940 /* Process the WQ release event */
8941 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008942 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008943 break;
8944 case CQE_CODE_XRI_ABORTED:
8945 /* Process the WQ XRI abort event */
8946 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008947 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008948 break;
James Smart4d9ab992009-10-02 15:16:39 -04008949 case CQE_CODE_RECEIVE:
8950 /* Process the RQ event */
8951 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008952 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008953 break;
James Smart4f774512009-05-22 14:52:35 -04008954 default:
8955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8956 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008957 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008958 break;
8959 }
8960 return workposted;
8961}
8962
8963/**
James Smart4f774512009-05-22 14:52:35 -04008964 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8965 * @phba: Pointer to HBA context object.
8966 * @eqe: Pointer to fast-path event queue entry.
8967 *
8968 * This routine process a event queue entry from the slow-path event queue.
8969 * It will check the MajorCode and MinorCode to determine this is for a
8970 * completion event on a completion queue, if not, an error shall be logged
8971 * and just return. Otherwise, it will get to the corresponding completion
8972 * queue and process all the entries on that completion queue, rearm the
8973 * completion queue, and then return.
8974 *
8975 **/
8976static void
8977lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8978{
8979 struct lpfc_queue *cq = NULL, *childq, *speq;
8980 struct lpfc_cqe *cqe;
8981 bool workposted = false;
8982 int ecount = 0;
8983 uint16_t cqid;
8984
James Smart28baac72010-02-12 14:42:03 -05008985 if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04008986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8987 "0359 Not a valid slow-path completion "
8988 "event: majorcode=x%x, minorcode=x%x\n",
8989 bf_get(lpfc_eqe_major_code, eqe),
8990 bf_get(lpfc_eqe_minor_code, eqe));
8991 return;
8992 }
8993
8994 /* Get the reference to the corresponding CQ */
8995 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8996
8997 /* Search for completion queue pointer matching this cqid */
8998 speq = phba->sli4_hba.sp_eq;
8999 list_for_each_entry(childq, &speq->child_list, list) {
9000 if (childq->queue_id == cqid) {
9001 cq = childq;
9002 break;
9003 }
9004 }
9005 if (unlikely(!cq)) {
9006 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9007 "0365 Slow-path CQ identifier (%d) does "
9008 "not exist\n", cqid);
9009 return;
9010 }
9011
9012 /* Process all the entries to the CQ */
9013 switch (cq->type) {
9014 case LPFC_MCQ:
9015 while ((cqe = lpfc_sli4_cq_get(cq))) {
9016 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9017 if (!(++ecount % LPFC_GET_QE_REL_INT))
9018 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9019 }
9020 break;
9021 case LPFC_WCQ:
9022 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04009023 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04009024 if (!(++ecount % LPFC_GET_QE_REL_INT))
9025 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9026 }
9027 break;
9028 default:
9029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9030 "0370 Invalid completion queue type (%d)\n",
9031 cq->type);
9032 return;
9033 }
9034
9035 /* Catch the no cq entry condition, log an error */
9036 if (unlikely(ecount == 0))
9037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9038 "0371 No entry from the CQ: identifier "
9039 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9040
9041 /* In any case, flash and re-arm the RCQ */
9042 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9043
9044 /* wake up worker thread if there are works to be done */
9045 if (workposted)
9046 lpfc_worker_wake_up(phba);
9047}
9048
9049/**
9050 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9051 * @eqe: Pointer to fast-path completion queue entry.
9052 *
9053 * This routine process a fast-path work queue completion entry from fast-path
9054 * event queue for FCP command response completion.
9055 **/
9056static void
9057lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9058 struct lpfc_wcqe_complete *wcqe)
9059{
9060 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9061 struct lpfc_iocbq *cmdiocbq;
9062 struct lpfc_iocbq irspiocbq;
9063 unsigned long iflags;
9064
9065 spin_lock_irqsave(&phba->hbalock, iflags);
9066 pring->stats.iocb_event++;
9067 spin_unlock_irqrestore(&phba->hbalock, iflags);
9068
9069 /* Check for response status */
9070 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9071 /* If resource errors reported from HBA, reduce queue
9072 * depth of the SCSI device.
9073 */
9074 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9075 IOSTAT_LOCAL_REJECT) &&
9076 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9077 phba->lpfc_rampdown_queue_depth(phba);
9078 }
9079 /* Log the error status */
9080 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9081 "0373 FCP complete error: status=x%x, "
9082 "hw_status=x%x, total_data_specified=%d, "
9083 "parameter=x%x, word3=x%x\n",
9084 bf_get(lpfc_wcqe_c_status, wcqe),
9085 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9086 wcqe->total_data_placed, wcqe->parameter,
9087 wcqe->word3);
9088 }
9089
9090 /* Look up the FCP command IOCB and create pseudo response IOCB */
9091 spin_lock_irqsave(&phba->hbalock, iflags);
9092 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9093 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9094 spin_unlock_irqrestore(&phba->hbalock, iflags);
9095 if (unlikely(!cmdiocbq)) {
9096 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9097 "0374 FCP complete with no corresponding "
9098 "cmdiocb: iotag (%d)\n",
9099 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9100 return;
9101 }
9102 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9103 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9104 "0375 FCP cmdiocb not callback function "
9105 "iotag: (%d)\n",
9106 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9107 return;
9108 }
9109
9110 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009111 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009112
James Smart0f65ff62010-02-26 14:14:23 -05009113 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9114 spin_lock_irqsave(&phba->hbalock, iflags);
9115 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9116 spin_unlock_irqrestore(&phba->hbalock, iflags);
9117 }
9118
James Smart4f774512009-05-22 14:52:35 -04009119 /* Pass the cmd_iocb and the rsp state to the upper layer */
9120 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9121}
9122
9123/**
9124 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9125 * @phba: Pointer to HBA context object.
9126 * @cq: Pointer to completion queue.
9127 * @wcqe: Pointer to work-queue completion queue entry.
9128 *
9129 * This routine handles an fast-path WQ entry comsumed event by invoking the
9130 * proper WQ release routine to the slow-path WQ.
9131 **/
9132static void
9133lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9134 struct lpfc_wcqe_release *wcqe)
9135{
9136 struct lpfc_queue *childwq;
9137 bool wqid_matched = false;
9138 uint16_t fcp_wqid;
9139
9140 /* Check for fast-path FCP work queue release */
9141 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9142 list_for_each_entry(childwq, &cq->child_list, list) {
9143 if (childwq->queue_id == fcp_wqid) {
9144 lpfc_sli4_wq_release(childwq,
9145 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9146 wqid_matched = true;
9147 break;
9148 }
9149 }
9150 /* Report warning log message if no match found */
9151 if (wqid_matched != true)
9152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9153 "2580 Fast-path wqe consume event carries "
9154 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9155}
9156
9157/**
9158 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9159 * @cq: Pointer to the completion queue.
9160 * @eqe: Pointer to fast-path completion queue entry.
9161 *
9162 * This routine process a fast-path work queue completion entry from fast-path
9163 * event queue for FCP command response completion.
9164 **/
9165static int
9166lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9167 struct lpfc_cqe *cqe)
9168{
9169 struct lpfc_wcqe_release wcqe;
9170 bool workposted = false;
9171
9172 /* Copy the work queue CQE and convert endian order if needed */
9173 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9174
9175 /* Check and process for different type of WCQE and dispatch */
9176 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9177 case CQE_CODE_COMPL_WQE:
9178 /* Process the WQ complete event */
9179 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9180 (struct lpfc_wcqe_complete *)&wcqe);
9181 break;
9182 case CQE_CODE_RELEASE_WQE:
9183 /* Process the WQ release event */
9184 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9185 (struct lpfc_wcqe_release *)&wcqe);
9186 break;
9187 case CQE_CODE_XRI_ABORTED:
9188 /* Process the WQ XRI abort event */
9189 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9190 (struct sli4_wcqe_xri_aborted *)&wcqe);
9191 break;
9192 default:
9193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9194 "0144 Not a valid WCQE code: x%x\n",
9195 bf_get(lpfc_wcqe_c_code, &wcqe));
9196 break;
9197 }
9198 return workposted;
9199}
9200
9201/**
9202 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9203 * @phba: Pointer to HBA context object.
9204 * @eqe: Pointer to fast-path event queue entry.
9205 *
9206 * This routine process a event queue entry from the fast-path event queue.
9207 * It will check the MajorCode and MinorCode to determine this is for a
9208 * completion event on a completion queue, if not, an error shall be logged
9209 * and just return. Otherwise, it will get to the corresponding completion
9210 * queue and process all the entries on the completion queue, rearm the
9211 * completion queue, and then return.
9212 **/
9213static void
9214lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9215 uint32_t fcp_cqidx)
9216{
9217 struct lpfc_queue *cq;
9218 struct lpfc_cqe *cqe;
9219 bool workposted = false;
9220 uint16_t cqid;
9221 int ecount = 0;
9222
James Smart28baac72010-02-12 14:42:03 -05009223 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9225 "0366 Not a valid fast-path completion "
9226 "event: majorcode=x%x, minorcode=x%x\n",
9227 bf_get(lpfc_eqe_major_code, eqe),
9228 bf_get(lpfc_eqe_minor_code, eqe));
9229 return;
9230 }
9231
9232 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9233 if (unlikely(!cq)) {
9234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9235 "0367 Fast-path completion queue does not "
9236 "exist\n");
9237 return;
9238 }
9239
9240 /* Get the reference to the corresponding CQ */
9241 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9242 if (unlikely(cqid != cq->queue_id)) {
9243 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9244 "0368 Miss-matched fast-path completion "
9245 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9246 cqid, cq->queue_id);
9247 return;
9248 }
9249
9250 /* Process all the entries to the CQ */
9251 while ((cqe = lpfc_sli4_cq_get(cq))) {
9252 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9253 if (!(++ecount % LPFC_GET_QE_REL_INT))
9254 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9255 }
9256
9257 /* Catch the no cq entry condition */
9258 if (unlikely(ecount == 0))
9259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9260 "0369 No entry from fast-path completion "
9261 "queue fcpcqid=%d\n", cq->queue_id);
9262
9263 /* In any case, flash and re-arm the CQ */
9264 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9265
9266 /* wake up worker thread if there are works to be done */
9267 if (workposted)
9268 lpfc_worker_wake_up(phba);
9269}
9270
9271static void
9272lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9273{
9274 struct lpfc_eqe *eqe;
9275
9276 /* walk all the EQ entries and drop on the floor */
9277 while ((eqe = lpfc_sli4_eq_get(eq)))
9278 ;
9279
9280 /* Clear and re-arm the EQ */
9281 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9282}
9283
9284/**
9285 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9286 * @irq: Interrupt number.
9287 * @dev_id: The device context pointer.
9288 *
9289 * This function is directly called from the PCI layer as an interrupt
9290 * service routine when device with SLI-4 interface spec is enabled with
9291 * MSI-X multi-message interrupt mode and there are slow-path events in
9292 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9293 * interrupt mode, this function is called as part of the device-level
9294 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9295 * undergoing initialization, the interrupt handler will not process the
9296 * interrupt. The link attention and ELS ring attention events are handled
9297 * by the worker thread. The interrupt handler signals the worker thread
9298 * and returns for these events. This function is called without any lock
9299 * held. It gets the hbalock to access and update SLI data structures.
9300 *
9301 * This function returns IRQ_HANDLED when interrupt is handled else it
9302 * returns IRQ_NONE.
9303 **/
9304irqreturn_t
9305lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9306{
9307 struct lpfc_hba *phba;
9308 struct lpfc_queue *speq;
9309 struct lpfc_eqe *eqe;
9310 unsigned long iflag;
9311 int ecount = 0;
9312
9313 /*
9314 * Get the driver's phba structure from the dev_id
9315 */
9316 phba = (struct lpfc_hba *)dev_id;
9317
9318 if (unlikely(!phba))
9319 return IRQ_NONE;
9320
9321 /* Get to the EQ struct associated with this vector */
9322 speq = phba->sli4_hba.sp_eq;
9323
9324 /* Check device state for handling interrupt */
9325 if (unlikely(lpfc_intr_state_check(phba))) {
9326 /* Check again for link_state with lock held */
9327 spin_lock_irqsave(&phba->hbalock, iflag);
9328 if (phba->link_state < LPFC_LINK_DOWN)
9329 /* Flush, clear interrupt, and rearm the EQ */
9330 lpfc_sli4_eq_flush(phba, speq);
9331 spin_unlock_irqrestore(&phba->hbalock, iflag);
9332 return IRQ_NONE;
9333 }
9334
9335 /*
9336 * Process all the event on FCP slow-path EQ
9337 */
9338 while ((eqe = lpfc_sli4_eq_get(speq))) {
9339 lpfc_sli4_sp_handle_eqe(phba, eqe);
9340 if (!(++ecount % LPFC_GET_QE_REL_INT))
9341 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9342 }
9343
9344 /* Always clear and re-arm the slow-path EQ */
9345 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9346
9347 /* Catch the no cq entry condition */
9348 if (unlikely(ecount == 0)) {
9349 if (phba->intr_type == MSIX)
9350 /* MSI-X treated interrupt served as no EQ share INT */
9351 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9352 "0357 MSI-X interrupt with no EQE\n");
9353 else
9354 /* Non MSI-X treated on interrupt as EQ share INT */
9355 return IRQ_NONE;
9356 }
9357
9358 return IRQ_HANDLED;
9359} /* lpfc_sli4_sp_intr_handler */
9360
9361/**
9362 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9363 * @irq: Interrupt number.
9364 * @dev_id: The device context pointer.
9365 *
9366 * This function is directly called from the PCI layer as an interrupt
9367 * service routine when device with SLI-4 interface spec is enabled with
9368 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9369 * ring event in the HBA. However, when the device is enabled with either
9370 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9371 * device-level interrupt handler. When the PCI slot is in error recovery
9372 * or the HBA is undergoing initialization, the interrupt handler will not
9373 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9374 * the intrrupt context. This function is called without any lock held.
9375 * It gets the hbalock to access and update SLI data structures. Note that,
9376 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9377 * equal to that of FCP CQ index.
9378 *
9379 * This function returns IRQ_HANDLED when interrupt is handled else it
9380 * returns IRQ_NONE.
9381 **/
9382irqreturn_t
9383lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9384{
9385 struct lpfc_hba *phba;
9386 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9387 struct lpfc_queue *fpeq;
9388 struct lpfc_eqe *eqe;
9389 unsigned long iflag;
9390 int ecount = 0;
9391 uint32_t fcp_eqidx;
9392
9393 /* Get the driver's phba structure from the dev_id */
9394 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9395 phba = fcp_eq_hdl->phba;
9396 fcp_eqidx = fcp_eq_hdl->idx;
9397
9398 if (unlikely(!phba))
9399 return IRQ_NONE;
9400
9401 /* Get to the EQ struct associated with this vector */
9402 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9403
9404 /* Check device state for handling interrupt */
9405 if (unlikely(lpfc_intr_state_check(phba))) {
9406 /* Check again for link_state with lock held */
9407 spin_lock_irqsave(&phba->hbalock, iflag);
9408 if (phba->link_state < LPFC_LINK_DOWN)
9409 /* Flush, clear interrupt, and rearm the EQ */
9410 lpfc_sli4_eq_flush(phba, fpeq);
9411 spin_unlock_irqrestore(&phba->hbalock, iflag);
9412 return IRQ_NONE;
9413 }
9414
9415 /*
9416 * Process all the event on FCP fast-path EQ
9417 */
9418 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9419 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9420 if (!(++ecount % LPFC_GET_QE_REL_INT))
9421 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9422 }
9423
9424 /* Always clear and re-arm the fast-path EQ */
9425 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9426
9427 if (unlikely(ecount == 0)) {
9428 if (phba->intr_type == MSIX)
9429 /* MSI-X treated interrupt served as no EQ share INT */
9430 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9431 "0358 MSI-X interrupt with no EQE\n");
9432 else
9433 /* Non MSI-X treated on interrupt as EQ share INT */
9434 return IRQ_NONE;
9435 }
9436
9437 return IRQ_HANDLED;
9438} /* lpfc_sli4_fp_intr_handler */
9439
9440/**
9441 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9442 * @irq: Interrupt number.
9443 * @dev_id: The device context pointer.
9444 *
9445 * This function is the device-level interrupt handler to device with SLI-4
9446 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9447 * interrupt mode is enabled and there is an event in the HBA which requires
9448 * driver attention. This function invokes the slow-path interrupt attention
9449 * handling function and fast-path interrupt attention handling function in
9450 * turn to process the relevant HBA attention events. This function is called
9451 * without any lock held. It gets the hbalock to access and update SLI data
9452 * structures.
9453 *
9454 * This function returns IRQ_HANDLED when interrupt is handled, else it
9455 * returns IRQ_NONE.
9456 **/
9457irqreturn_t
9458lpfc_sli4_intr_handler(int irq, void *dev_id)
9459{
9460 struct lpfc_hba *phba;
9461 irqreturn_t sp_irq_rc, fp_irq_rc;
9462 bool fp_handled = false;
9463 uint32_t fcp_eqidx;
9464
9465 /* Get the driver's phba structure from the dev_id */
9466 phba = (struct lpfc_hba *)dev_id;
9467
9468 if (unlikely(!phba))
9469 return IRQ_NONE;
9470
9471 /*
9472 * Invokes slow-path host attention interrupt handling as appropriate.
9473 */
9474 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9475
9476 /*
9477 * Invoke fast-path host attention interrupt handling as appropriate.
9478 */
9479 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9480 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9481 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9482 if (fp_irq_rc == IRQ_HANDLED)
9483 fp_handled |= true;
9484 }
9485
9486 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9487} /* lpfc_sli4_intr_handler */
9488
9489/**
9490 * lpfc_sli4_queue_free - free a queue structure and associated memory
9491 * @queue: The queue structure to free.
9492 *
9493 * This function frees a queue structure and the DMAable memeory used for
9494 * the host resident queue. This function must be called after destroying the
9495 * queue on the HBA.
9496 **/
9497void
9498lpfc_sli4_queue_free(struct lpfc_queue *queue)
9499{
9500 struct lpfc_dmabuf *dmabuf;
9501
9502 if (!queue)
9503 return;
9504
9505 while (!list_empty(&queue->page_list)) {
9506 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9507 list);
9508 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9509 dmabuf->virt, dmabuf->phys);
9510 kfree(dmabuf);
9511 }
9512 kfree(queue);
9513 return;
9514}
9515
9516/**
9517 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9518 * @phba: The HBA that this queue is being created on.
9519 * @entry_size: The size of each queue entry for this queue.
9520 * @entry count: The number of entries that this queue will handle.
9521 *
9522 * This function allocates a queue structure and the DMAable memory used for
9523 * the host resident queue. This function must be called before creating the
9524 * queue on the HBA.
9525 **/
9526struct lpfc_queue *
9527lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9528 uint32_t entry_count)
9529{
9530 struct lpfc_queue *queue;
9531 struct lpfc_dmabuf *dmabuf;
9532 int x, total_qe_count;
9533 void *dma_pointer;
9534
9535
9536 queue = kzalloc(sizeof(struct lpfc_queue) +
9537 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9538 if (!queue)
9539 return NULL;
9540 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9541 INIT_LIST_HEAD(&queue->list);
9542 INIT_LIST_HEAD(&queue->page_list);
9543 INIT_LIST_HEAD(&queue->child_list);
9544 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9545 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9546 if (!dmabuf)
9547 goto out_fail;
9548 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9549 PAGE_SIZE, &dmabuf->phys,
9550 GFP_KERNEL);
9551 if (!dmabuf->virt) {
9552 kfree(dmabuf);
9553 goto out_fail;
9554 }
James Smartd11e31d2009-06-10 17:23:06 -04009555 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009556 dmabuf->buffer_tag = x;
9557 list_add_tail(&dmabuf->list, &queue->page_list);
9558 /* initialize queue's entry array */
9559 dma_pointer = dmabuf->virt;
9560 for (; total_qe_count < entry_count &&
9561 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9562 total_qe_count++, dma_pointer += entry_size) {
9563 queue->qe[total_qe_count].address = dma_pointer;
9564 }
9565 }
9566 queue->entry_size = entry_size;
9567 queue->entry_count = entry_count;
9568 queue->phba = phba;
9569
9570 return queue;
9571out_fail:
9572 lpfc_sli4_queue_free(queue);
9573 return NULL;
9574}
9575
9576/**
9577 * lpfc_eq_create - Create an Event Queue on the HBA
9578 * @phba: HBA structure that indicates port to create a queue on.
9579 * @eq: The queue structure to use to create the event queue.
9580 * @imax: The maximum interrupt per second limit.
9581 *
9582 * This function creates an event queue, as detailed in @eq, on a port,
9583 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9584 *
9585 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9586 * is used to get the entry count and entry size that are necessary to
9587 * determine the number of pages to allocate and use for this queue. This
9588 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9589 * event queue. This function is asynchronous and will wait for the mailbox
9590 * command to finish before continuing.
9591 *
9592 * On success this function will return a zero. If unable to allocate enough
9593 * memory this function will return ENOMEM. If the queue create mailbox command
9594 * fails this function will return ENXIO.
9595 **/
9596uint32_t
9597lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9598{
9599 struct lpfc_mbx_eq_create *eq_create;
9600 LPFC_MBOXQ_t *mbox;
9601 int rc, length, status = 0;
9602 struct lpfc_dmabuf *dmabuf;
9603 uint32_t shdr_status, shdr_add_status;
9604 union lpfc_sli4_cfg_shdr *shdr;
9605 uint16_t dmult;
9606
9607 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9608 if (!mbox)
9609 return -ENOMEM;
9610 length = (sizeof(struct lpfc_mbx_eq_create) -
9611 sizeof(struct lpfc_sli4_cfg_mhdr));
9612 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9613 LPFC_MBOX_OPCODE_EQ_CREATE,
9614 length, LPFC_SLI4_MBX_EMBED);
9615 eq_create = &mbox->u.mqe.un.eq_create;
9616 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9617 eq->page_count);
9618 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9619 LPFC_EQE_SIZE);
9620 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9621 /* Calculate delay multiper from maximum interrupt per second */
9622 dmult = LPFC_DMULT_CONST/imax - 1;
9623 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9624 dmult);
9625 switch (eq->entry_count) {
9626 default:
9627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9628 "0360 Unsupported EQ count. (%d)\n",
9629 eq->entry_count);
9630 if (eq->entry_count < 256)
9631 return -EINVAL;
9632 /* otherwise default to smallest count (drop through) */
9633 case 256:
9634 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9635 LPFC_EQ_CNT_256);
9636 break;
9637 case 512:
9638 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9639 LPFC_EQ_CNT_512);
9640 break;
9641 case 1024:
9642 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9643 LPFC_EQ_CNT_1024);
9644 break;
9645 case 2048:
9646 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9647 LPFC_EQ_CNT_2048);
9648 break;
9649 case 4096:
9650 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9651 LPFC_EQ_CNT_4096);
9652 break;
9653 }
9654 list_for_each_entry(dmabuf, &eq->page_list, list) {
9655 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9656 putPaddrLow(dmabuf->phys);
9657 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9658 putPaddrHigh(dmabuf->phys);
9659 }
9660 mbox->vport = phba->pport;
9661 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9662 mbox->context1 = NULL;
9663 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9664 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9665 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9666 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9667 if (shdr_status || shdr_add_status || rc) {
9668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9669 "2500 EQ_CREATE mailbox failed with "
9670 "status x%x add_status x%x, mbx status x%x\n",
9671 shdr_status, shdr_add_status, rc);
9672 status = -ENXIO;
9673 }
9674 eq->type = LPFC_EQ;
9675 eq->subtype = LPFC_NONE;
9676 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9677 if (eq->queue_id == 0xFFFF)
9678 status = -ENXIO;
9679 eq->host_index = 0;
9680 eq->hba_index = 0;
9681
James Smart8fa38512009-07-19 10:01:03 -04009682 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009683 return status;
9684}
9685
9686/**
9687 * lpfc_cq_create - Create a Completion Queue on the HBA
9688 * @phba: HBA structure that indicates port to create a queue on.
9689 * @cq: The queue structure to use to create the completion queue.
9690 * @eq: The event queue to bind this completion queue to.
9691 *
9692 * This function creates a completion queue, as detailed in @wq, on a port,
9693 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9694 *
9695 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9696 * is used to get the entry count and entry size that are necessary to
9697 * determine the number of pages to allocate and use for this queue. The @eq
9698 * is used to indicate which event queue to bind this completion queue to. This
9699 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9700 * completion queue. This function is asynchronous and will wait for the mailbox
9701 * command to finish before continuing.
9702 *
9703 * On success this function will return a zero. If unable to allocate enough
9704 * memory this function will return ENOMEM. If the queue create mailbox command
9705 * fails this function will return ENXIO.
9706 **/
9707uint32_t
9708lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9709 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9710{
9711 struct lpfc_mbx_cq_create *cq_create;
9712 struct lpfc_dmabuf *dmabuf;
9713 LPFC_MBOXQ_t *mbox;
9714 int rc, length, status = 0;
9715 uint32_t shdr_status, shdr_add_status;
9716 union lpfc_sli4_cfg_shdr *shdr;
9717
9718 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9719 if (!mbox)
9720 return -ENOMEM;
9721 length = (sizeof(struct lpfc_mbx_cq_create) -
9722 sizeof(struct lpfc_sli4_cfg_mhdr));
9723 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9724 LPFC_MBOX_OPCODE_CQ_CREATE,
9725 length, LPFC_SLI4_MBX_EMBED);
9726 cq_create = &mbox->u.mqe.un.cq_create;
9727 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9728 cq->page_count);
9729 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9730 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9731 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9732 switch (cq->entry_count) {
9733 default:
9734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9735 "0361 Unsupported CQ count. (%d)\n",
9736 cq->entry_count);
9737 if (cq->entry_count < 256)
9738 return -EINVAL;
9739 /* otherwise default to smallest count (drop through) */
9740 case 256:
9741 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9742 LPFC_CQ_CNT_256);
9743 break;
9744 case 512:
9745 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9746 LPFC_CQ_CNT_512);
9747 break;
9748 case 1024:
9749 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9750 LPFC_CQ_CNT_1024);
9751 break;
9752 }
9753 list_for_each_entry(dmabuf, &cq->page_list, list) {
9754 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9755 putPaddrLow(dmabuf->phys);
9756 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9757 putPaddrHigh(dmabuf->phys);
9758 }
9759 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9760
9761 /* The IOCTL status is embedded in the mailbox subheader. */
9762 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9763 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9764 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9765 if (shdr_status || shdr_add_status || rc) {
9766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9767 "2501 CQ_CREATE mailbox failed with "
9768 "status x%x add_status x%x, mbx status x%x\n",
9769 shdr_status, shdr_add_status, rc);
9770 status = -ENXIO;
9771 goto out;
9772 }
9773 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9774 if (cq->queue_id == 0xFFFF) {
9775 status = -ENXIO;
9776 goto out;
9777 }
9778 /* link the cq onto the parent eq child list */
9779 list_add_tail(&cq->list, &eq->child_list);
9780 /* Set up completion queue's type and subtype */
9781 cq->type = type;
9782 cq->subtype = subtype;
9783 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9784 cq->host_index = 0;
9785 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009786
James Smart8fa38512009-07-19 10:01:03 -04009787out:
9788 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009789 return status;
9790}
9791
9792/**
James Smart04c68492009-05-22 14:52:52 -04009793 * lpfc_mq_create - Create a mailbox Queue on the HBA
9794 * @phba: HBA structure that indicates port to create a queue on.
9795 * @mq: The queue structure to use to create the mailbox queue.
9796 *
9797 * This function creates a mailbox queue, as detailed in @mq, on a port,
9798 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9799 *
9800 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9801 * is used to get the entry count and entry size that are necessary to
9802 * determine the number of pages to allocate and use for this queue. This
9803 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9804 * mailbox queue. This function is asynchronous and will wait for the mailbox
9805 * command to finish before continuing.
9806 *
9807 * On success this function will return a zero. If unable to allocate enough
9808 * memory this function will return ENOMEM. If the queue create mailbox command
9809 * fails this function will return ENXIO.
9810 **/
9811uint32_t
9812lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9813 struct lpfc_queue *cq, uint32_t subtype)
9814{
9815 struct lpfc_mbx_mq_create *mq_create;
9816 struct lpfc_dmabuf *dmabuf;
9817 LPFC_MBOXQ_t *mbox;
9818 int rc, length, status = 0;
9819 uint32_t shdr_status, shdr_add_status;
9820 union lpfc_sli4_cfg_shdr *shdr;
9821
9822 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9823 if (!mbox)
9824 return -ENOMEM;
9825 length = (sizeof(struct lpfc_mbx_mq_create) -
9826 sizeof(struct lpfc_sli4_cfg_mhdr));
9827 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9828 LPFC_MBOX_OPCODE_MQ_CREATE,
9829 length, LPFC_SLI4_MBX_EMBED);
9830 mq_create = &mbox->u.mqe.un.mq_create;
9831 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9832 mq->page_count);
9833 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9834 cq->queue_id);
9835 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9836 switch (mq->entry_count) {
9837 default:
9838 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9839 "0362 Unsupported MQ count. (%d)\n",
9840 mq->entry_count);
9841 if (mq->entry_count < 16)
9842 return -EINVAL;
9843 /* otherwise default to smallest count (drop through) */
9844 case 16:
9845 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9846 LPFC_MQ_CNT_16);
9847 break;
9848 case 32:
9849 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9850 LPFC_MQ_CNT_32);
9851 break;
9852 case 64:
9853 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9854 LPFC_MQ_CNT_64);
9855 break;
9856 case 128:
9857 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9858 LPFC_MQ_CNT_128);
9859 break;
9860 }
9861 list_for_each_entry(dmabuf, &mq->page_list, list) {
9862 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9863 putPaddrLow(dmabuf->phys);
9864 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9865 putPaddrHigh(dmabuf->phys);
9866 }
9867 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9868 /* The IOCTL status is embedded in the mailbox subheader. */
9869 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9870 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9871 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9872 if (shdr_status || shdr_add_status || rc) {
9873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9874 "2502 MQ_CREATE mailbox failed with "
9875 "status x%x add_status x%x, mbx status x%x\n",
9876 shdr_status, shdr_add_status, rc);
9877 status = -ENXIO;
9878 goto out;
9879 }
9880 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9881 if (mq->queue_id == 0xFFFF) {
9882 status = -ENXIO;
9883 goto out;
9884 }
9885 mq->type = LPFC_MQ;
9886 mq->subtype = subtype;
9887 mq->host_index = 0;
9888 mq->hba_index = 0;
9889
9890 /* link the mq onto the parent cq child list */
9891 list_add_tail(&mq->list, &cq->child_list);
9892out:
James Smart8fa38512009-07-19 10:01:03 -04009893 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009894 return status;
9895}
9896
9897/**
James Smart4f774512009-05-22 14:52:35 -04009898 * lpfc_wq_create - Create a Work Queue on the HBA
9899 * @phba: HBA structure that indicates port to create a queue on.
9900 * @wq: The queue structure to use to create the work queue.
9901 * @cq: The completion queue to bind this work queue to.
9902 * @subtype: The subtype of the work queue indicating its functionality.
9903 *
9904 * This function creates a work queue, as detailed in @wq, on a port, described
9905 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9906 *
9907 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9908 * is used to get the entry count and entry size that are necessary to
9909 * determine the number of pages to allocate and use for this queue. The @cq
9910 * is used to indicate which completion queue to bind this work queue to. This
9911 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9912 * work queue. This function is asynchronous and will wait for the mailbox
9913 * command to finish before continuing.
9914 *
9915 * On success this function will return a zero. If unable to allocate enough
9916 * memory this function will return ENOMEM. If the queue create mailbox command
9917 * fails this function will return ENXIO.
9918 **/
9919uint32_t
9920lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9921 struct lpfc_queue *cq, uint32_t subtype)
9922{
9923 struct lpfc_mbx_wq_create *wq_create;
9924 struct lpfc_dmabuf *dmabuf;
9925 LPFC_MBOXQ_t *mbox;
9926 int rc, length, status = 0;
9927 uint32_t shdr_status, shdr_add_status;
9928 union lpfc_sli4_cfg_shdr *shdr;
9929
9930 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9931 if (!mbox)
9932 return -ENOMEM;
9933 length = (sizeof(struct lpfc_mbx_wq_create) -
9934 sizeof(struct lpfc_sli4_cfg_mhdr));
9935 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9936 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9937 length, LPFC_SLI4_MBX_EMBED);
9938 wq_create = &mbox->u.mqe.un.wq_create;
9939 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9940 wq->page_count);
9941 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9942 cq->queue_id);
9943 list_for_each_entry(dmabuf, &wq->page_list, list) {
9944 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9945 putPaddrLow(dmabuf->phys);
9946 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9947 putPaddrHigh(dmabuf->phys);
9948 }
9949 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9950 /* The IOCTL status is embedded in the mailbox subheader. */
9951 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9952 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9953 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9954 if (shdr_status || shdr_add_status || rc) {
9955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9956 "2503 WQ_CREATE mailbox failed with "
9957 "status x%x add_status x%x, mbx status x%x\n",
9958 shdr_status, shdr_add_status, rc);
9959 status = -ENXIO;
9960 goto out;
9961 }
9962 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9963 if (wq->queue_id == 0xFFFF) {
9964 status = -ENXIO;
9965 goto out;
9966 }
9967 wq->type = LPFC_WQ;
9968 wq->subtype = subtype;
9969 wq->host_index = 0;
9970 wq->hba_index = 0;
9971
9972 /* link the wq onto the parent cq child list */
9973 list_add_tail(&wq->list, &cq->child_list);
9974out:
James Smart8fa38512009-07-19 10:01:03 -04009975 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009976 return status;
9977}
9978
9979/**
9980 * lpfc_rq_create - Create a Receive Queue on the HBA
9981 * @phba: HBA structure that indicates port to create a queue on.
9982 * @hrq: The queue structure to use to create the header receive queue.
9983 * @drq: The queue structure to use to create the data receive queue.
9984 * @cq: The completion queue to bind this work queue to.
9985 *
9986 * This function creates a receive buffer queue pair , as detailed in @hrq and
9987 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9988 * to the HBA.
9989 *
9990 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9991 * struct is used to get the entry count that is necessary to determine the
9992 * number of pages to use for this queue. The @cq is used to indicate which
9993 * completion queue to bind received buffers that are posted to these queues to.
9994 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9995 * receive queue pair. This function is asynchronous and will wait for the
9996 * mailbox command to finish before continuing.
9997 *
9998 * On success this function will return a zero. If unable to allocate enough
9999 * memory this function will return ENOMEM. If the queue create mailbox command
10000 * fails this function will return ENXIO.
10001 **/
10002uint32_t
10003lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10004 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10005{
10006 struct lpfc_mbx_rq_create *rq_create;
10007 struct lpfc_dmabuf *dmabuf;
10008 LPFC_MBOXQ_t *mbox;
10009 int rc, length, status = 0;
10010 uint32_t shdr_status, shdr_add_status;
10011 union lpfc_sli4_cfg_shdr *shdr;
10012
10013 if (hrq->entry_count != drq->entry_count)
10014 return -EINVAL;
10015 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10016 if (!mbox)
10017 return -ENOMEM;
10018 length = (sizeof(struct lpfc_mbx_rq_create) -
10019 sizeof(struct lpfc_sli4_cfg_mhdr));
10020 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10021 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10022 length, LPFC_SLI4_MBX_EMBED);
10023 rq_create = &mbox->u.mqe.un.rq_create;
10024 switch (hrq->entry_count) {
10025 default:
10026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10027 "2535 Unsupported RQ count. (%d)\n",
10028 hrq->entry_count);
10029 if (hrq->entry_count < 512)
10030 return -EINVAL;
10031 /* otherwise default to smallest count (drop through) */
10032 case 512:
10033 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10034 LPFC_RQ_RING_SIZE_512);
10035 break;
10036 case 1024:
10037 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10038 LPFC_RQ_RING_SIZE_1024);
10039 break;
10040 case 2048:
10041 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10042 LPFC_RQ_RING_SIZE_2048);
10043 break;
10044 case 4096:
10045 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10046 LPFC_RQ_RING_SIZE_4096);
10047 break;
10048 }
10049 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10050 cq->queue_id);
10051 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10052 hrq->page_count);
10053 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10054 LPFC_HDR_BUF_SIZE);
10055 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10056 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10057 putPaddrLow(dmabuf->phys);
10058 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10059 putPaddrHigh(dmabuf->phys);
10060 }
10061 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10062 /* The IOCTL status is embedded in the mailbox subheader. */
10063 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10064 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10065 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10066 if (shdr_status || shdr_add_status || rc) {
10067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10068 "2504 RQ_CREATE mailbox failed with "
10069 "status x%x add_status x%x, mbx status x%x\n",
10070 shdr_status, shdr_add_status, rc);
10071 status = -ENXIO;
10072 goto out;
10073 }
10074 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10075 if (hrq->queue_id == 0xFFFF) {
10076 status = -ENXIO;
10077 goto out;
10078 }
10079 hrq->type = LPFC_HRQ;
10080 hrq->subtype = subtype;
10081 hrq->host_index = 0;
10082 hrq->hba_index = 0;
10083
10084 /* now create the data queue */
10085 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10086 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10087 length, LPFC_SLI4_MBX_EMBED);
10088 switch (drq->entry_count) {
10089 default:
10090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10091 "2536 Unsupported RQ count. (%d)\n",
10092 drq->entry_count);
10093 if (drq->entry_count < 512)
10094 return -EINVAL;
10095 /* otherwise default to smallest count (drop through) */
10096 case 512:
10097 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10098 LPFC_RQ_RING_SIZE_512);
10099 break;
10100 case 1024:
10101 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10102 LPFC_RQ_RING_SIZE_1024);
10103 break;
10104 case 2048:
10105 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10106 LPFC_RQ_RING_SIZE_2048);
10107 break;
10108 case 4096:
10109 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10110 LPFC_RQ_RING_SIZE_4096);
10111 break;
10112 }
10113 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10114 cq->queue_id);
10115 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10116 drq->page_count);
10117 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10118 LPFC_DATA_BUF_SIZE);
10119 list_for_each_entry(dmabuf, &drq->page_list, list) {
10120 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10121 putPaddrLow(dmabuf->phys);
10122 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10123 putPaddrHigh(dmabuf->phys);
10124 }
10125 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10126 /* The IOCTL status is embedded in the mailbox subheader. */
10127 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10128 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10129 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10130 if (shdr_status || shdr_add_status || rc) {
10131 status = -ENXIO;
10132 goto out;
10133 }
10134 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10135 if (drq->queue_id == 0xFFFF) {
10136 status = -ENXIO;
10137 goto out;
10138 }
10139 drq->type = LPFC_DRQ;
10140 drq->subtype = subtype;
10141 drq->host_index = 0;
10142 drq->hba_index = 0;
10143
10144 /* link the header and data RQs onto the parent cq child list */
10145 list_add_tail(&hrq->list, &cq->child_list);
10146 list_add_tail(&drq->list, &cq->child_list);
10147
10148out:
James Smart8fa38512009-07-19 10:01:03 -040010149 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010150 return status;
10151}
10152
10153/**
10154 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10155 * @eq: The queue structure associated with the queue to destroy.
10156 *
10157 * This function destroys a queue, as detailed in @eq by sending an mailbox
10158 * command, specific to the type of queue, to the HBA.
10159 *
10160 * The @eq struct is used to get the queue ID of the queue to destroy.
10161 *
10162 * On success this function will return a zero. If the queue destroy mailbox
10163 * command fails this function will return ENXIO.
10164 **/
10165uint32_t
10166lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10167{
10168 LPFC_MBOXQ_t *mbox;
10169 int rc, length, status = 0;
10170 uint32_t shdr_status, shdr_add_status;
10171 union lpfc_sli4_cfg_shdr *shdr;
10172
10173 if (!eq)
10174 return -ENODEV;
10175 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10176 if (!mbox)
10177 return -ENOMEM;
10178 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10179 sizeof(struct lpfc_sli4_cfg_mhdr));
10180 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10181 LPFC_MBOX_OPCODE_EQ_DESTROY,
10182 length, LPFC_SLI4_MBX_EMBED);
10183 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10184 eq->queue_id);
10185 mbox->vport = eq->phba->pport;
10186 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10187
10188 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10189 /* The IOCTL status is embedded in the mailbox subheader. */
10190 shdr = (union lpfc_sli4_cfg_shdr *)
10191 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10192 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10193 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10194 if (shdr_status || shdr_add_status || rc) {
10195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10196 "2505 EQ_DESTROY mailbox failed with "
10197 "status x%x add_status x%x, mbx status x%x\n",
10198 shdr_status, shdr_add_status, rc);
10199 status = -ENXIO;
10200 }
10201
10202 /* Remove eq from any list */
10203 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010204 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010205 return status;
10206}
10207
10208/**
10209 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10210 * @cq: The queue structure associated with the queue to destroy.
10211 *
10212 * This function destroys a queue, as detailed in @cq by sending an mailbox
10213 * command, specific to the type of queue, to the HBA.
10214 *
10215 * The @cq struct is used to get the queue ID of the queue to destroy.
10216 *
10217 * On success this function will return a zero. If the queue destroy mailbox
10218 * command fails this function will return ENXIO.
10219 **/
10220uint32_t
10221lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10222{
10223 LPFC_MBOXQ_t *mbox;
10224 int rc, length, status = 0;
10225 uint32_t shdr_status, shdr_add_status;
10226 union lpfc_sli4_cfg_shdr *shdr;
10227
10228 if (!cq)
10229 return -ENODEV;
10230 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10231 if (!mbox)
10232 return -ENOMEM;
10233 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10234 sizeof(struct lpfc_sli4_cfg_mhdr));
10235 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10236 LPFC_MBOX_OPCODE_CQ_DESTROY,
10237 length, LPFC_SLI4_MBX_EMBED);
10238 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10239 cq->queue_id);
10240 mbox->vport = cq->phba->pport;
10241 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10242 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10243 /* The IOCTL status is embedded in the mailbox subheader. */
10244 shdr = (union lpfc_sli4_cfg_shdr *)
10245 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10246 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10247 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10248 if (shdr_status || shdr_add_status || rc) {
10249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10250 "2506 CQ_DESTROY mailbox failed with "
10251 "status x%x add_status x%x, mbx status x%x\n",
10252 shdr_status, shdr_add_status, rc);
10253 status = -ENXIO;
10254 }
10255 /* Remove cq from any list */
10256 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010257 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010258 return status;
10259}
10260
10261/**
James Smart04c68492009-05-22 14:52:52 -040010262 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10263 * @qm: The queue structure associated with the queue to destroy.
10264 *
10265 * This function destroys a queue, as detailed in @mq by sending an mailbox
10266 * command, specific to the type of queue, to the HBA.
10267 *
10268 * The @mq struct is used to get the queue ID of the queue to destroy.
10269 *
10270 * On success this function will return a zero. If the queue destroy mailbox
10271 * command fails this function will return ENXIO.
10272 **/
10273uint32_t
10274lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10275{
10276 LPFC_MBOXQ_t *mbox;
10277 int rc, length, status = 0;
10278 uint32_t shdr_status, shdr_add_status;
10279 union lpfc_sli4_cfg_shdr *shdr;
10280
10281 if (!mq)
10282 return -ENODEV;
10283 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10284 if (!mbox)
10285 return -ENOMEM;
10286 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10287 sizeof(struct lpfc_sli4_cfg_mhdr));
10288 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10289 LPFC_MBOX_OPCODE_MQ_DESTROY,
10290 length, LPFC_SLI4_MBX_EMBED);
10291 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10292 mq->queue_id);
10293 mbox->vport = mq->phba->pport;
10294 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10295 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10296 /* The IOCTL status is embedded in the mailbox subheader. */
10297 shdr = (union lpfc_sli4_cfg_shdr *)
10298 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10301 if (shdr_status || shdr_add_status || rc) {
10302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10303 "2507 MQ_DESTROY mailbox failed with "
10304 "status x%x add_status x%x, mbx status x%x\n",
10305 shdr_status, shdr_add_status, rc);
10306 status = -ENXIO;
10307 }
10308 /* Remove mq from any list */
10309 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010310 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010311 return status;
10312}
10313
10314/**
James Smart4f774512009-05-22 14:52:35 -040010315 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10316 * @wq: The queue structure associated with the queue to destroy.
10317 *
10318 * This function destroys a queue, as detailed in @wq by sending an mailbox
10319 * command, specific to the type of queue, to the HBA.
10320 *
10321 * The @wq struct is used to get the queue ID of the queue to destroy.
10322 *
10323 * On success this function will return a zero. If the queue destroy mailbox
10324 * command fails this function will return ENXIO.
10325 **/
10326uint32_t
10327lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10328{
10329 LPFC_MBOXQ_t *mbox;
10330 int rc, length, status = 0;
10331 uint32_t shdr_status, shdr_add_status;
10332 union lpfc_sli4_cfg_shdr *shdr;
10333
10334 if (!wq)
10335 return -ENODEV;
10336 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10337 if (!mbox)
10338 return -ENOMEM;
10339 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10340 sizeof(struct lpfc_sli4_cfg_mhdr));
10341 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10342 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10343 length, LPFC_SLI4_MBX_EMBED);
10344 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10345 wq->queue_id);
10346 mbox->vport = wq->phba->pport;
10347 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10348 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10349 shdr = (union lpfc_sli4_cfg_shdr *)
10350 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10351 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10352 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10353 if (shdr_status || shdr_add_status || rc) {
10354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10355 "2508 WQ_DESTROY mailbox failed with "
10356 "status x%x add_status x%x, mbx status x%x\n",
10357 shdr_status, shdr_add_status, rc);
10358 status = -ENXIO;
10359 }
10360 /* Remove wq from any list */
10361 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010362 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010363 return status;
10364}
10365
10366/**
10367 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10368 * @rq: The queue structure associated with the queue to destroy.
10369 *
10370 * This function destroys a queue, as detailed in @rq by sending an mailbox
10371 * command, specific to the type of queue, to the HBA.
10372 *
10373 * The @rq struct is used to get the queue ID of the queue to destroy.
10374 *
10375 * On success this function will return a zero. If the queue destroy mailbox
10376 * command fails this function will return ENXIO.
10377 **/
10378uint32_t
10379lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10380 struct lpfc_queue *drq)
10381{
10382 LPFC_MBOXQ_t *mbox;
10383 int rc, length, status = 0;
10384 uint32_t shdr_status, shdr_add_status;
10385 union lpfc_sli4_cfg_shdr *shdr;
10386
10387 if (!hrq || !drq)
10388 return -ENODEV;
10389 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10390 if (!mbox)
10391 return -ENOMEM;
10392 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10393 sizeof(struct mbox_header));
10394 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10395 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10396 length, LPFC_SLI4_MBX_EMBED);
10397 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10398 hrq->queue_id);
10399 mbox->vport = hrq->phba->pport;
10400 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10401 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10402 /* The IOCTL status is embedded in the mailbox subheader. */
10403 shdr = (union lpfc_sli4_cfg_shdr *)
10404 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10405 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10406 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10407 if (shdr_status || shdr_add_status || rc) {
10408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10409 "2509 RQ_DESTROY mailbox failed with "
10410 "status x%x add_status x%x, mbx status x%x\n",
10411 shdr_status, shdr_add_status, rc);
10412 if (rc != MBX_TIMEOUT)
10413 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10414 return -ENXIO;
10415 }
10416 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10417 drq->queue_id);
10418 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10419 shdr = (union lpfc_sli4_cfg_shdr *)
10420 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10421 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10422 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10423 if (shdr_status || shdr_add_status || rc) {
10424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10425 "2510 RQ_DESTROY mailbox failed with "
10426 "status x%x add_status x%x, mbx status x%x\n",
10427 shdr_status, shdr_add_status, rc);
10428 status = -ENXIO;
10429 }
10430 list_del_init(&hrq->list);
10431 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010432 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010433 return status;
10434}
10435
10436/**
10437 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10438 * @phba: The virtual port for which this call being executed.
10439 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10440 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10441 * @xritag: the xritag that ties this io to the SGL pages.
10442 *
10443 * This routine will post the sgl pages for the IO that has the xritag
10444 * that is in the iocbq structure. The xritag is assigned during iocbq
10445 * creation and persists for as long as the driver is loaded.
10446 * if the caller has fewer than 256 scatter gather segments to map then
10447 * pdma_phys_addr1 should be 0.
10448 * If the caller needs to map more than 256 scatter gather segment then
10449 * pdma_phys_addr1 should be a valid physical address.
10450 * physical address for SGLs must be 64 byte aligned.
10451 * If you are going to map 2 SGL's then the first one must have 256 entries
10452 * the second sgl can have between 1 and 256 entries.
10453 *
10454 * Return codes:
10455 * 0 - Success
10456 * -ENXIO, -ENOMEM - Failure
10457 **/
10458int
10459lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10460 dma_addr_t pdma_phys_addr0,
10461 dma_addr_t pdma_phys_addr1,
10462 uint16_t xritag)
10463{
10464 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10465 LPFC_MBOXQ_t *mbox;
10466 int rc;
10467 uint32_t shdr_status, shdr_add_status;
10468 union lpfc_sli4_cfg_shdr *shdr;
10469
10470 if (xritag == NO_XRI) {
10471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10472 "0364 Invalid param:\n");
10473 return -EINVAL;
10474 }
10475
10476 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10477 if (!mbox)
10478 return -ENOMEM;
10479
10480 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10481 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10482 sizeof(struct lpfc_mbx_post_sgl_pages) -
10483 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10484
10485 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10486 &mbox->u.mqe.un.post_sgl_pages;
10487 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10488 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10489
10490 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10491 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10492 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10493 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10494
10495 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10496 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10497 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10498 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10499 if (!phba->sli4_hba.intr_enable)
10500 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10501 else
10502 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10503 /* The IOCTL status is embedded in the mailbox subheader. */
10504 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10505 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10506 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10507 if (rc != MBX_TIMEOUT)
10508 mempool_free(mbox, phba->mbox_mem_pool);
10509 if (shdr_status || shdr_add_status || rc) {
10510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10511 "2511 POST_SGL mailbox failed with "
10512 "status x%x add_status x%x, mbx status x%x\n",
10513 shdr_status, shdr_add_status, rc);
10514 rc = -ENXIO;
10515 }
10516 return 0;
10517}
10518/**
10519 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10520 * @phba: The virtual port for which this call being executed.
10521 *
10522 * This routine will remove all of the sgl pages registered with the hba.
10523 *
10524 * Return codes:
10525 * 0 - Success
10526 * -ENXIO, -ENOMEM - Failure
10527 **/
10528int
10529lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10530{
10531 LPFC_MBOXQ_t *mbox;
10532 int rc;
10533 uint32_t shdr_status, shdr_add_status;
10534 union lpfc_sli4_cfg_shdr *shdr;
10535
10536 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10537 if (!mbox)
10538 return -ENOMEM;
10539
10540 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10541 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10542 LPFC_SLI4_MBX_EMBED);
10543 if (!phba->sli4_hba.intr_enable)
10544 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10545 else
10546 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10547 /* The IOCTL status is embedded in the mailbox subheader. */
10548 shdr = (union lpfc_sli4_cfg_shdr *)
10549 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10550 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10551 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10552 if (rc != MBX_TIMEOUT)
10553 mempool_free(mbox, phba->mbox_mem_pool);
10554 if (shdr_status || shdr_add_status || rc) {
10555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10556 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10557 "status x%x add_status x%x, mbx status x%x\n",
10558 shdr_status, shdr_add_status, rc);
10559 rc = -ENXIO;
10560 }
10561 return rc;
10562}
10563
10564/**
10565 * lpfc_sli4_next_xritag - Get an xritag for the io
10566 * @phba: Pointer to HBA context object.
10567 *
10568 * This function gets an xritag for the iocb. If there is no unused xritag
10569 * it will return 0xffff.
10570 * The function returns the allocated xritag if successful, else returns zero.
10571 * Zero is not a valid xritag.
10572 * The caller is not required to hold any lock.
10573 **/
10574uint16_t
10575lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10576{
10577 uint16_t xritag;
10578
10579 spin_lock_irq(&phba->hbalock);
10580 xritag = phba->sli4_hba.next_xri;
10581 if ((xritag != (uint16_t) -1) && xritag <
10582 (phba->sli4_hba.max_cfg_param.max_xri
10583 + phba->sli4_hba.max_cfg_param.xri_base)) {
10584 phba->sli4_hba.next_xri++;
10585 phba->sli4_hba.max_cfg_param.xri_used++;
10586 spin_unlock_irq(&phba->hbalock);
10587 return xritag;
10588 }
10589 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010590 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010591 "2004 Failed to allocate XRI.last XRITAG is %d"
10592 " Max XRI is %d, Used XRI is %d\n",
10593 phba->sli4_hba.next_xri,
10594 phba->sli4_hba.max_cfg_param.max_xri,
10595 phba->sli4_hba.max_cfg_param.xri_used);
10596 return -1;
10597}
10598
10599/**
10600 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10601 * @phba: pointer to lpfc hba data structure.
10602 *
10603 * This routine is invoked to post a block of driver's sgl pages to the
10604 * HBA using non-embedded mailbox command. No Lock is held. This routine
10605 * is only called when the driver is loading and after all IO has been
10606 * stopped.
10607 **/
10608int
10609lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10610{
10611 struct lpfc_sglq *sglq_entry;
10612 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10613 struct sgl_page_pairs *sgl_pg_pairs;
10614 void *viraddr;
10615 LPFC_MBOXQ_t *mbox;
10616 uint32_t reqlen, alloclen, pg_pairs;
10617 uint32_t mbox_tmo;
10618 uint16_t xritag_start = 0;
10619 int els_xri_cnt, rc = 0;
10620 uint32_t shdr_status, shdr_add_status;
10621 union lpfc_sli4_cfg_shdr *shdr;
10622
10623 /* The number of sgls to be posted */
10624 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10625
10626 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10627 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10628 if (reqlen > PAGE_SIZE) {
10629 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10630 "2559 Block sgl registration required DMA "
10631 "size (%d) great than a page\n", reqlen);
10632 return -ENOMEM;
10633 }
10634 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10635 if (!mbox) {
10636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10637 "2560 Failed to allocate mbox cmd memory\n");
10638 return -ENOMEM;
10639 }
10640
10641 /* Allocate DMA memory and set up the non-embedded mailbox command */
10642 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10643 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10644 LPFC_SLI4_MBX_NEMBED);
10645
10646 if (alloclen < reqlen) {
10647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10648 "0285 Allocated DMA memory size (%d) is "
10649 "less than the requested DMA memory "
10650 "size (%d)\n", alloclen, reqlen);
10651 lpfc_sli4_mbox_cmd_free(phba, mbox);
10652 return -ENOMEM;
10653 }
James Smart4f774512009-05-22 14:52:35 -040010654 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010655 viraddr = mbox->sge_array->addr[0];
10656
10657 /* Set up the SGL pages in the non-embedded DMA pages */
10658 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10659 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10660
10661 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10662 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10663 /* Set up the sge entry */
10664 sgl_pg_pairs->sgl_pg0_addr_lo =
10665 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10666 sgl_pg_pairs->sgl_pg0_addr_hi =
10667 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10668 sgl_pg_pairs->sgl_pg1_addr_lo =
10669 cpu_to_le32(putPaddrLow(0));
10670 sgl_pg_pairs->sgl_pg1_addr_hi =
10671 cpu_to_le32(putPaddrHigh(0));
10672 /* Keep the first xritag on the list */
10673 if (pg_pairs == 0)
10674 xritag_start = sglq_entry->sli4_xritag;
10675 sgl_pg_pairs++;
10676 }
10677 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010678 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010679 /* Perform endian conversion if necessary */
10680 sgl->word0 = cpu_to_le32(sgl->word0);
10681
10682 if (!phba->sli4_hba.intr_enable)
10683 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10684 else {
10685 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10686 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10687 }
10688 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10689 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10690 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10691 if (rc != MBX_TIMEOUT)
10692 lpfc_sli4_mbox_cmd_free(phba, mbox);
10693 if (shdr_status || shdr_add_status || rc) {
10694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10695 "2513 POST_SGL_BLOCK mailbox command failed "
10696 "status x%x add_status x%x mbx status x%x\n",
10697 shdr_status, shdr_add_status, rc);
10698 rc = -ENXIO;
10699 }
10700 return rc;
10701}
10702
10703/**
10704 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10705 * @phba: pointer to lpfc hba data structure.
10706 * @sblist: pointer to scsi buffer list.
10707 * @count: number of scsi buffers on the list.
10708 *
10709 * This routine is invoked to post a block of @count scsi sgl pages from a
10710 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10711 * No Lock is held.
10712 *
10713 **/
10714int
10715lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10716 int cnt)
10717{
10718 struct lpfc_scsi_buf *psb;
10719 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10720 struct sgl_page_pairs *sgl_pg_pairs;
10721 void *viraddr;
10722 LPFC_MBOXQ_t *mbox;
10723 uint32_t reqlen, alloclen, pg_pairs;
10724 uint32_t mbox_tmo;
10725 uint16_t xritag_start = 0;
10726 int rc = 0;
10727 uint32_t shdr_status, shdr_add_status;
10728 dma_addr_t pdma_phys_bpl1;
10729 union lpfc_sli4_cfg_shdr *shdr;
10730
10731 /* Calculate the requested length of the dma memory */
10732 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10733 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10734 if (reqlen > PAGE_SIZE) {
10735 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10736 "0217 Block sgl registration required DMA "
10737 "size (%d) great than a page\n", reqlen);
10738 return -ENOMEM;
10739 }
10740 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10741 if (!mbox) {
10742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10743 "0283 Failed to allocate mbox cmd memory\n");
10744 return -ENOMEM;
10745 }
10746
10747 /* Allocate DMA memory and set up the non-embedded mailbox command */
10748 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10749 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10750 LPFC_SLI4_MBX_NEMBED);
10751
10752 if (alloclen < reqlen) {
10753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10754 "2561 Allocated DMA memory size (%d) is "
10755 "less than the requested DMA memory "
10756 "size (%d)\n", alloclen, reqlen);
10757 lpfc_sli4_mbox_cmd_free(phba, mbox);
10758 return -ENOMEM;
10759 }
James Smart4f774512009-05-22 14:52:35 -040010760 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010761 viraddr = mbox->sge_array->addr[0];
10762
10763 /* Set up the SGL pages in the non-embedded DMA pages */
10764 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10765 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10766
10767 pg_pairs = 0;
10768 list_for_each_entry(psb, sblist, list) {
10769 /* Set up the sge entry */
10770 sgl_pg_pairs->sgl_pg0_addr_lo =
10771 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10772 sgl_pg_pairs->sgl_pg0_addr_hi =
10773 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10774 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10775 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10776 else
10777 pdma_phys_bpl1 = 0;
10778 sgl_pg_pairs->sgl_pg1_addr_lo =
10779 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10780 sgl_pg_pairs->sgl_pg1_addr_hi =
10781 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10782 /* Keep the first xritag on the list */
10783 if (pg_pairs == 0)
10784 xritag_start = psb->cur_iocbq.sli4_xritag;
10785 sgl_pg_pairs++;
10786 pg_pairs++;
10787 }
10788 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10789 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10790 /* Perform endian conversion if necessary */
10791 sgl->word0 = cpu_to_le32(sgl->word0);
10792
10793 if (!phba->sli4_hba.intr_enable)
10794 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10795 else {
10796 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10797 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10798 }
10799 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10800 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10801 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10802 if (rc != MBX_TIMEOUT)
10803 lpfc_sli4_mbox_cmd_free(phba, mbox);
10804 if (shdr_status || shdr_add_status || rc) {
10805 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10806 "2564 POST_SGL_BLOCK mailbox command failed "
10807 "status x%x add_status x%x mbx status x%x\n",
10808 shdr_status, shdr_add_status, rc);
10809 rc = -ENXIO;
10810 }
10811 return rc;
10812}
10813
10814/**
10815 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10816 * @phba: pointer to lpfc_hba struct that the frame was received on
10817 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10818 *
10819 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10820 * valid type of frame that the LPFC driver will handle. This function will
10821 * return a zero if the frame is a valid frame or a non zero value when the
10822 * frame does not pass the check.
10823 **/
10824static int
10825lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10826{
10827 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10828 char *type_names[] = FC_TYPE_NAMES_INIT;
10829 struct fc_vft_header *fc_vft_hdr;
10830
10831 switch (fc_hdr->fh_r_ctl) {
10832 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10833 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10834 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10835 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10836 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10837 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10838 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10839 case FC_RCTL_DD_CMD_STATUS: /* command status */
10840 case FC_RCTL_ELS_REQ: /* extended link services request */
10841 case FC_RCTL_ELS_REP: /* extended link services reply */
10842 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10843 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10844 case FC_RCTL_BA_NOP: /* basic link service NOP */
10845 case FC_RCTL_BA_ABTS: /* basic link service abort */
10846 case FC_RCTL_BA_RMC: /* remove connection */
10847 case FC_RCTL_BA_ACC: /* basic accept */
10848 case FC_RCTL_BA_RJT: /* basic reject */
10849 case FC_RCTL_BA_PRMT:
10850 case FC_RCTL_ACK_1: /* acknowledge_1 */
10851 case FC_RCTL_ACK_0: /* acknowledge_0 */
10852 case FC_RCTL_P_RJT: /* port reject */
10853 case FC_RCTL_F_RJT: /* fabric reject */
10854 case FC_RCTL_P_BSY: /* port busy */
10855 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10856 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10857 case FC_RCTL_LCR: /* link credit reset */
10858 case FC_RCTL_END: /* end */
10859 break;
10860 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10861 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10862 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10863 return lpfc_fc_frame_check(phba, fc_hdr);
10864 default:
10865 goto drop;
10866 }
10867 switch (fc_hdr->fh_type) {
10868 case FC_TYPE_BLS:
10869 case FC_TYPE_ELS:
10870 case FC_TYPE_FCP:
10871 case FC_TYPE_CT:
10872 break;
10873 case FC_TYPE_IP:
10874 case FC_TYPE_ILS:
10875 default:
10876 goto drop;
10877 }
10878 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10879 "2538 Received frame rctl:%s type:%s\n",
10880 rctl_names[fc_hdr->fh_r_ctl],
10881 type_names[fc_hdr->fh_type]);
10882 return 0;
10883drop:
10884 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10885 "2539 Dropped frame rctl:%s type:%s\n",
10886 rctl_names[fc_hdr->fh_r_ctl],
10887 type_names[fc_hdr->fh_type]);
10888 return 1;
10889}
10890
10891/**
10892 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10893 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10894 *
10895 * This function processes the FC header to retrieve the VFI from the VF
10896 * header, if one exists. This function will return the VFI if one exists
10897 * or 0 if no VSAN Header exists.
10898 **/
10899static uint32_t
10900lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10901{
10902 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10903
10904 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10905 return 0;
10906 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10907}
10908
10909/**
10910 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10911 * @phba: Pointer to the HBA structure to search for the vport on
10912 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10913 * @fcfi: The FC Fabric ID that the frame came from
10914 *
10915 * This function searches the @phba for a vport that matches the content of the
10916 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10917 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10918 * returns the matching vport pointer or NULL if unable to match frame to a
10919 * vport.
10920 **/
10921static struct lpfc_vport *
10922lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10923 uint16_t fcfi)
10924{
10925 struct lpfc_vport **vports;
10926 struct lpfc_vport *vport = NULL;
10927 int i;
10928 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10929 fc_hdr->fh_d_id[1] << 8 |
10930 fc_hdr->fh_d_id[2]);
10931
10932 vports = lpfc_create_vport_work_array(phba);
10933 if (vports != NULL)
10934 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10935 if (phba->fcf.fcfi == fcfi &&
10936 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10937 vports[i]->fc_myDID == did) {
10938 vport = vports[i];
10939 break;
10940 }
10941 }
10942 lpfc_destroy_vport_work_array(phba, vports);
10943 return vport;
10944}
10945
10946/**
James Smart45ed1192009-10-02 15:17:02 -040010947 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10948 * @vport: The vport to work on.
10949 *
10950 * This function updates the receive sequence time stamp for this vport. The
10951 * receive sequence time stamp indicates the time that the last frame of the
10952 * the sequence that has been idle for the longest amount of time was received.
10953 * the driver uses this time stamp to indicate if any received sequences have
10954 * timed out.
10955 **/
10956void
10957lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10958{
10959 struct lpfc_dmabuf *h_buf;
10960 struct hbq_dmabuf *dmabuf = NULL;
10961
10962 /* get the oldest sequence on the rcv list */
10963 h_buf = list_get_first(&vport->rcv_buffer_list,
10964 struct lpfc_dmabuf, list);
10965 if (!h_buf)
10966 return;
10967 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10968 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10969}
10970
10971/**
10972 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10973 * @vport: The vport that the received sequences were sent to.
10974 *
10975 * This function cleans up all outstanding received sequences. This is called
10976 * by the driver when a link event or user action invalidates all the received
10977 * sequences.
10978 **/
10979void
10980lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10981{
10982 struct lpfc_dmabuf *h_buf, *hnext;
10983 struct lpfc_dmabuf *d_buf, *dnext;
10984 struct hbq_dmabuf *dmabuf = NULL;
10985
10986 /* start with the oldest sequence on the rcv list */
10987 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10988 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10989 list_del_init(&dmabuf->hbuf.list);
10990 list_for_each_entry_safe(d_buf, dnext,
10991 &dmabuf->dbuf.list, list) {
10992 list_del_init(&d_buf->list);
10993 lpfc_in_buf_free(vport->phba, d_buf);
10994 }
10995 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10996 }
10997}
10998
10999/**
11000 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
11001 * @vport: The vport that the received sequences were sent to.
11002 *
11003 * This function determines whether any received sequences have timed out by
11004 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11005 * indicates that there is at least one timed out sequence this routine will
11006 * go through the received sequences one at a time from most inactive to most
11007 * active to determine which ones need to be cleaned up. Once it has determined
11008 * that a sequence needs to be cleaned up it will simply free up the resources
11009 * without sending an abort.
11010 **/
11011void
11012lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11013{
11014 struct lpfc_dmabuf *h_buf, *hnext;
11015 struct lpfc_dmabuf *d_buf, *dnext;
11016 struct hbq_dmabuf *dmabuf = NULL;
11017 unsigned long timeout;
11018 int abort_count = 0;
11019
11020 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11021 vport->rcv_buffer_time_stamp);
11022 if (list_empty(&vport->rcv_buffer_list) ||
11023 time_before(jiffies, timeout))
11024 return;
11025 /* start with the oldest sequence on the rcv list */
11026 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11027 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11028 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11029 dmabuf->time_stamp);
11030 if (time_before(jiffies, timeout))
11031 break;
11032 abort_count++;
11033 list_del_init(&dmabuf->hbuf.list);
11034 list_for_each_entry_safe(d_buf, dnext,
11035 &dmabuf->dbuf.list, list) {
11036 list_del_init(&d_buf->list);
11037 lpfc_in_buf_free(vport->phba, d_buf);
11038 }
11039 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11040 }
11041 if (abort_count)
11042 lpfc_update_rcv_time_stamp(vport);
11043}
11044
11045/**
James Smart4f774512009-05-22 14:52:35 -040011046 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11047 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11048 *
11049 * This function searches through the existing incomplete sequences that have
11050 * been sent to this @vport. If the frame matches one of the incomplete
11051 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11052 * make up that sequence. If no sequence is found that matches this frame then
11053 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11054 * This function returns a pointer to the first dmabuf in the sequence list that
11055 * the frame was linked to.
11056 **/
11057static struct hbq_dmabuf *
11058lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11059{
11060 struct fc_frame_header *new_hdr;
11061 struct fc_frame_header *temp_hdr;
11062 struct lpfc_dmabuf *d_buf;
11063 struct lpfc_dmabuf *h_buf;
11064 struct hbq_dmabuf *seq_dmabuf = NULL;
11065 struct hbq_dmabuf *temp_dmabuf = NULL;
11066
James Smart4d9ab992009-10-02 15:16:39 -040011067 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011068 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011069 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11070 /* Use the hdr_buf to find the sequence that this frame belongs to */
11071 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11072 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11073 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11074 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11075 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11076 continue;
11077 /* found a pending sequence that matches this frame */
11078 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11079 break;
11080 }
11081 if (!seq_dmabuf) {
11082 /*
11083 * This indicates first frame received for this sequence.
11084 * Queue the buffer on the vport's rcv_buffer_list.
11085 */
11086 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011087 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011088 return dmabuf;
11089 }
11090 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011091 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11092 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011093 list_del_init(&seq_dmabuf->hbuf.list);
11094 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11095 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011096 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011097 return dmabuf;
11098 }
James Smart45ed1192009-10-02 15:17:02 -040011099 /* move this sequence to the tail to indicate a young sequence */
11100 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11101 seq_dmabuf->time_stamp = jiffies;
11102 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011103 if (list_empty(&seq_dmabuf->dbuf.list)) {
11104 temp_hdr = dmabuf->hbuf.virt;
11105 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11106 return seq_dmabuf;
11107 }
James Smart4f774512009-05-22 14:52:35 -040011108 /* find the correct place in the sequence to insert this frame */
11109 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11110 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11111 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11112 /*
11113 * If the frame's sequence count is greater than the frame on
11114 * the list then insert the frame right after this frame
11115 */
James Smarteeead812009-12-21 17:01:23 -050011116 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11117 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011118 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11119 return seq_dmabuf;
11120 }
11121 }
11122 return NULL;
11123}
11124
11125/**
James Smart6669f9b2009-10-02 15:16:45 -040011126 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11127 * @vport: pointer to a vitural port
11128 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11129 *
11130 * This function tries to abort from the partially assembed sequence, described
11131 * by the information from basic abbort @dmabuf. It checks to see whether such
11132 * partially assembled sequence held by the driver. If so, it shall free up all
11133 * the frames from the partially assembled sequence.
11134 *
11135 * Return
11136 * true -- if there is matching partially assembled sequence present and all
11137 * the frames freed with the sequence;
11138 * false -- if there is no matching partially assembled sequence present so
11139 * nothing got aborted in the lower layer driver
11140 **/
11141static bool
11142lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11143 struct hbq_dmabuf *dmabuf)
11144{
11145 struct fc_frame_header *new_hdr;
11146 struct fc_frame_header *temp_hdr;
11147 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11148 struct hbq_dmabuf *seq_dmabuf = NULL;
11149
11150 /* Use the hdr_buf to find the sequence that matches this frame */
11151 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11152 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11153 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11154 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11155 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11156 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11157 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11158 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11159 continue;
11160 /* found a pending sequence that matches this frame */
11161 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11162 break;
11163 }
11164
11165 /* Free up all the frames from the partially assembled sequence */
11166 if (seq_dmabuf) {
11167 list_for_each_entry_safe(d_buf, n_buf,
11168 &seq_dmabuf->dbuf.list, list) {
11169 list_del_init(&d_buf->list);
11170 lpfc_in_buf_free(vport->phba, d_buf);
11171 }
11172 return true;
11173 }
11174 return false;
11175}
11176
11177/**
11178 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11179 * @phba: Pointer to HBA context object.
11180 * @cmd_iocbq: pointer to the command iocbq structure.
11181 * @rsp_iocbq: pointer to the response iocbq structure.
11182 *
11183 * This function handles the sequence abort accept iocb command complete
11184 * event. It properly releases the memory allocated to the sequence abort
11185 * accept iocb.
11186 **/
11187static void
11188lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11189 struct lpfc_iocbq *cmd_iocbq,
11190 struct lpfc_iocbq *rsp_iocbq)
11191{
11192 if (cmd_iocbq)
11193 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11194}
11195
11196/**
11197 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11198 * @phba: Pointer to HBA context object.
11199 * @fc_hdr: pointer to a FC frame header.
11200 *
11201 * This function sends a basic accept to a previous unsol sequence abort
11202 * event after aborting the sequence handling.
11203 **/
11204static void
11205lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11206 struct fc_frame_header *fc_hdr)
11207{
11208 struct lpfc_iocbq *ctiocb = NULL;
11209 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011210 uint16_t oxid, rxid;
11211 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011212 IOCB_t *icmd;
11213
11214 if (!lpfc_is_link_up(phba))
11215 return;
11216
11217 sid = sli4_sid_from_fc_hdr(fc_hdr);
11218 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011219 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011220
11221 ndlp = lpfc_findnode_did(phba->pport, sid);
11222 if (!ndlp) {
11223 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11224 "1268 Find ndlp returned NULL for oxid:x%x "
11225 "SID:x%x\n", oxid, sid);
11226 return;
11227 }
11228
11229 /* Allocate buffer for acc iocb */
11230 ctiocb = lpfc_sli_get_iocbq(phba);
11231 if (!ctiocb)
11232 return;
11233
James Smart5ffc2662009-11-18 15:39:44 -050011234 /* Extract the F_CTL field from FC_HDR */
11235 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11236
James Smart6669f9b2009-10-02 15:16:45 -040011237 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011238 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011239 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011240 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11241 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11242 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11243
11244 /* Fill in the rest of iocb fields */
11245 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11246 icmd->ulpBdeCount = 0;
11247 icmd->ulpLe = 1;
11248 icmd->ulpClass = CLASS3;
11249 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011250
James Smart6669f9b2009-10-02 15:16:45 -040011251 ctiocb->iocb_cmpl = NULL;
11252 ctiocb->vport = phba->pport;
11253 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11254
James Smart5ffc2662009-11-18 15:39:44 -050011255 if (fctl & FC_FC_EX_CTX) {
11256 /* ABTS sent by responder to CT exchange, construction
11257 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11258 * field and RX_ID from ABTS for RX_ID field.
11259 */
11260 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11261 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11262 ctiocb->sli4_xritag = oxid;
11263 } else {
11264 /* ABTS sent by initiator to CT exchange, construction
11265 * of BA_ACC will need to allocate a new XRI as for the
11266 * XRI_TAG and RX_ID fields.
11267 */
11268 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11269 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11270 ctiocb->sli4_xritag = NO_XRI;
11271 }
11272 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11273
James Smart6669f9b2009-10-02 15:16:45 -040011274 /* Xmit CT abts accept on exchange <xid> */
11275 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11276 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11277 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11278 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11279}
11280
11281/**
11282 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11283 * @vport: Pointer to the vport on which this sequence was received
11284 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11285 *
11286 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11287 * receive sequence is only partially assembed by the driver, it shall abort
11288 * the partially assembled frames for the sequence. Otherwise, if the
11289 * unsolicited receive sequence has been completely assembled and passed to
11290 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11291 * unsolicited sequence has been aborted. After that, it will issue a basic
11292 * accept to accept the abort.
11293 **/
11294void
11295lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11296 struct hbq_dmabuf *dmabuf)
11297{
11298 struct lpfc_hba *phba = vport->phba;
11299 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011300 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011301 bool abts_par;
11302
James Smart6669f9b2009-10-02 15:16:45 -040011303 /* Make a copy of fc_hdr before the dmabuf being released */
11304 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011305 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011306
James Smart5ffc2662009-11-18 15:39:44 -050011307 if (fctl & FC_FC_EX_CTX) {
11308 /*
11309 * ABTS sent by responder to exchange, just free the buffer
11310 */
James Smart6669f9b2009-10-02 15:16:45 -040011311 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011312 } else {
11313 /*
11314 * ABTS sent by initiator to exchange, need to do cleanup
11315 */
11316 /* Try to abort partially assembled seq */
11317 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11318
11319 /* Send abort to ULP if partially seq abort failed */
11320 if (abts_par == false)
11321 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11322 else
11323 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11324 }
James Smart6669f9b2009-10-02 15:16:45 -040011325 /* Send basic accept (BA_ACC) to the abort requester */
11326 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11327}
11328
11329/**
James Smart4f774512009-05-22 14:52:35 -040011330 * lpfc_seq_complete - Indicates if a sequence is complete
11331 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11332 *
11333 * This function checks the sequence, starting with the frame described by
11334 * @dmabuf, to see if all the frames associated with this sequence are present.
11335 * the frames associated with this sequence are linked to the @dmabuf using the
11336 * dbuf list. This function looks for two major things. 1) That the first frame
11337 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11338 * set. 3) That there are no holes in the sequence count. The function will
11339 * return 1 when the sequence is complete, otherwise it will return 0.
11340 **/
11341static int
11342lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11343{
11344 struct fc_frame_header *hdr;
11345 struct lpfc_dmabuf *d_buf;
11346 struct hbq_dmabuf *seq_dmabuf;
11347 uint32_t fctl;
11348 int seq_count = 0;
11349
11350 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11351 /* make sure first fame of sequence has a sequence count of zero */
11352 if (hdr->fh_seq_cnt != seq_count)
11353 return 0;
11354 fctl = (hdr->fh_f_ctl[0] << 16 |
11355 hdr->fh_f_ctl[1] << 8 |
11356 hdr->fh_f_ctl[2]);
11357 /* If last frame of sequence we can return success. */
11358 if (fctl & FC_FC_END_SEQ)
11359 return 1;
11360 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11361 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11362 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11363 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011364 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011365 return 0;
11366 fctl = (hdr->fh_f_ctl[0] << 16 |
11367 hdr->fh_f_ctl[1] << 8 |
11368 hdr->fh_f_ctl[2]);
11369 /* If last frame of sequence we can return success. */
11370 if (fctl & FC_FC_END_SEQ)
11371 return 1;
11372 }
11373 return 0;
11374}
11375
11376/**
11377 * lpfc_prep_seq - Prep sequence for ULP processing
11378 * @vport: Pointer to the vport on which this sequence was received
11379 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11380 *
11381 * This function takes a sequence, described by a list of frames, and creates
11382 * a list of iocbq structures to describe the sequence. This iocbq list will be
11383 * used to issue to the generic unsolicited sequence handler. This routine
11384 * returns a pointer to the first iocbq in the list. If the function is unable
11385 * to allocate an iocbq then it throw out the received frames that were not
11386 * able to be described and return a pointer to the first iocbq. If unable to
11387 * allocate any iocbqs (including the first) this function will return NULL.
11388 **/
11389static struct lpfc_iocbq *
11390lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11391{
11392 struct lpfc_dmabuf *d_buf, *n_buf;
11393 struct lpfc_iocbq *first_iocbq, *iocbq;
11394 struct fc_frame_header *fc_hdr;
11395 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011396 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011397
11398 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11399 /* remove from receive buffer list */
11400 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011401 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011402 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011403 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011404 /* Get an iocbq struct to fill in. */
11405 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11406 if (first_iocbq) {
11407 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011408 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011409 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11410 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11411 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11412 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11413 vport->vpi + vport->phba->vpi_base;
11414 /* put the first buffer into the first IOCBq */
11415 first_iocbq->context2 = &seq_dmabuf->dbuf;
11416 first_iocbq->context3 = NULL;
11417 first_iocbq->iocb.ulpBdeCount = 1;
11418 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11419 LPFC_DATA_BUF_SIZE;
11420 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011421 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011422 bf_get(lpfc_rcqe_length,
11423 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011424 }
11425 iocbq = first_iocbq;
11426 /*
11427 * Each IOCBq can have two Buffers assigned, so go through the list
11428 * of buffers for this sequence and save two buffers in each IOCBq
11429 */
11430 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11431 if (!iocbq) {
11432 lpfc_in_buf_free(vport->phba, d_buf);
11433 continue;
11434 }
11435 if (!iocbq->context3) {
11436 iocbq->context3 = d_buf;
11437 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011438 pbde = (struct ulp_bde64 *)
11439 &iocbq->iocb.unsli3.sli3Words[4];
11440 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011441 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011442 bf_get(lpfc_rcqe_length,
11443 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011444 } else {
11445 iocbq = lpfc_sli_get_iocbq(vport->phba);
11446 if (!iocbq) {
11447 if (first_iocbq) {
11448 first_iocbq->iocb.ulpStatus =
11449 IOSTAT_FCP_RSP_ERROR;
11450 first_iocbq->iocb.un.ulpWord[4] =
11451 IOERR_NO_RESOURCES;
11452 }
11453 lpfc_in_buf_free(vport->phba, d_buf);
11454 continue;
11455 }
11456 iocbq->context2 = d_buf;
11457 iocbq->context3 = NULL;
11458 iocbq->iocb.ulpBdeCount = 1;
11459 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11460 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011461 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011462 bf_get(lpfc_rcqe_length,
11463 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011464 iocbq->iocb.un.rcvels.remoteID = sid;
11465 list_add_tail(&iocbq->list, &first_iocbq->list);
11466 }
11467 }
11468 return first_iocbq;
11469}
11470
James Smart6669f9b2009-10-02 15:16:45 -040011471static void
11472lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11473 struct hbq_dmabuf *seq_dmabuf)
11474{
11475 struct fc_frame_header *fc_hdr;
11476 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11477 struct lpfc_hba *phba = vport->phba;
11478
11479 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11480 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11481 if (!iocbq) {
11482 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11483 "2707 Ring %d handler: Failed to allocate "
11484 "iocb Rctl x%x Type x%x received\n",
11485 LPFC_ELS_RING,
11486 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11487 return;
11488 }
11489 if (!lpfc_complete_unsol_iocb(phba,
11490 &phba->sli.ring[LPFC_ELS_RING],
11491 iocbq, fc_hdr->fh_r_ctl,
11492 fc_hdr->fh_type))
11493 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11494 "2540 Ring %d handler: unexpected Rctl "
11495 "x%x Type x%x received\n",
11496 LPFC_ELS_RING,
11497 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11498
11499 /* Free iocb created in lpfc_prep_seq */
11500 list_for_each_entry_safe(curr_iocb, next_iocb,
11501 &iocbq->list, list) {
11502 list_del_init(&curr_iocb->list);
11503 lpfc_sli_release_iocbq(phba, curr_iocb);
11504 }
11505 lpfc_sli_release_iocbq(phba, iocbq);
11506}
11507
James Smart4f774512009-05-22 14:52:35 -040011508/**
11509 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11510 * @phba: Pointer to HBA context object.
11511 *
11512 * This function is called with no lock held. This function processes all
11513 * the received buffers and gives it to upper layers when a received buffer
11514 * indicates that it is the final frame in the sequence. The interrupt
11515 * service routine processes received buffers at interrupt contexts and adds
11516 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11517 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11518 * appropriate receive function when the final frame in a sequence is received.
11519 **/
James Smart4d9ab992009-10-02 15:16:39 -040011520void
11521lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11522 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011523{
James Smart4d9ab992009-10-02 15:16:39 -040011524 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011525 struct fc_frame_header *fc_hdr;
11526 struct lpfc_vport *vport;
11527 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011528
James Smart4f774512009-05-22 14:52:35 -040011529 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011530 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11531 /* check to see if this a valid type of frame */
11532 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11533 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11534 return;
11535 }
11536 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11537 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011538 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011539 /* throw out the frame */
11540 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11541 return;
11542 }
James Smart6669f9b2009-10-02 15:16:45 -040011543 /* Handle the basic abort sequence (BA_ABTS) event */
11544 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11545 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11546 return;
11547 }
11548
James Smart4d9ab992009-10-02 15:16:39 -040011549 /* Link this frame */
11550 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11551 if (!seq_dmabuf) {
11552 /* unable to add frame to vport - throw it out */
11553 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11554 return;
11555 }
11556 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011557 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011558 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011559
James Smart6669f9b2009-10-02 15:16:45 -040011560 /* Send the complete sequence to the upper layer protocol */
11561 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011562}
James Smart6fb120a2009-05-22 14:52:59 -040011563
11564/**
11565 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11566 * @phba: pointer to lpfc hba data structure.
11567 *
11568 * This routine is invoked to post rpi header templates to the
11569 * HBA consistent with the SLI-4 interface spec. This routine
11570 * posts a PAGE_SIZE memory region to the port to hold up to
11571 * PAGE_SIZE modulo 64 rpi context headers.
11572 *
11573 * This routine does not require any locks. It's usage is expected
11574 * to be driver load or reset recovery when the driver is
11575 * sequential.
11576 *
11577 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011578 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011579 * EIO - The mailbox failed to complete successfully.
11580 * When this error occurs, the driver is not guaranteed
11581 * to have any rpi regions posted to the device and
11582 * must either attempt to repost the regions or take a
11583 * fatal error.
11584 **/
11585int
11586lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11587{
11588 struct lpfc_rpi_hdr *rpi_page;
11589 uint32_t rc = 0;
11590
11591 /* Post all rpi memory regions to the port. */
11592 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11593 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11594 if (rc != MBX_SUCCESS) {
11595 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11596 "2008 Error %d posting all rpi "
11597 "headers\n", rc);
11598 rc = -EIO;
11599 break;
11600 }
11601 }
11602
11603 return rc;
11604}
11605
11606/**
11607 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11608 * @phba: pointer to lpfc hba data structure.
11609 * @rpi_page: pointer to the rpi memory region.
11610 *
11611 * This routine is invoked to post a single rpi header to the
11612 * HBA consistent with the SLI-4 interface spec. This memory region
11613 * maps up to 64 rpi context regions.
11614 *
11615 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011616 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011617 * ENOMEM - No available memory
11618 * EIO - The mailbox failed to complete successfully.
11619 **/
11620int
11621lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11622{
11623 LPFC_MBOXQ_t *mboxq;
11624 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11625 uint32_t rc = 0;
11626 uint32_t mbox_tmo;
11627 uint32_t shdr_status, shdr_add_status;
11628 union lpfc_sli4_cfg_shdr *shdr;
11629
11630 /* The port is notified of the header region via a mailbox command. */
11631 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11632 if (!mboxq) {
11633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11634 "2001 Unable to allocate memory for issuing "
11635 "SLI_CONFIG_SPECIAL mailbox command\n");
11636 return -ENOMEM;
11637 }
11638
11639 /* Post all rpi memory regions to the port. */
11640 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11641 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11642 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11643 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11644 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11645 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11646 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11647 hdr_tmpl, rpi_page->page_count);
11648 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11649 rpi_page->start_rpi);
11650 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11651 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011652 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011653 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11654 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11655 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11656 if (rc != MBX_TIMEOUT)
11657 mempool_free(mboxq, phba->mbox_mem_pool);
11658 if (shdr_status || shdr_add_status || rc) {
11659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11660 "2514 POST_RPI_HDR mailbox failed with "
11661 "status x%x add_status x%x, mbx status x%x\n",
11662 shdr_status, shdr_add_status, rc);
11663 rc = -ENXIO;
11664 }
11665 return rc;
11666}
11667
11668/**
11669 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11670 * @phba: pointer to lpfc hba data structure.
11671 *
11672 * This routine is invoked to post rpi header templates to the
11673 * HBA consistent with the SLI-4 interface spec. This routine
11674 * posts a PAGE_SIZE memory region to the port to hold up to
11675 * PAGE_SIZE modulo 64 rpi context headers.
11676 *
11677 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011678 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011679 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11680 **/
11681int
11682lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11683{
11684 int rpi;
11685 uint16_t max_rpi, rpi_base, rpi_limit;
11686 uint16_t rpi_remaining;
11687 struct lpfc_rpi_hdr *rpi_hdr;
11688
11689 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11690 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11691 rpi_limit = phba->sli4_hba.next_rpi;
11692
11693 /*
11694 * The valid rpi range is not guaranteed to be zero-based. Start
11695 * the search at the rpi_base as reported by the port.
11696 */
11697 spin_lock_irq(&phba->hbalock);
11698 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11699 if (rpi >= rpi_limit || rpi < rpi_base)
11700 rpi = LPFC_RPI_ALLOC_ERROR;
11701 else {
11702 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11703 phba->sli4_hba.max_cfg_param.rpi_used++;
11704 phba->sli4_hba.rpi_count++;
11705 }
11706
11707 /*
11708 * Don't try to allocate more rpi header regions if the device limit
11709 * on available rpis max has been exhausted.
11710 */
11711 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11712 (phba->sli4_hba.rpi_count >= max_rpi)) {
11713 spin_unlock_irq(&phba->hbalock);
11714 return rpi;
11715 }
11716
11717 /*
11718 * If the driver is running low on rpi resources, allocate another
11719 * page now. Note that the next_rpi value is used because
11720 * it represents how many are actually in use whereas max_rpi notes
11721 * how many are supported max by the device.
11722 */
11723 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11724 phba->sli4_hba.rpi_count;
11725 spin_unlock_irq(&phba->hbalock);
11726 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11727 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11728 if (!rpi_hdr) {
11729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11730 "2002 Error Could not grow rpi "
11731 "count\n");
11732 } else {
11733 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11734 }
11735 }
11736
11737 return rpi;
11738}
11739
11740/**
11741 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11742 * @phba: pointer to lpfc hba data structure.
11743 *
11744 * This routine is invoked to release an rpi to the pool of
11745 * available rpis maintained by the driver.
11746 **/
11747void
11748lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11749{
11750 spin_lock_irq(&phba->hbalock);
11751 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11752 phba->sli4_hba.rpi_count--;
11753 phba->sli4_hba.max_cfg_param.rpi_used--;
11754 spin_unlock_irq(&phba->hbalock);
11755}
11756
11757/**
11758 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11759 * @phba: pointer to lpfc hba data structure.
11760 *
11761 * This routine is invoked to remove the memory region that
11762 * provided rpi via a bitmask.
11763 **/
11764void
11765lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11766{
11767 kfree(phba->sli4_hba.rpi_bmask);
11768}
11769
11770/**
11771 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11772 * @phba: pointer to lpfc hba data structure.
11773 *
11774 * This routine is invoked to remove the memory region that
11775 * provided rpi via a bitmask.
11776 **/
11777int
11778lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11779{
11780 LPFC_MBOXQ_t *mboxq;
11781 struct lpfc_hba *phba = ndlp->phba;
11782 int rc;
11783
11784 /* The port is notified of the header region via a mailbox command. */
11785 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11786 if (!mboxq)
11787 return -ENOMEM;
11788
11789 /* Post all rpi memory regions to the port. */
11790 lpfc_resume_rpi(mboxq, ndlp);
11791 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11792 if (rc == MBX_NOT_FINISHED) {
11793 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11794 "2010 Resume RPI Mailbox failed "
11795 "status %d, mbxStatus x%x\n", rc,
11796 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11797 mempool_free(mboxq, phba->mbox_mem_pool);
11798 return -EIO;
11799 }
11800 return 0;
11801}
11802
11803/**
11804 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11805 * @phba: pointer to lpfc hba data structure.
11806 * @vpi: vpi value to activate with the port.
11807 *
11808 * This routine is invoked to activate a vpi with the
11809 * port when the host intends to use vports with a
11810 * nonzero vpi.
11811 *
11812 * Returns:
11813 * 0 success
11814 * -Evalue otherwise
11815 **/
11816int
11817lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11818{
11819 LPFC_MBOXQ_t *mboxq;
11820 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011821 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011822 uint32_t mbox_tmo;
11823
11824 if (vpi == 0)
11825 return -EINVAL;
11826 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11827 if (!mboxq)
11828 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011829 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011830 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11831 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011832 if (rc != MBX_SUCCESS) {
11833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11834 "2022 INIT VPI Mailbox failed "
11835 "status %d, mbxStatus x%x\n", rc,
11836 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011837 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011838 }
James Smart6a9c52c2009-10-02 15:16:51 -040011839 if (rc != MBX_TIMEOUT)
11840 mempool_free(mboxq, phba->mbox_mem_pool);
11841
11842 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011843}
11844
11845/**
11846 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11847 * @phba: pointer to lpfc hba data structure.
11848 * @mboxq: Pointer to mailbox object.
11849 *
11850 * This routine is invoked to manually add a single FCF record. The caller
11851 * must pass a completely initialized FCF_Record. This routine takes
11852 * care of the nonembedded mailbox operations.
11853 **/
11854static void
11855lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11856{
11857 void *virt_addr;
11858 union lpfc_sli4_cfg_shdr *shdr;
11859 uint32_t shdr_status, shdr_add_status;
11860
11861 virt_addr = mboxq->sge_array->addr[0];
11862 /* The IOCTL status is embedded in the mailbox subheader. */
11863 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11864 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11865 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11866
11867 if ((shdr_status || shdr_add_status) &&
11868 (shdr_status != STATUS_FCF_IN_USE))
11869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11870 "2558 ADD_FCF_RECORD mailbox failed with "
11871 "status x%x add_status x%x\n",
11872 shdr_status, shdr_add_status);
11873
11874 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11875}
11876
11877/**
11878 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11879 * @phba: pointer to lpfc hba data structure.
11880 * @fcf_record: pointer to the initialized fcf record to add.
11881 *
11882 * This routine is invoked to manually add a single FCF record. The caller
11883 * must pass a completely initialized FCF_Record. This routine takes
11884 * care of the nonembedded mailbox operations.
11885 **/
11886int
11887lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11888{
11889 int rc = 0;
11890 LPFC_MBOXQ_t *mboxq;
11891 uint8_t *bytep;
11892 void *virt_addr;
11893 dma_addr_t phys_addr;
11894 struct lpfc_mbx_sge sge;
11895 uint32_t alloc_len, req_len;
11896 uint32_t fcfindex;
11897
11898 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11899 if (!mboxq) {
11900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11901 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11902 return -ENOMEM;
11903 }
11904
11905 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11906 sizeof(uint32_t);
11907
11908 /* Allocate DMA memory and set up the non-embedded mailbox command */
11909 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11910 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11911 req_len, LPFC_SLI4_MBX_NEMBED);
11912 if (alloc_len < req_len) {
11913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 "2523 Allocated DMA memory size (x%x) is "
11915 "less than the requested DMA memory "
11916 "size (x%x)\n", alloc_len, req_len);
11917 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11918 return -ENOMEM;
11919 }
11920
11921 /*
11922 * Get the first SGE entry from the non-embedded DMA memory. This
11923 * routine only uses a single SGE.
11924 */
11925 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11926 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011927 virt_addr = mboxq->sge_array->addr[0];
11928 /*
11929 * Configure the FCF record for FCFI 0. This is the driver's
11930 * hardcoded default and gets used in nonFIP mode.
11931 */
11932 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11933 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11934 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11935
11936 /*
11937 * Copy the fcf_index and the FCF Record Data. The data starts after
11938 * the FCoE header plus word10. The data copy needs to be endian
11939 * correct.
11940 */
11941 bytep += sizeof(uint32_t);
11942 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11943 mboxq->vport = phba->pport;
11944 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11945 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11946 if (rc == MBX_NOT_FINISHED) {
11947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11948 "2515 ADD_FCF_RECORD mailbox failed with "
11949 "status 0x%x\n", rc);
11950 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11951 rc = -EIO;
11952 } else
11953 rc = 0;
11954
11955 return rc;
11956}
11957
11958/**
11959 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11960 * @phba: pointer to lpfc hba data structure.
11961 * @fcf_record: pointer to the fcf record to write the default data.
11962 * @fcf_index: FCF table entry index.
11963 *
11964 * This routine is invoked to build the driver's default FCF record. The
11965 * values used are hardcoded. This routine handles memory initialization.
11966 *
11967 **/
11968void
11969lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11970 struct fcf_record *fcf_record,
11971 uint16_t fcf_index)
11972{
11973 memset(fcf_record, 0, sizeof(struct fcf_record));
11974 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11975 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11976 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11977 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11978 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11979 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11980 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11981 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11982 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11983 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11984 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11985 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11986 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011987 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011988 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11989 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11990 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11991 /* Set the VLAN bit map */
11992 if (phba->valid_vlan) {
11993 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11994 = 1 << (phba->vlan_id % 8);
11995 }
11996}
11997
11998/**
11999 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
12000 * @phba: pointer to lpfc hba data structure.
12001 * @fcf_index: FCF table entry offset.
12002 *
12003 * This routine is invoked to read up to @fcf_num of FCF record from the
12004 * device starting with the given @fcf_index.
12005 **/
12006int
12007lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
12008{
12009 int rc = 0, error;
12010 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040012011
James Smart32b97932009-07-19 10:01:21 -040012012 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040012013 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12014 if (!mboxq) {
12015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12016 "2000 Failed to allocate mbox for "
12017 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040012018 error = -ENOMEM;
12019 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040012020 }
James Smartecfd03c2010-02-12 14:41:27 -050012021 /* Construct the read FCF record mailbox command */
12022 rc = lpfc_sli4_mbx_read_fcf_record(phba, mboxq, fcf_index);
12023 if (rc) {
12024 error = -EINVAL;
James Smart4d9ab992009-10-02 15:16:39 -040012025 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040012026 }
James Smartecfd03c2010-02-12 14:41:27 -050012027 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040012028 mboxq->vport = phba->pport;
12029 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
12030 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050012031 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040012032 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050012033 else {
James Smart32b97932009-07-19 10:01:21 -040012034 spin_lock_irq(&phba->hbalock);
12035 phba->hba_flag |= FCF_DISC_INPROGRESS;
12036 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040012037 error = 0;
James Smart32b97932009-07-19 10:01:21 -040012038 }
James Smart4d9ab992009-10-02 15:16:39 -040012039fail_fcfscan:
12040 if (error) {
12041 if (mboxq)
12042 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12043 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12044 spin_lock_irq(&phba->hbalock);
12045 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12046 spin_unlock_irq(&phba->hbalock);
12047 }
James Smart6fb120a2009-05-22 14:52:59 -040012048 return error;
12049}
James Smarta0c87cb2009-07-19 10:01:10 -040012050
12051/**
James Smartecfd03c2010-02-12 14:41:27 -050012052 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12053 * @phba: pointer to lpfc hba data structure.
12054 *
12055 * This routine is the completion routine for the rediscover FCF table mailbox
12056 * command. If the mailbox command returned failure, it will try to stop the
12057 * FCF rediscover wait timer.
12058 **/
12059void
12060lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12061{
12062 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12063 uint32_t shdr_status, shdr_add_status;
12064
12065 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12066
12067 shdr_status = bf_get(lpfc_mbox_hdr_status,
12068 &redisc_fcf->header.cfg_shdr.response);
12069 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12070 &redisc_fcf->header.cfg_shdr.response);
12071 if (shdr_status || shdr_add_status) {
12072 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12073 "2746 Requesting for FCF rediscovery failed "
12074 "status x%x add_status x%x\n",
12075 shdr_status, shdr_add_status);
James Smartfc2b9892010-02-26 14:15:29 -050012076 if (phba->fcf.fcf_flag & FCF_CVL_FOVER) {
12077 spin_lock_irq(&phba->hbalock);
12078 phba->fcf.fcf_flag &= ~FCF_CVL_FOVER;
12079 spin_unlock_irq(&phba->hbalock);
12080 /*
12081 * CVL event triggered FCF rediscover request failed,
12082 * last resort to re-try current registered FCF entry.
12083 */
12084 lpfc_retry_pport_discovery(phba);
12085 } else {
12086 spin_lock_irq(&phba->hbalock);
12087 phba->fcf.fcf_flag &= ~FCF_DEAD_FOVER;
12088 spin_unlock_irq(&phba->hbalock);
12089 /*
12090 * DEAD FCF event triggered FCF rediscover request
12091 * failed, last resort to fail over as a link down
12092 * to FCF registration.
12093 */
12094 lpfc_sli4_fcf_dead_failthrough(phba);
12095 }
James Smartecfd03c2010-02-12 14:41:27 -050012096 } else
12097 /*
12098 * Start FCF rediscovery wait timer for pending FCF
12099 * before rescan FCF record table.
12100 */
12101 lpfc_fcf_redisc_wait_start_timer(phba);
12102
12103 mempool_free(mbox, phba->mbox_mem_pool);
12104}
12105
12106/**
12107 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12108 * @phba: pointer to lpfc hba data structure.
12109 *
12110 * This routine is invoked to request for rediscovery of the entire FCF table
12111 * by the port.
12112 **/
12113int
12114lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12115{
12116 LPFC_MBOXQ_t *mbox;
12117 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12118 int rc, length;
12119
12120 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12121 if (!mbox) {
12122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12123 "2745 Failed to allocate mbox for "
12124 "requesting FCF rediscover.\n");
12125 return -ENOMEM;
12126 }
12127
12128 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12129 sizeof(struct lpfc_sli4_cfg_mhdr));
12130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12131 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12132 length, LPFC_SLI4_MBX_EMBED);
12133
12134 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12135 /* Set count to 0 for invalidating the entire FCF database */
12136 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12137
12138 /* Issue the mailbox command asynchronously */
12139 mbox->vport = phba->pport;
12140 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12141 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12142
12143 if (rc == MBX_NOT_FINISHED) {
12144 mempool_free(mbox, phba->mbox_mem_pool);
12145 return -EIO;
12146 }
12147 return 0;
12148}
12149
12150/**
James Smartfc2b9892010-02-26 14:15:29 -050012151 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
12152 * @phba: pointer to lpfc hba data structure.
12153 *
12154 * This function is the failover routine as a last resort to the FCF DEAD
12155 * event when driver failed to perform fast FCF failover.
12156 **/
12157void
12158lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
12159{
12160 uint32_t link_state;
12161
12162 /*
12163 * Last resort as FCF DEAD event failover will treat this as
12164 * a link down, but save the link state because we don't want
12165 * it to be changed to Link Down unless it is already down.
12166 */
12167 link_state = phba->link_state;
12168 lpfc_linkdown(phba);
12169 phba->link_state = link_state;
12170
12171 /* Unregister FCF if no devices connected to it */
12172 lpfc_unregister_unused_fcf(phba);
12173}
12174
12175/**
James Smarta0c87cb2009-07-19 10:01:10 -040012176 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12177 * @phba: pointer to lpfc hba data structure.
12178 *
12179 * This function read region 23 and parse TLV for port status to
12180 * decide if the user disaled the port. If the TLV indicates the
12181 * port is disabled, the hba_flag is set accordingly.
12182 **/
12183void
12184lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12185{
12186 LPFC_MBOXQ_t *pmb = NULL;
12187 MAILBOX_t *mb;
12188 uint8_t *rgn23_data = NULL;
12189 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12190 int rc;
12191
12192 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12193 if (!pmb) {
12194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12195 "2600 lpfc_sli_read_serdes_param failed to"
12196 " allocate mailbox memory\n");
12197 goto out;
12198 }
12199 mb = &pmb->u.mb;
12200
12201 /* Get adapter Region 23 data */
12202 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12203 if (!rgn23_data)
12204 goto out;
12205
12206 do {
12207 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12208 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12209
12210 if (rc != MBX_SUCCESS) {
12211 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12212 "2601 lpfc_sli_read_link_ste failed to"
12213 " read config region 23 rc 0x%x Status 0x%x\n",
12214 rc, mb->mbxStatus);
12215 mb->un.varDmp.word_cnt = 0;
12216 }
12217 /*
12218 * dump mem may return a zero when finished or we got a
12219 * mailbox error, either way we are done.
12220 */
12221 if (mb->un.varDmp.word_cnt == 0)
12222 break;
12223 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12224 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12225
12226 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12227 rgn23_data + offset,
12228 mb->un.varDmp.word_cnt);
12229 offset += mb->un.varDmp.word_cnt;
12230 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12231
12232 data_size = offset;
12233 offset = 0;
12234
12235 if (!data_size)
12236 goto out;
12237
12238 /* Check the region signature first */
12239 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12241 "2619 Config region 23 has bad signature\n");
12242 goto out;
12243 }
12244 offset += 4;
12245
12246 /* Check the data structure version */
12247 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12249 "2620 Config region 23 has bad version\n");
12250 goto out;
12251 }
12252 offset += 4;
12253
12254 /* Parse TLV entries in the region */
12255 while (offset < data_size) {
12256 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12257 break;
12258 /*
12259 * If the TLV is not driver specific TLV or driver id is
12260 * not linux driver id, skip the record.
12261 */
12262 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12263 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12264 (rgn23_data[offset + 3] != 0)) {
12265 offset += rgn23_data[offset + 1] * 4 + 4;
12266 continue;
12267 }
12268
12269 /* Driver found a driver specific TLV in the config region */
12270 sub_tlv_len = rgn23_data[offset + 1] * 4;
12271 offset += 4;
12272 tlv_offset = 0;
12273
12274 /*
12275 * Search for configured port state sub-TLV.
12276 */
12277 while ((offset < data_size) &&
12278 (tlv_offset < sub_tlv_len)) {
12279 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12280 offset += 4;
12281 tlv_offset += 4;
12282 break;
12283 }
12284 if (rgn23_data[offset] != PORT_STE_TYPE) {
12285 offset += rgn23_data[offset + 1] * 4 + 4;
12286 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12287 continue;
12288 }
12289
12290 /* This HBA contains PORT_STE configured */
12291 if (!rgn23_data[offset + 2])
12292 phba->hba_flag |= LINK_DISABLED;
12293
12294 goto out;
12295 }
12296 }
12297out:
12298 if (pmb)
12299 mempool_free(pmb, phba->mbox_mem_pool);
12300 kfree(rgn23_data);
12301 return;
12302}
James Smart695a8142010-01-26 23:08:03 -050012303
12304/**
12305 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12306 * @vport: pointer to vport data structure.
12307 *
12308 * This function iterate through the mailboxq and clean up all REG_LOGIN
12309 * and REG_VPI mailbox commands associated with the vport. This function
12310 * is called when driver want to restart discovery of the vport due to
12311 * a Clear Virtual Link event.
12312 **/
12313void
12314lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12315{
12316 struct lpfc_hba *phba = vport->phba;
12317 LPFC_MBOXQ_t *mb, *nextmb;
12318 struct lpfc_dmabuf *mp;
12319
12320 spin_lock_irq(&phba->hbalock);
12321 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12322 if (mb->vport != vport)
12323 continue;
12324
12325 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12326 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12327 continue;
12328
12329 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12330 mp = (struct lpfc_dmabuf *) (mb->context1);
12331 if (mp) {
12332 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12333 kfree(mp);
12334 }
12335 }
12336 list_del(&mb->list);
12337 mempool_free(mb, phba->mbox_mem_pool);
12338 }
12339 mb = phba->sli.mbox_active;
12340 if (mb && (mb->vport == vport)) {
12341 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12342 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12343 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12344 }
12345 spin_unlock_irq(&phba->hbalock);
12346}
12347