blob: d51ee7e3273bd7548f3a3f4b49177ea1c466c258 [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 **/
497static struct lpfc_sglq *
498__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;
529 return sglq;
530}
531
532/**
James Smart3621a712009-04-06 18:47:14 -0400533 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400534 * @phba: Pointer to HBA context object.
535 *
536 * This function is called with no lock held. This function
537 * allocates a new driver iocb object from the iocb pool. If the
538 * allocation is successful, it returns pointer to the newly
539 * allocated iocb object else it returns NULL.
540 **/
James Smart2e0fef82007-06-17 19:56:36 -0500541struct lpfc_iocbq *
542lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500543{
James Smart2e0fef82007-06-17 19:56:36 -0500544 struct lpfc_iocbq * iocbq = NULL;
545 unsigned long iflags;
546
547 spin_lock_irqsave(&phba->hbalock, iflags);
548 iocbq = __lpfc_sli_get_iocbq(phba);
549 spin_unlock_irqrestore(&phba->hbalock, iflags);
550 return iocbq;
551}
552
James Smarte59058c2008-08-24 21:49:00 -0400553/**
James Smart4f774512009-05-22 14:52:35 -0400554 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
555 * @phba: Pointer to HBA context object.
556 * @iocbq: Pointer to driver iocb object.
557 *
558 * This function is called with hbalock held to release driver
559 * iocb object to the iocb pool. The iotag in the iocb object
560 * does not change for each use of the iocb object. This function
561 * clears all other fields of the iocb object when it is freed.
562 * The sqlq structure that holds the xritag and phys and virtual
563 * mappings for the scatter gather list is retrieved from the
564 * active array of sglq. The get of the sglq pointer also clears
565 * the entry in the array. If the status of the IO indiactes that
566 * this IO was aborted then the sglq entry it put on the
567 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
568 * IO has good status or fails for any other reason then the sglq
569 * entry is added to the free list (lpfc_sgl_list).
570 **/
571static void
572__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
573{
574 struct lpfc_sglq *sglq;
575 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
576 unsigned long iflag;
577
578 if (iocbq->sli4_xritag == NO_XRI)
579 sglq = NULL;
580 else
581 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
582 if (sglq) {
James Smart341af102010-01-26 23:07:37 -0500583 if (iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) {
James Smart4f774512009-05-22 14:52:35 -0400584 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
585 iflag);
586 list_add(&sglq->list,
587 &phba->sli4_hba.lpfc_abts_els_sgl_list);
588 spin_unlock_irqrestore(
589 &phba->sli4_hba.abts_sgl_list_lock, iflag);
590 } else
591 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
592 }
593
594
595 /*
596 * Clean all volatile data fields, preserve iotag and node struct.
597 */
598 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
599 iocbq->sli4_xritag = NO_XRI;
600 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
601}
602
603/**
James Smart3772a992009-05-22 14:50:54 -0400604 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
605 * @phba: Pointer to HBA context object.
606 * @iocbq: Pointer to driver iocb object.
607 *
608 * This function is called with hbalock held to release driver
609 * iocb object to the iocb pool. The iotag in the iocb object
610 * does not change for each use of the iocb object. This function
611 * clears all other fields of the iocb object when it is freed.
612 **/
613static void
614__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
615{
616 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
617
618 /*
619 * Clean all volatile data fields, preserve iotag and node struct.
620 */
621 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
622 iocbq->sli4_xritag = NO_XRI;
623 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
624}
625
626/**
James Smart3621a712009-04-06 18:47:14 -0400627 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400628 * @phba: Pointer to HBA context object.
629 * @iocbq: Pointer to driver iocb object.
630 *
631 * This function is called with hbalock held to release driver
632 * iocb object to the iocb pool. The iotag in the iocb object
633 * does not change for each use of the iocb object. This function
634 * clears all other fields of the iocb object when it is freed.
635 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100636static void
James Smart2e0fef82007-06-17 19:56:36 -0500637__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
638{
James Smart3772a992009-05-22 14:50:54 -0400639 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500640}
641
James Smarte59058c2008-08-24 21:49:00 -0400642/**
James Smart3621a712009-04-06 18:47:14 -0400643 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400644 * @phba: Pointer to HBA context object.
645 * @iocbq: Pointer to driver iocb object.
646 *
647 * This function is called with no lock held to release the iocb to
648 * iocb pool.
649 **/
James Smart2e0fef82007-06-17 19:56:36 -0500650void
651lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
652{
653 unsigned long iflags;
654
655 /*
656 * Clean all volatile data fields, preserve iotag and node struct.
657 */
658 spin_lock_irqsave(&phba->hbalock, iflags);
659 __lpfc_sli_release_iocbq(phba, iocbq);
660 spin_unlock_irqrestore(&phba->hbalock, iflags);
661}
662
James Smarte59058c2008-08-24 21:49:00 -0400663/**
James Smarta257bf92009-04-06 18:48:10 -0400664 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
665 * @phba: Pointer to HBA context object.
666 * @iocblist: List of IOCBs.
667 * @ulpstatus: ULP status in IOCB command field.
668 * @ulpWord4: ULP word-4 in IOCB command field.
669 *
670 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
671 * on the list by invoking the complete callback function associated with the
672 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
673 * fields.
674 **/
675void
676lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
677 uint32_t ulpstatus, uint32_t ulpWord4)
678{
679 struct lpfc_iocbq *piocb;
680
681 while (!list_empty(iocblist)) {
682 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
683
684 if (!piocb->iocb_cmpl)
685 lpfc_sli_release_iocbq(phba, piocb);
686 else {
687 piocb->iocb.ulpStatus = ulpstatus;
688 piocb->iocb.un.ulpWord[4] = ulpWord4;
689 (piocb->iocb_cmpl) (phba, piocb, piocb);
690 }
691 }
692 return;
693}
694
695/**
James Smart3621a712009-04-06 18:47:14 -0400696 * lpfc_sli_iocb_cmd_type - Get the iocb type
697 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400698 *
699 * This function is called by ring event handler function to get the iocb type.
700 * This function translates the iocb command to an iocb command type used to
701 * decide the final disposition of each completed IOCB.
702 * The function returns
703 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
704 * LPFC_SOL_IOCB if it is a solicited iocb completion
705 * LPFC_ABORT_IOCB if it is an abort iocb
706 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
707 *
708 * The caller is not required to hold any lock.
709 **/
dea31012005-04-17 16:05:31 -0500710static lpfc_iocb_type
711lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
712{
713 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
714
715 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
716 return 0;
717
718 switch (iocb_cmnd) {
719 case CMD_XMIT_SEQUENCE_CR:
720 case CMD_XMIT_SEQUENCE_CX:
721 case CMD_XMIT_BCAST_CN:
722 case CMD_XMIT_BCAST_CX:
723 case CMD_ELS_REQUEST_CR:
724 case CMD_ELS_REQUEST_CX:
725 case CMD_CREATE_XRI_CR:
726 case CMD_CREATE_XRI_CX:
727 case CMD_GET_RPI_CN:
728 case CMD_XMIT_ELS_RSP_CX:
729 case CMD_GET_RPI_CR:
730 case CMD_FCP_IWRITE_CR:
731 case CMD_FCP_IWRITE_CX:
732 case CMD_FCP_IREAD_CR:
733 case CMD_FCP_IREAD_CX:
734 case CMD_FCP_ICMND_CR:
735 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500736 case CMD_FCP_TSEND_CX:
737 case CMD_FCP_TRSP_CX:
738 case CMD_FCP_TRECEIVE_CX:
739 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500740 case CMD_ADAPTER_MSG:
741 case CMD_ADAPTER_DUMP:
742 case CMD_XMIT_SEQUENCE64_CR:
743 case CMD_XMIT_SEQUENCE64_CX:
744 case CMD_XMIT_BCAST64_CN:
745 case CMD_XMIT_BCAST64_CX:
746 case CMD_ELS_REQUEST64_CR:
747 case CMD_ELS_REQUEST64_CX:
748 case CMD_FCP_IWRITE64_CR:
749 case CMD_FCP_IWRITE64_CX:
750 case CMD_FCP_IREAD64_CR:
751 case CMD_FCP_IREAD64_CX:
752 case CMD_FCP_ICMND64_CR:
753 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500754 case CMD_FCP_TSEND64_CX:
755 case CMD_FCP_TRSP64_CX:
756 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500757 case CMD_GEN_REQUEST64_CR:
758 case CMD_GEN_REQUEST64_CX:
759 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400760 case DSSCMD_IWRITE64_CR:
761 case DSSCMD_IWRITE64_CX:
762 case DSSCMD_IREAD64_CR:
763 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -0500764 type = LPFC_SOL_IOCB;
765 break;
766 case CMD_ABORT_XRI_CN:
767 case CMD_ABORT_XRI_CX:
768 case CMD_CLOSE_XRI_CN:
769 case CMD_CLOSE_XRI_CX:
770 case CMD_XRI_ABORTED_CX:
771 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400772 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500773 type = LPFC_ABORT_IOCB;
774 break;
775 case CMD_RCV_SEQUENCE_CX:
776 case CMD_RCV_ELS_REQ_CX:
777 case CMD_RCV_SEQUENCE64_CX:
778 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400779 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500780 case CMD_IOCB_RCV_SEQ64_CX:
781 case CMD_IOCB_RCV_ELS64_CX:
782 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500783 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500784 type = LPFC_UNSOL_IOCB;
785 break;
James Smart3163f722008-02-08 18:50:25 -0500786 case CMD_IOCB_XMIT_MSEQ64_CR:
787 case CMD_IOCB_XMIT_MSEQ64_CX:
788 case CMD_IOCB_RCV_SEQ_LIST64_CX:
789 case CMD_IOCB_RCV_ELS_LIST64_CX:
790 case CMD_IOCB_CLOSE_EXTENDED_CN:
791 case CMD_IOCB_ABORT_EXTENDED_CN:
792 case CMD_IOCB_RET_HBQE64_CN:
793 case CMD_IOCB_FCP_IBIDIR64_CR:
794 case CMD_IOCB_FCP_IBIDIR64_CX:
795 case CMD_IOCB_FCP_ITASKMGT64_CX:
796 case CMD_IOCB_LOGENTRY_CN:
797 case CMD_IOCB_LOGENTRY_ASYNC_CN:
798 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700799 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500800 type = LPFC_UNKNOWN_IOCB;
801 break;
dea31012005-04-17 16:05:31 -0500802 default:
803 type = LPFC_UNKNOWN_IOCB;
804 break;
805 }
806
807 return type;
808}
809
James Smarte59058c2008-08-24 21:49:00 -0400810/**
James Smart3621a712009-04-06 18:47:14 -0400811 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400812 * @phba: Pointer to HBA context object.
813 *
814 * This function is called from SLI initialization code
815 * to configure every ring of the HBA's SLI interface. The
816 * caller is not required to hold any lock. This function issues
817 * a config_ring mailbox command for each ring.
818 * This function returns zero if successful else returns a negative
819 * error code.
820 **/
dea31012005-04-17 16:05:31 -0500821static int
James Smarted957682007-06-17 19:56:37 -0500822lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500823{
824 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500825 LPFC_MBOXQ_t *pmb;
826 MAILBOX_t *pmbox;
827 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500828
James Smarted957682007-06-17 19:56:37 -0500829 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
830 if (!pmb)
831 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400832 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500833 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500834 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500835 lpfc_config_ring(phba, i, pmb);
836 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
837 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400839 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500840 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
841 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400842 rc, pmbox->mbxCommand,
843 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500844 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500845 ret = -ENXIO;
846 break;
dea31012005-04-17 16:05:31 -0500847 }
848 }
James Smarted957682007-06-17 19:56:37 -0500849 mempool_free(pmb, phba->mbox_mem_pool);
850 return ret;
dea31012005-04-17 16:05:31 -0500851}
852
James Smarte59058c2008-08-24 21:49:00 -0400853/**
James Smart3621a712009-04-06 18:47:14 -0400854 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400855 * @phba: Pointer to HBA context object.
856 * @pring: Pointer to driver SLI ring object.
857 * @piocb: Pointer to the driver iocb object.
858 *
859 * This function is called with hbalock held. The function adds the
860 * new iocb to txcmplq of the given ring. This function always returns
861 * 0. If this function is called for ELS ring, this function checks if
862 * there is a vport associated with the ELS command. This function also
863 * starts els_tmofunc timer if this is an ELS command.
864 **/
dea31012005-04-17 16:05:31 -0500865static int
James Smart2e0fef82007-06-17 19:56:36 -0500866lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
867 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500868{
dea31012005-04-17 16:05:31 -0500869 list_add_tail(&piocb->list, &pring->txcmplq);
870 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500871 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
872 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
873 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
874 if (!piocb->vport)
875 BUG();
876 else
877 mod_timer(&piocb->vport->els_tmofunc,
878 jiffies + HZ * (phba->fc_ratov << 1));
879 }
880
dea31012005-04-17 16:05:31 -0500881
James Smart2e0fef82007-06-17 19:56:36 -0500882 return 0;
dea31012005-04-17 16:05:31 -0500883}
884
James Smarte59058c2008-08-24 21:49:00 -0400885/**
James Smart3621a712009-04-06 18:47:14 -0400886 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400887 * @phba: Pointer to HBA context object.
888 * @pring: Pointer to driver SLI ring object.
889 *
890 * This function is called with hbalock held to get next
891 * iocb in txq of the given ring. If there is any iocb in
892 * the txq, the function returns first iocb in the list after
893 * removing the iocb from the list, else it returns NULL.
894 **/
dea31012005-04-17 16:05:31 -0500895static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500896lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500897{
dea31012005-04-17 16:05:31 -0500898 struct lpfc_iocbq *cmd_iocb;
899
James Smart858c9f62007-06-17 19:56:39 -0500900 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
901 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500902 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500903 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500904}
905
James Smarte59058c2008-08-24 21:49:00 -0400906/**
James Smart3621a712009-04-06 18:47:14 -0400907 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400908 * @phba: Pointer to HBA context object.
909 * @pring: Pointer to driver SLI ring object.
910 *
911 * This function is called with hbalock held and the caller must post the
912 * iocb without releasing the lock. If the caller releases the lock,
913 * iocb slot returned by the function is not guaranteed to be available.
914 * The function returns pointer to the next available iocb slot if there
915 * is available slot in the ring, else it returns NULL.
916 * If the get index of the ring is ahead of the put index, the function
917 * will post an error attention event to the worker thread to take the
918 * HBA to offline state.
919 **/
dea31012005-04-17 16:05:31 -0500920static IOCB_t *
921lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
922{
James Smart34b02dc2008-08-24 21:49:55 -0400923 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500924 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500925 if ((pring->next_cmdidx == pring->cmdidx) &&
926 (++pring->next_cmdidx >= max_cmd_idx))
927 pring->next_cmdidx = 0;
928
929 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
930
931 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
932
933 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
934 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400935 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200936 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400937 pring->ringno,
dea31012005-04-17 16:05:31 -0500938 pring->local_getidx, max_cmd_idx);
939
James Smart2e0fef82007-06-17 19:56:36 -0500940 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500941 /*
942 * All error attention handlers are posted to
943 * worker thread
944 */
945 phba->work_ha |= HA_ERATT;
946 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500947
James Smart5e9d9b82008-06-14 22:52:53 -0400948 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500949
950 return NULL;
951 }
952
953 if (pring->local_getidx == pring->next_cmdidx)
954 return NULL;
955 }
956
James Smarted957682007-06-17 19:56:37 -0500957 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500958}
959
James Smarte59058c2008-08-24 21:49:00 -0400960/**
James Smart3621a712009-04-06 18:47:14 -0400961 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400962 * @phba: Pointer to HBA context object.
963 * @iocbq: Pointer to driver iocb object.
964 *
965 * This function gets an iotag for the iocb. If there is no unused iotag and
966 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
967 * array and assigns a new iotag.
968 * The function returns the allocated iotag if successful, else returns zero.
969 * Zero is not a valid iotag.
970 * The caller is not required to hold any lock.
971 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500972uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500973lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500974{
James Smart2e0fef82007-06-17 19:56:36 -0500975 struct lpfc_iocbq **new_arr;
976 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500977 size_t new_len;
978 struct lpfc_sli *psli = &phba->sli;
979 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500980
James Smart2e0fef82007-06-17 19:56:36 -0500981 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500982 iotag = psli->last_iotag;
983 if(++iotag < psli->iocbq_lookup_len) {
984 psli->last_iotag = iotag;
985 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500986 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500987 iocbq->iotag = iotag;
988 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500989 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500990 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
991 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500992 spin_unlock_irq(&phba->hbalock);
993 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500994 GFP_KERNEL);
995 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500996 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500997 old_arr = psli->iocbq_lookup;
998 if (new_len <= psli->iocbq_lookup_len) {
999 /* highly unprobable case */
1000 kfree(new_arr);
1001 iotag = psli->last_iotag;
1002 if(++iotag < psli->iocbq_lookup_len) {
1003 psli->last_iotag = iotag;
1004 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001005 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001006 iocbq->iotag = iotag;
1007 return iotag;
1008 }
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001010 return 0;
1011 }
1012 if (psli->iocbq_lookup)
1013 memcpy(new_arr, old_arr,
1014 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001015 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001016 psli->iocbq_lookup = new_arr;
1017 psli->iocbq_lookup_len = new_len;
1018 psli->last_iotag = iotag;
1019 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001020 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001021 iocbq->iotag = iotag;
1022 kfree(old_arr);
1023 return iotag;
1024 }
James Smart8f6d98d2006-08-01 07:34:00 -04001025 } else
James Smart2e0fef82007-06-17 19:56:36 -05001026 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001027
James Bottomley604a3e32005-10-29 10:28:33 -05001028 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001029 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1030 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001031
James Bottomley604a3e32005-10-29 10:28:33 -05001032 return 0;
dea31012005-04-17 16:05:31 -05001033}
1034
James Smarte59058c2008-08-24 21:49:00 -04001035/**
James Smart3621a712009-04-06 18:47:14 -04001036 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001037 * @phba: Pointer to HBA context object.
1038 * @pring: Pointer to driver SLI ring object.
1039 * @iocb: Pointer to iocb slot in the ring.
1040 * @nextiocb: Pointer to driver iocb object which need to be
1041 * posted to firmware.
1042 *
1043 * This function is called with hbalock held to post a new iocb to
1044 * the firmware. This function copies the new iocb to ring iocb slot and
1045 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1046 * a completion call back for this iocb else the function will free the
1047 * iocb object.
1048 **/
dea31012005-04-17 16:05:31 -05001049static void
1050lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1051 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1052{
1053 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001054 * Set up an iotag
dea31012005-04-17 16:05:31 -05001055 */
James Bottomley604a3e32005-10-29 10:28:33 -05001056 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001057
James Smarte2a0a9d2008-12-04 22:40:02 -05001058
James Smarta58cbd52007-08-02 11:09:43 -04001059 if (pring->ringno == LPFC_ELS_RING) {
1060 lpfc_debugfs_slow_ring_trc(phba,
1061 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1062 *(((uint32_t *) &nextiocb->iocb) + 4),
1063 *(((uint32_t *) &nextiocb->iocb) + 6),
1064 *(((uint32_t *) &nextiocb->iocb) + 7));
1065 }
1066
dea31012005-04-17 16:05:31 -05001067 /*
1068 * Issue iocb command to adapter
1069 */
James Smart92d7f7b2007-06-17 19:56:38 -05001070 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001071 wmb();
1072 pring->stats.iocb_cmd++;
1073
1074 /*
1075 * If there is no completion routine to call, we can release the
1076 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1077 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1078 */
1079 if (nextiocb->iocb_cmpl)
1080 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001081 else
James Smart2e0fef82007-06-17 19:56:36 -05001082 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001083
1084 /*
1085 * Let the HBA know what IOCB slot will be the next one the
1086 * driver will put a command into.
1087 */
1088 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001089 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001090}
1091
James Smarte59058c2008-08-24 21:49:00 -04001092/**
James Smart3621a712009-04-06 18:47:14 -04001093 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001094 * @phba: Pointer to HBA context object.
1095 * @pring: Pointer to driver SLI ring object.
1096 *
1097 * The caller is not required to hold any lock for calling this function.
1098 * This function updates the chip attention bits for the ring to inform firmware
1099 * that there are pending work to be done for this ring and requests an
1100 * interrupt when there is space available in the ring. This function is
1101 * called when the driver is unable to post more iocbs to the ring due
1102 * to unavailability of space in the ring.
1103 **/
dea31012005-04-17 16:05:31 -05001104static void
James Smart2e0fef82007-06-17 19:56:36 -05001105lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001106{
1107 int ringno = pring->ringno;
1108
1109 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1110
1111 wmb();
1112
1113 /*
1114 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1115 * The HBA will tell us when an IOCB entry is available.
1116 */
1117 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1118 readl(phba->CAregaddr); /* flush */
1119
1120 pring->stats.iocb_cmd_full++;
1121}
1122
James Smarte59058c2008-08-24 21:49:00 -04001123/**
James Smart3621a712009-04-06 18:47:14 -04001124 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001125 * @phba: Pointer to HBA context object.
1126 * @pring: Pointer to driver SLI ring object.
1127 *
1128 * This function updates the chip attention register bit for the
1129 * given ring to inform HBA that there is more work to be done
1130 * in this ring. The caller is not required to hold any lock.
1131 **/
dea31012005-04-17 16:05:31 -05001132static void
James Smart2e0fef82007-06-17 19:56:36 -05001133lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001134{
1135 int ringno = pring->ringno;
1136
1137 /*
1138 * Tell the HBA that there is work to do in this ring.
1139 */
James Smart34b02dc2008-08-24 21:49:55 -04001140 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1141 wmb();
1142 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1143 readl(phba->CAregaddr); /* flush */
1144 }
dea31012005-04-17 16:05:31 -05001145}
1146
James Smarte59058c2008-08-24 21:49:00 -04001147/**
James Smart3621a712009-04-06 18:47:14 -04001148 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001149 * @phba: Pointer to HBA context object.
1150 * @pring: Pointer to driver SLI ring object.
1151 *
1152 * This function is called with hbalock held to post pending iocbs
1153 * in the txq to the firmware. This function is called when driver
1154 * detects space available in the ring.
1155 **/
dea31012005-04-17 16:05:31 -05001156static void
James Smart2e0fef82007-06-17 19:56:36 -05001157lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001158{
1159 IOCB_t *iocb;
1160 struct lpfc_iocbq *nextiocb;
1161
1162 /*
1163 * Check to see if:
1164 * (a) there is anything on the txq to send
1165 * (b) link is up
1166 * (c) link attention events can be processed (fcp ring only)
1167 * (d) IOCB processing is not blocked by the outstanding mbox command.
1168 */
1169 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001170 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001171 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001172 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001173
1174 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1175 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1176 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1177
1178 if (iocb)
1179 lpfc_sli_update_ring(phba, pring);
1180 else
1181 lpfc_sli_update_full_ring(phba, pring);
1182 }
1183
1184 return;
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smart3621a712009-04-06 18:47:14 -04001188 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001189 * @phba: Pointer to HBA context object.
1190 * @hbqno: HBQ number.
1191 *
1192 * This function is called with hbalock held to get the next
1193 * available slot for the given HBQ. If there is free slot
1194 * available for the HBQ it will return pointer to the next available
1195 * HBQ entry else it will return NULL.
1196 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001197static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001198lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1199{
1200 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1201
1202 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1203 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1204 hbqp->next_hbqPutIdx = 0;
1205
1206 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001207 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001208 uint32_t getidx = le32_to_cpu(raw_index);
1209
1210 hbqp->local_hbqGetIdx = getidx;
1211
1212 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1213 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001214 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001215 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001216 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001217 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001218 hbqp->entry_count);
1219
1220 phba->link_state = LPFC_HBA_ERROR;
1221 return NULL;
1222 }
1223
1224 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1225 return NULL;
1226 }
1227
James Smart51ef4c22007-08-02 11:10:31 -04001228 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1229 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001230}
1231
James Smarte59058c2008-08-24 21:49:00 -04001232/**
James Smart3621a712009-04-06 18:47:14 -04001233 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001234 * @phba: Pointer to HBA context object.
1235 *
1236 * This function is called with no lock held to free all the
1237 * hbq buffers while uninitializing the SLI interface. It also
1238 * frees the HBQ buffers returned by the firmware but not yet
1239 * processed by the upper layers.
1240 **/
James Smarted957682007-06-17 19:56:37 -05001241void
1242lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1243{
James Smart92d7f7b2007-06-17 19:56:38 -05001244 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1245 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001246 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001247 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001248 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001249
James Smart51ef4c22007-08-02 11:10:31 -04001250 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001251 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001252 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001253 for (i = 0; i < hbq_count; ++i) {
1254 list_for_each_entry_safe(dmabuf, next_dmabuf,
1255 &phba->hbqs[i].hbq_buffer_list, list) {
1256 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1257 list_del(&hbq_buf->dbuf.list);
1258 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1259 }
James Smarta8adb832007-10-27 13:37:53 -04001260 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001261 }
James Smart3163f722008-02-08 18:50:25 -05001262 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001263 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1264 list) {
James Smart3163f722008-02-08 18:50:25 -05001265 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1266 list_del(&hbq_buf->dbuf.list);
1267 if (hbq_buf->tag == -1) {
1268 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1269 (phba, hbq_buf);
1270 } else {
1271 hbqno = hbq_buf->tag >> 16;
1272 if (hbqno >= LPFC_MAX_HBQS)
1273 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1274 (phba, hbq_buf);
1275 else
1276 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1277 hbq_buf);
1278 }
1279 }
1280
1281 /* Mark the HBQs not in use */
1282 phba->hbq_in_use = 0;
1283 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001284}
1285
James Smarte59058c2008-08-24 21:49:00 -04001286/**
James Smart3621a712009-04-06 18:47:14 -04001287 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001288 * @phba: Pointer to HBA context object.
1289 * @hbqno: HBQ number.
1290 * @hbq_buf: Pointer to HBQ buffer.
1291 *
1292 * This function is called with the hbalock held to post a
1293 * hbq buffer to the firmware. If the function finds an empty
1294 * slot in the HBQ, it will post the buffer. The function will return
1295 * pointer to the hbq entry if it successfully post the buffer
1296 * else it will return NULL.
1297 **/
James Smart3772a992009-05-22 14:50:54 -04001298static int
James Smarted957682007-06-17 19:56:37 -05001299lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001300 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001301{
James Smart3772a992009-05-22 14:50:54 -04001302 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1303}
1304
1305/**
1306 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1307 * @phba: Pointer to HBA context object.
1308 * @hbqno: HBQ number.
1309 * @hbq_buf: Pointer to HBQ buffer.
1310 *
1311 * This function is called with the hbalock held to post a hbq buffer to the
1312 * firmware. If the function finds an empty slot in the HBQ, it will post the
1313 * buffer and place it on the hbq_buffer_list. The function will return zero if
1314 * it successfully post the buffer else it will return an error.
1315 **/
1316static int
1317lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1318 struct hbq_dmabuf *hbq_buf)
1319{
James Smarted957682007-06-17 19:56:37 -05001320 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001321 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001322
1323 /* Get next HBQ entry slot to use */
1324 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1325 if (hbqe) {
1326 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1327
James Smart92d7f7b2007-06-17 19:56:38 -05001328 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1329 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001330 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001331 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001332 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1333 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1334 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001335 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1336 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001337 /* flush */
James Smarted957682007-06-17 19:56:37 -05001338 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001339 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001340 return 0;
1341 } else
1342 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001343}
1344
James Smart4f774512009-05-22 14:52:35 -04001345/**
1346 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1347 * @phba: Pointer to HBA context object.
1348 * @hbqno: HBQ number.
1349 * @hbq_buf: Pointer to HBQ buffer.
1350 *
1351 * This function is called with the hbalock held to post an RQE to the SLI4
1352 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1353 * the hbq_buffer_list and return zero, otherwise it will return an error.
1354 **/
1355static int
1356lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1357 struct hbq_dmabuf *hbq_buf)
1358{
1359 int rc;
1360 struct lpfc_rqe hrqe;
1361 struct lpfc_rqe drqe;
1362
1363 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1364 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1365 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1366 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1367 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1368 &hrqe, &drqe);
1369 if (rc < 0)
1370 return rc;
1371 hbq_buf->tag = rc;
1372 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1373 return 0;
1374}
1375
James Smarte59058c2008-08-24 21:49:00 -04001376/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001377static struct lpfc_hbq_init lpfc_els_hbq = {
1378 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001379 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001380 .mask_count = 0,
1381 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001382 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001383 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001384 .init_count = 40,
1385 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001386};
James Smarted957682007-06-17 19:56:37 -05001387
James Smarte59058c2008-08-24 21:49:00 -04001388/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001389static struct lpfc_hbq_init lpfc_extra_hbq = {
1390 .rn = 1,
1391 .entry_count = 200,
1392 .mask_count = 0,
1393 .profile = 0,
1394 .ring_mask = (1 << LPFC_EXTRA_RING),
1395 .buffer_count = 0,
1396 .init_count = 0,
1397 .add_count = 5,
1398};
1399
James Smarte59058c2008-08-24 21:49:00 -04001400/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001401struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001402 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001403 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001404};
1405
James Smarte59058c2008-08-24 21:49:00 -04001406/**
James Smart3621a712009-04-06 18:47:14 -04001407 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001408 * @phba: Pointer to HBA context object.
1409 * @hbqno: HBQ number.
1410 * @count: Number of HBQ buffers to be posted.
1411 *
James Smartd7c255b2008-08-24 21:50:00 -04001412 * This function is called with no lock held to post more hbq buffers to the
1413 * given HBQ. The function returns the number of HBQ buffers successfully
1414 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001415 **/
James Smart311464e2007-08-02 11:10:37 -04001416static int
James Smart92d7f7b2007-06-17 19:56:38 -05001417lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1418{
James Smartd7c255b2008-08-24 21:50:00 -04001419 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001420 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001421 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001422 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001423 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001424 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001425
James Smartd7c255b2008-08-24 21:50:00 -04001426 if ((phba->hbqs[hbqno].buffer_count + count) >
1427 lpfc_hbq_defs[hbqno]->entry_count)
1428 count = lpfc_hbq_defs[hbqno]->entry_count -
1429 phba->hbqs[hbqno].buffer_count;
1430 if (!count)
1431 return 0;
1432 /* Allocate HBQ entries */
1433 for (i = 0; i < count; i++) {
1434 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1435 if (!hbq_buffer)
1436 break;
1437 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1438 }
James Smart3163f722008-02-08 18:50:25 -05001439 /* Check whether HBQ is still in use */
1440 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001441 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001442 goto err;
1443 while (!list_empty(&hbq_buf_list)) {
1444 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1445 dbuf.list);
1446 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1447 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001448 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001449 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001450 posted++;
1451 } else
James Smart51ef4c22007-08-02 11:10:31 -04001452 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001453 }
James Smart3163f722008-02-08 18:50:25 -05001454 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001455 return posted;
1456err:
1457 spin_unlock_irqrestore(&phba->hbalock, flags);
1458 while (!list_empty(&hbq_buf_list)) {
1459 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1460 dbuf.list);
1461 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1462 }
James Smart92d7f7b2007-06-17 19:56:38 -05001463 return 0;
James Smarted957682007-06-17 19:56:37 -05001464}
1465
James Smarte59058c2008-08-24 21:49:00 -04001466/**
James Smart3621a712009-04-06 18:47:14 -04001467 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001468 * @phba: Pointer to HBA context object.
1469 * @qno: HBQ number.
1470 *
1471 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001472 * is called with no lock held. The function returns the number of HBQ entries
1473 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001474 **/
James Smarted957682007-06-17 19:56:37 -05001475int
James Smart92d7f7b2007-06-17 19:56:38 -05001476lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001477{
James Smartdef9c7a2009-12-21 17:02:28 -05001478 if (phba->sli_rev == LPFC_SLI_REV4)
1479 return 0;
1480 else
1481 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1482 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001483}
1484
James Smarte59058c2008-08-24 21:49:00 -04001485/**
James Smart3621a712009-04-06 18:47:14 -04001486 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001487 * @phba: Pointer to HBA context object.
1488 * @qno: HBQ queue number.
1489 *
1490 * This function is called from SLI initialization code path with
1491 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001492 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001493 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001494static int
James Smart92d7f7b2007-06-17 19:56:38 -05001495lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001496{
James Smartdef9c7a2009-12-21 17:02:28 -05001497 if (phba->sli_rev == LPFC_SLI_REV4)
1498 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1499 lpfc_hbq_defs[qno]->entry_count);
1500 else
1501 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1502 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001503}
1504
James Smarte59058c2008-08-24 21:49:00 -04001505/**
James Smart3772a992009-05-22 14:50:54 -04001506 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1507 * @phba: Pointer to HBA context object.
1508 * @hbqno: HBQ number.
1509 *
1510 * This function removes the first hbq buffer on an hbq list and returns a
1511 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1512 **/
1513static struct hbq_dmabuf *
1514lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1515{
1516 struct lpfc_dmabuf *d_buf;
1517
1518 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1519 if (!d_buf)
1520 return NULL;
1521 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1522}
1523
1524/**
James Smart3621a712009-04-06 18:47:14 -04001525 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001526 * @phba: Pointer to HBA context object.
1527 * @tag: Tag of the hbq buffer.
1528 *
1529 * This function is called with hbalock held. This function searches
1530 * for the hbq buffer associated with the given tag in the hbq buffer
1531 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1532 * it returns NULL.
1533 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001534static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001535lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1536{
James Smart92d7f7b2007-06-17 19:56:38 -05001537 struct lpfc_dmabuf *d_buf;
1538 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001539 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001540
James Smart51ef4c22007-08-02 11:10:31 -04001541 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001542 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001543 return NULL;
1544
James Smart3772a992009-05-22 14:50:54 -04001545 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001546 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001547 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001548 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001549 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001550 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001551 }
1552 }
James Smart3772a992009-05-22 14:50:54 -04001553 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001555 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001556 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001557 return NULL;
James Smarted957682007-06-17 19:56:37 -05001558}
1559
James Smarte59058c2008-08-24 21:49:00 -04001560/**
James Smart3621a712009-04-06 18:47:14 -04001561 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001562 * @phba: Pointer to HBA context object.
1563 * @hbq_buffer: Pointer to HBQ buffer.
1564 *
1565 * This function is called with hbalock. This function gives back
1566 * the hbq buffer to firmware. If the HBQ does not have space to
1567 * post the buffer, it will free the buffer.
1568 **/
James Smarted957682007-06-17 19:56:37 -05001569void
James Smart51ef4c22007-08-02 11:10:31 -04001570lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001571{
1572 uint32_t hbqno;
1573
James Smart51ef4c22007-08-02 11:10:31 -04001574 if (hbq_buffer) {
1575 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001576 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001577 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001578 }
1579}
1580
James Smarte59058c2008-08-24 21:49:00 -04001581/**
James Smart3621a712009-04-06 18:47:14 -04001582 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001583 * @mbxCommand: mailbox command code.
1584 *
1585 * This function is called by the mailbox event handler function to verify
1586 * that the completed mailbox command is a legitimate mailbox command. If the
1587 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1588 * and the mailbox event handler will take the HBA offline.
1589 **/
dea31012005-04-17 16:05:31 -05001590static int
1591lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1592{
1593 uint8_t ret;
1594
1595 switch (mbxCommand) {
1596 case MBX_LOAD_SM:
1597 case MBX_READ_NV:
1598 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001599 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001600 case MBX_RUN_BIU_DIAG:
1601 case MBX_INIT_LINK:
1602 case MBX_DOWN_LINK:
1603 case MBX_CONFIG_LINK:
1604 case MBX_CONFIG_RING:
1605 case MBX_RESET_RING:
1606 case MBX_READ_CONFIG:
1607 case MBX_READ_RCONFIG:
1608 case MBX_READ_SPARM:
1609 case MBX_READ_STATUS:
1610 case MBX_READ_RPI:
1611 case MBX_READ_XRI:
1612 case MBX_READ_REV:
1613 case MBX_READ_LNK_STAT:
1614 case MBX_REG_LOGIN:
1615 case MBX_UNREG_LOGIN:
1616 case MBX_READ_LA:
1617 case MBX_CLEAR_LA:
1618 case MBX_DUMP_MEMORY:
1619 case MBX_DUMP_CONTEXT:
1620 case MBX_RUN_DIAGS:
1621 case MBX_RESTART:
1622 case MBX_UPDATE_CFG:
1623 case MBX_DOWN_LOAD:
1624 case MBX_DEL_LD_ENTRY:
1625 case MBX_RUN_PROGRAM:
1626 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001627 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001628 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001629 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001630 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001631 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001632 case MBX_LOAD_AREA:
1633 case MBX_RUN_BIU_DIAG64:
1634 case MBX_CONFIG_PORT:
1635 case MBX_READ_SPARM64:
1636 case MBX_READ_RPI64:
1637 case MBX_REG_LOGIN64:
1638 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001639 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001640 case MBX_SET_DEBUG:
1641 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001642 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001643 case MBX_REG_VPI:
1644 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001645 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001646 case MBX_PORT_CAPABILITIES:
1647 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001648 case MBX_SLI4_CONFIG:
1649 case MBX_SLI4_REQ_FTRS:
1650 case MBX_REG_FCFI:
1651 case MBX_UNREG_FCFI:
1652 case MBX_REG_VFI:
1653 case MBX_UNREG_VFI:
1654 case MBX_INIT_VPI:
1655 case MBX_INIT_VFI:
1656 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001657 ret = mbxCommand;
1658 break;
1659 default:
1660 ret = MBX_SHUTDOWN;
1661 break;
1662 }
James Smart2e0fef82007-06-17 19:56:36 -05001663 return ret;
dea31012005-04-17 16:05:31 -05001664}
James Smarte59058c2008-08-24 21:49:00 -04001665
1666/**
James Smart3621a712009-04-06 18:47:14 -04001667 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001668 * @phba: Pointer to HBA context object.
1669 * @pmboxq: Pointer to mailbox command.
1670 *
1671 * This is completion handler function for mailbox commands issued from
1672 * lpfc_sli_issue_mbox_wait function. This function is called by the
1673 * mailbox event handler function with no lock held. This function
1674 * will wake up thread waiting on the wait queue pointed by context1
1675 * of the mailbox.
1676 **/
James Smart04c68492009-05-22 14:52:52 -04001677void
James Smart2e0fef82007-06-17 19:56:36 -05001678lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001679{
1680 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001681 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001682
1683 /*
1684 * If pdone_q is empty, the driver thread gave up waiting and
1685 * continued running.
1686 */
James Smart7054a602007-04-25 09:52:34 -04001687 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001688 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001689 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1690 if (pdone_q)
1691 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001692 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001693 return;
1694}
1695
James Smarte59058c2008-08-24 21:49:00 -04001696
1697/**
James Smart3621a712009-04-06 18:47:14 -04001698 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001699 * @phba: Pointer to HBA context object.
1700 * @pmb: Pointer to mailbox object.
1701 *
1702 * This function is the default mailbox completion handler. It
1703 * frees the memory resources associated with the completed mailbox
1704 * command. If the completed command is a REG_LOGIN mailbox command,
1705 * this function will issue a UREG_LOGIN to re-claim the RPI.
1706 **/
dea31012005-04-17 16:05:31 -05001707void
James Smart2e0fef82007-06-17 19:56:36 -05001708lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001709{
1710 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001711 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001712 int rc;
James Smart695a8142010-01-26 23:08:03 -05001713 struct lpfc_vport *vport = pmb->vport;
James Smart7054a602007-04-25 09:52:34 -04001714
dea31012005-04-17 16:05:31 -05001715 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001716
dea31012005-04-17 16:05:31 -05001717 if (mp) {
1718 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1719 kfree(mp);
1720 }
James Smart7054a602007-04-25 09:52:34 -04001721
James Smart04c68492009-05-22 14:52:52 -04001722 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1723 (phba->sli_rev == LPFC_SLI_REV4))
1724 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1725
James Smart7054a602007-04-25 09:52:34 -04001726 /*
1727 * If a REG_LOGIN succeeded after node is destroyed or node
1728 * is in re-discovery driver need to cleanup the RPI.
1729 */
James Smart2e0fef82007-06-17 19:56:36 -05001730 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001731 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1732 !pmb->u.mb.mbxStatus) {
1733 rpi = pmb->u.mb.un.varWords[0];
1734 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1735 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001736 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001737 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1738 if (rc != MBX_NOT_FINISHED)
1739 return;
1740 }
1741
James Smart695a8142010-01-26 23:08:03 -05001742 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1743 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1744 !(phba->pport->load_flag & FC_UNLOADING) &&
1745 !pmb->u.mb.mbxStatus) {
1746 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1747 pmb->vport = vport;
1748 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1749 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1750 if (rc != MBX_NOT_FINISHED)
1751 return;
1752 }
1753
James Smart04c68492009-05-22 14:52:52 -04001754 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1755 lpfc_sli4_mbox_cmd_free(phba, pmb);
1756 else
1757 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001758}
1759
James Smarte59058c2008-08-24 21:49:00 -04001760/**
James Smart3621a712009-04-06 18:47:14 -04001761 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001762 * @phba: Pointer to HBA context object.
1763 *
1764 * This function is called with no lock held. This function processes all
1765 * the completed mailbox commands and gives it to upper layers. The interrupt
1766 * service routine processes mailbox completion interrupt and adds completed
1767 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1768 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1769 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1770 * function returns the mailbox commands to the upper layer by calling the
1771 * completion handler function of each mailbox.
1772 **/
dea31012005-04-17 16:05:31 -05001773int
James Smart2e0fef82007-06-17 19:56:36 -05001774lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001775{
James Smart92d7f7b2007-06-17 19:56:38 -05001776 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001777 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001778 int rc;
1779 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001780
1781 phba->sli.slistat.mbox_event++;
1782
James Smart92d7f7b2007-06-17 19:56:38 -05001783 /* Get all completed mailboxe buffers into the cmplq */
1784 spin_lock_irq(&phba->hbalock);
1785 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1786 spin_unlock_irq(&phba->hbalock);
1787
dea31012005-04-17 16:05:31 -05001788 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001789 do {
1790 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1791 if (pmb == NULL)
1792 break;
1793
James Smart04c68492009-05-22 14:52:52 -04001794 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001795
James Smart858c9f62007-06-17 19:56:39 -05001796 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1797 if (pmb->vport) {
1798 lpfc_debugfs_disc_trc(pmb->vport,
1799 LPFC_DISC_TRC_MBOX_VPORT,
1800 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1801 (uint32_t)pmbox->mbxCommand,
1802 pmbox->un.varWords[0],
1803 pmbox->un.varWords[1]);
1804 }
1805 else {
1806 lpfc_debugfs_disc_trc(phba->pport,
1807 LPFC_DISC_TRC_MBOX,
1808 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1809 (uint32_t)pmbox->mbxCommand,
1810 pmbox->un.varWords[0],
1811 pmbox->un.varWords[1]);
1812 }
1813 }
1814
dea31012005-04-17 16:05:31 -05001815 /*
1816 * It is a fatal error if unknown mbox command completion.
1817 */
1818 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1819 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001820 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001821 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001822 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001823 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001824 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001825 pmbox->mbxCommand,
1826 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001827 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001828 phba->work_hs = HS_FFER3;
1829 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001830 continue;
dea31012005-04-17 16:05:31 -05001831 }
1832
dea31012005-04-17 16:05:31 -05001833 if (pmbox->mbxStatus) {
1834 phba->sli.slistat.mbox_stat_err++;
1835 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1836 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001837 lpfc_printf_log(phba, KERN_INFO,
1838 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001839 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001840 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001841 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001842 pmb->vport ? pmb->vport->vpi :0,
1843 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001844 lpfc_sli4_mbox_opcode_get(phba,
1845 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001846 pmbox->mbxStatus,
1847 pmbox->un.varWords[0],
1848 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001849 pmbox->mbxStatus = 0;
1850 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001851 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001852 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001853 continue;
dea31012005-04-17 16:05:31 -05001854 }
1855 }
1856
1857 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001858 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001859 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001860 "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 -05001861 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001862 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001863 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001864 pmb->mbox_cmpl,
1865 *((uint32_t *) pmbox),
1866 pmbox->un.varWords[0],
1867 pmbox->un.varWords[1],
1868 pmbox->un.varWords[2],
1869 pmbox->un.varWords[3],
1870 pmbox->un.varWords[4],
1871 pmbox->un.varWords[5],
1872 pmbox->un.varWords[6],
1873 pmbox->un.varWords[7]);
1874
James Smart92d7f7b2007-06-17 19:56:38 -05001875 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001876 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001877 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001878 return 0;
dea31012005-04-17 16:05:31 -05001879}
James Smart92d7f7b2007-06-17 19:56:38 -05001880
James Smarte59058c2008-08-24 21:49:00 -04001881/**
James Smart3621a712009-04-06 18:47:14 -04001882 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001883 * @phba: Pointer to HBA context object.
1884 * @pring: Pointer to driver SLI ring object.
1885 * @tag: buffer tag.
1886 *
1887 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1888 * is set in the tag the buffer is posted for a particular exchange,
1889 * the function will return the buffer without replacing the buffer.
1890 * If the buffer is for unsolicited ELS or CT traffic, this function
1891 * returns the buffer and also posts another buffer to the firmware.
1892 **/
James Smart76bb24e2007-10-27 13:38:00 -04001893static struct lpfc_dmabuf *
1894lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001895 struct lpfc_sli_ring *pring,
1896 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001897{
James Smart9f1e1b52008-12-04 22:39:40 -05001898 struct hbq_dmabuf *hbq_entry;
1899
James Smart76bb24e2007-10-27 13:38:00 -04001900 if (tag & QUE_BUFTAG_BIT)
1901 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001902 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1903 if (!hbq_entry)
1904 return NULL;
1905 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001906}
James Smart57127f12007-10-27 13:37:05 -04001907
James Smart3772a992009-05-22 14:50:54 -04001908/**
1909 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1910 * @phba: Pointer to HBA context object.
1911 * @pring: Pointer to driver SLI ring object.
1912 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1913 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1914 * @fch_type: the type for the first frame of the sequence.
1915 *
1916 * This function is called with no lock held. This function uses the r_ctl and
1917 * type of the received sequence to find the correct callback function to call
1918 * to process the sequence.
1919 **/
1920static int
1921lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1922 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1923 uint32_t fch_type)
1924{
1925 int i;
1926
1927 /* unSolicited Responses */
1928 if (pring->prt[0].profile) {
1929 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1930 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1931 saveq);
1932 return 1;
1933 }
1934 /* We must search, based on rctl / type
1935 for the right routine */
1936 for (i = 0; i < pring->num_mask; i++) {
1937 if ((pring->prt[i].rctl == fch_r_ctl) &&
1938 (pring->prt[i].type == fch_type)) {
1939 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1940 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1941 (phba, pring, saveq);
1942 return 1;
1943 }
1944 }
1945 return 0;
1946}
James Smarte59058c2008-08-24 21:49:00 -04001947
1948/**
James Smart3621a712009-04-06 18:47:14 -04001949 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001950 * @phba: Pointer to HBA context object.
1951 * @pring: Pointer to driver SLI ring object.
1952 * @saveq: Pointer to the unsolicited iocb.
1953 *
1954 * This function is called with no lock held by the ring event handler
1955 * when there is an unsolicited iocb posted to the response ring by the
1956 * firmware. This function gets the buffer associated with the iocbs
1957 * and calls the event handler for the ring. This function handles both
1958 * qring buffers and hbq buffers.
1959 * When the function returns 1 the caller can free the iocb object otherwise
1960 * upper layer functions will free the iocb objects.
1961 **/
dea31012005-04-17 16:05:31 -05001962static int
1963lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1964 struct lpfc_iocbq *saveq)
1965{
1966 IOCB_t * irsp;
1967 WORD5 * w5p;
1968 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001969 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001970 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001971 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001972
1973 match = 0;
1974 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001975
1976 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1977 if (pring->lpfc_sli_rcv_async_status)
1978 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1979 else
1980 lpfc_printf_log(phba,
1981 KERN_WARNING,
1982 LOG_SLI,
1983 "0316 Ring %d handler: unexpected "
1984 "ASYNC_STATUS iocb received evt_code "
1985 "0x%x\n",
1986 pring->ringno,
1987 irsp->un.asyncstat.evt_code);
1988 return 1;
1989 }
1990
James Smart3163f722008-02-08 18:50:25 -05001991 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1992 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1993 if (irsp->ulpBdeCount > 0) {
1994 dmzbuf = lpfc_sli_get_buff(phba, pring,
1995 irsp->un.ulpWord[3]);
1996 lpfc_in_buf_free(phba, dmzbuf);
1997 }
1998
1999 if (irsp->ulpBdeCount > 1) {
2000 dmzbuf = lpfc_sli_get_buff(phba, pring,
2001 irsp->unsli3.sli3Words[3]);
2002 lpfc_in_buf_free(phba, dmzbuf);
2003 }
2004
2005 if (irsp->ulpBdeCount > 2) {
2006 dmzbuf = lpfc_sli_get_buff(phba, pring,
2007 irsp->unsli3.sli3Words[7]);
2008 lpfc_in_buf_free(phba, dmzbuf);
2009 }
2010
2011 return 1;
2012 }
2013
James Smart92d7f7b2007-06-17 19:56:38 -05002014 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002015 if (irsp->ulpBdeCount != 0) {
2016 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002017 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002018 if (!saveq->context2)
2019 lpfc_printf_log(phba,
2020 KERN_ERR,
2021 LOG_SLI,
2022 "0341 Ring %d Cannot find buffer for "
2023 "an unsolicited iocb. tag 0x%x\n",
2024 pring->ringno,
2025 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002026 }
2027 if (irsp->ulpBdeCount == 2) {
2028 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002029 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002030 if (!saveq->context3)
2031 lpfc_printf_log(phba,
2032 KERN_ERR,
2033 LOG_SLI,
2034 "0342 Ring %d Cannot find buffer for an"
2035 " unsolicited iocb. tag 0x%x\n",
2036 pring->ringno,
2037 irsp->unsli3.sli3Words[7]);
2038 }
2039 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002040 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002041 if (irsp->ulpBdeCount != 0) {
2042 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2043 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002044 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002045 lpfc_printf_log(phba,
2046 KERN_ERR,
2047 LOG_SLI,
2048 "0343 Ring %d Cannot find "
2049 "buffer for an unsolicited iocb"
2050 ". tag 0x%x\n", pring->ringno,
2051 irsp->un.ulpWord[3]);
2052 }
2053 if (irsp->ulpBdeCount == 2) {
2054 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2055 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002056 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002057 lpfc_printf_log(phba,
2058 KERN_ERR,
2059 LOG_SLI,
2060 "0344 Ring %d Cannot find "
2061 "buffer for an unsolicited "
2062 "iocb. tag 0x%x\n",
2063 pring->ringno,
2064 irsp->unsli3.sli3Words[7]);
2065 }
2066 }
James Smart92d7f7b2007-06-17 19:56:38 -05002067 }
James Smart9c2face2008-01-11 01:53:18 -05002068 if (irsp->ulpBdeCount != 0 &&
2069 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2070 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2071 int found = 0;
2072
2073 /* search continue save q for same XRI */
2074 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2075 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2076 list_add_tail(&saveq->list, &iocbq->list);
2077 found = 1;
2078 break;
2079 }
2080 }
2081 if (!found)
2082 list_add_tail(&saveq->clist,
2083 &pring->iocb_continue_saveq);
2084 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2085 list_del_init(&iocbq->clist);
2086 saveq = iocbq;
2087 irsp = &(saveq->iocb);
2088 } else
2089 return 0;
2090 }
2091 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2092 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2093 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002094 Rctl = FC_RCTL_ELS_REQ;
2095 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002096 } else {
2097 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2098 Rctl = w5p->hcsw.Rctl;
2099 Type = w5p->hcsw.Type;
2100
2101 /* Firmware Workaround */
2102 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2103 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2104 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002105 Rctl = FC_RCTL_ELS_REQ;
2106 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002107 w5p->hcsw.Rctl = Rctl;
2108 w5p->hcsw.Type = Type;
2109 }
2110 }
James Smart92d7f7b2007-06-17 19:56:38 -05002111
James Smart3772a992009-05-22 14:50:54 -04002112 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002113 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002114 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002115 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002116 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002117
James Smart92d7f7b2007-06-17 19:56:38 -05002118 return 1;
dea31012005-04-17 16:05:31 -05002119}
2120
James Smarte59058c2008-08-24 21:49:00 -04002121/**
James Smart3621a712009-04-06 18:47:14 -04002122 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002123 * @phba: Pointer to HBA context object.
2124 * @pring: Pointer to driver SLI ring object.
2125 * @prspiocb: Pointer to response iocb object.
2126 *
2127 * This function looks up the iocb_lookup table to get the command iocb
2128 * corresponding to the given response iocb using the iotag of the
2129 * response iocb. This function is called with the hbalock held.
2130 * This function returns the command iocb object if it finds the command
2131 * iocb else returns NULL.
2132 **/
dea31012005-04-17 16:05:31 -05002133static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002134lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2135 struct lpfc_sli_ring *pring,
2136 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002137{
dea31012005-04-17 16:05:31 -05002138 struct lpfc_iocbq *cmd_iocb = NULL;
2139 uint16_t iotag;
2140
James Bottomley604a3e32005-10-29 10:28:33 -05002141 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002142
James Bottomley604a3e32005-10-29 10:28:33 -05002143 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2144 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002145 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002146 pring->txcmplq_cnt--;
2147 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002148 }
2149
dea31012005-04-17 16:05:31 -05002150 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002151 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002152 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002153 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002154 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002155 return NULL;
2156}
2157
James Smarte59058c2008-08-24 21:49:00 -04002158/**
James Smart3772a992009-05-22 14:50:54 -04002159 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2160 * @phba: Pointer to HBA context object.
2161 * @pring: Pointer to driver SLI ring object.
2162 * @iotag: IOCB tag.
2163 *
2164 * This function looks up the iocb_lookup table to get the command iocb
2165 * corresponding to the given iotag. This function is called with the
2166 * hbalock held.
2167 * This function returns the command iocb object if it finds the command
2168 * iocb else returns NULL.
2169 **/
2170static struct lpfc_iocbq *
2171lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2172 struct lpfc_sli_ring *pring, uint16_t iotag)
2173{
2174 struct lpfc_iocbq *cmd_iocb;
2175
2176 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2177 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2178 list_del_init(&cmd_iocb->list);
2179 pring->txcmplq_cnt--;
2180 return cmd_iocb;
2181 }
2182
2183 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2184 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2185 iotag, phba->sli.last_iotag);
2186 return NULL;
2187}
2188
2189/**
James Smart3621a712009-04-06 18:47:14 -04002190 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002191 * @phba: Pointer to HBA context object.
2192 * @pring: Pointer to driver SLI ring object.
2193 * @saveq: Pointer to the response iocb to be processed.
2194 *
2195 * This function is called by the ring event handler for non-fcp
2196 * rings when there is a new response iocb in the response ring.
2197 * The caller is not required to hold any locks. This function
2198 * gets the command iocb associated with the response iocb and
2199 * calls the completion handler for the command iocb. If there
2200 * is no completion handler, the function will free the resources
2201 * associated with command iocb. If the response iocb is for
2202 * an already aborted command iocb, the status of the completion
2203 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2204 * This function always returns 1.
2205 **/
dea31012005-04-17 16:05:31 -05002206static int
James Smart2e0fef82007-06-17 19:56:36 -05002207lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002208 struct lpfc_iocbq *saveq)
2209{
James Smart2e0fef82007-06-17 19:56:36 -05002210 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002211 int rc = 1;
2212 unsigned long iflag;
2213
2214 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002215 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002216 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002217 spin_unlock_irqrestore(&phba->hbalock, iflag);
2218
dea31012005-04-17 16:05:31 -05002219 if (cmdiocbp) {
2220 if (cmdiocbp->iocb_cmpl) {
2221 /*
James Smartea2151b2008-09-07 11:52:10 -04002222 * If an ELS command failed send an event to mgmt
2223 * application.
2224 */
2225 if (saveq->iocb.ulpStatus &&
2226 (pring->ringno == LPFC_ELS_RING) &&
2227 (cmdiocbp->iocb.ulpCommand ==
2228 CMD_ELS_REQUEST64_CR))
2229 lpfc_send_els_failure_event(phba,
2230 cmdiocbp, saveq);
2231
2232 /*
dea31012005-04-17 16:05:31 -05002233 * Post all ELS completions to the worker thread.
2234 * All other are passed to the completion callback.
2235 */
2236 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002237 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2238 (cmdiocbp->iocb_flag &
2239 LPFC_DRIVER_ABORTED)) {
2240 spin_lock_irqsave(&phba->hbalock,
2241 iflag);
James Smart07951072007-04-25 09:51:38 -04002242 cmdiocbp->iocb_flag &=
2243 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002244 spin_unlock_irqrestore(&phba->hbalock,
2245 iflag);
James Smart07951072007-04-25 09:51:38 -04002246 saveq->iocb.ulpStatus =
2247 IOSTAT_LOCAL_REJECT;
2248 saveq->iocb.un.ulpWord[4] =
2249 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002250
2251 /* Firmware could still be in progress
2252 * of DMAing payload, so don't free data
2253 * buffer till after a hbeat.
2254 */
James Smart341af102010-01-26 23:07:37 -05002255 spin_lock_irqsave(&phba->hbalock,
2256 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002257 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002258 spin_unlock_irqrestore(&phba->hbalock,
2259 iflag);
2260 }
2261 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2262 (saveq->iocb_flag & LPFC_EXCHANGE_BUSY)) {
2263 /* Set cmdiocb flag for the exchange
2264 * busy so sgl (xri) will not be
2265 * released until the abort xri is
2266 * received from hba, clear the
2267 * LPFC_DRIVER_ABORTED bit in case
2268 * it was driver initiated abort.
2269 */
2270 spin_lock_irqsave(&phba->hbalock,
2271 iflag);
2272 cmdiocbp->iocb_flag &=
2273 ~LPFC_DRIVER_ABORTED;
2274 cmdiocbp->iocb_flag |=
2275 LPFC_EXCHANGE_BUSY;
2276 spin_unlock_irqrestore(&phba->hbalock,
2277 iflag);
2278 cmdiocbp->iocb.ulpStatus =
2279 IOSTAT_LOCAL_REJECT;
2280 cmdiocbp->iocb.un.ulpWord[4] =
2281 IOERR_ABORT_REQUESTED;
2282 /*
2283 * For SLI4, irsiocb contains NO_XRI
2284 * in sli_xritag, it shall not affect
2285 * releasing sgl (xri) process.
2286 */
2287 saveq->iocb.ulpStatus =
2288 IOSTAT_LOCAL_REJECT;
2289 saveq->iocb.un.ulpWord[4] =
2290 IOERR_SLI_ABORTED;
2291 spin_lock_irqsave(&phba->hbalock,
2292 iflag);
2293 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2294 spin_unlock_irqrestore(&phba->hbalock,
2295 iflag);
James Smart07951072007-04-25 09:51:38 -04002296 }
dea31012005-04-17 16:05:31 -05002297 }
James Smart2e0fef82007-06-17 19:56:36 -05002298 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002299 } else
2300 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002301 } else {
2302 /*
2303 * Unknown initiating command based on the response iotag.
2304 * This could be the case on the ELS ring because of
2305 * lpfc_els_abort().
2306 */
2307 if (pring->ringno != LPFC_ELS_RING) {
2308 /*
2309 * Ring <ringno> handler: unexpected completion IoTag
2310 * <IoTag>
2311 */
James Smarta257bf92009-04-06 18:48:10 -04002312 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002313 "0322 Ring %d handler: "
2314 "unexpected completion IoTag x%x "
2315 "Data: x%x x%x x%x x%x\n",
2316 pring->ringno,
2317 saveq->iocb.ulpIoTag,
2318 saveq->iocb.ulpStatus,
2319 saveq->iocb.un.ulpWord[4],
2320 saveq->iocb.ulpCommand,
2321 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002322 }
2323 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002324
dea31012005-04-17 16:05:31 -05002325 return rc;
2326}
2327
James Smarte59058c2008-08-24 21:49:00 -04002328/**
James Smart3621a712009-04-06 18:47:14 -04002329 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002330 * @phba: Pointer to HBA context object.
2331 * @pring: Pointer to driver SLI ring object.
2332 *
2333 * This function is called from the iocb ring event handlers when
2334 * put pointer is ahead of the get pointer for a ring. This function signal
2335 * an error attention condition to the worker thread and the worker
2336 * thread will transition the HBA to offline state.
2337 **/
James Smart2e0fef82007-06-17 19:56:36 -05002338static void
2339lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002340{
James Smart34b02dc2008-08-24 21:49:55 -04002341 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002342 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002343 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002344 * rsp ring <portRspMax>
2345 */
2346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002347 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002348 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002349 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002350 pring->numRiocb);
2351
James Smart2e0fef82007-06-17 19:56:36 -05002352 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002353
2354 /*
2355 * All error attention handlers are posted to
2356 * worker thread
2357 */
2358 phba->work_ha |= HA_ERATT;
2359 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002360
James Smart5e9d9b82008-06-14 22:52:53 -04002361 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002362
2363 return;
2364}
2365
James Smarte59058c2008-08-24 21:49:00 -04002366/**
James Smart3621a712009-04-06 18:47:14 -04002367 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002368 * @ptr: Pointer to address of HBA context object.
2369 *
2370 * This function is invoked by the Error Attention polling timer when the
2371 * timer times out. It will check the SLI Error Attention register for
2372 * possible attention events. If so, it will post an Error Attention event
2373 * and wake up worker thread to process it. Otherwise, it will set up the
2374 * Error Attention polling timer for the next poll.
2375 **/
2376void lpfc_poll_eratt(unsigned long ptr)
2377{
2378 struct lpfc_hba *phba;
2379 uint32_t eratt = 0;
2380
2381 phba = (struct lpfc_hba *)ptr;
2382
2383 /* Check chip HA register for error event */
2384 eratt = lpfc_sli_check_eratt(phba);
2385
2386 if (eratt)
2387 /* Tell the worker thread there is work to do */
2388 lpfc_worker_wake_up(phba);
2389 else
2390 /* Restart the timer for next eratt poll */
2391 mod_timer(&phba->eratt_poll, jiffies +
2392 HZ * LPFC_ERATT_POLL_INTERVAL);
2393 return;
2394}
2395
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002396
James Smarte59058c2008-08-24 21:49:00 -04002397/**
James Smart3621a712009-04-06 18:47:14 -04002398 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002399 * @phba: Pointer to HBA context object.
2400 * @pring: Pointer to driver SLI ring object.
2401 * @mask: Host attention register mask for this ring.
2402 *
2403 * This function is called from the interrupt context when there is a ring
2404 * event for the fcp ring. The caller does not hold any lock.
2405 * The function processes each response iocb in the response ring until it
2406 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2407 * LE bit set. The function will call the completion handler of the command iocb
2408 * if the response iocb indicates a completion for a command iocb or it is
2409 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2410 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002411 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002412 * to check it explicitly.
2413 */
2414int
James Smart2e0fef82007-06-17 19:56:36 -05002415lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2416 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002417{
James Smart34b02dc2008-08-24 21:49:55 -04002418 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002419 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002420 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002421 struct lpfc_iocbq *cmdiocbq = NULL;
2422 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002423 uint32_t status;
2424 uint32_t portRspPut, portRspMax;
2425 int rc = 1;
2426 lpfc_iocb_type type;
2427 unsigned long iflag;
2428 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002429
James Smart2e0fef82007-06-17 19:56:36 -05002430 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002431 pring->stats.iocb_event++;
2432
dea31012005-04-17 16:05:31 -05002433 /*
2434 * The next available response entry should never exceed the maximum
2435 * entries. If it does, treat it as an adapter hardware error.
2436 */
2437 portRspMax = pring->numRiocb;
2438 portRspPut = le32_to_cpu(pgp->rspPutInx);
2439 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002440 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002441 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002442 return 1;
2443 }
James Smart45ed1192009-10-02 15:17:02 -04002444 if (phba->fcp_ring_in_use) {
2445 spin_unlock_irqrestore(&phba->hbalock, iflag);
2446 return 1;
2447 } else
2448 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05002449
2450 rmb();
2451 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002452 /*
2453 * Fetch an entry off the ring and copy it into a local data
2454 * structure. The copy involves a byte-swap since the
2455 * network byte order and pci byte orders are different.
2456 */
James Smarted957682007-06-17 19:56:37 -05002457 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002458 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002459
2460 if (++pring->rspidx >= portRspMax)
2461 pring->rspidx = 0;
2462
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002463 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2464 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002465 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002466 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002467 irsp = &rspiocbq.iocb;
2468
dea31012005-04-17 16:05:31 -05002469 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2470 pring->stats.iocb_rsp++;
2471 rsp_cmpl++;
2472
2473 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002474 /*
2475 * If resource errors reported from HBA, reduce
2476 * queuedepths of the SCSI device.
2477 */
2478 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2479 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2480 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002481 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002482 spin_lock_irqsave(&phba->hbalock, iflag);
2483 }
2484
dea31012005-04-17 16:05:31 -05002485 /* Rsp ring <ringno> error: IOCB */
2486 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002487 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002488 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002489 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002490 irsp->un.ulpWord[0],
2491 irsp->un.ulpWord[1],
2492 irsp->un.ulpWord[2],
2493 irsp->un.ulpWord[3],
2494 irsp->un.ulpWord[4],
2495 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002496 *(uint32_t *)&irsp->un1,
2497 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002498 }
2499
2500 switch (type) {
2501 case LPFC_ABORT_IOCB:
2502 case LPFC_SOL_IOCB:
2503 /*
2504 * Idle exchange closed via ABTS from port. No iocb
2505 * resources need to be recovered.
2506 */
2507 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002508 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002509 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002510 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002511 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002512 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002513 break;
2514 }
2515
James Bottomley604a3e32005-10-29 10:28:33 -05002516 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2517 &rspiocbq);
dea31012005-04-17 16:05:31 -05002518 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
James Smart2e0fef82007-06-17 19:56:36 -05002519 spin_unlock_irqrestore(&phba->hbalock,
2520 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002521 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2522 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002523 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002524 iflag);
2525 }
dea31012005-04-17 16:05:31 -05002526 break;
James Smarta4bc3372006-12-02 13:34:16 -05002527 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002528 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002529 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002530 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002531 break;
dea31012005-04-17 16:05:31 -05002532 default:
2533 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2534 char adaptermsg[LPFC_MAX_ADPTMSG];
2535 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2536 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2537 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002538 dev_warn(&((phba->pcidev)->dev),
2539 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002540 phba->brd_no, adaptermsg);
2541 } else {
2542 /* Unknown IOCB command */
2543 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002544 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002545 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002546 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002547 irsp->ulpStatus,
2548 irsp->ulpIoTag,
2549 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002550 }
2551 break;
2552 }
2553
2554 /*
2555 * The response IOCB has been processed. Update the ring
2556 * pointer in SLIM. If the port response put pointer has not
2557 * been updated, sync the pgp->rspPutInx and fetch the new port
2558 * response put pointer.
2559 */
James Smarted957682007-06-17 19:56:37 -05002560 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002561
2562 if (pring->rspidx == portRspPut)
2563 portRspPut = le32_to_cpu(pgp->rspPutInx);
2564 }
2565
2566 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2567 pring->stats.iocb_rsp_full++;
2568 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2569 writel(status, phba->CAregaddr);
2570 readl(phba->CAregaddr);
2571 }
2572 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2573 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2574 pring->stats.iocb_cmd_empty++;
2575
2576 /* Force update of the local copy of cmdGetInx */
2577 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2578 lpfc_sli_resume_iocb(phba, pring);
2579
2580 if ((pring->lpfc_sli_cmd_available))
2581 (pring->lpfc_sli_cmd_available) (phba, pring);
2582
2583 }
2584
James Smart45ed1192009-10-02 15:17:02 -04002585 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05002586 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002587 return rc;
2588}
2589
James Smarte59058c2008-08-24 21:49:00 -04002590/**
James Smart3772a992009-05-22 14:50:54 -04002591 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2592 * @phba: Pointer to HBA context object.
2593 * @pring: Pointer to driver SLI ring object.
2594 * @rspiocbp: Pointer to driver response IOCB object.
2595 *
2596 * This function is called from the worker thread when there is a slow-path
2597 * response IOCB to process. This function chains all the response iocbs until
2598 * seeing the iocb with the LE bit set. The function will call
2599 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2600 * completion of a command iocb. The function will call the
2601 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2602 * The function frees the resources or calls the completion handler if this
2603 * iocb is an abort completion. The function returns NULL when the response
2604 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2605 * this function shall chain the iocb on to the iocb_continueq and return the
2606 * response iocb passed in.
2607 **/
2608static struct lpfc_iocbq *
2609lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2610 struct lpfc_iocbq *rspiocbp)
2611{
2612 struct lpfc_iocbq *saveq;
2613 struct lpfc_iocbq *cmdiocbp;
2614 struct lpfc_iocbq *next_iocb;
2615 IOCB_t *irsp = NULL;
2616 uint32_t free_saveq;
2617 uint8_t iocb_cmd_type;
2618 lpfc_iocb_type type;
2619 unsigned long iflag;
2620 int rc;
2621
2622 spin_lock_irqsave(&phba->hbalock, iflag);
2623 /* First add the response iocb to the countinueq list */
2624 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2625 pring->iocb_continueq_cnt++;
2626
2627 /* Now, determine whetehr the list is completed for processing */
2628 irsp = &rspiocbp->iocb;
2629 if (irsp->ulpLe) {
2630 /*
2631 * By default, the driver expects to free all resources
2632 * associated with this iocb completion.
2633 */
2634 free_saveq = 1;
2635 saveq = list_get_first(&pring->iocb_continueq,
2636 struct lpfc_iocbq, list);
2637 irsp = &(saveq->iocb);
2638 list_del_init(&pring->iocb_continueq);
2639 pring->iocb_continueq_cnt = 0;
2640
2641 pring->stats.iocb_rsp++;
2642
2643 /*
2644 * If resource errors reported from HBA, reduce
2645 * queuedepths of the SCSI device.
2646 */
2647 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2648 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2649 spin_unlock_irqrestore(&phba->hbalock, iflag);
2650 phba->lpfc_rampdown_queue_depth(phba);
2651 spin_lock_irqsave(&phba->hbalock, iflag);
2652 }
2653
2654 if (irsp->ulpStatus) {
2655 /* Rsp ring <ringno> error: IOCB */
2656 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2657 "0328 Rsp Ring %d error: "
2658 "IOCB Data: "
2659 "x%x x%x x%x x%x "
2660 "x%x x%x x%x x%x "
2661 "x%x x%x x%x x%x "
2662 "x%x x%x x%x x%x\n",
2663 pring->ringno,
2664 irsp->un.ulpWord[0],
2665 irsp->un.ulpWord[1],
2666 irsp->un.ulpWord[2],
2667 irsp->un.ulpWord[3],
2668 irsp->un.ulpWord[4],
2669 irsp->un.ulpWord[5],
2670 *(((uint32_t *) irsp) + 6),
2671 *(((uint32_t *) irsp) + 7),
2672 *(((uint32_t *) irsp) + 8),
2673 *(((uint32_t *) irsp) + 9),
2674 *(((uint32_t *) irsp) + 10),
2675 *(((uint32_t *) irsp) + 11),
2676 *(((uint32_t *) irsp) + 12),
2677 *(((uint32_t *) irsp) + 13),
2678 *(((uint32_t *) irsp) + 14),
2679 *(((uint32_t *) irsp) + 15));
2680 }
2681
2682 /*
2683 * Fetch the IOCB command type and call the correct completion
2684 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2685 * get freed back to the lpfc_iocb_list by the discovery
2686 * kernel thread.
2687 */
2688 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2689 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2690 switch (type) {
2691 case LPFC_SOL_IOCB:
2692 spin_unlock_irqrestore(&phba->hbalock, iflag);
2693 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2694 spin_lock_irqsave(&phba->hbalock, iflag);
2695 break;
2696
2697 case LPFC_UNSOL_IOCB:
2698 spin_unlock_irqrestore(&phba->hbalock, iflag);
2699 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2700 spin_lock_irqsave(&phba->hbalock, iflag);
2701 if (!rc)
2702 free_saveq = 0;
2703 break;
2704
2705 case LPFC_ABORT_IOCB:
2706 cmdiocbp = NULL;
2707 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2708 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2709 saveq);
2710 if (cmdiocbp) {
2711 /* Call the specified completion routine */
2712 if (cmdiocbp->iocb_cmpl) {
2713 spin_unlock_irqrestore(&phba->hbalock,
2714 iflag);
2715 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2716 saveq);
2717 spin_lock_irqsave(&phba->hbalock,
2718 iflag);
2719 } else
2720 __lpfc_sli_release_iocbq(phba,
2721 cmdiocbp);
2722 }
2723 break;
2724
2725 case LPFC_UNKNOWN_IOCB:
2726 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2727 char adaptermsg[LPFC_MAX_ADPTMSG];
2728 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2729 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2730 MAX_MSG_DATA);
2731 dev_warn(&((phba->pcidev)->dev),
2732 "lpfc%d: %s\n",
2733 phba->brd_no, adaptermsg);
2734 } else {
2735 /* Unknown IOCB command */
2736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2737 "0335 Unknown IOCB "
2738 "command Data: x%x "
2739 "x%x x%x x%x\n",
2740 irsp->ulpCommand,
2741 irsp->ulpStatus,
2742 irsp->ulpIoTag,
2743 irsp->ulpContext);
2744 }
2745 break;
2746 }
2747
2748 if (free_saveq) {
2749 list_for_each_entry_safe(rspiocbp, next_iocb,
2750 &saveq->list, list) {
2751 list_del(&rspiocbp->list);
2752 __lpfc_sli_release_iocbq(phba, rspiocbp);
2753 }
2754 __lpfc_sli_release_iocbq(phba, saveq);
2755 }
2756 rspiocbp = NULL;
2757 }
2758 spin_unlock_irqrestore(&phba->hbalock, iflag);
2759 return rspiocbp;
2760}
2761
2762/**
2763 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002764 * @phba: Pointer to HBA context object.
2765 * @pring: Pointer to driver SLI ring object.
2766 * @mask: Host attention register mask for this ring.
2767 *
James Smart3772a992009-05-22 14:50:54 -04002768 * This routine wraps the actual slow_ring event process routine from the
2769 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002770 **/
James Smart3772a992009-05-22 14:50:54 -04002771void
James Smart2e0fef82007-06-17 19:56:36 -05002772lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2773 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002774{
James Smart3772a992009-05-22 14:50:54 -04002775 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2776}
2777
2778/**
2779 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2780 * @phba: Pointer to HBA context object.
2781 * @pring: Pointer to driver SLI ring object.
2782 * @mask: Host attention register mask for this ring.
2783 *
2784 * This function is called from the worker thread when there is a ring event
2785 * for non-fcp rings. The caller does not hold any lock. The function will
2786 * remove each response iocb in the response ring and calls the handle
2787 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2788 **/
2789static void
2790lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2791 struct lpfc_sli_ring *pring, uint32_t mask)
2792{
James Smart34b02dc2008-08-24 21:49:55 -04002793 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002794 IOCB_t *entry;
2795 IOCB_t *irsp = NULL;
2796 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002797 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002798 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002799 uint32_t status;
dea31012005-04-17 16:05:31 -05002800
James Smart34b02dc2008-08-24 21:49:55 -04002801 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002802 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002803 pring->stats.iocb_event++;
2804
dea31012005-04-17 16:05:31 -05002805 /*
2806 * The next available response entry should never exceed the maximum
2807 * entries. If it does, treat it as an adapter hardware error.
2808 */
2809 portRspMax = pring->numRiocb;
2810 portRspPut = le32_to_cpu(pgp->rspPutInx);
2811 if (portRspPut >= portRspMax) {
2812 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002813 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002814 * rsp ring <portRspMax>
2815 */
James Smarted957682007-06-17 19:56:37 -05002816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002817 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002818 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002819 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002820
James Smart2e0fef82007-06-17 19:56:36 -05002821 phba->link_state = LPFC_HBA_ERROR;
2822 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002823
2824 phba->work_hs = HS_FFER3;
2825 lpfc_handle_eratt(phba);
2826
James Smart3772a992009-05-22 14:50:54 -04002827 return;
dea31012005-04-17 16:05:31 -05002828 }
2829
2830 rmb();
dea31012005-04-17 16:05:31 -05002831 while (pring->rspidx != portRspPut) {
2832 /*
2833 * Build a completion list and call the appropriate handler.
2834 * The process is to get the next available response iocb, get
2835 * a free iocb from the list, copy the response data into the
2836 * free iocb, insert to the continuation list, and update the
2837 * next response index to slim. This process makes response
2838 * iocb's in the ring available to DMA as fast as possible but
2839 * pays a penalty for a copy operation. Since the iocb is
2840 * only 32 bytes, this penalty is considered small relative to
2841 * the PCI reads for register values and a slim write. When
2842 * the ulpLe field is set, the entire Command has been
2843 * received.
2844 */
James Smarted957682007-06-17 19:56:37 -05002845 entry = lpfc_resp_iocb(phba, pring);
2846
James Smart858c9f62007-06-17 19:56:39 -05002847 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002848 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002849 if (rspiocbp == NULL) {
2850 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002851 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002852 break;
2853 }
2854
James Smarted957682007-06-17 19:56:37 -05002855 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2856 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002857 irsp = &rspiocbp->iocb;
2858
2859 if (++pring->rspidx >= portRspMax)
2860 pring->rspidx = 0;
2861
James Smarta58cbd52007-08-02 11:09:43 -04002862 if (pring->ringno == LPFC_ELS_RING) {
2863 lpfc_debugfs_slow_ring_trc(phba,
2864 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2865 *(((uint32_t *) irsp) + 4),
2866 *(((uint32_t *) irsp) + 6),
2867 *(((uint32_t *) irsp) + 7));
2868 }
2869
James Smarted957682007-06-17 19:56:37 -05002870 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002871
James Smart3772a992009-05-22 14:50:54 -04002872 spin_unlock_irqrestore(&phba->hbalock, iflag);
2873 /* Handle the response IOCB */
2874 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2875 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002876
2877 /*
2878 * If the port response put pointer has not been updated, sync
2879 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2880 * response put pointer.
2881 */
2882 if (pring->rspidx == portRspPut) {
2883 portRspPut = le32_to_cpu(pgp->rspPutInx);
2884 }
2885 } /* while (pring->rspidx != portRspPut) */
2886
James Smart92d7f7b2007-06-17 19:56:38 -05002887 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002888 /* At least one response entry has been freed */
2889 pring->stats.iocb_rsp_full++;
2890 /* SET RxRE_RSP in Chip Att register */
2891 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2892 writel(status, phba->CAregaddr);
2893 readl(phba->CAregaddr); /* flush */
2894 }
2895 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2896 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2897 pring->stats.iocb_cmd_empty++;
2898
2899 /* Force update of the local copy of cmdGetInx */
2900 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2901 lpfc_sli_resume_iocb(phba, pring);
2902
2903 if ((pring->lpfc_sli_cmd_available))
2904 (pring->lpfc_sli_cmd_available) (phba, pring);
2905
2906 }
2907
James Smart2e0fef82007-06-17 19:56:36 -05002908 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002909 return;
dea31012005-04-17 16:05:31 -05002910}
2911
James Smarte59058c2008-08-24 21:49:00 -04002912/**
James Smart4f774512009-05-22 14:52:35 -04002913 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2914 * @phba: Pointer to HBA context object.
2915 * @pring: Pointer to driver SLI ring object.
2916 * @mask: Host attention register mask for this ring.
2917 *
2918 * This function is called from the worker thread when there is a pending
2919 * ELS response iocb on the driver internal slow-path response iocb worker
2920 * queue. The caller does not hold any lock. The function will remove each
2921 * response iocb from the response worker queue and calls the handle
2922 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2923 **/
2924static void
2925lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2926 struct lpfc_sli_ring *pring, uint32_t mask)
2927{
2928 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04002929 struct hbq_dmabuf *dmabuf;
2930 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04002931 unsigned long iflag;
2932
James Smart45ed1192009-10-02 15:17:02 -04002933 spin_lock_irqsave(&phba->hbalock, iflag);
2934 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2935 spin_unlock_irqrestore(&phba->hbalock, iflag);
2936 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04002937 /* Get the response iocb from the head of work queue */
2938 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04002939 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04002940 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04002941 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04002942
2943 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2944 case CQE_CODE_COMPL_WQE:
2945 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2946 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04002947 /* Translate ELS WCQE to response IOCBQ */
2948 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2949 irspiocbq);
2950 if (irspiocbq)
2951 lpfc_sli_sp_handle_rspiocb(phba, pring,
2952 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04002953 break;
2954 case CQE_CODE_RECEIVE:
2955 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2956 cq_event);
2957 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2958 break;
2959 default:
2960 break;
2961 }
James Smart4f774512009-05-22 14:52:35 -04002962 }
2963}
2964
2965/**
James Smart3621a712009-04-06 18:47:14 -04002966 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04002967 * @phba: Pointer to HBA context object.
2968 * @pring: Pointer to driver SLI ring object.
2969 *
2970 * This function aborts all iocbs in the given ring and frees all the iocb
2971 * objects in txq. This function issues an abort iocb for all the iocb commands
2972 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2973 * the return of this function. The caller is not required to hold any locks.
2974 **/
James Smart2e0fef82007-06-17 19:56:36 -05002975void
dea31012005-04-17 16:05:31 -05002976lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2977{
James Smart2534ba72007-04-25 09:52:20 -04002978 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05002979 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05002980
James Smart92d7f7b2007-06-17 19:56:38 -05002981 if (pring->ringno == LPFC_ELS_RING) {
2982 lpfc_fabric_abort_hba(phba);
2983 }
2984
dea31012005-04-17 16:05:31 -05002985 /* Error everything on txq and txcmplq
2986 * First do the txq.
2987 */
James Smart2e0fef82007-06-17 19:56:36 -05002988 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002989 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05002990 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05002991
2992 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04002993 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2994 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2995
James Smart2e0fef82007-06-17 19:56:36 -05002996 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04002997
James Smarta257bf92009-04-06 18:48:10 -04002998 /* Cancel all the IOCBs from the completions list */
2999 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3000 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003001}
3002
James Smarte59058c2008-08-24 21:49:00 -04003003/**
James Smart3621a712009-04-06 18:47:14 -04003004 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003005 * @phba: Pointer to HBA context object.
3006 *
3007 * This function flushes all iocbs in the fcp ring and frees all the iocb
3008 * objects in txq and txcmplq. This function will not issue abort iocbs
3009 * for all the iocb commands in txcmplq, they will just be returned with
3010 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3011 * slot has been permanently disabled.
3012 **/
3013void
3014lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3015{
3016 LIST_HEAD(txq);
3017 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003018 struct lpfc_sli *psli = &phba->sli;
3019 struct lpfc_sli_ring *pring;
3020
3021 /* Currently, only one fcp ring */
3022 pring = &psli->ring[psli->fcp_ring];
3023
3024 spin_lock_irq(&phba->hbalock);
3025 /* Retrieve everything on txq */
3026 list_splice_init(&pring->txq, &txq);
3027 pring->txq_cnt = 0;
3028
3029 /* Retrieve everything on the txcmplq */
3030 list_splice_init(&pring->txcmplq, &txcmplq);
3031 pring->txcmplq_cnt = 0;
3032 spin_unlock_irq(&phba->hbalock);
3033
3034 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003035 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3036 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003037
3038 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003039 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3040 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003041}
3042
3043/**
James Smart3772a992009-05-22 14:50:54 -04003044 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003045 * @phba: Pointer to HBA context object.
3046 * @mask: Bit mask to be checked.
3047 *
3048 * This function reads the host status register and compares
3049 * with the provided bit mask to check if HBA completed
3050 * the restart. This function will wait in a loop for the
3051 * HBA to complete restart. If the HBA does not restart within
3052 * 15 iterations, the function will reset the HBA again. The
3053 * function returns 1 when HBA fail to restart otherwise returns
3054 * zero.
3055 **/
James Smart3772a992009-05-22 14:50:54 -04003056static int
3057lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003058{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003059 uint32_t status;
3060 int i = 0;
3061 int retval = 0;
dea31012005-04-17 16:05:31 -05003062
Jamie Wellnitz41415862006-02-28 19:25:27 -05003063 /* Read the HBA Host Status Register */
3064 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003065
Jamie Wellnitz41415862006-02-28 19:25:27 -05003066 /*
3067 * Check status register every 100ms for 5 retries, then every
3068 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3069 * every 2.5 sec for 4.
3070 * Break our of the loop if errors occurred during init.
3071 */
3072 while (((status & mask) != mask) &&
3073 !(status & HS_FFERM) &&
3074 i++ < 20) {
dea31012005-04-17 16:05:31 -05003075
Jamie Wellnitz41415862006-02-28 19:25:27 -05003076 if (i <= 5)
3077 msleep(10);
3078 else if (i <= 10)
3079 msleep(500);
3080 else
3081 msleep(2500);
dea31012005-04-17 16:05:31 -05003082
Jamie Wellnitz41415862006-02-28 19:25:27 -05003083 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003084 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003085 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003086 lpfc_sli_brdrestart(phba);
3087 }
3088 /* Read the HBA Host Status Register */
3089 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003090 }
dea31012005-04-17 16:05:31 -05003091
Jamie Wellnitz41415862006-02-28 19:25:27 -05003092 /* Check to see if any errors occurred during init */
3093 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003094 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003095 retval = 1;
3096 }
dea31012005-04-17 16:05:31 -05003097
Jamie Wellnitz41415862006-02-28 19:25:27 -05003098 return retval;
dea31012005-04-17 16:05:31 -05003099}
3100
James Smartda0436e2009-05-22 14:51:39 -04003101/**
3102 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3103 * @phba: Pointer to HBA context object.
3104 * @mask: Bit mask to be checked.
3105 *
3106 * This function checks the host status register to check if HBA is
3107 * ready. This function will wait in a loop for the HBA to be ready
3108 * If the HBA is not ready , the function will will reset the HBA PCI
3109 * function again. The function returns 1 when HBA fail to be ready
3110 * otherwise returns zero.
3111 **/
3112static int
3113lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3114{
3115 uint32_t status;
3116 int retval = 0;
3117
3118 /* Read the HBA Host Status Register */
3119 status = lpfc_sli4_post_status_check(phba);
3120
3121 if (status) {
3122 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3123 lpfc_sli_brdrestart(phba);
3124 status = lpfc_sli4_post_status_check(phba);
3125 }
3126
3127 /* Check to see if any errors occurred during init */
3128 if (status) {
3129 phba->link_state = LPFC_HBA_ERROR;
3130 retval = 1;
3131 } else
3132 phba->sli4_hba.intr_enable = 0;
3133
3134 return retval;
3135}
3136
3137/**
3138 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3139 * @phba: Pointer to HBA context object.
3140 * @mask: Bit mask to be checked.
3141 *
3142 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3143 * from the API jump table function pointer from the lpfc_hba struct.
3144 **/
3145int
3146lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3147{
3148 return phba->lpfc_sli_brdready(phba, mask);
3149}
3150
James Smart92908312006-03-07 15:04:13 -05003151#define BARRIER_TEST_PATTERN (0xdeadbeef)
3152
James Smarte59058c2008-08-24 21:49:00 -04003153/**
James Smart3621a712009-04-06 18:47:14 -04003154 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003155 * @phba: Pointer to HBA context object.
3156 *
3157 * This function is called before resetting an HBA. This
3158 * function requests HBA to quiesce DMAs before a reset.
3159 **/
James Smart2e0fef82007-06-17 19:56:36 -05003160void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003161{
James Smart65a29c12006-07-06 15:50:50 -04003162 uint32_t __iomem *resp_buf;
3163 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003164 volatile uint32_t mbox;
3165 uint32_t hc_copy;
3166 int i;
3167 uint8_t hdrtype;
3168
3169 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3170 if (hdrtype != 0x80 ||
3171 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3172 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3173 return;
3174
3175 /*
3176 * Tell the other part of the chip to suspend temporarily all
3177 * its DMA activity.
3178 */
James Smart65a29c12006-07-06 15:50:50 -04003179 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003180
3181 /* Disable the error attention */
3182 hc_copy = readl(phba->HCregaddr);
3183 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3184 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003185 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003186
3187 if (readl(phba->HAregaddr) & HA_ERATT) {
3188 /* Clear Chip error bit */
3189 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003190 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003191 }
3192
3193 mbox = 0;
3194 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3195 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3196
3197 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003198 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003199 writel(mbox, mbox_buf);
3200
3201 for (i = 0;
3202 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3203 mdelay(1);
3204
3205 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003206 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003207 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003208 goto restore_hc;
3209 else
3210 goto clear_errat;
3211 }
3212
3213 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3214 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3215 mdelay(1);
3216
3217clear_errat:
3218
3219 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3220 mdelay(1);
3221
3222 if (readl(phba->HAregaddr) & HA_ERATT) {
3223 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003224 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003225 }
3226
3227restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003228 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003229 writel(hc_copy, phba->HCregaddr);
3230 readl(phba->HCregaddr); /* flush */
3231}
3232
James Smarte59058c2008-08-24 21:49:00 -04003233/**
James Smart3621a712009-04-06 18:47:14 -04003234 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003235 * @phba: Pointer to HBA context object.
3236 *
3237 * This function issues a kill_board mailbox command and waits for
3238 * the error attention interrupt. This function is called for stopping
3239 * the firmware processing. The caller is not required to hold any
3240 * locks. This function calls lpfc_hba_down_post function to free
3241 * any pending commands after the kill. The function will return 1 when it
3242 * fails to kill the board else will return 0.
3243 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003244int
James Smart2e0fef82007-06-17 19:56:36 -05003245lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003246{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003247 struct lpfc_sli *psli;
3248 LPFC_MBOXQ_t *pmb;
3249 uint32_t status;
3250 uint32_t ha_copy;
3251 int retval;
3252 int i = 0;
3253
3254 psli = &phba->sli;
3255
3256 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003257 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003258 "0329 Kill HBA Data: x%x x%x\n",
3259 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003260
James Smart98c9ea52007-10-27 13:37:33 -04003261 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3262 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003263 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003264
3265 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003266 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003267 status = readl(phba->HCregaddr);
3268 status &= ~HC_ERINT_ENA;
3269 writel(status, phba->HCregaddr);
3270 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003271 phba->link_flag |= LS_IGNORE_ERATT;
3272 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003273
3274 lpfc_kill_board(phba, pmb);
3275 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3276 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3277
3278 if (retval != MBX_SUCCESS) {
3279 if (retval != MBX_BUSY)
3280 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003281 spin_lock_irq(&phba->hbalock);
3282 phba->link_flag &= ~LS_IGNORE_ERATT;
3283 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003284 return 1;
3285 }
3286
James Smartf4b4c682009-05-22 14:53:12 -04003287 spin_lock_irq(&phba->hbalock);
3288 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3289 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003290
Jamie Wellnitz41415862006-02-28 19:25:27 -05003291 mempool_free(pmb, phba->mbox_mem_pool);
3292
3293 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3294 * attention every 100ms for 3 seconds. If we don't get ERATT after
3295 * 3 seconds we still set HBA_ERROR state because the status of the
3296 * board is now undefined.
3297 */
3298 ha_copy = readl(phba->HAregaddr);
3299
3300 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3301 mdelay(100);
3302 ha_copy = readl(phba->HAregaddr);
3303 }
3304
3305 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003306 if (ha_copy & HA_ERATT) {
3307 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003308 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003309 }
James Smart2e0fef82007-06-17 19:56:36 -05003310 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003311 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003312 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003313 phba->link_flag &= ~LS_IGNORE_ERATT;
3314 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003315
Jamie Wellnitz41415862006-02-28 19:25:27 -05003316 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003317 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003318
James Smart2e0fef82007-06-17 19:56:36 -05003319 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003320}
3321
James Smarte59058c2008-08-24 21:49:00 -04003322/**
James Smart3772a992009-05-22 14:50:54 -04003323 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003324 * @phba: Pointer to HBA context object.
3325 *
3326 * This function resets the HBA by writing HC_INITFF to the control
3327 * register. After the HBA resets, this function resets all the iocb ring
3328 * indices. This function disables PCI layer parity checking during
3329 * the reset.
3330 * This function returns 0 always.
3331 * The caller is not required to hold any locks.
3332 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003333int
James Smart2e0fef82007-06-17 19:56:36 -05003334lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003335{
3336 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003337 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003338 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003339 int i;
dea31012005-04-17 16:05:31 -05003340
Jamie Wellnitz41415862006-02-28 19:25:27 -05003341 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003342
Jamie Wellnitz41415862006-02-28 19:25:27 -05003343 /* Reset HBA */
3344 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003345 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003346 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003347
3348 /* perform board reset */
3349 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003350 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003351 phba->pport->fc_myDID = 0;
3352 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003353
Jamie Wellnitz41415862006-02-28 19:25:27 -05003354 /* Turn off parity checking and serr during the physical reset */
3355 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3356 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3357 (cfg_value &
3358 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3359
James Smart3772a992009-05-22 14:50:54 -04003360 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3361
Jamie Wellnitz41415862006-02-28 19:25:27 -05003362 /* Now toggle INITFF bit in the Host Control Register */
3363 writel(HC_INITFF, phba->HCregaddr);
3364 mdelay(1);
3365 readl(phba->HCregaddr); /* flush */
3366 writel(0, phba->HCregaddr);
3367 readl(phba->HCregaddr); /* flush */
3368
3369 /* Restore PCI cmd register */
3370 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003371
3372 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003373 for (i = 0; i < psli->num_rings; i++) {
3374 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003375 pring->flag = 0;
3376 pring->rspidx = 0;
3377 pring->next_cmdidx = 0;
3378 pring->local_getidx = 0;
3379 pring->cmdidx = 0;
3380 pring->missbufcnt = 0;
3381 }
dea31012005-04-17 16:05:31 -05003382
James Smart2e0fef82007-06-17 19:56:36 -05003383 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003384 return 0;
3385}
3386
James Smarte59058c2008-08-24 21:49:00 -04003387/**
James Smartda0436e2009-05-22 14:51:39 -04003388 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3389 * @phba: Pointer to HBA context object.
3390 *
3391 * This function resets a SLI4 HBA. This function disables PCI layer parity
3392 * checking during resets the device. The caller is not required to hold
3393 * any locks.
3394 *
3395 * This function returns 0 always.
3396 **/
3397int
3398lpfc_sli4_brdreset(struct lpfc_hba *phba)
3399{
3400 struct lpfc_sli *psli = &phba->sli;
3401 uint16_t cfg_value;
3402 uint8_t qindx;
3403
3404 /* Reset HBA */
3405 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3406 "0295 Reset HBA Data: x%x x%x\n",
3407 phba->pport->port_state, psli->sli_flag);
3408
3409 /* perform board reset */
3410 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003411 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003412 phba->pport->fc_myDID = 0;
3413 phba->pport->fc_prevDID = 0;
3414
3415 /* Turn off parity checking and serr during the physical reset */
3416 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3417 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3418 (cfg_value &
3419 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3420
3421 spin_lock_irq(&phba->hbalock);
3422 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3423 phba->fcf.fcf_flag = 0;
3424 /* Clean up the child queue list for the CQs */
3425 list_del_init(&phba->sli4_hba.mbx_wq->list);
3426 list_del_init(&phba->sli4_hba.els_wq->list);
3427 list_del_init(&phba->sli4_hba.hdr_rq->list);
3428 list_del_init(&phba->sli4_hba.dat_rq->list);
3429 list_del_init(&phba->sli4_hba.mbx_cq->list);
3430 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003431 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3432 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3433 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3434 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3435 spin_unlock_irq(&phba->hbalock);
3436
3437 /* Now physically reset the device */
3438 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3439 "0389 Performing PCI function reset!\n");
3440 /* Perform FCoE PCI function reset */
3441 lpfc_pci_function_reset(phba);
3442
3443 return 0;
3444}
3445
3446/**
3447 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003448 * @phba: Pointer to HBA context object.
3449 *
3450 * This function is called in the SLI initialization code path to
3451 * restart the HBA. The caller is not required to hold any lock.
3452 * This function writes MBX_RESTART mailbox command to the SLIM and
3453 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3454 * function to free any pending commands. The function enables
3455 * POST only during the first initialization. The function returns zero.
3456 * The function does not guarantee completion of MBX_RESTART mailbox
3457 * command before the return of this function.
3458 **/
James Smartda0436e2009-05-22 14:51:39 -04003459static int
3460lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003461{
3462 MAILBOX_t *mb;
3463 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003464 volatile uint32_t word0;
3465 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04003466 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003467
James Smart2e0fef82007-06-17 19:56:36 -05003468 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003469
James Smart0d878412009-10-02 15:16:56 -04003470 /* Take PCIe device Advanced Error Reporting (AER) state */
3471 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3472
Jamie Wellnitz41415862006-02-28 19:25:27 -05003473 psli = &phba->sli;
3474
3475 /* Restart HBA */
3476 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003477 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003478 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003479
3480 word0 = 0;
3481 mb = (MAILBOX_t *) &word0;
3482 mb->mbxCommand = MBX_RESTART;
3483 mb->mbxHc = 1;
3484
James Smart92908312006-03-07 15:04:13 -05003485 lpfc_reset_barrier(phba);
3486
Jamie Wellnitz41415862006-02-28 19:25:27 -05003487 to_slim = phba->MBslimaddr;
3488 writel(*(uint32_t *) mb, to_slim);
3489 readl(to_slim); /* flush */
3490
3491 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003492 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003493 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003494 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003495 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003496 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003497 writel(*(uint32_t *) mb, to_slim);
3498 readl(to_slim); /* flush */
3499
3500 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003501 phba->pport->stopped = 0;
3502 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003503 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003504 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003505
James Smart64ba8812006-08-02 15:24:34 -04003506 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3507 psli->stats_start = get_seconds();
3508
James Smarteaf15d52008-12-04 22:39:29 -05003509 /* Give the INITFF and Post time to settle. */
3510 mdelay(100);
dea31012005-04-17 16:05:31 -05003511
James Smart0d878412009-10-02 15:16:56 -04003512 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3513 if (hba_aer_enabled)
3514 pci_disable_pcie_error_reporting(phba->pcidev);
3515
Jamie Wellnitz41415862006-02-28 19:25:27 -05003516 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003517
3518 return 0;
3519}
3520
James Smarte59058c2008-08-24 21:49:00 -04003521/**
James Smartda0436e2009-05-22 14:51:39 -04003522 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3523 * @phba: Pointer to HBA context object.
3524 *
3525 * This function is called in the SLI initialization code path to restart
3526 * a SLI4 HBA. The caller is not required to hold any lock.
3527 * At the end of the function, it calls lpfc_hba_down_post function to
3528 * free any pending commands.
3529 **/
3530static int
3531lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3532{
3533 struct lpfc_sli *psli = &phba->sli;
3534
3535
3536 /* Restart HBA */
3537 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3538 "0296 Restart HBA Data: x%x x%x\n",
3539 phba->pport->port_state, psli->sli_flag);
3540
3541 lpfc_sli4_brdreset(phba);
3542
3543 spin_lock_irq(&phba->hbalock);
3544 phba->pport->stopped = 0;
3545 phba->link_state = LPFC_INIT_START;
3546 phba->hba_flag = 0;
3547 spin_unlock_irq(&phba->hbalock);
3548
3549 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3550 psli->stats_start = get_seconds();
3551
3552 lpfc_hba_down_post(phba);
3553
3554 return 0;
3555}
3556
3557/**
3558 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3559 * @phba: Pointer to HBA context object.
3560 *
3561 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3562 * API jump table function pointer from the lpfc_hba struct.
3563**/
3564int
3565lpfc_sli_brdrestart(struct lpfc_hba *phba)
3566{
3567 return phba->lpfc_sli_brdrestart(phba);
3568}
3569
3570/**
James Smart3621a712009-04-06 18:47:14 -04003571 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003572 * @phba: Pointer to HBA context object.
3573 *
3574 * This function is called after a HBA restart to wait for successful
3575 * restart of the HBA. Successful restart of the HBA is indicated by
3576 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3577 * iteration, the function will restart the HBA again. The function returns
3578 * zero if HBA successfully restarted else returns negative error code.
3579 **/
dea31012005-04-17 16:05:31 -05003580static int
3581lpfc_sli_chipset_init(struct lpfc_hba *phba)
3582{
3583 uint32_t status, i = 0;
3584
3585 /* Read the HBA Host Status Register */
3586 status = readl(phba->HSregaddr);
3587
3588 /* Check status register to see what current state is */
3589 i = 0;
3590 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3591
3592 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3593 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3594 * 4.
3595 */
3596 if (i++ >= 20) {
3597 /* Adapter failed to init, timeout, status reg
3598 <status> */
James Smarted957682007-06-17 19:56:37 -05003599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003600 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003601 "timeout, status reg x%x, "
3602 "FW Data: A8 x%x AC x%x\n", status,
3603 readl(phba->MBslimaddr + 0xa8),
3604 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003605 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003606 return -ETIMEDOUT;
3607 }
3608
3609 /* Check to see if any errors occurred during init */
3610 if (status & HS_FFERM) {
3611 /* ERROR: During chipset initialization */
3612 /* Adapter failed to init, chipset, status reg
3613 <status> */
James Smarted957682007-06-17 19:56:37 -05003614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003615 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003616 "chipset, status reg x%x, "
3617 "FW Data: A8 x%x AC x%x\n", status,
3618 readl(phba->MBslimaddr + 0xa8),
3619 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003620 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003621 return -EIO;
3622 }
3623
3624 if (i <= 5) {
3625 msleep(10);
3626 } else if (i <= 10) {
3627 msleep(500);
3628 } else {
3629 msleep(2500);
3630 }
3631
3632 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003633 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003634 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003635 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003636 }
3637 /* Read the HBA Host Status Register */
3638 status = readl(phba->HSregaddr);
3639 }
3640
3641 /* Check to see if any errors occurred during init */
3642 if (status & HS_FFERM) {
3643 /* ERROR: During chipset initialization */
3644 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003646 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003647 "status reg x%x, "
3648 "FW Data: A8 x%x AC x%x\n", status,
3649 readl(phba->MBslimaddr + 0xa8),
3650 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003651 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003652 return -EIO;
3653 }
3654
3655 /* Clear all interrupt enable conditions */
3656 writel(0, phba->HCregaddr);
3657 readl(phba->HCregaddr); /* flush */
3658
3659 /* setup host attn register */
3660 writel(0xffffffff, phba->HAregaddr);
3661 readl(phba->HAregaddr); /* flush */
3662 return 0;
3663}
3664
James Smarte59058c2008-08-24 21:49:00 -04003665/**
James Smart3621a712009-04-06 18:47:14 -04003666 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003667 *
3668 * This function calculates and returns the number of HBQs required to be
3669 * configured.
3670 **/
James Smart78b2d852007-08-02 11:10:21 -04003671int
James Smarted957682007-06-17 19:56:37 -05003672lpfc_sli_hbq_count(void)
3673{
James Smart92d7f7b2007-06-17 19:56:38 -05003674 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003675}
3676
James Smarte59058c2008-08-24 21:49:00 -04003677/**
James Smart3621a712009-04-06 18:47:14 -04003678 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003679 *
3680 * This function adds the number of hbq entries in every HBQ to get
3681 * the total number of hbq entries required for the HBA and returns
3682 * the total count.
3683 **/
James Smarted957682007-06-17 19:56:37 -05003684static int
3685lpfc_sli_hbq_entry_count(void)
3686{
3687 int hbq_count = lpfc_sli_hbq_count();
3688 int count = 0;
3689 int i;
3690
3691 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003692 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003693 return count;
3694}
3695
James Smarte59058c2008-08-24 21:49:00 -04003696/**
James Smart3621a712009-04-06 18:47:14 -04003697 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003698 *
3699 * This function calculates amount of memory required for all hbq entries
3700 * to be configured and returns the total memory required.
3701 **/
dea31012005-04-17 16:05:31 -05003702int
James Smarted957682007-06-17 19:56:37 -05003703lpfc_sli_hbq_size(void)
3704{
3705 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3706}
3707
James Smarte59058c2008-08-24 21:49:00 -04003708/**
James Smart3621a712009-04-06 18:47:14 -04003709 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003710 * @phba: Pointer to HBA context object.
3711 *
3712 * This function is called during the SLI initialization to configure
3713 * all the HBQs and post buffers to the HBQ. The caller is not
3714 * required to hold any locks. This function will return zero if successful
3715 * else it will return negative error code.
3716 **/
James Smarted957682007-06-17 19:56:37 -05003717static int
3718lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3719{
3720 int hbq_count = lpfc_sli_hbq_count();
3721 LPFC_MBOXQ_t *pmb;
3722 MAILBOX_t *pmbox;
3723 uint32_t hbqno;
3724 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003725
James Smart92d7f7b2007-06-17 19:56:38 -05003726 /* Get a Mailbox buffer to setup mailbox
3727 * commands for HBA initialization
3728 */
James Smarted957682007-06-17 19:56:37 -05003729 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3730
3731 if (!pmb)
3732 return -ENOMEM;
3733
James Smart04c68492009-05-22 14:52:52 -04003734 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003735
3736 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3737 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003738 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003739
3740 hbq_entry_index = 0;
3741 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3742 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3743 phba->hbqs[hbqno].hbqPutIdx = 0;
3744 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3745 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003746 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003747 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3748 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003749 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3750
3751 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3752 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3753 mbxStatus <status>, ring <num> */
3754
3755 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003756 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003757 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003758 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003759 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003760 pmbox->mbxStatus, hbqno);
3761
3762 phba->link_state = LPFC_HBA_ERROR;
3763 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003764 return ENXIO;
3765 }
3766 }
3767 phba->hbq_count = hbq_count;
3768
James Smarted957682007-06-17 19:56:37 -05003769 mempool_free(pmb, phba->mbox_mem_pool);
3770
James Smart92d7f7b2007-06-17 19:56:38 -05003771 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003772 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3773 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003774 return 0;
3775}
3776
James Smarte59058c2008-08-24 21:49:00 -04003777/**
James Smart4f774512009-05-22 14:52:35 -04003778 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3779 * @phba: Pointer to HBA context object.
3780 *
3781 * This function is called during the SLI initialization to configure
3782 * all the HBQs and post buffers to the HBQ. The caller is not
3783 * required to hold any locks. This function will return zero if successful
3784 * else it will return negative error code.
3785 **/
3786static int
3787lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3788{
3789 phba->hbq_in_use = 1;
3790 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3791 phba->hbq_count = 1;
3792 /* Initially populate or replenish the HBQs */
3793 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3794 return 0;
3795}
3796
3797/**
James Smart3621a712009-04-06 18:47:14 -04003798 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003799 * @phba: Pointer to HBA context object.
3800 * @sli_mode: sli mode - 2/3
3801 *
3802 * This function is called by the sli intialization code path
3803 * to issue config_port mailbox command. This function restarts the
3804 * HBA firmware and issues a config_port mailbox command to configure
3805 * the SLI interface in the sli mode specified by sli_mode
3806 * variable. The caller is not required to hold any locks.
3807 * The function returns 0 if successful, else returns negative error
3808 * code.
3809 **/
James Smart93996272008-08-24 21:50:30 -04003810int
3811lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003812{
3813 LPFC_MBOXQ_t *pmb;
3814 uint32_t resetcount = 0, rc = 0, done = 0;
3815
3816 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3817 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003818 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003819 return -ENOMEM;
3820 }
3821
James Smarted957682007-06-17 19:56:37 -05003822 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003823 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003824 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003825 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003826 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003827 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003828 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003829 rc = lpfc_sli_chipset_init(phba);
3830 if (rc)
3831 break;
3832
James Smart2e0fef82007-06-17 19:56:36 -05003833 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003834 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003835 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003836 resetcount++;
3837
James Smarted957682007-06-17 19:56:37 -05003838 /* Call pre CONFIG_PORT mailbox command initialization. A
3839 * value of 0 means the call was successful. Any other
3840 * nonzero value is a failure, but if ERESTART is returned,
3841 * the driver may reset the HBA and try again.
3842 */
dea31012005-04-17 16:05:31 -05003843 rc = lpfc_config_port_prep(phba);
3844 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003845 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003846 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003847 } else if (rc)
dea31012005-04-17 16:05:31 -05003848 break;
James Smart2e0fef82007-06-17 19:56:36 -05003849 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003850 lpfc_config_port(phba, pmb);
3851 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003852 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3853 LPFC_SLI3_HBQ_ENABLED |
3854 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003855 LPFC_SLI3_INB_ENABLED |
3856 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003857 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003859 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003860 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003861 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003862 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003863 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003864 spin_unlock_irq(&phba->hbalock);
3865 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003866 } else {
3867 /* Allow asynchronous mailbox command to go through */
3868 spin_lock_irq(&phba->hbalock);
3869 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3870 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003871 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003872 }
dea31012005-04-17 16:05:31 -05003873 }
James Smarted957682007-06-17 19:56:37 -05003874 if (!done) {
3875 rc = -EINVAL;
3876 goto do_prep_failed;
3877 }
James Smart04c68492009-05-22 14:52:52 -04003878 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3879 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003880 rc = -ENXIO;
3881 goto do_prep_failed;
3882 }
James Smart04c68492009-05-22 14:52:52 -04003883 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003884 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003885 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3886 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3887 phba->max_vpi : phba->max_vports;
3888
James Smart34b02dc2008-08-24 21:49:55 -04003889 } else
3890 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003891 if (pmb->u.mb.un.varCfgPort.gdss)
3892 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3893 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003894 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003895 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003896 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003897 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003898 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003899 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003900 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3901 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3902 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3903 phba->inb_last_counter =
3904 phba->mbox->us.s3_inb_pgp.counter;
3905 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003906 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003907 phba->port_gp = phba->mbox->us.s3_pgp.port;
3908 phba->inb_ha_copy = NULL;
3909 phba->inb_counter = NULL;
3910 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003911
3912 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003913 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003914 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3915 else
3916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3917 "0443 Adapter did not grant "
3918 "BlockGuard\n");
3919 }
James Smart34b02dc2008-08-24 21:49:55 -04003920 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003921 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003922 phba->port_gp = phba->mbox->us.s2.port;
3923 phba->inb_ha_copy = NULL;
3924 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003925 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003926 }
James Smart92d7f7b2007-06-17 19:56:38 -05003927do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003928 mempool_free(pmb, phba->mbox_mem_pool);
3929 return rc;
3930}
3931
James Smarte59058c2008-08-24 21:49:00 -04003932
3933/**
James Smart3621a712009-04-06 18:47:14 -04003934 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003935 * @phba: Pointer to HBA context object.
3936 *
3937 * This function is the main SLI intialization function. This function
3938 * is called by the HBA intialization code, HBA reset code and HBA
3939 * error attention handler code. Caller is not required to hold any
3940 * locks. This function issues config_port mailbox command to configure
3941 * the SLI, setup iocb rings and HBQ rings. In the end the function
3942 * calls the config_port_post function to issue init_link mailbox
3943 * command and to start the discovery. The function will return zero
3944 * if successful, else it will return negative error code.
3945 **/
James Smarted957682007-06-17 19:56:37 -05003946int
3947lpfc_sli_hba_setup(struct lpfc_hba *phba)
3948{
3949 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003950 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05003951
3952 switch (lpfc_sli_mode) {
3953 case 2:
James Smart78b2d852007-08-02 11:10:21 -04003954 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05003955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003956 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05003957 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04003958 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05003959 break;
3960 }
James Smarted957682007-06-17 19:56:37 -05003961 mode = 2;
3962 break;
3963 case 0:
3964 case 3:
3965 break;
3966 default:
James Smart92d7f7b2007-06-17 19:56:38 -05003967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003968 "1819 Unrecognized lpfc_sli_mode "
3969 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05003970
3971 break;
3972 }
3973
James Smart93996272008-08-24 21:50:30 -04003974 rc = lpfc_sli_config_port(phba, mode);
3975
James Smarted957682007-06-17 19:56:37 -05003976 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05003977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003978 "1820 Unable to select SLI-3. "
3979 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05003980 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04003981 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05003982 if (rc)
dea31012005-04-17 16:05:31 -05003983 goto lpfc_sli_hba_setup_error;
3984
James Smart0d878412009-10-02 15:16:56 -04003985 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
3986 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
3987 rc = pci_enable_pcie_error_reporting(phba->pcidev);
3988 if (!rc) {
3989 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3990 "2709 This device supports "
3991 "Advanced Error Reporting (AER)\n");
3992 spin_lock_irq(&phba->hbalock);
3993 phba->hba_flag |= HBA_AER_ENABLED;
3994 spin_unlock_irq(&phba->hbalock);
3995 } else {
3996 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3997 "2708 This device does not support "
3998 "Advanced Error Reporting (AER)\n");
3999 phba->cfg_aer_support = 0;
4000 }
4001 }
4002
James Smarted957682007-06-17 19:56:37 -05004003 if (phba->sli_rev == 3) {
4004 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4005 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004006 } else {
4007 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4008 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004009 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004010 }
4011
4012 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004013 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4014 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004015 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004016
4017 if (rc)
4018 goto lpfc_sli_hba_setup_error;
4019
James Smart93996272008-08-24 21:50:30 -04004020 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004021 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4022 rc = lpfc_sli_hbq_setup(phba);
4023 if (rc)
4024 goto lpfc_sli_hba_setup_error;
4025 }
James Smart04c68492009-05-22 14:52:52 -04004026 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004027 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004028 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004029
4030 rc = lpfc_config_port_post(phba);
4031 if (rc)
4032 goto lpfc_sli_hba_setup_error;
4033
James Smarted957682007-06-17 19:56:37 -05004034 return rc;
4035
James Smart92d7f7b2007-06-17 19:56:38 -05004036lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004037 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05004038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004039 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004040 return rc;
4041}
4042
James Smartda0436e2009-05-22 14:51:39 -04004043/**
4044 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4045 * @phba: Pointer to HBA context object.
4046 * @mboxq: mailbox pointer.
4047 * This function issue a dump mailbox command to read config region
4048 * 23 and parse the records in the region and populate driver
4049 * data structure.
4050 **/
4051static int
4052lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4053 LPFC_MBOXQ_t *mboxq)
4054{
4055 struct lpfc_dmabuf *mp;
4056 struct lpfc_mqe *mqe;
4057 uint32_t data_length;
4058 int rc;
4059
4060 /* Program the default value of vlan_id and fc_map */
4061 phba->valid_vlan = 0;
4062 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4063 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4064 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4065
4066 mqe = &mboxq->u.mqe;
4067 if (lpfc_dump_fcoe_param(phba, mboxq))
4068 return -ENOMEM;
4069
4070 mp = (struct lpfc_dmabuf *) mboxq->context1;
4071 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4072
4073 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4074 "(%d):2571 Mailbox cmd x%x Status x%x "
4075 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4076 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4077 "CQ: x%x x%x x%x x%x\n",
4078 mboxq->vport ? mboxq->vport->vpi : 0,
4079 bf_get(lpfc_mqe_command, mqe),
4080 bf_get(lpfc_mqe_status, mqe),
4081 mqe->un.mb_words[0], mqe->un.mb_words[1],
4082 mqe->un.mb_words[2], mqe->un.mb_words[3],
4083 mqe->un.mb_words[4], mqe->un.mb_words[5],
4084 mqe->un.mb_words[6], mqe->un.mb_words[7],
4085 mqe->un.mb_words[8], mqe->un.mb_words[9],
4086 mqe->un.mb_words[10], mqe->un.mb_words[11],
4087 mqe->un.mb_words[12], mqe->un.mb_words[13],
4088 mqe->un.mb_words[14], mqe->un.mb_words[15],
4089 mqe->un.mb_words[16], mqe->un.mb_words[50],
4090 mboxq->mcqe.word0,
4091 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4092 mboxq->mcqe.trailer);
4093
4094 if (rc) {
4095 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4096 kfree(mp);
4097 return -EIO;
4098 }
4099 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004100 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004101 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4102 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004103 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004104 }
James Smartda0436e2009-05-22 14:51:39 -04004105
4106 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4107 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4108 kfree(mp);
4109 return 0;
4110}
4111
4112/**
4113 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4114 * @phba: pointer to lpfc hba data structure.
4115 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4116 * @vpd: pointer to the memory to hold resulting port vpd data.
4117 * @vpd_size: On input, the number of bytes allocated to @vpd.
4118 * On output, the number of data bytes in @vpd.
4119 *
4120 * This routine executes a READ_REV SLI4 mailbox command. In
4121 * addition, this routine gets the port vpd data.
4122 *
4123 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004124 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004125 * ENOMEM - could not allocated memory.
4126 **/
4127static int
4128lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4129 uint8_t *vpd, uint32_t *vpd_size)
4130{
4131 int rc = 0;
4132 uint32_t dma_size;
4133 struct lpfc_dmabuf *dmabuf;
4134 struct lpfc_mqe *mqe;
4135
4136 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4137 if (!dmabuf)
4138 return -ENOMEM;
4139
4140 /*
4141 * Get a DMA buffer for the vpd data resulting from the READ_REV
4142 * mailbox command.
4143 */
4144 dma_size = *vpd_size;
4145 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4146 dma_size,
4147 &dmabuf->phys,
4148 GFP_KERNEL);
4149 if (!dmabuf->virt) {
4150 kfree(dmabuf);
4151 return -ENOMEM;
4152 }
4153 memset(dmabuf->virt, 0, dma_size);
4154
4155 /*
4156 * The SLI4 implementation of READ_REV conflicts at word1,
4157 * bits 31:16 and SLI4 adds vpd functionality not present
4158 * in SLI3. This code corrects the conflicts.
4159 */
4160 lpfc_read_rev(phba, mboxq);
4161 mqe = &mboxq->u.mqe;
4162 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4163 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4164 mqe->un.read_rev.word1 &= 0x0000FFFF;
4165 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4166 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4167
4168 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4169 if (rc) {
4170 dma_free_coherent(&phba->pcidev->dev, dma_size,
4171 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004172 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004173 return -EIO;
4174 }
4175
James Smartda0436e2009-05-22 14:51:39 -04004176 /*
4177 * The available vpd length cannot be bigger than the
4178 * DMA buffer passed to the port. Catch the less than
4179 * case and update the caller's size.
4180 */
4181 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4182 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4183
4184 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4185 dma_free_coherent(&phba->pcidev->dev, dma_size,
4186 dmabuf->virt, dmabuf->phys);
4187 kfree(dmabuf);
4188 return 0;
4189}
4190
4191/**
4192 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4193 * @phba: pointer to lpfc hba data structure.
4194 *
4195 * This routine is called to explicitly arm the SLI4 device's completion and
4196 * event queues
4197 **/
4198static void
4199lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4200{
4201 uint8_t fcp_eqidx;
4202
4203 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4204 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004205 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4206 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4207 LPFC_QUEUE_REARM);
4208 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4209 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4210 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4211 LPFC_QUEUE_REARM);
4212}
4213
4214/**
4215 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4216 * @phba: Pointer to HBA context object.
4217 *
4218 * This function is the main SLI4 device intialization PCI function. This
4219 * function is called by the HBA intialization code, HBA reset code and
4220 * HBA error attention handler code. Caller is not required to hold any
4221 * locks.
4222 **/
4223int
4224lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4225{
4226 int rc;
4227 LPFC_MBOXQ_t *mboxq;
4228 struct lpfc_mqe *mqe;
4229 uint8_t *vpd;
4230 uint32_t vpd_size;
4231 uint32_t ftr_rsp = 0;
4232 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4233 struct lpfc_vport *vport = phba->pport;
4234 struct lpfc_dmabuf *mp;
4235
4236 /* Perform a PCI function reset to start from clean */
4237 rc = lpfc_pci_function_reset(phba);
4238 if (unlikely(rc))
4239 return -ENODEV;
4240
4241 /* Check the HBA Host Status Register for readyness */
4242 rc = lpfc_sli4_post_status_check(phba);
4243 if (unlikely(rc))
4244 return -ENODEV;
4245 else {
4246 spin_lock_irq(&phba->hbalock);
4247 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4248 spin_unlock_irq(&phba->hbalock);
4249 }
4250
4251 /*
4252 * Allocate a single mailbox container for initializing the
4253 * port.
4254 */
4255 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4256 if (!mboxq)
4257 return -ENOMEM;
4258
4259 /*
4260 * Continue initialization with default values even if driver failed
4261 * to read FCoE param config regions
4262 */
4263 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4264 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004265 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004266
4267 /* Issue READ_REV to collect vpd and FW information. */
4268 vpd_size = PAGE_SIZE;
4269 vpd = kzalloc(vpd_size, GFP_KERNEL);
4270 if (!vpd) {
4271 rc = -ENOMEM;
4272 goto out_free_mbox;
4273 }
4274
4275 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4276 if (unlikely(rc))
4277 goto out_free_vpd;
4278
4279 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004280 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4281 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4282 phba->hba_flag |= HBA_FCOE_SUPPORT;
James Smart45ed1192009-10-02 15:17:02 -04004283
4284 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4285 LPFC_DCBX_CEE_MODE)
4286 phba->hba_flag |= HBA_FIP_SUPPORT;
4287 else
4288 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4289
James Smartf1126682009-06-10 17:22:44 -04004290 if (phba->sli_rev != LPFC_SLI_REV4 ||
4291 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4293 "0376 READ_REV Error. SLI Level %d "
4294 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004295 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004296 rc = -EIO;
4297 goto out_free_vpd;
4298 }
James Smartda0436e2009-05-22 14:51:39 -04004299 /*
4300 * Evaluate the read rev and vpd data. Populate the driver
4301 * state with the results. If this routine fails, the failure
4302 * is not fatal as the driver will use generic values.
4303 */
4304 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4305 if (unlikely(!rc)) {
4306 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4307 "0377 Error %d parsing vpd. "
4308 "Using defaults.\n", rc);
4309 rc = 0;
4310 }
4311
James Smartf1126682009-06-10 17:22:44 -04004312 /* Save information as VPD data */
4313 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4314 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4315 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4316 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4317 &mqe->un.read_rev);
4318 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4319 &mqe->un.read_rev);
4320 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4321 &mqe->un.read_rev);
4322 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4323 &mqe->un.read_rev);
4324 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4325 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4326 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4327 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4328 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4329 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4330 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4331 "(%d):0380 READ_REV Status x%x "
4332 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4333 mboxq->vport ? mboxq->vport->vpi : 0,
4334 bf_get(lpfc_mqe_status, mqe),
4335 phba->vpd.rev.opFwName,
4336 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4337 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004338
4339 /*
4340 * Discover the port's supported feature set and match it against the
4341 * hosts requests.
4342 */
4343 lpfc_request_features(phba, mboxq);
4344 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4345 if (unlikely(rc)) {
4346 rc = -EIO;
4347 goto out_free_vpd;
4348 }
4349
4350 /*
4351 * The port must support FCP initiator mode as this is the
4352 * only mode running in the host.
4353 */
4354 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4355 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4356 "0378 No support for fcpi mode.\n");
4357 ftr_rsp++;
4358 }
4359
4360 /*
4361 * If the port cannot support the host's requested features
4362 * then turn off the global config parameters to disable the
4363 * feature in the driver. This is not a fatal error.
4364 */
4365 if ((phba->cfg_enable_bg) &&
4366 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4367 ftr_rsp++;
4368
4369 if (phba->max_vpi && phba->cfg_enable_npiv &&
4370 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4371 ftr_rsp++;
4372
4373 if (ftr_rsp) {
4374 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4375 "0379 Feature Mismatch Data: x%08x %08x "
4376 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4377 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4378 phba->cfg_enable_npiv, phba->max_vpi);
4379 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4380 phba->cfg_enable_bg = 0;
4381 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4382 phba->cfg_enable_npiv = 0;
4383 }
4384
4385 /* These SLI3 features are assumed in SLI4 */
4386 spin_lock_irq(&phba->hbalock);
4387 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4388 spin_unlock_irq(&phba->hbalock);
4389
4390 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05004391 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4392 if (rc) {
4393 phba->link_state = LPFC_HBA_ERROR;
4394 rc = -ENOMEM;
4395 goto out_free_vpd;
4396 }
4397
James Smartda0436e2009-05-22 14:51:39 -04004398 mboxq->vport = vport;
4399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4400 mp = (struct lpfc_dmabuf *) mboxq->context1;
4401 if (rc == MBX_SUCCESS) {
4402 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4403 rc = 0;
4404 }
4405
4406 /*
4407 * This memory was allocated by the lpfc_read_sparam routine. Release
4408 * it to the mbuf pool.
4409 */
4410 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4411 kfree(mp);
4412 mboxq->context1 = NULL;
4413 if (unlikely(rc)) {
4414 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4415 "0382 READ_SPARAM command failed "
4416 "status %d, mbxStatus x%x\n",
4417 rc, bf_get(lpfc_mqe_status, mqe));
4418 phba->link_state = LPFC_HBA_ERROR;
4419 rc = -EIO;
4420 goto out_free_vpd;
4421 }
4422
4423 if (phba->cfg_soft_wwnn)
4424 u64_to_wwn(phba->cfg_soft_wwnn,
4425 vport->fc_sparam.nodeName.u.wwn);
4426 if (phba->cfg_soft_wwpn)
4427 u64_to_wwn(phba->cfg_soft_wwpn,
4428 vport->fc_sparam.portName.u.wwn);
4429 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4430 sizeof(struct lpfc_name));
4431 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4432 sizeof(struct lpfc_name));
4433
4434 /* Update the fc_host data structures with new wwn. */
4435 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4436 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4437
4438 /* Register SGL pool to the device using non-embedded mailbox command */
4439 rc = lpfc_sli4_post_sgl_list(phba);
4440 if (unlikely(rc)) {
4441 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004442 "0582 Error %d during sgl post operation\n",
4443 rc);
James Smartda0436e2009-05-22 14:51:39 -04004444 rc = -ENODEV;
4445 goto out_free_vpd;
4446 }
4447
4448 /* Register SCSI SGL pool to the device */
4449 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4450 if (unlikely(rc)) {
4451 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004452 "0383 Error %d during scsi sgl post "
4453 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004454 /* Some Scsi buffers were moved to the abort scsi list */
4455 /* A pci function reset will repost them */
4456 rc = -ENODEV;
4457 goto out_free_vpd;
4458 }
4459
4460 /* Post the rpi header region to the device. */
4461 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4462 if (unlikely(rc)) {
4463 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4464 "0393 Error %d during rpi post operation\n",
4465 rc);
4466 rc = -ENODEV;
4467 goto out_free_vpd;
4468 }
James Smartda0436e2009-05-22 14:51:39 -04004469
4470 /* Set up all the queues to the device */
4471 rc = lpfc_sli4_queue_setup(phba);
4472 if (unlikely(rc)) {
4473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4474 "0381 Error %d during queue setup.\n ", rc);
4475 goto out_stop_timers;
4476 }
4477
4478 /* Arm the CQs and then EQs on device */
4479 lpfc_sli4_arm_cqeq_intr(phba);
4480
4481 /* Indicate device interrupt mode */
4482 phba->sli4_hba.intr_enable = 1;
4483
4484 /* Allow asynchronous mailbox command to go through */
4485 spin_lock_irq(&phba->hbalock);
4486 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4487 spin_unlock_irq(&phba->hbalock);
4488
4489 /* Post receive buffers to the device */
4490 lpfc_sli4_rb_setup(phba);
4491
4492 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004493 mod_timer(&vport->els_tmofunc,
4494 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004495
4496 /* Start heart beat timer */
4497 mod_timer(&phba->hb_tmofunc,
4498 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4499 phba->hb_outstanding = 0;
4500 phba->last_completion_time = jiffies;
4501
4502 /* Start error attention (ERATT) polling timer */
4503 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4504
4505 /*
4506 * The port is ready, set the host's link state to LINK_DOWN
4507 * in preparation for link interrupts.
4508 */
4509 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4510 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4511 lpfc_set_loopback_flag(phba);
4512 /* Change driver state to LPFC_LINK_DOWN right before init link */
4513 spin_lock_irq(&phba->hbalock);
4514 phba->link_state = LPFC_LINK_DOWN;
4515 spin_unlock_irq(&phba->hbalock);
4516 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4517 if (unlikely(rc != MBX_NOT_FINISHED)) {
4518 kfree(vpd);
4519 return 0;
4520 } else
4521 rc = -EIO;
4522
4523 /* Unset all the queues set up in this routine when error out */
4524 if (rc)
4525 lpfc_sli4_queue_unset(phba);
4526
4527out_stop_timers:
4528 if (rc)
4529 lpfc_stop_hba_timers(phba);
4530out_free_vpd:
4531 kfree(vpd);
4532out_free_mbox:
4533 mempool_free(mboxq, phba->mbox_mem_pool);
4534 return rc;
4535}
James Smarte59058c2008-08-24 21:49:00 -04004536
4537/**
James Smart3621a712009-04-06 18:47:14 -04004538 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004539 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004540 *
James Smarte59058c2008-08-24 21:49:00 -04004541 * This is the callback function for mailbox timer. The mailbox
4542 * timer is armed when a new mailbox command is issued and the timer
4543 * is deleted when the mailbox complete. The function is called by
4544 * the kernel timer code when a mailbox does not complete within
4545 * expected time. This function wakes up the worker thread to
4546 * process the mailbox timeout and returns. All the processing is
4547 * done by the worker thread function lpfc_mbox_timeout_handler.
4548 **/
dea31012005-04-17 16:05:31 -05004549void
4550lpfc_mbox_timeout(unsigned long ptr)
4551{
James Smart92d7f7b2007-06-17 19:56:38 -05004552 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004553 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004554 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004555
James Smart2e0fef82007-06-17 19:56:36 -05004556 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004557 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004558 if (!tmo_posted)
4559 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4560 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4561
James Smart5e9d9b82008-06-14 22:52:53 -04004562 if (!tmo_posted)
4563 lpfc_worker_wake_up(phba);
4564 return;
dea31012005-04-17 16:05:31 -05004565}
4566
James Smarte59058c2008-08-24 21:49:00 -04004567
4568/**
James Smart3621a712009-04-06 18:47:14 -04004569 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004570 * @phba: Pointer to HBA context object.
4571 *
4572 * This function is called from worker thread when a mailbox command times out.
4573 * The caller is not required to hold any locks. This function will reset the
4574 * HBA and recover all the pending commands.
4575 **/
dea31012005-04-17 16:05:31 -05004576void
4577lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4578{
James Smart2e0fef82007-06-17 19:56:36 -05004579 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004580 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004581 struct lpfc_sli *psli = &phba->sli;
4582 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004583
James Smarta257bf92009-04-06 18:48:10 -04004584 /* Check the pmbox pointer first. There is a race condition
4585 * between the mbox timeout handler getting executed in the
4586 * worklist and the mailbox actually completing. When this
4587 * race condition occurs, the mbox_active will be NULL.
4588 */
4589 spin_lock_irq(&phba->hbalock);
4590 if (pmbox == NULL) {
4591 lpfc_printf_log(phba, KERN_WARNING,
4592 LOG_MBOX | LOG_SLI,
4593 "0353 Active Mailbox cleared - mailbox timeout "
4594 "exiting\n");
4595 spin_unlock_irq(&phba->hbalock);
4596 return;
4597 }
4598
dea31012005-04-17 16:05:31 -05004599 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004600 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004601 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004602 mb->mbxCommand,
4603 phba->pport->port_state,
4604 phba->sli.sli_flag,
4605 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004606 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004607
James Smart1dcb58e2007-04-25 09:51:30 -04004608 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4609 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4610 * it to fail all oustanding SCSI IO.
4611 */
James Smart2e0fef82007-06-17 19:56:36 -05004612 spin_lock_irq(&phba->pport->work_port_lock);
4613 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4614 spin_unlock_irq(&phba->pport->work_port_lock);
4615 spin_lock_irq(&phba->hbalock);
4616 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004617 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004618 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004619
4620 pring = &psli->ring[psli->fcp_ring];
4621 lpfc_sli_abort_iocb_ring(phba, pring);
4622
4623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004624 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004625
4626 /* Reset the HBA device */
4627 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004628}
4629
James Smarte59058c2008-08-24 21:49:00 -04004630/**
James Smart3772a992009-05-22 14:50:54 -04004631 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004632 * @phba: Pointer to HBA context object.
4633 * @pmbox: Pointer to mailbox object.
4634 * @flag: Flag indicating how the mailbox need to be processed.
4635 *
4636 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004637 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4638 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004639 * The mailbox command can be submitted in polling mode, in which case
4640 * this function will wait in a polling loop for the completion of the
4641 * mailbox.
4642 * If the mailbox is submitted in no_wait mode (not polling) the
4643 * function will submit the command and returns immediately without waiting
4644 * for the mailbox completion. The no_wait is supported only when HBA
4645 * is in SLI2/SLI3 mode - interrupts are enabled.
4646 * The SLI interface allows only one mailbox pending at a time. If the
4647 * mailbox is issued in polling mode and there is already a mailbox
4648 * pending, then the function will return an error. If the mailbox is issued
4649 * in NO_WAIT mode and there is a mailbox pending already, the function
4650 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4651 * The sli layer owns the mailbox object until the completion of mailbox
4652 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4653 * return codes the caller owns the mailbox command after the return of
4654 * the function.
4655 **/
James Smart3772a992009-05-22 14:50:54 -04004656static int
4657lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4658 uint32_t flag)
dea31012005-04-17 16:05:31 -05004659{
dea31012005-04-17 16:05:31 -05004660 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004661 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004662 uint32_t status, evtctr;
4663 uint32_t ha_copy;
4664 int i;
James Smart09372822008-01-11 01:52:54 -05004665 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004666 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004667 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004668 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004669 int processing_queue = 0;
4670
4671 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4672 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004673 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004674 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004675 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4676 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4677 return MBX_SUCCESS;
4678 }
James Smart58da1ff2008-04-07 10:15:56 -04004679 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004680 pmbox = lpfc_mbox_get(phba);
4681 if (!pmbox) {
4682 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4683 return MBX_SUCCESS;
4684 }
4685 }
dea31012005-04-17 16:05:31 -05004686
James Smarted957682007-06-17 19:56:37 -05004687 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004688 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004689 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004691 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004692 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004693 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004694 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004695 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004696 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004697 }
4698 }
4699
Linas Vepstas8d63f372007-02-14 14:28:36 -06004700 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004701 if (unlikely(pci_channel_offline(phba->pcidev))) {
4702 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4703 goto out_not_finished;
4704 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004705
James Smarta257bf92009-04-06 18:48:10 -04004706 /* If HBA has a deferred error attention, fail the iocb. */
4707 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4709 goto out_not_finished;
4710 }
4711
dea31012005-04-17 16:05:31 -05004712 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004713
James Smart3772a992009-05-22 14:50:54 -04004714 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004715 status = MBX_SUCCESS;
4716
James Smart2e0fef82007-06-17 19:56:36 -05004717 if (phba->link_state == LPFC_HBA_ERROR) {
4718 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004719
4720 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004721 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4722 "(%d):0311 Mailbox command x%x cannot "
4723 "issue Data: x%x x%x\n",
4724 pmbox->vport ? pmbox->vport->vpi : 0,
4725 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004726 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004727 }
4728
James Smart92908312006-03-07 15:04:13 -05004729 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4730 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004732 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4733 "(%d):2528 Mailbox command x%x cannot "
4734 "issue Data: x%x x%x\n",
4735 pmbox->vport ? pmbox->vport->vpi : 0,
4736 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004737 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004738 }
4739
dea31012005-04-17 16:05:31 -05004740 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4741 /* Polling for a mbox command when another one is already active
4742 * is not allowed in SLI. Also, the driver must have established
4743 * SLI2 mode to queue and process multiple mbox commands.
4744 */
4745
4746 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004747 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004748
4749 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004750 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4751 "(%d):2529 Mailbox command x%x "
4752 "cannot issue Data: x%x x%x\n",
4753 pmbox->vport ? pmbox->vport->vpi : 0,
4754 pmbox->u.mb.mbxCommand,
4755 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004756 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004757 }
4758
James Smart3772a992009-05-22 14:50:54 -04004759 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004760 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004761 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4763 "(%d):2530 Mailbox command x%x "
4764 "cannot issue Data: x%x x%x\n",
4765 pmbox->vport ? pmbox->vport->vpi : 0,
4766 pmbox->u.mb.mbxCommand,
4767 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004768 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004769 }
4770
dea31012005-04-17 16:05:31 -05004771 /* Another mailbox command is still being processed, queue this
4772 * command to be processed later.
4773 */
4774 lpfc_mbox_put(phba, pmbox);
4775
4776 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004777 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004778 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004779 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004780 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4781 mb->mbxCommand, phba->pport->port_state,
4782 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004783
4784 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004785 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004786
James Smart858c9f62007-06-17 19:56:39 -05004787 if (pmbox->vport) {
4788 lpfc_debugfs_disc_trc(pmbox->vport,
4789 LPFC_DISC_TRC_MBOX_VPORT,
4790 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4791 (uint32_t)mb->mbxCommand,
4792 mb->un.varWords[0], mb->un.varWords[1]);
4793 }
4794 else {
4795 lpfc_debugfs_disc_trc(phba->pport,
4796 LPFC_DISC_TRC_MBOX,
4797 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4798 (uint32_t)mb->mbxCommand,
4799 mb->un.varWords[0], mb->un.varWords[1]);
4800 }
4801
James Smart2e0fef82007-06-17 19:56:36 -05004802 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004803 }
4804
dea31012005-04-17 16:05:31 -05004805 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4806
4807 /* If we are not polling, we MUST be in SLI2 mode */
4808 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004809 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004810 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004811 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004813 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4815 "(%d):2531 Mailbox command x%x "
4816 "cannot issue Data: x%x x%x\n",
4817 pmbox->vport ? pmbox->vport->vpi : 0,
4818 pmbox->u.mb.mbxCommand,
4819 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004820 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004821 }
4822 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004823 mod_timer(&psli->mbox_tmo, (jiffies +
4824 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004825 }
4826
4827 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004828 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004829 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004830 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004831 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004832 mb->mbxCommand, phba->pport->port_state,
4833 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004834
James Smart858c9f62007-06-17 19:56:39 -05004835 if (mb->mbxCommand != MBX_HEARTBEAT) {
4836 if (pmbox->vport) {
4837 lpfc_debugfs_disc_trc(pmbox->vport,
4838 LPFC_DISC_TRC_MBOX_VPORT,
4839 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4840 (uint32_t)mb->mbxCommand,
4841 mb->un.varWords[0], mb->un.varWords[1]);
4842 }
4843 else {
4844 lpfc_debugfs_disc_trc(phba->pport,
4845 LPFC_DISC_TRC_MBOX,
4846 "MBOX Send: cmd:x%x mb:x%x x%x",
4847 (uint32_t)mb->mbxCommand,
4848 mb->un.varWords[0], mb->un.varWords[1]);
4849 }
4850 }
4851
dea31012005-04-17 16:05:31 -05004852 psli->slistat.mbox_cmd++;
4853 evtctr = psli->slistat.mbox_event;
4854
4855 /* next set own bit for the adapter and copy over command word */
4856 mb->mbxOwner = OWN_CHIP;
4857
James Smart3772a992009-05-22 14:50:54 -04004858 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004859 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004860 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004861 } else {
James Smart92908312006-03-07 15:04:13 -05004862 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004863 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004864 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004865 }
4866
4867 /* First copy mbox command data to HBA SLIM, skip past first
4868 word */
4869 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4870 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4871 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4872
4873 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004874 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004875 to_slim = phba->MBslimaddr;
4876 writel(ldata, to_slim);
4877 readl(to_slim); /* flush */
4878
4879 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4880 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004881 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004882 }
4883 }
4884
4885 wmb();
dea31012005-04-17 16:05:31 -05004886
4887 switch (flag) {
4888 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004889 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004890 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004891 /* Interrupt board to do it */
4892 writel(CA_MBATT, phba->CAregaddr);
4893 readl(phba->CAregaddr); /* flush */
4894 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004895 break;
4896
4897 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004898 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004899 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004900 /* Interrupt board to do it */
4901 writel(CA_MBATT, phba->CAregaddr);
4902 readl(phba->CAregaddr); /* flush */
4903
James Smart3772a992009-05-22 14:50:54 -04004904 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004905 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004906 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004907 word0 = le32_to_cpu(word0);
4908 } else {
4909 /* First read mbox status word */
4910 word0 = readl(phba->MBslimaddr);
4911 }
4912
4913 /* Read the HBA Host Attention Register */
4914 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004915 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4916 mb->mbxCommand) *
4917 1000) + jiffies;
4918 i = 0;
dea31012005-04-17 16:05:31 -05004919 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004920 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4921 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004922 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004923 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004924 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004925 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004926 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004927 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004928 }
4929
4930 /* Check if we took a mbox interrupt while we were
4931 polling */
4932 if (((word0 & OWN_CHIP) != OWN_CHIP)
4933 && (evtctr != psli->slistat.mbox_event))
4934 break;
4935
James Smart09372822008-01-11 01:52:54 -05004936 if (i++ > 10) {
4937 spin_unlock_irqrestore(&phba->hbalock,
4938 drvr_flag);
4939 msleep(1);
4940 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4941 }
dea31012005-04-17 16:05:31 -05004942
James Smart3772a992009-05-22 14:50:54 -04004943 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004944 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004945 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004946 word0 = le32_to_cpu(word0);
4947 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4948 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004949 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004950 /* Check real SLIM for any errors */
4951 slimword0 = readl(phba->MBslimaddr);
4952 slimmb = (MAILBOX_t *) & slimword0;
4953 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4954 && slimmb->mbxStatus) {
4955 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004956 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004957 word0 = slimword0;
4958 }
4959 }
4960 } else {
4961 /* First copy command data */
4962 word0 = readl(phba->MBslimaddr);
4963 }
4964 /* Read the HBA Host Attention Register */
4965 ha_copy = readl(phba->HAregaddr);
4966 }
4967
James Smart3772a992009-05-22 14:50:54 -04004968 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004969 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04004970 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004971 } else {
4972 /* First copy command data */
4973 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4974 MAILBOX_CMD_SIZE);
4975 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4976 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05004977 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05004978 phba->MBslimaddr + DMP_RSP_OFFSET,
4979 mb->un.varDmp.word_cnt);
4980 }
4981 }
4982
4983 writel(HA_MBATT, phba->HAregaddr);
4984 readl(phba->HAregaddr); /* flush */
4985
4986 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4987 status = mb->mbxStatus;
4988 }
4989
James Smart2e0fef82007-06-17 19:56:36 -05004990 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4991 return status;
James Smart58da1ff2008-04-07 10:15:56 -04004992
4993out_not_finished:
4994 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04004995 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04004996 lpfc_mbox_cmpl_put(phba, pmbox);
4997 }
4998 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05004999}
5000
James Smarte59058c2008-08-24 21:49:00 -04005001/**
James Smartf1126682009-06-10 17:22:44 -04005002 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5003 * @phba: Pointer to HBA context object.
5004 *
5005 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5006 * the driver internal pending mailbox queue. It will then try to wait out the
5007 * possible outstanding mailbox command before return.
5008 *
5009 * Returns:
5010 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5011 * the outstanding mailbox command timed out.
5012 **/
5013static int
5014lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5015{
5016 struct lpfc_sli *psli = &phba->sli;
5017 uint8_t actcmd = MBX_HEARTBEAT;
5018 int rc = 0;
5019 unsigned long timeout;
5020
5021 /* Mark the asynchronous mailbox command posting as blocked */
5022 spin_lock_irq(&phba->hbalock);
5023 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5024 if (phba->sli.mbox_active)
5025 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5026 spin_unlock_irq(&phba->hbalock);
5027 /* Determine how long we might wait for the active mailbox
5028 * command to be gracefully completed by firmware.
5029 */
5030 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5031 jiffies;
5032 /* Wait for the outstnading mailbox command to complete */
5033 while (phba->sli.mbox_active) {
5034 /* Check active mailbox complete status every 2ms */
5035 msleep(2);
5036 if (time_after(jiffies, timeout)) {
5037 /* Timeout, marked the outstanding cmd not complete */
5038 rc = 1;
5039 break;
5040 }
5041 }
5042
5043 /* Can not cleanly block async mailbox command, fails it */
5044 if (rc) {
5045 spin_lock_irq(&phba->hbalock);
5046 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5047 spin_unlock_irq(&phba->hbalock);
5048 }
5049 return rc;
5050}
5051
5052/**
5053 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5054 * @phba: Pointer to HBA context object.
5055 *
5056 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5057 * commands from the driver internal pending mailbox queue. It makes sure
5058 * that there is no outstanding mailbox command before resuming posting
5059 * asynchronous mailbox commands. If, for any reason, there is outstanding
5060 * mailbox command, it will try to wait it out before resuming asynchronous
5061 * mailbox command posting.
5062 **/
5063static void
5064lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5065{
5066 struct lpfc_sli *psli = &phba->sli;
5067
5068 spin_lock_irq(&phba->hbalock);
5069 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5070 /* Asynchronous mailbox posting is not blocked, do nothing */
5071 spin_unlock_irq(&phba->hbalock);
5072 return;
5073 }
5074
5075 /* Outstanding synchronous mailbox command is guaranteed to be done,
5076 * successful or timeout, after timing-out the outstanding mailbox
5077 * command shall always be removed, so just unblock posting async
5078 * mailbox command and resume
5079 */
5080 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5081 spin_unlock_irq(&phba->hbalock);
5082
5083 /* wake up worker thread to post asynchronlous mailbox command */
5084 lpfc_worker_wake_up(phba);
5085}
5086
5087/**
James Smartda0436e2009-05-22 14:51:39 -04005088 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5089 * @phba: Pointer to HBA context object.
5090 * @mboxq: Pointer to mailbox object.
5091 *
5092 * The function posts a mailbox to the port. The mailbox is expected
5093 * to be comletely filled in and ready for the port to operate on it.
5094 * This routine executes a synchronous completion operation on the
5095 * mailbox by polling for its completion.
5096 *
5097 * The caller must not be holding any locks when calling this routine.
5098 *
5099 * Returns:
5100 * MBX_SUCCESS - mailbox posted successfully
5101 * Any of the MBX error values.
5102 **/
5103static int
5104lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5105{
5106 int rc = MBX_SUCCESS;
5107 unsigned long iflag;
5108 uint32_t db_ready;
5109 uint32_t mcqe_status;
5110 uint32_t mbx_cmnd;
5111 unsigned long timeout;
5112 struct lpfc_sli *psli = &phba->sli;
5113 struct lpfc_mqe *mb = &mboxq->u.mqe;
5114 struct lpfc_bmbx_create *mbox_rgn;
5115 struct dma_address *dma_address;
5116 struct lpfc_register bmbx_reg;
5117
5118 /*
5119 * Only one mailbox can be active to the bootstrap mailbox region
5120 * at a time and there is no queueing provided.
5121 */
5122 spin_lock_irqsave(&phba->hbalock, iflag);
5123 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5124 spin_unlock_irqrestore(&phba->hbalock, iflag);
5125 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5126 "(%d):2532 Mailbox command x%x (x%x) "
5127 "cannot issue Data: x%x x%x\n",
5128 mboxq->vport ? mboxq->vport->vpi : 0,
5129 mboxq->u.mb.mbxCommand,
5130 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5131 psli->sli_flag, MBX_POLL);
5132 return MBXERR_ERROR;
5133 }
5134 /* The server grabs the token and owns it until release */
5135 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5136 phba->sli.mbox_active = mboxq;
5137 spin_unlock_irqrestore(&phba->hbalock, iflag);
5138
5139 /*
5140 * Initialize the bootstrap memory region to avoid stale data areas
5141 * in the mailbox post. Then copy the caller's mailbox contents to
5142 * the bmbx mailbox region.
5143 */
5144 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5145 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5146 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5147 sizeof(struct lpfc_mqe));
5148
5149 /* Post the high mailbox dma address to the port and wait for ready. */
5150 dma_address = &phba->sli4_hba.bmbx.dma_address;
5151 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5152
5153 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5154 * 1000) + jiffies;
5155 do {
5156 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5157 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5158 if (!db_ready)
5159 msleep(2);
5160
5161 if (time_after(jiffies, timeout)) {
5162 rc = MBXERR_ERROR;
5163 goto exit;
5164 }
5165 } while (!db_ready);
5166
5167 /* Post the low mailbox dma address to the port. */
5168 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5169 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5170 * 1000) + jiffies;
5171 do {
5172 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5173 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5174 if (!db_ready)
5175 msleep(2);
5176
5177 if (time_after(jiffies, timeout)) {
5178 rc = MBXERR_ERROR;
5179 goto exit;
5180 }
5181 } while (!db_ready);
5182
5183 /*
5184 * Read the CQ to ensure the mailbox has completed.
5185 * If so, update the mailbox status so that the upper layers
5186 * can complete the request normally.
5187 */
5188 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5189 sizeof(struct lpfc_mqe));
5190 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5191 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5192 sizeof(struct lpfc_mcqe));
5193 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5194
5195 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5196 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5197 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5198 rc = MBXERR_ERROR;
5199 }
5200
5201 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5202 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5203 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5204 " x%x x%x CQ: x%x x%x x%x x%x\n",
5205 mboxq->vport ? mboxq->vport->vpi : 0,
5206 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5207 bf_get(lpfc_mqe_status, mb),
5208 mb->un.mb_words[0], mb->un.mb_words[1],
5209 mb->un.mb_words[2], mb->un.mb_words[3],
5210 mb->un.mb_words[4], mb->un.mb_words[5],
5211 mb->un.mb_words[6], mb->un.mb_words[7],
5212 mb->un.mb_words[8], mb->un.mb_words[9],
5213 mb->un.mb_words[10], mb->un.mb_words[11],
5214 mb->un.mb_words[12], mboxq->mcqe.word0,
5215 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5216 mboxq->mcqe.trailer);
5217exit:
5218 /* We are holding the token, no needed for lock when release */
5219 spin_lock_irqsave(&phba->hbalock, iflag);
5220 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5221 phba->sli.mbox_active = NULL;
5222 spin_unlock_irqrestore(&phba->hbalock, iflag);
5223 return rc;
5224}
5225
5226/**
5227 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5228 * @phba: Pointer to HBA context object.
5229 * @pmbox: Pointer to mailbox object.
5230 * @flag: Flag indicating how the mailbox need to be processed.
5231 *
5232 * This function is called by discovery code and HBA management code to submit
5233 * a mailbox command to firmware with SLI-4 interface spec.
5234 *
5235 * Return codes the caller owns the mailbox command after the return of the
5236 * function.
5237 **/
5238static int
5239lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5240 uint32_t flag)
5241{
5242 struct lpfc_sli *psli = &phba->sli;
5243 unsigned long iflags;
5244 int rc;
5245
James Smart8fa38512009-07-19 10:01:03 -04005246 rc = lpfc_mbox_dev_check(phba);
5247 if (unlikely(rc)) {
5248 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5249 "(%d):2544 Mailbox command x%x (x%x) "
5250 "cannot issue Data: x%x x%x\n",
5251 mboxq->vport ? mboxq->vport->vpi : 0,
5252 mboxq->u.mb.mbxCommand,
5253 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5254 psli->sli_flag, flag);
5255 goto out_not_finished;
5256 }
5257
James Smartda0436e2009-05-22 14:51:39 -04005258 /* Detect polling mode and jump to a handler */
5259 if (!phba->sli4_hba.intr_enable) {
5260 if (flag == MBX_POLL)
5261 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5262 else
5263 rc = -EIO;
5264 if (rc != MBX_SUCCESS)
5265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5266 "(%d):2541 Mailbox command x%x "
5267 "(x%x) cannot issue Data: x%x x%x\n",
5268 mboxq->vport ? mboxq->vport->vpi : 0,
5269 mboxq->u.mb.mbxCommand,
5270 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5271 psli->sli_flag, flag);
5272 return rc;
5273 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005274 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5275 "(%d):2542 Try to issue mailbox command "
5276 "x%x (x%x) synchronously ahead of async"
5277 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005278 mboxq->vport ? mboxq->vport->vpi : 0,
5279 mboxq->u.mb.mbxCommand,
5280 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5281 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005282 /* Try to block the asynchronous mailbox posting */
5283 rc = lpfc_sli4_async_mbox_block(phba);
5284 if (!rc) {
5285 /* Successfully blocked, now issue sync mbox cmd */
5286 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5287 if (rc != MBX_SUCCESS)
5288 lpfc_printf_log(phba, KERN_ERR,
5289 LOG_MBOX | LOG_SLI,
5290 "(%d):2597 Mailbox command "
5291 "x%x (x%x) cannot issue "
5292 "Data: x%x x%x\n",
5293 mboxq->vport ?
5294 mboxq->vport->vpi : 0,
5295 mboxq->u.mb.mbxCommand,
5296 lpfc_sli4_mbox_opcode_get(phba,
5297 mboxq),
5298 psli->sli_flag, flag);
5299 /* Unblock the async mailbox posting afterward */
5300 lpfc_sli4_async_mbox_unblock(phba);
5301 }
5302 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005303 }
5304
5305 /* Now, interrupt mode asynchrous mailbox command */
5306 rc = lpfc_mbox_cmd_check(phba, mboxq);
5307 if (rc) {
5308 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5309 "(%d):2543 Mailbox command x%x (x%x) "
5310 "cannot issue Data: x%x x%x\n",
5311 mboxq->vport ? mboxq->vport->vpi : 0,
5312 mboxq->u.mb.mbxCommand,
5313 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5314 psli->sli_flag, flag);
5315 goto out_not_finished;
5316 }
James Smartda0436e2009-05-22 14:51:39 -04005317
5318 /* Put the mailbox command to the driver internal FIFO */
5319 psli->slistat.mbox_busy++;
5320 spin_lock_irqsave(&phba->hbalock, iflags);
5321 lpfc_mbox_put(phba, mboxq);
5322 spin_unlock_irqrestore(&phba->hbalock, iflags);
5323 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5324 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5325 "x%x (x%x) x%x x%x x%x\n",
5326 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5327 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5328 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5329 phba->pport->port_state,
5330 psli->sli_flag, MBX_NOWAIT);
5331 /* Wake up worker thread to transport mailbox command from head */
5332 lpfc_worker_wake_up(phba);
5333
5334 return MBX_BUSY;
5335
5336out_not_finished:
5337 return MBX_NOT_FINISHED;
5338}
5339
5340/**
5341 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5342 * @phba: Pointer to HBA context object.
5343 *
5344 * This function is called by worker thread to send a mailbox command to
5345 * SLI4 HBA firmware.
5346 *
5347 **/
5348int
5349lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5350{
5351 struct lpfc_sli *psli = &phba->sli;
5352 LPFC_MBOXQ_t *mboxq;
5353 int rc = MBX_SUCCESS;
5354 unsigned long iflags;
5355 struct lpfc_mqe *mqe;
5356 uint32_t mbx_cmnd;
5357
5358 /* Check interrupt mode before post async mailbox command */
5359 if (unlikely(!phba->sli4_hba.intr_enable))
5360 return MBX_NOT_FINISHED;
5361
5362 /* Check for mailbox command service token */
5363 spin_lock_irqsave(&phba->hbalock, iflags);
5364 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5365 spin_unlock_irqrestore(&phba->hbalock, iflags);
5366 return MBX_NOT_FINISHED;
5367 }
5368 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5369 spin_unlock_irqrestore(&phba->hbalock, iflags);
5370 return MBX_NOT_FINISHED;
5371 }
5372 if (unlikely(phba->sli.mbox_active)) {
5373 spin_unlock_irqrestore(&phba->hbalock, iflags);
5374 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5375 "0384 There is pending active mailbox cmd\n");
5376 return MBX_NOT_FINISHED;
5377 }
5378 /* Take the mailbox command service token */
5379 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5380
5381 /* Get the next mailbox command from head of queue */
5382 mboxq = lpfc_mbox_get(phba);
5383
5384 /* If no more mailbox command waiting for post, we're done */
5385 if (!mboxq) {
5386 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5387 spin_unlock_irqrestore(&phba->hbalock, iflags);
5388 return MBX_SUCCESS;
5389 }
5390 phba->sli.mbox_active = mboxq;
5391 spin_unlock_irqrestore(&phba->hbalock, iflags);
5392
5393 /* Check device readiness for posting mailbox command */
5394 rc = lpfc_mbox_dev_check(phba);
5395 if (unlikely(rc))
5396 /* Driver clean routine will clean up pending mailbox */
5397 goto out_not_finished;
5398
5399 /* Prepare the mbox command to be posted */
5400 mqe = &mboxq->u.mqe;
5401 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5402
5403 /* Start timer for the mbox_tmo and log some mailbox post messages */
5404 mod_timer(&psli->mbox_tmo, (jiffies +
5405 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5406
5407 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5408 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5409 "x%x x%x\n",
5410 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5411 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5412 phba->pport->port_state, psli->sli_flag);
5413
5414 if (mbx_cmnd != MBX_HEARTBEAT) {
5415 if (mboxq->vport) {
5416 lpfc_debugfs_disc_trc(mboxq->vport,
5417 LPFC_DISC_TRC_MBOX_VPORT,
5418 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5419 mbx_cmnd, mqe->un.mb_words[0],
5420 mqe->un.mb_words[1]);
5421 } else {
5422 lpfc_debugfs_disc_trc(phba->pport,
5423 LPFC_DISC_TRC_MBOX,
5424 "MBOX Send: cmd:x%x mb:x%x x%x",
5425 mbx_cmnd, mqe->un.mb_words[0],
5426 mqe->un.mb_words[1]);
5427 }
5428 }
5429 psli->slistat.mbox_cmd++;
5430
5431 /* Post the mailbox command to the port */
5432 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5433 if (rc != MBX_SUCCESS) {
5434 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5435 "(%d):2533 Mailbox command x%x (x%x) "
5436 "cannot issue Data: x%x x%x\n",
5437 mboxq->vport ? mboxq->vport->vpi : 0,
5438 mboxq->u.mb.mbxCommand,
5439 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5440 psli->sli_flag, MBX_NOWAIT);
5441 goto out_not_finished;
5442 }
5443
5444 return rc;
5445
5446out_not_finished:
5447 spin_lock_irqsave(&phba->hbalock, iflags);
5448 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5449 __lpfc_mbox_cmpl_put(phba, mboxq);
5450 /* Release the token */
5451 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5452 phba->sli.mbox_active = NULL;
5453 spin_unlock_irqrestore(&phba->hbalock, iflags);
5454
5455 return MBX_NOT_FINISHED;
5456}
5457
5458/**
5459 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5460 * @phba: Pointer to HBA context object.
5461 * @pmbox: Pointer to mailbox object.
5462 * @flag: Flag indicating how the mailbox need to be processed.
5463 *
5464 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5465 * the API jump table function pointer from the lpfc_hba struct.
5466 *
5467 * Return codes the caller owns the mailbox command after the return of the
5468 * function.
5469 **/
5470int
5471lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5472{
5473 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5474}
5475
5476/**
5477 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5478 * @phba: The hba struct for which this call is being executed.
5479 * @dev_grp: The HBA PCI-Device group number.
5480 *
5481 * This routine sets up the mbox interface API function jump table in @phba
5482 * struct.
5483 * Returns: 0 - success, -ENODEV - failure.
5484 **/
5485int
5486lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5487{
5488
5489 switch (dev_grp) {
5490 case LPFC_PCI_DEV_LP:
5491 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5492 phba->lpfc_sli_handle_slow_ring_event =
5493 lpfc_sli_handle_slow_ring_event_s3;
5494 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5495 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5496 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5497 break;
5498 case LPFC_PCI_DEV_OC:
5499 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5500 phba->lpfc_sli_handle_slow_ring_event =
5501 lpfc_sli_handle_slow_ring_event_s4;
5502 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5503 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5504 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5505 break;
5506 default:
5507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5508 "1420 Invalid HBA PCI-device group: 0x%x\n",
5509 dev_grp);
5510 return -ENODEV;
5511 break;
5512 }
5513 return 0;
5514}
5515
5516/**
James Smart3621a712009-04-06 18:47:14 -04005517 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005518 * @phba: Pointer to HBA context object.
5519 * @pring: Pointer to driver SLI ring object.
5520 * @piocb: Pointer to address of newly added command iocb.
5521 *
5522 * This function is called with hbalock held to add a command
5523 * iocb to the txq when SLI layer cannot submit the command iocb
5524 * to the ring.
5525 **/
James Smart858c9f62007-06-17 19:56:39 -05005526static void
James Smart92d7f7b2007-06-17 19:56:38 -05005527__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005528 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005529{
5530 /* Insert the caller's iocb in the txq tail for later processing. */
5531 list_add_tail(&piocb->list, &pring->txq);
5532 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005533}
5534
James Smarte59058c2008-08-24 21:49:00 -04005535/**
James Smart3621a712009-04-06 18:47:14 -04005536 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005537 * @phba: Pointer to HBA context object.
5538 * @pring: Pointer to driver SLI ring object.
5539 * @piocb: Pointer to address of newly added command iocb.
5540 *
5541 * This function is called with hbalock held before a new
5542 * iocb is submitted to the firmware. This function checks
5543 * txq to flush the iocbs in txq to Firmware before
5544 * submitting new iocbs to the Firmware.
5545 * If there are iocbs in the txq which need to be submitted
5546 * to firmware, lpfc_sli_next_iocb returns the first element
5547 * of the txq after dequeuing it from txq.
5548 * If there is no iocb in the txq then the function will return
5549 * *piocb and *piocb is set to NULL. Caller needs to check
5550 * *piocb to find if there are more commands in the txq.
5551 **/
dea31012005-04-17 16:05:31 -05005552static struct lpfc_iocbq *
5553lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005554 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005555{
5556 struct lpfc_iocbq * nextiocb;
5557
5558 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5559 if (!nextiocb) {
5560 nextiocb = *piocb;
5561 *piocb = NULL;
5562 }
5563
5564 return nextiocb;
5565}
5566
James Smarte59058c2008-08-24 21:49:00 -04005567/**
James Smart3772a992009-05-22 14:50:54 -04005568 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005569 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005570 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005571 * @piocb: Pointer to command iocb.
5572 * @flag: Flag indicating if this command can be put into txq.
5573 *
James Smart3772a992009-05-22 14:50:54 -04005574 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5575 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5576 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5577 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5578 * this function allows only iocbs for posting buffers. This function finds
5579 * next available slot in the command ring and posts the command to the
5580 * available slot and writes the port attention register to request HBA start
5581 * processing new iocb. If there is no slot available in the ring and
5582 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5583 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005584 *
James Smart3772a992009-05-22 14:50:54 -04005585 * This function is called with hbalock held. The function will return success
5586 * after it successfully submit the iocb to firmware or after adding to the
5587 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005588 **/
James Smart98c9ea52007-10-27 13:37:33 -04005589static int
James Smart3772a992009-05-22 14:50:54 -04005590__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005591 struct lpfc_iocbq *piocb, uint32_t flag)
5592{
5593 struct lpfc_iocbq *nextiocb;
5594 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005595 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005596
James Smart92d7f7b2007-06-17 19:56:38 -05005597 if (piocb->iocb_cmpl && (!piocb->vport) &&
5598 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5599 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5600 lpfc_printf_log(phba, KERN_ERR,
5601 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005602 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005603 piocb->iocb.ulpCommand);
5604 dump_stack();
5605 return IOCB_ERROR;
5606 }
5607
5608
Linas Vepstas8d63f372007-02-14 14:28:36 -06005609 /* If the PCI channel is in offline state, do not post iocbs. */
5610 if (unlikely(pci_channel_offline(phba->pcidev)))
5611 return IOCB_ERROR;
5612
James Smarta257bf92009-04-06 18:48:10 -04005613 /* If HBA has a deferred error attention, fail the iocb. */
5614 if (unlikely(phba->hba_flag & DEFER_ERATT))
5615 return IOCB_ERROR;
5616
dea31012005-04-17 16:05:31 -05005617 /*
5618 * We should never get an IOCB if we are in a < LINK_DOWN state
5619 */
James Smart2e0fef82007-06-17 19:56:36 -05005620 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005621 return IOCB_ERROR;
5622
5623 /*
5624 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005625 * outstanding event.
dea31012005-04-17 16:05:31 -05005626 */
James Smart0b727fe2007-10-27 13:37:25 -04005627 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005628 goto iocb_busy;
5629
James Smart2e0fef82007-06-17 19:56:36 -05005630 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005631 /*
James Smart2680eea2007-04-25 09:52:55 -04005632 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005633 * can be issued if the link is not up.
5634 */
5635 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005636 case CMD_GEN_REQUEST64_CR:
5637 case CMD_GEN_REQUEST64_CX:
5638 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5639 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04005640 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04005641 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5642 MENLO_TRANSPORT_TYPE))
5643
5644 goto iocb_busy;
5645 break;
dea31012005-04-17 16:05:31 -05005646 case CMD_QUE_RING_BUF_CN:
5647 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005648 /*
5649 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5650 * completion, iocb_cmpl MUST be 0.
5651 */
5652 if (piocb->iocb_cmpl)
5653 piocb->iocb_cmpl = NULL;
5654 /*FALLTHROUGH*/
5655 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005656 case CMD_CLOSE_XRI_CN:
5657 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005658 break;
5659 default:
5660 goto iocb_busy;
5661 }
5662
5663 /*
5664 * For FCP commands, we must be in a state where we can process link
5665 * attention events.
5666 */
5667 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005668 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005669 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005670 }
dea31012005-04-17 16:05:31 -05005671
dea31012005-04-17 16:05:31 -05005672 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5673 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5674 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5675
5676 if (iocb)
5677 lpfc_sli_update_ring(phba, pring);
5678 else
5679 lpfc_sli_update_full_ring(phba, pring);
5680
5681 if (!piocb)
5682 return IOCB_SUCCESS;
5683
5684 goto out_busy;
5685
5686 iocb_busy:
5687 pring->stats.iocb_cmd_delay++;
5688
5689 out_busy:
5690
5691 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005692 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005693 return IOCB_SUCCESS;
5694 }
5695
5696 return IOCB_BUSY;
5697}
5698
James Smart3772a992009-05-22 14:50:54 -04005699/**
James Smart4f774512009-05-22 14:52:35 -04005700 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5701 * @phba: Pointer to HBA context object.
5702 * @piocb: Pointer to command iocb.
5703 * @sglq: Pointer to the scatter gather queue object.
5704 *
5705 * This routine converts the bpl or bde that is in the IOCB
5706 * to a sgl list for the sli4 hardware. The physical address
5707 * of the bpl/bde is converted back to a virtual address.
5708 * If the IOCB contains a BPL then the list of BDE's is
5709 * converted to sli4_sge's. If the IOCB contains a single
5710 * BDE then it is converted to a single sli_sge.
5711 * The IOCB is still in cpu endianess so the contents of
5712 * the bpl can be used without byte swapping.
5713 *
5714 * Returns valid XRI = Success, NO_XRI = Failure.
5715**/
5716static uint16_t
5717lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5718 struct lpfc_sglq *sglq)
5719{
5720 uint16_t xritag = NO_XRI;
5721 struct ulp_bde64 *bpl = NULL;
5722 struct ulp_bde64 bde;
5723 struct sli4_sge *sgl = NULL;
5724 IOCB_t *icmd;
5725 int numBdes = 0;
5726 int i = 0;
5727
5728 if (!piocbq || !sglq)
5729 return xritag;
5730
5731 sgl = (struct sli4_sge *)sglq->sgl;
5732 icmd = &piocbq->iocb;
5733 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5734 numBdes = icmd->un.genreq64.bdl.bdeSize /
5735 sizeof(struct ulp_bde64);
5736 /* The addrHigh and addrLow fields within the IOCB
5737 * have not been byteswapped yet so there is no
5738 * need to swap them back.
5739 */
5740 bpl = (struct ulp_bde64 *)
5741 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5742
5743 if (!bpl)
5744 return xritag;
5745
5746 for (i = 0; i < numBdes; i++) {
5747 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05005748 sgl->addr_hi = bpl->addrHigh;
5749 sgl->addr_lo = bpl->addrLow;
5750
James Smart4f774512009-05-22 14:52:35 -04005751 if ((i+1) == numBdes)
5752 bf_set(lpfc_sli4_sge_last, sgl, 1);
5753 else
5754 bf_set(lpfc_sli4_sge_last, sgl, 0);
5755 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005756 /* swap the size field back to the cpu so we
5757 * can assign it to the sgl.
5758 */
5759 bde.tus.w = le32_to_cpu(bpl->tus.w);
5760 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005761 bpl++;
5762 sgl++;
5763 }
5764 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5765 /* The addrHigh and addrLow fields of the BDE have not
5766 * been byteswapped yet so they need to be swapped
5767 * before putting them in the sgl.
5768 */
5769 sgl->addr_hi =
5770 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5771 sgl->addr_lo =
5772 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart4f774512009-05-22 14:52:35 -04005773 bf_set(lpfc_sli4_sge_last, sgl, 1);
5774 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05005775 sgl->sge_len =
5776 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04005777 }
5778 return sglq->sli4_xritag;
5779}
5780
5781/**
5782 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5783 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005784 *
5785 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005786 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5787 * held.
James Smart4f774512009-05-22 14:52:35 -04005788 *
5789 * Return: index into SLI4 fast-path FCP queue index.
5790 **/
5791static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005792lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005793{
James Smart8fa38512009-07-19 10:01:03 -04005794 ++phba->fcp_qidx;
5795 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5796 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005797
James Smart8fa38512009-07-19 10:01:03 -04005798 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005799}
5800
5801/**
5802 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5803 * @phba: Pointer to HBA context object.
5804 * @piocb: Pointer to command iocb.
5805 * @wqe: Pointer to the work queue entry.
5806 *
5807 * This routine converts the iocb command to its Work Queue Entry
5808 * equivalent. The wqe pointer should not have any fields set when
5809 * this routine is called because it will memcpy over them.
5810 * This routine does not set the CQ_ID or the WQEC bits in the
5811 * wqe.
5812 *
5813 * Returns: 0 = Success, IOCB_ERROR = Failure.
5814 **/
5815static int
5816lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5817 union lpfc_wqe *wqe)
5818{
James Smart5ffc2662009-11-18 15:39:44 -05005819 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04005820 uint8_t ct = 0;
5821 uint32_t fip;
5822 uint32_t abort_tag;
5823 uint8_t command_type = ELS_COMMAND_NON_FIP;
5824 uint8_t cmnd;
5825 uint16_t xritag;
5826 struct ulp_bde64 *bpl = NULL;
James Smartc8685952009-11-18 15:39:16 -05005827 uint32_t els_id = ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05005828 int numBdes, i;
5829 struct ulp_bde64 bde;
James Smart4f774512009-05-22 14:52:35 -04005830
James Smart45ed1192009-10-02 15:17:02 -04005831 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04005832 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005833 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005834 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05005835 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04005836 command_type = ELS_COMMAND_FIP;
5837 else
5838 command_type = ELS_COMMAND_NON_FIP;
5839
James Smart4f774512009-05-22 14:52:35 -04005840 /* Some of the fields are in the right position already */
5841 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5842 abort_tag = (uint32_t) iocbq->iotag;
5843 xritag = iocbq->sli4_xritag;
5844 wqe->words[7] = 0; /* The ct field has moved so reset */
5845 /* words0-2 bpl convert bde */
5846 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05005847 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5848 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04005849 bpl = (struct ulp_bde64 *)
5850 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5851 if (!bpl)
5852 return IOCB_ERROR;
5853
5854 /* Should already be byte swapped. */
5855 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5856 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5857 /* swap the size field back to the cpu so we
5858 * can assign it to the sgl.
5859 */
5860 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05005861 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5862 total_len = 0;
5863 for (i = 0; i < numBdes; i++) {
5864 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5865 total_len += bde.tus.f.bdeSize;
5866 }
James Smart4f774512009-05-22 14:52:35 -04005867 } else
James Smart5ffc2662009-11-18 15:39:44 -05005868 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04005869
5870 iocbq->iocb.ulpIoTag = iocbq->iotag;
5871 cmnd = iocbq->iocb.ulpCommand;
5872
5873 switch (iocbq->iocb.ulpCommand) {
5874 case CMD_ELS_REQUEST64_CR:
5875 if (!iocbq->iocb.ulpLe) {
5876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5877 "2007 Only Limited Edition cmd Format"
5878 " supported 0x%x\n",
5879 iocbq->iocb.ulpCommand);
5880 return IOCB_ERROR;
5881 }
James Smart5ffc2662009-11-18 15:39:44 -05005882 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005883 /* Els_reguest64 has a TMO */
5884 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5885 iocbq->iocb.ulpTimeout);
5886 /* Need a VF for word 4 set the vf bit*/
5887 bf_set(els_req64_vf, &wqe->els_req, 0);
5888 /* And a VFID for word 12 */
5889 bf_set(els_req64_vfid, &wqe->els_req, 0);
5890 /*
5891 * Set ct field to 3, indicates that the context_tag field
5892 * contains the FCFI and remote N_Port_ID is
5893 * in word 5.
5894 */
5895
5896 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5897 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5898 iocbq->iocb.ulpContext);
5899
James Smart4f774512009-05-22 14:52:35 -04005900 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5901 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5902 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartc8685952009-11-18 15:39:16 -05005903
5904 if (command_type == ELS_COMMAND_FIP) {
5905 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5906 >> LPFC_FIP_ELS_ID_SHIFT);
5907 }
5908 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5909
James Smart4f774512009-05-22 14:52:35 -04005910 break;
James Smart5ffc2662009-11-18 15:39:44 -05005911 case CMD_XMIT_SEQUENCE64_CX:
5912 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5913 iocbq->iocb.un.ulpWord[3]);
5914 wqe->generic.word3 = 0;
5915 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
James Smart5ffc2662009-11-18 15:39:44 -05005916 /* The entire sequence is transmitted for this IOCB */
5917 xmit_len = total_len;
5918 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart4f774512009-05-22 14:52:35 -04005919 case CMD_XMIT_SEQUENCE64_CR:
5920 /* word3 iocb=io_tag32 wqe=payload_offset */
5921 /* payload offset used for multilpe outstanding
5922 * sequences on the same exchange
5923 */
5924 wqe->words[3] = 0;
5925 /* word4 relative_offset memcpy */
5926 /* word5 r_ctl/df_ctl memcpy */
5927 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart5ffc2662009-11-18 15:39:44 -05005928 wqe->xmit_sequence.xmit_len = xmit_len;
5929 command_type = OTHER_COMMAND;
James Smart4f774512009-05-22 14:52:35 -04005930 break;
5931 case CMD_XMIT_BCAST64_CN:
5932 /* word3 iocb=iotag32 wqe=payload_len */
5933 wqe->words[3] = 0; /* no definition for this in wqe */
5934 /* word4 iocb=rsvd wqe=rsvd */
5935 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5936 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5937 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5938 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5939 break;
5940 case CMD_FCP_IWRITE64_CR:
5941 command_type = FCP_COMMAND_DATA_OUT;
5942 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5943 * confusing.
5944 * word3 is payload_len: byte offset to the sgl entry for the
5945 * fcp_command.
5946 * word4 is total xfer len, same as the IOCB->ulpParameter.
5947 * word5 is initial xfer len 0 = wait for xfer-ready
5948 */
5949
5950 /* Always wait for xfer-ready before sending data */
5951 wqe->fcp_iwrite.initial_xfer_len = 0;
5952 /* word 4 (xfer length) should have been set on the memcpy */
5953
5954 /* allow write to fall through to read */
5955 case CMD_FCP_IREAD64_CR:
5956 /* FCP_CMD is always the 1st sgl entry */
5957 wqe->fcp_iread.payload_len =
James Smart5ffc2662009-11-18 15:39:44 -05005958 xmit_len + sizeof(struct fcp_rsp);
James Smart4f774512009-05-22 14:52:35 -04005959
5960 /* word 4 (xfer length) should have been set on the memcpy */
5961
5962 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5963 iocbq->iocb.ulpFCP2Rcvy);
5964 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5965 /* The XC bit and the XS bit are similar. The driver never
5966 * tracked whether or not the exchange was previouslly open.
5967 * XC = Exchange create, 0 is create. 1 is already open.
5968 * XS = link cmd: 1 do not close the exchange after command.
5969 * XS = 0 close exchange when command completes.
5970 * The only time we would not set the XC bit is when the XS bit
5971 * is set and we are sending our 2nd or greater command on
5972 * this exchange.
5973 */
James Smart4f774512009-05-22 14:52:35 -04005974 /* Always open the exchange */
5975 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5976
5977 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5978 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04005979 break;
5980 case CMD_FCP_ICMND64_CR:
5981 /* Always open the exchange */
5982 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5983
5984 wqe->words[4] = 0;
5985 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5986 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04005987 break;
5988 case CMD_GEN_REQUEST64_CR:
5989 /* word3 command length is described as byte offset to the
5990 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5991 * sgl[0] = cmnd
5992 * sgl[1] = rsp.
5993 *
5994 */
James Smart5ffc2662009-11-18 15:39:44 -05005995 wqe->gen_req.command_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04005996 /* Word4 parameter copied in the memcpy */
5997 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
5998 /* word6 context tag copied in memcpy */
5999 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6000 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6001 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6002 "2015 Invalid CT %x command 0x%x\n",
6003 ct, iocbq->iocb.ulpCommand);
6004 return IOCB_ERROR;
6005 }
6006 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6007 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6008 iocbq->iocb.ulpTimeout);
6009
6010 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6011 command_type = OTHER_COMMAND;
6012 break;
6013 case CMD_XMIT_ELS_RSP64_CX:
6014 /* words0-2 BDE memcpy */
6015 /* word3 iocb=iotag32 wqe=rsvd */
6016 wqe->words[3] = 0;
6017 /* word4 iocb=did wge=rsvd. */
6018 wqe->words[4] = 0;
6019 /* word5 iocb=rsvd wge=did */
6020 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6021 iocbq->iocb.un.elsreq64.remoteID);
6022
6023 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6024 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6025
6026 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6027 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6028 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6029 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6030 iocbq->vport->vpi + phba->vpi_base);
6031 command_type = OTHER_COMMAND;
6032 break;
6033 case CMD_CLOSE_XRI_CN:
6034 case CMD_ABORT_XRI_CN:
6035 case CMD_ABORT_XRI_CX:
6036 /* words 0-2 memcpy should be 0 rserved */
6037 /* port will send abts */
6038 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6039 /*
6040 * The link is down so the fw does not need to send abts
6041 * on the wire.
6042 */
6043 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6044 else
6045 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6046 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smart4f774512009-05-22 14:52:35 -04006047 wqe->words[5] = 0;
6048 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6049 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6050 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04006051 /*
6052 * The abort handler will send us CMD_ABORT_XRI_CN or
6053 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6054 */
6055 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6056 cmnd = CMD_ABORT_XRI_CX;
6057 command_type = OTHER_COMMAND;
6058 xritag = 0;
6059 break;
James Smart6669f9b2009-10-02 15:16:45 -04006060 case CMD_XMIT_BLS_RSP64_CX:
6061 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6062 * we re-construct this WQE here based on information in
6063 * iocbq from scratch.
6064 */
6065 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05006066 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04006067 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart5ffc2662009-11-18 15:39:44 -05006068 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6069 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6070 LPFC_ABTS_UNSOL_INT) {
6071 /* ABTS sent by initiator to CT exchange, the
6072 * RX_ID field will be filled with the newly
6073 * allocated responder XRI.
6074 */
6075 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6076 iocbq->sli4_xritag);
6077 } else {
6078 /* ABTS sent by responder to CT exchange, the
6079 * RX_ID field will be filled with the responder
6080 * RX_ID from ABTS.
6081 */
6082 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6083 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6084 }
James Smart6669f9b2009-10-02 15:16:45 -04006085 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6086 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6087 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6088 iocbq->iocb.ulpContext);
6089 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6090 command_type = OTHER_COMMAND;
6091 break;
James Smart4f774512009-05-22 14:52:35 -04006092 case CMD_XRI_ABORTED_CX:
6093 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6094 /* words0-2 are all 0's no bde */
6095 /* word3 and word4 are rsvrd */
6096 wqe->words[3] = 0;
6097 wqe->words[4] = 0;
6098 /* word5 iocb=rsvd wge=did */
6099 /* There is no remote port id in the IOCB? */
6100 /* Let this fall through and fail */
6101 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6102 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6103 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6104 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6105 default:
6106 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6107 "2014 Invalid command 0x%x\n",
6108 iocbq->iocb.ulpCommand);
6109 return IOCB_ERROR;
6110 break;
6111
6112 }
6113 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6114 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6115 wqe->generic.abort_tag = abort_tag;
6116 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6117 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6118 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6119 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6120
6121 return 0;
6122}
6123
6124/**
6125 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6126 * @phba: Pointer to HBA context object.
6127 * @ring_number: SLI ring number to issue iocb on.
6128 * @piocb: Pointer to command iocb.
6129 * @flag: Flag indicating if this command can be put into txq.
6130 *
6131 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6132 * an iocb command to an HBA with SLI-4 interface spec.
6133 *
6134 * This function is called with hbalock held. The function will return success
6135 * after it successfully submit the iocb to firmware or after adding to the
6136 * txq.
6137 **/
6138static int
6139__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6140 struct lpfc_iocbq *piocb, uint32_t flag)
6141{
6142 struct lpfc_sglq *sglq;
6143 uint16_t xritag;
6144 union lpfc_wqe wqe;
6145 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04006146
6147 if (piocb->sli4_xritag == NO_XRI) {
6148 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006149 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006150 sglq = NULL;
6151 else {
6152 sglq = __lpfc_sli_get_sglq(phba);
6153 if (!sglq)
6154 return IOCB_ERROR;
6155 piocb->sli4_xritag = sglq->sli4_xritag;
6156 }
6157 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6158 sglq = NULL; /* These IO's already have an XRI and
6159 * a mapped sgl.
6160 */
6161 } else {
6162 /* This is a continuation of a commandi,(CX) so this
6163 * sglq is on the active list
6164 */
6165 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6166 if (!sglq)
6167 return IOCB_ERROR;
6168 }
6169
6170 if (sglq) {
6171 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6172 if (xritag != sglq->sli4_xritag)
6173 return IOCB_ERROR;
6174 }
6175
6176 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6177 return IOCB_ERROR;
6178
James Smart341af102010-01-26 23:07:37 -05006179 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6180 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart5ffc2662009-11-18 15:39:44 -05006181 /*
6182 * For FCP command IOCB, get a new WQ index to distribute
6183 * WQE across the WQsr. On the other hand, for abort IOCB,
6184 * it carries the same WQ index to the original command
6185 * IOCB.
6186 */
James Smart341af102010-01-26 23:07:37 -05006187 if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart5ffc2662009-11-18 15:39:44 -05006188 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6189 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6190 &wqe))
James Smart4f774512009-05-22 14:52:35 -04006191 return IOCB_ERROR;
6192 } else {
6193 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6194 return IOCB_ERROR;
6195 }
6196 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6197
6198 return 0;
6199}
6200
6201/**
James Smart3772a992009-05-22 14:50:54 -04006202 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6203 *
6204 * This routine wraps the actual lockless version for issusing IOCB function
6205 * pointer from the lpfc_hba struct.
6206 *
6207 * Return codes:
6208 * IOCB_ERROR - Error
6209 * IOCB_SUCCESS - Success
6210 * IOCB_BUSY - Busy
6211 **/
6212static inline int
6213__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6214 struct lpfc_iocbq *piocb, uint32_t flag)
6215{
6216 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6217}
6218
6219/**
6220 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6221 * @phba: The hba struct for which this call is being executed.
6222 * @dev_grp: The HBA PCI-Device group number.
6223 *
6224 * This routine sets up the SLI interface API function jump table in @phba
6225 * struct.
6226 * Returns: 0 - success, -ENODEV - failure.
6227 **/
6228int
6229lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6230{
6231
6232 switch (dev_grp) {
6233 case LPFC_PCI_DEV_LP:
6234 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6235 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6236 break;
James Smart4f774512009-05-22 14:52:35 -04006237 case LPFC_PCI_DEV_OC:
6238 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6239 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6240 break;
James Smart3772a992009-05-22 14:50:54 -04006241 default:
6242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6243 "1419 Invalid HBA PCI-device group: 0x%x\n",
6244 dev_grp);
6245 return -ENODEV;
6246 break;
6247 }
6248 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6249 return 0;
6250}
James Smart92d7f7b2007-06-17 19:56:38 -05006251
James Smarte59058c2008-08-24 21:49:00 -04006252/**
James Smart3621a712009-04-06 18:47:14 -04006253 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006254 * @phba: Pointer to HBA context object.
6255 * @pring: Pointer to driver SLI ring object.
6256 * @piocb: Pointer to command iocb.
6257 * @flag: Flag indicating if this command can be put into txq.
6258 *
6259 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6260 * function. This function gets the hbalock and calls
6261 * __lpfc_sli_issue_iocb function and will return the error returned
6262 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6263 * functions which do not hold hbalock.
6264 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006265int
James Smart3772a992009-05-22 14:50:54 -04006266lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006267 struct lpfc_iocbq *piocb, uint32_t flag)
6268{
6269 unsigned long iflags;
6270 int rc;
6271
6272 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006273 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006274 spin_unlock_irqrestore(&phba->hbalock, iflags);
6275
6276 return rc;
6277}
6278
James Smarte59058c2008-08-24 21:49:00 -04006279/**
James Smart3621a712009-04-06 18:47:14 -04006280 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006281 * @phba: Pointer to HBA context object.
6282 *
6283 * This function is called while driver attaches with the
6284 * HBA to setup the extra ring. The extra ring is used
6285 * only when driver needs to support target mode functionality
6286 * or IP over FC functionalities.
6287 *
6288 * This function is called with no lock held.
6289 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006290static int
6291lpfc_extra_ring_setup( struct lpfc_hba *phba)
6292{
6293 struct lpfc_sli *psli;
6294 struct lpfc_sli_ring *pring;
6295
6296 psli = &phba->sli;
6297
6298 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006299
6300 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006301 pring = &psli->ring[psli->fcp_ring];
6302 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6303 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6304 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6305 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6306
James Smarta4bc3372006-12-02 13:34:16 -05006307 /* and give them to the extra ring */
6308 pring = &psli->ring[psli->extra_ring];
6309
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006310 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6311 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6312 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6313 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6314
6315 /* Setup default profile for this ring */
6316 pring->iotag_max = 4096;
6317 pring->num_mask = 1;
6318 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006319 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6320 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006321 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6322 return 0;
6323}
6324
James Smarte59058c2008-08-24 21:49:00 -04006325/**
James Smart3621a712009-04-06 18:47:14 -04006326 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006327 * @phba: Pointer to HBA context object.
6328 * @pring: Pointer to driver SLI ring object.
6329 * @iocbq: Pointer to iocb object.
6330 *
6331 * This function is called by the slow ring event handler
6332 * function when there is an ASYNC event iocb in the ring.
6333 * This function is called with no lock held.
6334 * Currently this function handles only temperature related
6335 * ASYNC events. The function decodes the temperature sensor
6336 * event message and posts events for the management applications.
6337 **/
James Smart98c9ea52007-10-27 13:37:33 -04006338static void
James Smart57127f12007-10-27 13:37:05 -04006339lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6340 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6341{
6342 IOCB_t *icmd;
6343 uint16_t evt_code;
6344 uint16_t temp;
6345 struct temp_event temp_event_data;
6346 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006347 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006348
6349 icmd = &iocbq->iocb;
6350 evt_code = icmd->un.asyncstat.evt_code;
6351 temp = icmd->ulpContext;
6352
6353 if ((evt_code != ASYNC_TEMP_WARN) &&
6354 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006355 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006356 lpfc_printf_log(phba,
6357 KERN_ERR,
6358 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006359 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006360 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006361 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6362 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6363 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6364 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006365 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006366 icmd->un.asyncstat.evt_code,
6367 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6368 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6369 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6370 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6371
James Smart57127f12007-10-27 13:37:05 -04006372 return;
6373 }
6374 temp_event_data.data = (uint32_t)temp;
6375 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6376 if (evt_code == ASYNC_TEMP_WARN) {
6377 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6378 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006379 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006380 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006381 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006382 "corrective action. temperature : %d Celsius\n",
6383 temp);
6384 }
6385 if (evt_code == ASYNC_TEMP_SAFE) {
6386 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6387 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006388 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006389 LOG_TEMP,
6390 "0340 Adapter temperature is OK now. "
6391 "temperature : %d Celsius\n",
6392 temp);
6393 }
6394
6395 /* Send temperature change event to applications */
6396 shost = lpfc_shost_from_vport(phba->pport);
6397 fc_host_post_vendor_event(shost, fc_get_event_number(),
6398 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006399 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006400
6401}
6402
6403
James Smarte59058c2008-08-24 21:49:00 -04006404/**
James Smart3621a712009-04-06 18:47:14 -04006405 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006406 * @phba: Pointer to HBA context object.
6407 *
6408 * lpfc_sli_setup sets up rings of the SLI interface with
6409 * number of iocbs per ring and iotags. This function is
6410 * called while driver attach to the HBA and before the
6411 * interrupts are enabled. So there is no need for locking.
6412 *
6413 * This function always returns 0.
6414 **/
dea31012005-04-17 16:05:31 -05006415int
6416lpfc_sli_setup(struct lpfc_hba *phba)
6417{
James Smarted957682007-06-17 19:56:37 -05006418 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006419 struct lpfc_sli *psli = &phba->sli;
6420 struct lpfc_sli_ring *pring;
6421
6422 psli->num_rings = MAX_CONFIGURED_RINGS;
6423 psli->sli_flag = 0;
6424 psli->fcp_ring = LPFC_FCP_RING;
6425 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006426 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006427
James Bottomley604a3e32005-10-29 10:28:33 -05006428 psli->iocbq_lookup = NULL;
6429 psli->iocbq_lookup_len = 0;
6430 psli->last_iotag = 0;
6431
dea31012005-04-17 16:05:31 -05006432 for (i = 0; i < psli->num_rings; i++) {
6433 pring = &psli->ring[i];
6434 switch (i) {
6435 case LPFC_FCP_RING: /* ring 0 - FCP */
6436 /* numCiocb and numRiocb are used in config_port */
6437 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6438 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6439 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6440 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6441 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6442 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006443 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006444 SLI3_IOCB_CMD_SIZE :
6445 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006446 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006447 SLI3_IOCB_RSP_SIZE :
6448 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006449 pring->iotag_ctr = 0;
6450 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006451 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006452 pring->fast_iotag = pring->iotag_max;
6453 pring->num_mask = 0;
6454 break;
James Smarta4bc3372006-12-02 13:34:16 -05006455 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006456 /* numCiocb and numRiocb are used in config_port */
6457 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6458 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006459 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006460 SLI3_IOCB_CMD_SIZE :
6461 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006462 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006463 SLI3_IOCB_RSP_SIZE :
6464 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006465 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006466 pring->num_mask = 0;
6467 break;
6468 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6469 /* numCiocb and numRiocb are used in config_port */
6470 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6471 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006472 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006473 SLI3_IOCB_CMD_SIZE :
6474 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006475 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006476 SLI3_IOCB_RSP_SIZE :
6477 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006478 pring->fast_iotag = 0;
6479 pring->iotag_ctr = 0;
6480 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006481 pring->lpfc_sli_rcv_async_status =
6482 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006483 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006484 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04006485 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6486 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006487 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006488 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006489 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04006490 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6491 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05006492 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006493 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006494 pring->prt[2].profile = 0; /* Mask 2 */
6495 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04006496 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05006497 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006498 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006499 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006500 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006501 pring->prt[3].profile = 0; /* Mask 3 */
6502 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04006503 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05006504 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04006505 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05006506 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006507 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006508 /* abort unsolicited sequence */
6509 pring->prt[4].profile = 0; /* Mask 4 */
6510 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6511 pring->prt[4].type = FC_TYPE_BLS;
6512 pring->prt[4].lpfc_sli_rcv_unsol_event =
6513 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006514 break;
6515 }
James Smarted957682007-06-17 19:56:37 -05006516 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006517 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006518 }
James Smarted957682007-06-17 19:56:37 -05006519 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006520 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006521 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6522 "SLI2 SLIM Data: x%x x%lx\n",
6523 phba->brd_no, totiocbsize,
6524 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006525 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006526 if (phba->cfg_multi_ring_support == 2)
6527 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006528
6529 return 0;
6530}
6531
James Smarte59058c2008-08-24 21:49:00 -04006532/**
James Smart3621a712009-04-06 18:47:14 -04006533 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006534 * @phba: Pointer to HBA context object.
6535 *
6536 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6537 * ring. This function also initializes ring indices of each ring.
6538 * This function is called during the initialization of the SLI
6539 * interface of an HBA.
6540 * This function is called with no lock held and always returns
6541 * 1.
6542 **/
dea31012005-04-17 16:05:31 -05006543int
James Smart2e0fef82007-06-17 19:56:36 -05006544lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006545{
6546 struct lpfc_sli *psli;
6547 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006548 int i;
dea31012005-04-17 16:05:31 -05006549
6550 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006551 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006552 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006553 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006554 /* Initialize list headers for txq and txcmplq as double linked lists */
6555 for (i = 0; i < psli->num_rings; i++) {
6556 pring = &psli->ring[i];
6557 pring->ringno = i;
6558 pring->next_cmdidx = 0;
6559 pring->local_getidx = 0;
6560 pring->cmdidx = 0;
6561 INIT_LIST_HEAD(&pring->txq);
6562 INIT_LIST_HEAD(&pring->txcmplq);
6563 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006564 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006565 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006566 }
James Smart2e0fef82007-06-17 19:56:36 -05006567 spin_unlock_irq(&phba->hbalock);
6568 return 1;
dea31012005-04-17 16:05:31 -05006569}
6570
James Smarte59058c2008-08-24 21:49:00 -04006571/**
James Smart04c68492009-05-22 14:52:52 -04006572 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6573 * @phba: Pointer to HBA context object.
6574 *
6575 * This routine flushes the mailbox command subsystem. It will unconditionally
6576 * flush all the mailbox commands in the three possible stages in the mailbox
6577 * command sub-system: pending mailbox command queue; the outstanding mailbox
6578 * command; and completed mailbox command queue. It is caller's responsibility
6579 * to make sure that the driver is in the proper state to flush the mailbox
6580 * command sub-system. Namely, the posting of mailbox commands into the
6581 * pending mailbox command queue from the various clients must be stopped;
6582 * either the HBA is in a state that it will never works on the outstanding
6583 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6584 * mailbox command has been completed.
6585 **/
6586static void
6587lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6588{
6589 LIST_HEAD(completions);
6590 struct lpfc_sli *psli = &phba->sli;
6591 LPFC_MBOXQ_t *pmb;
6592 unsigned long iflag;
6593
6594 /* Flush all the mailbox commands in the mbox system */
6595 spin_lock_irqsave(&phba->hbalock, iflag);
6596 /* The pending mailbox command queue */
6597 list_splice_init(&phba->sli.mboxq, &completions);
6598 /* The outstanding active mailbox command */
6599 if (psli->mbox_active) {
6600 list_add_tail(&psli->mbox_active->list, &completions);
6601 psli->mbox_active = NULL;
6602 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6603 }
6604 /* The completed mailbox command queue */
6605 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6606 spin_unlock_irqrestore(&phba->hbalock, iflag);
6607
6608 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6609 while (!list_empty(&completions)) {
6610 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6611 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6612 if (pmb->mbox_cmpl)
6613 pmb->mbox_cmpl(phba, pmb);
6614 }
6615}
6616
6617/**
James Smart3621a712009-04-06 18:47:14 -04006618 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006619 * @vport: Pointer to virtual port object.
6620 *
6621 * lpfc_sli_host_down is called to clean up the resources
6622 * associated with a vport before destroying virtual
6623 * port data structures.
6624 * This function does following operations:
6625 * - Free discovery resources associated with this virtual
6626 * port.
6627 * - Free iocbs associated with this virtual port in
6628 * the txq.
6629 * - Send abort for all iocb commands associated with this
6630 * vport in txcmplq.
6631 *
6632 * This function is called with no lock held and always returns 1.
6633 **/
dea31012005-04-17 16:05:31 -05006634int
James Smart92d7f7b2007-06-17 19:56:38 -05006635lpfc_sli_host_down(struct lpfc_vport *vport)
6636{
James Smart858c9f62007-06-17 19:56:39 -05006637 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006638 struct lpfc_hba *phba = vport->phba;
6639 struct lpfc_sli *psli = &phba->sli;
6640 struct lpfc_sli_ring *pring;
6641 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006642 int i;
6643 unsigned long flags = 0;
6644 uint16_t prev_pring_flag;
6645
6646 lpfc_cleanup_discovery_resources(vport);
6647
6648 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006649 for (i = 0; i < psli->num_rings; i++) {
6650 pring = &psli->ring[i];
6651 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006652 /* Only slow rings */
6653 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006654 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006655 /* Set the lpfc data pending flag */
6656 set_bit(LPFC_DATA_READY, &phba->data_flags);
6657 }
James Smart92d7f7b2007-06-17 19:56:38 -05006658 /*
6659 * Error everything on the txq since these iocbs have not been
6660 * given to the FW yet.
6661 */
James Smart92d7f7b2007-06-17 19:56:38 -05006662 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6663 if (iocb->vport != vport)
6664 continue;
James Smart858c9f62007-06-17 19:56:39 -05006665 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006666 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006667 }
6668
6669 /* Next issue ABTS for everything on the txcmplq */
6670 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6671 list) {
6672 if (iocb->vport != vport)
6673 continue;
6674 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6675 }
6676
6677 pring->flag = prev_pring_flag;
6678 }
6679
6680 spin_unlock_irqrestore(&phba->hbalock, flags);
6681
James Smarta257bf92009-04-06 18:48:10 -04006682 /* Cancel all the IOCBs from the completions list */
6683 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6684 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006685 return 1;
6686}
6687
James Smarte59058c2008-08-24 21:49:00 -04006688/**
James Smart3621a712009-04-06 18:47:14 -04006689 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006690 * @phba: Pointer to HBA context object.
6691 *
6692 * This function cleans up all iocb, buffers, mailbox commands
6693 * while shutting down the HBA. This function is called with no
6694 * lock held and always returns 1.
6695 * This function does the following to cleanup driver resources:
6696 * - Free discovery resources for each virtual port
6697 * - Cleanup any pending fabric iocbs
6698 * - Iterate through the iocb txq and free each entry
6699 * in the list.
6700 * - Free up any buffer posted to the HBA
6701 * - Free mailbox commands in the mailbox queue.
6702 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006703int
James Smart2e0fef82007-06-17 19:56:36 -05006704lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006705{
James Smart2534ba72007-04-25 09:52:20 -04006706 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006707 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006708 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006709 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006710 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006711 int i;
6712
6713 /* Shutdown the mailbox command sub-system */
6714 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006715
dea31012005-04-17 16:05:31 -05006716 lpfc_hba_down_prep(phba);
6717
James Smart92d7f7b2007-06-17 19:56:38 -05006718 lpfc_fabric_abort_hba(phba);
6719
James Smart2e0fef82007-06-17 19:56:36 -05006720 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006721 for (i = 0; i < psli->num_rings; i++) {
6722 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006723 /* Only slow rings */
6724 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006725 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006726 /* Set the lpfc data pending flag */
6727 set_bit(LPFC_DATA_READY, &phba->data_flags);
6728 }
dea31012005-04-17 16:05:31 -05006729
6730 /*
6731 * Error everything on the txq since these iocbs have not been
6732 * given to the FW yet.
6733 */
James Smart2534ba72007-04-25 09:52:20 -04006734 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006735 pring->txq_cnt = 0;
6736
dea31012005-04-17 16:05:31 -05006737 }
James Smart2e0fef82007-06-17 19:56:36 -05006738 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006739
James Smarta257bf92009-04-06 18:48:10 -04006740 /* Cancel all the IOCBs from the completions list */
6741 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6742 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006743
James Smart0ff10d42008-01-11 01:52:36 -05006744 spin_lock_irqsave(&phba->hbalock, flags);
6745 list_splice_init(&phba->elsbuf, &completions);
6746 phba->elsbuf_cnt = 0;
6747 phba->elsbuf_prev_cnt = 0;
6748 spin_unlock_irqrestore(&phba->hbalock, flags);
6749
6750 while (!list_empty(&completions)) {
6751 list_remove_head(&completions, buf_ptr,
6752 struct lpfc_dmabuf, list);
6753 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6754 kfree(buf_ptr);
6755 }
6756
dea31012005-04-17 16:05:31 -05006757 /* Return any active mbox cmds */
6758 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006759
James Smartda0436e2009-05-22 14:51:39 -04006760 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006761 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006762 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006763
James Smartda0436e2009-05-22 14:51:39 -04006764 return 1;
6765}
James Smart92d7f7b2007-06-17 19:56:38 -05006766
James Smartda0436e2009-05-22 14:51:39 -04006767/**
6768 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6769 * @phba: Pointer to HBA context object.
6770 *
6771 * This function cleans up all queues, iocb, buffers, mailbox commands while
6772 * shutting down the SLI4 HBA FCoE function. This function is called with no
6773 * lock held and always returns 1.
6774 *
6775 * This function does the following to cleanup driver FCoE function resources:
6776 * - Free discovery resources for each virtual port
6777 * - Cleanup any pending fabric iocbs
6778 * - Iterate through the iocb txq and free each entry in the list.
6779 * - Free up any buffer posted to the HBA.
6780 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6781 * - Free mailbox commands in the mailbox queue.
6782 **/
6783int
6784lpfc_sli4_hba_down(struct lpfc_hba *phba)
6785{
6786 /* Stop the SLI4 device port */
6787 lpfc_stop_port(phba);
6788
6789 /* Tear down the queues in the HBA */
6790 lpfc_sli4_queue_unset(phba);
6791
6792 /* unregister default FCFI from the HBA */
6793 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6794
dea31012005-04-17 16:05:31 -05006795 return 1;
6796}
6797
James Smarte59058c2008-08-24 21:49:00 -04006798/**
James Smart3621a712009-04-06 18:47:14 -04006799 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006800 * @srcp: Source memory pointer.
6801 * @destp: Destination memory pointer.
6802 * @cnt: Number of words required to be copied.
6803 *
6804 * This function is used for copying data between driver memory
6805 * and the SLI memory. This function also changes the endianness
6806 * of each word if native endianness is different from SLI
6807 * endianness. This function can be called with or without
6808 * lock.
6809 **/
dea31012005-04-17 16:05:31 -05006810void
6811lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6812{
6813 uint32_t *src = srcp;
6814 uint32_t *dest = destp;
6815 uint32_t ldata;
6816 int i;
6817
6818 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6819 ldata = *src;
6820 ldata = le32_to_cpu(ldata);
6821 *dest = ldata;
6822 src++;
6823 dest++;
6824 }
6825}
6826
James Smarte59058c2008-08-24 21:49:00 -04006827
6828/**
James Smarta0c87cb2009-07-19 10:01:10 -04006829 * lpfc_sli_bemem_bcopy - SLI memory copy function
6830 * @srcp: Source memory pointer.
6831 * @destp: Destination memory pointer.
6832 * @cnt: Number of words required to be copied.
6833 *
6834 * This function is used for copying data between a data structure
6835 * with big endian representation to local endianness.
6836 * This function can be called with or without lock.
6837 **/
6838void
6839lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6840{
6841 uint32_t *src = srcp;
6842 uint32_t *dest = destp;
6843 uint32_t ldata;
6844 int i;
6845
6846 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6847 ldata = *src;
6848 ldata = be32_to_cpu(ldata);
6849 *dest = ldata;
6850 src++;
6851 dest++;
6852 }
6853}
6854
6855/**
James Smart3621a712009-04-06 18:47:14 -04006856 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006857 * @phba: Pointer to HBA context object.
6858 * @pring: Pointer to driver SLI ring object.
6859 * @mp: Pointer to driver buffer object.
6860 *
6861 * This function is called with no lock held.
6862 * It always return zero after adding the buffer to the postbufq
6863 * buffer list.
6864 **/
dea31012005-04-17 16:05:31 -05006865int
James Smart2e0fef82007-06-17 19:56:36 -05006866lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6867 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006868{
6869 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6870 later */
James Smart2e0fef82007-06-17 19:56:36 -05006871 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006872 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006873 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006874 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006875 return 0;
6876}
6877
James Smarte59058c2008-08-24 21:49:00 -04006878/**
James Smart3621a712009-04-06 18:47:14 -04006879 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006880 * @phba: Pointer to HBA context object.
6881 *
6882 * When HBQ is enabled, buffers are searched based on tags. This function
6883 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6884 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6885 * does not conflict with tags of buffer posted for unsolicited events.
6886 * The function returns the allocated tag. The function is called with
6887 * no locks held.
6888 **/
James Smart76bb24e2007-10-27 13:38:00 -04006889uint32_t
6890lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6891{
6892 spin_lock_irq(&phba->hbalock);
6893 phba->buffer_tag_count++;
6894 /*
6895 * Always set the QUE_BUFTAG_BIT to distiguish between
6896 * a tag assigned by HBQ.
6897 */
6898 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6899 spin_unlock_irq(&phba->hbalock);
6900 return phba->buffer_tag_count;
6901}
6902
James Smarte59058c2008-08-24 21:49:00 -04006903/**
James Smart3621a712009-04-06 18:47:14 -04006904 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006905 * @phba: Pointer to HBA context object.
6906 * @pring: Pointer to driver SLI ring object.
6907 * @tag: Buffer tag.
6908 *
6909 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6910 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6911 * iocb is posted to the response ring with the tag of the buffer.
6912 * This function searches the pring->postbufq list using the tag
6913 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6914 * iocb. If the buffer is found then lpfc_dmabuf object of the
6915 * buffer is returned to the caller else NULL is returned.
6916 * This function is called with no lock held.
6917 **/
James Smart76bb24e2007-10-27 13:38:00 -04006918struct lpfc_dmabuf *
6919lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6920 uint32_t tag)
6921{
6922 struct lpfc_dmabuf *mp, *next_mp;
6923 struct list_head *slp = &pring->postbufq;
6924
6925 /* Search postbufq, from the begining, looking for a match on tag */
6926 spin_lock_irq(&phba->hbalock);
6927 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6928 if (mp->buffer_tag == tag) {
6929 list_del_init(&mp->list);
6930 pring->postbufq_cnt--;
6931 spin_unlock_irq(&phba->hbalock);
6932 return mp;
6933 }
6934 }
6935
6936 spin_unlock_irq(&phba->hbalock);
6937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006938 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006939 "ring %d Data x%lx x%p x%p x%x\n",
6940 pring->ringno, (unsigned long) tag,
6941 slp->next, slp->prev, pring->postbufq_cnt);
6942
6943 return NULL;
6944}
dea31012005-04-17 16:05:31 -05006945
James Smarte59058c2008-08-24 21:49:00 -04006946/**
James Smart3621a712009-04-06 18:47:14 -04006947 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006948 * @phba: Pointer to HBA context object.
6949 * @pring: Pointer to driver SLI ring object.
6950 * @phys: DMA address of the buffer.
6951 *
6952 * This function searches the buffer list using the dma_address
6953 * of unsolicited event to find the driver's lpfc_dmabuf object
6954 * corresponding to the dma_address. The function returns the
6955 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6956 * This function is called by the ct and els unsolicited event
6957 * handlers to get the buffer associated with the unsolicited
6958 * event.
6959 *
6960 * This function is called with no lock held.
6961 **/
dea31012005-04-17 16:05:31 -05006962struct lpfc_dmabuf *
6963lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6964 dma_addr_t phys)
6965{
6966 struct lpfc_dmabuf *mp, *next_mp;
6967 struct list_head *slp = &pring->postbufq;
6968
6969 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006970 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006971 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6972 if (mp->phys == phys) {
6973 list_del_init(&mp->list);
6974 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006975 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006976 return mp;
6977 }
6978 }
6979
James Smart2e0fef82007-06-17 19:56:36 -05006980 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006982 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006983 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006984 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006985 slp->next, slp->prev, pring->postbufq_cnt);
6986 return NULL;
6987}
6988
James Smarte59058c2008-08-24 21:49:00 -04006989/**
James Smart3621a712009-04-06 18:47:14 -04006990 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006991 * @phba: Pointer to HBA context object.
6992 * @cmdiocb: Pointer to driver command iocb object.
6993 * @rspiocb: Pointer to driver response iocb object.
6994 *
6995 * This function is the completion handler for the abort iocbs for
6996 * ELS commands. This function is called from the ELS ring event
6997 * handler with no lock held. This function frees memory resources
6998 * associated with the abort iocb.
6999 **/
dea31012005-04-17 16:05:31 -05007000static void
James Smart2e0fef82007-06-17 19:56:36 -05007001lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7002 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007003{
James Smart2e0fef82007-06-17 19:56:36 -05007004 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007005 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007006 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007007 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7008
7009 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007010
7011 if (irsp->ulpStatus) {
7012 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7013 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7014
James Smart2e0fef82007-06-17 19:56:36 -05007015 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04007016 if (phba->sli_rev < LPFC_SLI_REV4) {
7017 if (abort_iotag != 0 &&
7018 abort_iotag <= phba->sli.last_iotag)
7019 abort_iocb =
7020 phba->sli.iocbq_lookup[abort_iotag];
7021 } else
7022 /* For sli4 the abort_tag is the XRI,
7023 * so the abort routine puts the iotag of the iocb
7024 * being aborted in the context field of the abort
7025 * IOCB.
7026 */
7027 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04007028
James Smart92d7f7b2007-06-17 19:56:38 -05007029 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007030 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007031 "with tag %x context %x, abort status %x, "
7032 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007033 abort_iocb, abort_iotag, abort_context,
7034 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007035
7036 /*
James Smart58da1ff2008-04-07 10:15:56 -04007037 * If the iocb is not found in Firmware queue the iocb
7038 * might have completed already. Do not free it again.
7039 */
James Smart9b379602008-04-07 10:16:00 -04007040 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart45ed1192009-10-02 15:17:02 -04007041 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7042 spin_unlock_irq(&phba->hbalock);
7043 lpfc_sli_release_iocbq(phba, cmdiocb);
7044 return;
7045 }
7046 /* For SLI4 the ulpContext field for abort IOCB
7047 * holds the iotag of the IOCB being aborted so
7048 * the local abort_context needs to be reset to
7049 * match the aborted IOCBs ulpContext.
7050 */
7051 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7052 abort_context = abort_iocb->iocb.ulpContext;
James Smart58da1ff2008-04-07 10:15:56 -04007053 }
7054 /*
James Smart2680eea2007-04-25 09:52:55 -04007055 * make sure we have the right iocbq before taking it
7056 * off the txcmplq and try to call completion routine.
7057 */
James Smart2e0fef82007-06-17 19:56:36 -05007058 if (!abort_iocb ||
7059 abort_iocb->iocb.ulpContext != abort_context ||
7060 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7061 spin_unlock_irq(&phba->hbalock);
James Smart341af102010-01-26 23:07:37 -05007062 else if (phba->sli_rev < LPFC_SLI_REV4) {
7063 /*
7064 * leave the SLI4 aborted command on the txcmplq
7065 * list and the command complete WCQE's XB bit
7066 * will tell whether the SGL (XRI) can be released
7067 * immediately or to the aborted SGL list for the
7068 * following abort XRI from the HBA.
7069 */
James Smart92d7f7b2007-06-17 19:56:38 -05007070 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007071 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007072 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007073
James Smart0ff10d42008-01-11 01:52:36 -05007074 /* Firmware could still be in progress of DMAing
7075 * payload, so don't free data buffer till after
7076 * a hbeat.
7077 */
James Smart341af102010-01-26 23:07:37 -05007078 spin_lock_irq(&phba->hbalock);
James Smart0ff10d42008-01-11 01:52:36 -05007079 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart92d7f7b2007-06-17 19:56:38 -05007080 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05007081 spin_unlock_irq(&phba->hbalock);
7082
James Smart92d7f7b2007-06-17 19:56:38 -05007083 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
James Smart341af102010-01-26 23:07:37 -05007084 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
James Smart92d7f7b2007-06-17 19:56:38 -05007085 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007086 }
7087 }
7088
James Bottomley604a3e32005-10-29 10:28:33 -05007089 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007090 return;
7091}
7092
James Smarte59058c2008-08-24 21:49:00 -04007093/**
James Smart3621a712009-04-06 18:47:14 -04007094 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007095 * @phba: Pointer to HBA context object.
7096 * @cmdiocb: Pointer to driver command iocb object.
7097 * @rspiocb: Pointer to driver response iocb object.
7098 *
7099 * The function is called from SLI ring event handler with no
7100 * lock held. This function is the completion handler for ELS commands
7101 * which are aborted. The function frees memory resources used for
7102 * the aborted ELS commands.
7103 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007104static void
7105lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7106 struct lpfc_iocbq *rspiocb)
7107{
7108 IOCB_t *irsp = &rspiocb->iocb;
7109
7110 /* ELS cmd tag <ulpIoTag> completes */
7111 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007112 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007113 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007114 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007115 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007116 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7117 lpfc_ct_free_iocb(phba, cmdiocb);
7118 else
7119 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007120 return;
7121}
7122
James Smarte59058c2008-08-24 21:49:00 -04007123/**
James Smart3621a712009-04-06 18:47:14 -04007124 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007125 * @phba: Pointer to HBA context object.
7126 * @pring: Pointer to driver SLI ring object.
7127 * @cmdiocb: Pointer to driver command iocb object.
7128 *
7129 * This function issues an abort iocb for the provided command
7130 * iocb. This function is called with hbalock held.
7131 * The function returns 0 when it fails due to memory allocation
7132 * failure or when the command iocb is an abort request.
7133 **/
dea31012005-04-17 16:05:31 -05007134int
James Smart2e0fef82007-06-17 19:56:36 -05007135lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7136 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007137{
James Smart2e0fef82007-06-17 19:56:36 -05007138 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007139 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007140 IOCB_t *icmd = NULL;
7141 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007142 int retval = IOCB_ERROR;
7143
James Smart92d7f7b2007-06-17 19:56:38 -05007144 /*
7145 * There are certain command types we don't want to abort. And we
7146 * don't want to abort commands that are already in the process of
7147 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007148 */
7149 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007150 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007151 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7152 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007153 return 0;
7154
James Smart858c9f62007-06-17 19:56:39 -05007155 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7156 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007157 */
James Smart858c9f62007-06-17 19:56:39 -05007158 if ((vport->load_flag & FC_UNLOADING) &&
7159 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007160 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7161 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7162 else
7163 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007164 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007165 }
dea31012005-04-17 16:05:31 -05007166
7167 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007168 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007169 if (abtsiocbp == NULL)
7170 return 0;
dea31012005-04-17 16:05:31 -05007171
James Smart07951072007-04-25 09:51:38 -04007172 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05007173 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04007174 */
7175 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7176
dea31012005-04-17 16:05:31 -05007177 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007178 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7179 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04007180 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007181 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04007182 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7183 }
James Smartda0436e2009-05-22 14:51:39 -04007184 else
7185 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007186 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007187 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007188
James Smart5ffc2662009-11-18 15:39:44 -05007189 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7190 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007191 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7192 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007193
James Smart2e0fef82007-06-17 19:56:36 -05007194 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007195 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7196 else
7197 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7198
7199 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007200
James Smarte8b62012007-08-02 11:10:09 -04007201 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7202 "0339 Abort xri x%x, original iotag x%x, "
7203 "abort cmd iotag x%x\n",
7204 iabt->un.acxri.abortContextTag,
7205 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007206 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007207
James Smartd7c255b2008-08-24 21:50:00 -04007208 if (retval)
7209 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007210abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007211 /*
7212 * Caller to this routine should check for IOCB_ERROR
7213 * and handle it properly. This routine no longer removes
7214 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007215 */
James Smart2e0fef82007-06-17 19:56:36 -05007216 return retval;
dea31012005-04-17 16:05:31 -05007217}
7218
James Smarte59058c2008-08-24 21:49:00 -04007219/**
James Smart3621a712009-04-06 18:47:14 -04007220 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007221 * @iocbq: Pointer to driver iocb object.
7222 * @vport: Pointer to driver virtual port object.
7223 * @tgt_id: SCSI ID of the target.
7224 * @lun_id: LUN ID of the scsi device.
7225 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7226 *
James Smart3621a712009-04-06 18:47:14 -04007227 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007228 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7229 * 0 if the filtering criteria is met for the given iocb and will return
7230 * 1 if the filtering criteria is not met.
7231 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7232 * given iocb is for the SCSI device specified by vport, tgt_id and
7233 * lun_id parameter.
7234 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7235 * given iocb is for the SCSI target specified by vport and tgt_id
7236 * parameters.
7237 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7238 * given iocb is for the SCSI host associated with the given vport.
7239 * This function is called with no locks held.
7240 **/
dea31012005-04-17 16:05:31 -05007241static int
James Smart51ef4c22007-08-02 11:10:31 -04007242lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7243 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007244 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007245{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007246 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007247 int rc = 1;
7248
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007249 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7250 return rc;
7251
James Smart51ef4c22007-08-02 11:10:31 -04007252 if (iocbq->vport != vport)
7253 return rc;
7254
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007255 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007256
James Smart495a7142008-06-14 22:52:59 -04007257 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007258 return rc;
7259
7260 switch (ctx_cmd) {
7261 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007262 if ((lpfc_cmd->rdata->pnode) &&
7263 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7264 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007265 rc = 0;
7266 break;
7267 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007268 if ((lpfc_cmd->rdata->pnode) &&
7269 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007270 rc = 0;
7271 break;
dea31012005-04-17 16:05:31 -05007272 case LPFC_CTX_HOST:
7273 rc = 0;
7274 break;
7275 default:
7276 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007277 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007278 break;
7279 }
7280
7281 return rc;
7282}
7283
James Smarte59058c2008-08-24 21:49:00 -04007284/**
James Smart3621a712009-04-06 18:47:14 -04007285 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007286 * @vport: Pointer to virtual port.
7287 * @tgt_id: SCSI ID of the target.
7288 * @lun_id: LUN ID of the scsi device.
7289 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7290 *
7291 * This function returns number of FCP commands pending for the vport.
7292 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7293 * commands pending on the vport associated with SCSI device specified
7294 * by tgt_id and lun_id parameters.
7295 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7296 * commands pending on the vport associated with SCSI target specified
7297 * by tgt_id parameter.
7298 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7299 * commands pending on the vport.
7300 * This function returns the number of iocbs which satisfy the filter.
7301 * This function is called without any lock held.
7302 **/
dea31012005-04-17 16:05:31 -05007303int
James Smart51ef4c22007-08-02 11:10:31 -04007304lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7305 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007306{
James Smart51ef4c22007-08-02 11:10:31 -04007307 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007308 struct lpfc_iocbq *iocbq;
7309 int sum, i;
dea31012005-04-17 16:05:31 -05007310
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007311 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7312 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007313
James Smart51ef4c22007-08-02 11:10:31 -04007314 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7315 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007316 sum++;
dea31012005-04-17 16:05:31 -05007317 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007318
dea31012005-04-17 16:05:31 -05007319 return sum;
7320}
7321
James Smarte59058c2008-08-24 21:49:00 -04007322/**
James Smart3621a712009-04-06 18:47:14 -04007323 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007324 * @phba: Pointer to HBA context object
7325 * @cmdiocb: Pointer to command iocb object.
7326 * @rspiocb: Pointer to response iocb object.
7327 *
7328 * This function is called when an aborted FCP iocb completes. This
7329 * function is called by the ring event handler with no lock held.
7330 * This function frees the iocb.
7331 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007332void
James Smart2e0fef82007-06-17 19:56:36 -05007333lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7334 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007335{
James Bottomley604a3e32005-10-29 10:28:33 -05007336 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007337 return;
7338}
7339
James Smarte59058c2008-08-24 21:49:00 -04007340/**
James Smart3621a712009-04-06 18:47:14 -04007341 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007342 * @vport: Pointer to virtual port.
7343 * @pring: Pointer to driver SLI ring object.
7344 * @tgt_id: SCSI ID of the target.
7345 * @lun_id: LUN ID of the scsi device.
7346 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7347 *
7348 * This function sends an abort command for every SCSI command
7349 * associated with the given virtual port pending on the ring
7350 * filtered by lpfc_sli_validate_fcp_iocb function.
7351 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7352 * FCP iocbs associated with lun specified by tgt_id and lun_id
7353 * parameters
7354 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7355 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7356 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7357 * FCP iocbs associated with virtual port.
7358 * This function returns number of iocbs it failed to abort.
7359 * This function is called with no locks held.
7360 **/
dea31012005-04-17 16:05:31 -05007361int
James Smart51ef4c22007-08-02 11:10:31 -04007362lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7363 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007364{
James Smart51ef4c22007-08-02 11:10:31 -04007365 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007366 struct lpfc_iocbq *iocbq;
7367 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007368 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007369 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007370 int i;
dea31012005-04-17 16:05:31 -05007371
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007372 for (i = 1; i <= phba->sli.last_iotag; i++) {
7373 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007374
James Smart51ef4c22007-08-02 11:10:31 -04007375 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007376 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007377 continue;
7378
7379 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007380 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007381 if (abtsiocb == NULL) {
7382 errcnt++;
7383 continue;
7384 }
dea31012005-04-17 16:05:31 -05007385
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007386 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007387 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7388 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007389 if (phba->sli_rev == LPFC_SLI_REV4)
7390 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7391 else
7392 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007393 abtsiocb->iocb.ulpLe = 1;
7394 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007395 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007396
James Smart5ffc2662009-11-18 15:39:44 -05007397 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7398 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05007399 if (iocbq->iocb_flag & LPFC_IO_FCP)
7400 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05007401
James Smart2e0fef82007-06-17 19:56:36 -05007402 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007403 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7404 else
7405 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7406
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007407 /* Setup callback routine and issue the command. */
7408 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007409 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7410 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007411 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007412 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007413 errcnt++;
7414 continue;
7415 }
7416 }
7417
7418 return errcnt;
7419}
7420
James Smarte59058c2008-08-24 21:49:00 -04007421/**
James Smart3621a712009-04-06 18:47:14 -04007422 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007423 * @phba: Pointer to HBA context object.
7424 * @cmdiocbq: Pointer to command iocb.
7425 * @rspiocbq: Pointer to response iocb.
7426 *
7427 * This function is the completion handler for iocbs issued using
7428 * lpfc_sli_issue_iocb_wait function. This function is called by the
7429 * ring event handler function without any lock held. This function
7430 * can be called from both worker thread context and interrupt
7431 * context. This function also can be called from other thread which
7432 * cleans up the SLI layer objects.
7433 * This function copy the contents of the response iocb to the
7434 * response iocb memory object provided by the caller of
7435 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7436 * sleeps for the iocb completion.
7437 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007438static void
7439lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7440 struct lpfc_iocbq *cmdiocbq,
7441 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007442{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007443 wait_queue_head_t *pdone_q;
7444 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007445
James Smart2e0fef82007-06-17 19:56:36 -05007446 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007447 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7448 if (cmdiocbq->context2 && rspiocbq)
7449 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7450 &rspiocbq->iocb, sizeof(IOCB_t));
7451
7452 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007453 if (pdone_q)
7454 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007455 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007456 return;
7457}
7458
James Smarte59058c2008-08-24 21:49:00 -04007459/**
James Smartd11e31d2009-06-10 17:23:06 -04007460 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7461 * @phba: Pointer to HBA context object..
7462 * @piocbq: Pointer to command iocb.
7463 * @flag: Flag to test.
7464 *
7465 * This routine grabs the hbalock and then test the iocb_flag to
7466 * see if the passed in flag is set.
7467 * Returns:
7468 * 1 if flag is set.
7469 * 0 if flag is not set.
7470 **/
7471static int
7472lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7473 struct lpfc_iocbq *piocbq, uint32_t flag)
7474{
7475 unsigned long iflags;
7476 int ret;
7477
7478 spin_lock_irqsave(&phba->hbalock, iflags);
7479 ret = piocbq->iocb_flag & flag;
7480 spin_unlock_irqrestore(&phba->hbalock, iflags);
7481 return ret;
7482
7483}
7484
7485/**
James Smart3621a712009-04-06 18:47:14 -04007486 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007487 * @phba: Pointer to HBA context object..
7488 * @pring: Pointer to sli ring.
7489 * @piocb: Pointer to command iocb.
7490 * @prspiocbq: Pointer to response iocb.
7491 * @timeout: Timeout in number of seconds.
7492 *
7493 * This function issues the iocb to firmware and waits for the
7494 * iocb to complete. If the iocb command is not
7495 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7496 * Caller should not free the iocb resources if this function
7497 * returns IOCB_TIMEDOUT.
7498 * The function waits for the iocb completion using an
7499 * non-interruptible wait.
7500 * This function will sleep while waiting for iocb completion.
7501 * So, this function should not be called from any context which
7502 * does not allow sleeping. Due to the same reason, this function
7503 * cannot be called with interrupt disabled.
7504 * This function assumes that the iocb completions occur while
7505 * this function sleep. So, this function cannot be called from
7506 * the thread which process iocb completion for this ring.
7507 * This function clears the iocb_flag of the iocb object before
7508 * issuing the iocb and the iocb completion handler sets this
7509 * flag and wakes this thread when the iocb completes.
7510 * The contents of the response iocb will be copied to prspiocbq
7511 * by the completion handler when the command completes.
7512 * This function returns IOCB_SUCCESS when success.
7513 * This function is called with no lock held.
7514 **/
dea31012005-04-17 16:05:31 -05007515int
James Smart2e0fef82007-06-17 19:56:36 -05007516lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007517 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007518 struct lpfc_iocbq *piocb,
7519 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007520 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007521{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007522 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007523 long timeleft, timeout_req = 0;
7524 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007525 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007526
7527 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007528 * If the caller has provided a response iocbq buffer, then context2
7529 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007530 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007531 if (prspiocbq) {
7532 if (piocb->context2)
7533 return IOCB_ERROR;
7534 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007535 }
7536
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007537 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7538 piocb->context_un.wait_queue = &done_q;
7539 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007540
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007541 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7542 creg_val = readl(phba->HCregaddr);
7543 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7544 writel(creg_val, phba->HCregaddr);
7545 readl(phba->HCregaddr); /* flush */
7546 }
7547
James Smartda0436e2009-05-22 14:51:39 -04007548 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007549 if (retval == IOCB_SUCCESS) {
7550 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007551 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007552 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007553 timeout_req);
dea31012005-04-17 16:05:31 -05007554
James Smart7054a602007-04-25 09:52:34 -04007555 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7556 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007557 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007558 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007560 "0338 IOCB wait timeout error - no "
7561 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007562 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007563 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007565 "0330 IOCB wake NOT set, "
7566 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007567 timeout, (timeleft / jiffies));
7568 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007569 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007570 } else {
7571 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007572 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007573 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007574 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007575 }
7576
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007577 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7578 creg_val = readl(phba->HCregaddr);
7579 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7580 writel(creg_val, phba->HCregaddr);
7581 readl(phba->HCregaddr); /* flush */
7582 }
7583
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007584 if (prspiocbq)
7585 piocb->context2 = NULL;
7586
7587 piocb->context_un.wait_queue = NULL;
7588 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007589 return retval;
7590}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007591
James Smarte59058c2008-08-24 21:49:00 -04007592/**
James Smart3621a712009-04-06 18:47:14 -04007593 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007594 * @phba: Pointer to HBA context object.
7595 * @pmboxq: Pointer to driver mailbox object.
7596 * @timeout: Timeout in number of seconds.
7597 *
7598 * This function issues the mailbox to firmware and waits for the
7599 * mailbox command to complete. If the mailbox command is not
7600 * completed within timeout seconds, it returns MBX_TIMEOUT.
7601 * The function waits for the mailbox completion using an
7602 * interruptible wait. If the thread is woken up due to a
7603 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7604 * should not free the mailbox resources, if this function returns
7605 * MBX_TIMEOUT.
7606 * This function will sleep while waiting for mailbox completion.
7607 * So, this function should not be called from any context which
7608 * does not allow sleeping. Due to the same reason, this function
7609 * cannot be called with interrupt disabled.
7610 * This function assumes that the mailbox completion occurs while
7611 * this function sleep. So, this function cannot be called from
7612 * the worker thread which processes mailbox completion.
7613 * This function is called in the context of HBA management
7614 * applications.
7615 * This function returns MBX_SUCCESS when successful.
7616 * This function is called with no lock held.
7617 **/
dea31012005-04-17 16:05:31 -05007618int
James Smart2e0fef82007-06-17 19:56:36 -05007619lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007620 uint32_t timeout)
7621{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007622 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007623 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007624 unsigned long flag;
dea31012005-04-17 16:05:31 -05007625
7626 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007627 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007628 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007629
James Smart495a7142008-06-14 22:52:59 -04007630 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007631 /* setup wake call as IOCB callback */
7632 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7633 /* setup context field to pass wait_queue pointer to wake function */
7634 pmboxq->context1 = &done_q;
7635
dea31012005-04-17 16:05:31 -05007636 /* now issue the command */
7637 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7638
7639 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007640 wait_event_interruptible_timeout(done_q,
7641 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7642 timeout * HZ);
7643
James Smart858c9f62007-06-17 19:56:39 -05007644 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007645 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007646 /*
7647 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7648 * else do not free the resources.
7649 */
7650 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007651 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007652 else {
James Smart7054a602007-04-25 09:52:34 -04007653 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007654 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7655 }
7656 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007657 }
7658
dea31012005-04-17 16:05:31 -05007659 return retval;
7660}
7661
James Smarte59058c2008-08-24 21:49:00 -04007662/**
James Smart3772a992009-05-22 14:50:54 -04007663 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007664 * @phba: Pointer to HBA context.
7665 *
James Smart3772a992009-05-22 14:50:54 -04007666 * This function is called to shutdown the driver's mailbox sub-system.
7667 * It first marks the mailbox sub-system is in a block state to prevent
7668 * the asynchronous mailbox command from issued off the pending mailbox
7669 * command queue. If the mailbox command sub-system shutdown is due to
7670 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7671 * the mailbox sub-system flush routine to forcefully bring down the
7672 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7673 * as with offline or HBA function reset), this routine will wait for the
7674 * outstanding mailbox command to complete before invoking the mailbox
7675 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007676 **/
James Smart3772a992009-05-22 14:50:54 -04007677void
7678lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007679{
James Smart3772a992009-05-22 14:50:54 -04007680 struct lpfc_sli *psli = &phba->sli;
7681 uint8_t actcmd = MBX_HEARTBEAT;
7682 unsigned long timeout;
7683
7684 spin_lock_irq(&phba->hbalock);
7685 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7686 spin_unlock_irq(&phba->hbalock);
7687
7688 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7689 spin_lock_irq(&phba->hbalock);
7690 if (phba->sli.mbox_active)
7691 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7692 spin_unlock_irq(&phba->hbalock);
7693 /* Determine how long we might wait for the active mailbox
7694 * command to be gracefully completed by firmware.
7695 */
7696 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7697 1000) + jiffies;
7698 while (phba->sli.mbox_active) {
7699 /* Check active mailbox complete status every 2ms */
7700 msleep(2);
7701 if (time_after(jiffies, timeout))
7702 /* Timeout, let the mailbox flush routine to
7703 * forcefully release active mailbox command
7704 */
7705 break;
7706 }
7707 }
7708 lpfc_sli_mbox_sys_flush(phba);
7709}
7710
7711/**
7712 * lpfc_sli_eratt_read - read sli-3 error attention events
7713 * @phba: Pointer to HBA context.
7714 *
7715 * This function is called to read the SLI3 device error attention registers
7716 * for possible error attention events. The caller must hold the hostlock
7717 * with spin_lock_irq().
7718 *
7719 * This fucntion returns 1 when there is Error Attention in the Host Attention
7720 * Register and returns 0 otherwise.
7721 **/
7722static int
7723lpfc_sli_eratt_read(struct lpfc_hba *phba)
7724{
James Smarted957682007-06-17 19:56:37 -05007725 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007726
James Smart3772a992009-05-22 14:50:54 -04007727 /* Read chip Host Attention (HA) register */
7728 ha_copy = readl(phba->HAregaddr);
7729 if (ha_copy & HA_ERATT) {
7730 /* Read host status register to retrieve error event */
7731 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007732
James Smart3772a992009-05-22 14:50:54 -04007733 /* Check if there is a deferred error condition is active */
7734 if ((HS_FFER1 & phba->work_hs) &&
7735 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7736 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007737 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007738 /* Clear all interrupt enable conditions */
7739 writel(0, phba->HCregaddr);
7740 readl(phba->HCregaddr);
7741 }
7742
7743 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007744 phba->work_ha |= HA_ERATT;
7745 /* Indicate polling handles this ERATT */
7746 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007747 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007748 }
James Smart3772a992009-05-22 14:50:54 -04007749 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007750}
7751
James Smarte59058c2008-08-24 21:49:00 -04007752/**
James Smartda0436e2009-05-22 14:51:39 -04007753 * lpfc_sli4_eratt_read - read sli-4 error attention events
7754 * @phba: Pointer to HBA context.
7755 *
7756 * This function is called to read the SLI4 device error attention registers
7757 * for possible error attention events. The caller must hold the hostlock
7758 * with spin_lock_irq().
7759 *
7760 * This fucntion returns 1 when there is Error Attention in the Host Attention
7761 * Register and returns 0 otherwise.
7762 **/
7763static int
7764lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7765{
7766 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smartda0436e2009-05-22 14:51:39 -04007767
7768 /* For now, use the SLI4 device internal unrecoverable error
7769 * registers for error attention. This can be changed later.
7770 */
James Smarta747c9c2009-11-18 15:41:10 -05007771 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7772 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7773 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7774 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7776 "1423 HBA Unrecoverable error: "
7777 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7778 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7779 uerr_sta_lo, uerr_sta_hi,
7780 phba->sli4_hba.ue_mask_lo,
7781 phba->sli4_hba.ue_mask_hi);
7782 phba->work_status[0] = uerr_sta_lo;
7783 phba->work_status[1] = uerr_sta_hi;
7784 /* Set the driver HA work bitmap */
7785 phba->work_ha |= HA_ERATT;
7786 /* Indicate polling handles this ERATT */
7787 phba->hba_flag |= HBA_ERATT_HANDLED;
7788 return 1;
James Smartda0436e2009-05-22 14:51:39 -04007789 }
7790 return 0;
7791}
7792
7793/**
James Smart3621a712009-04-06 18:47:14 -04007794 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007795 * @phba: Pointer to HBA context.
7796 *
James Smart3772a992009-05-22 14:50:54 -04007797 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007798 * error attention register bit for error attention events.
7799 *
7800 * This fucntion returns 1 when there is Error Attention in the Host Attention
7801 * Register and returns 0 otherwise.
7802 **/
7803int
7804lpfc_sli_check_eratt(struct lpfc_hba *phba)
7805{
7806 uint32_t ha_copy;
7807
7808 /* If somebody is waiting to handle an eratt, don't process it
7809 * here. The brdkill function will do this.
7810 */
7811 if (phba->link_flag & LS_IGNORE_ERATT)
7812 return 0;
7813
7814 /* Check if interrupt handler handles this ERATT */
7815 spin_lock_irq(&phba->hbalock);
7816 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7817 /* Interrupt handler has handled ERATT */
7818 spin_unlock_irq(&phba->hbalock);
7819 return 0;
7820 }
7821
James Smarta257bf92009-04-06 18:48:10 -04007822 /*
7823 * If there is deferred error attention, do not check for error
7824 * attention
7825 */
7826 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7827 spin_unlock_irq(&phba->hbalock);
7828 return 0;
7829 }
7830
James Smart3772a992009-05-22 14:50:54 -04007831 /* If PCI channel is offline, don't process it */
7832 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007833 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007834 return 0;
7835 }
7836
7837 switch (phba->sli_rev) {
7838 case LPFC_SLI_REV2:
7839 case LPFC_SLI_REV3:
7840 /* Read chip Host Attention (HA) register */
7841 ha_copy = lpfc_sli_eratt_read(phba);
7842 break;
James Smartda0436e2009-05-22 14:51:39 -04007843 case LPFC_SLI_REV4:
7844 /* Read devcie Uncoverable Error (UERR) registers */
7845 ha_copy = lpfc_sli4_eratt_read(phba);
7846 break;
James Smart3772a992009-05-22 14:50:54 -04007847 default:
7848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7849 "0299 Invalid SLI revision (%d)\n",
7850 phba->sli_rev);
7851 ha_copy = 0;
7852 break;
James Smart93996272008-08-24 21:50:30 -04007853 }
7854 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007855
7856 return ha_copy;
7857}
7858
7859/**
7860 * lpfc_intr_state_check - Check device state for interrupt handling
7861 * @phba: Pointer to HBA context.
7862 *
7863 * This inline routine checks whether a device or its PCI slot is in a state
7864 * that the interrupt should be handled.
7865 *
7866 * This function returns 0 if the device or the PCI slot is in a state that
7867 * interrupt should be handled, otherwise -EIO.
7868 */
7869static inline int
7870lpfc_intr_state_check(struct lpfc_hba *phba)
7871{
7872 /* If the pci channel is offline, ignore all the interrupts */
7873 if (unlikely(pci_channel_offline(phba->pcidev)))
7874 return -EIO;
7875
7876 /* Update device level interrupt statistics */
7877 phba->sli.slistat.sli_intr++;
7878
7879 /* Ignore all interrupts during initialization. */
7880 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7881 return -EIO;
7882
James Smart93996272008-08-24 21:50:30 -04007883 return 0;
7884}
7885
7886/**
James Smart3772a992009-05-22 14:50:54 -04007887 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007888 * @irq: Interrupt number.
7889 * @dev_id: The device context pointer.
7890 *
James Smart93996272008-08-24 21:50:30 -04007891 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007892 * service routine when device with SLI-3 interface spec is enabled with
7893 * MSI-X multi-message interrupt mode and there are slow-path events in
7894 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7895 * interrupt mode, this function is called as part of the device-level
7896 * interrupt handler. When the PCI slot is in error recovery or the HBA
7897 * is undergoing initialization, the interrupt handler will not process
7898 * the interrupt. The link attention and ELS ring attention events are
7899 * handled by the worker thread. The interrupt handler signals the worker
7900 * thread and returns for these events. This function is called without
7901 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007902 * structures.
7903 *
7904 * This function returns IRQ_HANDLED when interrupt is handled else it
7905 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007906 **/
dea31012005-04-17 16:05:31 -05007907irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007908lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007909{
James Smart2e0fef82007-06-17 19:56:36 -05007910 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -05007911 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007912 uint32_t work_ha_copy;
7913 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007914 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007915 uint32_t control;
7916
James Smart92d7f7b2007-06-17 19:56:38 -05007917 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007918 struct lpfc_vport *vport;
7919 struct lpfc_nodelist *ndlp;
7920 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007921 LPFC_MBOXQ_t *pmb;
7922 int rc;
7923
dea31012005-04-17 16:05:31 -05007924 /*
7925 * Get the driver's phba structure from the dev_id and
7926 * assume the HBA is not interrupting.
7927 */
James Smart93996272008-08-24 21:50:30 -04007928 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007929
7930 if (unlikely(!phba))
7931 return IRQ_NONE;
7932
dea31012005-04-17 16:05:31 -05007933 /*
James Smart93996272008-08-24 21:50:30 -04007934 * Stuff needs to be attented to when this function is invoked as an
7935 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007936 */
James Smart93996272008-08-24 21:50:30 -04007937 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007938 /* Check device state for handling interrupt */
7939 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007940 return IRQ_NONE;
7941 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007942 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007943 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007944 /* If somebody is waiting to handle an eratt don't process it
7945 * here. The brdkill function will do this.
7946 */
7947 if (phba->link_flag & LS_IGNORE_ERATT)
7948 ha_copy &= ~HA_ERATT;
7949 /* Check the need for handling ERATT in interrupt handler */
7950 if (ha_copy & HA_ERATT) {
7951 if (phba->hba_flag & HBA_ERATT_HANDLED)
7952 /* ERATT polling has handled ERATT */
7953 ha_copy &= ~HA_ERATT;
7954 else
7955 /* Indicate interrupt handler handles ERATT */
7956 phba->hba_flag |= HBA_ERATT_HANDLED;
7957 }
James Smarta257bf92009-04-06 18:48:10 -04007958
7959 /*
7960 * If there is deferred error attention, do not check for any
7961 * interrupt.
7962 */
7963 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007964 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007965 return IRQ_NONE;
7966 }
7967
James Smart93996272008-08-24 21:50:30 -04007968 /* Clear up only attention source related to slow-path */
James Smarta747c9c2009-11-18 15:41:10 -05007969 hc_copy = readl(phba->HCregaddr);
7970 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
7971 HC_LAINT_ENA | HC_ERINT_ENA),
7972 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04007973 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7974 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05007975 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04007976 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007977 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007978 } else
7979 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007980
dea31012005-04-17 16:05:31 -05007981 work_ha_copy = ha_copy & phba->work_ha_mask;
7982
James Smart93996272008-08-24 21:50:30 -04007983 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007984 if (work_ha_copy & HA_LATT) {
7985 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7986 /*
7987 * Turn off Link Attention interrupts
7988 * until CLEAR_LA done
7989 */
James Smart5b75da22008-12-04 22:39:35 -05007990 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007991 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7992 control = readl(phba->HCregaddr);
7993 control &= ~HC_LAINT_ENA;
7994 writel(control, phba->HCregaddr);
7995 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007996 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007997 }
7998 else
7999 work_ha_copy &= ~HA_LATT;
8000 }
8001
James Smart93996272008-08-24 21:50:30 -04008002 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05008003 /*
8004 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8005 * the only slow ring.
8006 */
8007 status = (work_ha_copy &
8008 (HA_RXMASK << (4*LPFC_ELS_RING)));
8009 status >>= (4*LPFC_ELS_RING);
8010 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05008011 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05008012 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04008013
8014 lpfc_debugfs_slow_ring_trc(phba,
8015 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8016 control, status,
8017 (uint32_t)phba->sli.slistat.sli_intr);
8018
James Smart858c9f62007-06-17 19:56:39 -05008019 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04008020 lpfc_debugfs_slow_ring_trc(phba,
8021 "ISR Disable ring:"
8022 "pwork:x%x hawork:x%x wait:x%x",
8023 phba->work_ha, work_ha_copy,
8024 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008025 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008026
James Smart858c9f62007-06-17 19:56:39 -05008027 control &=
8028 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05008029 writel(control, phba->HCregaddr);
8030 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05008031 }
James Smarta58cbd52007-08-02 11:09:43 -04008032 else {
8033 lpfc_debugfs_slow_ring_trc(phba,
8034 "ISR slow ring: pwork:"
8035 "x%x hawork:x%x wait:x%x",
8036 phba->work_ha, work_ha_copy,
8037 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04008038 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008039 }
James Smart5b75da22008-12-04 22:39:35 -05008040 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008041 }
8042 }
James Smart5b75da22008-12-04 22:39:35 -05008043 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008044 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008045 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008046 /*
8047 * Check if there is a deferred error condition
8048 * is active
8049 */
8050 if ((HS_FFER1 & phba->work_hs) &&
8051 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8052 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8053 phba->hba_flag |= DEFER_ERATT;
8054 /* Clear all interrupt enable conditions */
8055 writel(0, phba->HCregaddr);
8056 readl(phba->HCregaddr);
8057 }
8058 }
8059
James Smart93996272008-08-24 21:50:30 -04008060 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008061 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008062 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008063 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008064 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008065
8066 /* First check out the status word */
8067 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8068 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008069 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008070 /*
8071 * Stray Mailbox Interrupt, mbxCommand <cmd>
8072 * mbxStatus <status>
8073 */
James Smart09372822008-01-11 01:52:54 -05008074 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008075 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008076 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008077 "Interrupt mbxCommand x%x "
8078 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008079 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008080 pmbox->mbxCommand,
8081 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008082 /* clear mailbox attention bit */
8083 work_ha_copy &= ~HA_MBATT;
8084 } else {
James Smart97eab632008-04-07 10:16:05 -04008085 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008086 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008087 phba->last_completion_time = jiffies;
8088 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008089 if (pmb->mbox_cmpl) {
8090 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8091 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008092 }
James Smart09372822008-01-11 01:52:54 -05008093 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8094 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8095
8096 lpfc_debugfs_disc_trc(vport,
8097 LPFC_DISC_TRC_MBOX_VPORT,
8098 "MBOX dflt rpi: : "
8099 "status:x%x rpi:x%x",
8100 (uint32_t)pmbox->mbxStatus,
8101 pmbox->un.varWords[0], 0);
8102
8103 if (!pmbox->mbxStatus) {
8104 mp = (struct lpfc_dmabuf *)
8105 (pmb->context1);
8106 ndlp = (struct lpfc_nodelist *)
8107 pmb->context2;
8108
8109 /* Reg_LOGIN of dflt RPI was
8110 * successful. new lets get
8111 * rid of the RPI using the
8112 * same mbox buffer.
8113 */
8114 lpfc_unreg_login(phba,
8115 vport->vpi,
8116 pmbox->un.varWords[0],
8117 pmb);
8118 pmb->mbox_cmpl =
8119 lpfc_mbx_cmpl_dflt_rpi;
8120 pmb->context1 = mp;
8121 pmb->context2 = ndlp;
8122 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008123 rc = lpfc_sli_issue_mbox(phba,
8124 pmb,
8125 MBX_NOWAIT);
8126 if (rc != MBX_BUSY)
8127 lpfc_printf_log(phba,
8128 KERN_ERR,
8129 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008130 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -04008131 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -04008132 if (rc != MBX_NOT_FINISHED)
8133 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008134 }
8135 }
James Smart5b75da22008-12-04 22:39:35 -05008136 spin_lock_irqsave(
8137 &phba->pport->work_port_lock,
8138 iflag);
James Smart09372822008-01-11 01:52:54 -05008139 phba->pport->work_port_events &=
8140 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008141 spin_unlock_irqrestore(
8142 &phba->pport->work_port_lock,
8143 iflag);
James Smart09372822008-01-11 01:52:54 -05008144 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008145 }
James Smart97eab632008-04-07 10:16:05 -04008146 } else
James Smart5b75da22008-12-04 22:39:35 -05008147 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008148
James Smart92d7f7b2007-06-17 19:56:38 -05008149 if ((work_ha_copy & HA_MBATT) &&
8150 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008151send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008152 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008153 do {
8154 rc = lpfc_sli_issue_mbox(phba, NULL,
8155 MBX_NOWAIT);
8156 } while (rc == MBX_NOT_FINISHED);
8157 if (rc != MBX_SUCCESS)
8158 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8159 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -04008160 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -05008161 }
8162
James Smart5b75da22008-12-04 22:39:35 -05008163 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008164 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008165 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008166 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008167 }
James Smart93996272008-08-24 21:50:30 -04008168 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008169
James Smart3772a992009-05-22 14:50:54 -04008170} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008171
8172/**
James Smart3772a992009-05-22 14:50:54 -04008173 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008174 * @irq: Interrupt number.
8175 * @dev_id: The device context pointer.
8176 *
8177 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008178 * service routine when device with SLI-3 interface spec is enabled with
8179 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8180 * ring event in the HBA. However, when the device is enabled with either
8181 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8182 * device-level interrupt handler. When the PCI slot is in error recovery
8183 * or the HBA is undergoing initialization, the interrupt handler will not
8184 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8185 * the intrrupt context. This function is called without any lock held.
8186 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008187 *
8188 * This function returns IRQ_HANDLED when interrupt is handled else it
8189 * returns IRQ_NONE.
8190 **/
8191irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008192lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008193{
8194 struct lpfc_hba *phba;
8195 uint32_t ha_copy;
8196 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008197 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008198
8199 /* Get the driver's phba structure from the dev_id and
8200 * assume the HBA is not interrupting.
8201 */
8202 phba = (struct lpfc_hba *) dev_id;
8203
8204 if (unlikely(!phba))
8205 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008206
8207 /*
James Smart93996272008-08-24 21:50:30 -04008208 * Stuff needs to be attented to when this function is invoked as an
8209 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008210 */
James Smart93996272008-08-24 21:50:30 -04008211 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008212 /* Check device state for handling interrupt */
8213 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008214 return IRQ_NONE;
8215 /* Need to read HA REG for FCP ring and other ring events */
8216 ha_copy = readl(phba->HAregaddr);
8217 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008218 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008219 /*
8220 * If there is deferred error attention, do not check for
8221 * any interrupt.
8222 */
8223 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008224 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008225 return IRQ_NONE;
8226 }
James Smart93996272008-08-24 21:50:30 -04008227 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8228 phba->HAregaddr);
8229 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008230 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008231 } else
8232 ha_copy = phba->ha_copy;
8233
8234 /*
8235 * Process all events on FCP ring. Take the optimized path for FCP IO.
8236 */
8237 ha_copy &= ~(phba->work_ha_mask);
8238
8239 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008240 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008241 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008242 lpfc_sli_handle_fast_ring_event(phba,
8243 &phba->sli.ring[LPFC_FCP_RING],
8244 status);
James Smarta4bc3372006-12-02 13:34:16 -05008245
8246 if (phba->cfg_multi_ring_support == 2) {
8247 /*
James Smart93996272008-08-24 21:50:30 -04008248 * Process all events on extra ring. Take the optimized path
8249 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008250 */
James Smart93996272008-08-24 21:50:30 -04008251 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008252 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008253 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008254 lpfc_sli_handle_fast_ring_event(phba,
8255 &phba->sli.ring[LPFC_EXTRA_RING],
8256 status);
8257 }
8258 }
dea31012005-04-17 16:05:31 -05008259 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008260} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008261
James Smart93996272008-08-24 21:50:30 -04008262/**
James Smart3772a992009-05-22 14:50:54 -04008263 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008264 * @irq: Interrupt number.
8265 * @dev_id: The device context pointer.
8266 *
James Smart3772a992009-05-22 14:50:54 -04008267 * This function is the HBA device-level interrupt handler to device with
8268 * SLI-3 interface spec, called from the PCI layer when either MSI or
8269 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8270 * requires driver attention. This function invokes the slow-path interrupt
8271 * attention handling function and fast-path interrupt attention handling
8272 * function in turn to process the relevant HBA attention events. This
8273 * function is called without any lock held. It gets the hbalock to access
8274 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008275 *
8276 * This function returns IRQ_HANDLED when interrupt is handled, else it
8277 * returns IRQ_NONE.
8278 **/
8279irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008280lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008281{
8282 struct lpfc_hba *phba;
8283 irqreturn_t sp_irq_rc, fp_irq_rc;
8284 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -05008285 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -04008286
8287 /*
8288 * Get the driver's phba structure from the dev_id and
8289 * assume the HBA is not interrupting.
8290 */
8291 phba = (struct lpfc_hba *) dev_id;
8292
8293 if (unlikely(!phba))
8294 return IRQ_NONE;
8295
James Smart3772a992009-05-22 14:50:54 -04008296 /* Check device state for handling interrupt */
8297 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008298 return IRQ_NONE;
8299
8300 spin_lock(&phba->hbalock);
8301 phba->ha_copy = readl(phba->HAregaddr);
8302 if (unlikely(!phba->ha_copy)) {
8303 spin_unlock(&phba->hbalock);
8304 return IRQ_NONE;
8305 } else if (phba->ha_copy & HA_ERATT) {
8306 if (phba->hba_flag & HBA_ERATT_HANDLED)
8307 /* ERATT polling has handled ERATT */
8308 phba->ha_copy &= ~HA_ERATT;
8309 else
8310 /* Indicate interrupt handler handles ERATT */
8311 phba->hba_flag |= HBA_ERATT_HANDLED;
8312 }
8313
James Smarta257bf92009-04-06 18:48:10 -04008314 /*
8315 * If there is deferred error attention, do not check for any interrupt.
8316 */
8317 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8318 spin_unlock_irq(&phba->hbalock);
8319 return IRQ_NONE;
8320 }
8321
James Smart93996272008-08-24 21:50:30 -04008322 /* Clear attention sources except link and error attentions */
James Smarta747c9c2009-11-18 15:41:10 -05008323 hc_copy = readl(phba->HCregaddr);
8324 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8325 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8326 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008327 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -05008328 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -04008329 readl(phba->HAregaddr); /* flush */
8330 spin_unlock(&phba->hbalock);
8331
8332 /*
8333 * Invokes slow-path host attention interrupt handling as appropriate.
8334 */
8335
8336 /* status of events with mailbox and link attention */
8337 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8338
8339 /* status of events with ELS ring */
8340 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8341 status2 >>= (4*LPFC_ELS_RING);
8342
8343 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008344 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008345 else
8346 sp_irq_rc = IRQ_NONE;
8347
8348 /*
8349 * Invoke fast-path host attention interrupt handling as appropriate.
8350 */
8351
8352 /* status of events with FCP ring */
8353 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8354 status1 >>= (4*LPFC_FCP_RING);
8355
8356 /* status of events with extra ring */
8357 if (phba->cfg_multi_ring_support == 2) {
8358 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8359 status2 >>= (4*LPFC_EXTRA_RING);
8360 } else
8361 status2 = 0;
8362
8363 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008364 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008365 else
8366 fp_irq_rc = IRQ_NONE;
8367
8368 /* Return device-level interrupt handling status */
8369 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008370} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008371
8372/**
8373 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8374 * @phba: pointer to lpfc hba data structure.
8375 *
8376 * This routine is invoked by the worker thread to process all the pending
8377 * SLI4 FCP abort XRI events.
8378 **/
8379void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8380{
8381 struct lpfc_cq_event *cq_event;
8382
8383 /* First, declare the fcp xri abort event has been handled */
8384 spin_lock_irq(&phba->hbalock);
8385 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8386 spin_unlock_irq(&phba->hbalock);
8387 /* Now, handle all the fcp xri abort events */
8388 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8389 /* Get the first event from the head of the event queue */
8390 spin_lock_irq(&phba->hbalock);
8391 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8392 cq_event, struct lpfc_cq_event, list);
8393 spin_unlock_irq(&phba->hbalock);
8394 /* Notify aborted XRI for FCP work queue */
8395 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8396 /* Free the event processed back to the free pool */
8397 lpfc_sli4_cq_event_release(phba, cq_event);
8398 }
8399}
8400
8401/**
8402 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8403 * @phba: pointer to lpfc hba data structure.
8404 *
8405 * This routine is invoked by the worker thread to process all the pending
8406 * SLI4 els abort xri events.
8407 **/
8408void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8409{
8410 struct lpfc_cq_event *cq_event;
8411
8412 /* First, declare the els xri abort event has been handled */
8413 spin_lock_irq(&phba->hbalock);
8414 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8415 spin_unlock_irq(&phba->hbalock);
8416 /* Now, handle all the els xri abort events */
8417 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8418 /* Get the first event from the head of the event queue */
8419 spin_lock_irq(&phba->hbalock);
8420 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8421 cq_event, struct lpfc_cq_event, list);
8422 spin_unlock_irq(&phba->hbalock);
8423 /* Notify aborted XRI for ELS work queue */
8424 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8425 /* Free the event processed back to the free pool */
8426 lpfc_sli4_cq_event_release(phba, cq_event);
8427 }
8428}
8429
James Smart341af102010-01-26 23:07:37 -05008430/**
8431 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8432 * @phba: pointer to lpfc hba data structure
8433 * @pIocbIn: pointer to the rspiocbq
8434 * @pIocbOut: pointer to the cmdiocbq
8435 * @wcqe: pointer to the complete wcqe
8436 *
8437 * This routine transfers the fields of a command iocbq to a response iocbq
8438 * by copying all the IOCB fields from command iocbq and transferring the
8439 * completion status information from the complete wcqe.
8440 **/
James Smart4f774512009-05-22 14:52:35 -04008441static void
James Smart341af102010-01-26 23:07:37 -05008442lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8443 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -04008444 struct lpfc_iocbq *pIocbOut,
8445 struct lpfc_wcqe_complete *wcqe)
8446{
James Smart341af102010-01-26 23:07:37 -05008447 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008448 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8449
8450 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8451 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -04008452 /* Map WCQE parameters into irspiocb parameters */
8453 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8454 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8455 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8456 pIocbIn->iocb.un.fcpi.fcpi_parm =
8457 pIocbOut->iocb.un.fcpi.fcpi_parm -
8458 wcqe->total_data_placed;
8459 else
8460 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008461 else {
James Smart4f774512009-05-22 14:52:35 -04008462 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -05008463 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8464 }
James Smart341af102010-01-26 23:07:37 -05008465
8466 /* Pick up HBA exchange busy condition */
8467 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8468 spin_lock_irqsave(&phba->hbalock, iflags);
8469 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8470 spin_unlock_irqrestore(&phba->hbalock, iflags);
8471 }
James Smart4f774512009-05-22 14:52:35 -04008472}
8473
8474/**
James Smart45ed1192009-10-02 15:17:02 -04008475 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8476 * @phba: Pointer to HBA context object.
8477 * @wcqe: Pointer to work-queue completion queue entry.
8478 *
8479 * This routine handles an ELS work-queue completion event and construct
8480 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8481 * discovery engine to handle.
8482 *
8483 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8484 **/
8485static struct lpfc_iocbq *
8486lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8487 struct lpfc_iocbq *irspiocbq)
8488{
8489 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8490 struct lpfc_iocbq *cmdiocbq;
8491 struct lpfc_wcqe_complete *wcqe;
8492 unsigned long iflags;
8493
8494 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8495 spin_lock_irqsave(&phba->hbalock, iflags);
8496 pring->stats.iocb_event++;
8497 /* Look up the ELS command IOCB and create pseudo response IOCB */
8498 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8499 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8500 spin_unlock_irqrestore(&phba->hbalock, iflags);
8501
8502 if (unlikely(!cmdiocbq)) {
8503 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8504 "0386 ELS complete with no corresponding "
8505 "cmdiocb: iotag (%d)\n",
8506 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8507 lpfc_sli_release_iocbq(phba, irspiocbq);
8508 return NULL;
8509 }
8510
8511 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05008512 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -04008513
8514 return irspiocbq;
8515}
8516
8517/**
James Smart04c68492009-05-22 14:52:52 -04008518 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8519 * @phba: Pointer to HBA context object.
8520 * @cqe: Pointer to mailbox completion queue entry.
8521 *
8522 * This routine process a mailbox completion queue entry with asynchrous
8523 * event.
8524 *
8525 * Return: true if work posted to worker thread, otherwise false.
8526 **/
8527static bool
8528lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8529{
8530 struct lpfc_cq_event *cq_event;
8531 unsigned long iflags;
8532
8533 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8534 "0392 Async Event: word0:x%x, word1:x%x, "
8535 "word2:x%x, word3:x%x\n", mcqe->word0,
8536 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8537
8538 /* Allocate a new internal CQ_EVENT entry */
8539 cq_event = lpfc_sli4_cq_event_alloc(phba);
8540 if (!cq_event) {
8541 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8542 "0394 Failed to allocate CQ_EVENT entry\n");
8543 return false;
8544 }
8545
8546 /* Move the CQE into an asynchronous event entry */
8547 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8548 spin_lock_irqsave(&phba->hbalock, iflags);
8549 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8550 /* Set the async event flag */
8551 phba->hba_flag |= ASYNC_EVENT;
8552 spin_unlock_irqrestore(&phba->hbalock, iflags);
8553
8554 return true;
8555}
8556
8557/**
8558 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8559 * @phba: Pointer to HBA context object.
8560 * @cqe: Pointer to mailbox completion queue entry.
8561 *
8562 * This routine process a mailbox completion queue entry with mailbox
8563 * completion event.
8564 *
8565 * Return: true if work posted to worker thread, otherwise false.
8566 **/
8567static bool
8568lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8569{
8570 uint32_t mcqe_status;
8571 MAILBOX_t *mbox, *pmbox;
8572 struct lpfc_mqe *mqe;
8573 struct lpfc_vport *vport;
8574 struct lpfc_nodelist *ndlp;
8575 struct lpfc_dmabuf *mp;
8576 unsigned long iflags;
8577 LPFC_MBOXQ_t *pmb;
8578 bool workposted = false;
8579 int rc;
8580
8581 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8582 if (!bf_get(lpfc_trailer_completed, mcqe))
8583 goto out_no_mqe_complete;
8584
8585 /* Get the reference to the active mbox command */
8586 spin_lock_irqsave(&phba->hbalock, iflags);
8587 pmb = phba->sli.mbox_active;
8588 if (unlikely(!pmb)) {
8589 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8590 "1832 No pending MBOX command to handle\n");
8591 spin_unlock_irqrestore(&phba->hbalock, iflags);
8592 goto out_no_mqe_complete;
8593 }
8594 spin_unlock_irqrestore(&phba->hbalock, iflags);
8595 mqe = &pmb->u.mqe;
8596 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8597 mbox = phba->mbox;
8598 vport = pmb->vport;
8599
8600 /* Reset heartbeat timer */
8601 phba->last_completion_time = jiffies;
8602 del_timer(&phba->sli.mbox_tmo);
8603
8604 /* Move mbox data to caller's mailbox region, do endian swapping */
8605 if (pmb->mbox_cmpl && mbox)
8606 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8607 /* Set the mailbox status with SLI4 range 0x4000 */
8608 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8609 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8610 bf_set(lpfc_mqe_status, mqe,
8611 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8612
8613 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8614 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8615 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8616 "MBOX dflt rpi: status:x%x rpi:x%x",
8617 mcqe_status,
8618 pmbox->un.varWords[0], 0);
8619 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8620 mp = (struct lpfc_dmabuf *)(pmb->context1);
8621 ndlp = (struct lpfc_nodelist *)pmb->context2;
8622 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8623 * RID of the PPI using the same mbox buffer.
8624 */
8625 lpfc_unreg_login(phba, vport->vpi,
8626 pmbox->un.varWords[0], pmb);
8627 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8628 pmb->context1 = mp;
8629 pmb->context2 = ndlp;
8630 pmb->vport = vport;
8631 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8632 if (rc != MBX_BUSY)
8633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8634 LOG_SLI, "0385 rc should "
8635 "have been MBX_BUSY\n");
8636 if (rc != MBX_NOT_FINISHED)
8637 goto send_current_mbox;
8638 }
8639 }
8640 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8641 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8642 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8643
8644 /* There is mailbox completion work to do */
8645 spin_lock_irqsave(&phba->hbalock, iflags);
8646 __lpfc_mbox_cmpl_put(phba, pmb);
8647 phba->work_ha |= HA_MBATT;
8648 spin_unlock_irqrestore(&phba->hbalock, iflags);
8649 workposted = true;
8650
8651send_current_mbox:
8652 spin_lock_irqsave(&phba->hbalock, iflags);
8653 /* Release the mailbox command posting token */
8654 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8655 /* Setting active mailbox pointer need to be in sync to flag clear */
8656 phba->sli.mbox_active = NULL;
8657 spin_unlock_irqrestore(&phba->hbalock, iflags);
8658 /* Wake up worker thread to post the next pending mailbox command */
8659 lpfc_worker_wake_up(phba);
8660out_no_mqe_complete:
8661 if (bf_get(lpfc_trailer_consumed, mcqe))
8662 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8663 return workposted;
8664}
8665
8666/**
8667 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8668 * @phba: Pointer to HBA context object.
8669 * @cqe: Pointer to mailbox completion queue entry.
8670 *
8671 * This routine process a mailbox completion queue entry, it invokes the
8672 * proper mailbox complete handling or asynchrous event handling routine
8673 * according to the MCQE's async bit.
8674 *
8675 * Return: true if work posted to worker thread, otherwise false.
8676 **/
8677static bool
8678lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8679{
8680 struct lpfc_mcqe mcqe;
8681 bool workposted;
8682
8683 /* Copy the mailbox MCQE and convert endian order as needed */
8684 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8685
8686 /* Invoke the proper event handling routine */
8687 if (!bf_get(lpfc_trailer_async, &mcqe))
8688 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8689 else
8690 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8691 return workposted;
8692}
8693
8694/**
James Smart4f774512009-05-22 14:52:35 -04008695 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8696 * @phba: Pointer to HBA context object.
8697 * @wcqe: Pointer to work-queue completion queue entry.
8698 *
8699 * This routine handles an ELS work-queue completion event.
8700 *
8701 * Return: true if work posted to worker thread, otherwise false.
8702 **/
8703static bool
8704lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8705 struct lpfc_wcqe_complete *wcqe)
8706{
James Smart4f774512009-05-22 14:52:35 -04008707 struct lpfc_iocbq *irspiocbq;
8708 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -04008709
James Smart45ed1192009-10-02 15:17:02 -04008710 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -04008711 irspiocbq = lpfc_sli_get_iocbq(phba);
8712 if (!irspiocbq) {
8713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8714 "0387 Failed to allocate an iocbq\n");
James Smart45ed1192009-10-02 15:17:02 -04008715 return false;
James Smart4f774512009-05-22 14:52:35 -04008716 }
James Smart4f774512009-05-22 14:52:35 -04008717
James Smart45ed1192009-10-02 15:17:02 -04008718 /* Save off the slow-path queue event for work thread to process */
8719 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -04008720 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008721 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008722 &phba->sli4_hba.sp_queue_event);
8723 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -04008724 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -04008725
James Smart45ed1192009-10-02 15:17:02 -04008726 return true;
James Smart4f774512009-05-22 14:52:35 -04008727}
8728
8729/**
8730 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8731 * @phba: Pointer to HBA context object.
8732 * @wcqe: Pointer to work-queue completion queue entry.
8733 *
8734 * This routine handles slow-path WQ entry comsumed event by invoking the
8735 * proper WQ release routine to the slow-path WQ.
8736 **/
8737static void
8738lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8739 struct lpfc_wcqe_release *wcqe)
8740{
8741 /* Check for the slow-path ELS work queue */
8742 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8743 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8744 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8745 else
8746 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8747 "2579 Slow-path wqe consume event carries "
8748 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8749 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8750 phba->sli4_hba.els_wq->queue_id);
8751}
8752
8753/**
8754 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8755 * @phba: Pointer to HBA context object.
8756 * @cq: Pointer to a WQ completion queue.
8757 * @wcqe: Pointer to work-queue completion queue entry.
8758 *
8759 * This routine handles an XRI abort event.
8760 *
8761 * Return: true if work posted to worker thread, otherwise false.
8762 **/
8763static bool
8764lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8765 struct lpfc_queue *cq,
8766 struct sli4_wcqe_xri_aborted *wcqe)
8767{
8768 bool workposted = false;
8769 struct lpfc_cq_event *cq_event;
8770 unsigned long iflags;
8771
8772 /* Allocate a new internal CQ_EVENT entry */
8773 cq_event = lpfc_sli4_cq_event_alloc(phba);
8774 if (!cq_event) {
8775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8776 "0602 Failed to allocate CQ_EVENT entry\n");
8777 return false;
8778 }
8779
8780 /* Move the CQE into the proper xri abort event list */
8781 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8782 switch (cq->subtype) {
8783 case LPFC_FCP:
8784 spin_lock_irqsave(&phba->hbalock, iflags);
8785 list_add_tail(&cq_event->list,
8786 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8787 /* Set the fcp xri abort event flag */
8788 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8789 spin_unlock_irqrestore(&phba->hbalock, iflags);
8790 workposted = true;
8791 break;
8792 case LPFC_ELS:
8793 spin_lock_irqsave(&phba->hbalock, iflags);
8794 list_add_tail(&cq_event->list,
8795 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8796 /* Set the els xri abort event flag */
8797 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8798 spin_unlock_irqrestore(&phba->hbalock, iflags);
8799 workposted = true;
8800 break;
8801 default:
8802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8803 "0603 Invalid work queue CQE subtype (x%x)\n",
8804 cq->subtype);
8805 workposted = false;
8806 break;
8807 }
8808 return workposted;
8809}
8810
8811/**
James Smart4d9ab992009-10-02 15:16:39 -04008812 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008813 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008814 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008815 *
James Smart4d9ab992009-10-02 15:16:39 -04008816 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008817 *
8818 * Return: true if work posted to worker thread, otherwise false.
8819 **/
8820static bool
James Smart4d9ab992009-10-02 15:16:39 -04008821lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8822{
8823 bool workposted = false;
8824 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8825 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8826 struct hbq_dmabuf *dma_buf;
8827 uint32_t status;
8828 unsigned long iflags;
8829
James Smart4d9ab992009-10-02 15:16:39 -04008830 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8831 goto out;
8832
8833 status = bf_get(lpfc_rcqe_status, rcqe);
8834 switch (status) {
8835 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8836 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8837 "2537 Receive Frame Truncated!!\n");
8838 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -05008839 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -04008840 spin_lock_irqsave(&phba->hbalock, iflags);
8841 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8842 if (!dma_buf) {
8843 spin_unlock_irqrestore(&phba->hbalock, iflags);
8844 goto out;
8845 }
8846 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8847 /* save off the frame for the word thread to process */
8848 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -04008849 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -04008850 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -04008851 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -04008852 spin_unlock_irqrestore(&phba->hbalock, iflags);
8853 workposted = true;
8854 break;
8855 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8856 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8857 /* Post more buffers if possible */
8858 spin_lock_irqsave(&phba->hbalock, iflags);
8859 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8860 spin_unlock_irqrestore(&phba->hbalock, iflags);
8861 workposted = true;
8862 break;
8863 }
8864out:
8865 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -04008866}
8867
8868/**
8869 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8870 * @phba: Pointer to HBA context object.
8871 * @cq: Pointer to the completion queue.
8872 * @wcqe: Pointer to a completion queue entry.
8873 *
8874 * This routine process a slow-path work-queue or recieve queue completion queue
8875 * entry.
8876 *
8877 * Return: true if work posted to worker thread, otherwise false.
8878 **/
8879static bool
8880lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008881 struct lpfc_cqe *cqe)
8882{
James Smart45ed1192009-10-02 15:17:02 -04008883 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -04008884 bool workposted = false;
8885
8886 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -04008887 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -04008888
8889 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -04008890 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -04008891 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -04008892 /* Process the WQ/RQ complete event */
James Smart4f774512009-05-22 14:52:35 -04008893 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008894 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008895 break;
8896 case CQE_CODE_RELEASE_WQE:
8897 /* Process the WQ release event */
8898 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008899 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008900 break;
8901 case CQE_CODE_XRI_ABORTED:
8902 /* Process the WQ XRI abort event */
8903 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -04008904 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -04008905 break;
James Smart4d9ab992009-10-02 15:16:39 -04008906 case CQE_CODE_RECEIVE:
8907 /* Process the RQ event */
8908 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -04008909 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -04008910 break;
James Smart4f774512009-05-22 14:52:35 -04008911 default:
8912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8913 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -04008914 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -04008915 break;
8916 }
8917 return workposted;
8918}
8919
8920/**
James Smart4f774512009-05-22 14:52:35 -04008921 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8922 * @phba: Pointer to HBA context object.
8923 * @eqe: Pointer to fast-path event queue entry.
8924 *
8925 * This routine process a event queue entry from the slow-path event queue.
8926 * It will check the MajorCode and MinorCode to determine this is for a
8927 * completion event on a completion queue, if not, an error shall be logged
8928 * and just return. Otherwise, it will get to the corresponding completion
8929 * queue and process all the entries on that completion queue, rearm the
8930 * completion queue, and then return.
8931 *
8932 **/
8933static void
8934lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8935{
8936 struct lpfc_queue *cq = NULL, *childq, *speq;
8937 struct lpfc_cqe *cqe;
8938 bool workposted = false;
8939 int ecount = 0;
8940 uint16_t cqid;
8941
James Smart28baac72010-02-12 14:42:03 -05008942 if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
James Smart4f774512009-05-22 14:52:35 -04008943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8944 "0359 Not a valid slow-path completion "
8945 "event: majorcode=x%x, minorcode=x%x\n",
8946 bf_get(lpfc_eqe_major_code, eqe),
8947 bf_get(lpfc_eqe_minor_code, eqe));
8948 return;
8949 }
8950
8951 /* Get the reference to the corresponding CQ */
8952 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8953
8954 /* Search for completion queue pointer matching this cqid */
8955 speq = phba->sli4_hba.sp_eq;
8956 list_for_each_entry(childq, &speq->child_list, list) {
8957 if (childq->queue_id == cqid) {
8958 cq = childq;
8959 break;
8960 }
8961 }
8962 if (unlikely(!cq)) {
8963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8964 "0365 Slow-path CQ identifier (%d) does "
8965 "not exist\n", cqid);
8966 return;
8967 }
8968
8969 /* Process all the entries to the CQ */
8970 switch (cq->type) {
8971 case LPFC_MCQ:
8972 while ((cqe = lpfc_sli4_cq_get(cq))) {
8973 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8974 if (!(++ecount % LPFC_GET_QE_REL_INT))
8975 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8976 }
8977 break;
8978 case LPFC_WCQ:
8979 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04008980 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04008981 if (!(++ecount % LPFC_GET_QE_REL_INT))
8982 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8983 }
8984 break;
8985 default:
8986 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8987 "0370 Invalid completion queue type (%d)\n",
8988 cq->type);
8989 return;
8990 }
8991
8992 /* Catch the no cq entry condition, log an error */
8993 if (unlikely(ecount == 0))
8994 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8995 "0371 No entry from the CQ: identifier "
8996 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8997
8998 /* In any case, flash and re-arm the RCQ */
8999 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9000
9001 /* wake up worker thread if there are works to be done */
9002 if (workposted)
9003 lpfc_worker_wake_up(phba);
9004}
9005
9006/**
9007 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9008 * @eqe: Pointer to fast-path completion queue entry.
9009 *
9010 * This routine process a fast-path work queue completion entry from fast-path
9011 * event queue for FCP command response completion.
9012 **/
9013static void
9014lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9015 struct lpfc_wcqe_complete *wcqe)
9016{
9017 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9018 struct lpfc_iocbq *cmdiocbq;
9019 struct lpfc_iocbq irspiocbq;
9020 unsigned long iflags;
9021
9022 spin_lock_irqsave(&phba->hbalock, iflags);
9023 pring->stats.iocb_event++;
9024 spin_unlock_irqrestore(&phba->hbalock, iflags);
9025
9026 /* Check for response status */
9027 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9028 /* If resource errors reported from HBA, reduce queue
9029 * depth of the SCSI device.
9030 */
9031 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9032 IOSTAT_LOCAL_REJECT) &&
9033 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9034 phba->lpfc_rampdown_queue_depth(phba);
9035 }
9036 /* Log the error status */
9037 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9038 "0373 FCP complete error: status=x%x, "
9039 "hw_status=x%x, total_data_specified=%d, "
9040 "parameter=x%x, word3=x%x\n",
9041 bf_get(lpfc_wcqe_c_status, wcqe),
9042 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9043 wcqe->total_data_placed, wcqe->parameter,
9044 wcqe->word3);
9045 }
9046
9047 /* Look up the FCP command IOCB and create pseudo response IOCB */
9048 spin_lock_irqsave(&phba->hbalock, iflags);
9049 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9050 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9051 spin_unlock_irqrestore(&phba->hbalock, iflags);
9052 if (unlikely(!cmdiocbq)) {
9053 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9054 "0374 FCP complete with no corresponding "
9055 "cmdiocb: iotag (%d)\n",
9056 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9057 return;
9058 }
9059 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9061 "0375 FCP cmdiocb not callback function "
9062 "iotag: (%d)\n",
9063 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9064 return;
9065 }
9066
9067 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -05009068 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -04009069
9070 /* Pass the cmd_iocb and the rsp state to the upper layer */
9071 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9072}
9073
9074/**
9075 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9076 * @phba: Pointer to HBA context object.
9077 * @cq: Pointer to completion queue.
9078 * @wcqe: Pointer to work-queue completion queue entry.
9079 *
9080 * This routine handles an fast-path WQ entry comsumed event by invoking the
9081 * proper WQ release routine to the slow-path WQ.
9082 **/
9083static void
9084lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9085 struct lpfc_wcqe_release *wcqe)
9086{
9087 struct lpfc_queue *childwq;
9088 bool wqid_matched = false;
9089 uint16_t fcp_wqid;
9090
9091 /* Check for fast-path FCP work queue release */
9092 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9093 list_for_each_entry(childwq, &cq->child_list, list) {
9094 if (childwq->queue_id == fcp_wqid) {
9095 lpfc_sli4_wq_release(childwq,
9096 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9097 wqid_matched = true;
9098 break;
9099 }
9100 }
9101 /* Report warning log message if no match found */
9102 if (wqid_matched != true)
9103 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9104 "2580 Fast-path wqe consume event carries "
9105 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9106}
9107
9108/**
9109 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9110 * @cq: Pointer to the completion queue.
9111 * @eqe: Pointer to fast-path completion queue entry.
9112 *
9113 * This routine process a fast-path work queue completion entry from fast-path
9114 * event queue for FCP command response completion.
9115 **/
9116static int
9117lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9118 struct lpfc_cqe *cqe)
9119{
9120 struct lpfc_wcqe_release wcqe;
9121 bool workposted = false;
9122
9123 /* Copy the work queue CQE and convert endian order if needed */
9124 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9125
9126 /* Check and process for different type of WCQE and dispatch */
9127 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9128 case CQE_CODE_COMPL_WQE:
9129 /* Process the WQ complete event */
9130 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9131 (struct lpfc_wcqe_complete *)&wcqe);
9132 break;
9133 case CQE_CODE_RELEASE_WQE:
9134 /* Process the WQ release event */
9135 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9136 (struct lpfc_wcqe_release *)&wcqe);
9137 break;
9138 case CQE_CODE_XRI_ABORTED:
9139 /* Process the WQ XRI abort event */
9140 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9141 (struct sli4_wcqe_xri_aborted *)&wcqe);
9142 break;
9143 default:
9144 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9145 "0144 Not a valid WCQE code: x%x\n",
9146 bf_get(lpfc_wcqe_c_code, &wcqe));
9147 break;
9148 }
9149 return workposted;
9150}
9151
9152/**
9153 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9154 * @phba: Pointer to HBA context object.
9155 * @eqe: Pointer to fast-path event queue entry.
9156 *
9157 * This routine process a event queue entry from the fast-path event queue.
9158 * It will check the MajorCode and MinorCode to determine this is for a
9159 * completion event on a completion queue, if not, an error shall be logged
9160 * and just return. Otherwise, it will get to the corresponding completion
9161 * queue and process all the entries on the completion queue, rearm the
9162 * completion queue, and then return.
9163 **/
9164static void
9165lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9166 uint32_t fcp_cqidx)
9167{
9168 struct lpfc_queue *cq;
9169 struct lpfc_cqe *cqe;
9170 bool workposted = false;
9171 uint16_t cqid;
9172 int ecount = 0;
9173
James Smart28baac72010-02-12 14:42:03 -05009174 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -04009175 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9176 "0366 Not a valid fast-path completion "
9177 "event: majorcode=x%x, minorcode=x%x\n",
9178 bf_get(lpfc_eqe_major_code, eqe),
9179 bf_get(lpfc_eqe_minor_code, eqe));
9180 return;
9181 }
9182
9183 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9184 if (unlikely(!cq)) {
9185 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9186 "0367 Fast-path completion queue does not "
9187 "exist\n");
9188 return;
9189 }
9190
9191 /* Get the reference to the corresponding CQ */
9192 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9193 if (unlikely(cqid != cq->queue_id)) {
9194 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9195 "0368 Miss-matched fast-path completion "
9196 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9197 cqid, cq->queue_id);
9198 return;
9199 }
9200
9201 /* Process all the entries to the CQ */
9202 while ((cqe = lpfc_sli4_cq_get(cq))) {
9203 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9204 if (!(++ecount % LPFC_GET_QE_REL_INT))
9205 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9206 }
9207
9208 /* Catch the no cq entry condition */
9209 if (unlikely(ecount == 0))
9210 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9211 "0369 No entry from fast-path completion "
9212 "queue fcpcqid=%d\n", cq->queue_id);
9213
9214 /* In any case, flash and re-arm the CQ */
9215 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9216
9217 /* wake up worker thread if there are works to be done */
9218 if (workposted)
9219 lpfc_worker_wake_up(phba);
9220}
9221
9222static void
9223lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9224{
9225 struct lpfc_eqe *eqe;
9226
9227 /* walk all the EQ entries and drop on the floor */
9228 while ((eqe = lpfc_sli4_eq_get(eq)))
9229 ;
9230
9231 /* Clear and re-arm the EQ */
9232 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9233}
9234
9235/**
9236 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9237 * @irq: Interrupt number.
9238 * @dev_id: The device context pointer.
9239 *
9240 * This function is directly called from the PCI layer as an interrupt
9241 * service routine when device with SLI-4 interface spec is enabled with
9242 * MSI-X multi-message interrupt mode and there are slow-path events in
9243 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9244 * interrupt mode, this function is called as part of the device-level
9245 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9246 * undergoing initialization, the interrupt handler will not process the
9247 * interrupt. The link attention and ELS ring attention events are handled
9248 * by the worker thread. The interrupt handler signals the worker thread
9249 * and returns for these events. This function is called without any lock
9250 * held. It gets the hbalock to access and update SLI data structures.
9251 *
9252 * This function returns IRQ_HANDLED when interrupt is handled else it
9253 * returns IRQ_NONE.
9254 **/
9255irqreturn_t
9256lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9257{
9258 struct lpfc_hba *phba;
9259 struct lpfc_queue *speq;
9260 struct lpfc_eqe *eqe;
9261 unsigned long iflag;
9262 int ecount = 0;
9263
9264 /*
9265 * Get the driver's phba structure from the dev_id
9266 */
9267 phba = (struct lpfc_hba *)dev_id;
9268
9269 if (unlikely(!phba))
9270 return IRQ_NONE;
9271
9272 /* Get to the EQ struct associated with this vector */
9273 speq = phba->sli4_hba.sp_eq;
9274
9275 /* Check device state for handling interrupt */
9276 if (unlikely(lpfc_intr_state_check(phba))) {
9277 /* Check again for link_state with lock held */
9278 spin_lock_irqsave(&phba->hbalock, iflag);
9279 if (phba->link_state < LPFC_LINK_DOWN)
9280 /* Flush, clear interrupt, and rearm the EQ */
9281 lpfc_sli4_eq_flush(phba, speq);
9282 spin_unlock_irqrestore(&phba->hbalock, iflag);
9283 return IRQ_NONE;
9284 }
9285
9286 /*
9287 * Process all the event on FCP slow-path EQ
9288 */
9289 while ((eqe = lpfc_sli4_eq_get(speq))) {
9290 lpfc_sli4_sp_handle_eqe(phba, eqe);
9291 if (!(++ecount % LPFC_GET_QE_REL_INT))
9292 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9293 }
9294
9295 /* Always clear and re-arm the slow-path EQ */
9296 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9297
9298 /* Catch the no cq entry condition */
9299 if (unlikely(ecount == 0)) {
9300 if (phba->intr_type == MSIX)
9301 /* MSI-X treated interrupt served as no EQ share INT */
9302 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9303 "0357 MSI-X interrupt with no EQE\n");
9304 else
9305 /* Non MSI-X treated on interrupt as EQ share INT */
9306 return IRQ_NONE;
9307 }
9308
9309 return IRQ_HANDLED;
9310} /* lpfc_sli4_sp_intr_handler */
9311
9312/**
9313 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9314 * @irq: Interrupt number.
9315 * @dev_id: The device context pointer.
9316 *
9317 * This function is directly called from the PCI layer as an interrupt
9318 * service routine when device with SLI-4 interface spec is enabled with
9319 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9320 * ring event in the HBA. However, when the device is enabled with either
9321 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9322 * device-level interrupt handler. When the PCI slot is in error recovery
9323 * or the HBA is undergoing initialization, the interrupt handler will not
9324 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9325 * the intrrupt context. This function is called without any lock held.
9326 * It gets the hbalock to access and update SLI data structures. Note that,
9327 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9328 * equal to that of FCP CQ index.
9329 *
9330 * This function returns IRQ_HANDLED when interrupt is handled else it
9331 * returns IRQ_NONE.
9332 **/
9333irqreturn_t
9334lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9335{
9336 struct lpfc_hba *phba;
9337 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9338 struct lpfc_queue *fpeq;
9339 struct lpfc_eqe *eqe;
9340 unsigned long iflag;
9341 int ecount = 0;
9342 uint32_t fcp_eqidx;
9343
9344 /* Get the driver's phba structure from the dev_id */
9345 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9346 phba = fcp_eq_hdl->phba;
9347 fcp_eqidx = fcp_eq_hdl->idx;
9348
9349 if (unlikely(!phba))
9350 return IRQ_NONE;
9351
9352 /* Get to the EQ struct associated with this vector */
9353 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9354
9355 /* Check device state for handling interrupt */
9356 if (unlikely(lpfc_intr_state_check(phba))) {
9357 /* Check again for link_state with lock held */
9358 spin_lock_irqsave(&phba->hbalock, iflag);
9359 if (phba->link_state < LPFC_LINK_DOWN)
9360 /* Flush, clear interrupt, and rearm the EQ */
9361 lpfc_sli4_eq_flush(phba, fpeq);
9362 spin_unlock_irqrestore(&phba->hbalock, iflag);
9363 return IRQ_NONE;
9364 }
9365
9366 /*
9367 * Process all the event on FCP fast-path EQ
9368 */
9369 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9370 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9371 if (!(++ecount % LPFC_GET_QE_REL_INT))
9372 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9373 }
9374
9375 /* Always clear and re-arm the fast-path EQ */
9376 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9377
9378 if (unlikely(ecount == 0)) {
9379 if (phba->intr_type == MSIX)
9380 /* MSI-X treated interrupt served as no EQ share INT */
9381 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9382 "0358 MSI-X interrupt with no EQE\n");
9383 else
9384 /* Non MSI-X treated on interrupt as EQ share INT */
9385 return IRQ_NONE;
9386 }
9387
9388 return IRQ_HANDLED;
9389} /* lpfc_sli4_fp_intr_handler */
9390
9391/**
9392 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9393 * @irq: Interrupt number.
9394 * @dev_id: The device context pointer.
9395 *
9396 * This function is the device-level interrupt handler to device with SLI-4
9397 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9398 * interrupt mode is enabled and there is an event in the HBA which requires
9399 * driver attention. This function invokes the slow-path interrupt attention
9400 * handling function and fast-path interrupt attention handling function in
9401 * turn to process the relevant HBA attention events. This function is called
9402 * without any lock held. It gets the hbalock to access and update SLI data
9403 * structures.
9404 *
9405 * This function returns IRQ_HANDLED when interrupt is handled, else it
9406 * returns IRQ_NONE.
9407 **/
9408irqreturn_t
9409lpfc_sli4_intr_handler(int irq, void *dev_id)
9410{
9411 struct lpfc_hba *phba;
9412 irqreturn_t sp_irq_rc, fp_irq_rc;
9413 bool fp_handled = false;
9414 uint32_t fcp_eqidx;
9415
9416 /* Get the driver's phba structure from the dev_id */
9417 phba = (struct lpfc_hba *)dev_id;
9418
9419 if (unlikely(!phba))
9420 return IRQ_NONE;
9421
9422 /*
9423 * Invokes slow-path host attention interrupt handling as appropriate.
9424 */
9425 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9426
9427 /*
9428 * Invoke fast-path host attention interrupt handling as appropriate.
9429 */
9430 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9431 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9432 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9433 if (fp_irq_rc == IRQ_HANDLED)
9434 fp_handled |= true;
9435 }
9436
9437 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9438} /* lpfc_sli4_intr_handler */
9439
9440/**
9441 * lpfc_sli4_queue_free - free a queue structure and associated memory
9442 * @queue: The queue structure to free.
9443 *
9444 * This function frees a queue structure and the DMAable memeory used for
9445 * the host resident queue. This function must be called after destroying the
9446 * queue on the HBA.
9447 **/
9448void
9449lpfc_sli4_queue_free(struct lpfc_queue *queue)
9450{
9451 struct lpfc_dmabuf *dmabuf;
9452
9453 if (!queue)
9454 return;
9455
9456 while (!list_empty(&queue->page_list)) {
9457 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9458 list);
9459 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9460 dmabuf->virt, dmabuf->phys);
9461 kfree(dmabuf);
9462 }
9463 kfree(queue);
9464 return;
9465}
9466
9467/**
9468 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9469 * @phba: The HBA that this queue is being created on.
9470 * @entry_size: The size of each queue entry for this queue.
9471 * @entry count: The number of entries that this queue will handle.
9472 *
9473 * This function allocates a queue structure and the DMAable memory used for
9474 * the host resident queue. This function must be called before creating the
9475 * queue on the HBA.
9476 **/
9477struct lpfc_queue *
9478lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9479 uint32_t entry_count)
9480{
9481 struct lpfc_queue *queue;
9482 struct lpfc_dmabuf *dmabuf;
9483 int x, total_qe_count;
9484 void *dma_pointer;
9485
9486
9487 queue = kzalloc(sizeof(struct lpfc_queue) +
9488 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9489 if (!queue)
9490 return NULL;
9491 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9492 INIT_LIST_HEAD(&queue->list);
9493 INIT_LIST_HEAD(&queue->page_list);
9494 INIT_LIST_HEAD(&queue->child_list);
9495 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9496 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9497 if (!dmabuf)
9498 goto out_fail;
9499 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9500 PAGE_SIZE, &dmabuf->phys,
9501 GFP_KERNEL);
9502 if (!dmabuf->virt) {
9503 kfree(dmabuf);
9504 goto out_fail;
9505 }
James Smartd11e31d2009-06-10 17:23:06 -04009506 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009507 dmabuf->buffer_tag = x;
9508 list_add_tail(&dmabuf->list, &queue->page_list);
9509 /* initialize queue's entry array */
9510 dma_pointer = dmabuf->virt;
9511 for (; total_qe_count < entry_count &&
9512 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9513 total_qe_count++, dma_pointer += entry_size) {
9514 queue->qe[total_qe_count].address = dma_pointer;
9515 }
9516 }
9517 queue->entry_size = entry_size;
9518 queue->entry_count = entry_count;
9519 queue->phba = phba;
9520
9521 return queue;
9522out_fail:
9523 lpfc_sli4_queue_free(queue);
9524 return NULL;
9525}
9526
9527/**
9528 * lpfc_eq_create - Create an Event Queue on the HBA
9529 * @phba: HBA structure that indicates port to create a queue on.
9530 * @eq: The queue structure to use to create the event queue.
9531 * @imax: The maximum interrupt per second limit.
9532 *
9533 * This function creates an event queue, as detailed in @eq, on a port,
9534 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9535 *
9536 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9537 * is used to get the entry count and entry size that are necessary to
9538 * determine the number of pages to allocate and use for this queue. This
9539 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9540 * event queue. This function is asynchronous and will wait for the mailbox
9541 * command to finish before continuing.
9542 *
9543 * On success this function will return a zero. If unable to allocate enough
9544 * memory this function will return ENOMEM. If the queue create mailbox command
9545 * fails this function will return ENXIO.
9546 **/
9547uint32_t
9548lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9549{
9550 struct lpfc_mbx_eq_create *eq_create;
9551 LPFC_MBOXQ_t *mbox;
9552 int rc, length, status = 0;
9553 struct lpfc_dmabuf *dmabuf;
9554 uint32_t shdr_status, shdr_add_status;
9555 union lpfc_sli4_cfg_shdr *shdr;
9556 uint16_t dmult;
9557
9558 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9559 if (!mbox)
9560 return -ENOMEM;
9561 length = (sizeof(struct lpfc_mbx_eq_create) -
9562 sizeof(struct lpfc_sli4_cfg_mhdr));
9563 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9564 LPFC_MBOX_OPCODE_EQ_CREATE,
9565 length, LPFC_SLI4_MBX_EMBED);
9566 eq_create = &mbox->u.mqe.un.eq_create;
9567 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9568 eq->page_count);
9569 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9570 LPFC_EQE_SIZE);
9571 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9572 /* Calculate delay multiper from maximum interrupt per second */
9573 dmult = LPFC_DMULT_CONST/imax - 1;
9574 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9575 dmult);
9576 switch (eq->entry_count) {
9577 default:
9578 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9579 "0360 Unsupported EQ count. (%d)\n",
9580 eq->entry_count);
9581 if (eq->entry_count < 256)
9582 return -EINVAL;
9583 /* otherwise default to smallest count (drop through) */
9584 case 256:
9585 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9586 LPFC_EQ_CNT_256);
9587 break;
9588 case 512:
9589 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9590 LPFC_EQ_CNT_512);
9591 break;
9592 case 1024:
9593 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9594 LPFC_EQ_CNT_1024);
9595 break;
9596 case 2048:
9597 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9598 LPFC_EQ_CNT_2048);
9599 break;
9600 case 4096:
9601 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9602 LPFC_EQ_CNT_4096);
9603 break;
9604 }
9605 list_for_each_entry(dmabuf, &eq->page_list, list) {
9606 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9607 putPaddrLow(dmabuf->phys);
9608 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9609 putPaddrHigh(dmabuf->phys);
9610 }
9611 mbox->vport = phba->pport;
9612 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9613 mbox->context1 = NULL;
9614 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9615 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9616 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9617 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9618 if (shdr_status || shdr_add_status || rc) {
9619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9620 "2500 EQ_CREATE mailbox failed with "
9621 "status x%x add_status x%x, mbx status x%x\n",
9622 shdr_status, shdr_add_status, rc);
9623 status = -ENXIO;
9624 }
9625 eq->type = LPFC_EQ;
9626 eq->subtype = LPFC_NONE;
9627 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9628 if (eq->queue_id == 0xFFFF)
9629 status = -ENXIO;
9630 eq->host_index = 0;
9631 eq->hba_index = 0;
9632
James Smart8fa38512009-07-19 10:01:03 -04009633 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009634 return status;
9635}
9636
9637/**
9638 * lpfc_cq_create - Create a Completion Queue on the HBA
9639 * @phba: HBA structure that indicates port to create a queue on.
9640 * @cq: The queue structure to use to create the completion queue.
9641 * @eq: The event queue to bind this completion queue to.
9642 *
9643 * This function creates a completion queue, as detailed in @wq, on a port,
9644 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9645 *
9646 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9647 * is used to get the entry count and entry size that are necessary to
9648 * determine the number of pages to allocate and use for this queue. The @eq
9649 * is used to indicate which event queue to bind this completion queue to. This
9650 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9651 * completion queue. This function is asynchronous and will wait for the mailbox
9652 * command to finish before continuing.
9653 *
9654 * On success this function will return a zero. If unable to allocate enough
9655 * memory this function will return ENOMEM. If the queue create mailbox command
9656 * fails this function will return ENXIO.
9657 **/
9658uint32_t
9659lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9660 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9661{
9662 struct lpfc_mbx_cq_create *cq_create;
9663 struct lpfc_dmabuf *dmabuf;
9664 LPFC_MBOXQ_t *mbox;
9665 int rc, length, status = 0;
9666 uint32_t shdr_status, shdr_add_status;
9667 union lpfc_sli4_cfg_shdr *shdr;
9668
9669 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9670 if (!mbox)
9671 return -ENOMEM;
9672 length = (sizeof(struct lpfc_mbx_cq_create) -
9673 sizeof(struct lpfc_sli4_cfg_mhdr));
9674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9675 LPFC_MBOX_OPCODE_CQ_CREATE,
9676 length, LPFC_SLI4_MBX_EMBED);
9677 cq_create = &mbox->u.mqe.un.cq_create;
9678 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9679 cq->page_count);
9680 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9681 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9682 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9683 switch (cq->entry_count) {
9684 default:
9685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9686 "0361 Unsupported CQ count. (%d)\n",
9687 cq->entry_count);
9688 if (cq->entry_count < 256)
9689 return -EINVAL;
9690 /* otherwise default to smallest count (drop through) */
9691 case 256:
9692 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9693 LPFC_CQ_CNT_256);
9694 break;
9695 case 512:
9696 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9697 LPFC_CQ_CNT_512);
9698 break;
9699 case 1024:
9700 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9701 LPFC_CQ_CNT_1024);
9702 break;
9703 }
9704 list_for_each_entry(dmabuf, &cq->page_list, list) {
9705 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9706 putPaddrLow(dmabuf->phys);
9707 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9708 putPaddrHigh(dmabuf->phys);
9709 }
9710 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9711
9712 /* The IOCTL status is embedded in the mailbox subheader. */
9713 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9714 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9715 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9716 if (shdr_status || shdr_add_status || rc) {
9717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9718 "2501 CQ_CREATE mailbox failed with "
9719 "status x%x add_status x%x, mbx status x%x\n",
9720 shdr_status, shdr_add_status, rc);
9721 status = -ENXIO;
9722 goto out;
9723 }
9724 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9725 if (cq->queue_id == 0xFFFF) {
9726 status = -ENXIO;
9727 goto out;
9728 }
9729 /* link the cq onto the parent eq child list */
9730 list_add_tail(&cq->list, &eq->child_list);
9731 /* Set up completion queue's type and subtype */
9732 cq->type = type;
9733 cq->subtype = subtype;
9734 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9735 cq->host_index = 0;
9736 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009737
James Smart8fa38512009-07-19 10:01:03 -04009738out:
9739 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009740 return status;
9741}
9742
9743/**
James Smart04c68492009-05-22 14:52:52 -04009744 * lpfc_mq_create - Create a mailbox Queue on the HBA
9745 * @phba: HBA structure that indicates port to create a queue on.
9746 * @mq: The queue structure to use to create the mailbox queue.
9747 *
9748 * This function creates a mailbox queue, as detailed in @mq, on a port,
9749 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9750 *
9751 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9752 * is used to get the entry count and entry size that are necessary to
9753 * determine the number of pages to allocate and use for this queue. This
9754 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9755 * mailbox queue. This function is asynchronous and will wait for the mailbox
9756 * command to finish before continuing.
9757 *
9758 * On success this function will return a zero. If unable to allocate enough
9759 * memory this function will return ENOMEM. If the queue create mailbox command
9760 * fails this function will return ENXIO.
9761 **/
9762uint32_t
9763lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9764 struct lpfc_queue *cq, uint32_t subtype)
9765{
9766 struct lpfc_mbx_mq_create *mq_create;
9767 struct lpfc_dmabuf *dmabuf;
9768 LPFC_MBOXQ_t *mbox;
9769 int rc, length, status = 0;
9770 uint32_t shdr_status, shdr_add_status;
9771 union lpfc_sli4_cfg_shdr *shdr;
9772
9773 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9774 if (!mbox)
9775 return -ENOMEM;
9776 length = (sizeof(struct lpfc_mbx_mq_create) -
9777 sizeof(struct lpfc_sli4_cfg_mhdr));
9778 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9779 LPFC_MBOX_OPCODE_MQ_CREATE,
9780 length, LPFC_SLI4_MBX_EMBED);
9781 mq_create = &mbox->u.mqe.un.mq_create;
9782 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9783 mq->page_count);
9784 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9785 cq->queue_id);
9786 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9787 switch (mq->entry_count) {
9788 default:
9789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9790 "0362 Unsupported MQ count. (%d)\n",
9791 mq->entry_count);
9792 if (mq->entry_count < 16)
9793 return -EINVAL;
9794 /* otherwise default to smallest count (drop through) */
9795 case 16:
9796 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9797 LPFC_MQ_CNT_16);
9798 break;
9799 case 32:
9800 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9801 LPFC_MQ_CNT_32);
9802 break;
9803 case 64:
9804 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9805 LPFC_MQ_CNT_64);
9806 break;
9807 case 128:
9808 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9809 LPFC_MQ_CNT_128);
9810 break;
9811 }
9812 list_for_each_entry(dmabuf, &mq->page_list, list) {
9813 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9814 putPaddrLow(dmabuf->phys);
9815 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9816 putPaddrHigh(dmabuf->phys);
9817 }
9818 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9819 /* The IOCTL status is embedded in the mailbox subheader. */
9820 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9821 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9822 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9823 if (shdr_status || shdr_add_status || rc) {
9824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9825 "2502 MQ_CREATE mailbox failed with "
9826 "status x%x add_status x%x, mbx status x%x\n",
9827 shdr_status, shdr_add_status, rc);
9828 status = -ENXIO;
9829 goto out;
9830 }
9831 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9832 if (mq->queue_id == 0xFFFF) {
9833 status = -ENXIO;
9834 goto out;
9835 }
9836 mq->type = LPFC_MQ;
9837 mq->subtype = subtype;
9838 mq->host_index = 0;
9839 mq->hba_index = 0;
9840
9841 /* link the mq onto the parent cq child list */
9842 list_add_tail(&mq->list, &cq->child_list);
9843out:
James Smart8fa38512009-07-19 10:01:03 -04009844 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009845 return status;
9846}
9847
9848/**
James Smart4f774512009-05-22 14:52:35 -04009849 * lpfc_wq_create - Create a Work Queue on the HBA
9850 * @phba: HBA structure that indicates port to create a queue on.
9851 * @wq: The queue structure to use to create the work queue.
9852 * @cq: The completion queue to bind this work queue to.
9853 * @subtype: The subtype of the work queue indicating its functionality.
9854 *
9855 * This function creates a work queue, as detailed in @wq, on a port, described
9856 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9857 *
9858 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9859 * is used to get the entry count and entry size that are necessary to
9860 * determine the number of pages to allocate and use for this queue. The @cq
9861 * is used to indicate which completion queue to bind this work queue to. This
9862 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9863 * work queue. This function is asynchronous and will wait for the mailbox
9864 * command to finish before continuing.
9865 *
9866 * On success this function will return a zero. If unable to allocate enough
9867 * memory this function will return ENOMEM. If the queue create mailbox command
9868 * fails this function will return ENXIO.
9869 **/
9870uint32_t
9871lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9872 struct lpfc_queue *cq, uint32_t subtype)
9873{
9874 struct lpfc_mbx_wq_create *wq_create;
9875 struct lpfc_dmabuf *dmabuf;
9876 LPFC_MBOXQ_t *mbox;
9877 int rc, length, status = 0;
9878 uint32_t shdr_status, shdr_add_status;
9879 union lpfc_sli4_cfg_shdr *shdr;
9880
9881 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9882 if (!mbox)
9883 return -ENOMEM;
9884 length = (sizeof(struct lpfc_mbx_wq_create) -
9885 sizeof(struct lpfc_sli4_cfg_mhdr));
9886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9887 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9888 length, LPFC_SLI4_MBX_EMBED);
9889 wq_create = &mbox->u.mqe.un.wq_create;
9890 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9891 wq->page_count);
9892 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9893 cq->queue_id);
9894 list_for_each_entry(dmabuf, &wq->page_list, list) {
9895 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9896 putPaddrLow(dmabuf->phys);
9897 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9898 putPaddrHigh(dmabuf->phys);
9899 }
9900 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9901 /* The IOCTL status is embedded in the mailbox subheader. */
9902 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9905 if (shdr_status || shdr_add_status || rc) {
9906 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9907 "2503 WQ_CREATE mailbox failed with "
9908 "status x%x add_status x%x, mbx status x%x\n",
9909 shdr_status, shdr_add_status, rc);
9910 status = -ENXIO;
9911 goto out;
9912 }
9913 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9914 if (wq->queue_id == 0xFFFF) {
9915 status = -ENXIO;
9916 goto out;
9917 }
9918 wq->type = LPFC_WQ;
9919 wq->subtype = subtype;
9920 wq->host_index = 0;
9921 wq->hba_index = 0;
9922
9923 /* link the wq onto the parent cq child list */
9924 list_add_tail(&wq->list, &cq->child_list);
9925out:
James Smart8fa38512009-07-19 10:01:03 -04009926 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009927 return status;
9928}
9929
9930/**
9931 * lpfc_rq_create - Create a Receive Queue on the HBA
9932 * @phba: HBA structure that indicates port to create a queue on.
9933 * @hrq: The queue structure to use to create the header receive queue.
9934 * @drq: The queue structure to use to create the data receive queue.
9935 * @cq: The completion queue to bind this work queue to.
9936 *
9937 * This function creates a receive buffer queue pair , as detailed in @hrq and
9938 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9939 * to the HBA.
9940 *
9941 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9942 * struct is used to get the entry count that is necessary to determine the
9943 * number of pages to use for this queue. The @cq is used to indicate which
9944 * completion queue to bind received buffers that are posted to these queues to.
9945 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9946 * receive queue pair. This function is asynchronous and will wait for the
9947 * mailbox command to finish before continuing.
9948 *
9949 * On success this function will return a zero. If unable to allocate enough
9950 * memory this function will return ENOMEM. If the queue create mailbox command
9951 * fails this function will return ENXIO.
9952 **/
9953uint32_t
9954lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9955 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9956{
9957 struct lpfc_mbx_rq_create *rq_create;
9958 struct lpfc_dmabuf *dmabuf;
9959 LPFC_MBOXQ_t *mbox;
9960 int rc, length, status = 0;
9961 uint32_t shdr_status, shdr_add_status;
9962 union lpfc_sli4_cfg_shdr *shdr;
9963
9964 if (hrq->entry_count != drq->entry_count)
9965 return -EINVAL;
9966 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9967 if (!mbox)
9968 return -ENOMEM;
9969 length = (sizeof(struct lpfc_mbx_rq_create) -
9970 sizeof(struct lpfc_sli4_cfg_mhdr));
9971 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9972 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9973 length, LPFC_SLI4_MBX_EMBED);
9974 rq_create = &mbox->u.mqe.un.rq_create;
9975 switch (hrq->entry_count) {
9976 default:
9977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9978 "2535 Unsupported RQ count. (%d)\n",
9979 hrq->entry_count);
9980 if (hrq->entry_count < 512)
9981 return -EINVAL;
9982 /* otherwise default to smallest count (drop through) */
9983 case 512:
9984 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9985 LPFC_RQ_RING_SIZE_512);
9986 break;
9987 case 1024:
9988 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9989 LPFC_RQ_RING_SIZE_1024);
9990 break;
9991 case 2048:
9992 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9993 LPFC_RQ_RING_SIZE_2048);
9994 break;
9995 case 4096:
9996 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9997 LPFC_RQ_RING_SIZE_4096);
9998 break;
9999 }
10000 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10001 cq->queue_id);
10002 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10003 hrq->page_count);
10004 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10005 LPFC_HDR_BUF_SIZE);
10006 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10007 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10008 putPaddrLow(dmabuf->phys);
10009 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10010 putPaddrHigh(dmabuf->phys);
10011 }
10012 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10013 /* The IOCTL status is embedded in the mailbox subheader. */
10014 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10015 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10016 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10017 if (shdr_status || shdr_add_status || rc) {
10018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10019 "2504 RQ_CREATE mailbox failed with "
10020 "status x%x add_status x%x, mbx status x%x\n",
10021 shdr_status, shdr_add_status, rc);
10022 status = -ENXIO;
10023 goto out;
10024 }
10025 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10026 if (hrq->queue_id == 0xFFFF) {
10027 status = -ENXIO;
10028 goto out;
10029 }
10030 hrq->type = LPFC_HRQ;
10031 hrq->subtype = subtype;
10032 hrq->host_index = 0;
10033 hrq->hba_index = 0;
10034
10035 /* now create the data queue */
10036 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10037 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10038 length, LPFC_SLI4_MBX_EMBED);
10039 switch (drq->entry_count) {
10040 default:
10041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10042 "2536 Unsupported RQ count. (%d)\n",
10043 drq->entry_count);
10044 if (drq->entry_count < 512)
10045 return -EINVAL;
10046 /* otherwise default to smallest count (drop through) */
10047 case 512:
10048 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10049 LPFC_RQ_RING_SIZE_512);
10050 break;
10051 case 1024:
10052 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10053 LPFC_RQ_RING_SIZE_1024);
10054 break;
10055 case 2048:
10056 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10057 LPFC_RQ_RING_SIZE_2048);
10058 break;
10059 case 4096:
10060 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10061 LPFC_RQ_RING_SIZE_4096);
10062 break;
10063 }
10064 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10065 cq->queue_id);
10066 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10067 drq->page_count);
10068 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10069 LPFC_DATA_BUF_SIZE);
10070 list_for_each_entry(dmabuf, &drq->page_list, list) {
10071 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10072 putPaddrLow(dmabuf->phys);
10073 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10074 putPaddrHigh(dmabuf->phys);
10075 }
10076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10077 /* The IOCTL status is embedded in the mailbox subheader. */
10078 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10079 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10080 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10081 if (shdr_status || shdr_add_status || rc) {
10082 status = -ENXIO;
10083 goto out;
10084 }
10085 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10086 if (drq->queue_id == 0xFFFF) {
10087 status = -ENXIO;
10088 goto out;
10089 }
10090 drq->type = LPFC_DRQ;
10091 drq->subtype = subtype;
10092 drq->host_index = 0;
10093 drq->hba_index = 0;
10094
10095 /* link the header and data RQs onto the parent cq child list */
10096 list_add_tail(&hrq->list, &cq->child_list);
10097 list_add_tail(&drq->list, &cq->child_list);
10098
10099out:
James Smart8fa38512009-07-19 10:01:03 -040010100 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010101 return status;
10102}
10103
10104/**
10105 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10106 * @eq: The queue structure associated with the queue to destroy.
10107 *
10108 * This function destroys a queue, as detailed in @eq by sending an mailbox
10109 * command, specific to the type of queue, to the HBA.
10110 *
10111 * The @eq struct is used to get the queue ID of the queue to destroy.
10112 *
10113 * On success this function will return a zero. If the queue destroy mailbox
10114 * command fails this function will return ENXIO.
10115 **/
10116uint32_t
10117lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10118{
10119 LPFC_MBOXQ_t *mbox;
10120 int rc, length, status = 0;
10121 uint32_t shdr_status, shdr_add_status;
10122 union lpfc_sli4_cfg_shdr *shdr;
10123
10124 if (!eq)
10125 return -ENODEV;
10126 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10127 if (!mbox)
10128 return -ENOMEM;
10129 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10130 sizeof(struct lpfc_sli4_cfg_mhdr));
10131 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10132 LPFC_MBOX_OPCODE_EQ_DESTROY,
10133 length, LPFC_SLI4_MBX_EMBED);
10134 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10135 eq->queue_id);
10136 mbox->vport = eq->phba->pport;
10137 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10138
10139 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10140 /* The IOCTL status is embedded in the mailbox subheader. */
10141 shdr = (union lpfc_sli4_cfg_shdr *)
10142 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10143 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10144 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10145 if (shdr_status || shdr_add_status || rc) {
10146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10147 "2505 EQ_DESTROY mailbox failed with "
10148 "status x%x add_status x%x, mbx status x%x\n",
10149 shdr_status, shdr_add_status, rc);
10150 status = -ENXIO;
10151 }
10152
10153 /* Remove eq from any list */
10154 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010155 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010156 return status;
10157}
10158
10159/**
10160 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10161 * @cq: The queue structure associated with the queue to destroy.
10162 *
10163 * This function destroys a queue, as detailed in @cq by sending an mailbox
10164 * command, specific to the type of queue, to the HBA.
10165 *
10166 * The @cq struct is used to get the queue ID of the queue to destroy.
10167 *
10168 * On success this function will return a zero. If the queue destroy mailbox
10169 * command fails this function will return ENXIO.
10170 **/
10171uint32_t
10172lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10173{
10174 LPFC_MBOXQ_t *mbox;
10175 int rc, length, status = 0;
10176 uint32_t shdr_status, shdr_add_status;
10177 union lpfc_sli4_cfg_shdr *shdr;
10178
10179 if (!cq)
10180 return -ENODEV;
10181 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10182 if (!mbox)
10183 return -ENOMEM;
10184 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10185 sizeof(struct lpfc_sli4_cfg_mhdr));
10186 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10187 LPFC_MBOX_OPCODE_CQ_DESTROY,
10188 length, LPFC_SLI4_MBX_EMBED);
10189 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10190 cq->queue_id);
10191 mbox->vport = cq->phba->pport;
10192 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10193 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10194 /* The IOCTL status is embedded in the mailbox subheader. */
10195 shdr = (union lpfc_sli4_cfg_shdr *)
10196 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10199 if (shdr_status || shdr_add_status || rc) {
10200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10201 "2506 CQ_DESTROY mailbox failed with "
10202 "status x%x add_status x%x, mbx status x%x\n",
10203 shdr_status, shdr_add_status, rc);
10204 status = -ENXIO;
10205 }
10206 /* Remove cq from any list */
10207 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010208 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010209 return status;
10210}
10211
10212/**
James Smart04c68492009-05-22 14:52:52 -040010213 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10214 * @qm: The queue structure associated with the queue to destroy.
10215 *
10216 * This function destroys a queue, as detailed in @mq by sending an mailbox
10217 * command, specific to the type of queue, to the HBA.
10218 *
10219 * The @mq struct is used to get the queue ID of the queue to destroy.
10220 *
10221 * On success this function will return a zero. If the queue destroy mailbox
10222 * command fails this function will return ENXIO.
10223 **/
10224uint32_t
10225lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10226{
10227 LPFC_MBOXQ_t *mbox;
10228 int rc, length, status = 0;
10229 uint32_t shdr_status, shdr_add_status;
10230 union lpfc_sli4_cfg_shdr *shdr;
10231
10232 if (!mq)
10233 return -ENODEV;
10234 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10235 if (!mbox)
10236 return -ENOMEM;
10237 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10238 sizeof(struct lpfc_sli4_cfg_mhdr));
10239 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10240 LPFC_MBOX_OPCODE_MQ_DESTROY,
10241 length, LPFC_SLI4_MBX_EMBED);
10242 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10243 mq->queue_id);
10244 mbox->vport = mq->phba->pport;
10245 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10246 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10247 /* The IOCTL status is embedded in the mailbox subheader. */
10248 shdr = (union lpfc_sli4_cfg_shdr *)
10249 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10250 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10251 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10252 if (shdr_status || shdr_add_status || rc) {
10253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10254 "2507 MQ_DESTROY mailbox failed with "
10255 "status x%x add_status x%x, mbx status x%x\n",
10256 shdr_status, shdr_add_status, rc);
10257 status = -ENXIO;
10258 }
10259 /* Remove mq from any list */
10260 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010261 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010262 return status;
10263}
10264
10265/**
James Smart4f774512009-05-22 14:52:35 -040010266 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10267 * @wq: The queue structure associated with the queue to destroy.
10268 *
10269 * This function destroys a queue, as detailed in @wq by sending an mailbox
10270 * command, specific to the type of queue, to the HBA.
10271 *
10272 * The @wq struct is used to get the queue ID of the queue to destroy.
10273 *
10274 * On success this function will return a zero. If the queue destroy mailbox
10275 * command fails this function will return ENXIO.
10276 **/
10277uint32_t
10278lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10279{
10280 LPFC_MBOXQ_t *mbox;
10281 int rc, length, status = 0;
10282 uint32_t shdr_status, shdr_add_status;
10283 union lpfc_sli4_cfg_shdr *shdr;
10284
10285 if (!wq)
10286 return -ENODEV;
10287 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10288 if (!mbox)
10289 return -ENOMEM;
10290 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10291 sizeof(struct lpfc_sli4_cfg_mhdr));
10292 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10293 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10294 length, LPFC_SLI4_MBX_EMBED);
10295 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10296 wq->queue_id);
10297 mbox->vport = wq->phba->pport;
10298 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10299 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10300 shdr = (union lpfc_sli4_cfg_shdr *)
10301 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10302 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10303 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10304 if (shdr_status || shdr_add_status || rc) {
10305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10306 "2508 WQ_DESTROY mailbox failed with "
10307 "status x%x add_status x%x, mbx status x%x\n",
10308 shdr_status, shdr_add_status, rc);
10309 status = -ENXIO;
10310 }
10311 /* Remove wq from any list */
10312 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010313 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010314 return status;
10315}
10316
10317/**
10318 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10319 * @rq: The queue structure associated with the queue to destroy.
10320 *
10321 * This function destroys a queue, as detailed in @rq by sending an mailbox
10322 * command, specific to the type of queue, to the HBA.
10323 *
10324 * The @rq struct is used to get the queue ID of the queue to destroy.
10325 *
10326 * On success this function will return a zero. If the queue destroy mailbox
10327 * command fails this function will return ENXIO.
10328 **/
10329uint32_t
10330lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10331 struct lpfc_queue *drq)
10332{
10333 LPFC_MBOXQ_t *mbox;
10334 int rc, length, status = 0;
10335 uint32_t shdr_status, shdr_add_status;
10336 union lpfc_sli4_cfg_shdr *shdr;
10337
10338 if (!hrq || !drq)
10339 return -ENODEV;
10340 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10341 if (!mbox)
10342 return -ENOMEM;
10343 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10344 sizeof(struct mbox_header));
10345 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10346 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10347 length, LPFC_SLI4_MBX_EMBED);
10348 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10349 hrq->queue_id);
10350 mbox->vport = hrq->phba->pport;
10351 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10352 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10353 /* The IOCTL status is embedded in the mailbox subheader. */
10354 shdr = (union lpfc_sli4_cfg_shdr *)
10355 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10356 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10357 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10358 if (shdr_status || shdr_add_status || rc) {
10359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10360 "2509 RQ_DESTROY mailbox failed with "
10361 "status x%x add_status x%x, mbx status x%x\n",
10362 shdr_status, shdr_add_status, rc);
10363 if (rc != MBX_TIMEOUT)
10364 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10365 return -ENXIO;
10366 }
10367 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10368 drq->queue_id);
10369 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10370 shdr = (union lpfc_sli4_cfg_shdr *)
10371 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10372 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10373 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10374 if (shdr_status || shdr_add_status || rc) {
10375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10376 "2510 RQ_DESTROY mailbox failed with "
10377 "status x%x add_status x%x, mbx status x%x\n",
10378 shdr_status, shdr_add_status, rc);
10379 status = -ENXIO;
10380 }
10381 list_del_init(&hrq->list);
10382 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010383 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010384 return status;
10385}
10386
10387/**
10388 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10389 * @phba: The virtual port for which this call being executed.
10390 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10391 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10392 * @xritag: the xritag that ties this io to the SGL pages.
10393 *
10394 * This routine will post the sgl pages for the IO that has the xritag
10395 * that is in the iocbq structure. The xritag is assigned during iocbq
10396 * creation and persists for as long as the driver is loaded.
10397 * if the caller has fewer than 256 scatter gather segments to map then
10398 * pdma_phys_addr1 should be 0.
10399 * If the caller needs to map more than 256 scatter gather segment then
10400 * pdma_phys_addr1 should be a valid physical address.
10401 * physical address for SGLs must be 64 byte aligned.
10402 * If you are going to map 2 SGL's then the first one must have 256 entries
10403 * the second sgl can have between 1 and 256 entries.
10404 *
10405 * Return codes:
10406 * 0 - Success
10407 * -ENXIO, -ENOMEM - Failure
10408 **/
10409int
10410lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10411 dma_addr_t pdma_phys_addr0,
10412 dma_addr_t pdma_phys_addr1,
10413 uint16_t xritag)
10414{
10415 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10416 LPFC_MBOXQ_t *mbox;
10417 int rc;
10418 uint32_t shdr_status, shdr_add_status;
10419 union lpfc_sli4_cfg_shdr *shdr;
10420
10421 if (xritag == NO_XRI) {
10422 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10423 "0364 Invalid param:\n");
10424 return -EINVAL;
10425 }
10426
10427 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10428 if (!mbox)
10429 return -ENOMEM;
10430
10431 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10432 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10433 sizeof(struct lpfc_mbx_post_sgl_pages) -
10434 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10435
10436 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10437 &mbox->u.mqe.un.post_sgl_pages;
10438 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10439 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10440
10441 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10442 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10443 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10444 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10445
10446 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10447 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10448 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10449 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10450 if (!phba->sli4_hba.intr_enable)
10451 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10452 else
10453 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10454 /* The IOCTL status is embedded in the mailbox subheader. */
10455 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10456 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10457 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10458 if (rc != MBX_TIMEOUT)
10459 mempool_free(mbox, phba->mbox_mem_pool);
10460 if (shdr_status || shdr_add_status || rc) {
10461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10462 "2511 POST_SGL mailbox failed with "
10463 "status x%x add_status x%x, mbx status x%x\n",
10464 shdr_status, shdr_add_status, rc);
10465 rc = -ENXIO;
10466 }
10467 return 0;
10468}
10469/**
10470 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10471 * @phba: The virtual port for which this call being executed.
10472 *
10473 * This routine will remove all of the sgl pages registered with the hba.
10474 *
10475 * Return codes:
10476 * 0 - Success
10477 * -ENXIO, -ENOMEM - Failure
10478 **/
10479int
10480lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10481{
10482 LPFC_MBOXQ_t *mbox;
10483 int rc;
10484 uint32_t shdr_status, shdr_add_status;
10485 union lpfc_sli4_cfg_shdr *shdr;
10486
10487 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10488 if (!mbox)
10489 return -ENOMEM;
10490
10491 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10492 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10493 LPFC_SLI4_MBX_EMBED);
10494 if (!phba->sli4_hba.intr_enable)
10495 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10496 else
10497 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10498 /* The IOCTL status is embedded in the mailbox subheader. */
10499 shdr = (union lpfc_sli4_cfg_shdr *)
10500 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10503 if (rc != MBX_TIMEOUT)
10504 mempool_free(mbox, phba->mbox_mem_pool);
10505 if (shdr_status || shdr_add_status || rc) {
10506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10507 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10508 "status x%x add_status x%x, mbx status x%x\n",
10509 shdr_status, shdr_add_status, rc);
10510 rc = -ENXIO;
10511 }
10512 return rc;
10513}
10514
10515/**
10516 * lpfc_sli4_next_xritag - Get an xritag for the io
10517 * @phba: Pointer to HBA context object.
10518 *
10519 * This function gets an xritag for the iocb. If there is no unused xritag
10520 * it will return 0xffff.
10521 * The function returns the allocated xritag if successful, else returns zero.
10522 * Zero is not a valid xritag.
10523 * The caller is not required to hold any lock.
10524 **/
10525uint16_t
10526lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10527{
10528 uint16_t xritag;
10529
10530 spin_lock_irq(&phba->hbalock);
10531 xritag = phba->sli4_hba.next_xri;
10532 if ((xritag != (uint16_t) -1) && xritag <
10533 (phba->sli4_hba.max_cfg_param.max_xri
10534 + phba->sli4_hba.max_cfg_param.xri_base)) {
10535 phba->sli4_hba.next_xri++;
10536 phba->sli4_hba.max_cfg_param.xri_used++;
10537 spin_unlock_irq(&phba->hbalock);
10538 return xritag;
10539 }
10540 spin_unlock_irq(&phba->hbalock);
James Smart6a9c52c2009-10-02 15:16:51 -040010541 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart4f774512009-05-22 14:52:35 -040010542 "2004 Failed to allocate XRI.last XRITAG is %d"
10543 " Max XRI is %d, Used XRI is %d\n",
10544 phba->sli4_hba.next_xri,
10545 phba->sli4_hba.max_cfg_param.max_xri,
10546 phba->sli4_hba.max_cfg_param.xri_used);
10547 return -1;
10548}
10549
10550/**
10551 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10552 * @phba: pointer to lpfc hba data structure.
10553 *
10554 * This routine is invoked to post a block of driver's sgl pages to the
10555 * HBA using non-embedded mailbox command. No Lock is held. This routine
10556 * is only called when the driver is loading and after all IO has been
10557 * stopped.
10558 **/
10559int
10560lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10561{
10562 struct lpfc_sglq *sglq_entry;
10563 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10564 struct sgl_page_pairs *sgl_pg_pairs;
10565 void *viraddr;
10566 LPFC_MBOXQ_t *mbox;
10567 uint32_t reqlen, alloclen, pg_pairs;
10568 uint32_t mbox_tmo;
10569 uint16_t xritag_start = 0;
10570 int els_xri_cnt, rc = 0;
10571 uint32_t shdr_status, shdr_add_status;
10572 union lpfc_sli4_cfg_shdr *shdr;
10573
10574 /* The number of sgls to be posted */
10575 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10576
10577 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10578 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10579 if (reqlen > PAGE_SIZE) {
10580 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10581 "2559 Block sgl registration required DMA "
10582 "size (%d) great than a page\n", reqlen);
10583 return -ENOMEM;
10584 }
10585 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10586 if (!mbox) {
10587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10588 "2560 Failed to allocate mbox cmd memory\n");
10589 return -ENOMEM;
10590 }
10591
10592 /* Allocate DMA memory and set up the non-embedded mailbox command */
10593 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10594 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10595 LPFC_SLI4_MBX_NEMBED);
10596
10597 if (alloclen < reqlen) {
10598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10599 "0285 Allocated DMA memory size (%d) is "
10600 "less than the requested DMA memory "
10601 "size (%d)\n", alloclen, reqlen);
10602 lpfc_sli4_mbox_cmd_free(phba, mbox);
10603 return -ENOMEM;
10604 }
James Smart4f774512009-05-22 14:52:35 -040010605 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010606 viraddr = mbox->sge_array->addr[0];
10607
10608 /* Set up the SGL pages in the non-embedded DMA pages */
10609 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10610 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10611
10612 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10613 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10614 /* Set up the sge entry */
10615 sgl_pg_pairs->sgl_pg0_addr_lo =
10616 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10617 sgl_pg_pairs->sgl_pg0_addr_hi =
10618 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10619 sgl_pg_pairs->sgl_pg1_addr_lo =
10620 cpu_to_le32(putPaddrLow(0));
10621 sgl_pg_pairs->sgl_pg1_addr_hi =
10622 cpu_to_le32(putPaddrHigh(0));
10623 /* Keep the first xritag on the list */
10624 if (pg_pairs == 0)
10625 xritag_start = sglq_entry->sli4_xritag;
10626 sgl_pg_pairs++;
10627 }
10628 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart6a9c52c2009-10-02 15:16:51 -040010629 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040010630 /* Perform endian conversion if necessary */
10631 sgl->word0 = cpu_to_le32(sgl->word0);
10632
10633 if (!phba->sli4_hba.intr_enable)
10634 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10635 else {
10636 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10637 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10638 }
10639 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10640 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10641 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10642 if (rc != MBX_TIMEOUT)
10643 lpfc_sli4_mbox_cmd_free(phba, mbox);
10644 if (shdr_status || shdr_add_status || rc) {
10645 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10646 "2513 POST_SGL_BLOCK mailbox command failed "
10647 "status x%x add_status x%x mbx status x%x\n",
10648 shdr_status, shdr_add_status, rc);
10649 rc = -ENXIO;
10650 }
10651 return rc;
10652}
10653
10654/**
10655 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10656 * @phba: pointer to lpfc hba data structure.
10657 * @sblist: pointer to scsi buffer list.
10658 * @count: number of scsi buffers on the list.
10659 *
10660 * This routine is invoked to post a block of @count scsi sgl pages from a
10661 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10662 * No Lock is held.
10663 *
10664 **/
10665int
10666lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10667 int cnt)
10668{
10669 struct lpfc_scsi_buf *psb;
10670 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10671 struct sgl_page_pairs *sgl_pg_pairs;
10672 void *viraddr;
10673 LPFC_MBOXQ_t *mbox;
10674 uint32_t reqlen, alloclen, pg_pairs;
10675 uint32_t mbox_tmo;
10676 uint16_t xritag_start = 0;
10677 int rc = 0;
10678 uint32_t shdr_status, shdr_add_status;
10679 dma_addr_t pdma_phys_bpl1;
10680 union lpfc_sli4_cfg_shdr *shdr;
10681
10682 /* Calculate the requested length of the dma memory */
10683 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10684 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10685 if (reqlen > PAGE_SIZE) {
10686 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10687 "0217 Block sgl registration required DMA "
10688 "size (%d) great than a page\n", reqlen);
10689 return -ENOMEM;
10690 }
10691 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10692 if (!mbox) {
10693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10694 "0283 Failed to allocate mbox cmd memory\n");
10695 return -ENOMEM;
10696 }
10697
10698 /* Allocate DMA memory and set up the non-embedded mailbox command */
10699 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10700 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10701 LPFC_SLI4_MBX_NEMBED);
10702
10703 if (alloclen < reqlen) {
10704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10705 "2561 Allocated DMA memory size (%d) is "
10706 "less than the requested DMA memory "
10707 "size (%d)\n", alloclen, reqlen);
10708 lpfc_sli4_mbox_cmd_free(phba, mbox);
10709 return -ENOMEM;
10710 }
James Smart4f774512009-05-22 14:52:35 -040010711 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040010712 viraddr = mbox->sge_array->addr[0];
10713
10714 /* Set up the SGL pages in the non-embedded DMA pages */
10715 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10716 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10717
10718 pg_pairs = 0;
10719 list_for_each_entry(psb, sblist, list) {
10720 /* Set up the sge entry */
10721 sgl_pg_pairs->sgl_pg0_addr_lo =
10722 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10723 sgl_pg_pairs->sgl_pg0_addr_hi =
10724 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10725 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10726 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10727 else
10728 pdma_phys_bpl1 = 0;
10729 sgl_pg_pairs->sgl_pg1_addr_lo =
10730 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10731 sgl_pg_pairs->sgl_pg1_addr_hi =
10732 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10733 /* Keep the first xritag on the list */
10734 if (pg_pairs == 0)
10735 xritag_start = psb->cur_iocbq.sli4_xritag;
10736 sgl_pg_pairs++;
10737 pg_pairs++;
10738 }
10739 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10740 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10741 /* Perform endian conversion if necessary */
10742 sgl->word0 = cpu_to_le32(sgl->word0);
10743
10744 if (!phba->sli4_hba.intr_enable)
10745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10746 else {
10747 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10748 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10749 }
10750 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10751 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10752 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10753 if (rc != MBX_TIMEOUT)
10754 lpfc_sli4_mbox_cmd_free(phba, mbox);
10755 if (shdr_status || shdr_add_status || rc) {
10756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10757 "2564 POST_SGL_BLOCK mailbox command failed "
10758 "status x%x add_status x%x mbx status x%x\n",
10759 shdr_status, shdr_add_status, rc);
10760 rc = -ENXIO;
10761 }
10762 return rc;
10763}
10764
10765/**
10766 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10767 * @phba: pointer to lpfc_hba struct that the frame was received on
10768 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10769 *
10770 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10771 * valid type of frame that the LPFC driver will handle. This function will
10772 * return a zero if the frame is a valid frame or a non zero value when the
10773 * frame does not pass the check.
10774 **/
10775static int
10776lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10777{
10778 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10779 char *type_names[] = FC_TYPE_NAMES_INIT;
10780 struct fc_vft_header *fc_vft_hdr;
10781
10782 switch (fc_hdr->fh_r_ctl) {
10783 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10784 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10785 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10786 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10787 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10788 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10789 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10790 case FC_RCTL_DD_CMD_STATUS: /* command status */
10791 case FC_RCTL_ELS_REQ: /* extended link services request */
10792 case FC_RCTL_ELS_REP: /* extended link services reply */
10793 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10794 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10795 case FC_RCTL_BA_NOP: /* basic link service NOP */
10796 case FC_RCTL_BA_ABTS: /* basic link service abort */
10797 case FC_RCTL_BA_RMC: /* remove connection */
10798 case FC_RCTL_BA_ACC: /* basic accept */
10799 case FC_RCTL_BA_RJT: /* basic reject */
10800 case FC_RCTL_BA_PRMT:
10801 case FC_RCTL_ACK_1: /* acknowledge_1 */
10802 case FC_RCTL_ACK_0: /* acknowledge_0 */
10803 case FC_RCTL_P_RJT: /* port reject */
10804 case FC_RCTL_F_RJT: /* fabric reject */
10805 case FC_RCTL_P_BSY: /* port busy */
10806 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10807 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10808 case FC_RCTL_LCR: /* link credit reset */
10809 case FC_RCTL_END: /* end */
10810 break;
10811 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10812 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10813 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10814 return lpfc_fc_frame_check(phba, fc_hdr);
10815 default:
10816 goto drop;
10817 }
10818 switch (fc_hdr->fh_type) {
10819 case FC_TYPE_BLS:
10820 case FC_TYPE_ELS:
10821 case FC_TYPE_FCP:
10822 case FC_TYPE_CT:
10823 break;
10824 case FC_TYPE_IP:
10825 case FC_TYPE_ILS:
10826 default:
10827 goto drop;
10828 }
10829 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10830 "2538 Received frame rctl:%s type:%s\n",
10831 rctl_names[fc_hdr->fh_r_ctl],
10832 type_names[fc_hdr->fh_type]);
10833 return 0;
10834drop:
10835 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10836 "2539 Dropped frame rctl:%s type:%s\n",
10837 rctl_names[fc_hdr->fh_r_ctl],
10838 type_names[fc_hdr->fh_type]);
10839 return 1;
10840}
10841
10842/**
10843 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10844 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10845 *
10846 * This function processes the FC header to retrieve the VFI from the VF
10847 * header, if one exists. This function will return the VFI if one exists
10848 * or 0 if no VSAN Header exists.
10849 **/
10850static uint32_t
10851lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10852{
10853 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10854
10855 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10856 return 0;
10857 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10858}
10859
10860/**
10861 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10862 * @phba: Pointer to the HBA structure to search for the vport on
10863 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10864 * @fcfi: The FC Fabric ID that the frame came from
10865 *
10866 * This function searches the @phba for a vport that matches the content of the
10867 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10868 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10869 * returns the matching vport pointer or NULL if unable to match frame to a
10870 * vport.
10871 **/
10872static struct lpfc_vport *
10873lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10874 uint16_t fcfi)
10875{
10876 struct lpfc_vport **vports;
10877 struct lpfc_vport *vport = NULL;
10878 int i;
10879 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10880 fc_hdr->fh_d_id[1] << 8 |
10881 fc_hdr->fh_d_id[2]);
10882
10883 vports = lpfc_create_vport_work_array(phba);
10884 if (vports != NULL)
10885 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10886 if (phba->fcf.fcfi == fcfi &&
10887 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10888 vports[i]->fc_myDID == did) {
10889 vport = vports[i];
10890 break;
10891 }
10892 }
10893 lpfc_destroy_vport_work_array(phba, vports);
10894 return vport;
10895}
10896
10897/**
James Smart45ed1192009-10-02 15:17:02 -040010898 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10899 * @vport: The vport to work on.
10900 *
10901 * This function updates the receive sequence time stamp for this vport. The
10902 * receive sequence time stamp indicates the time that the last frame of the
10903 * the sequence that has been idle for the longest amount of time was received.
10904 * the driver uses this time stamp to indicate if any received sequences have
10905 * timed out.
10906 **/
10907void
10908lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10909{
10910 struct lpfc_dmabuf *h_buf;
10911 struct hbq_dmabuf *dmabuf = NULL;
10912
10913 /* get the oldest sequence on the rcv list */
10914 h_buf = list_get_first(&vport->rcv_buffer_list,
10915 struct lpfc_dmabuf, list);
10916 if (!h_buf)
10917 return;
10918 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10919 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10920}
10921
10922/**
10923 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10924 * @vport: The vport that the received sequences were sent to.
10925 *
10926 * This function cleans up all outstanding received sequences. This is called
10927 * by the driver when a link event or user action invalidates all the received
10928 * sequences.
10929 **/
10930void
10931lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10932{
10933 struct lpfc_dmabuf *h_buf, *hnext;
10934 struct lpfc_dmabuf *d_buf, *dnext;
10935 struct hbq_dmabuf *dmabuf = NULL;
10936
10937 /* start with the oldest sequence on the rcv list */
10938 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10939 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10940 list_del_init(&dmabuf->hbuf.list);
10941 list_for_each_entry_safe(d_buf, dnext,
10942 &dmabuf->dbuf.list, list) {
10943 list_del_init(&d_buf->list);
10944 lpfc_in_buf_free(vport->phba, d_buf);
10945 }
10946 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10947 }
10948}
10949
10950/**
10951 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10952 * @vport: The vport that the received sequences were sent to.
10953 *
10954 * This function determines whether any received sequences have timed out by
10955 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
10956 * indicates that there is at least one timed out sequence this routine will
10957 * go through the received sequences one at a time from most inactive to most
10958 * active to determine which ones need to be cleaned up. Once it has determined
10959 * that a sequence needs to be cleaned up it will simply free up the resources
10960 * without sending an abort.
10961 **/
10962void
10963lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
10964{
10965 struct lpfc_dmabuf *h_buf, *hnext;
10966 struct lpfc_dmabuf *d_buf, *dnext;
10967 struct hbq_dmabuf *dmabuf = NULL;
10968 unsigned long timeout;
10969 int abort_count = 0;
10970
10971 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10972 vport->rcv_buffer_time_stamp);
10973 if (list_empty(&vport->rcv_buffer_list) ||
10974 time_before(jiffies, timeout))
10975 return;
10976 /* start with the oldest sequence on the rcv list */
10977 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10978 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10979 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
10980 dmabuf->time_stamp);
10981 if (time_before(jiffies, timeout))
10982 break;
10983 abort_count++;
10984 list_del_init(&dmabuf->hbuf.list);
10985 list_for_each_entry_safe(d_buf, dnext,
10986 &dmabuf->dbuf.list, list) {
10987 list_del_init(&d_buf->list);
10988 lpfc_in_buf_free(vport->phba, d_buf);
10989 }
10990 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10991 }
10992 if (abort_count)
10993 lpfc_update_rcv_time_stamp(vport);
10994}
10995
10996/**
James Smart4f774512009-05-22 14:52:35 -040010997 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10998 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10999 *
11000 * This function searches through the existing incomplete sequences that have
11001 * been sent to this @vport. If the frame matches one of the incomplete
11002 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11003 * make up that sequence. If no sequence is found that matches this frame then
11004 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11005 * This function returns a pointer to the first dmabuf in the sequence list that
11006 * the frame was linked to.
11007 **/
11008static struct hbq_dmabuf *
11009lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11010{
11011 struct fc_frame_header *new_hdr;
11012 struct fc_frame_header *temp_hdr;
11013 struct lpfc_dmabuf *d_buf;
11014 struct lpfc_dmabuf *h_buf;
11015 struct hbq_dmabuf *seq_dmabuf = NULL;
11016 struct hbq_dmabuf *temp_dmabuf = NULL;
11017
James Smart4d9ab992009-10-02 15:16:39 -040011018 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011019 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040011020 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11021 /* Use the hdr_buf to find the sequence that this frame belongs to */
11022 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11023 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11024 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11025 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11026 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11027 continue;
11028 /* found a pending sequence that matches this frame */
11029 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11030 break;
11031 }
11032 if (!seq_dmabuf) {
11033 /*
11034 * This indicates first frame received for this sequence.
11035 * Queue the buffer on the vport's rcv_buffer_list.
11036 */
11037 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040011038 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011039 return dmabuf;
11040 }
11041 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050011042 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11043 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040011044 list_del_init(&seq_dmabuf->hbuf.list);
11045 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11046 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011047 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011048 return dmabuf;
11049 }
James Smart45ed1192009-10-02 15:17:02 -040011050 /* move this sequence to the tail to indicate a young sequence */
11051 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11052 seq_dmabuf->time_stamp = jiffies;
11053 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050011054 if (list_empty(&seq_dmabuf->dbuf.list)) {
11055 temp_hdr = dmabuf->hbuf.virt;
11056 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11057 return seq_dmabuf;
11058 }
James Smart4f774512009-05-22 14:52:35 -040011059 /* find the correct place in the sequence to insert this frame */
11060 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11061 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11062 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11063 /*
11064 * If the frame's sequence count is greater than the frame on
11065 * the list then insert the frame right after this frame
11066 */
James Smarteeead812009-12-21 17:01:23 -050011067 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11068 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040011069 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11070 return seq_dmabuf;
11071 }
11072 }
11073 return NULL;
11074}
11075
11076/**
James Smart6669f9b2009-10-02 15:16:45 -040011077 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11078 * @vport: pointer to a vitural port
11079 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11080 *
11081 * This function tries to abort from the partially assembed sequence, described
11082 * by the information from basic abbort @dmabuf. It checks to see whether such
11083 * partially assembled sequence held by the driver. If so, it shall free up all
11084 * the frames from the partially assembled sequence.
11085 *
11086 * Return
11087 * true -- if there is matching partially assembled sequence present and all
11088 * the frames freed with the sequence;
11089 * false -- if there is no matching partially assembled sequence present so
11090 * nothing got aborted in the lower layer driver
11091 **/
11092static bool
11093lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11094 struct hbq_dmabuf *dmabuf)
11095{
11096 struct fc_frame_header *new_hdr;
11097 struct fc_frame_header *temp_hdr;
11098 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11099 struct hbq_dmabuf *seq_dmabuf = NULL;
11100
11101 /* Use the hdr_buf to find the sequence that matches this frame */
11102 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11103 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11104 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11105 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11106 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11107 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11108 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11109 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11110 continue;
11111 /* found a pending sequence that matches this frame */
11112 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11113 break;
11114 }
11115
11116 /* Free up all the frames from the partially assembled sequence */
11117 if (seq_dmabuf) {
11118 list_for_each_entry_safe(d_buf, n_buf,
11119 &seq_dmabuf->dbuf.list, list) {
11120 list_del_init(&d_buf->list);
11121 lpfc_in_buf_free(vport->phba, d_buf);
11122 }
11123 return true;
11124 }
11125 return false;
11126}
11127
11128/**
11129 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11130 * @phba: Pointer to HBA context object.
11131 * @cmd_iocbq: pointer to the command iocbq structure.
11132 * @rsp_iocbq: pointer to the response iocbq structure.
11133 *
11134 * This function handles the sequence abort accept iocb command complete
11135 * event. It properly releases the memory allocated to the sequence abort
11136 * accept iocb.
11137 **/
11138static void
11139lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11140 struct lpfc_iocbq *cmd_iocbq,
11141 struct lpfc_iocbq *rsp_iocbq)
11142{
11143 if (cmd_iocbq)
11144 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11145}
11146
11147/**
11148 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11149 * @phba: Pointer to HBA context object.
11150 * @fc_hdr: pointer to a FC frame header.
11151 *
11152 * This function sends a basic accept to a previous unsol sequence abort
11153 * event after aborting the sequence handling.
11154 **/
11155static void
11156lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11157 struct fc_frame_header *fc_hdr)
11158{
11159 struct lpfc_iocbq *ctiocb = NULL;
11160 struct lpfc_nodelist *ndlp;
James Smart5ffc2662009-11-18 15:39:44 -050011161 uint16_t oxid, rxid;
11162 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011163 IOCB_t *icmd;
11164
11165 if (!lpfc_is_link_up(phba))
11166 return;
11167
11168 sid = sli4_sid_from_fc_hdr(fc_hdr);
11169 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050011170 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040011171
11172 ndlp = lpfc_findnode_did(phba->pport, sid);
11173 if (!ndlp) {
11174 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11175 "1268 Find ndlp returned NULL for oxid:x%x "
11176 "SID:x%x\n", oxid, sid);
11177 return;
11178 }
11179
11180 /* Allocate buffer for acc iocb */
11181 ctiocb = lpfc_sli_get_iocbq(phba);
11182 if (!ctiocb)
11183 return;
11184
James Smart5ffc2662009-11-18 15:39:44 -050011185 /* Extract the F_CTL field from FC_HDR */
11186 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11187
James Smart6669f9b2009-10-02 15:16:45 -040011188 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040011189 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050011190 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040011191 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11192 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11193 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11194
11195 /* Fill in the rest of iocb fields */
11196 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11197 icmd->ulpBdeCount = 0;
11198 icmd->ulpLe = 1;
11199 icmd->ulpClass = CLASS3;
11200 icmd->ulpContext = ndlp->nlp_rpi;
James Smart6669f9b2009-10-02 15:16:45 -040011201
James Smart6669f9b2009-10-02 15:16:45 -040011202 ctiocb->iocb_cmpl = NULL;
11203 ctiocb->vport = phba->pport;
11204 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11205
James Smart5ffc2662009-11-18 15:39:44 -050011206 if (fctl & FC_FC_EX_CTX) {
11207 /* ABTS sent by responder to CT exchange, construction
11208 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11209 * field and RX_ID from ABTS for RX_ID field.
11210 */
11211 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11212 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11213 ctiocb->sli4_xritag = oxid;
11214 } else {
11215 /* ABTS sent by initiator to CT exchange, construction
11216 * of BA_ACC will need to allocate a new XRI as for the
11217 * XRI_TAG and RX_ID fields.
11218 */
11219 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11220 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11221 ctiocb->sli4_xritag = NO_XRI;
11222 }
11223 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11224
James Smart6669f9b2009-10-02 15:16:45 -040011225 /* Xmit CT abts accept on exchange <xid> */
11226 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11227 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11228 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11229 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11230}
11231
11232/**
11233 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11234 * @vport: Pointer to the vport on which this sequence was received
11235 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11236 *
11237 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11238 * receive sequence is only partially assembed by the driver, it shall abort
11239 * the partially assembled frames for the sequence. Otherwise, if the
11240 * unsolicited receive sequence has been completely assembled and passed to
11241 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11242 * unsolicited sequence has been aborted. After that, it will issue a basic
11243 * accept to accept the abort.
11244 **/
11245void
11246lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11247 struct hbq_dmabuf *dmabuf)
11248{
11249 struct lpfc_hba *phba = vport->phba;
11250 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050011251 uint32_t fctl;
James Smart6669f9b2009-10-02 15:16:45 -040011252 bool abts_par;
11253
James Smart6669f9b2009-10-02 15:16:45 -040011254 /* Make a copy of fc_hdr before the dmabuf being released */
11255 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050011256 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040011257
James Smart5ffc2662009-11-18 15:39:44 -050011258 if (fctl & FC_FC_EX_CTX) {
11259 /*
11260 * ABTS sent by responder to exchange, just free the buffer
11261 */
James Smart6669f9b2009-10-02 15:16:45 -040011262 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smart5ffc2662009-11-18 15:39:44 -050011263 } else {
11264 /*
11265 * ABTS sent by initiator to exchange, need to do cleanup
11266 */
11267 /* Try to abort partially assembled seq */
11268 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11269
11270 /* Send abort to ULP if partially seq abort failed */
11271 if (abts_par == false)
11272 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11273 else
11274 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11275 }
James Smart6669f9b2009-10-02 15:16:45 -040011276 /* Send basic accept (BA_ACC) to the abort requester */
11277 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11278}
11279
11280/**
James Smart4f774512009-05-22 14:52:35 -040011281 * lpfc_seq_complete - Indicates if a sequence is complete
11282 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11283 *
11284 * This function checks the sequence, starting with the frame described by
11285 * @dmabuf, to see if all the frames associated with this sequence are present.
11286 * the frames associated with this sequence are linked to the @dmabuf using the
11287 * dbuf list. This function looks for two major things. 1) That the first frame
11288 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11289 * set. 3) That there are no holes in the sequence count. The function will
11290 * return 1 when the sequence is complete, otherwise it will return 0.
11291 **/
11292static int
11293lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11294{
11295 struct fc_frame_header *hdr;
11296 struct lpfc_dmabuf *d_buf;
11297 struct hbq_dmabuf *seq_dmabuf;
11298 uint32_t fctl;
11299 int seq_count = 0;
11300
11301 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11302 /* make sure first fame of sequence has a sequence count of zero */
11303 if (hdr->fh_seq_cnt != seq_count)
11304 return 0;
11305 fctl = (hdr->fh_f_ctl[0] << 16 |
11306 hdr->fh_f_ctl[1] << 8 |
11307 hdr->fh_f_ctl[2]);
11308 /* If last frame of sequence we can return success. */
11309 if (fctl & FC_FC_END_SEQ)
11310 return 1;
11311 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11312 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11313 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11314 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050011315 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040011316 return 0;
11317 fctl = (hdr->fh_f_ctl[0] << 16 |
11318 hdr->fh_f_ctl[1] << 8 |
11319 hdr->fh_f_ctl[2]);
11320 /* If last frame of sequence we can return success. */
11321 if (fctl & FC_FC_END_SEQ)
11322 return 1;
11323 }
11324 return 0;
11325}
11326
11327/**
11328 * lpfc_prep_seq - Prep sequence for ULP processing
11329 * @vport: Pointer to the vport on which this sequence was received
11330 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11331 *
11332 * This function takes a sequence, described by a list of frames, and creates
11333 * a list of iocbq structures to describe the sequence. This iocbq list will be
11334 * used to issue to the generic unsolicited sequence handler. This routine
11335 * returns a pointer to the first iocbq in the list. If the function is unable
11336 * to allocate an iocbq then it throw out the received frames that were not
11337 * able to be described and return a pointer to the first iocbq. If unable to
11338 * allocate any iocbqs (including the first) this function will return NULL.
11339 **/
11340static struct lpfc_iocbq *
11341lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11342{
11343 struct lpfc_dmabuf *d_buf, *n_buf;
11344 struct lpfc_iocbq *first_iocbq, *iocbq;
11345 struct fc_frame_header *fc_hdr;
11346 uint32_t sid;
James Smarteeead812009-12-21 17:01:23 -050011347 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040011348
11349 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11350 /* remove from receive buffer list */
11351 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040011352 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040011353 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011354 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011355 /* Get an iocbq struct to fill in. */
11356 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11357 if (first_iocbq) {
11358 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011359 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011360 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11361 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11362 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11363 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11364 vport->vpi + vport->phba->vpi_base;
11365 /* put the first buffer into the first IOCBq */
11366 first_iocbq->context2 = &seq_dmabuf->dbuf;
11367 first_iocbq->context3 = NULL;
11368 first_iocbq->iocb.ulpBdeCount = 1;
11369 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11370 LPFC_DATA_BUF_SIZE;
11371 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011372 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011373 bf_get(lpfc_rcqe_length,
11374 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011375 }
11376 iocbq = first_iocbq;
11377 /*
11378 * Each IOCBq can have two Buffers assigned, so go through the list
11379 * of buffers for this sequence and save two buffers in each IOCBq
11380 */
11381 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11382 if (!iocbq) {
11383 lpfc_in_buf_free(vport->phba, d_buf);
11384 continue;
11385 }
11386 if (!iocbq->context3) {
11387 iocbq->context3 = d_buf;
11388 iocbq->iocb.ulpBdeCount++;
James Smarteeead812009-12-21 17:01:23 -050011389 pbde = (struct ulp_bde64 *)
11390 &iocbq->iocb.unsli3.sli3Words[4];
11391 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011392 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011393 bf_get(lpfc_rcqe_length,
11394 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011395 } else {
11396 iocbq = lpfc_sli_get_iocbq(vport->phba);
11397 if (!iocbq) {
11398 if (first_iocbq) {
11399 first_iocbq->iocb.ulpStatus =
11400 IOSTAT_FCP_RSP_ERROR;
11401 first_iocbq->iocb.un.ulpWord[4] =
11402 IOERR_NO_RESOURCES;
11403 }
11404 lpfc_in_buf_free(vport->phba, d_buf);
11405 continue;
11406 }
11407 iocbq->context2 = d_buf;
11408 iocbq->context3 = NULL;
11409 iocbq->iocb.ulpBdeCount = 1;
11410 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11411 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011412 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011413 bf_get(lpfc_rcqe_length,
11414 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011415 iocbq->iocb.un.rcvels.remoteID = sid;
11416 list_add_tail(&iocbq->list, &first_iocbq->list);
11417 }
11418 }
11419 return first_iocbq;
11420}
11421
James Smart6669f9b2009-10-02 15:16:45 -040011422static void
11423lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11424 struct hbq_dmabuf *seq_dmabuf)
11425{
11426 struct fc_frame_header *fc_hdr;
11427 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11428 struct lpfc_hba *phba = vport->phba;
11429
11430 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11431 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11432 if (!iocbq) {
11433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11434 "2707 Ring %d handler: Failed to allocate "
11435 "iocb Rctl x%x Type x%x received\n",
11436 LPFC_ELS_RING,
11437 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11438 return;
11439 }
11440 if (!lpfc_complete_unsol_iocb(phba,
11441 &phba->sli.ring[LPFC_ELS_RING],
11442 iocbq, fc_hdr->fh_r_ctl,
11443 fc_hdr->fh_type))
11444 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11445 "2540 Ring %d handler: unexpected Rctl "
11446 "x%x Type x%x received\n",
11447 LPFC_ELS_RING,
11448 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11449
11450 /* Free iocb created in lpfc_prep_seq */
11451 list_for_each_entry_safe(curr_iocb, next_iocb,
11452 &iocbq->list, list) {
11453 list_del_init(&curr_iocb->list);
11454 lpfc_sli_release_iocbq(phba, curr_iocb);
11455 }
11456 lpfc_sli_release_iocbq(phba, iocbq);
11457}
11458
James Smart4f774512009-05-22 14:52:35 -040011459/**
11460 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11461 * @phba: Pointer to HBA context object.
11462 *
11463 * This function is called with no lock held. This function processes all
11464 * the received buffers and gives it to upper layers when a received buffer
11465 * indicates that it is the final frame in the sequence. The interrupt
11466 * service routine processes received buffers at interrupt contexts and adds
11467 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11468 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11469 * appropriate receive function when the final frame in a sequence is received.
11470 **/
James Smart4d9ab992009-10-02 15:16:39 -040011471void
11472lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11473 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011474{
James Smart4d9ab992009-10-02 15:16:39 -040011475 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011476 struct fc_frame_header *fc_hdr;
11477 struct lpfc_vport *vport;
11478 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011479
James Smart4f774512009-05-22 14:52:35 -040011480 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011481 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11482 /* check to see if this a valid type of frame */
11483 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11484 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11485 return;
11486 }
11487 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11488 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smartc8685952009-11-18 15:39:16 -050011489 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
James Smart4d9ab992009-10-02 15:16:39 -040011490 /* throw out the frame */
11491 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11492 return;
11493 }
James Smart6669f9b2009-10-02 15:16:45 -040011494 /* Handle the basic abort sequence (BA_ABTS) event */
11495 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11496 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11497 return;
11498 }
11499
James Smart4d9ab992009-10-02 15:16:39 -040011500 /* Link this frame */
11501 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11502 if (!seq_dmabuf) {
11503 /* unable to add frame to vport - throw it out */
11504 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11505 return;
11506 }
11507 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050011508 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040011509 return;
James Smartdef9c7a2009-12-21 17:02:28 -050011510
James Smart6669f9b2009-10-02 15:16:45 -040011511 /* Send the complete sequence to the upper layer protocol */
11512 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011513}
James Smart6fb120a2009-05-22 14:52:59 -040011514
11515/**
11516 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11517 * @phba: pointer to lpfc hba data structure.
11518 *
11519 * This routine is invoked to post rpi header templates to the
11520 * HBA consistent with the SLI-4 interface spec. This routine
11521 * posts a PAGE_SIZE memory region to the port to hold up to
11522 * PAGE_SIZE modulo 64 rpi context headers.
11523 *
11524 * This routine does not require any locks. It's usage is expected
11525 * to be driver load or reset recovery when the driver is
11526 * sequential.
11527 *
11528 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011529 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011530 * EIO - The mailbox failed to complete successfully.
11531 * When this error occurs, the driver is not guaranteed
11532 * to have any rpi regions posted to the device and
11533 * must either attempt to repost the regions or take a
11534 * fatal error.
11535 **/
11536int
11537lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11538{
11539 struct lpfc_rpi_hdr *rpi_page;
11540 uint32_t rc = 0;
11541
11542 /* Post all rpi memory regions to the port. */
11543 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11544 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11545 if (rc != MBX_SUCCESS) {
11546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11547 "2008 Error %d posting all rpi "
11548 "headers\n", rc);
11549 rc = -EIO;
11550 break;
11551 }
11552 }
11553
11554 return rc;
11555}
11556
11557/**
11558 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11559 * @phba: pointer to lpfc hba data structure.
11560 * @rpi_page: pointer to the rpi memory region.
11561 *
11562 * This routine is invoked to post a single rpi header to the
11563 * HBA consistent with the SLI-4 interface spec. This memory region
11564 * maps up to 64 rpi context regions.
11565 *
11566 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011567 * 0 - successful
James Smart6fb120a2009-05-22 14:52:59 -040011568 * ENOMEM - No available memory
11569 * EIO - The mailbox failed to complete successfully.
11570 **/
11571int
11572lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11573{
11574 LPFC_MBOXQ_t *mboxq;
11575 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11576 uint32_t rc = 0;
11577 uint32_t mbox_tmo;
11578 uint32_t shdr_status, shdr_add_status;
11579 union lpfc_sli4_cfg_shdr *shdr;
11580
11581 /* The port is notified of the header region via a mailbox command. */
11582 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11583 if (!mboxq) {
11584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11585 "2001 Unable to allocate memory for issuing "
11586 "SLI_CONFIG_SPECIAL mailbox command\n");
11587 return -ENOMEM;
11588 }
11589
11590 /* Post all rpi memory regions to the port. */
11591 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11592 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11593 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11594 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11595 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11596 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11597 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11598 hdr_tmpl, rpi_page->page_count);
11599 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11600 rpi_page->start_rpi);
11601 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11602 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011603 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011604 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11605 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11606 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11607 if (rc != MBX_TIMEOUT)
11608 mempool_free(mboxq, phba->mbox_mem_pool);
11609 if (shdr_status || shdr_add_status || rc) {
11610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11611 "2514 POST_RPI_HDR mailbox failed with "
11612 "status x%x add_status x%x, mbx status x%x\n",
11613 shdr_status, shdr_add_status, rc);
11614 rc = -ENXIO;
11615 }
11616 return rc;
11617}
11618
11619/**
11620 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11621 * @phba: pointer to lpfc hba data structure.
11622 *
11623 * This routine is invoked to post rpi header templates to the
11624 * HBA consistent with the SLI-4 interface spec. This routine
11625 * posts a PAGE_SIZE memory region to the port to hold up to
11626 * PAGE_SIZE modulo 64 rpi context headers.
11627 *
11628 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020011629 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040011630 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11631 **/
11632int
11633lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11634{
11635 int rpi;
11636 uint16_t max_rpi, rpi_base, rpi_limit;
11637 uint16_t rpi_remaining;
11638 struct lpfc_rpi_hdr *rpi_hdr;
11639
11640 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11641 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11642 rpi_limit = phba->sli4_hba.next_rpi;
11643
11644 /*
11645 * The valid rpi range is not guaranteed to be zero-based. Start
11646 * the search at the rpi_base as reported by the port.
11647 */
11648 spin_lock_irq(&phba->hbalock);
11649 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11650 if (rpi >= rpi_limit || rpi < rpi_base)
11651 rpi = LPFC_RPI_ALLOC_ERROR;
11652 else {
11653 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11654 phba->sli4_hba.max_cfg_param.rpi_used++;
11655 phba->sli4_hba.rpi_count++;
11656 }
11657
11658 /*
11659 * Don't try to allocate more rpi header regions if the device limit
11660 * on available rpis max has been exhausted.
11661 */
11662 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11663 (phba->sli4_hba.rpi_count >= max_rpi)) {
11664 spin_unlock_irq(&phba->hbalock);
11665 return rpi;
11666 }
11667
11668 /*
11669 * If the driver is running low on rpi resources, allocate another
11670 * page now. Note that the next_rpi value is used because
11671 * it represents how many are actually in use whereas max_rpi notes
11672 * how many are supported max by the device.
11673 */
11674 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11675 phba->sli4_hba.rpi_count;
11676 spin_unlock_irq(&phba->hbalock);
11677 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11678 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11679 if (!rpi_hdr) {
11680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11681 "2002 Error Could not grow rpi "
11682 "count\n");
11683 } else {
11684 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11685 }
11686 }
11687
11688 return rpi;
11689}
11690
11691/**
11692 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11693 * @phba: pointer to lpfc hba data structure.
11694 *
11695 * This routine is invoked to release an rpi to the pool of
11696 * available rpis maintained by the driver.
11697 **/
11698void
11699lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11700{
11701 spin_lock_irq(&phba->hbalock);
11702 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11703 phba->sli4_hba.rpi_count--;
11704 phba->sli4_hba.max_cfg_param.rpi_used--;
11705 spin_unlock_irq(&phba->hbalock);
11706}
11707
11708/**
11709 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11710 * @phba: pointer to lpfc hba data structure.
11711 *
11712 * This routine is invoked to remove the memory region that
11713 * provided rpi via a bitmask.
11714 **/
11715void
11716lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11717{
11718 kfree(phba->sli4_hba.rpi_bmask);
11719}
11720
11721/**
11722 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11723 * @phba: pointer to lpfc hba data structure.
11724 *
11725 * This routine is invoked to remove the memory region that
11726 * provided rpi via a bitmask.
11727 **/
11728int
11729lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11730{
11731 LPFC_MBOXQ_t *mboxq;
11732 struct lpfc_hba *phba = ndlp->phba;
11733 int rc;
11734
11735 /* The port is notified of the header region via a mailbox command. */
11736 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11737 if (!mboxq)
11738 return -ENOMEM;
11739
11740 /* Post all rpi memory regions to the port. */
11741 lpfc_resume_rpi(mboxq, ndlp);
11742 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11743 if (rc == MBX_NOT_FINISHED) {
11744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11745 "2010 Resume RPI Mailbox failed "
11746 "status %d, mbxStatus x%x\n", rc,
11747 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11748 mempool_free(mboxq, phba->mbox_mem_pool);
11749 return -EIO;
11750 }
11751 return 0;
11752}
11753
11754/**
11755 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11756 * @phba: pointer to lpfc hba data structure.
11757 * @vpi: vpi value to activate with the port.
11758 *
11759 * This routine is invoked to activate a vpi with the
11760 * port when the host intends to use vports with a
11761 * nonzero vpi.
11762 *
11763 * Returns:
11764 * 0 success
11765 * -Evalue otherwise
11766 **/
11767int
11768lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11769{
11770 LPFC_MBOXQ_t *mboxq;
11771 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040011772 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040011773 uint32_t mbox_tmo;
11774
11775 if (vpi == 0)
11776 return -EINVAL;
11777 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11778 if (!mboxq)
11779 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011780 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011781 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11782 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040011783 if (rc != MBX_SUCCESS) {
11784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11785 "2022 INIT VPI Mailbox failed "
11786 "status %d, mbxStatus x%x\n", rc,
11787 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040011788 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040011789 }
James Smart6a9c52c2009-10-02 15:16:51 -040011790 if (rc != MBX_TIMEOUT)
11791 mempool_free(mboxq, phba->mbox_mem_pool);
11792
11793 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040011794}
11795
11796/**
11797 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11798 * @phba: pointer to lpfc hba data structure.
11799 * @mboxq: Pointer to mailbox object.
11800 *
11801 * This routine is invoked to manually add a single FCF record. The caller
11802 * must pass a completely initialized FCF_Record. This routine takes
11803 * care of the nonembedded mailbox operations.
11804 **/
11805static void
11806lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11807{
11808 void *virt_addr;
11809 union lpfc_sli4_cfg_shdr *shdr;
11810 uint32_t shdr_status, shdr_add_status;
11811
11812 virt_addr = mboxq->sge_array->addr[0];
11813 /* The IOCTL status is embedded in the mailbox subheader. */
11814 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11815 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11816 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11817
11818 if ((shdr_status || shdr_add_status) &&
11819 (shdr_status != STATUS_FCF_IN_USE))
11820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11821 "2558 ADD_FCF_RECORD mailbox failed with "
11822 "status x%x add_status x%x\n",
11823 shdr_status, shdr_add_status);
11824
11825 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11826}
11827
11828/**
11829 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11830 * @phba: pointer to lpfc hba data structure.
11831 * @fcf_record: pointer to the initialized fcf record to add.
11832 *
11833 * This routine is invoked to manually add a single FCF record. The caller
11834 * must pass a completely initialized FCF_Record. This routine takes
11835 * care of the nonembedded mailbox operations.
11836 **/
11837int
11838lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11839{
11840 int rc = 0;
11841 LPFC_MBOXQ_t *mboxq;
11842 uint8_t *bytep;
11843 void *virt_addr;
11844 dma_addr_t phys_addr;
11845 struct lpfc_mbx_sge sge;
11846 uint32_t alloc_len, req_len;
11847 uint32_t fcfindex;
11848
11849 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11850 if (!mboxq) {
11851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11852 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11853 return -ENOMEM;
11854 }
11855
11856 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11857 sizeof(uint32_t);
11858
11859 /* Allocate DMA memory and set up the non-embedded mailbox command */
11860 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11861 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11862 req_len, LPFC_SLI4_MBX_NEMBED);
11863 if (alloc_len < req_len) {
11864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11865 "2523 Allocated DMA memory size (x%x) is "
11866 "less than the requested DMA memory "
11867 "size (x%x)\n", alloc_len, req_len);
11868 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11869 return -ENOMEM;
11870 }
11871
11872 /*
11873 * Get the first SGE entry from the non-embedded DMA memory. This
11874 * routine only uses a single SGE.
11875 */
11876 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11877 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
James Smart6fb120a2009-05-22 14:52:59 -040011878 virt_addr = mboxq->sge_array->addr[0];
11879 /*
11880 * Configure the FCF record for FCFI 0. This is the driver's
11881 * hardcoded default and gets used in nonFIP mode.
11882 */
11883 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11884 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11885 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11886
11887 /*
11888 * Copy the fcf_index and the FCF Record Data. The data starts after
11889 * the FCoE header plus word10. The data copy needs to be endian
11890 * correct.
11891 */
11892 bytep += sizeof(uint32_t);
11893 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11894 mboxq->vport = phba->pport;
11895 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11896 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11897 if (rc == MBX_NOT_FINISHED) {
11898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11899 "2515 ADD_FCF_RECORD mailbox failed with "
11900 "status 0x%x\n", rc);
11901 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11902 rc = -EIO;
11903 } else
11904 rc = 0;
11905
11906 return rc;
11907}
11908
11909/**
11910 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11911 * @phba: pointer to lpfc hba data structure.
11912 * @fcf_record: pointer to the fcf record to write the default data.
11913 * @fcf_index: FCF table entry index.
11914 *
11915 * This routine is invoked to build the driver's default FCF record. The
11916 * values used are hardcoded. This routine handles memory initialization.
11917 *
11918 **/
11919void
11920lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11921 struct fcf_record *fcf_record,
11922 uint16_t fcf_index)
11923{
11924 memset(fcf_record, 0, sizeof(struct fcf_record));
11925 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11926 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11927 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11928 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11929 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11930 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11931 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11932 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11933 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11934 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11935 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11936 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11937 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011938 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011939 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11940 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11941 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11942 /* Set the VLAN bit map */
11943 if (phba->valid_vlan) {
11944 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11945 = 1 << (phba->vlan_id % 8);
11946 }
11947}
11948
11949/**
11950 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11951 * @phba: pointer to lpfc hba data structure.
11952 * @fcf_index: FCF table entry offset.
11953 *
11954 * This routine is invoked to read up to @fcf_num of FCF record from the
11955 * device starting with the given @fcf_index.
11956 **/
11957int
11958lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11959{
11960 int rc = 0, error;
11961 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040011962
James Smart32b97932009-07-19 10:01:21 -040011963 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040011964 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11965 if (!mboxq) {
11966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11967 "2000 Failed to allocate mbox for "
11968 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040011969 error = -ENOMEM;
11970 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011971 }
James Smartecfd03c2010-02-12 14:41:27 -050011972 /* Construct the read FCF record mailbox command */
11973 rc = lpfc_sli4_mbx_read_fcf_record(phba, mboxq, fcf_index);
11974 if (rc) {
11975 error = -EINVAL;
James Smart4d9ab992009-10-02 15:16:39 -040011976 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011977 }
James Smartecfd03c2010-02-12 14:41:27 -050011978 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040011979 mboxq->vport = phba->pport;
11980 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11981 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050011982 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040011983 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050011984 else {
James Smart32b97932009-07-19 10:01:21 -040011985 spin_lock_irq(&phba->hbalock);
11986 phba->hba_flag |= FCF_DISC_INPROGRESS;
11987 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040011988 error = 0;
James Smart32b97932009-07-19 10:01:21 -040011989 }
James Smart4d9ab992009-10-02 15:16:39 -040011990fail_fcfscan:
11991 if (error) {
11992 if (mboxq)
11993 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11994 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11995 spin_lock_irq(&phba->hbalock);
11996 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11997 spin_unlock_irq(&phba->hbalock);
11998 }
James Smart6fb120a2009-05-22 14:52:59 -040011999 return error;
12000}
James Smarta0c87cb2009-07-19 10:01:10 -040012001
12002/**
James Smartecfd03c2010-02-12 14:41:27 -050012003 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12004 * @phba: pointer to lpfc hba data structure.
12005 *
12006 * This routine is the completion routine for the rediscover FCF table mailbox
12007 * command. If the mailbox command returned failure, it will try to stop the
12008 * FCF rediscover wait timer.
12009 **/
12010void
12011lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12012{
12013 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12014 uint32_t shdr_status, shdr_add_status;
12015
12016 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12017
12018 shdr_status = bf_get(lpfc_mbox_hdr_status,
12019 &redisc_fcf->header.cfg_shdr.response);
12020 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12021 &redisc_fcf->header.cfg_shdr.response);
12022 if (shdr_status || shdr_add_status) {
12023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12024 "2746 Requesting for FCF rediscovery failed "
12025 "status x%x add_status x%x\n",
12026 shdr_status, shdr_add_status);
12027 /*
12028 * Request failed, last resort to re-try current
12029 * registered FCF entry
12030 */
12031 lpfc_retry_pport_discovery(phba);
12032 } else
12033 /*
12034 * Start FCF rediscovery wait timer for pending FCF
12035 * before rescan FCF record table.
12036 */
12037 lpfc_fcf_redisc_wait_start_timer(phba);
12038
12039 mempool_free(mbox, phba->mbox_mem_pool);
12040}
12041
12042/**
12043 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12044 * @phba: pointer to lpfc hba data structure.
12045 *
12046 * This routine is invoked to request for rediscovery of the entire FCF table
12047 * by the port.
12048 **/
12049int
12050lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12051{
12052 LPFC_MBOXQ_t *mbox;
12053 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12054 int rc, length;
12055
12056 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12057 if (!mbox) {
12058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12059 "2745 Failed to allocate mbox for "
12060 "requesting FCF rediscover.\n");
12061 return -ENOMEM;
12062 }
12063
12064 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12065 sizeof(struct lpfc_sli4_cfg_mhdr));
12066 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12067 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12068 length, LPFC_SLI4_MBX_EMBED);
12069
12070 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12071 /* Set count to 0 for invalidating the entire FCF database */
12072 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12073
12074 /* Issue the mailbox command asynchronously */
12075 mbox->vport = phba->pport;
12076 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12077 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12078
12079 if (rc == MBX_NOT_FINISHED) {
12080 mempool_free(mbox, phba->mbox_mem_pool);
12081 return -EIO;
12082 }
12083 return 0;
12084}
12085
12086/**
James Smarta0c87cb2009-07-19 10:01:10 -040012087 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12088 * @phba: pointer to lpfc hba data structure.
12089 *
12090 * This function read region 23 and parse TLV for port status to
12091 * decide if the user disaled the port. If the TLV indicates the
12092 * port is disabled, the hba_flag is set accordingly.
12093 **/
12094void
12095lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12096{
12097 LPFC_MBOXQ_t *pmb = NULL;
12098 MAILBOX_t *mb;
12099 uint8_t *rgn23_data = NULL;
12100 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12101 int rc;
12102
12103 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12104 if (!pmb) {
12105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12106 "2600 lpfc_sli_read_serdes_param failed to"
12107 " allocate mailbox memory\n");
12108 goto out;
12109 }
12110 mb = &pmb->u.mb;
12111
12112 /* Get adapter Region 23 data */
12113 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12114 if (!rgn23_data)
12115 goto out;
12116
12117 do {
12118 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12119 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12120
12121 if (rc != MBX_SUCCESS) {
12122 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12123 "2601 lpfc_sli_read_link_ste failed to"
12124 " read config region 23 rc 0x%x Status 0x%x\n",
12125 rc, mb->mbxStatus);
12126 mb->un.varDmp.word_cnt = 0;
12127 }
12128 /*
12129 * dump mem may return a zero when finished or we got a
12130 * mailbox error, either way we are done.
12131 */
12132 if (mb->un.varDmp.word_cnt == 0)
12133 break;
12134 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12135 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12136
12137 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12138 rgn23_data + offset,
12139 mb->un.varDmp.word_cnt);
12140 offset += mb->un.varDmp.word_cnt;
12141 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12142
12143 data_size = offset;
12144 offset = 0;
12145
12146 if (!data_size)
12147 goto out;
12148
12149 /* Check the region signature first */
12150 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12152 "2619 Config region 23 has bad signature\n");
12153 goto out;
12154 }
12155 offset += 4;
12156
12157 /* Check the data structure version */
12158 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12160 "2620 Config region 23 has bad version\n");
12161 goto out;
12162 }
12163 offset += 4;
12164
12165 /* Parse TLV entries in the region */
12166 while (offset < data_size) {
12167 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12168 break;
12169 /*
12170 * If the TLV is not driver specific TLV or driver id is
12171 * not linux driver id, skip the record.
12172 */
12173 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12174 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12175 (rgn23_data[offset + 3] != 0)) {
12176 offset += rgn23_data[offset + 1] * 4 + 4;
12177 continue;
12178 }
12179
12180 /* Driver found a driver specific TLV in the config region */
12181 sub_tlv_len = rgn23_data[offset + 1] * 4;
12182 offset += 4;
12183 tlv_offset = 0;
12184
12185 /*
12186 * Search for configured port state sub-TLV.
12187 */
12188 while ((offset < data_size) &&
12189 (tlv_offset < sub_tlv_len)) {
12190 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12191 offset += 4;
12192 tlv_offset += 4;
12193 break;
12194 }
12195 if (rgn23_data[offset] != PORT_STE_TYPE) {
12196 offset += rgn23_data[offset + 1] * 4 + 4;
12197 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12198 continue;
12199 }
12200
12201 /* This HBA contains PORT_STE configured */
12202 if (!rgn23_data[offset + 2])
12203 phba->hba_flag |= LINK_DISABLED;
12204
12205 goto out;
12206 }
12207 }
12208out:
12209 if (pmb)
12210 mempool_free(pmb, phba->mbox_mem_pool);
12211 kfree(rgn23_data);
12212 return;
12213}
James Smart695a8142010-01-26 23:08:03 -050012214
12215/**
12216 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12217 * @vport: pointer to vport data structure.
12218 *
12219 * This function iterate through the mailboxq and clean up all REG_LOGIN
12220 * and REG_VPI mailbox commands associated with the vport. This function
12221 * is called when driver want to restart discovery of the vport due to
12222 * a Clear Virtual Link event.
12223 **/
12224void
12225lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12226{
12227 struct lpfc_hba *phba = vport->phba;
12228 LPFC_MBOXQ_t *mb, *nextmb;
12229 struct lpfc_dmabuf *mp;
12230
12231 spin_lock_irq(&phba->hbalock);
12232 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12233 if (mb->vport != vport)
12234 continue;
12235
12236 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12237 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12238 continue;
12239
12240 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12241 mp = (struct lpfc_dmabuf *) (mb->context1);
12242 if (mp) {
12243 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12244 kfree(mp);
12245 }
12246 }
12247 list_del(&mb->list);
12248 mempool_free(mb, phba->mbox_mem_pool);
12249 }
12250 mb = phba->sli.mbox_active;
12251 if (mb && (mb->vport == vport)) {
12252 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12253 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12254 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12255 }
12256 spin_unlock_irq(&phba->hbalock);
12257}
12258